Skip to content

gitindex: resolve a hex branch name as a branch before a commit hash - #1290

Merged
keegancsmith merged 1 commit into
sourcegraph:mainfrom
Indian-boult:gitindex-hex-branch-name
Nov 26, 2026
Merged

keegancsmith merged 1 commit into
sourcegraph:mainfrom
Indian-boult:gitindex-hex-branch-name

Conversation

@Indian-boult

Copy link
Copy Markdown
Contributor

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 #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.

TestIndexGitRepo_HexBranchName names a branch after the hash prefix of an older commit and fails on main with indexed content ["old content"], want the branch tip's "new content". go test ./... passes, and -race passes on gitindex.

#1233 also rewrites getCommit. If it goes in first, I can rebase this onto it.

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
keegancsmith force-pushed the gitindex-hex-branch-name branch from 3701f31 to d02b88c Compare November 26, 2026 14:20
@keegancsmith
keegancsmith merged commit c3e1ab5 into sourcegraph:main Nov 26, 2026
8 checks passed
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]>
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