Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

ResolveRevision doesn't resolve annotated tags #772

Closed
rykov opened this issue Mar 5, 2018 · 7 comments
Closed

ResolveRevision doesn't resolve annotated tags #772

rykov opened this issue Mar 5, 2018 · 7 comments
Labels

Comments

@rykov
Copy link
Contributor

rykov commented Mar 5, 2018

Looking through the revision.Ref case for the switch statement in ResolveRevision, the tag is resolved, however, calling r.CommitObject fails with "object not found" error because the resolved commit is not a commit Hash. Seems like one needs to call r.resolveToCommitHash either before CommitObject or inside of it (depending on the intended API)

go-git/repository.go

Lines 914 to 930 in ecda5c1

for _, rule := range append([]string{"%s"}, plumbing.RefRevParseRules...) {
ref, err = storer.ResolveReference(r.Storer, plumbing.ReferenceName(fmt.Sprintf(rule, revisionRef)))
if err == nil {
break
}
}
if ref == nil {
return &plumbing.ZeroHash, plumbing.ErrReferenceNotFound
}
commit, err = r.CommitObject(ref.Hash())
if err != nil {
return &plumbing.ZeroHash, err
}

@mcuadros
Copy link
Contributor

Can you provide an example repository? Thanks

@rykov
Copy link
Contributor Author

rykov commented Mar 12, 2018

Here's a quick way to reproduce this:

$ git init 
$ git commit --allow-empty -m "Initial commit"
$ git tag -a ann-tag -m "Annotated!"
$ git tag reg-tag

And then run:

package main

import(
  "gopkg.in/src-d/go-git.v4"
  "gopkg.in/src-d/go-git.v4/plumbing"
  "fmt"
)

func main() {
  r, _ := git.PlainOpen(".")
  rev1, err := r.ResolveRevision(plumbing.Revision("reg-tag"))
  fmt.Println("Regular tag: ", rev1, err)
  rev2, err := r.ResolveRevision(plumbing.Revision("ann-tag"))
  fmt.Println("Annotated tag: ", rev2, err)
}

The output that I see with v4.1.1 is:

Regular tag:  7e454763ed760efa060937a9fd603a60b9fd2879 <nil>
Annotated tag:  0000000000000000000000000000000000000000 object not found

@orirawlings
Copy link
Contributor

It seems like git rev-parse would resolve to the hash of the tag object in this case. So we probably want to mimic that behavior as well.

@rykov
Copy link
Contributor Author

rykov commented Mar 15, 2018

If you mean the hash of the annotated tag object, then it makes sense, but we also then need a public method to unpeel that to a commit hash. Something similar to r.resolveToCommitHash

@pinoniq
Copy link

pinoniq commented Apr 18, 2018

In the meantime, is there another way to resolve annotated tags using go-git?

@pinoniq
Copy link

pinoniq commented Apr 18, 2018

I did some digging and I managed to get it working "kind of".

repository.ResolveRevision expects the resolved reference to be a commit. This is the case for lightweight tags. But not at all for annotated tags.

replacing

			if ref != nil {
				refCommit, rErr = r.CommitObject(ref.Hash())
			} else {
				rErr = plumbing.ErrReferenceNotFound
			}

with

			if ref != nil {
				if ref.Name().IsTag() {
					refTagObject, rErr = r.TagObject(ref.Hash())
					refCommit, rErr = refTagObject.Commit()
				} else {
					refCommit, rErr = r.CommitObject(ref.Hash())
				}
			} else {
				rErr = plumbing.ErrReferenceNotFound
			}

seems to do the job.

I'm willing to open a PR if this solution seems acceptable for you

@mcuadros
Copy link
Contributor

Solved

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants