From 39116a062dffd0c2048a6dea6e40623f45a823db Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 19 Jul 2020 16:58:48 +0800 Subject: [PATCH] refactor: add get_home_e() --- scripts/commit-msg.crs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/scripts/commit-msg.crs b/scripts/commit-msg.crs index 4fc10dc..35271df 100755 --- a/scripts/commit-msg.crs +++ b/scripts/commit-msg.crs @@ -104,20 +104,26 @@ fn search_git_root_path_e() -> PathBuf { search_git_root_path().unwrap_or_else(|| exit_with_error_message("Git root path not found!")) } +fn get_home_e() -> PathBuf { + 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 { - let mut 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() { print_info(&format!("Found file: {:?}", settings_regexp)); return fs::read_to_string(settings_regexp).unwrap(); } - let mut 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() { print_info(&format!("Found file: {:?}", path_regexp)); return fs::read_to_string(path_regexp).unwrap(); } - let home_path_regexp = PathBuf::from(env::var("HOME").unwrap()).join(&(".".to_owned() + file)); + 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(); @@ -135,10 +141,7 @@ fn get_commit_msg_usage() -> String { } fn install_commit_msg(force: bool) { - let home_path = PathBuf::from(env::var("HOME").unwrap_or_else(|e| { - exit_with_error_message(&format!("Get env HOME failed: {}!", e)); - })); - let commit_msg_crs = home_path.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() { print_warn(&format!("File {:?} NOT exists!", commit_msg_crs)); PathBuf::from(env::args().next().unwrap())