Skip to content

Handle git indexing path correctly for Git worktrees#1025

Merged
keegancsmith merged 3 commits intosourcegraph:mainfrom
manuelvanrijn:worktrees-with-common-dir-support
Mar 25, 2026
Merged

Handle git indexing path correctly for Git worktrees#1025
keegancsmith merged 3 commits intosourcegraph:mainfrom
manuelvanrijn:worktrees-with-common-dir-support

Conversation

@manuelvanrijn
Copy link
Copy Markdown
Contributor

Problem

Zoekt’s git indexing path does not correctly handle Git worktrees.

In a worktree, the repository metadata is split:

  • the worktree has its own git dir
  • refs / objects / config may live in the shared common git dir

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 gitindex try to index committed content from a worktree checkout.

Solution

This PR updates the relevant gitindex plain-open paths to use a dedicated helper:

  • plainOpenRepo(...)
  • implemented with git.PlainOpenWithOptions(...)
  • enables:
    • DetectDotGit: true
    • EnableDotGitCommonDir: true

That gives go-git the correct view of worktree repositories, including access to the shared common-dir metadata.

The helper is now used in:

  • repository opening for indexing
  • repository opening for config/template resolution

Zoekt's optimized repository opener assumes a standard checkout where .git is a directory. Git worktrees expose .git as 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.

`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.
@manuelvanrijn manuelvanrijn force-pushed the worktrees-with-common-dir-support branch from e048395 to 4e9a52f Compare March 25, 2026 10:52
Copy link
Copy Markdown
Member

@keegancsmith keegancsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@keegancsmith
Copy link
Copy Markdown
Member

@manuelvanrijn failures on CI seem legit. Mind taking a look / getting tests to pass locally?

@manuelvanrijn
Copy link
Copy Markdown
Contributor Author

@manuelvanrijn failures on CI seem legit. Mind taking a look / getting tests to pass locally?

@keegancsmith should be resolved now 💪

@keegancsmith keegancsmith merged commit a0f5789 into sourcegraph:main Mar 25, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants