Debian Codegraph @

_examples/find-if-any-tag-point-head/main.go

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