| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
|
| 7 |
"github.com/go-git/go-git/v6" |
| 8 |
. "github.com/go-git/go-git/v6/_examples" |
| 9 |
"github.com/go-git/go-git/v6/plumbing" |
| 10 |
) |
| 11 |
|
| 12 |
// Basic example of how to find if HEAD is tagged. |
| 13 |
func main() { |
| 14 |
CheckArgs("<path>") |
| 15 |
path := os.Args[1] |
| 16 |
|
| 17 |
// We instantiate a new repository targeting the given path (the .git folder) |
| 18 |
r, err := git.PlainOpen(path) |
| 19 |
CheckIfError(err) |
| 20 |
defer func() { _ = r.Close() }() |
| 21 |
|
| 22 |
// Get HEAD reference to use for comparison later on. |
| 23 |
ref, err := r.Head() |
| 24 |
CheckIfError(err) |
| 25 |
|
| 26 |
tags, err := r.Tags() |
| 27 |
CheckIfError(err) |
| 28 |
|
| 29 |
// List all tags, both lightweight tags and annotated tags and see if some tag points to HEAD reference. |
| 30 |
err = tags.ForEach(func(t *plumbing.Reference) error { |
| 31 |
// This technique should work for both lightweight and annotated tags. |
| 32 |
revHash, err := r.ResolveRevision(plumbing.Revision(t.Name())) |
| 33 |
CheckIfError(err) |
| 34 |
if *revHash == ref.Hash() { |
| 35 |
fmt.Printf("Found tag %s with hash %s pointing to HEAD %s\n", t.Name().Short(), revHash, ref.Hash()) |
| 36 |
} |
| 37 |
return nil |
| 38 |
}) |
| 39 |
} |