From eefdd24304ee93370b5a209f150e0125b099ba9f Mon Sep 17 00:00:00 2001 From: Fontrodona Nicolas <nicolas.fontrodona@ens-lyon.fr> Date: Wed, 11 May 2022 15:40:34 +0200 Subject: [PATCH] src/create_hooks.rs: create gbl hooks --- src/create_hooks.rs | 101 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/create_hooks.rs diff --git a/src/create_hooks.rs b/src/create_hooks.rs new file mode 100644 index 0000000..7e98eed --- /dev/null +++ b/src/create_hooks.rs @@ -0,0 +1,101 @@ +use std::fs::File; +use std::io::Write; +use std::{ + path::PathBuf, + process::{exit, Command}, +}; + +/// Function used to get the git hooks repository +fn get_hooks_folder() -> PathBuf { + let output = Command::new("git") + .arg("rev-parse") + .arg("--show-cdup") + .output() + .unwrap(); + match output.status.code().unwrap() { + 128 => { + eprintln!("Not a git repository !"); + exit(128); + } + 127 => { + eprintln!("Git not found !"); + exit(127); + } + 0 => (), + num => { + eprintln!("{}", String::from_utf8(output.stderr).unwrap()); + exit(num) + } + } + let mut string_path = String::from_utf8(output.stdout).unwrap(); + if string_path.ends_with('\n') { + string_path.pop(); + } + let mut p = PathBuf::from(&string_path); + p.push(".git"); + if !p.is_dir() { + eprintln!("Folder {} not found !", p.to_str().unwrap()); + exit(5); + } + p +} + +/// Function used to create a file +/// +/// # Arguments: +/// * `folder`: The folder where the file will be created +/// * `file_name`: The file name of the file to create +/// * `content`: The content of the file to create +fn create_file(folder: &PathBuf, file_name: &str, content: &str) { + let mut hfile = folder.to_owned(); + hfile.push(file_name); + if hfile.is_file() { + eprintln!( + "Warning: The file {} already exists. Skipping...", + hfile.to_str().unwrap() + ); + return (); + } + let mut file = File::open(&hfile).unwrap_or_else(|e| { + eprintln!( + "Unable to create the file {}.\n{}", + hfile.to_str().unwrap(), + e + ); + exit(6); + }); + write!(file, "{}", content).unwrap_or_else(|e| { + eprintln!( + "Unable to write the file {}.\n{}", + hfile.to_str().unwrap(), + e + ); + exit(7); + }); +} + +/// Create 3 files in `.git/hooks` folder +/// +/// 1. Create pre-commit hook +/// 2. Create pre-checkout hook +/// 3. Create post-checkout hook +pub fn create_hooks(compression: &str) { + let git_folder = get_hooks_folder(); + let pre_commit_cmd = format!("gbl commit --compression {}", &compression); + let pre_commit_cmd = pre_commit_cmd.as_str(); + let pre_co_cmd = "gbl pre-co"; + let post_co_cmd = "gbl checkout"; + let v = vec![pre_commit_cmd, pre_co_cmd, post_co_cmd]; + let file_name = vec!["pre-commit", "pre-checkout", "post-checkout"]; + for (fname, c_cmd) in v.iter().zip(file_name.iter()) { + let content = format!( + "#!/bin/sh \n\ + \n\ + {} + \n\ + ", + c_cmd + ); + create_file(&git_folder, fname, &content); + } +} -- GitLab