Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a command to install a git hook to automatically run x.py test tidy --bless #76356

Merged
merged 7 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bootstrap/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ impl<'a> Builder<'a> {
install::Src,
install::Rustc
),
Kind::Run => describe!(run::ExpandYamlAnchors, run::BuildManifest,),
Kind::Run => describe!(run::ExpandYamlAnchors, run::BuildManifest),
}
}

Expand Down
40 changes: 40 additions & 0 deletions src/bootstrap/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)?;
caass marked this conversation as resolved.
Show resolved Hide resolved
break match input.trim().to_lowercase().as_str() {
"y" | "yes" => true,
// is this the right way to check for "entered nothing"?
"n" | "no" | "" => false,
caass marked this conversation as resolved.
Show resolved Hide resolved
_ => {
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");
caass marked this conversation as resolved.
Show resolved Hide resolved
let dst = src_path.join("/.git/hooks/pre-commit");
caass marked this conversation as resolved.
Show resolved Hide resolved
fs::hard_link(src, dst)?;
println!("Linked `src/etc/pre-commit.sh` to `.git/hooks/pre-commit`");
} else {
println!("Ok, skipping installation!");
};

Ok(())
}
21 changes: 21 additions & 0 deletions src/etc/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
#
# Call `tidy --bless` before each commit
# Copy this scripts to .git/hooks to activate,
# and remove it from .git/hooks to deactivate.
#

set -Eeuo pipefail

ROOT_DIR="$(git rev-parse --show-toplevel)";
COMMAND="$ROOT_DIR/x.py test tidy --bless";
caass marked this conversation as resolved.
Show resolved Hide resolved

if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
COMMAND="python $COMMAND"
fi

echo "Running pre-commit script '$COMMAND'";

cd "$ROOT_DIR"

$COMMAND;