0

I'm trying to get the tree of files from a git tag and everything worked just fine until I found out some tags are not annotated and hence not a TagObject

This is my current code:

var obj *plumbing.Reference
obj, err = r.Tag(tag)
checkError(err)

var t *object.Tag
t, err = r.TagObject(obj.Hash())
checkError(err)

var tree *object.Tree
tree, err = t.Tree()
checkError(err)

but now that I had to change it to the following to have access to lightweight tags too

var obj *plumbing.Reference
obj, err = r.Tag(tag)
checkError(err)

var t *plumbing.Reference
t, err = r.Tag(obj.Name().String())
checkError(err)

var tree *object.Tree
tree, err = t.Tree()
checkError(err)

the third part (tree, err = t.Tree()) doesn't work anymore of course, because *plumbing.Reference has no field or method Tree

Is there any way to go from

t, err = r.Tag(obj.Name().String())

to getting a tree?

torek
  • 448,244
  • 59
  • 642
  • 775
  • Use the plumbing reference to get a generic `object.Object`. Then, switch on the type: if it's a `*Tree` you're good to go, if it's a `*Tag` use it to fetch its target object and repeat, and if it's a `*Commit` use that to get the tree. If it's anything else (ie `*Blob`), your reference was to something that's not usable. See also the type-switch example [here](https://pkg.go.dev/github.com/go-git/go-git/v5@v5.4.2/plumbing/object#Object). – torek Jul 14 '21 at 08:41
  • Thanks! I'll try this! – Domenico Jul 14 '21 at 09:17

0 Answers0