Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select larger tag for commits with multiple lightweight tags #41

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
17 changes: 14 additions & 3 deletions version/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,25 @@ func getTagMap(repo *git.Repository, match func(string) bool) (map[string]Tag, e
if err != nil {
return nil
}
tagName := ref.Name().Short()
if !match(tagName) {
return nil
}
hash := commit.Hash.String()
if c, ok := result[hash]; ok && !commit.Committer.When.After(c.When) {
c, ok := result[hash]
if !ok {
result[hash] = Tag{Name: tagName, When: commit.Committer.When}
return nil
}
tagName := ref.Name().Short()
if match(tagName) {
// two tags on the same commit, select the larger one.
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}
}
return nil
default:
return err
}
Expand Down
25 changes: 25 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ 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
}
if v.Patch != o.Patch {
return v.Patch - o.Patch
}
if v.Commits != o.Commits {
return v.Commits - o.Commits
}
if v.Meta != o.Meta {
return strings.Compare(v.Meta, o.Meta)
}
// alphabetic order
return strings.Compare(v.String(), o.String())
}

// 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
13 changes: 13 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,16 @@ 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)
assert.Equal(t, v.Compare(&Version{Major: 1, Minor: 2, Patch: 3}), 0)

v = Version{Major: 1, Minor: 2, Patch: 3, Meta: "dev.2"}
assert.Equal(t, v.Compare(&Version{Major: 1, Minor: 2, Patch: 3, Meta: "dev.1"}), 1)
assert.Equal(t, v.Compare(&Version{Major: 1, Minor: 2, Patch: 3, Meta: "dev.5"}), -1)
assert.Equal(t, v.Compare(&Version{Major: 1, Minor: 2, Patch: 3, Meta: "dev.2"}), 0)
}