diff --git a/docs/dev/README.md b/docs/dev/README.md index 006518afc0..0823ca09ab 100644 --- a/docs/dev/README.md +++ b/docs/dev/README.md @@ -55,7 +55,7 @@ We use Travis for CI. Most of the things, including formatting, are checked by be green as well. We use bors-ng to enforce the [not rocket science](https://graydon2.dreamwidth.org/1597.html) rule. -You can run `cargo format-hook` to install git-hook to run rustfmt on commit. +You can run `cargo xtask install-pre-commit-hook` to install git-hook to run rustfmt on commit. # Code organization diff --git a/xtask/src/bin/pre-commit.rs b/xtask/src/bin/pre-commit.rs deleted file mode 100644 index 44507fb748..0000000000 --- a/xtask/src/bin/pre-commit.rs +++ /dev/null @@ -1,31 +0,0 @@ -//! FIXME: write short doc here - -use std::process::Command; - -use xtask::{codegen::Mode, project_root, run, run_rustfmt, Result}; - -fn main() -> Result<()> { - run_rustfmt(Mode::Overwrite)?; - update_staged() -} - -fn update_staged() -> Result<()> { - let root = project_root(); - let output = Command::new("git") - .arg("diff") - .arg("--diff-filter=MAR") - .arg("--name-only") - .arg("--cached") - .current_dir(&root) - .output()?; - if !output.status.success() { - anyhow::bail!( - "`git diff --diff-filter=MAR --name-only --cached` exited with {}", - output.status - ); - } - for line in String::from_utf8(output.stdout)?.lines() { - run(&format!("git update-index --add {}", root.join(line).to_string_lossy()), ".")?; - } - Ok(()) -} diff --git a/xtask/src/help.rs b/xtask/src/help.rs index 730eb5c615..f4e25dcde9 100644 --- a/xtask/src/help.rs +++ b/xtask/src/help.rs @@ -10,7 +10,7 @@ FLAGS: SUBCOMMANDS: format - format-hook + install-pre-commit-hook fuzz-tests codegen install diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index bfee2f9c8e..7332a40729 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -83,19 +83,12 @@ pub fn install_rustfmt() -> Result<()> { run(&format!("rustup component add rustfmt --toolchain {}", TOOLCHAIN), ".") } -pub fn install_format_hook() -> Result<()> { - let result_path = Path::new(if cfg!(windows) { - "./.git/hooks/pre-commit.exe" - } else { - "./.git/hooks/pre-commit" - }); +pub fn install_pre_commit_hook() -> Result<()> { + let result_path = + PathBuf::from(format!("./.git/hooks/pre-commit{}", std::env::consts::EXE_SUFFIX)); if !result_path.exists() { - run("cargo build --package xtask --bin pre-commit", ".")?; - if cfg!(windows) { - fs::copy("./target/debug/pre-commit.exe", result_path)?; - } else { - fs::copy("./target/debug/pre-commit", result_path)?; - } + let me = std::env::current_exe()?; + fs::copy(me, result_path)?; } else { Err(IoError::new(ErrorKind::AlreadyExists, "Git hook already created"))?; } @@ -150,6 +143,27 @@ pub fn run_fuzzer() -> Result<()> { run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax") } +pub fn reformat_staged_files() -> Result<()> { + let root = project_root(); + let output = Command::new("git") + .arg("diff") + .arg("--diff-filter=MAR") + .arg("--name-only") + .arg("--cached") + .current_dir(&root) + .output()?; + if !output.status.success() { + anyhow::bail!( + "`git diff --diff-filter=MAR --name-only --cached` exited with {}", + output.status + ); + } + for line in String::from_utf8(output.stdout)?.lines() { + run(&format!("git update-index --add {}", root.join(line).to_string_lossy()), ".")?; + } + Ok(()) +} + fn do_run(cmdline: &str, dir: &str, mut f: F) -> Result where F: FnMut(&mut Command), diff --git a/xtask/src/main.rs b/xtask/src/main.rs index f14e6c8ae7..663e281035 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -16,7 +16,8 @@ use pico_args::Arguments; use std::{env, path::PathBuf}; use xtask::{ codegen::{self, Mode}, - install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt, run_with_output, Cmd, Result, + install_pre_commit_hook, reformat_staged_files, run, run_clippy, run_fuzzer, run_rustfmt, + run_with_output, Cmd, Result, }; // Latest stable, feel free to send a PR if this lags behind. @@ -36,6 +37,10 @@ struct ServerOpt { } fn main() -> Result<()> { + if std::env::args().next().map(|it| it.contains("pre-commit")) == Some(true) { + return reformat_staged_files(); + } + let subcommand = match std::env::args_os().nth(1) { None => { eprintln!("{}", help::GLOBAL_HELP); @@ -81,12 +86,12 @@ fn main() -> Result<()> { } run_rustfmt(Mode::Overwrite)? } - "format-hook" => { + "install-pre-commit-hook" => { if matches.contains(["-h", "--help"]) { help::print_no_param_subcommand_help(&subcommand); return Ok(()); } - install_format_hook()? + install_pre_commit_hook()? } "lint" => { if matches.contains(["-h", "--help"]) {