Skip to content

Commit

Permalink
Select larger tag for commits with multiple lightweight tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Weiming Zhao committed Jun 15, 2024
1 parent 17c6da6 commit af7e394
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
17 changes: 12 additions & 5 deletions version/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,20 @@ func getTagMap(repo *git.Repository, match func(string) bool) (map[string]Tag, e
if err != nil {
return nil
}
hash := commit.Hash.String()
if c, ok := result[hash]; ok && !commit.Committer.When.After(c.When) {
tagName := ref.Name().Short()

if !match(tagName) {
return nil
}
tagName := ref.Name().Short()
if match(tagName) {
result[hash] = Tag{Name: tagName, When: commit.Committer.When}
hash := commit.Hash.String()
if c, ok := result[hash]; !ok || (ok && !commit.Committer.When.After(c.When)) {
h0 := RepoHead{c.Name, 0, hash}
h1 := RepoHead{tagName, 0, hash}
v0, err0 := NewFromHead(&h0, "")
v1, err1 := NewFromHead(&h1, "")
if err0 != nil || (err1 == nil && v1.Compare(&v0) > 0) {
result[hash] = Tag{Name: tagName, When: commit.Committer.When}
}
}
default:
return err
Expand Down
15 changes: 15 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ type Version struct {
Meta string
}

// Return 0 if both versions are equal
// Compares two versions. It returns
// - 0 if both versions are equal
// - some positive number if v > o
// - some negative number if v < o
func (v Version) Compare(o *Version) int {
if v.Major != o.Major {
return v.Major - o.Major
}
if v.Minor != o.Minor {
return v.Minor - o.Minor
}
return v.Patch - o.Patch
}

// BumpTo increases the version to the next patch/minor/major version. The version components with
// lower priority than the update target will be reset to zero.
//
Expand Down
7 changes: 7 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,10 @@ func TestParseRelease(t *testing.T) {
require.NoError(t, target.Set("major"))
assert.Equal(t, Major, target)
}

func TestVersionCompare(t *testing.T) {
v := Version{Major: 1, Minor: 2, Patch: 3}
assert.Equal(t, v.Compare(&Version{Major: 1, Minor: 3, Patch: 0}), -1)
assert.Equal(t, v.Compare(&Version{Major: 1, Minor: 0, Patch: 0}), 2)
assert.Equal(t, v.Compare(&Version{Major: 1, Minor: 2, Patch: 3}), 0)
}

0 comments on commit af7e394

Please sign in to comment.