diff --git a/testdata/vfile1 b/testdata/vfile1 new file mode 100644 index 0000000..0097a10 --- /dev/null +++ b/testdata/vfile1 @@ -0,0 +1,2 @@ +0.1.2 +version 1.2.3 diff --git a/testdata/vfile2 b/testdata/vfile2 new file mode 100644 index 0000000..80e78df --- /dev/null +++ b/testdata/vfile2 @@ -0,0 +1 @@ +1.3.5 diff --git a/versionfile.go b/versionfile.go index b853a68..e8245b6 100644 --- a/versionfile.go +++ b/versionfile.go @@ -14,11 +14,15 @@ import ( "github.com/saracen/walker" ) -const versionRegBase = `(?i)((?:^|[^-_0-9a-zA-Z])version[^-_0-9a-zA-Z].{0,50})` +const ( + versionRegBase = `(?i)((?:^|[^-_0-9a-zA-Z])version[^-_0-9a-zA-Z].{0,50})` + semverRegBase = `([0-9]+\.[0-9]+\.[0-9]+)` +) var ( - versionReg = regexp.MustCompile(versionRegBase + `([0-9]+\.[0-9]+\.[0-9]+)`) - skipDirs = map[string]bool{ + versionReg = regexp.MustCompile(versionRegBase + semverRegBase) + versionRegFallback = regexp.MustCompile(semverRegBase) + skipDirs = map[string]bool{ // The "testdata" directory is ommited because of the test code for tagpr itself "testdata": true, ".git": true, @@ -190,11 +194,16 @@ func retrieveVersionFromFile(fpath string, vPrefix bool) (*semv, error) { if err != nil { return nil, err } - m := versionReg.FindSubmatch(bs) - if len(m) < 3 { - return nil, fmt.Errorf("no version detected from file: %s", fpath) + var ver string + if m := versionReg.FindSubmatch(bs); len(m) >= 3 { + ver = string(m[2]) + } else { + m := versionRegFallback.FindSubmatch(bs) + if len(m) < 2 { + return nil, fmt.Errorf("no version detected from file: %s", fpath) + } + ver = string(m[1]) } - ver := string(m[2]) if vPrefix { ver = "v" + ver } diff --git a/versionfile_test.go b/versionfile_test.go index 59a88c4..aae52e7 100644 --- a/versionfile_test.go +++ b/versionfile_test.go @@ -35,6 +35,16 @@ func TestRetrieveVersionFile(t *testing.T) { if ver.Naked() != version { t.Errorf("detected: %s, expected: %s", ver.Naked(), version) } + + ver, err = retrieveVersionFromFile("testdata/vfile1", true) + if e, g := "v1.2.3", ver.Tag(); e != g { + t.Errorf("got: %s, expected: %s", g, e) + } + + ver, err = retrieveVersionFromFile("testdata/vfile2", false) + if e, g := "1.3.5", ver.Tag(); e != g { + t.Errorf("got: %s, expected: %s", g, e) + } } func TestDetectVersionFile(t *testing.T) {