Skip to content

Commit d585c96

Browse files
committed
Add install_git_hook_maybe to setup.rs
1 parent 68ca474 commit d585c96

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/bootstrap/setup.rs

+40
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub fn setup(src_path: &Path, include_name: &str) {
4646
_ => return,
4747
};
4848

49+
t!(install_git_hook_maybe(src_path));
50+
4951
println!("To get started, try one of the following commands:");
5052
for cmd in suggestions {
5153
println!("- `x.py {}`", cmd);
@@ -86,3 +88,41 @@ d) Install Rust from source"
8688
};
8789
Ok(template.to_owned())
8890
}
91+
92+
// install a git hook to automatically run tidy --bless, if they want
93+
fn install_git_hook_maybe(src_path: &Path) -> io::Result<()> {
94+
let mut input = String::new();
95+
println!(
96+
"Rust's CI will automatically fail if it doesn't pass `tidy`, the internal tool for ensuring code quality.
97+
If you'd like, x.py can install a git hook for you that will automatically run `tidy --bless` on each commit
98+
to ensure your code is up to par. If you decide later that this behavior is undesirable,
99+
simply delete the `pre-commit` file from .git/hooks."
100+
);
101+
102+
let should_install = loop {
103+
print!("Would you like to install the git hook?: [y/N] ");
104+
io::stdout().flush()?;
105+
io::stdin().read_line(&mut input)?;
106+
break match input.trim().to_lowercase().as_str() {
107+
"y" | "yes" => true,
108+
// is this the right way to check for "entered nothing"?
109+
"n" | "no" | "" => false,
110+
_ => {
111+
println!("error: unrecognized option '{}'", input.trim());
112+
println!("note: press Ctrl+C to exit");
113+
continue;
114+
}
115+
};
116+
};
117+
118+
if should_install {
119+
let src = src_path.join("/etc/pre-commit.rs");
120+
let dst = src_path.join("/.git/hooks/pre-commit");
121+
fs::hard_link(src, dst)?;
122+
println!("Linked `src/etc/pre-commit.sh` to `.git/hooks/pre-commit`");
123+
} else {
124+
println!("Ok, skipping installation!");
125+
};
126+
127+
Ok(())
128+
}

0 commit comments

Comments
 (0)