Skip to content

Commit d2890f3

Browse files
magmaxjannfis
authored andcommitted
Multiple tags on the same image return same order (#173)
1 parent 147b465 commit d2890f3

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

pkg/tag/tag.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ func (il ImageTagList) SortByDate() SortableImageTagList {
117117
sil = append(sil, v)
118118
}
119119
sort.Slice(sil, func(i, j int) bool {
120+
if sil[i].TagDate.Equal(*sil[j].TagDate) {
121+
// if an image has two tags, return the same consistently
122+
return sil[i].TagName < sil[j].TagName
123+
}
120124
return sil[i].TagDate.Before(*sil[j].TagDate)
121125
})
122126
return sil

pkg/tag/tag_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,23 @@ func Test_SortableImageTagList(t *testing.T) {
9898
assert.Equal(t, "v2.0.3", sil[3].TagName)
9999
assert.Equal(t, "v2.0.2", sil[4].TagName)
100100
})
101+
102+
t.Run("Sort by date with same dates", func(t *testing.T) {
103+
names := []string{"v2.0.2", "v1.0", "v1.0.1", "v2.0.3", "v2.0"}
104+
date := time.Unix(0, 0)
105+
il := NewImageTagList()
106+
for _, name := range names {
107+
tag := NewImageTag(name, date)
108+
il.Add(tag)
109+
}
110+
sil := il.SortByDate()
111+
require.Len(t, sil, len(names))
112+
assert.Equal(t, "v1.0", sil[0].TagName)
113+
assert.Equal(t, "v1.0.1", sil[1].TagName)
114+
assert.Equal(t, "v2.0", sil[2].TagName)
115+
assert.Equal(t, "v2.0.2", sil[3].TagName)
116+
assert.Equal(t, "v2.0.3", sil[4].TagName)
117+
})
101118
}
102119

103120
func Test_TagsFromTagList(t *testing.T) {

0 commit comments

Comments
 (0)