diff --git a/scripts/commit-msg.crs b/scripts/commit-msg.crs index 35271df..cbf28d7 100755 --- a/scripts/commit-msg.crs +++ b/scripts/commit-msg.crs @@ -4,6 +4,15 @@ use std::{ env, fs, process }; use std::path::PathBuf; use std::os::unix::fs::PermissionsExt; +const RED: &str = "\x1B[91m"; +const GREEN: &str = "\x1B[92m"; +const YELLOW: &str = "\x1B[93m"; +const BOLD: &str = "\x1B[1m"; +const UNDER: &str = "\x1B[4m"; +const END: &str = "\x1B[0m"; + +const DEFAULT_COMMIT_MSG_REGEXP: &str = "^(feat|fix|docs|style|refactor|test|chore)(\\([\\w\\-_.]+\\))?:\\s*.*"; + /// Git commit message format check util /// /// Commit format MUST be (): subject @@ -50,18 +59,10 @@ fn main() { } } -const RED: &str = "\x1B[91m"; -const GREEN: &str = "\x1B[92m"; -const YELLOW: &str = "\x1B[93m"; -const BOLD: &str = "\x1B[1m"; -const UNDER: &str = "\x1B[4m"; -const END: &str = "\x1B[0m"; - -const DEFAULT_COMMIT_MSG_REGEXP: &str = "^(feat|fix|docs|style|refactor|test|chore)(\\([\\w\\-_.]+\\))?:\\s*.*"; - fn print_usage() { - let usage = get_commit_msg_usage(); - if !usage.is_empty() { print_info(&usage); return; } + if let Some(usage) = get_commit_msg_usage() { + print_info(&usage); return; + } print_info(&format!(r#"Please follow the commit spec: Format: {b}{e}(){b}: {e} is optional @@ -94,9 +95,7 @@ fn search_git_root_path() -> Option { if p.join(".git").is_dir() { return Some(p); } - if !p.pop() { - return None; - } + if !p.pop() { return None; } } } @@ -105,48 +104,48 @@ fn search_git_root_path_e() -> PathBuf { } fn get_home_e() -> PathBuf { - PathBuf::from(env::var("HOME").unwrap_or_else(|e| { - exit_with_error_message(&format!("Get env HOME failed: {}!", e)); - })) + PathBuf::from(env::var("HOME").unwrap_or_else(|e| + exit_with_error_message(&format!("Get env HOME failed: {}!", e)) + )) } -fn get_commit_msg_file(file: &str, default_content: &str) -> String { +fn get_commit_msg_file(file: &str) -> Option { let settings_regexp = search_git_root_path_e().join("settings").join(file); if settings_regexp.exists() { print_info(&format!("Found file: {:?}", settings_regexp)); - return fs::read_to_string(settings_regexp).unwrap(); + return Some(fs::read_to_string(settings_regexp).unwrap()); } let path_regexp = search_git_root_path_e().join(".git").join("hooks").join(file); if path_regexp.exists() { print_info(&format!("Found file: {:?}", path_regexp)); - return fs::read_to_string(path_regexp).unwrap(); + return Some(fs::read_to_string(path_regexp).unwrap()); } let home_path_regexp = get_home_e().join(&(".".to_owned() + file)); if home_path_regexp.exists() { print_info(&format!("Found file: {:?}", home_path_regexp)); - return fs::read_to_string(home_path_regexp).unwrap(); + return Some(fs::read_to_string(home_path_regexp).unwrap()); } - default_content.to_owned() + None } fn get_commit_msg_regexp() -> String { - get_commit_msg_file("commit-msg-regexp", DEFAULT_COMMIT_MSG_REGEXP) + get_commit_msg_file("commit-msg-regexp").unwrap_or_else(|| DEFAULT_COMMIT_MSG_REGEXP.to_owned()) } -fn get_commit_msg_usage() -> String { - get_commit_msg_file("commit-msg-regexp-usage", "") +fn get_commit_msg_usage() -> Option { + get_commit_msg_file("commit-msg-regexp-usage") } fn install_commit_msg(force: bool) { let commit_msg_crs = get_home_e().join("bin").join("commit-msg.crs"); - let commig_msg_exec_file = if !commit_msg_crs.exists() { + let commig_msg_exec_file = if commit_msg_crs.exists() { + commit_msg_crs + } else { print_warn(&format!("File {:?} NOT exists!", commit_msg_crs)); PathBuf::from(env::args().next().unwrap()) - } else { - commit_msg_crs }; let git_hooks = search_git_root_path_e().join(".git").join("hooks"); @@ -175,7 +174,7 @@ fn install_commit_msg(force: bool) { fn exit_with_error() -> ! { process::exit(1) } fn exit_with_error_message(msg: &str) -> ! { print_error(msg); exit_with_error() } -fn print_info(msg: &str) { println!("{b}[INFO ]{e} {m}", b = BOLD, e = END, m = msg); } -fn print_ok(msg: &str) { println!("{g}{b}[OK ]{e} {g}{m}{e}", g = GREEN, b = BOLD, e = END, m = msg); } +fn print_info(msg: &str) { println!("{b}[INFO ]{e} {m}", b = BOLD, e = END, m = msg); } +fn print_ok(msg: &str) { println!("{g}{b}[OK ]{e} {g}{m}{e}", g = GREEN, b = BOLD, e = END, m = msg); } fn print_warn(msg: &str) { println!("{y}{b}[WARN ]{e} {y}{m}{e}", y = YELLOW, b = BOLD, e = END, m = msg); } -fn print_error(msg: &str) { println!("{r}{b}[ERROR]{e} {r}{m}{e}", r = RED, b = BOLD, e = END, m = msg); } +fn print_error(msg: &str) { println!("{r}{b}[ERROR]{e} {r}{m}{e}", r = RED, b = BOLD, e = END, m = msg); }