diff --git a/build.go b/build.go index b6cfbcd55..f92bc3301 100644 --- a/build.go +++ b/build.go @@ -200,22 +200,41 @@ func test(gopath string, args ...string) error { return cmd.Run() } -// getVersion returns a version string, either from the file VERSION in the -// current directory or from git. -func getVersion() string { - v, err := ioutil.ReadFile("VERSION") - version := strings.TrimSpace(string(v)) - if err == nil { - verbosePrintf("version from file 'VERSION' is %s\n", version) - return version +// getVersion returns the version string from the file VERSION in the current +// directory. +func getVersionFromFile() string { + buf, err := ioutil.ReadFile("VERSION") + if err != nil { + verbosePrintf("error reading file VERSION: %v\n", err) + return "" } - return gitVersion() + return strings.TrimSpace(string(buf)) } -// gitVersion returns a version string that identifies the currently checked -// out git commit. -func gitVersion() string { +// getVersion returns a version string which is a combination of the contents +// of the file VERSION in the current directory and the version from git (if +// available). +func getVersion() string { + versionFile := getVersionFromFile() + versionGit := getVersionFromGit() + + verbosePrintf("version from file 'VERSION' is %q, version from git %q\n", + versionFile, versionGit) + + switch { + case versionFile == "": + return versionGit + case versionGit == "": + return versionFile + } + + return fmt.Sprintf("%s (%s)", versionFile, versionGit) +} + +// getVersionFromGit returns a version string that identifies the currently +// checked out git commit. +func getVersionFromGit() string { cmd := exec.Command("git", "describe", "--long", "--tags", "--dirty", "--always") out, err := cmd.Output()