diff --git a/internal/controller/artifact.go b/internal/controller/artifact.go index bebc8d5ae..e386990ee 100644 --- a/internal/controller/artifact.go +++ b/internal/controller/artifact.go @@ -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 } diff --git a/internal/controller/artifact_test.go b/internal/controller/artifact_test.go index d40548c3c..e802641ba 100644 --- a/internal/controller/artifact_test.go +++ b/internal/controller/artifact_test.go @@ -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) {