style: code style

This commit is contained in:
2020-07-19 21:38:11 +08:00
parent 8e5596472d
commit be2b083c02

View File

@@ -4,6 +4,15 @@ use std::{ env, fs, process };
use std::path::PathBuf; use std::path::PathBuf;
use std::os::unix::fs::PermissionsExt; 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 /// Git commit message format check util
/// ///
/// Commit format MUST be <type>(<scope>): subject /// Commit format MUST be <type>(<scope>): 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() { fn print_usage() {
let usage = get_commit_msg_usage(); if let Some(usage) = get_commit_msg_usage() {
if !usage.is_empty() { print_info(&usage); return; } print_info(&usage); return;
}
print_info(&format!(r#"Please follow the commit spec: print_info(&format!(r#"Please follow the commit spec:
Format: {b}<type>{e}(<scope>){b}: <subject>{e} Format: {b}<type>{e}(<scope>){b}: <subject>{e}
<scope> is optional <scope> is optional
@@ -94,9 +95,7 @@ fn search_git_root_path() -> Option<PathBuf> {
if p.join(".git").is_dir() { if p.join(".git").is_dir() {
return Some(p); return Some(p);
} }
if !p.pop() { if !p.pop() { return None; }
return None;
}
} }
} }
@@ -105,48 +104,48 @@ fn search_git_root_path_e() -> PathBuf {
} }
fn get_home_e() -> PathBuf { fn get_home_e() -> PathBuf {
PathBuf::from(env::var("HOME").unwrap_or_else(|e| { PathBuf::from(env::var("HOME").unwrap_or_else(|e|
exit_with_error_message(&format!("Get env HOME failed: {}!", 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<String> {
let settings_regexp = search_git_root_path_e().join("settings").join(file); let settings_regexp = search_git_root_path_e().join("settings").join(file);
if settings_regexp.exists() { if settings_regexp.exists() {
print_info(&format!("Found file: {:?}", settings_regexp)); 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); let path_regexp = search_git_root_path_e().join(".git").join("hooks").join(file);
if path_regexp.exists() { if path_regexp.exists() {
print_info(&format!("Found file: {:?}", path_regexp)); 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)); let home_path_regexp = get_home_e().join(&(".".to_owned() + file));
if home_path_regexp.exists() { if home_path_regexp.exists() {
print_info(&format!("Found file: {:?}", home_path_regexp)); 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 { 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 { fn get_commit_msg_usage() -> Option<String> {
get_commit_msg_file("commit-msg-regexp-usage", "") get_commit_msg_file("commit-msg-regexp-usage")
} }
fn install_commit_msg(force: bool) { fn install_commit_msg(force: bool) {
let commit_msg_crs = get_home_e().join("bin").join("commit-msg.crs"); 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)); print_warn(&format!("File {:?} NOT exists!", commit_msg_crs));
PathBuf::from(env::args().next().unwrap()) PathBuf::from(env::args().next().unwrap())
} else {
commit_msg_crs
}; };
let git_hooks = search_git_root_path_e().join(".git").join("hooks"); 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() -> ! { process::exit(1) }
fn exit_with_error_message(msg: &str) -> ! { print_error(msg); exit_with_error() } 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_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_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_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); }