fix: Find the nearest .git when a worktree is inside a clone - #8976
Open
kachkaev wants to merge 1 commit into
Open
fix: Find the nearest .git when a worktree is inside a clone#8976kachkaev wants to merge 1 commit into
.git when a worktree is inside a clone#8976kachkaev wants to merge 1 commit into
Conversation
`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>
This was referenced Jul 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #8975.
findRepoRootruns two independentfindUpsearches and prefers the directory result, so a clone further up wins over a worktree's own.gitfile:With
useGitignore: truethe clone's.gitignoreis then applied to the worktree's files, so a worktree kept in a gitignored folder inside its own clone — the.worktreeslayout this repository uses itself — has every file treated as ignored andcspellreportsFiles checked: 0.This searches once for a
.gitof either kind, via a predicate, so the nearest one marks the root. That also drops one full walk up the tree.Note that omitting
typeis not an alternative:findUpFromUrldefaults it to'file', so a plainfindUp('.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 onmainwith 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) || cwdfallback ingenerateGitIgnorecannot mask the result: before,Files checked: 0; after, the misspellings are reported.