@@ -16,6 +16,7 @@ import (
16
16
17
17
"github.com/charmbracelet/log"
18
18
"github.com/google/shlex"
19
+ "github.com/numtide/treefmt/v2/git"
19
20
"github.com/numtide/treefmt/v2/walk"
20
21
"github.com/spf13/pflag"
21
22
"github.com/spf13/viper"
@@ -290,58 +291,68 @@ func determineTreeRoot(v *viper.Viper, cfg *Config, logger *log.Logger) error {
290
291
return errors .New ("at most one of tree-root, tree-root-cmd or tree-root-file can be specified" )
291
292
}
292
293
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
-
303
294
switch {
304
295
case cfg .TreeRoot != "" :
305
- logger .Debugf ("tree root specified explicitly: %s" , cfg .TreeRoot )
296
+ logger .Infof ("tree root specified explicitly: %s" , cfg .TreeRoot )
306
297
307
298
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 )
309
300
310
301
_ , cfg .TreeRoot , err = FindUp (cfg .WorkingDirectory , cfg .TreeRootFile )
311
302
if err != nil {
312
303
return fmt .Errorf ("failed to find tree-root based on tree-root-file: %w" , err )
313
304
}
314
305
315
306
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 )
317
308
318
- if cfg .TreeRoot , err = execTreeRootCmd (cfg ); err != nil {
309
+ if cfg .TreeRoot , err = execTreeRootCmd (cfg . TreeRootCmd , cfg . WorkingDirectory ); err != nil {
319
310
return err
320
311
}
321
312
322
313
default :
323
314
// 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
+ }
328
326
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
+ }
330
341
}
331
342
332
343
// resolve tree root to an absolute path
333
344
if cfg .TreeRoot , err = filepath .Abs (cfg .TreeRoot ); err != nil {
334
345
return fmt .Errorf ("failed to get absolute path for tree root: %w" , err )
335
346
}
336
347
337
- logger .Debugf ("tree root: %s" , cfg .TreeRoot )
348
+ logger .Infof ("tree root: %s" , cfg .TreeRoot )
338
349
339
350
return nil
340
351
}
341
352
342
- func execTreeRootCmd (cfg * Config ) (string , error ) {
353
+ func execTreeRootCmd (treeRootCmd string , workingDir string ) (string , error ) {
343
354
// split the command first, resolving any '' and "" entries
344
- parts , splitErr := shlex .Split (cfg . TreeRootCmd )
355
+ parts , splitErr := shlex .Split (treeRootCmd )
345
356
if splitErr != nil {
346
357
return "" , fmt .Errorf ("failed to parse tree-root-cmd: %w" , splitErr )
347
358
}
@@ -356,7 +367,7 @@ func execTreeRootCmd(cfg *Config) (string, error) {
356
367
// construct the command, setting the correct working directory
357
368
//nolint:gosec
358
369
cmd := exec .CommandContext (ctx , parts [0 ], parts [1 :]... )
359
- cmd .Dir = cfg . WorkingDirectory
370
+ cmd .Dir = workingDir
360
371
361
372
// setup some pipes to capture stdout and stderr
362
373
stdout , err := cmd .StdoutPipe ()
@@ -428,13 +439,13 @@ func execTreeRootCmd(cfg *Config) (string, error) {
428
439
429
440
case 0 :
430
441
// 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 )
432
443
433
444
default :
434
445
// multiple lines received on stdout, dump the output to make it clear what happened and throw an error
435
446
log .WithPrefix ("tree-root-cmd | stdout" ).Errorf ("\n %s" , outputStr )
436
447
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 )
438
449
}
439
450
}
440
451
0 commit comments