+1
_examples/README.md
+1
_examples/README.md
···
19
19
- [branch](branch/main.go) - How to create and remove branches or any other kind of reference.
20
20
- [tag](tag/main.go) - List/print repository tags.
21
21
- [tag create and push](tag-create-push/main.go) - Create and push a new tag.
22
+
- [tag find if head is tagged](find-if-any-tag-point-head/main.go) - Find if `HEAD` is tagged.
22
23
- [remotes](remotes/main.go) - Working with remotes: adding, removing, etc.
23
24
- [progress](progress/main.go) - Printing the progress information from the sideband.
24
25
- [revision](revision/main.go) - Solve a revision into a commit.
+38
_examples/find-if-any-tag-point-head/main.go
+38
_examples/find-if-any-tag-point-head/main.go
···
1
+
package main
2
+
3
+
import (
4
+
"fmt"
5
+
"os"
6
+
7
+
"github.com/go-git/go-git/v5"
8
+
. "github.com/go-git/go-git/v5/_examples"
9
+
"github.com/go-git/go-git/v5/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
+
21
+
// Get HEAD reference to use for comparison later on.
22
+
ref, err := r.Head()
23
+
CheckIfError(err)
24
+
25
+
tags, err := r.Tags()
26
+
CheckIfError(err)
27
+
28
+
// List all tags, both lightweight tags and annotated tags and see if some tag points to HEAD reference.
29
+
err = tags.ForEach(func(t *plumbing.Reference) error {
30
+
// This technique should work for both lightweight and annotated tags.
31
+
revHash, err := r.ResolveRevision(plumbing.Revision(t.Name()))
32
+
CheckIfError(err)
33
+
if *revHash == ref.Hash() {
34
+
fmt.Printf("Found tag %s with hash %s pointing to HEAD %s\n", t.Name().Short(), revHash, ref.Hash())
35
+
}
36
+
return nil
37
+
})
38
+
}