Skip to content

git: repository, Prefer refs to abbreviated ids in ResolveRevision - #2645

Open
r0h1tb wants to merge 2 commits into
go-git:mainfrom
r0h1tb:fix/resolve-revision-ref-before-short-id
Open

r0h1tb wants to merge 2 commits into
go-git:mainfrom
r0h1tb:fix/resolve-revision-ref-before-short-id

Conversation

@r0h1tb

@r0h1tb r0h1tb commented Nov 22, 2026

Copy link
Copy Markdown

Fixes #2644.

ResolveRevision tried a name as an abbreviated object id before looking it up as a ref. A branch or tag whose name is also the start of an object id (1669, cafe, a build number) silently resolved to that object, where git rev-parse returns the ref and warns that it is ambiguous. It also took 1 to 3 hex digits as an abbreviated id, so a name like 918 that Git rejects resolved to a commit.

The PR follows Git's object-name.c in two commits:

  • get_oid_1 calls get_oid_basic (a full-length id, then the refs) and uses get_short_oid only when no ref matched. ResolveRevision now tries a full id, then the ref, and looks for abbreviated ids only when no ref matched. The full-id check moved out of resolveHashPrefix into fullHash and is otherwise unchanged. When the ref exists, the object store is no longer scanned for the prefix.
  • init_object_disambiguation rejects abbreviations shorter than minimum_abbrev (4). resolveHashPrefix now returns nothing for shorter input.

Tests: TestResolveRevisionRefNamedLikeAbbreviatedHash adds refs 918c, tags/1669 and b to the basic fixture. Each one is also a prefix of a commit id in the fixture, and each must resolve to its ref. TestResolveRevisionAbbreviatedHashTooShort checks that 918, 91 and 9 fail and 918c still resolves. I took the expected values from git rev-parse (2.50.1) on the same fixture. Both tests fail on main. go test ., go vet . and golangci-lint v2.14.0 pass.

Behaviour change: code that passes 1 to 3 hex digits to ResolveRevision now gets ErrReferenceNotFound unless a ref has that name, which matches Git. releases/v5.x has the same ordering. I can backport it after this lands if you want that.

AI disclosure: this was found with Claude Code (Claude Opus 5.5) by comparing ResolveRevision with git rev-parse on generated repositories, and the fix and tests were written with it. Each commit has an Assisted-by trailer.

ResolveRevision tried a name as an abbreviated object id before looking
it up as a ref, so a branch or tag whose name is also the start of an
object id resolved to that object instead. With a tag named 1669 in a
repository that has a commit 1669dce1, "1669" returned the commit, while
`git rev-parse 1669` returns the tag and only warns that the refname is
ambiguous. Branch and tag names made of hex digits are common, such as
build or ticket numbers, and collisions get more likely as a repository
grows. The caller silently gets a different commit.

In Git's object-name.c, get_oid_1 calls get_oid_basic, which takes a
full-length object id first and then the refs, and falls back to
get_short_oid only when no ref matched. Do the same: try a full id, then
the ref, and look for abbreviated ids only when there is no such ref.
This also skips the object store scan when the ref exists.

The old comment said Git prefers the id to a ref, which holds only for a
full-length id.

Fixes go-git#2644

Assisted-by: Claude Opus 5.5
Signed-off-by: Rohit Behera <[email protected]>
ResolveRevision read any hex prefix as an abbreviated object id, down to
a single digit. A name like 918 that is neither a ref nor long enough for
Git resolved to whichever commit starts with it, where `git rev-parse 918`
fails with "unknown revision". A one-digit name also scanned every object
in the repository.

Git rejects abbreviations shorter than minimum_abbrev, which is 4
(init_object_disambiguation in object-name.c). Return no candidates for
shorter input, so such a name resolves only as a ref.

Assisted-by: Claude Opus 5.5
Signed-off-by: Rohit Behera <[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.

ResolveRevision prefers an abbreviated object id to a ref of the same name

1 participant