@@ -44,65 +44,58 @@ fn rustfmt(src: &Path, rustfmt: &Path, paths: &[PathBuf], check: bool) -> impl F
44
44
}
45
45
}
46
46
47
- fn verify_timestamp ( build : & Builder < ' _ > ) -> bool {
48
- let stamp_file = {
49
- let mut s = build. out . clone ( ) ;
50
- s. push ( "rustfmt.stamp" ) ;
51
- s
52
- } ;
47
+ fn get_rustfmt_version ( build : & Builder < ' _ > ) -> Option < ( String , PathBuf ) > {
48
+ let stamp_file = build. out . join ( "rustfmt.stamp" ) ;
53
49
54
50
let mut cmd = Command :: new ( match build. initial_rustfmt ( ) {
55
51
Some ( p) => p,
56
- None => return false ,
52
+ None => return None ,
57
53
} ) ;
58
54
cmd. arg ( "--version" ) ;
59
55
let output = match cmd. output ( ) {
60
56
Ok ( status) => status,
61
- Err ( _) => return false ,
57
+ Err ( _) => return None ,
62
58
} ;
63
59
if !output. status . success ( ) {
64
- return false ;
60
+ return None ;
65
61
}
66
- let version = String :: from_utf8 ( output. stdout ) . unwrap ( ) ;
67
- !program_out_of_date ( & stamp_file, & version)
62
+ Some ( ( String :: from_utf8 ( output. stdout ) . unwrap ( ) , stamp_file) )
68
63
}
69
64
70
- fn update_timestamp ( build : & Builder < ' _ > ) {
71
- let stamp_file = {
72
- let mut s = build. out . clone ( ) ;
73
- s. push ( "rustfmt.stamp" ) ;
74
- s
75
- } ;
76
-
77
- let mut cmd = Command :: new ( match build. initial_rustfmt ( ) {
78
- Some ( p) => p,
79
- None => return ,
80
- } ) ;
81
- cmd. arg ( "--version" ) ;
82
- let output = match cmd. output ( ) {
83
- Ok ( status) => status,
84
- Err ( _) => return ,
85
- } ;
86
- if !output. status . success ( ) {
87
- return ;
88
- }
89
- let version = String :: from_utf8 ( output. stdout ) . unwrap ( ) ;
65
+ /// Return whether the format cache can be reused.
66
+ fn verify_rustfmt_version ( build : & Builder < ' _ > ) -> bool {
67
+ let Some ( ( version, stamp_file) ) = get_rustfmt_version ( build) else { return false ; } ;
68
+ !program_out_of_date ( & stamp_file, & version)
69
+ }
90
70
71
+ /// Updates the last rustfmt version used
72
+ fn update_rustfmt_version ( build : & Builder < ' _ > ) {
73
+ let Some ( ( version, stamp_file) ) = get_rustfmt_version ( build) else { return ; } ;
91
74
t ! ( std:: fs:: write( stamp_file, version) )
92
75
}
93
76
77
+ /// Returns the files modified between the `merge-base` of HEAD and
78
+ /// rust-lang/master and what is now on the disk.
79
+ ///
80
+ /// Returns `None` if all files should be formatted.
94
81
fn get_modified_files ( build : & Builder < ' _ > ) -> Option < Vec < String > > {
95
82
let Ok ( remote) = get_rust_lang_rust_remote ( ) else { return None ; } ;
96
- if !verify_timestamp ( build) {
83
+ if !verify_rustfmt_version ( build) {
97
84
return None ;
98
85
}
99
- let base =
100
- output ( build. config . git ( ) . arg ( "merge-base" ) . arg ( "HEAD" ) . arg ( format ! ( "{remote}/master" ) ) ) ;
101
86
Some (
102
- output ( build. config . git ( ) . arg ( "diff" ) . arg ( "--name-only" ) . arg ( base. trim ( ) ) )
103
- . lines ( )
104
- . map ( |s| s. trim ( ) . to_owned ( ) )
105
- . collect ( ) ,
87
+ output (
88
+ build
89
+ . config
90
+ . git ( )
91
+ . arg ( "diff-index" )
92
+ . arg ( "--name-only" )
93
+ . arg ( "--merge-base" )
94
+ . arg ( & format ! ( "{remote}/master" ) ) ,
95
+ )
96
+ . lines ( )
97
+ . map ( |s| s. trim ( ) . to_owned ( ) )
98
+ . collect ( ) ,
106
99
)
107
100
}
108
101
@@ -286,6 +279,7 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
286
279
drop ( tx) ;
287
280
288
281
thread. join ( ) . unwrap ( ) ;
289
-
290
- update_timestamp ( build) ;
282
+ if !check {
283
+ update_rustfmt_version ( build) ;
284
+ }
291
285
}
0 commit comments