@@ -16,6 +16,7 @@ import (
1616
1717 "github.com/charmbracelet/log"
1818 "github.com/google/shlex"
19+ "github.com/numtide/treefmt/v2/git"
1920 "github.com/numtide/treefmt/v2/walk"
2021 "github.com/spf13/pflag"
2122 "github.com/spf13/viper"
@@ -290,58 +291,68 @@ func determineTreeRoot(v *viper.Viper, cfg *Config, logger *log.Logger) error {
290291 return errors .New ("at most one of tree-root, tree-root-cmd or tree-root-file can be specified" )
291292 }
292293
293- // set git-based tree root command if the walker is git and no tree root has been specified
294- if cfg .Walk == walk .Git .String () && count == 0 {
295- cfg .TreeRootCmd = "git rev-parse --show-toplevel"
296-
297- logger .Infof (
298- "git walker enabled and tree root has not been specified: defaulting tree-root-cmd to '%s'" ,
299- cfg .TreeRootCmd ,
300- )
301- }
302-
303294 switch {
304295 case cfg .TreeRoot != "" :
305- logger .Debugf ("tree root specified explicitly: %s" , cfg .TreeRoot )
296+ logger .Infof ("tree root specified explicitly: %s" , cfg .TreeRoot )
306297
307298 case cfg .TreeRootFile != "" :
308- logger .Debugf ("searching for tree root using tree-root-file: %s" , cfg .TreeRootFile )
299+ logger .Infof ("searching for tree root using tree-root-file: %s" , cfg .TreeRootFile )
309300
310301 _ , cfg .TreeRoot , err = FindUp (cfg .WorkingDirectory , cfg .TreeRootFile )
311302 if err != nil {
312303 return fmt .Errorf ("failed to find tree-root based on tree-root-file: %w" , err )
313304 }
314305
315306 case cfg .TreeRootCmd != "" :
316- logger .Debugf ("searching for tree root using tree-root-cmd: %s" , cfg .TreeRootCmd )
307+ logger .Infof ("searching for tree root using tree-root-cmd: %s" , cfg .TreeRootCmd )
317308
318- if cfg .TreeRoot , err = execTreeRootCmd (cfg ); err != nil {
309+ if cfg .TreeRoot , err = execTreeRootCmd (cfg . TreeRootCmd , cfg . WorkingDirectory ); err != nil {
319310 return err
320311 }
321312
322313 default :
323314 // no tree root was specified
324- logger .Debugf (
325- "no tree root specified, defaulting to the directory containing the config file: %s" ,
326- v .ConfigFileUsed (),
327- )
315+ logger .Infof ("no tree root specified" )
316+
317+ // attempt to resolve with git
318+ if cfg .Walk == walk .Auto .String () || cfg .Walk == walk .Git .String () {
319+ logger .Infof ("attempting to resolve tree root using git: %s" , git .TreeRootCmd )
320+
321+ // attempt to resolve the tree root with git
322+ cfg .TreeRoot , err = execTreeRootCmd (git .TreeRootCmd , cfg .WorkingDirectory )
323+ if err != nil && cfg .Walk == walk .Git .String () {
324+ return fmt .Errorf ("failed to resolve tree root with git: %w" , err )
325+ }
328326
329- cfg .TreeRoot = filepath .Dir (v .ConfigFileUsed ())
327+ if err != nil {
328+ logger .Infof ("failed to resolve tree root with git: %v" , err )
329+ }
330+ }
331+
332+ if cfg .TreeRoot == "" {
333+ // fallback to the directory containing the config file
334+ logger .Infof (
335+ "setting tree root to the directory containing the config file: %s" ,
336+ v .ConfigFileUsed (),
337+ )
338+
339+ cfg .TreeRoot = filepath .Dir (v .ConfigFileUsed ())
340+ }
330341 }
331342
332343 // resolve tree root to an absolute path
333344 if cfg .TreeRoot , err = filepath .Abs (cfg .TreeRoot ); err != nil {
334345 return fmt .Errorf ("failed to get absolute path for tree root: %w" , err )
335346 }
336347
337- logger .Debugf ("tree root: %s" , cfg .TreeRoot )
348+ logger .Infof ("tree root: %s" , cfg .TreeRoot )
338349
339350 return nil
340351}
341352
342- func execTreeRootCmd (cfg * Config ) (string , error ) {
353+ func execTreeRootCmd (treeRootCmd string , workingDir string ) (string , error ) {
343354 // split the command first, resolving any '' and "" entries
344- parts , splitErr := shlex .Split (cfg . TreeRootCmd )
355+ parts , splitErr := shlex .Split (treeRootCmd )
345356 if splitErr != nil {
346357 return "" , fmt .Errorf ("failed to parse tree-root-cmd: %w" , splitErr )
347358 }
@@ -356,7 +367,7 @@ func execTreeRootCmd(cfg *Config) (string, error) {
356367 // construct the command, setting the correct working directory
357368 //nolint:gosec
358369 cmd := exec .CommandContext (ctx , parts [0 ], parts [1 :]... )
359- cmd .Dir = cfg . WorkingDirectory
370+ cmd .Dir = workingDir
360371
361372 // setup some pipes to capture stdout and stderr
362373 stdout , err := cmd .StdoutPipe ()
@@ -428,13 +439,13 @@ func execTreeRootCmd(cfg *Config) (string, error) {
428439
429440 case 0 :
430441 // no output was received on stdout
431- return "" , fmt .Errorf ("empty output received after executing tree-root-cmd: %s" , cfg . TreeRootCmd )
442+ return "" , fmt .Errorf ("empty output received after executing tree-root-cmd: %s" , treeRootCmd )
432443
433444 default :
434445 // multiple lines received on stdout, dump the output to make it clear what happened and throw an error
435446 log .WithPrefix ("tree-root-cmd | stdout" ).Errorf ("\n %s" , outputStr )
436447
437- return "" , fmt .Errorf ("tree-root-cmd cannot output multiple lines: %s" , cfg . TreeRootCmd )
448+ return "" , fmt .Errorf ("tree-root-cmd cannot output multiple lines: %s" , treeRootCmd )
438449 }
439450}
440451
0 commit comments