Skip to content

Commit

Permalink
Version prefix matters when sorting tags names
Browse files Browse the repository at this point in the history
Make sure that 'v' prefix is taken into account.

Sort identical tag names with 'v' prefix at the end.

Signed-off-by: Michal Minář <miminar@redhat.com>
  • Loading branch information
Michal Minář committed Nov 23, 2017
1 parent 0f6440d commit 691009b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
17 changes: 15 additions & 2 deletions pkg/image/apis/image/helper.go
Expand Up @@ -856,6 +856,7 @@ type prioritizedTag struct {
tag string
priority tagPriority
semver semver.Version
prefix string
}

func prioritizeTag(tag string) prioritizedTag {
Expand All @@ -866,14 +867,20 @@ func prioritizeTag(tag string) prioritizedTag {
}
}

short := strings.TrimLeft(tag, "v")
short := tag
prefix := ""
if strings.HasPrefix(tag, "v") {
prefix = "v"
short = tag[1:]
}

// 5.1.3
if v, err := semver.Parse(short); err == nil {
return prioritizedTag{
tag: tag,
priority: tagPriorityFull,
semver: v,
prefix: prefix,
}
}

Expand All @@ -884,6 +891,7 @@ func prioritizeTag(tag string) prioritizedTag {
tag: tag,
priority: tagPriorityMinor,
semver: v,
prefix: prefix,
}
}
}
Expand All @@ -895,6 +903,7 @@ func prioritizeTag(tag string) prioritizedTag {
tag: tag,
priority: tagPriorityMinor,
semver: v,
prefix: prefix,
}
}
}
Expand All @@ -903,6 +912,7 @@ func prioritizeTag(tag string) prioritizedTag {
return prioritizedTag{
tag: tag,
priority: tagPriorityOther,
prefix: prefix,
}
}

Expand All @@ -920,7 +930,10 @@ func (t prioritizedTags) Less(i, j int) bool {
}

cmp := t[i].semver.Compare(t[j].semver)
return cmp > 0 // the newer tag has a higher priority
if cmp > 0 { // the newer tag has a higher priority
return true
}
return t[i].prefix < t[j].prefix
}

// PrioritizeTags orders a set of image tags with a few conventions:
Expand Down
6 changes: 5 additions & 1 deletion pkg/image/apis/image/helper_test.go
Expand Up @@ -1161,7 +1161,7 @@ func TestPrioritizeTags(t *testing.T) {
}{
{
tags: []string{"other", "latest", "v5.5", "5.2.3", "v5.3.6-bother", "5.3.6-abba", "5.6"},
expected: []string{"latest", "5.6", "v5.5", "v5.3.6-bother", "5.3.6-abba", "5.2.3", "other"},
expected: []string{"latest", "5.6", "v5.5", "5.3.6-abba", "v5.3.6-bother", "5.2.3", "other"},
},
{
tags: []string{"1.1-beta1", "1.2-rc1", "1.1-rc1", "1.1-beta2", "1.2-beta1", "1.2-alpha1", "1.2-beta4", "latest"},
Expand All @@ -1171,6 +1171,10 @@ func TestPrioritizeTags(t *testing.T) {
tags: []string{"7.1", "v7.1", "7.1.0"},
expected: []string{"7.1", "v7.1", "7.1.0"},
},
{
tags: []string{"7.1.0", "v7.1", "7.1"},
expected: []string{"7.1", "v7.1", "7.1.0"},
},
}

for _, tc := range tests {
Expand Down

0 comments on commit 691009b

Please sign in to comment.