Skip to content

Commit e1d52fc

Browse files
committed
Suppress diff output by regex
Signed-off-by: Jan-Otto Kröpke <[email protected]>
1 parent 882b211 commit e1d52fc

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

cmd/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func AddDiffOptions(f *pflag.FlagSet, o *diff.Options) {
1515
f.StringVar(&o.OutputFormat, "output", "diff", "Possible values: diff, simple, template, dyff. When set to \"template\", use the env var HELM_DIFF_TPL to specify the template.")
1616
f.BoolVar(&o.StripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
1717
f.Float32VarP(&o.FindRenames, "find-renames", "D", 0, "Enable rename detection if set to any value greater than 0. If specified, the value denotes the maximum fraction of changed content as lines added + removed compared to total lines in a diff for considering it a rename. Only objects of the same Kind are attempted to be matched")
18+
f.StringArrayVar(&o.SuppressedOutputLineRegex, "suppress-output-line-regex", []string{}, "a regex to suppress diff output lines that match")
1819
}
1920

2021
// ProcessDiffOptions processes the set flags and handles possible interactions between them

diff/diff.go

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"math"
8+
"regexp"
89
"sort"
910
"strings"
1011

@@ -20,12 +21,13 @@ import (
2021

2122
// Options are all the options to be passed to generate a diff
2223
type Options struct {
23-
OutputFormat string
24-
OutputContext int
25-
StripTrailingCR bool
26-
ShowSecrets bool
27-
SuppressedKinds []string
28-
FindRenames float32
24+
OutputFormat string
25+
OutputContext int
26+
StripTrailingCR bool
27+
ShowSecrets bool
28+
SuppressedKinds []string
29+
FindRenames float32
30+
SuppressedOutputLineRegex []string
2931
}
3032

3133
// Manifests diff on manifests
@@ -64,8 +66,55 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, options *O
6466
doDiff(&report, key, nil, newContent, options)
6567
}
6668

69+
filteredReport := Report{}
70+
if len(options.SuppressedOutputLineRegex) > 0 {
71+
filteredReport.format = report.format
72+
filteredReport.entries = []ReportEntry{}
73+
74+
var suppressOutputRegexes []*regexp.Regexp
75+
76+
for _, suppressOutputRegex := range options.SuppressedOutputLineRegex {
77+
regexp, err := regexp.Compile(suppressOutputRegex)
78+
if err != nil {
79+
panic(err)
80+
}
81+
82+
suppressOutputRegexes = append(suppressOutputRegexes, regexp)
83+
}
84+
85+
for _, entry := range report.entries {
86+
var diffs []difflib.DiffRecord
87+
88+
for _, diff := range entry.diffs {
89+
matched := false
90+
91+
for _, suppressOutputRegex := range suppressOutputRegexes {
92+
if suppressOutputRegex.MatchString(diff.Payload) {
93+
matched = true
94+
break
95+
}
96+
}
97+
98+
if !matched {
99+
diffs = append(diffs, diff)
100+
}
101+
}
102+
103+
// Add entry to the report, if diffs are present.
104+
for _, diff := range diffs {
105+
if diff.Delta.String() != " " {
106+
filteredReport.addEntry(entry.key, entry.suppressedKinds, entry.kind, entry.context, diffs, entry.changeType)
107+
break
108+
}
109+
}
110+
}
111+
} else {
112+
filteredReport = report
113+
}
114+
67115
seenAnyChanges := len(report.entries) > 0
68-
report.print(to)
116+
filteredReport.print(to)
117+
filteredReport.clean()
69118
report.clean()
70119
return seenAnyChanges
71120
}

0 commit comments

Comments
 (0)