]> git.lizzy.rs Git - micro.git/blob - tools/build-version.go
Ignore tool files
[micro.git] / tools / build-version.go
1 //+build ignore
2
3 package main
4
5 import (
6         "fmt"
7         "os/exec"
8         "strings"
9
10         "github.com/blang/semver"
11 )
12
13 func getTag(match ...string) (string, *semver.PRVersion) {
14         args := append([]string{
15                 "describe", "--tags",
16         }, match...)
17         if tag, err := exec.Command("git", args...).Output(); err != nil {
18                 return "", nil
19         } else {
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
24                         }
25                 } else if len(tagParts) == 4 {
26                         if ahead, err := semver.NewPRVersion(tagParts[2]); err == nil {
27                                 return tagParts[0] + "-" + tagParts[1], &ahead
28                         }
29                 }
30
31                 return string(tag), nil
32         }
33 }
34
35 func main() {
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()
39         }
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)
43         if err != nil {
44                 // no version tag found so just return what ever we can find.
45                 fmt.Println("0.0.0-unknown")
46                 return
47         }
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())
54                 return
55         }
56
57         // If we don't have any tag assume "dev"
58         if tag == "" || strings.HasPrefix(tag, "nightly") {
59                 tag = "dev"
60         }
61         // Get the most likely next version:
62         if !strings.Contains(version.String(), "rc") {
63                 version.Patch = version.Patch + 1
64         }
65
66         if pr, err := semver.NewPRVersion(tag); err == nil {
67                 // append the tag as pre-release name
68                 version.Pre = append(version.Pre, pr)
69         }
70
71         if ahead != nil {
72                 // if we know how many commits we are ahead of the last release, append that too.
73                 version.Pre = append(version.Pre, *ahead)
74         }
75
76         fmt.Println(version.String())
77 }