-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (122 loc) · 4.08 KB
/
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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"errors"
"flag"
"fmt"
"log"
"strconv"
"strings"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)
var getDefault bool
var path string
var (
getTag = flag.Bool("t", false, "Return the latest semver tag (annotated or lightweight Git tag)")
getBranch = flag.Bool("b", false, "Return the current branch")
getCommit = flag.Bool("c", false, "Return the current commit")
ignoreUncleanTag = flag.Bool("ignore-unclean-tag", false, "Return only tag name even if the latest tag doesn't point to HEAD (\"v1.0.4\" instead of \"v1.0.4-1-5227b593\")")
)
// Basic example of how to list tags.
func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of tagver: [-t] [-b] [-c] [<git dir>]\n\n")
fmt.Fprint(flag.CommandLine.Output(), "Default output is very close to \"git describe --tags\" if there's a tag present, otherwise defaults to branch and commit:\n")
fmt.Fprint(flag.CommandLine.Output(), "\tIf HEAD is not tagged: <branch>-<HEAD SHA> (example: main-63380731)\n")
fmt.Fprint(flag.CommandLine.Output(), "\tIf HEAD is tagged: <tag>-<HEAD SHA> (example: v1.0.4-63380731)\n")
fmt.Fprint(flag.CommandLine.Output(), "\tIf HEAD is tagged but has commits after latest tag: <tag>-<commits since tag>-<HEAD SHA> (example: v1.0.4-1-5227b593)\n\n")
fmt.Fprint(flag.CommandLine.Output(), "If \"-b\" or \"-c\" are provided with \"-t\", it is additive and will include commits since tag if unclean.\n")
fmt.Fprint(flag.CommandLine.Output(), "The number of commits since tag can be ignored with \"-ignore-unclean-tag\".\n\n")
fmt.Fprint(flag.CommandLine.Output(), "Print order will be <tag>-<branch>[-<commits since tag>]-<SHA>.\n\n")
fmt.Fprintln(flag.CommandLine.Output(), "Set one or more flags.")
flag.PrintDefaults()
}
flag.Parse()
if !*getTag && !*getBranch && !*getCommit {
getDefault = true
}
if args := flag.Args(); len(args) != 0 {
path = args[0]
} else {
path = "./"
}
// We instance a new repository targeting the given path (the .git folder)
r, err := plainOpen(path)
if err != nil {
if err == git.ErrRepositoryNotExists {
log.Fatalf("Directory %s is not a git repository", path)
} else {
log.Fatalf("Failed to open git repo %s", path)
}
}
var commit string
var branch string
var tag string
var count int
// Check if we're in a CI environment
if isCI() {
commit, branch, tag = getRefsFromCI()
} else {
if isDetachedHead(r) {
// Check if we're in a detached head state
branch, err = getCurrentBranchFromDetachedHead(r)
if err != nil {
log.Fatalf("Failed to get current branch from %s, err %s\n", path, err.Error())
}
} else {
branch, err = getCurrentBranchFromRepository(r)
if err != nil {
log.Fatalf("Failed to get current branch from %q, %s\n", path, err.Error())
}
}
commit, err = getCurrentCommitFromRepository(r)
if err != nil {
log.Fatalf("Failed to get current commit from %s, err %s\n", path, err.Error())
}
tag, count, err = getLatestTagFromRepository(r)
if err != nil {
if errors.Is(err, plumbing.ErrObjectNotFound) {
tag = ""
} else {
log.Fatalf("Failed to get latest tag from %s, err %s\n", path, err.Error())
}
}
}
var idents []string
if *getTag || getDefault {
if tag != "" {
idents = append(idents, tag)
}
}
if tag == "" && getDefault {
*getBranch = true
*getCommit = true
}
if *getBranch {
if branch != "" {
idents = append(idents, branch)
}
}
if (*getTag || getDefault) && !*ignoreUncleanTag && count != 0 {
idents = append(idents, strconv.Itoa(count))
if !*getCommit && *getTag {
if commit == "" {
log.Fatalf("Failed to get current commit from %s\n", path)
}
// Forcefully add commit even if it's not desired since we're
// in an unclean state.
idents = append(idents, commit)
}
}
if *getCommit || getDefault {
if commit == "" {
log.Fatalf("Failed to get current commit from %s\n", path)
}
idents = append(idents, commit)
}
if len(idents) == 0 {
log.Println("no version information found")
return
}
fmt.Printf("%s\n", strings.Join(idents, "-"))
}