src-d / go-git

Project has been moved to: https://github.com/go-git/go-git
https://github.com/go-git/go-git
Apache License 2.0
4.91k stars 541 forks source link

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

Open easonyq opened 5 years ago

easonyq commented 5 years ago

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 commented 5 years ago

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
})