10 "github.com/blang/semver"
13 func getTag(match ...string) (string, *semver.PRVersion) {
14 args := append([]string{
17 if tag, err := exec.Command("git", args...).Output(); err != nil {
20 tagParts := strings.Split(string(tag), "-")
21 if len(tagParts) == 3 {
22 if ahead, err := semver.NewPRVersion(tagParts[1]); err == nil {
23 return tagParts[0], &ahead
25 } else if len(tagParts) == 4 {
26 if ahead, err := semver.NewPRVersion(tagParts[2]); err == nil {
27 return tagParts[0] + "-" + tagParts[1], &ahead
31 return string(tag), nil
36 if tags, err := exec.Command("git", "tag").Output(); err != nil || len(tags) == 0 {
37 // no tags found -- fetch them
38 exec.Command("git", "fetch", "--tags").Run()
40 // Find the last vX.X.X Tag and get how many builds we are ahead of it.
41 versionStr, ahead := getTag("--match", "v*")
42 version, err := semver.ParseTolerant(versionStr)
44 // no version tag found so just return what ever we can find.
45 fmt.Println("0.0.0-unknown")
48 // Get the tag of the current revision.
49 tag, _ := getTag("--exact-match")
50 if tag == versionStr {
51 // Seems that we are going to build a release.
52 // So the version number should already be correct.
53 fmt.Println(version.String())
57 // If we don't have any tag assume "dev"
58 if tag == "" || strings.HasPrefix(tag, "nightly") {
61 // Get the most likely next version:
62 if !strings.Contains(version.String(), "rc") {
63 version.Patch = version.Patch + 1
66 if pr, err := semver.NewPRVersion(tag); err == nil {
67 // append the tag as pre-release name
68 version.Pre = append(version.Pre, pr)
72 // if we know how many commits we are ahead of the last release, append that too.
73 version.Pre = append(version.Pre, *ahead)
76 fmt.Println(version.String())