@@ -4,7 +4,7 @@ use crate::Build;
4
4
use build_helper:: { output, t} ;
5
5
use ignore:: WalkBuilder ;
6
6
use std:: path:: Path ;
7
- use std:: process:: Command ;
7
+ use std:: process:: { Command , Stdio } ;
8
8
9
9
fn rustfmt ( src : & Path , rustfmt : & Path , path : & Path , check : bool ) {
10
10
let mut cmd = Command :: new ( & rustfmt) ;
@@ -56,16 +56,48 @@ pub fn format(build: &Build, check: bool) {
56
56
for ignore in rustfmt_config. ignore {
57
57
ignore_fmt. add ( & format ! ( "!{}" , ignore) ) . expect ( & ignore) ;
58
58
}
59
- let untracked_paths_output = output (
60
- Command :: new ( "git" ) . arg ( "status" ) . arg ( "--porcelain" ) . arg ( "--untracked-files=normal" ) ,
61
- ) ;
62
- let untracked_paths = untracked_paths_output
63
- . lines ( )
64
- . filter ( |entry| entry. starts_with ( "??" ) )
65
- . map ( |entry| entry. split ( " " ) . nth ( 1 ) . expect ( "every git status entry should list a path" ) ) ;
66
- for untracked_path in untracked_paths {
67
- eprintln ! ( "skip untracked path {} during rustfmt invocations" , untracked_path) ;
68
- ignore_fmt. add ( & format ! ( "!{}" , untracked_path) ) . expect ( & untracked_path) ;
59
+ let git_available = match Command :: new ( "git" )
60
+ . arg ( "--version" )
61
+ . stdout ( Stdio :: null ( ) )
62
+ . stderr ( Stdio :: null ( ) )
63
+ . status ( )
64
+ {
65
+ Ok ( status) => status. success ( ) ,
66
+ Err ( _) => false ,
67
+ } ;
68
+ if git_available {
69
+ let in_working_tree = match Command :: new ( "git" )
70
+ . arg ( "rev-parse" )
71
+ . arg ( "--is-inside-work-tree" )
72
+ . stdout ( Stdio :: null ( ) )
73
+ . stderr ( Stdio :: null ( ) )
74
+ . status ( )
75
+ {
76
+ Ok ( status) => status. success ( ) ,
77
+ Err ( _) => false ,
78
+ } ;
79
+ if in_working_tree {
80
+ let untracked_paths_output = output (
81
+ Command :: new ( "git" )
82
+ . arg ( "status" )
83
+ . arg ( "--porcelain" )
84
+ . arg ( "--untracked-files=normal" ) ,
85
+ ) ;
86
+ let untracked_paths = untracked_paths_output
87
+ . lines ( )
88
+ . filter ( |entry| entry. starts_with ( "??" ) )
89
+ . map ( |entry| {
90
+ entry. split ( " " ) . nth ( 1 ) . expect ( "every git status entry should list a path" )
91
+ } ) ;
92
+ for untracked_path in untracked_paths {
93
+ eprintln ! ( "skip untracked path {} during rustfmt invocations" , untracked_path) ;
94
+ ignore_fmt. add ( & format ! ( "!{}" , untracked_path) ) . expect ( & untracked_path) ;
95
+ }
96
+ } else {
97
+ eprintln ! ( "Not in git tree. Skipping git-aware format checks" ) ;
98
+ }
99
+ } else {
100
+ eprintln ! ( "Could not find usable git. Skipping git-aware format checks" ) ;
69
101
}
70
102
let ignore_fmt = ignore_fmt. build ( ) . unwrap ( ) ;
71
103
0 commit comments