From 54657adaf0884bc47bc24f287a919f21afac2b24 Mon Sep 17 00:00:00 2001 From: Bauke Date: Fri, 28 Oct 2022 20:24:07 +0200 Subject: [PATCH] Add the uninstall subcommand. --- hooked-cli/source/cli/mod.rs | 9 ++++++++- hooked-cli/source/main.rs | 21 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/hooked-cli/source/cli/mod.rs b/hooked-cli/source/cli/mod.rs index 605c335..0b86e91 100644 --- a/hooked-cli/source/cli/mod.rs +++ b/hooked-cli/source/cli/mod.rs @@ -24,7 +24,14 @@ pub enum MainSubcommands { /// Install Hooked into ".git/hooks". Install { /// Overwrite existing files. - #[clap(long, default_value = "false")] + #[clap(long)] overwrite: bool, }, + + /// Remove installed hooks. + Uninstall { + /// Remove hooks not installed by Hooked. + #[clap(long)] + all: bool, + }, } diff --git a/hooked-cli/source/main.rs b/hooked-cli/source/main.rs index 8316a7c..a894533 100644 --- a/hooked-cli/source/main.rs +++ b/hooked-cli/source/main.rs @@ -6,7 +6,7 @@ #![warn(missing_docs, clippy::missing_docs_in_private_items)] use std::{ - fs::{set_permissions, write, Permissions}, + fs::{read_to_string, remove_file, set_permissions, write, Permissions}, os::unix::fs::PermissionsExt, path::PathBuf, }; @@ -61,6 +61,25 @@ fn main() -> Result<()> { set_permissions(hook_path, Permissions::from_mode(0o775))?; } } + + MainSubcommands::Uninstall { all } => { + for hook_type in HOOK_TYPES { + let hook_path = git_hooks_dir.join(hook_type); + if !hook_path.exists() { + continue; + } + + let hook_contents = read_to_string(&hook_path)?; + if all || hook_contents.contains("# Installed by Hooked.") { + remove_file(hook_path)?; + } else { + println!( + "{:?} wasn't installed by Hooked, use --all to remove it", + hook_path + ); + } + } + } } Ok(())