Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ $ janus version -format='v%M.%m.%P+%C-%S'
%B, _B - hybrid patch version: `(%P * 100) + %C`
%C, _C - commit count since last tag
%S, _S - HEAD sha1 (first 7 characters)
%T, _T - full tag string
```
_Note_: you may use either `%M` or `_M` syntax to interpolate version variables, since escaping `%` in batch scripts is rather tricky.

Expand Down
8 changes: 7 additions & 1 deletion gitvv/gitvv.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,12 @@ func getLastTag(dir string) (string, bool) {

// Assumes using semver format for tags, eg v3.5.0 or 3.4.0
func parseSemverFromTag(s string) []string {
tag := strings.TrimPrefix(s, "v")
re := regexp.MustCompile(`\d*\.\d*\.\d*`)
tag := re.FindStringSubmatch(s)[0]
vers := strings.Split(tag, ".")
if len(vers) != 3 {
return nil
}
return vers
}

Expand Down Expand Up @@ -284,6 +288,8 @@ func GetVersion(format, dir string) string {
out = strings.Replace(out, "%B", "?", -1)
out = strings.Replace(out, "_B", "?", -1)
}
out = strings.Replace(out, "_T", lastTag, -1)
out = strings.Replace(out, "%T", lastTag, -1)

return out
}
8 changes: 8 additions & 0 deletions gitvv/gitvv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ func TestGetVersion(t *testing.T) {
{onTagDir, "v%M.%m.%P", "v0.0.1"},
{aboveTagDir, "v%M.%m.%P", "v0.0.1"},

{noTagsDir, "%T", ""},
{onTagDir, "%T-stable", "v0.0.1-stable"},
{aboveTagDir, "%T-unstable", "v0.0.1-unstable"},

{noTagsDir, "%C", "1"},
{onTagDir, "%C", "0"},
{aboveTagDir, "%C", "1"},
Expand Down Expand Up @@ -331,6 +335,10 @@ func Test_parseSemverFromTag(t *testing.T) {
}{
{"v0.1.7", []string{"0", "1", "7"}},
{"0.1.7", []string{"0", "1", "7"}},
{"v0.1.7-stable", []string{"0", "1", "7"}},
{"vv0.1.7.very-unstable", []string{"0", "1", "7"}},
{"V0.1.7.very...unstable", []string{"0", "1", "7"}},
{"1.0.is-this-a-feature-or-bug?", []string{"1","0"}}, // PTAL IDK.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IDK.

}

for _, tt := range table {
Expand Down