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::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 <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() {
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}<type>{e}(<scope>){b}: <subject>{e}
<scope> is optional
@@ -94,9 +95,7 @@ fn search_git_root_path() -> Option<PathBuf> {
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<String> {
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<String> {
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");