diff --git a/README.md b/README.md index 14ef27c..2bf5dd8 100644 --- a/README.md +++ b/README.md @@ -61,11 +61,29 @@ func main() { # Behind the curtain +These are the commands that are used under the hood to produce the versions. + +Shows the git tag + description. Assumes that you're using the semver format `v1.0.0` for your base tags. + ```bash git describe --tags --dirty --always # v1.0.0 -# v1.0.0-1 - -git log --format='format:%cI' -n 1 --since=(git describe --tags --dirty --always) -git rev-parse HEAD +# v1.0.0-1-g0000000 +# v1.0.0-dirty +``` + +Show the commit date (when the commit made it into the current tree). +Internally we use the current date when the working tree is dirty. + +```bash +git show v1.0.0-1-g0000000 --format=%cd --date=format:%Y-%m-%dT%H:%M:%SZ%z --no-patch +# 2010-01-01T20:30:00Z-0600 +# fatal: ambiguous argument 'v1.0.0-1-g0000000-dirty': unknown revision or path not in the working tree. +``` + +Shows the most recent commit. + +```bash +git rev-parse HEAD +# 0000000000000000000000000000000000000000 ``` diff --git a/gitver.go b/gitver.go index 56feed3..209f7e9 100644 --- a/gitver.go +++ b/gitver.go @@ -19,13 +19,19 @@ var exactVer *regexp.Regexp var gitVer *regexp.Regexp var verFile = "generated-version.go" +var ( + GitRev = "0000000" + GitVersion = "v0.0.0-pre0+g0000000" + GitTimestamp = "0000-00-00T00:00:00+0000" +) + func init() { // exactly vX.Y.Z (go-compatible semver) exactVer = regexp.MustCompile(`^v\d+\.\d+\.\d+$`) // vX.Y.Z-n-g0000000 git post-release, semver prerelease // vX.Y.Z-dirty git post-release, semver prerelease - gitVer = regexp.MustCompile(`^(v\d+\.\d+)\.(\d+)-((\d+)-)?(dirty|g)`) + gitVer = regexp.MustCompile(`^(v\d+\.\d+)\.(\d+)(-(\d+))?(-(g[0-9a-f]+))?(-(dirty))?`) } func main() { @@ -34,6 +40,11 @@ func main() { arg := args[i] if "-f" == arg || "--fail" == arg { exitCode = 1 + } else if "-V" == arg || "version" == arg || "-version" == arg || "--version" == arg { + fmt.Println(GitRev) + fmt.Println(GitVersion) + fmt.Println(GitTimestamp) + os.Exit(0) } } @@ -46,7 +57,6 @@ func main() { ver := semVer(desc) ts, err := gitTimestamp(desc) if nil != err { - fmt.Println("badtimes", err) // TODO remove ts = time.Now() } @@ -137,6 +147,11 @@ func semVer(desc string) string { } // v1.0.1-pre1 ver = fmt.Sprintf("%s.%d-pre%s", vers[1], patch+1, vers[4]) + fmt.Println(desc, vers) + if "dirty" == vers[8] { + //if strings.Contains(desc, "dirty") { + ver += "+dirty" + } } return ver } @@ -151,9 +166,6 @@ func gitTimestamp(desc string) (time.Time, error) { } cmd := exec.Command(args[0], args[1:]...) out, err := cmd.CombinedOutput() - fmt.Printf("args:\n%#v\n\n", args) - fmt.Printf("in:\n%s\n\n", strings.Join(args, " ")) - fmt.Println("out:\n\n", string(out), err) if nil != err { // a dirty desc was probably used return time.Time{}, err