Skip to content

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

Open

Description

@r0h1tb

Repository.ResolveRevision resolves a branch or tag to the wrong commit when its name is also the start of an object id. Git resolves the ref.

git init demo && cd demo
for i in $(seq 1 40); do git commit -q --allow-empty -m c$i; done
p=$(git rev-parse --short=4 HEAD~10)   # 6a33 in my run
git tag $p HEAD
git branch 5 HEAD
git rev-parse $p 5   # HEAD twice, with "warning: refname '6a33' is ambiguous."
git rev-parse 918    # fatal: ambiguous argument '918': unknown revision

With go-git (main and releases/v5.x) on the same repository:

ResolveRevision("6a33") -> HEAD~10   (expected HEAD)
ResolveRevision("5")    -> 59c2c73   (a commit starting with 5; expected HEAD)

ResolveRevision tries the name as an abbreviated id before it looks up the ref, and the comment there says Git prefers the id. That holds only for a full-length id. In Git's object-name.c, get_oid_1 calls get_oid_basic (a full id, then the refs) and falls back to get_short_oid only when no ref matched. get_short_oid also rejects anything shorter than minimum_abbrev (4), so git rev-parse 918 fails, while go-git accepts 1 to 3 hex digits as an abbreviated id.

Branch and tag names that are all hex digits are common (build numbers, ticket numbers, cafe, beef), and the chance of a collision grows with the repository. The result is silently wrong: the caller gets a different commit and no error.

I have a fix with tests and will open a PR for it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions