Skip to content

Commit

Permalink
feat: consider untracked/uncommitted in the change detector.
Browse files Browse the repository at this point in the history
Signed-off-by: i4k <[email protected]>
  • Loading branch information
i4ki committed Oct 8, 2024
1 parent dde9186 commit 96f084b
Show file tree
Hide file tree
Showing 9 changed files with 513 additions and 47 deletions.
4 changes: 2 additions & 2 deletions benchmarks/changed/changed_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func BenchmarkChangeDetection(b *testing.B) {

b.StartTimer()
for i := 0; i < b.N; i++ {
report, err := manager.ListChanged("origin/main")
report, err := manager.ListChanged(stack.ChangeConfig{BaseRef: "origin/main"})
assert.NoError(b, err)
assert.EqualInts(b, 1, len(report.Stacks))
assert.EqualStrings(b, fmt.Sprintf("/stack-%d", nstacks-1), report.Stacks[0].Stack.Dir.String())
Expand Down Expand Up @@ -91,7 +91,7 @@ func BenchmarkChangeDetectionTFAndTG(b *testing.B) {

b.StartTimer()
for i := 0; i < b.N; i++ {
report, err := manager.ListChanged("origin/main")
report, err := manager.ListChanged(stack.ChangeConfig{BaseRef: "origin/main"})
assert.NoError(b, err)
assert.EqualInts(b, 2, len(report.Stacks))
assert.EqualStrings(b, fmt.Sprintf("/stack-%d", nTfStacks-1), report.Stacks[0].Stack.Dir.String())
Expand Down
56 changes: 55 additions & 1 deletion cmd/terramate/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path"
"path/filepath"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -150,10 +151,13 @@ type cliSpec struct {
cloudFilterFlags
Target string `help:"Select the deployment target of the filtered stacks."`
RunOrder bool `default:"false" help:"Sort listed stacks by order of execution"`

disableChangeDetectionFlag
} `cmd:"" help:"List stacks."`

Run struct {
cloudFilterFlags
enableChangeDetectionFlag
Target string `help:"Set the deployment target for stacks synchronized to Terramate Cloud."`
FromTarget string `help:"Migrate stacks from given deployment target."`
EnableSharing bool `help:"Enable sharing of stack outputs as stack inputs."`
Expand Down Expand Up @@ -200,6 +204,7 @@ type cliSpec struct {
} `cmd:"" help:"Show detailed information about a script"`
Run struct {
cloudFilterFlags
enableChangeDetectionFlag
Target string `help:"Set the deployment target for stacks synchronized to Terramate Cloud."`
FromTarget string `help:"Migrate stacks from given deployment target."`
NoRecursive bool `default:"false" help:"Do not recurse into nested child stacks."`
Expand Down Expand Up @@ -253,6 +258,7 @@ type cliSpec struct {
IgnoreChange bool `default:"false" help:"Trigger stacks to be ignored by change detection"`
Reason string `default:"" name:"reason" help:"Set a reason for triggering the stack."`
cloudFilterFlags
disableChangeDetectionFlag
} `cmd:"" help:"Mark a stack as changed so it will be triggered in Change Detection."`

RunGraph struct {
Expand Down Expand Up @@ -334,6 +340,14 @@ type cloudFilterFlags struct {
DriftStatus string `help:"Filter by Terramate Cloud drift status of the stack"`
}

type disableChangeDetectionFlag struct {
DisableChangeDetection []string `help:"Disable specific change detection modes" enum:"git-untracked,git-uncommitted"`
}

type enableChangeDetectionFlag struct {
EnableChangeDetection []string `help:"Enable specific change detection modes" enum:"git-untracked,git-uncommitted"`
}

// Exec will execute terramate with the provided flags defined on args.
// Only flags should be on the args slice.
//
Expand Down Expand Up @@ -380,6 +394,13 @@ type cli struct {
checkpointResults chan *checkpoint.CheckResponse

tags filter.TagClause

changeDetection changeDetection
}

type changeDetection struct {
untracked *bool
uncommitted *bool
}

func newCLI(version string, args []string, stdin io.Reader, stdout, stderr io.Writer) *cli {
Expand Down Expand Up @@ -662,20 +683,24 @@ func (c *cli) run() {
c.scanCreate()
case "list":
c.setupGit()
c.setupListChangeDetection()
c.printStacks()
case "run":
fatal("no command specified")
case "run <cmd>":
c.setupGit()
c.setupRunChangeDetection()
c.setupSafeguards(c.parsedArgs.Run.runSafeguardsCliSpec)
c.runOnStacks()
case "generate":
c.generate()
case "experimental clone <srcdir> <destdir>":
c.cloneStack()
case "experimental trigger":
c.setupRunChangeDetection()
c.triggerStackByFilter()
case "experimental trigger <stack>":
c.setupRunChangeDetection()
c.triggerStack(c.parsedArgs.Experimental.Trigger.Stack)
case "experimental vendor download <source> <ref>":
c.vendorDownload()
Expand All @@ -693,6 +718,7 @@ func (c *cli) run() {
c.generateGraph()
case "experimental run-order":
c.setupGit()
c.setupRunChangeDetection()
c.printRunOrder(false)
case "debug show runtime-env":
c.setupGit()
Expand Down Expand Up @@ -735,6 +761,7 @@ func (c *cli) run() {
case "script run <cmds>":
c.checkScriptEnabled()
c.setupGit()
c.setupRunChangeDetection()
c.setupSafeguards(c.parsedArgs.Script.Run.runSafeguardsCliSpec)
c.runScript()
default:
Expand Down Expand Up @@ -1228,6 +1255,29 @@ func (c *cli) gitSafeguardDefaultBranchIsReachable() {
}
}

func isChangeDetectionMode(set bool, name string, options []string) *bool {
var value *bool
if slices.Contains(options, name) {
value = &set
}
return value
}

func (c *cli) setupListChangeDetection() {
c.changeDetection.untracked = isChangeDetectionMode(false, "git-untracked", c.parsedArgs.List.DisableChangeDetection)
c.changeDetection.uncommitted = isChangeDetectionMode(false, "git-uncommitted", c.parsedArgs.List.DisableChangeDetection)
}

func (c *cli) setupRunChangeDetection() {
c.changeDetection.untracked = isChangeDetectionMode(true, "git-untracked", c.parsedArgs.Run.EnableChangeDetection)
c.changeDetection.uncommitted = isChangeDetectionMode(true, "git-uncommitted", c.parsedArgs.Run.EnableChangeDetection)
}

func (c *cli) setupScriptRunChangeDetection() {
c.changeDetection.untracked = isChangeDetectionMode(true, "git-untracked", c.parsedArgs.Script.Run.EnableChangeDetection)
c.changeDetection.uncommitted = isChangeDetectionMode(true, "git-uncommitted", c.parsedArgs.Script.Run.EnableChangeDetection)
}

func (c *cli) listStacks(isChanged bool, target string, stackFilters cloud.StatusFilters) (*stack.Report, error) {
var (
err error
Expand All @@ -1237,7 +1287,11 @@ func (c *cli) listStacks(isChanged bool, target string, stackFilters cloud.Statu
mgr := c.stackManager()

if isChanged {
report, err = mgr.ListChanged(c.baseRef())
report, err = mgr.ListChanged(stack.ChangeConfig{
BaseRef: c.baseRef(),
UntrackedChanges: c.changeDetection.untracked,
UncommittedChanges: c.changeDetection.uncommitted,
})
} else {
report, err = mgr.List(true)
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/terramate/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,11 @@ func (c *cli) getAffectedStacks() []stack.Entry {
var report *stack.Report
var err error
if c.parsedArgs.Changed {
report, err = mgr.ListChanged(c.baseRef())
report, err = mgr.ListChanged(stack.ChangeConfig{
BaseRef: c.baseRef(),
UntrackedChanges: c.changeDetection.untracked,
UncommittedChanges: c.changeDetection.uncommitted,
})
if err != nil {
fatalWithDetailf(err, "listing changed stacks")
}
Expand Down
Loading

0 comments on commit 96f084b

Please sign in to comment.