aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/pre_commit.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-01-07 14:37:27 +0000
committerGitHub <[email protected]>2020-01-07 14:37:27 +0000
commit9df1663c12f48df7f4921e68c928016877390565 (patch)
tree4b03a633fcb27c7fdeb5322b1bb3ca3889778b7f /xtask/src/pre_commit.rs
parent6c1401404baca846103c85e048b5d0a959ddc81b (diff)
parent5e7995eeb7b7ab4cf0d80ddfa2d20e506216f895 (diff)
Merge #2755
2755: Cleanup r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'xtask/src/pre_commit.rs')
-rw-r--r--xtask/src/pre_commit.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/xtask/src/pre_commit.rs b/xtask/src/pre_commit.rs
new file mode 100644
index 000000000..88e868ca6
--- /dev/null
+++ b/xtask/src/pre_commit.rs
@@ -0,0 +1,36 @@
1//! pre-commit hook for code formatting.
2
3use std::{fs, path::PathBuf};
4
5use anyhow::{bail, Result};
6
7use crate::{cmd::run_with_output, project_root, run, run_rustfmt, Mode};
8
9// FIXME: if there are changed `.ts` files, also reformat TypeScript (by
10// shelling out to `npm fmt`).
11pub fn run_hook() -> Result<()> {
12 run_rustfmt(Mode::Overwrite)?;
13
14 let diff = run_with_output("git diff --diff-filter=MAR --name-only --cached", ".")?;
15
16 let root = project_root();
17 for line in String::from_utf8(diff.stdout)?.lines() {
18 run(&format!("git update-index --add {}", root.join(line).to_string_lossy()), ".")?;
19 }
20
21 Ok(())
22}
23
24pub fn install_hook() -> Result<()> {
25 let hook_path: PathBuf =
26 format!("./.git/hooks/pre-commit{}", std::env::consts::EXE_SUFFIX).into();
27
28 if hook_path.exists() {
29 bail!("Git hook already created");
30 }
31
32 let me = std::env::current_exe()?;
33 fs::copy(me, hook_path)?;
34
35 Ok(())
36}