/
main.go
182 lines (171 loc) · 3.67 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/storer"
"github.com/p9c/opts/version"
)
var (
URL string
GitRef string
GitCommit string
BuildTime string
Tag string
Major, Minor, Patch int
Meta string
PathBase string
)
func main() {
I.Ln(version.Get())
BuildTime = time.Now().Format(time.RFC3339)
var cwd string
var e error
if cwd, e = os.Getwd(); E.Chk(e) {
return
}
cwd = filepath.Dir(cwd)
// I.Ln(cwd)
var repo *git.Repository
if repo, e = git.PlainOpen(cwd); E.Chk(e) {
return
}
var rr []*git.Remote
if rr, e = repo.Remotes(); E.Chk(e) {
return
}
for i := range rr {
rs := rr[i].String()
if strings.HasPrefix(rs, "origin") {
rss := strings.Split(rs, "git@")
if len(rss) > 1 {
rsss := strings.Split(rss[1], ".git")
URL = strings.ReplaceAll(rsss[0], ":", "/")
break
}
rss = strings.Split(rs, "https://")
if len(rss) > 1 {
rsss := strings.Split(rss[1], ".git")
URL = rsss[0]
break
}
}
}
var tr *git.Worktree
if tr, e = repo.Worktree(); E.Chk(e) {
}
var rh *plumbing.Reference
if rh, e = repo.Head(); E.Chk(e) {
return
}
rhs := rh.Strings()
GitRef = rhs[0]
GitCommit = rhs[1]
var rt storer.ReferenceIter
if rt, e = repo.Tags(); E.Chk(e) {
return
}
var maxVersion int
var maxString string
var maxIs bool
if e = rt.ForEach(
func(pr *plumbing.Reference) (e error) {
s := strings.Split(pr.String(), "/")
prs := s[2]
if strings.HasPrefix(prs, "v") {
var va [3]int
var meta string
_, _ = fmt.Sscanf(prs, "v%d.%d.%d%s", &va[0], &va[1], &va[2], &meta)
vn := va[0]*1000000 + va[1]*1000 + va[2]
if maxVersion < vn {
maxVersion = vn
maxString = prs
Major = va[0]
Minor = va[1]
Patch = va[2]
Meta = meta
}
if pr.Hash() == rh.Hash() {
maxIs = true
return
}
}
return
},
); E.Chk(e) {
return
}
if !maxIs {
maxString += "+"
}
Tag = maxString
PathBase = tr.Filesystem.Root() + "/"
// I.Ln(PathBase)
versionFile := `package version
`+`//go:generate go run ./update/.
import (
"fmt"
)
var (
// URL is the git URL for the repository
URL = "%s"
// GitRef is the gitref, as in refs/heads/branchname
GitRef = "%s"
// GitCommit is the commit hash of the current HEAD
GitCommit = "%s"
// BuildTime stores the time when the current binary was built
BuildTime = "%s"
// Tag lists the Tag on the build, adding a + to the newest Tag if the commit is
// not that commit
Tag = "%s"
// PathBase is the path base returned from runtime caller
PathBase = "%s"
// Major is the major number from the tag
Major = %d
// Minor is the minor number from the tag
Minor = %d
// Patch is the patch version number from the tag
Patch = %d
// Meta is the extra arbitrary string field from Semver spec
Meta = "%s"
)
// Get returns a pretty printed version information string
func Get() string {
return fmt.Sprint(
"\nRepository Information\n"+
"\tGit repository: "+URL+"\n",
"\tBranch: "+GitRef+"\n"+
"\tCommit: "+GitCommit+"\n"+
"\tBuilt: "+BuildTime+"\n"+
"\tTag: "+Tag+"\n",
"\tMajor:", Major, "\n",
"\tMinor:", Minor, "\n",
"\tPatch:", Patch, "\n",
"\tMeta: ", Meta, "\n",
)
}
`
versionFileOut := fmt.Sprintf(
versionFile,
URL,
GitRef,
GitCommit,
BuildTime,
Tag,
PathBase,
Major,
Minor,
Patch,
Meta,
)
path := filepath.Join(filepath.Join(PathBase, "version"), "version.go")
if e = ioutil.WriteFile(path, []byte(versionFileOut), 0666); E.Chk(e) {
}
// I.Ln("updated version.go written")
return
}