Repository navigation
Conversation
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]>
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.
Fixes #2644.
ResolveRevisiontried 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, wheregit rev-parsereturns the ref and warns that it is ambiguous. It also took 1 to 3 hex digits as an abbreviated id, so a name like918that Git rejects resolved to a commit.The PR follows Git's
object-name.cin two commits:get_oid_1callsget_oid_basic(a full-length id, then the refs) and usesget_short_oidonly when no ref matched.ResolveRevisionnow tries a full id, then the ref, and looks for abbreviated ids only when no ref matched. The full-id check moved out ofresolveHashPrefixintofullHashand is otherwise unchanged. When the ref exists, the object store is no longer scanned for the prefix.init_object_disambiguationrejects abbreviations shorter thanminimum_abbrev(4).resolveHashPrefixnow returns nothing for shorter input.Tests:
TestResolveRevisionRefNamedLikeAbbreviatedHashadds refs918c,tags/1669andbto the basic fixture. Each one is also a prefix of a commit id in the fixture, and each must resolve to its ref.TestResolveRevisionAbbreviatedHashTooShortchecks that918,91and9fail and918cstill resolves. I took the expected values fromgit 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
ResolveRevisionnow getsErrReferenceNotFoundunless 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
ResolveRevisionwithgit rev-parseon generated repositories, and the fix and tests were written with it. Each commit has anAssisted-bytrailer.