Skip to content

Commit a009e28

Browse files
committed
There isn't a way to pass --remove yet, but you can rm if u like
1 parent d245464 commit a009e28

File tree

8 files changed

+70
-1
lines changed

8 files changed

+70
-1
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,10 @@ dependencies = [
14071407
"hashbrown",
14081408
]
14091409

1410+
[[package]]
1411+
name = "install-git-hook"
1412+
version = "0.1.0"
1413+
14101414
[[package]]
14111415
name = "installer"
14121416
version = "0.0.0"

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ members = [
88
"src/tools/clippy",
99
"src/tools/compiletest",
1010
"src/tools/error_index_generator",
11+
"src/tools/install-git-hook",
1112
"src/tools/linkchecker",
1213
"src/tools/rustbook",
1314
"src/tools/unstable-book-gen",

src/bootstrap/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ impl<'a> Builder<'a> {
487487
install::Src,
488488
install::Rustc
489489
),
490-
Kind::Run => describe!(run::ExpandYamlAnchors,),
490+
Kind::Run => describe!(run::ExpandYamlAnchors, run::InstallGitHook),
491491
}
492492
}
493493

src/bootstrap/run.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,27 @@ fn try_run(builder: &Builder<'_>, cmd: &mut Command) -> bool {
4141
}
4242
true
4343
}
44+
45+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
46+
pub struct InstallGitHook;
47+
48+
impl Step for InstallGitHook {
49+
type Output = ();
50+
51+
/// Runs the `install-git-hook` tool.
52+
///
53+
/// This tool in `src/tools` installs a git hook to automatically run
54+
/// `tidy --bless` before each commit, so you don't forget to do it
55+
fn run(self, builder: &Builder<'_>) {
56+
builder.info("Installing git hook");
57+
try_run(builder, &mut builder.tool_cmd(Tool::InstallGitHook).arg(&builder.src));
58+
}
59+
60+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
61+
run.path("src/tools/install-git-hook")
62+
}
63+
64+
fn make_run(run: RunConfig<'_>) {
65+
run.builder.ensure(InstallGitHook);
66+
}
67+
}

src/bootstrap/tool.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ bootstrap_tool!(
365365
RustInstaller, "src/tools/rust-installer", "fabricate", is_external_tool = true;
366366
RustdocTheme, "src/tools/rustdoc-themes", "rustdoc-themes";
367367
ExpandYamlAnchors, "src/tools/expand-yaml-anchors", "expand-yaml-anchors";
368+
InstallGitHook, "src/tools/install-git-hook", "install-git-hook";
368369
);
369370

370371
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]

src/tools/install-git-hook/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "install-git-hook"
3+
version = "0.1.0"
4+
authors = ["Cass Fridkin <[email protected]>"]
5+
edition = "2018"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! Small helper program to install a git hook to automatically run
2+
//! `x.py test tidy --bless` before each commit.
3+
4+
use std::env;
5+
use std::fs;
6+
use std::path::PathBuf;
7+
8+
fn main() {
9+
let root_path: PathBuf = env::args_os().nth(1).expect("need path to root of repo").into();
10+
let script_path: PathBuf = root_path.join("src/tools/install-git-hook/src/pre-commit.sh");
11+
let hook_path: PathBuf = root_path.join(".git/hooks/pre-commit");
12+
13+
fs::copy(&script_path, &hook_path).expect(
14+
format!("Failed to copy pre-commit script to {}", &hook_path.to_string_lossy()).as_str(),
15+
);
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
#
3+
# Call `tidy --bless` before each commit
4+
#
5+
# To enable this hook, run `./x.py run install-git-hook`.
6+
# To disable it, run `./x.py run install-git-hook --remove`
7+
set -Eeuo pipefail
8+
9+
ROOT_DIR=$(git rev-parse --show-toplevel);
10+
COMMAND="$ROOT_DIR/x.py test tidy --bless";
11+
12+
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
13+
COMMAND="python $COMMAND"
14+
fi
15+
16+
echo "Running pre-commit script $COMMAND";
17+
18+
$COMMAND;

0 commit comments

Comments
 (0)