merge pr-607 (#607 feat(git): 工作区根为仓库时仍探测并展示子目录独立仓库) into dev(@iasiv5)

This commit is contained in:
Menghuan1918 committed 2026-10-16 16:33:40 +08:00
commit 15665f1ff3
2 files changed
+124 -18

No files matched your search

+41 -18
View File
@@ -513,6 +513,12 @@ export async function blame(
* A home-directory cwd can hold hundreds of visible folders (Library, iCloud
* mounts…); probing them all serially is what froze the panel in #369. */
const DISCOVERY_LIMIT = 200
/** How many BFS levels the nested-repo discovery descends below cwd; repo
* roots up to depth DISCOVERY_MAX_DEPTH - 1 are detected (level d holds
* depth-(d-1) dirs, whose entries are read for `.git`). 4 is the minimum
* useful value. Cost stays bounded by the depth ≤ 3 directory count; the
* depth-4 frontier is collected but never expanded. */
const DISCOVERY_MAX_DEPTH = 4
/** Per-probe and direct-discovery budget. `rev-parse` is millisecond-scale on
* a healthy checkout; a probe that needs longer is a stalled mount and is
* better abandoned than waited on. */
@@ -543,9 +549,10 @@ async function directRepoRoot(cwd: string): Promise<string> {
return out.trim()
}
/** Discover the current repository or direct child repositories. Results are
* cached per cwd and concurrent callers share one in-flight scan, so opening
* the panel (three parallel git.* requests) costs a single discovery pass. */
/** Discover the current repository plus child repositories nested up to
* DISCOVERY_MAX_DEPTH - 1 levels below cwd. Results are cached per cwd and
* concurrent callers share one in-flight scan, so opening the panel (three
* parallel git.* requests) costs a single discovery pass. */
export function repoRoots(cwd: string): Promise<string[]> {
const cached = repoRootsCache.get(cwd)
if (cached !== undefined && cached.expires > Date.now()) return Promise.resolve(cached.roots)
@@ -566,25 +573,41 @@ export function repoRoots(cwd: string): Promise<string[]> {
return promise
}
/** Breadth-first scan below `cwd`: one `readdir` per visited directory, no
* per-child git process (dirent `.git` presence — directory, worktree file
* or symlink form — decides). Keeps the serial `rev-parse`-per-child storm
* that froze the panel in #369 structurally impossible. The scan descends
* into repositories it finds, so repos embedded inside other repos (vendored
* checkouts that are not submodules) are discovered too. */
async function discoverRepoRoots(cwd: string): Promise<string[]> {
const roots: string[] = []
try {
return [await directRepoRoot(cwd)]
roots.push(await directRepoRoot(cwd))
} catch {
const entries = await readdir(cwd, { withFileTypes: true }).catch(() => [])
const roots: string[] = []
for (const entry of entries
.filter(entry => entry.isDirectory() && !entry.name.startsWith('.') && entry.name !== 'node_modules')
.sort((left, right) => left.name.localeCompare(right.name))
.slice(0, DISCOVERY_LIMIT)) {
try {
const root = await directRepoRoot(join(cwd, entry.name))
if (!roots.some(existing => pathIdentity(existing) === pathIdentity(root))) roots.push(root)
} catch {
// Ordinary child directory; keep discovering sibling repositories.
}
}
return roots
// Current directory is not a Git repository root.
}
let level = [cwd]
for (let depth = 1; depth <= DISCOVERY_MAX_DEPTH && level.length > 0; depth++) {
const next: string[] = []
for (const dir of level.slice(0, DISCOVERY_LIMIT)) {
const entries = await readdir(dir, { withFileTypes: true }).catch(() => [])
let hasGit = false
const subdirs: string[] = []
for (const entry of entries) {
if (entry.name === '.git') {
// Worktrees and submodules carry `.git` as a file (or symlink), not a directory.
if (entry.isDirectory() || entry.isFile() || entry.isSymbolicLink()) hasGit = true
} else if (entry.isDirectory() && !entry.isSymbolicLink() && !entry.name.startsWith('.') && entry.name !== 'node_modules') {
subdirs.push(entry.name)
}
}
if (hasGit && dir !== cwd && !roots.some(existing => pathIdentity(existing) === pathIdentity(dir))) roots.push(dir)
for (const name of subdirs.sort((left, right) => left.localeCompare(right))) next.push(join(dir, name))
}
level = next
}
return roots
}
/** Resolve the selected repository, defaulting to the first discovered root. */
+83
View File
@@ -42,6 +42,89 @@ describe('git parsing', () => {
}
})
it('discovers root and direct child repositories when root is also a git repository', async () => {
const workspace = await mkdtemp(join(tmpdir(), 'dsh-better-sidebar-root-git-'))
const child = join(workspace, 'child-repo')
try {
await mkdir(child)
await Promise.all([
execFileAsync('git', ['-C', workspace, 'init']),
execFileAsync('git', ['-C', child, 'init']),
])
await expect(repoRoots(workspace)).resolves.toEqual([canonical(workspace), canonical(child)])
await expect(status(workspace, canonical(child))).resolves.toMatchObject({
isRepo: true,
root: canonical(child),
repositories: [canonical(workspace), canonical(child)],
})
} finally {
await rm(workspace, { recursive: true, force: true })
}
})
it('discovers nested child repositories up to three levels below a git root', async () => {
const workspace = await mkdtemp(join(tmpdir(), 'dsh-better-sidebar-nested-git-'))
const level2 = join(workspace, 'group', 'project')
const level3 = join(level2, 'vendor-lib')
const level3Worktree = join(level2, 'vendor-tools')
try {
mkdirSync(level3, { recursive: true })
mkdirSync(level3Worktree, { recursive: true })
await Promise.all([
execFileAsync('git', ['-C', workspace, 'init']),
execFileAsync('git', ['-C', level2, 'init']),
execFileAsync('git', ['-C', level3, 'init']),
// Worktree form: `.git` is a plain file pointing at the real gitdir.
writeFileSync(join(level3Worktree, '.git'), 'gitdir: /tmp/nonexistent/worktrees/x\n'),
])
await expect(repoRoots(workspace)).resolves.toEqual([
canonical(workspace),
canonical(level2),
canonical(level3),
canonical(level3Worktree),
])
await expect(status(workspace, canonical(level3))).resolves.toMatchObject({
isRepo: true,
root: canonical(level3),
repositories: [canonical(workspace), canonical(level2), canonical(level3), canonical(level3Worktree)],
})
} finally {
await rm(workspace, { recursive: true, force: true })
}
})
it('descends into discovered repositories and skips hidden, node_modules and beyond-cap depths', async () => {
const workspace = await mkdtemp(join(tmpdir(), 'dsh-better-sidebar-embedded-git-'))
const outer = join(workspace, 'outer')
const embedded = join(outer, 'packages', 'embedded')
const inNodeModules = join(outer, 'node_modules', 'shadowed')
const tooDeep = join(workspace, 'a', 'b', 'c', 'too-deep')
try {
mkdirSync(embedded, { recursive: true })
mkdirSync(inNodeModules, { recursive: true })
mkdirSync(tooDeep, { recursive: true })
await Promise.all([
execFileAsync('git', ['-C', workspace, 'init']),
execFileAsync('git', ['-C', outer, 'init']),
execFileAsync('git', ['-C', embedded, 'init']),
execFileAsync('git', ['-C', inNodeModules, 'init']),
execFileAsync('git', ['-C', tooDeep, 'init']),
])
// Repos hidden inside node_modules or beyond depth DISCOVERY_MAX_DEPTH - 1
// stay invisible; the repo embedded inside `outer` is still discovered.
await expect(repoRoots(workspace)).resolves.toEqual([
canonical(workspace),
canonical(outer),
canonical(embedded),
])
} finally {
await rm(workspace, { recursive: true, force: true })
}
})
it('parses porcelain -z entries including renames', () => {
const output = ['M src/a.ts', ' M src/b.ts', '?? src/c.ts', 'R src/new.ts', 'src/old.ts', ''].join('\0')
const entries = parsePorcelainZ(output)