@@ -2,6 +2,7 @@ package mutator
22
33import (
44 "context"
5+ "fmt"
56 "path/filepath"
67
78 "github.com/databricks/cli/bundle"
@@ -27,29 +28,55 @@ func (m *loadGitDetails) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn
2728 }
2829
2930 b .WorktreeRoot = info .GuessedWorktreeRoot
31+ config := & b .Config .Bundle .Git
3032
31- b .Config .Bundle .Git .ActualBranch = info .CurrentBranch
32- if b .Config .Bundle .Git .Branch == "" {
33- // Only load branch if there's no user defined value
34- b .Config .Bundle .Git .Inferred = true
35- b .Config .Bundle .Git .Branch = info .CurrentBranch
33+ config .ActualBranch = info .CurrentBranch
34+ if config .Branch == "" && info .CurrentBranch != "" {
35+ config .Inferred = true
3636 }
3737
38- // load commit hash if undefined
39- if b .Config .Bundle .Git .Commit == "" {
40- b .Config .Bundle .Git .Commit = info .LatestCommit
41- }
42-
43- // load origin url if undefined
44- if b .Config .Bundle .Git .OriginURL == "" {
45- b .Config .Bundle .Git .OriginURL = info .OriginURL
46- }
38+ diags = checkMatch (diags , "Git branch" , info .CurrentBranch , & config .Branch , b .Config .Bundle .Force )
39+ diags = checkMatch (diags , "Git commit" , info .LatestCommit , & config .Commit , b .Config .Bundle .Force )
40+ diags = checkMatch (diags , "Git originURL" , info .OriginURL , & config .OriginURL , true )
4741
4842 relBundlePath , err := filepath .Rel (b .WorktreeRoot .Native (), b .BundleRoot .Native ())
4943 if err != nil {
5044 diags = append (diags , diag .FromErr (err )... )
5145 }
5246
53- b .Config .Bundle .Git .BundleRootPath = filepath .ToSlash (relBundlePath )
47+ config .BundleRootPath = filepath .ToSlash (relBundlePath )
48+ return diags
49+ }
50+
51+ func checkMatch (diags []diag.Diagnostic , field , fetchedValue string , configValue * string , allowedToMismatch bool ) []diag.Diagnostic {
52+ if fetchedValue == "" {
53+ return diags
54+ }
55+
56+ // The value from config takes precedence; however, we always warn if configValue and fetchedValue disagree.
57+ // In case of branch and commit and absence of --force we raise severity to Error
58+
59+ if * configValue == "" {
60+ * configValue = fetchedValue
61+ return diags
62+ }
63+
64+ if * configValue != fetchedValue {
65+ tmpl := "not on the right %s:\n expected according to configuration: %s\n actual: %s%s"
66+ extra := ""
67+ var severity diag.Severity
68+ if allowedToMismatch {
69+ severity = diag .Warning
70+ } else {
71+ severity = diag .Error
72+ extra = "\n use --force to override"
73+ }
74+
75+ return append (diags , diag.Diagnostic {
76+ Severity : severity ,
77+ Summary : fmt .Sprintf (tmpl , field , * configValue , fetchedValue , extra ),
78+ })
79+ }
80+
5481 return diags
5582}
0 commit comments