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