Handle git indexing path correctly for Git worktrees#1025
Merged
keegancsmith merged 3 commits intosourcegraph:mainfrom Mar 25, 2026
Merged
Handle git indexing path correctly for Git worktrees#1025keegancsmith merged 3 commits intosourcegraph:mainfrom
keegancsmith merged 3 commits intosourcegraph:mainfrom
Conversation
`gitindex` assumed repositories could be opened with the default go-git repo opener. That works for normal clones, but it breaks for Git worktrees because refs and object data are split between the worktree git dir and the shared common git dir. This change introduces a small `plainOpenRepo` helper that uses `git`. `PlainOpenWithOptions` with `DetectDotGit` and `EnableDotGitCommonDir`. The helper is then used in the plain-open code paths inside `gitindex`, including config/template loading. With this change, Zoekt can read Git worktrees through go-git using the same logical repository view as Git itself, instead of failing on missing refs or incomplete repository layouts.
e048395 to
4e9a52f
Compare
Member
|
@manuelvanrijn failures on CI seem legit. Mind taking a look / getting tests to pass locally? |
Contributor
Author
@keegancsmith should be resolved now 💪 |
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.
Problem
Zoekt’s git indexing path does not correctly handle Git worktrees.
In a worktree, the repository metadata is split:
The existing plain-open path uses the default go-git repo opening behavior, which does not fully resolve this split layout. As a result, worktree repositories can fail during indexing with errors like missing refs or repository-open failures, even though normal Git commands work fine.
This shows up especially when tools built on top of
gitindextry to index committed content from a worktree checkout.Solution
This PR updates the relevant
gitindexplain-open paths to use a dedicated helper:plainOpenRepo(...)git.PlainOpenWithOptions(...)DetectDotGit: trueEnableDotGitCommonDir: trueThat gives go-git the correct view of worktree repositories, including access to the shared common-dir metadata.
The helper is now used in:
Zoekt's optimized repository opener assumes a standard checkout where
.gitis a directory. Git worktrees expose.gitas a file pointing at the worktree git dir, so that code path misidentifies the repository layout and fails to open it correctly. This change adds an explicit worktree-aware fallback while preserving the existing optimized path for normal repositories.Why this fix
This is the smallest native fix that makes Zoekt worktree-aware without changing indexing behavior for normal repositories.
Result
Zoekt can now open and index Git worktrees correctly through the plain-open path, while preserving existing behavior for standard repositories.