Skip to content
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
18 changes: 11 additions & 7 deletions internal/controller/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,24 @@ import (

type artifactSet []*meta.Artifact

// Diff returns true if any of the revisions in the artifactSet does not match any of the given artifacts.
// Diff returns true if the revisions in the artifactSet do not match those of
// the given set, regardless of order. A revision that appears more than once
// must appear the same number of times in both sets, otherwise a change in a
// set that holds duplicate revisions goes unnoticed.
func (s artifactSet) Diff(set artifactSet) bool {
if len(s) != len(set) {
return true
}

outer:
counts := make(map[string]int, len(set))
for _, k := range set {
counts[k.Revision]++
}
for _, j := range s {
for _, k := range set {
if k.HasRevision(j.Revision) {
continue outer
}
if counts[j.Revision] == 0 {
return true
}
return true
counts[j.Revision]--
}
return false
}
40 changes: 40 additions & 0 deletions internal/controller/artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,46 @@ func Test_artifactSet_Diff(t *testing.T) {
},
expected: true,
},
{
name: "duplicate revision replaces a distinct one, diff",
current: artifactSet{
{
Revision: "foo",
},
{
Revision: "foo",
},
},
updated: artifactSet{
{
Revision: "foo",
},
{
Revision: "bar",
},
},
expected: true,
},
{
name: "same revisions out of order, no diff",
current: artifactSet{
{
Revision: "foo",
},
{
Revision: "bar",
},
},
updated: artifactSet{
{
Revision: "bar",
},
{
Revision: "foo",
},
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down