Merge pull request #281 from restic/version-with-git

build.go: use new combined version string
This commit is contained in:
Alexander Neumann 2015-08-26 20:53:24 +02:00
commit 9aefc2b7a6
1 changed files with 31 additions and 12 deletions

View File

@ -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()