Skip to content

feat: --find-renames for discovering renames based on content #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 22, 2022
Merged
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
24 changes: 24 additions & 0 deletions cmd/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"github.com/databus23/helm-diff/v3/diff"
"github.com/spf13/pflag"
)

// AddDiffOptions adds flags for the various consolidated options to the functions in the diff package
func AddDiffOptions(f *pflag.FlagSet, o *diff.Options) {
f.BoolP("suppress-secrets", "q", false, "suppress secrets in the output")
f.BoolVar(&o.ShowSecrets, "show-secrets", false, "do not redact secret values in the output")
f.StringArrayVar(&o.SuppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
f.IntVarP(&o.OutputContext, "context", "C", -1, "output NUM lines of context around changes")
f.StringVar(&o.OutputFormat, "output", "diff", "Possible values: diff, simple, template. When set to \"template\", use the env var HELM_DIFF_TPL to specify the template.")
f.BoolVar(&o.StripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
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")
}

// ProcessDiffOptions processes the set flags and handles possible interactions between them
func ProcessDiffOptions(f *pflag.FlagSet, o *diff.Options) {
if q, _ := f.GetBool("suppress-secrets"); q {
o.SuppressedKinds = append(o.SuppressedKinds, "Secret")
}
}
29 changes: 5 additions & 24 deletions cmd/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ import (
type release struct {
client helm.Interface
detailedExitCode bool
suppressedKinds []string
releases []string
outputContext int
includeTests bool
showSecrets bool
output string
stripTrailingCR bool
normalizeManifests bool
diff.Options
}

const releaseCmdLongUsage = `
Expand Down Expand Up @@ -59,9 +55,7 @@ func releaseCmd() *cobra.Command {
return errors.New("Too few arguments to Command \"release\".\nMinimum 2 arguments required: release name-1, release name-2")
}

if q, _ := cmd.Flags().GetBool("suppress-secrets"); q {
diff.suppressedKinds = append(diff.suppressedKinds, "Secret")
}
ProcessDiffOptions(cmd.Flags(), &diff.Options)

diff.releases = args[0:]
if isHelm3() {
Expand All @@ -74,15 +68,10 @@ func releaseCmd() *cobra.Command {
},
}

releaseCmd.Flags().BoolP("suppress-secrets", "q", false, "suppress secrets in the output")
releaseCmd.Flags().BoolVar(&diff.showSecrets, "show-secrets", false, "do not redact secret values in the output")
releaseCmd.Flags().BoolVar(&diff.detailedExitCode, "detailed-exitcode", false, "return a non-zero exit code when there are changes")
releaseCmd.Flags().StringArrayVar(&diff.suppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
releaseCmd.Flags().IntVarP(&diff.outputContext, "context", "C", -1, "output NUM lines of context around changes")
releaseCmd.Flags().BoolVar(&diff.includeTests, "include-tests", false, "enable the diffing of the helm test hooks")
releaseCmd.Flags().StringVar(&diff.output, "output", "diff", "Possible values: diff, simple, template. When set to \"template\", use the env var HELM_DIFF_TPL to specify the template.")
releaseCmd.Flags().BoolVar(&diff.stripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
releaseCmd.Flags().BoolVar(&diff.normalizeManifests, "normalize-manifests", false, "normalize manifests before running diff to exclude style differences from the output")
AddDiffOptions(releaseCmd.Flags(), &diff.Options)

releaseCmd.SuggestionsMinimumDistance = 1

Expand Down Expand Up @@ -121,11 +110,7 @@ func (d *release) differentiateHelm3() error {
seenAnyChanges := diff.Releases(
manifest.Parse(string(releaseResponse1), namespace, d.normalizeManifests, excludes...),
manifest.Parse(string(releaseResponse2), namespace, d.normalizeManifests, excludes...),
d.suppressedKinds,
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
&d.Options,
os.Stdout)

if d.detailedExitCode && seenAnyChanges {
Expand Down Expand Up @@ -156,11 +141,7 @@ func (d *release) differentiate() error {
seenAnyChanges := diff.Releases(
manifest.ParseRelease(releaseResponse1.Release, d.includeTests, d.normalizeManifests),
manifest.ParseRelease(releaseResponse2.Release, d.includeTests, d.normalizeManifests),
d.suppressedKinds,
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
&d.Options,
os.Stdout)

if d.detailedExitCode && seenAnyChanges {
Expand Down
41 changes: 7 additions & 34 deletions cmd/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ type revision struct {
release string
client helm.Interface
detailedExitCode bool
suppressedKinds []string
revisions []string
outputContext int
includeTests bool
showSecrets bool
output string
stripTrailingCR bool
normalizeManifests bool
diff.Options
}

const revisionCmdLongUsage = `
Expand Down Expand Up @@ -68,9 +64,7 @@ func revisionCmd() *cobra.Command {
return errors.New("Too many arguments to Command \"revision\".\nMaximum 3 arguments allowed: release name, revision1, revision2")
}

if q, _ := cmd.Flags().GetBool("suppress-secrets"); q {
diff.suppressedKinds = append(diff.suppressedKinds, "Secret")
}
ProcessDiffOptions(cmd.Flags(), &diff.Options)

diff.release = args[0]
diff.revisions = args[1:]
Expand All @@ -84,15 +78,10 @@ func revisionCmd() *cobra.Command {
},
}

revisionCmd.Flags().BoolP("suppress-secrets", "q", false, "suppress secrets in the output")
revisionCmd.Flags().BoolVar(&diff.showSecrets, "show-secrets", false, "do not redact secret values in the output")
revisionCmd.Flags().BoolVar(&diff.detailedExitCode, "detailed-exitcode", false, "return a non-zero exit code when there are changes")
revisionCmd.Flags().StringArrayVar(&diff.suppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
revisionCmd.Flags().IntVarP(&diff.outputContext, "context", "C", -1, "output NUM lines of context around changes")
revisionCmd.Flags().BoolVar(&diff.includeTests, "include-tests", false, "enable the diffing of the helm test hooks")
revisionCmd.Flags().StringVar(&diff.output, "output", "diff", "Possible values: diff, simple, template. When set to \"template\", use the env var HELM_DIFF_TPL to specify the template.")
revisionCmd.Flags().BoolVar(&diff.stripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
revisionCmd.Flags().BoolVar(&diff.normalizeManifests, "normalize-manifests", false, "normalize manifests before running diff to exclude style differences from the output")
AddDiffOptions(revisionCmd.Flags(), &diff.Options)

revisionCmd.SuggestionsMinimumDistance = 1

Expand Down Expand Up @@ -126,11 +115,7 @@ func (d *revision) differentiateHelm3() error {
diff.Manifests(
manifest.Parse(string(revisionResponse), namespace, d.normalizeManifests, excludes...),
manifest.Parse(string(releaseResponse), namespace, d.normalizeManifests, excludes...),
d.suppressedKinds,
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
&d.Options,
os.Stdout)

case 2:
Expand All @@ -153,11 +138,7 @@ func (d *revision) differentiateHelm3() error {
seenAnyChanges := diff.Manifests(
manifest.Parse(string(revisionResponse1), namespace, d.normalizeManifests, excludes...),
manifest.Parse(string(revisionResponse2), namespace, d.normalizeManifests, excludes...),
d.suppressedKinds,
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
&d.Options,
os.Stdout)

if d.detailedExitCode && seenAnyChanges {
Expand Down Expand Up @@ -193,11 +174,7 @@ func (d *revision) differentiate() error {
diff.Manifests(
manifest.ParseRelease(revisionResponse.Release, d.includeTests, d.normalizeManifests),
manifest.ParseRelease(releaseResponse.Release, d.includeTests, d.normalizeManifests),
d.suppressedKinds,
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
&d.Options,
os.Stdout)

case 2:
Expand All @@ -220,11 +197,7 @@ func (d *revision) differentiate() error {
seenAnyChanges := diff.Manifests(
manifest.ParseRelease(revisionResponse1.Release, d.includeTests, d.normalizeManifests),
manifest.ParseRelease(revisionResponse2.Release, d.includeTests, d.normalizeManifests),
d.suppressedKinds,
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
&d.Options,
os.Stdout)

if d.detailedExitCode && seenAnyChanges {
Expand Down
29 changes: 5 additions & 24 deletions cmd/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ type rollback struct {
release string
client helm.Interface
detailedExitCode bool
suppressedKinds []string
revisions []string
outputContext int
includeTests bool
showSecrets bool
output string
stripTrailingCR bool
normalizeManifests bool
diff.Options
}

const rollbackCmdLongUsage = `
Expand Down Expand Up @@ -57,9 +53,7 @@ func rollbackCmd() *cobra.Command {
return err
}

if q, _ := cmd.Flags().GetBool("suppress-secrets"); q {
diff.suppressedKinds = append(diff.suppressedKinds, "Secret")
}
ProcessDiffOptions(cmd.Flags(), &diff.Options)

diff.release = args[0]
diff.revisions = args[1:]
Expand All @@ -76,15 +70,10 @@ func rollbackCmd() *cobra.Command {
},
}

rollbackCmd.Flags().BoolP("suppress-secrets", "q", false, "suppress secrets in the output")
rollbackCmd.Flags().BoolVar(&diff.showSecrets, "show-secrets", false, "do not redact secret values in the output")
rollbackCmd.Flags().BoolVar(&diff.detailedExitCode, "detailed-exitcode", false, "return a non-zero exit code when there are changes")
rollbackCmd.Flags().StringArrayVar(&diff.suppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
rollbackCmd.Flags().IntVarP(&diff.outputContext, "context", "C", -1, "output NUM lines of context around changes")
rollbackCmd.Flags().BoolVar(&diff.includeTests, "include-tests", false, "enable the diffing of the helm test hooks")
rollbackCmd.Flags().StringVar(&diff.output, "output", "diff", "Possible values: diff, simple, template. When set to \"template\", use the env var HELM_DIFF_TPL to specify the template.")
rollbackCmd.Flags().BoolVar(&diff.stripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
rollbackCmd.Flags().BoolVar(&diff.normalizeManifests, "normalize-manifests", false, "normalize manifests before running diff to exclude style differences from the output")
AddDiffOptions(rollbackCmd.Flags(), &diff.Options)

rollbackCmd.SuggestionsMinimumDistance = 1

Expand Down Expand Up @@ -119,11 +108,7 @@ func (d *rollback) backcastHelm3() error {
seenAnyChanges := diff.Manifests(
manifest.Parse(string(releaseResponse), namespace, d.normalizeManifests, excludes...),
manifest.Parse(string(revisionResponse), namespace, d.normalizeManifests, excludes...),
d.suppressedKinds,
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
&d.Options,
os.Stdout)

if d.detailedExitCode && seenAnyChanges {
Expand Down Expand Up @@ -156,11 +141,7 @@ func (d *rollback) backcast() error {
seenAnyChanges := diff.Manifests(
manifest.ParseRelease(releaseResponse.Release, d.includeTests, d.normalizeManifests),
manifest.ParseRelease(revisionResponse.Release, d.includeTests, d.normalizeManifests),
d.suppressedKinds,
d.showSecrets,
d.outputContext,
d.output,
d.stripTrailingCR,
&d.Options,
os.Stdout)

if d.detailedExitCode && seenAnyChanges {
Expand Down
23 changes: 7 additions & 16 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,13 @@ type diffCmd struct {
allowUnreleased bool
noHooks bool
includeTests bool
suppressedKinds []string
outputContext int
showSecrets bool
postRenderer string
output string
install bool
stripTrailingCR bool
normalizeManifests bool
threeWayMerge bool
extraAPIs []string
useUpgradeDryRun bool
diff.Options
}

func (d *diffCmd) isAllowUnreleased() bool {
Expand Down Expand Up @@ -132,9 +128,7 @@ func newChartCommand() *cobra.Command {
}
}

if q, _ := cmd.Flags().GetBool("suppress-secrets"); q {
diff.suppressedKinds = append(diff.suppressedKinds, "Secret")
}
ProcessDiffOptions(cmd.Flags(), &diff.Options)

diff.release = args[0]
diff.chart = args[1]
Expand Down Expand Up @@ -163,8 +157,6 @@ func newChartCommand() *cobra.Command {
// - https://github.com/helm/helm/blob/d9ffe37d371c9d06448c55c852c800051830e49a/cmd/helm/template.go#L184
// - https://github.com/databus23/helm-diff/issues/318
f.StringArrayVarP(&diff.extraAPIs, "api-versions", "a", []string{}, "Kubernetes api versions used for Capabilities.APIVersions")
f.BoolP("suppress-secrets", "q", false, "suppress secrets in the output")
f.BoolVar(&diff.showSecrets, "show-secrets", false, "do not redact secret values in the output")
f.VarP(&diff.valueFiles, "values", "f", "specify values in a YAML file (can specify multiple)")
f.StringArrayVar(&diff.values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
f.StringArrayVar(&diff.stringValues, "set-string", []string{}, "set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
Expand All @@ -176,15 +168,14 @@ func newChartCommand() *cobra.Command {
f.BoolVar(&diff.noHooks, "no-hooks", false, "disable diffing of hooks")
f.BoolVar(&diff.includeTests, "include-tests", false, "enable the diffing of the helm test hooks")
f.BoolVar(&diff.devel, "devel", false, "use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored.")
f.StringArrayVar(&diff.suppressedKinds, "suppress", []string{}, "allows suppression of the values listed in the diff output")
f.IntVarP(&diff.outputContext, "context", "C", -1, "output NUM lines of context around changes")
f.BoolVar(&diff.disableValidation, "disable-validation", false, "disables rendered templates validation against the Kubernetes cluster you are currently pointing to. This is the same validation performed on an install")
f.BoolVar(&diff.disableOpenAPIValidation, "disable-openapi-validation", false, "disables rendered templates validation against the Kubernetes OpenAPI Schema")
f.BoolVar(&diff.dryRun, "dry-run", false, "disables cluster access and show diff as if it was install. Implies --install, --reset-values, and --disable-validation")
f.StringVar(&diff.postRenderer, "post-renderer", "", "the path to an executable to be used for post rendering. If it exists in $PATH, the binary will be used, otherwise it will try to look for the executable at the given path")
f.StringVar(&diff.output, "output", "diff", "Possible values: diff, simple, json, template. When set to \"template\", use the env var HELM_DIFF_TPL to specify the template.")
f.BoolVar(&diff.stripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input")
f.BoolVar(&diff.normalizeManifests, "normalize-manifests", false, "normalize manifests before running diff to exclude style differences from the output")

AddDiffOptions(f, &diff.Options)

if !isHelm3() {
f.StringVar(&diff.namespace, "namespace", "default", "namespace to assume the release to be installed into")
}
Expand Down Expand Up @@ -271,7 +262,7 @@ func (d *diffCmd) runHelm3() error {
} else {
newSpecs = manifest.Parse(string(installManifest), d.namespace, d.normalizeManifests, helm3TestHook, helm2TestSuccessHook)
}
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, d.suppressedKinds, d.showSecrets, d.outputContext, d.output, d.stripTrailingCR, os.Stdout)
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, &d.Options, os.Stdout)

if d.detailedExitCode && seenAnyChanges {
return Error{
Expand Down Expand Up @@ -463,7 +454,7 @@ func (d *diffCmd) run() error {
}
}

seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, d.suppressedKinds, d.showSecrets, d.outputContext, d.output, d.stripTrailingCR, os.Stdout)
seenAnyChanges := diff.Manifests(currentSpecs, newSpecs, &d.Options, os.Stdout)

if d.detailedExitCode && seenAnyChanges {
return Error{
Expand Down
Loading