11//! Runs rustfmt on the repository.
22
33use 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 ;
66use ignore:: WalkBuilder ;
77use std:: collections:: VecDeque ;
88use std:: path:: { Path , PathBuf } ;
@@ -79,21 +79,24 @@ fn update_rustfmt_version(build: &Builder<'_>) {
7979/// rust-lang/master and what is now on the disk.
8080///
8181/// 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+
8485 if !verify_rustfmt_version ( build) {
85- return None ;
86+ return Ok ( None ) ;
8687 }
8788
8889 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+ ) )
97100}
98101
99102#[ derive( serde:: Deserialize ) ]
@@ -130,6 +133,9 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
130133 Ok ( status) => status. success ( ) ,
131134 Err ( _) => false ,
132135 } ;
136+
137+ let mut paths = paths. to_vec ( ) ;
138+
133139 if git_available {
134140 let in_working_tree = match build
135141 . config
@@ -163,10 +169,21 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
163169 ignore_fmt. add ( & format ! ( "!/{}" , untracked_path) ) . expect ( & untracked_path) ;
164170 }
165171 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 ( ) ) ;
170187 }
171188 }
172189 }
@@ -176,6 +193,7 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
176193 } else {
177194 println ! ( "Could not find usable git. Skipping git-aware format checks" ) ;
178195 }
196+
179197 let ignore_fmt = ignore_fmt. build ( ) . unwrap ( ) ;
180198
181199 let rustfmt_path = build. initial_rustfmt ( ) . unwrap_or_else ( || {
0 commit comments