about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCassandra Fridkin <cass@swag.lgbt>2020-09-04 21:00:58 -0400
committerCassandra Fridkin <cass@swag.lgbt>2020-09-04 21:00:58 -0400
commita009e2838b25df2761093d727d322a59f69d8f68 (patch)
treeb5277bfaa2f1d1af9d8ffaa6802f3a4644bd525b
parentd2454643e137bde519786ee9e650c455d7ad6f34 (diff)
downloadrust-a009e2838b25df2761093d727d322a59f69d8f68.tar.gz
rust-a009e2838b25df2761093d727d322a59f69d8f68.zip
There isn't a way to pass --remove yet, but you can rm if u like
-rw-r--r--Cargo.lock4
-rw-r--r--Cargo.toml1
-rw-r--r--src/bootstrap/builder.rs2
-rw-r--r--src/bootstrap/run.rs24
-rw-r--r--src/bootstrap/tool.rs1
-rw-r--r--src/tools/install-git-hook/Cargo.toml5
-rw-r--r--src/tools/install-git-hook/src/main.rs16
-rwxr-xr-xsrc/tools/install-git-hook/src/pre-commit.sh18
8 files changed, 70 insertions, 1 deletions
diff --git a/Cargo.lock b/Cargo.lock
index f94d95d2dc8..4e6c1fca351 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1408,6 +1408,10 @@ dependencies = [
 ]
 
 [[package]]
+name = "install-git-hook"
+version = "0.1.0"
+
+[[package]]
 name = "installer"
 version = "0.0.0"
 dependencies = [
diff --git a/Cargo.toml b/Cargo.toml
index fde1cb5a35c..4156ef8f124 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,7 @@ members = [
   "src/tools/clippy",
   "src/tools/compiletest",
   "src/tools/error_index_generator",
+  "src/tools/install-git-hook",
   "src/tools/linkchecker",
   "src/tools/rustbook",
   "src/tools/unstable-book-gen",
diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs
index cecc9ef75ea..0a7df6e595c 100644
--- a/src/bootstrap/builder.rs
+++ b/src/bootstrap/builder.rs
@@ -487,7 +487,7 @@ impl<'a> Builder<'a> {
                 install::Src,
                 install::Rustc
             ),
-            Kind::Run => describe!(run::ExpandYamlAnchors,),
+            Kind::Run => describe!(run::ExpandYamlAnchors, run::InstallGitHook),
         }
     }
 
diff --git a/src/bootstrap/run.rs b/src/bootstrap/run.rs
index 90053471427..34170898a54 100644
--- a/src/bootstrap/run.rs
+++ b/src/bootstrap/run.rs
@@ -41,3 +41,27 @@ fn try_run(builder: &Builder<'_>, cmd: &mut Command) -> bool {
     }
     true
 }
+
+#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
+pub struct InstallGitHook;
+
+impl Step for InstallGitHook {
+    type Output = ();
+
+    /// Runs the `install-git-hook` tool.
+    ///
+    /// This tool in `src/tools` installs a git hook to automatically run
+    /// `tidy --bless` before each commit, so you don't forget to do it
+    fn run(self, builder: &Builder<'_>) {
+        builder.info("Installing git hook");
+        try_run(builder, &mut builder.tool_cmd(Tool::InstallGitHook).arg(&builder.src));
+    }
+
+    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
+        run.path("src/tools/install-git-hook")
+    }
+
+    fn make_run(run: RunConfig<'_>) {
+        run.builder.ensure(InstallGitHook);
+    }
+}
diff --git a/src/bootstrap/tool.rs b/src/bootstrap/tool.rs
index fe3f1e78029..5d9bdc9d843 100644
--- a/src/bootstrap/tool.rs
+++ b/src/bootstrap/tool.rs
@@ -365,6 +365,7 @@ bootstrap_tool!(
     RustInstaller, "src/tools/rust-installer", "fabricate", is_external_tool = true;
     RustdocTheme, "src/tools/rustdoc-themes", "rustdoc-themes";
     ExpandYamlAnchors, "src/tools/expand-yaml-anchors", "expand-yaml-anchors";
+    InstallGitHook, "src/tools/install-git-hook", "install-git-hook";
 );
 
 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
diff --git a/src/tools/install-git-hook/Cargo.toml b/src/tools/install-git-hook/Cargo.toml
new file mode 100644
index 00000000000..1b089a84da8
--- /dev/null
+++ b/src/tools/install-git-hook/Cargo.toml
@@ -0,0 +1,5 @@
+[package]
+name = "install-git-hook"
+version = "0.1.0"
+authors = ["Cass Fridkin <cass@swag.lgbt>"]
+edition = "2018"
\ No newline at end of file
diff --git a/src/tools/install-git-hook/src/main.rs b/src/tools/install-git-hook/src/main.rs
new file mode 100644
index 00000000000..64b5233dfb3
--- /dev/null
+++ b/src/tools/install-git-hook/src/main.rs
@@ -0,0 +1,16 @@
+//! Small helper program to install a git hook to automatically run
+//! `x.py test tidy --bless` before each commit.
+
+use std::env;
+use std::fs;
+use std::path::PathBuf;
+
+fn main() {
+    let root_path: PathBuf = env::args_os().nth(1).expect("need path to root of repo").into();
+    let script_path: PathBuf = root_path.join("src/tools/install-git-hook/src/pre-commit.sh");
+    let hook_path: PathBuf = root_path.join(".git/hooks/pre-commit");
+
+    fs::copy(&script_path, &hook_path).expect(
+        format!("Failed to copy pre-commit script to {}", &hook_path.to_string_lossy()).as_str(),
+    );
+}
diff --git a/src/tools/install-git-hook/src/pre-commit.sh b/src/tools/install-git-hook/src/pre-commit.sh
new file mode 100755
index 00000000000..f47ca3e039a
--- /dev/null
+++ b/src/tools/install-git-hook/src/pre-commit.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+#
+# Call `tidy --bless` before each commit
+#
+# To enable this hook, run `./x.py run install-git-hook`.
+# To disable it, run `./x.py run install-git-hook --remove`
+set -Eeuo pipefail
+
+ROOT_DIR=$(git rev-parse --show-toplevel);
+COMMAND="$ROOT_DIR/x.py test tidy --bless";
+
+if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
+  COMMAND="python $COMMAND"
+fi
+
+echo "Running pre-commit script $COMMAND";
+
+$COMMAND;