Repository navigation
gitindex: resolve a hex branch name as a branch before a commit hash - #1290
Merged
keegancsmith merged 1 commit intoNov 26, 2026
Merged
Conversation
keegancsmith
approved these changes
Nov 26, 2026
getCommit resolves a branch with go-git's ResolveRevision. For a name made only of hex digits, ResolveRevision first looks for a commit whose hash starts with it, with no minimum length, and only then for a ref. git does the opposite for short names. So a branch named `c`, `1234` or `cafe` was indexed at whatever commit's hash starts with those characters, if one exists, and that commit was recorded as the branch version. In a clone with more than about 65k commits nearly every 4 digit name collides. This explains sourcegraph#960, where a branch named `c` could not be searched. When a branch prefix is set and the name is hex and shorter than a full hash, getCommit now looks up the prefixed ref first. Full hashes, abbreviated hashes with no such branch, and other names resolve as before.
keegancsmith
force-pushed
the
gitindex-hex-branch-name
branch
from
November 26, 2026 14:20
3701f31 to
d02b88c
Compare
keegancsmith
added a commit
that referenced
this pull request
Nov 26, 2026
* gitindex: resolve branches with git rev-parse 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 <[email protected]> #1290, and main:file.txt names a blob, so it no longer resolves. * gitindex: resolve tags passed as branch names and drop --end-of-options 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 <[email protected]> * gitindex: run rev-parse with GIT_DIR set to the repository 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 <[email protected]> * gitindex: move git ref resolution to revparse.go 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 <[email protected]> * gitindex: fall back to the bare name when the prefixed branch is missing 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 <[email protected]> --------- Co-authored-by: Amp <[email protected]>
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.
getCommitresolves a branch with go-git'sResolveRevision. For a name made only of hex digits,ResolveRevisionfirst looks for a commit whose hash starts with it, with no minimum length, and only then for a ref. git does the opposite for short names. So a branch namedc,1234orcafewas indexed at whatever commit's hash starts with those characters, if one exists, and that commit was recorded as the branch version. In a clone with more than about 65k commits nearly every 4 digit name collides. This explains #960, where a branch namedccould not be searched.When a branch prefix is set and the name is hex and shorter than a full hash,
getCommitnow looks up the prefixed ref first. Full hashes, abbreviated hashes with no such branch, and other names resolve as before.TestIndexGitRepo_HexBranchNamenames a branch after the hash prefix of an older commit and fails on main withindexed content ["old content"], want the branch tip's "new content".go test ./...passes, and-racepasses on gitindex.#1233 also rewrites
getCommit. If it goes in first, I can rebase this onto it.