1
1
//! Runs rustfmt on the repository.
2
2
3
3
use crate :: builder:: Builder ;
4
- use crate :: util:: { output, program_out_of_date, t} ;
5
- use build_helper:: git:: get_rust_lang_rust_remote ;
4
+ use crate :: util:: { output, output_result , program_out_of_date, t} ;
5
+ use build_helper:: git:: updated_master_branch ;
6
6
use ignore:: WalkBuilder ;
7
7
use std:: collections:: VecDeque ;
8
8
use std:: path:: { Path , PathBuf } ;
@@ -79,21 +79,24 @@ fn update_rustfmt_version(build: &Builder<'_>) {
79
79
/// rust-lang/master and what is now on the disk.
80
80
///
81
81
/// Returns `None` if all files should be formatted.
82
- fn get_modified_rs_files ( build : & Builder < ' _ > ) -> Option < Vec < String > > {
83
- let Ok ( remote) = get_rust_lang_rust_remote ( ) else { return None ; } ;
82
+ fn get_modified_rs_files ( build : & Builder < ' _ > ) -> Result < Option < Vec < String > > , String > {
83
+ let Ok ( updated_master) = updated_master_branch ( Some ( & build. config . src ) ) else { return Ok ( None ) ; } ;
84
+
84
85
if !verify_rustfmt_version ( build) {
85
- return None ;
86
+ return Ok ( None ) ;
86
87
}
87
88
88
89
let merge_base =
89
- output ( build. config . git ( ) . arg ( "merge-base" ) . arg ( & format ! ( "{remote}/master" ) ) . arg ( "HEAD" ) ) ;
90
- Some (
91
- output ( build. config . git ( ) . arg ( "diff-index" ) . arg ( "--name-only" ) . arg ( merge_base. trim ( ) ) )
92
- . lines ( )
93
- . map ( |s| s. trim ( ) . to_owned ( ) )
94
- . filter ( |f| Path :: new ( f) . extension ( ) . map_or ( false , |ext| ext == "rs" ) )
95
- . collect ( ) ,
96
- )
90
+ output_result ( build. config . git ( ) . arg ( "merge-base" ) . arg ( & updated_master) . arg ( "HEAD" ) ) ?;
91
+ Ok ( Some (
92
+ output_result (
93
+ build. config . git ( ) . arg ( "diff-index" ) . arg ( "--name-only" ) . arg ( merge_base. trim ( ) ) ,
94
+ ) ?
95
+ . lines ( )
96
+ . map ( |s| s. trim ( ) . to_owned ( ) )
97
+ . filter ( |f| Path :: new ( f) . extension ( ) . map_or ( false , |ext| ext == "rs" ) )
98
+ . collect ( ) ,
99
+ ) )
97
100
}
98
101
99
102
#[ derive( serde:: Deserialize ) ]
@@ -130,6 +133,9 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
130
133
Ok ( status) => status. success ( ) ,
131
134
Err ( _) => false ,
132
135
} ;
136
+
137
+ let mut paths = paths. to_vec ( ) ;
138
+
133
139
if git_available {
134
140
let in_working_tree = match build
135
141
. config
@@ -163,10 +169,21 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
163
169
ignore_fmt. add ( & format ! ( "!/{}" , untracked_path) ) . expect ( & untracked_path) ;
164
170
}
165
171
if !check && paths. is_empty ( ) {
166
- if let Some ( files) = get_modified_rs_files ( build) {
167
- for file in files {
168
- println ! ( "formatting modified file {file}" ) ;
169
- ignore_fmt. add ( & format ! ( "/{file}" ) ) . expect ( & file) ;
172
+ match get_modified_rs_files ( build) {
173
+ Ok ( Some ( files) ) => {
174
+ for file in files {
175
+ println ! ( "formatting modified file {file}" ) ;
176
+ ignore_fmt. add ( & format ! ( "/{file}" ) ) . expect ( & file) ;
177
+ }
178
+ }
179
+ Ok ( None ) => { }
180
+ Err ( err) => {
181
+ println ! (
182
+ "WARN: Something went wrong when running git commands:\n {err}\n \
183
+ Falling back to formatting all files."
184
+ ) ;
185
+ // Something went wrong when getting the version. Just format all the files.
186
+ paths. push ( "." . into ( ) ) ;
170
187
}
171
188
}
172
189
}
@@ -176,6 +193,7 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
176
193
} else {
177
194
println ! ( "Could not find usable git. Skipping git-aware format checks" ) ;
178
195
}
196
+
179
197
let ignore_fmt = ignore_fmt. build ( ) . unwrap ( ) ;
180
198
181
199
let rustfmt_path = build. initial_rustfmt ( ) . unwrap_or_else ( || {
0 commit comments