go-git icon indicating copy to clipboard operation
go-git copied to clipboard

How to know whether a tag is a lightweight tag or an annotated one

Open easonyq opened this issue 6 years ago • 1 comments

I've know that go-git supported creating an annotated tag from issue#430.

But I couldnot find a way to get:

  1. Whether a tag is an annotated tag or a lightweight tag
  2. Commit message of a lightweight tag (I found a way and listed it below. If there is any better ways please let me know)
  3. Commit message of an annotated tag

What I've tried:

tagrefs, _ := repo.Tags()
tagrefs.ForEach(func(t *plumbing.Reference) error {
    // Here t contains Name(), Hash(), Type() ...
    fmt.Println(t)
    return nil
})

Actually, git cat-file -t <tagname> and git tag -n is somewhat similar to what I need.

Thanks for your replying.

easonyq avatar Aug 19 '19 07:08 easonyq

I found the following way to get commit message of lightweight tags.

tagrefs, _ := repo.Tags()
tagrefs.ForEach(func(t *plumbing.Reference) error {
    h, err := repo.ResolveRevision(plumbing.Revision(t.Hash().String()))
    checkError(err)
    if h == nil {
        fmt.Println("empty return")
        return nil
    }

    obj, err := repo.Object(plumbing.AnyObject, *h)
    checkError(err)

    o := obj.(*object.Commit)
    fmt.Println(o.Message)
    return nil
})

easonyq avatar Aug 19 '19 07:08 easonyq