MCPcopy Create free account
hub / github.com/go-git/go-git / ResolveRevision

Method ResolveRevision

repository.go:1549–1687  ·  view source on GitHub ↗

ResolveRevision resolves revision to corresponding hash. It will always resolve to a commit hash, not a tree or annotated tag. Implemented resolvers : HEAD, branch, tag, heads/branch, refs/heads/branch, refs/tags/tag, refs/remotes/origin/branch, refs/remotes/origin/HEAD, tilde and caret (HEAD~1, ma

(in plumbing.Revision)

Source from the content-addressed store, hash-verified

1547// Implemented resolvers : HEAD, branch, tag, heads/branch, refs/heads/branch,
1548// refs/tags/tag, refs/remotes/origin/branch, refs/remotes/origin/HEAD, tilde and caret (HEAD~1, master~^, tag~2, ref/heads/master~1, ...), selection by text (HEAD^{/fix nasty bug}), hash (prefix and full)
1549func (r *Repository) ResolveRevision(in plumbing.Revision) (*plumbing.Hash, error) {
1550 rev := in.String()
1551 if rev == "" {
1552 return &plumbing.ZeroHash, plumbing.ErrReferenceNotFound
1553 }
1554
1555 p := revision.NewParserFromString(rev)
1556 items, err := p.Parse()
1557
1558 if err != nil {
1559 return nil, err
1560 }
1561
1562 var commit *object.Commit
1563
1564 for _, item := range items {
1565 switch item := item.(type) {
1566 case revision.Ref:
1567 revisionRef := item
1568
1569 var tryHashes []plumbing.Hash
1570
1571 tryHashes = append(tryHashes, r.resolveHashPrefix(string(revisionRef))...)
1572
1573 ref, err := expand_ref(r.Storer, plumbing.ReferenceName(revisionRef))
1574 if err == nil {
1575 tryHashes = append(tryHashes, ref.Hash())
1576 }
1577
1578 // in ambiguous cases, `git rev-parse` will emit a warning, but
1579 // will always return the oid in preference to a ref; we don't have
1580 // the ability to emit a warning here, so (for speed purposes)
1581 // don't bother to detect the ambiguity either, just return in the
1582 // priority that git would.
1583 gotOne := false
1584 for _, hash := range tryHashes {
1585 commitObj, err := r.CommitObject(hash)
1586 if err == nil {
1587 commit = commitObj
1588 gotOne = true
1589 break
1590 }
1591
1592 tagObj, err := r.TagObject(hash)
1593 if err == nil {
1594 // If the tag target lookup fails here, this most likely
1595 // represents some sort of repo corruption, so let the
1596 // error bubble up.
1597 tagCommit, err := tagObj.Commit()
1598 if err != nil {
1599 return &plumbing.ZeroHash, err
1600 }
1601 commit = tagCommit
1602 gotOne = true
1603 break
1604 }
1605 }
1606

Callers 8

TestResolveRevisionMethod · 0.80
TestIssue674Method · 0.80
mainFunction · 0.80
mainFunction · 0.80
mainFunction · 0.80
mainFunction · 0.80

Calls 14

resolveHashPrefixMethod · 0.95
CommitObjectMethod · 0.95
TagObjectMethod · 0.95
ParentsMethod · 0.95
NewParserFromStringFunction · 0.92
ReferenceNameTypeAlias · 0.92
NewCommitPreorderIterFunction · 0.92
expand_refFunction · 0.85
StringMethod · 0.65
HashMethod · 0.65
CommitMethod · 0.65
NextMethod · 0.65

Tested by 4

TestResolveRevisionMethod · 0.64
TestIssue674Method · 0.64