Skip to content

fix: Find the nearest .git when a worktree is inside a clone - #8976

Open
kachkaev wants to merge 1 commit into
streetsidesoftware:mainfrom
kachkaev:fix/find-repo-root-worktree
Open

fix: Find the nearest .git when a worktree is inside a clone#8976
kachkaev wants to merge 1 commit into
streetsidesoftware:mainfrom
kachkaev:fix/find-repo-root-worktree

Conversation

@kachkaev

Copy link
Copy Markdown
Contributor

Fixes #8975.

findRepoRoot runs two independent findUp searches and prefers the directory result, so a clone further up wins over a worktree's own .git file:

const foundDir = await vfs.findUp('.git', directory, { type: 'directory' });
const foundFile = await vfs.findUp('.git', directory, { type: 'file' });
const found = foundDir || foundFile;

With useGitignore: true the clone's .gitignore is then applied to the worktree's files, so a worktree kept in a gitignored folder inside its own clone — the .worktrees layout this repository uses itself — has every file treated as ignored and cspell reports Files checked: 0.

This searches once for a .git of either kind, via a predicate, so the nearest one marks the root. That also drops one full walk up the tree.

Note that omitting type is not an alternative: findUpFromUrl defaults it to 'file', so a plain findUp('.git', directory) stops finding normal clones.

The new test builds a clone with a worktree nested inside it under temp/ and checks both roots resolve to themselves. It fails on main with the worktree resolving to the clone.

Verified with the built CLI on a scratch repository, run from a subdirectory of the worktree so that the findRepoRoot(cwd) || cwd fallback in generateGitIgnore cannot mask the result: before, Files checked: 0; after, the misspellings are reported.

`findRepoRoot` ran one search for a `.git` directory and one for a `.git` file, then
preferred the directory. The two searches are independent, so a clone further up won
over a worktree's own `.git` file. With `useGitignore` that applied the clone's
`.gitignore` to the worktree, and a worktree kept in a gitignored folder inside its
clone had every file treated as ignored.

Search once for a `.git` of either kind so the nearest one marks the root.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: findRepoRoot prefers a distant .git directory over a worktree's own .git file

1 participant