From d585c96eaf5ba0d719b7385f15f241a4be765b20 Mon Sep 17 00:00:00 2001 From: Cassandra Fridkin Date: Mon, 5 Oct 2020 20:14:11 -0400 Subject: [PATCH] Add install_git_hook_maybe to setup.rs --- src/bootstrap/setup.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/bootstrap/setup.rs b/src/bootstrap/setup.rs index 9d3a889aa00..f5ec1d6d247 100644 --- a/src/bootstrap/setup.rs +++ b/src/bootstrap/setup.rs @@ -46,6 +46,8 @@ pub fn setup(src_path: &Path, include_name: &str) { _ => return, }; + t!(install_git_hook_maybe(src_path)); + println!("To get started, try one of the following commands:"); for cmd in suggestions { println!("- `x.py {}`", cmd); @@ -86,3 +88,41 @@ d) Install Rust from source" }; Ok(template.to_owned()) } + +// install a git hook to automatically run tidy --bless, if they want +fn install_git_hook_maybe(src_path: &Path) -> io::Result<()> { + let mut input = String::new(); + println!( + "Rust's CI will automatically fail if it doesn't pass `tidy`, the internal tool for ensuring code quality. +If you'd like, x.py can install a git hook for you that will automatically run `tidy --bless` on each commit +to ensure your code is up to par. If you decide later that this behavior is undesirable, +simply delete the `pre-commit` file from .git/hooks." + ); + + let should_install = loop { + print!("Would you like to install the git hook?: [y/N] "); + io::stdout().flush()?; + io::stdin().read_line(&mut input)?; + break match input.trim().to_lowercase().as_str() { + "y" | "yes" => true, + // is this the right way to check for "entered nothing"? + "n" | "no" | "" => false, + _ => { + println!("error: unrecognized option '{}'", input.trim()); + println!("note: press Ctrl+C to exit"); + continue; + } + }; + }; + + if should_install { + let src = src_path.join("/etc/pre-commit.rs"); + let dst = src_path.join("/.git/hooks/pre-commit"); + fs::hard_link(src, dst)?; + println!("Linked `src/etc/pre-commit.sh` to `.git/hooks/pre-commit`"); + } else { + println!("Ok, skipping installation!"); + }; + + Ok(()) +}