Repository navigation
gitindex: resolve branches with git rev-parse - #1300
Merged
Merged
Conversation
go-git's ResolveRevision does not follow git's rules for short names. It tries a hex name as an abbreviated commit ID before trying it as a ref, which #1290 worked around, and with a branch prefix it tries the bare name first, so a local branch or tag can shadow the requested remote branch (#1233). gitindex already requires the git binary for cat-file, so let git resolve names with rev-parse --verify --end-of-options. With a prefix, <prefix>/<name> now wins over any other ref or object ID of that name, and a short name missing from the namespace is reported missing instead of falling back to a local branch or tag. HEAD, object IDs, qualified refs and expressions still resolve as git resolves them. ZOEKT_DISABLE_CATFILE_BATCH=true keeps the previous go-git resolution as a kill switch, as it does for blob reading. The branch prefix tests are taken from #1233. Cases where a hex branch and an abbreviated object ID collide now expect the branch, matching git and Amp-Thread-ID: https://ampcode.com/threads/T-5b43e219-dc7b-4c53-85b0-8889c69ac42b Co-authored-by: Amp <amp@ampcode.com> #1290, and main:file.txt names a blob, so it no longer resolves.
zoekt-git-index users can pass a tag in -branches with the default refs/heads/ prefix, which go-git resolved, so only a missing local or remote-tracking branch is reported missing now. rev-parse only accepts --end-of-options since git 2.30, and older git would report every branch missing, so reject names starting with a dash instead. No ref name can. Amp-Thread-ID: https://ampcode.com/threads/T-5b43e219-dc7b-4c53-85b0-8889c69ac42b Co-authored-by: Amp <amp@ampcode.com>
This was referenced Nov 26, 2026
Without it an inherited GIT_DIR, for example when zoekt runs from a git hook, makes git resolve branches in another repository, and a directory git doesn't recognize makes it search parent directories. Sourcegraph's gitserver sets GIT_DIR=. for the same reason. GIT_NO_LAZY_FETCH=1 matches go-git, which never fetches from a promisor remote. Amp-Thread-ID: https://ampcode.com/threads/T-5b43e219-dc7b-4c53-85b0-8889c69ac42b Co-authored-by: Amp <amp@ampcode.com>
getCommit is now a small switch between the git binary and the go-git fallback, with each resolver returning a commit ID, so removing the fallback later is deleting resolveCommitGoGit and its branch. The git helpers live in revparse.go next to catfile.go instead of growing index.go. AllowMissingBranch now checks plumbing.ErrReferenceNotFound with errors.Is instead of comparing the error text. Amp-Thread-ID: https://ampcode.com/threads/T-5b43e219-dc7b-4c53-85b0-8889c69ac42b Co-authored-by: Amp <amp@ampcode.com>
keegancsmith
marked this pull request as ready for review
November 26, 2026 18:07
Treating a short branch name missing from the namespace as missing broke zoekt-git-index -branches origin/feature with the default refs/heads/ prefix, since origin/feature is the short name of a remote-tracking branch. It only protected a narrow case, a stale local branch outliving its pruned remote under a remote prefix, so resolve <prefix>/<name> first and otherwise the bare name, as before. Amp-Thread-ID: https://ampcode.com/threads/T-5b43e219-dc7b-4c53-85b0-8889c69ac42b Co-authored-by: Amp <amp@ampcode.com>
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.
go-git's ResolveRevision keeps getting branch resolution wrong: it tries a hex name as an abbreviated commit ID before trying it as a ref (#1290 patched that), and with a
BranchPrefixit tries the bare name first, so a local branch can shadow the requested remote branch (#1233). gitindex already needs the git binary for cat-file, so this lets git resolve names withgit rev-parse --verify <name>^{commit}, run in the repository's git dir (which also works for submodules go-git opened). go-git still loads the commit object afterwards. Names starting with-are rejected rather than passing--end-of-options, whichrev-parseonly accepts since git 2.30. Like Sourcegraph's gitserver, git runs withGIT_DIR=.in that directory, so an inheritedGIT_DIR(say from a hook) or a parent repository can't redirect it, and withGIT_NO_LAZY_FETCH=1so it never fetches from a promisor remote, which go-git never did either.With a prefix,
<prefix>/<name>is now tried first, so it wins over any other ref or object ID with that name. Otherwise the bare name resolves the way git resolves it, as before, which covers HEAD, tags passed as branch names, object IDs,origin/featurewith the defaultrefs/heads/prefix, qualified refs and revision expressions.ZOEKT_DISABLE_CATFILE_BATCH=trueturns the git binary off here too and keeps the old go-git resolution.The tests are #1233's table and delta test, plus #1290's test run in both modes. A few #1233 expectations changed. A branch missing from the namespace falls back to the bare name, as on main, rather than being reported missing: that rule broke
-branches origin/featurewith the default prefix. When a hex branch name collides with an abbreviated object ID, the branch now wins, which matches git and #1290.main^0follows the namespacedmain.main:file.txtnames a blob, so it no longer resolves to a commit. This supersedes #1233.