This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
203 lines (169 loc) · 4.44 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/Masterminds/semver"
)
var (
version string
ignorePrevious bool
incPattern string
versionPrefix string
versionPrerelease string
versionMetadata string
)
func init() {
version = os.Getenv("version_base")
ignorePrevious, _ = strconv.ParseBool(os.Getenv("version_ignorePrevious"))
incPattern = os.Getenv("version_pattern")
versionPrerelease = os.Getenv("version_prerelease")
versionPrefix = os.Getenv("version_prefix")
versionMetadata = os.Getenv("version_metadata")
}
func getCommitSHA() string {
var out bytes.Buffer
var stdErr bytes.Buffer
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Stdout = &out
cmd.Stderr = &stdErr
err := cmd.Run()
if err != nil {
fmt.Printf("Unable to determine current commit SHA %v\n", err)
fmt.Printf("%s\n", stdErr.String())
os.Exit(1)
}
return strings.TrimSpace(out.String())
}
func getLatestGitTag(sha string) string {
var out bytes.Buffer
var stdErr bytes.Buffer
cmd := exec.Command("git", "describe", "--tag", "--abbrev=0", fmt.Sprintf("%s", sha))
cmd.Stdout = &out
cmd.Stderr = &stdErr
err := cmd.Run()
if err != nil {
return "0.0.0"
}
return strings.TrimSpace(out.String())
}
func getTagCommitSha(tag string) string {
var out bytes.Buffer
var stdErr bytes.Buffer
cmd := exec.Command("git", "rev-list", "-n 1", fmt.Sprintf("%s", tag))
cmd.Stdout = &out
cmd.Stderr = &stdErr
err := cmd.Run()
if err != nil {
return "0.0.0"
}
return strings.TrimSpace(out.String())
}
func timeSinceLastTag(tag string) string {
var out bytes.Buffer
var stdErr bytes.Buffer
cmd := exec.Command("git", "log", "--pretty=%cI", "-n 1", fmt.Sprintf("%s", getTagCommitSha(tag)))
cmd.Stdout = &out
cmd.Stderr = &stdErr
err := cmd.Run()
var topOutput string
if err != nil {
cmd = exec.Command("git", "log", "--pretty=%cI", "-n 1", fmt.Sprintf("%s", getCommitSHA()))
cmd.Stdout = &out
cmd.Stderr = &stdErr
cmd.Run()
}
topOutput = strings.TrimSpace(out.String())
t, err := time.Parse(time.RFC3339, topOutput)
if err != nil {
fmt.Printf("Git did not return expected time format or there was a failure parsing the time [%s] %v\n", topOutput, err)
fmt.Printf("%s\n", topOutput)
os.Exit(1)
}
timeSince := time.Since(t)
return fmt.Sprintf("b%09.f", timeSince.Seconds())
}
// Process entrypoint to do the work and determine when/how to increment.
func Process(v, pattern, prerelease, metadata, prefix string, ignore bool) (string, error) {
lastTag := getLatestGitTag(getCommitSHA())
if ignore && v == "" {
return "", fmt.Errorf("ignore previous is true but no base version provided, please check input")
} else if !ignore {
v = lastTag
}
if prerelease == "" {
prerelease = timeSinceLastTag(lastTag)
}
semVer, err := semver.NewVersion(v)
if err != nil {
return "", fmt.Errorf("version parse failed, [%v]. %s is not a valid SemVer tag", err, v)
}
outVersion := incVersion(*semVer, pattern)
t, err := outVersion.SetPrerelease(prerelease)
if err != nil {
return "", fmt.Errorf("error appending pre-release data: %v", err)
}
outVersion = t
if metadata != "" {
t, err := outVersion.SetMetadata(metadata)
if err != nil {
return "", fmt.Errorf("error appending metadata: %v", err)
}
outVersion = t
}
outString := outVersion.String()
if prefix != "" {
outString = fmt.Sprintf("%s%s", prefix, outString)
}
return outString, nil
}
func determineFromPattern(pattern string) (major bool, minor bool, patch bool) {
splitPattern := strings.Split(pattern, ".")
// loop through the pattern so we can determine which pieces should increment
for i, p := range splitPattern {
if p == "^" {
// determine which number to increment based on the index of the ^
switch i {
case 0:
major = true
break
case 1:
minor = true
break
case 2:
patch = true
break
}
}
}
return
}
func incVersion(version semver.Version, pattern string) semver.Version {
major, minor, patch := determineFromPattern(pattern)
// no increment desired
if !major && !minor && !patch {
minor = true
}
if major {
version = version.IncMajor()
}
if minor {
version = version.IncMinor()
}
if patch {
version = version.IncPatch()
}
return version
}
func main() {
v, err := Process(version, incPattern, versionPrerelease, versionMetadata, versionPrefix, ignorePrevious)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
os.Exit(1)
}
fmt.Printf("%s\n", v)
}