Skip to content

Commit b07a1e3

Browse files
Put final touches
1 parent 00b23e8 commit b07a1e3

File tree

1 file changed

+34
-40
lines changed

1 file changed

+34
-40
lines changed

src/bootstrap/format.rs

+34-40
Original file line numberDiff line numberDiff line change
@@ -44,65 +44,58 @@ fn rustfmt(src: &Path, rustfmt: &Path, paths: &[PathBuf], check: bool) -> impl F
4444
}
4545
}
4646

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");
5349

5450
let mut cmd = Command::new(match build.initial_rustfmt() {
5551
Some(p) => p,
56-
None => return false,
52+
None => return None,
5753
});
5854
cmd.arg("--version");
5955
let output = match cmd.output() {
6056
Ok(status) => status,
61-
Err(_) => return false,
57+
Err(_) => return None,
6258
};
6359
if !output.status.success() {
64-
return false;
60+
return None;
6561
}
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))
6863
}
6964

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+
}
9070

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;};
9174
t!(std::fs::write(stamp_file, version))
9275
}
9376

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.
9481
fn get_modified_files(build: &Builder<'_>) -> Option<Vec<String>> {
9582
let Ok(remote) = get_rust_lang_rust_remote() else {return None;};
96-
if !verify_timestamp(build) {
83+
if !verify_rustfmt_version(build) {
9784
return None;
9885
}
99-
let base =
100-
output(build.config.git().arg("merge-base").arg("HEAD").arg(format!("{remote}/master")));
10186
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(),
10699
)
107100
}
108101

@@ -286,6 +279,7 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
286279
drop(tx);
287280

288281
thread.join().unwrap();
289-
290-
update_timestamp(build);
282+
if !check {
283+
update_rustfmt_version(build);
284+
}
291285
}

0 commit comments

Comments
 (0)