From 76da22e66aaccda4a428e41e233ef7f3732463fd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 18 Nov 2019 15:22:51 +0300 Subject: Don't create a separate bin for format hook --- xtask/src/bin/pre-commit.rs | 31 ------------------------------- xtask/src/help.rs | 2 +- xtask/src/lib.rs | 38 ++++++++++++++++++++++++++------------ xtask/src/main.rs | 11 ++++++++--- 4 files changed, 35 insertions(+), 47 deletions(-) delete mode 100644 xtask/src/bin/pre-commit.rs (limited to 'xtask') diff --git a/xtask/src/bin/pre-commit.rs b/xtask/src/bin/pre-commit.rs deleted file mode 100644 index 44507fb74..000000000 --- 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 730eb5c61..f4e25dcde 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 bfee2f9c8..7332a4072 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 f14e6c8ae..663e28103 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"]) { -- cgit v1.2.3