rust-analyzer/xtask/src/pre_commit.rs

37 lines
878 B
Rust
Raw Normal View History

2020-01-07 13:42:56 +00:00
//! pre-commit hook for code formatting.
use std::{fs, path::PathBuf};
use anyhow::{bail, Result};
2020-02-14 14:59:19 +00:00
use crate::{not_bash::run, project_root, run_rustfmt, Mode};
2020-01-07 13:42:56 +00:00
// FIXME: if there are changed `.ts` files, also reformat TypeScript (by
// shelling out to `npm fmt`).
pub fn run_hook() -> Result<()> {
run_rustfmt(Mode::Overwrite)?;
2020-02-14 14:59:19 +00:00
let diff = run!("git diff --diff-filter=MAR --name-only --cached")?;
2020-01-07 13:42:56 +00:00
let root = project_root();
2020-02-10 14:16:07 +00:00
for line in diff.lines() {
2020-02-14 14:59:19 +00:00
run!("git update-index --add {}", root.join(line).display())?;
2020-01-07 13:42:56 +00:00
}
Ok(())
}
pub fn install_hook() -> Result<()> {
let hook_path: PathBuf =
format!("./.git/hooks/pre-commit{}", std::env::consts::EXE_SUFFIX).into();
if hook_path.exists() {
bail!("Git hook already created");
}
let me = std::env::current_exe()?;
fs::copy(me, hook_path)?;
Ok(())
}