diff --git a/src/create_hooks.rs b/src/create_hooks.rs index bf40a42ebf0acf1c03d4d31eae487d6e0152ee50..d085a48134dd2b6a791788d4ef27b6b34836f612 100644 --- a/src/create_hooks.rs +++ b/src/create_hooks.rs @@ -1,3 +1,4 @@ +use std::fs; use std::fs::File; use std::fs::OpenOptions; use std::io::Write; @@ -93,18 +94,27 @@ fn update_config_file() { eprintln!("Error: The file .git/config wasn't found"); exit(19); } - let mut file = OpenOptions::new() - .write(true) - .append(true) - .open(&git_config) - .unwrap(); - writeln!(file, "[alias]\n\tco = checkout -q").unwrap(); + let content = fs::read_to_string(&git_config).unwrap(); + let alias = format!( + "{}\n\t{}\n\t{}", + "co = checkout -q", + "conh = -c core.hooksPath=/dev/null checkout", + "cnh = -c core.hooksPath=/dev/null commit" + ); + if !content.contains(&alias) { + let mut file = OpenOptions::new() + .write(true) + .append(true) + .open(&git_config) + .unwrap(); + writeln!(file, "[alias]\n\t{}", alias).unwrap(); + } } /// Create 2 files in `.git/hooks` folder /// /// 1. Create post-commit hook -/// 3. Create post-checkout hook +/// 2. Create post-checkout hook /// /// # Arguments /// * `compression`: The compression that will automatically be used to save the results folder (no, lz4, zstd, zlib or lzma) after a git commit @@ -138,3 +148,25 @@ pub fn create_hooks(compression: &str, mode: &str) { create_file(&git_folder, fname, &content); } } + +/// Delete 2 hooks files in `.git/hooks` folder +/// +/// 1. Create post-commit hook +/// 2. Create post-checkout hook +pub fn delete_hooks() { + let git_folder = get_hooks_folder(); + let file_name = vec!["post-commit", "post-checkout"]; + for cfile in file_name { + let mut hfile = git_folder.to_owned(); + hfile.push(cfile); + let cmd = format!("rm {}", hfile.to_str().unwrap()); + let output = Command::new("sh").arg("-c").arg(cmd).output().unwrap(); + match output.status.code().unwrap() { + 0 => (), + num => { + eprintln!("{}", String::from_utf8(output.stderr).unwrap()); + exit(num) + } + } + } +}