Skip to main content

gitru/hook/
mod.rs

1use crate::util::colored_print::{print_error, print_success};
2
3pub mod commit_msg;
4
5pub fn init(hook: &str, force: bool) -> Result<(), String> {
6    // write files to the current project root based on the hook type
7    match hook {
8        "commit-msg" => {
9            commit_msg::init(force)?;
10        }
11        _ => {
12            return Err(
13                std::io::Error::new(std::io::ErrorKind::InvalidInput, "unknown hook").to_string(),
14            );
15        }
16    }
17
18    Ok(())
19}
20
21pub fn install(hook: &str, force: bool) -> Result<(), String> {
22    match hook {
23        "commit-msg" => {
24            commit_msg::install(force)?;
25        }
26        _ => {
27            return Err(
28                std::io::Error::new(std::io::ErrorKind::InvalidInput, "unknown hook").to_string(),
29            );
30        }
31    }
32
33    Ok(())
34}
35
36pub fn uninstall(hook: &str) -> Result<(), String> {
37    match hook {
38        "commit-msg" => {
39            commit_msg::uninstall()?;
40        }
41        _ => {
42            return Err(
43                std::io::Error::new(std::io::ErrorKind::InvalidInput, "unknown hook").to_string(),
44            );
45        }
46    }
47
48    Ok(())
49}
50
51pub fn run_hook(hook: &str, file: Option<&str>) -> Result<(), String> {
52    match hook {
53        "commit-msg" => {
54            let path = file.ok_or("commit-msg requires a file")?;
55
56            if let Err(e) = commit_msg::run(path) {
57                print_error(&e);
58                // status code 1 means fail
59                std::process::exit(1);
60            }
61
62            // status code 0 means success
63            print_success("commit-msg validation passed");
64            std::process::exit(0);
65        }
66        _ => Err(format!("unsupported hook: {}", hook)),
67    }
68}