@@ -81,14 +81,19 @@ fn update_rustfmt_version(build: &Builder<'_>) {
8181 let Some ( ( version, stamp_file) ) = get_rustfmt_version ( build) else {
8282 return ;
8383 } ;
84- t ! ( std:: fs:: write( stamp_file. path( ) , version) )
84+
85+ t ! ( stamp_file. add_stamp( version) . write( ) ) ;
8586}
8687
87- /// Returns the Rust files modified between the ` merge-base` of HEAD and
88- /// rust-lang/master and what is now on the disk. Does not include removed files.
88+ /// Returns the Rust files modified between the last merge commit and what is now on the disk.
89+ /// Does not include removed files.
8990///
9091/// Returns `None` if all files should be formatted.
9192fn get_modified_rs_files ( build : & Builder < ' _ > ) -> Result < Option < Vec < String > > , String > {
93+ // In CI `get_git_modified_files` returns something different to normal environment.
94+ // This shouldn't be called in CI anyway.
95+ assert ! ( !build. config. is_running_on_ci) ;
96+
9297 if !verify_rustfmt_version ( build) {
9398 return Ok ( None ) ;
9499 }
@@ -103,7 +108,7 @@ struct RustfmtConfig {
103108
104109// Prints output describing a collection of paths, with lines such as "formatted modified file
105110// foo/bar/baz" or "skipped 20 untracked files".
106- fn print_paths ( build : & Builder < ' _ > , verb : & str , adjective : Option < & str > , paths : & [ String ] ) {
111+ fn print_paths ( verb : & str , adjective : Option < & str > , paths : & [ String ] ) {
107112 let len = paths. len ( ) ;
108113 let adjective =
109114 if let Some ( adjective) = adjective { format ! ( "{adjective} " ) } else { String :: new ( ) } ;
@@ -114,9 +119,6 @@ fn print_paths(build: &Builder<'_>, verb: &str, adjective: Option<&str>, paths:
114119 } else {
115120 println ! ( "fmt: {verb} {len} {adjective}files" ) ;
116121 }
117- if len > 1000 && !build. config . is_running_on_ci {
118- println ! ( "hint: if this number seems too high, try running `git fetch origin master`" ) ;
119- }
120122}
121123
122124pub fn format ( build : & Builder < ' _ > , check : bool , all : bool , paths : & [ PathBuf ] ) {
@@ -189,7 +191,7 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
189191 )
190192 . map ( |x| x. to_string ( ) )
191193 . collect ( ) ;
192- print_paths ( build , "skipped" , Some ( "untracked" ) , & untracked_paths) ;
194+ print_paths ( "skipped" , Some ( "untracked" ) , & untracked_paths) ;
193195
194196 for untracked_path in untracked_paths {
195197 // The leading `/` makes it an exact match against the
@@ -212,7 +214,13 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
212214 override_builder. add ( & format ! ( "/{file}" ) ) . expect ( & file) ;
213215 }
214216 }
215- Ok ( None ) => { }
217+ Ok ( None ) => {
218+ // NOTE: `Ok(None)` signifies that we need to format all files.
219+ // The tricky part here is that if `override_builder` isn't given any white
220+ // list files (i.e. files to be formatted, added without leading `!`), it
221+ // will instead look for *all* files. So, by doing nothing here, we are
222+ // actually making it so we format all files.
223+ }
216224 Err ( err) => {
217225 eprintln ! ( "fmt warning: Something went wrong running git commands:" ) ;
218226 eprintln ! ( "fmt warning: {err}" ) ;
@@ -318,7 +326,7 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
318326 } ) ;
319327 let mut paths = formatted_paths. into_inner ( ) . unwrap ( ) ;
320328 paths. sort ( ) ;
321- print_paths ( build , if check { "checked" } else { "formatted" } , adjective, & paths) ;
329+ print_paths ( if check { "checked" } else { "formatted" } , adjective, & paths) ;
322330
323331 drop ( tx) ;
324332
@@ -328,7 +336,10 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
328336 crate :: exit!( 1 ) ;
329337 }
330338
331- if !check {
332- update_rustfmt_version ( build) ;
333- }
339+ // Update `build/.rustfmt-stamp`, allowing this code to ignore files which have not been changed
340+ // since last merge.
341+ //
342+ // NOTE: Because of the exit above, this is only reachable if formatting / format checking
343+ // succeeded. So we are not commiting the version if formatting was not good.
344+ update_rustfmt_version ( build) ;
334345}
0 commit comments