8 "github.com/blang/semver"
11 func getTag(match ...string) (string, *semver.PRVersion) {
12 args := append([]string{
15 if tag, err := exec.Command("git", args...).Output(); err != nil {
18 tagParts := strings.Split(string(tag), "-")
19 if len(tagParts) == 3 {
20 if ahead, err := semver.NewPRVersion(tagParts[1]); err == nil {
21 return tagParts[0], &ahead
23 } else if len(tagParts) == 4 {
24 if ahead, err := semver.NewPRVersion(tagParts[2]); err == nil {
25 return tagParts[0] + "-" + tagParts[1], &ahead
29 return string(tag), nil
34 // Find the last vX.X.X Tag and get how many builds we are ahead of it.
35 versionStr, ahead := getTag("--match", "v*")
36 version, err := semver.ParseTolerant(versionStr)
38 // no version tag found so just return what ever we can find.
39 fmt.Println("0.0.0-unknown")
42 // Get the tag of the current revision.
43 tag, _ := getTag("--exact-match")
44 if tag == versionStr {
45 // Seems that we are going to build a release.
46 // So the version number should already be correct.
47 fmt.Println(version.String())
51 // If we don't have any tag assume "dev"
55 // Get the most likely next version:
56 if !strings.Contains(version.String(), "rc") {
57 version.Patch = version.Patch + 1
60 if pr, err := semver.NewPRVersion(tag); err == nil {
61 // append the tag as pre-release name
62 version.Pre = append(version.Pre, pr)
66 // if we know how many commits we are ahead of the last release, append that too.
67 version.Pre = append(version.Pre, *ahead)
70 fmt.Println(version.String())