@@ -81,14 +81,19 @@ fn update_rustfmt_version(build: &Builder<'_>) {
81
81
let Some ( ( version, stamp_file) ) = get_rustfmt_version ( build) else {
82
82
return ;
83
83
} ;
84
- t ! ( std:: fs:: write( stamp_file. path( ) , version) )
84
+
85
+ t ! ( stamp_file. add_stamp( version) . write( ) ) ;
85
86
}
86
87
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.
89
90
///
90
91
/// Returns `None` if all files should be formatted.
91
92
fn 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
+
92
97
if !verify_rustfmt_version ( build) {
93
98
return Ok ( None ) ;
94
99
}
@@ -103,7 +108,7 @@ struct RustfmtConfig {
103
108
104
109
// Prints output describing a collection of paths, with lines such as "formatted modified file
105
110
// 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 ] ) {
107
112
let len = paths. len ( ) ;
108
113
let adjective =
109
114
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:
114
119
} else {
115
120
println ! ( "fmt: {verb} {len} {adjective}files" ) ;
116
121
}
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
- }
120
122
}
121
123
122
124
pub 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]) {
189
191
)
190
192
. map ( |x| x. to_string ( ) )
191
193
. collect ( ) ;
192
- print_paths ( build , "skipped" , Some ( "untracked" ) , & untracked_paths) ;
194
+ print_paths ( "skipped" , Some ( "untracked" ) , & untracked_paths) ;
193
195
194
196
for untracked_path in untracked_paths {
195
197
// The leading `/` makes it an exact match against the
@@ -212,7 +214,13 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
212
214
override_builder. add ( & format ! ( "/{file}" ) ) . expect ( & file) ;
213
215
}
214
216
}
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
+ }
216
224
Err ( err) => {
217
225
eprintln ! ( "fmt warning: Something went wrong running git commands:" ) ;
218
226
eprintln ! ( "fmt warning: {err}" ) ;
@@ -318,7 +326,7 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
318
326
} ) ;
319
327
let mut paths = formatted_paths. into_inner ( ) . unwrap ( ) ;
320
328
paths. sort ( ) ;
321
- print_paths ( build , if check { "checked" } else { "formatted" } , adjective, & paths) ;
329
+ print_paths ( if check { "checked" } else { "formatted" } , adjective, & paths) ;
322
330
323
331
drop ( tx) ;
324
332
@@ -328,7 +336,10 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
328
336
crate :: exit!( 1 ) ;
329
337
}
330
338
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) ;
334
345
}
0 commit comments