Skip to content

Commit a0d6eed

Browse files
authored
Rollup merge of #70235 - dillona:70182-check-before-using-git, r=Mark-Simulacrum
Validate git setup before accessing functionality Closes #70182
2 parents d584f5a + 37c63ed commit a0d6eed

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

src/bootstrap/format.rs

+43-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::Build;
44
use build_helper::{output, t};
55
use ignore::WalkBuilder;
66
use std::path::Path;
7-
use std::process::Command;
7+
use std::process::{Command, Stdio};
88

99
fn rustfmt(src: &Path, rustfmt: &Path, path: &Path, check: bool) {
1010
let mut cmd = Command::new(&rustfmt);
@@ -56,16 +56,48 @@ pub fn format(build: &Build, check: bool) {
5656
for ignore in rustfmt_config.ignore {
5757
ignore_fmt.add(&format!("!{}", ignore)).expect(&ignore);
5858
}
59-
let untracked_paths_output = output(
60-
Command::new("git").arg("status").arg("--porcelain").arg("--untracked-files=normal"),
61-
);
62-
let untracked_paths = untracked_paths_output
63-
.lines()
64-
.filter(|entry| entry.starts_with("??"))
65-
.map(|entry| entry.split(" ").nth(1).expect("every git status entry should list a path"));
66-
for untracked_path in untracked_paths {
67-
eprintln!("skip untracked path {} during rustfmt invocations", untracked_path);
68-
ignore_fmt.add(&format!("!{}", untracked_path)).expect(&untracked_path);
59+
let git_available = match Command::new("git")
60+
.arg("--version")
61+
.stdout(Stdio::null())
62+
.stderr(Stdio::null())
63+
.status()
64+
{
65+
Ok(status) => status.success(),
66+
Err(_) => false,
67+
};
68+
if git_available {
69+
let in_working_tree = match Command::new("git")
70+
.arg("rev-parse")
71+
.arg("--is-inside-work-tree")
72+
.stdout(Stdio::null())
73+
.stderr(Stdio::null())
74+
.status()
75+
{
76+
Ok(status) => status.success(),
77+
Err(_) => false,
78+
};
79+
if in_working_tree {
80+
let untracked_paths_output = output(
81+
Command::new("git")
82+
.arg("status")
83+
.arg("--porcelain")
84+
.arg("--untracked-files=normal"),
85+
);
86+
let untracked_paths = untracked_paths_output
87+
.lines()
88+
.filter(|entry| entry.starts_with("??"))
89+
.map(|entry| {
90+
entry.split(" ").nth(1).expect("every git status entry should list a path")
91+
});
92+
for untracked_path in untracked_paths {
93+
eprintln!("skip untracked path {} during rustfmt invocations", untracked_path);
94+
ignore_fmt.add(&format!("!{}", untracked_path)).expect(&untracked_path);
95+
}
96+
} else {
97+
eprintln!("Not in git tree. Skipping git-aware format checks");
98+
}
99+
} else {
100+
eprintln!("Could not find usable git. Skipping git-aware format checks");
69101
}
70102
let ignore_fmt = ignore_fmt.build().unwrap();
71103

0 commit comments

Comments
 (0)