Merge pull request #1451 from calmh/relverv2

Get version from RELEASE file if it exists, or guess from directory (fixes #1449)
This commit is contained in:
Audrius Butkevicius 2015-03-12 10:38:39 +00:00
commit d2d894d808
2 changed files with 55 additions and 3 deletions

1
.gitignore vendored
View File

@ -15,3 +15,4 @@ coverage.xml
.DS_Store .DS_Store
syncthing.md5 syncthing.md5
syncthing.exe.md5 syncthing.exe.md5
RELEASE

View File

@ -23,6 +23,7 @@ import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"crypto/md5" "crypto/md5"
"errors"
"flag" "flag"
"fmt" "fmt"
"io" "io"
@ -350,16 +351,66 @@ func rmr(paths ...string) {
} }
} }
func getVersion() string { func getReleaseVersion() (string, error) {
fd, err := os.Open("RELEASE")
if err != nil {
return "", err
}
defer fd.Close()
bs, err := ioutil.ReadAll(fd)
if err != nil {
return "", err
}
return string(bytes.TrimSpace(bs)), nil
}
func getGitVersion() (string, error) {
v, err := runError("git", "describe", "--always", "--dirty") v, err := runError("git", "describe", "--always", "--dirty")
if err != nil { if err != nil {
return "unknown-dev" return "", err
} }
v = versionRe.ReplaceAllFunc(v, func(s []byte) []byte { v = versionRe.ReplaceAllFunc(v, func(s []byte) []byte {
s[0] = '+' s[0] = '+'
return s return s
}) })
return string(v) return string(v), nil
}
func getDirectoryVersion() (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", err
}
base := filepath.Base(filepath.Clean(wd))
re := regexp.MustCompile(`syncthing-(v?\d+\.\d+\.\d+)`)
parts := re.FindStringSubmatch(base)
if len(parts) != 2 {
return "", errors.New("not a release directory")
}
if strings.HasPrefix(parts[1], "v") {
return parts[1], nil
}
return "v" + parts[1], nil
}
func getVersion() string {
// First try for a RELEASE file,
if ver, err := getReleaseVersion(); err == nil {
return ver
}
// ... then see if we have a Git tag,
if ver, err := getGitVersion(); err == nil {
return ver
}
// ... otherwise try to guess from the directory name.
if ver, err := getDirectoryVersion(); err == nil {
return ver
}
// This seems to be a dev build.
return "unknown-dev"
} }
func buildStamp() int64 { func buildStamp() int64 {