Skip to content

gitindex: resolve branches with git rev-parse - #1300

Merged
keegancsmith merged 6 commits into
mainfrom
k/gitindex-rev-parse
Nov 26, 2026
Merged

keegancsmith merged 6 commits into
mainfrom
k/gitindex-rev-parse

Conversation

@keegancsmith

@keegancsmith keegancsmith commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

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 BranchPrefix it 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 with git 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, which rev-parse only accepts since git 2.30. Like Sourcegraph's gitserver, git runs with GIT_DIR=. in that directory, so an inherited GIT_DIR (say from a hook) or a parent repository can't redirect it, and with GIT_NO_LAZY_FETCH=1 so 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/feature with the default refs/heads/ prefix, qualified refs and revision expressions. ZOEKT_DISABLE_CATFILE_BATCH=true turns 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/feature with the default prefix. When a hex branch name collides with an abbreviated object ID, the branch now wins, which matches git and #1290. main^0 follows the namespaced main. main:file.txt names a blob, so it no longer resolves to a commit. This supersedes #1233.

keegancsmith and others added 2 commits November 26, 2026 15:17
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>
keegancsmith and others added 3 commits November 26, 2026 17:46
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
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>
@keegancsmith
keegancsmith merged commit 1ae38a1 into main Nov 26, 2026
8 checks passed
@keegancsmith
keegancsmith deleted the k/gitindex-rev-parse branch November 26, 2026 18:11
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.

1 participant