Skip to content

Commit 5ba167d

Browse files
author
Michal Minář
committed
Version prefix matters when sorting tags names
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>
1 parent 70b4b14 commit 5ba167d

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

pkg/image/apis/image/helper.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,7 @@ type prioritizedTag struct {
856856
tag string
857857
priority tagPriority
858858
semver semver.Version
859+
prefix string
859860
}
860861

861862
func prioritizeTag(tag string) prioritizedTag {
@@ -866,14 +867,20 @@ func prioritizeTag(tag string) prioritizedTag {
866867
}
867868
}
868869

869-
short := strings.TrimLeft(tag, "v")
870+
short := tag
871+
prefix := ""
872+
if strings.HasPrefix(tag, "v") {
873+
prefix = "v"
874+
short = tag[1:]
875+
}
870876

871877
// 5.1.3
872878
if v, err := semver.Parse(short); err == nil {
873879
return prioritizedTag{
874880
tag: tag,
875881
priority: tagPriorityFull,
876882
semver: v,
883+
prefix: prefix,
877884
}
878885
}
879886

@@ -884,6 +891,7 @@ func prioritizeTag(tag string) prioritizedTag {
884891
tag: tag,
885892
priority: tagPriorityMinor,
886893
semver: v,
894+
prefix: prefix,
887895
}
888896
}
889897
}
@@ -895,6 +903,7 @@ func prioritizeTag(tag string) prioritizedTag {
895903
tag: tag,
896904
priority: tagPriorityMinor,
897905
semver: v,
906+
prefix: prefix,
898907
}
899908
}
900909
}
@@ -903,6 +912,7 @@ func prioritizeTag(tag string) prioritizedTag {
903912
return prioritizedTag{
904913
tag: tag,
905914
priority: tagPriorityOther,
915+
prefix: prefix,
906916
}
907917
}
908918

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

922932
cmp := t[i].semver.Compare(t[j].semver)
923-
return cmp > 0 // the newer tag has a higher priority
933+
if cmp > 0 { // the newer tag has a higher priority
934+
return true
935+
}
936+
return t[i].prefix < t[j].prefix
924937
}
925938

926939
// PrioritizeTags orders a set of image tags with a few conventions:

pkg/image/apis/image/helper_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ func TestPrioritizeTags(t *testing.T) {
11611161
}{
11621162
{
11631163
tags: []string{"other", "latest", "v5.5", "5.2.3", "v5.3.6-bother", "5.3.6-abba", "5.6"},
1164-
expected: []string{"latest", "5.6", "v5.5", "v5.3.6-bother", "5.3.6-abba", "5.2.3", "other"},
1164+
expected: []string{"latest", "5.6", "v5.5", "5.3.6-abba", "v5.3.6-bother", "5.2.3", "other"},
11651165
},
11661166
{
11671167
tags: []string{"1.1-beta1", "1.2-rc1", "1.1-rc1", "1.1-beta2", "1.2-beta1", "1.2-alpha1", "1.2-beta4", "latest"},
@@ -1171,6 +1171,10 @@ func TestPrioritizeTags(t *testing.T) {
11711171
tags: []string{"7.1", "v7.1", "7.1.0"},
11721172
expected: []string{"7.1", "v7.1", "7.1.0"},
11731173
},
1174+
{
1175+
tags: []string{"7.1.0", "v7.1", "7.1"},
1176+
expected: []string{"7.1", "v7.1", "7.1.0"},
1177+
},
11741178
}
11751179

11761180
for _, tc := range tests {

0 commit comments

Comments
 (0)