-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker_git.go
77 lines (59 loc) · 1.44 KB
/
checker_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
67
68
69
70
71
72
73
74
75
76
77
package k6lint
import (
"context"
"sort"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)
type version struct {
semver *semver.Version
tag string
}
type gitChecker struct {
repo *git.Repository
versions []*version
}
func newGitChecker() *gitChecker {
return new(gitChecker)
}
func (gc *gitChecker) isWorkDir(_ context.Context, dir string) *checkResult {
repo, err := git.PlainOpen(dir)
if err != nil {
return checkError(err)
}
gc.repo = repo
return checkPassed("found git worktree")
}
func (gc *gitChecker) hasVersions(ctx context.Context, dir string) *checkResult {
if gc.repo == nil {
res := gc.isWorkDir(ctx, dir)
if !res.passed {
return res
}
}
iter, err := gc.repo.Tags()
if err != nil {
return checkError(err)
}
const tagPrefix = "refs/tags/"
err = iter.ForEach(func(ref *plumbing.Reference) error {
tag := strings.TrimPrefix(ref.Name().String(), tagPrefix)
ver, err := semver.NewVersion(tag)
if err == nil {
gc.versions = append(gc.versions, &version{semver: ver, tag: tag})
}
return nil
})
if err != nil {
return checkError(err)
}
if len(gc.versions) == 0 {
return checkFailed("missing release tags")
}
sort.Slice(gc.versions, func(i, j int) bool {
return gc.versions[j].semver.LessThan(gc.versions[i].semver)
})
return checkPassed("found `%d` versions, the latest is `%s`", len(gc.versions), gc.versions[0].tag)
}