Skip to content

gitindex: honor branch prefixes when resolving short names - #1233

Closed
perfloop-agent wants to merge 2 commits into
sourcegraph:mainfrom
perfloop-oss:perfloop-pr-open-6p9k352b7j
Closed

perfloop-agent wants to merge 2 commits into
sourcegraph:mainfrom
perfloop-oss:perfloop-pr-open-6p9k352b7j

Conversation

@perfloop-agent

Copy link
Copy Markdown
Contributor

BranchPrefix currently applies only after an unprefixed lookup fails. A local branch or tag can therefore override a requested remote branch. During a full-then-delta update, introducing a local main can replace searchable remote content with the local commit.

Resolve short branch names in the configured namespace instead. A private reference-storage view leaves go-git responsible for hash lookup, revision parsing and tag peeling, while preventing alternative short-name expansions from escaping the namespace. The view forwards native hash-prefix lookup, and returned commits use the original repository.

Native full and abbreviated commit-hash lookup, qualified refs, HEAD, top-level refs and revision-expression fallback remain in place. Empty-prefix lookups are unchanged. A missing namespaced branch is not satisfied by an unrelated head or tag.

gitindex/index_test.go covers collisions, namespace misses, explicit-revision compatibility and a full-then-delta update that checks searchable content and branch versions without a full-build fallback.

The scoped Go tests for branch resolution, full/delta and worktree indexing, submodules, missing branches, wildcard branches, latest-commit metadata and ignore handling passed.

Full test suite: left to target CI.

Related work: #941 changes reference path joining; this change addresses namespace selection.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart. To ask for a change, comment or review in plain words. How Perfloop handles pull requests.

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Tomás Senart <ts@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Tomás Senart <ts@perfloop.ai>
@tsenart

tsenart commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Hey @keegancsmith, here's another one when you have time. With BranchPrefix set, a local branch or tag with the same short name could override the remote branch, so a delta update could index the local commit instead. This resolves short names inside the prefix. Thanks!

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 <amp@ampcode.com>
#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 <amp@ampcode.com>

* 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 <amp@ampcode.com>

* 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 <amp@ampcode.com>

* 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 <amp@ampcode.com>

---------

Co-authored-by: Amp <amp@ampcode.com>
@keegancsmith

Copy link
Copy Markdown
Member

Thanks for this, and for the test cases. #1300 supersedes it: it resolves branch names with git rev-parse and tries <prefix>/<name> first, so a local branch or tag no longer shadows the requested remote branch. Your table and the full-then-delta test are in #1300 as regression tests. One deliberate difference: if the prefixed branch is missing, #1300 still falls back to the bare name, because treating it as missing broke -branches origin/feature with the default refs/heads/ prefix.

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.

3 participants