Skip to content

Commit d735f69

Browse files
committed
Fix random tag selection if two are equal
Also add tests, add workflow for tests, add CODEOWNERS
1 parent d04ac5f commit d735f69

File tree

6 files changed

+88
-5
lines changed

6 files changed

+88
-5
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @csmith

.github/workflows/test.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: run go tests
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
9+
jobs:
10+
build:
11+
uses: csmith/github-workflows/.github/workflows/go-test.yml@master

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# v1.1.0 (Fri Jul 30 2021)
2+
3+
- GetLatestTag will now return a stable answer if there are multiple, equivalent
4+
tags in the repository (e.g. `refs/tags/v1.0.0` and `refs/tags/1.0.0`). Previously
5+
the returned tag would be randomly selected.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module github.com/csmith/gitrefs
22

3-
go 1.17
3+
go 1.20
44

55
require github.com/hashicorp/go-version v1.6.0

tags.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,23 @@ func LatestTag(url string, options ...Option) (string, string, error) {
1616
return "", "", err
1717
}
1818

19+
return latestTag(refs)
20+
}
21+
22+
func latestTag(refs map[string]string) (string, string, error) {
1923
best := version.Must(version.NewVersion("0.0.0"))
2024
bestTag := ""
2125
bestHash := ""
2226
for r := range refs {
2327
tag := strings.TrimPrefix(r, tagPrefix)
2428
v, err := version.NewVersion(tag)
25-
if err == nil && v.GreaterThanOrEqual(best) && v.Prerelease() == "" {
26-
best = v
27-
bestTag = tag
28-
bestHash = refs[r]
29+
if err == nil && v.Prerelease() == "" {
30+
println(v.Original(), best.Original(), v.GreaterThan(best), v.Equal(best), strings.Compare(v.Original(), best.Original()) < 0)
31+
if v.GreaterThan(best) || (v.Equal(best) && strings.Compare(v.Original(), best.Original()) < 0) {
32+
best = v
33+
bestTag = tag
34+
bestHash = refs[r]
35+
}
2936
}
3037
}
3138

tags_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package gitrefs
2+
3+
import "testing"
4+
5+
const (
6+
commit1 = "a1b2c3d4e5f6"
7+
commit2 = "f6e5d4c3b2a1"
8+
commit3 = "1a2b3c4d5e6f"
9+
)
10+
11+
func Test_latestTag(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
args map[string]string
15+
wantTag string
16+
wantHash string
17+
wantErr bool
18+
}{
19+
{"No refs", map[string]string{}, "", "", true},
20+
{"No tags", map[string]string{
21+
"refs/heads/master": commit1,
22+
"refs/heads/v1.0.0": commit2,
23+
}, "", "", true},
24+
{"Single tag", map[string]string{
25+
"refs/heads/master": commit1,
26+
"refs/heads/v1.0.0": commit2,
27+
"refs/tags/v1.0.0": commit3,
28+
}, "v1.0.0", commit3, false},
29+
{"Multiple tags", map[string]string{
30+
"refs/tags/v0.9.9": commit1,
31+
"refs/tags/v1.0.0": commit2,
32+
"refs/tags/v1.0.1": commit3,
33+
}, "v1.0.1", commit3, false},
34+
{"Pre-release tags", map[string]string{
35+
"refs/tags/v0.9.9": commit1,
36+
"refs/tags/v1.0.0-rc1": commit2,
37+
"refs/tags/v1.0.0-alpha7": commit3,
38+
}, "v0.9.9", commit1, false},
39+
{"Equal tags", map[string]string{
40+
"refs/tags/v1.0.0": commit1,
41+
"refs/tags/1.0.0": commit2,
42+
}, "1.0.0", commit2, false},
43+
}
44+
for _, tt := range tests {
45+
t.Run(tt.name, func(t *testing.T) {
46+
tag, hash, err := latestTag(tt.args)
47+
if (err != nil) != tt.wantErr {
48+
t.Errorf("latestTag() error = %v, wantErr %v", err, tt.wantErr)
49+
return
50+
}
51+
if tag != tt.wantTag {
52+
t.Errorf("latestTag() tag = %v, wantTag %v", tag, tt.wantTag)
53+
}
54+
if hash != tt.wantHash {
55+
t.Errorf("latestTag() hash = %v, wantTag %v", hash, tt.wantHash)
56+
}
57+
})
58+
}
59+
}

0 commit comments

Comments
 (0)