2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-25 22:27:35 +00:00

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

View File

@ -200,22 +200,41 @@ func test(gopath string, args ...string) error {
return cmd.Run() return cmd.Run()
} }
// getVersion returns a version string, either from the file VERSION in the // getVersion returns the version string from the file VERSION in the current
// current directory or from git. // directory.
func getVersion() string { func getVersionFromFile() string {
v, err := ioutil.ReadFile("VERSION") buf, err := ioutil.ReadFile("VERSION")
version := strings.TrimSpace(string(v)) if err != nil {
if err == nil { verbosePrintf("error reading file VERSION: %v\n", err)
verbosePrintf("version from file 'VERSION' is %s\n", version) return ""
return version
} }
return gitVersion() return strings.TrimSpace(string(buf))
} }
// gitVersion returns a version string that identifies the currently checked // getVersion returns a version string which is a combination of the contents
// out git commit. // of the file VERSION in the current directory and the version from git (if
func gitVersion() string { // 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", cmd := exec.Command("git", "describe",
"--long", "--tags", "--dirty", "--always") "--long", "--tags", "--dirty", "--always")
out, err := cmd.Output() out, err := cmd.Output()