Skip to content
Snippets Groups Projects
Commit eefdd243 authored by nfontrod's avatar nfontrod
Browse files

src/create_hooks.rs: create gbl hooks

parent ca5a6510
No related branches found
No related tags found
No related merge requests found
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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment