Skip to content

Commit cdc960f

Browse files
committed
Extract redaction/diff/report generation
The three steps are called in three places, we better combine them into a single function.
1 parent f56d020 commit cdc960f

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

diff/diff.go

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,58 +21,67 @@ import (
2121
// Manifests diff on manifests
2222
func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, suppressedKinds []string, showSecrets bool, context int, output string, stripTrailingCR bool, to io.Writer) bool {
2323
report.setupReportFormat(output)
24-
seenAnyChanges := false
25-
emptyMapping := &manifest.MappingResult{}
2624
for _, key := range sortedKeys(oldIndex) {
2725
oldContent := oldIndex[key]
2826

2927
if newContent, ok := newIndex[key]; ok {
30-
if oldContent.Content != newContent.Content {
31-
// modified
32-
if !showSecrets {
33-
redactSecrets(oldContent, newContent)
34-
}
35-
36-
diffs := diffMappingResults(oldContent, newContent, stripTrailingCR)
37-
if len(diffs) > 0 {
38-
seenAnyChanges = true
39-
}
40-
report.addEntry(key, suppressedKinds, oldContent.Kind, context, diffs, "MODIFY")
41-
}
28+
// modified?
29+
doDiff(key, oldContent, newContent, suppressedKinds, showSecrets, stripTrailingCR, context)
4230
} else {
4331
// removed
44-
if !showSecrets {
45-
redactSecrets(oldContent, nil)
46-
47-
}
48-
diffs := diffMappingResults(oldContent, emptyMapping, stripTrailingCR)
49-
if len(diffs) > 0 {
50-
seenAnyChanges = true
51-
}
52-
report.addEntry(key, suppressedKinds, oldContent.Kind, context, diffs, "REMOVE")
32+
doDiff(key, oldContent, nil, suppressedKinds, showSecrets, stripTrailingCR, context)
5333
}
5434
}
5535

5636
for _, key := range sortedKeys(newIndex) {
5737
newContent := newIndex[key]
5838

5939
if _, ok := oldIndex[key]; !ok {
60-
// added
61-
if !showSecrets {
62-
redactSecrets(nil, newContent)
63-
}
64-
diffs := diffMappingResults(emptyMapping, newContent, stripTrailingCR)
65-
if len(diffs) > 0 {
66-
seenAnyChanges = true
67-
}
68-
report.addEntry(key, suppressedKinds, newContent.Kind, context, diffs, "ADD")
40+
doDiff(key, nil, newContent, suppressedKinds, showSecrets, stripTrailingCR, context)
6941
}
7042
}
43+
44+
seenAnyChanges := len(report.entries) > 0
7145
report.print(to)
7246
report.clean()
7347
return seenAnyChanges
7448
}
7549

50+
func actualChanges(diff []difflib.DiffRecord) int {
51+
changes := 0
52+
for _, record := range diff {
53+
if record.Delta != difflib.Common {
54+
changes++
55+
}
56+
}
57+
return changes
58+
}
59+
60+
func doDiff(key string, oldContent *manifest.MappingResult, newContent *manifest.MappingResult, suppressedKinds []string, showSecrets bool, stripTrailingCR bool, context int) {
61+
if oldContent != nil && newContent != nil && oldContent.Content == newContent.Content {
62+
return
63+
}
64+
65+
if !showSecrets {
66+
redactSecrets(oldContent, newContent)
67+
}
68+
69+
if oldContent == nil {
70+
emptyMapping := &manifest.MappingResult{}
71+
diffs := diffMappingResults(emptyMapping, newContent, stripTrailingCR)
72+
report.addEntry(key, suppressedKinds, newContent.Kind, context, diffs, "ADD")
73+
} else if newContent == nil {
74+
emptyMapping := &manifest.MappingResult{}
75+
diffs := diffMappingResults(oldContent, emptyMapping, stripTrailingCR)
76+
report.addEntry(key, suppressedKinds, oldContent.Kind, context, diffs, "REMOVE")
77+
} else {
78+
diffs := diffMappingResults(oldContent, newContent, stripTrailingCR)
79+
if actualChanges(diffs) > 0 {
80+
report.addEntry(key, suppressedKinds, oldContent.Kind, context, diffs, "MODIFY")
81+
}
82+
}
83+
}
84+
7685
func redactSecrets(old, new *manifest.MappingResult) {
7786
if (old != nil && old.Kind != "Secret") || (new != nil && new.Kind != "Secret") {
7887
return

0 commit comments

Comments
 (0)