-
Notifications
You must be signed in to change notification settings - Fork 0
/
with_git._go
66 lines (62 loc) · 1.37 KB
/
with_git._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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"os"
log "github.com/cantara/bragi/sbragi"
"github.com/cantara/buri/version/filter"
"github.com/cantara/buri/version/release"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"golang.org/x/exp/slog"
)
func main() {
log.NewLogger(slog.HandlerOptions{
AddSource: true,
Level: log.LevelTrace,
}.NewTextHandler(os.Stderr))
log.Info("starting", "test", "test")
r, err := git.PlainOpen(".")
if err != nil {
log.WithError(err).Fatal("could not find git repo on disk")
return
}
tags, err := r.Tags()
if err != nil {
log.WithError(err).Fatal("while getting tags")
}
defer tags.Close()
var newest *release.Version
err = tags.ForEach(func(ref *plumbing.Reference) error {
if ref == nil {
log.Debug("tag refference was nil")
return nil
}
if !ref.Name().IsTag() {
log.Debug("refference was not tag")
return nil
}
v, err := release.Parse(ref.Name().Short())
if err != nil {
log.WithError(err).Warning("tag was not release version", "tag", ref.Name().Short())
return nil
}
if newest == nil {
newest = &v
return nil
}
if newest.IsStrictlySemanticNewer(filter.AllReleases, v) {
newest = &v
return nil
}
return nil
})
if err != nil {
log.WithError(err).Fatal("during tag iteration")
return
}
if newest == nil {
fmt.Print("v0.0.0")
return
}
fmt.Print(newest.String())
}