From b48f041befdca6ad14acfda2db68c280ee3fbc85 Mon Sep 17 00:00:00 2001 From: xFrednet Date: Wed, 16 Jun 2021 18:59:28 +0200 Subject: [PATCH] Added the `cargo dev remove` command for convenience --- clippy_dev/src/main.rs | 13 +++++++- clippy_dev/src/setup/git_hook.rs | 53 ++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/clippy_dev/src/main.rs b/clippy_dev/src/main.rs index faf8700f5..70c3d93ed 100644 --- a/clippy_dev/src/main.rs +++ b/clippy_dev/src/main.rs @@ -38,9 +38,14 @@ fn main() { }, ("setup", Some(sub_command)) => match sub_command.subcommand() { ("intellij", Some(matches)) => setup::intellij::run(matches.value_of("rustc-repo-path")), - ("git-hook", Some(matches)) => setup::git_hook::run(matches.is_present("force-override")), + ("git-hook", Some(matches)) => setup::git_hook::install_hook(matches.is_present("force-override")), _ => {}, }, + ("remove", Some(sub_command)) => { + if let ("git-hook", Some(_)) = sub_command.subcommand() { + setup::git_hook::remove_hook(); + } + }, ("serve", Some(matches)) => { let port = matches.value_of("port").unwrap().parse().unwrap(); let lint = matches.value_of("lint"); @@ -173,6 +178,12 @@ fn get_clap_config<'a>() -> ArgMatches<'a> { ), ), ) + .subcommand( + SubCommand::with_name("remove") + .about("Support for undoing changes done by the setup command") + .setting(AppSettings::ArgRequiredElseHelp) + .subcommand(SubCommand::with_name("git-hook").about("Remove any existing pre-commit git hook")), + ) .subcommand( SubCommand::with_name("serve") .about("Launch a local 'ALL the Clippy Lints' website in a browser") diff --git a/clippy_dev/src/setup/git_hook.rs b/clippy_dev/src/setup/git_hook.rs index 741738e37..beb07a073 100644 --- a/clippy_dev/src/setup/git_hook.rs +++ b/clippy_dev/src/setup/git_hook.rs @@ -6,11 +6,11 @@ use std::path::Path; /// the hook if `clippy_dev` would be used in the rust tree. The hook also references this tool /// for formatting and should therefor only be used in a normal clone of clippy const REPO_GIT_DIR: &str = ".git"; -const HOOK_SOURCE_PATH: &str = "util/etc/pre-commit.sh"; -const HOOK_TARGET_PATH: &str = ".git/hooks/pre-commit"; +const HOOK_SOURCE_FILE: &str = "util/etc/pre-commit.sh"; +const HOOK_TARGET_FILE: &str = ".git/hooks/pre-commit"; -pub fn run(force_override: bool) { - if let Err(_) = check_precondition(force_override) { +pub fn install_hook(force_override: bool) { + if check_precondition(force_override).is_err() { return; } @@ -23,11 +23,14 @@ pub fn run(force_override: bool) { // that we can check in a file with execution permissions and the sync it to create // a file with the flag set. We then copy this file here. The copy function will also // include the `execute` permission. - match fs::copy(HOOK_SOURCE_PATH, HOOK_TARGET_PATH) { - Ok(_) => println!("Git hook successfully installed :)"), + match fs::copy(HOOK_SOURCE_FILE, HOOK_TARGET_FILE) { + Ok(_) => { + println!("note: the hook can be removed with `cargo dev remove git-hook`"); + println!("Git hook successfully installed :)"); + }, Err(err) => println!( "error: unable to copy `{}` to `{}` ({})", - HOOK_SOURCE_PATH, HOOK_TARGET_PATH, err + HOOK_SOURCE_FILE, HOOK_TARGET_FILE, err ), } } @@ -41,21 +44,33 @@ fn check_precondition(force_override: bool) -> Result<(), ()> { } // Make sure that we don't override an existing hook by accident - let path = Path::new(HOOK_TARGET_PATH); + let path = Path::new(HOOK_TARGET_FILE); if path.exists() { - if !force_override { - println!("warn: The found `.git` directory already has a commit hook"); - } - - if force_override || super::ask_yes_no_question("Do you want to override it?") { - if fs::remove_file(path).is_err() { - println!("error: unable to delete existing pre-commit git hook"); - return Err(()); - } - } else { - return Err(()); + if force_override || super::ask_yes_no_question("Do you want to override the existing pre-commit hook it?") { + return delete_git_hook_file(path); } + return Err(()); } Ok(()) } + +pub fn remove_hook() { + let path = Path::new(HOOK_TARGET_FILE); + if path.exists() { + if delete_git_hook_file(path).is_ok() { + println!("Git hook successfully removed :)"); + } + } else { + println!("No pre-commit hook was found. You're good to go :)"); + } +} + +fn delete_git_hook_file(path: &Path) -> Result<(), ()> { + if fs::remove_file(path).is_err() { + println!("error: unable to delete existing pre-commit git hook"); + Err(()) + } else { + Ok(()) + } +}