diff --git a/src/commit.rs b/src/commit.rs index d057c15..d7decb4 100644 --- a/src/commit.rs +++ b/src/commit.rs @@ -1,9 +1,9 @@ use anyhow::{anyhow, Result}; -use std::fs; -use std::io::Write; +use std::io::{BufRead, BufReader, Write}; use std::path::PathBuf; -use std::process::{exit, Command, Output}; +use std::process::{exit, Command, Output, Stdio}; +use std::{fs, thread}; pub fn git_exec(args: &[&str]) -> Result { let output = Command::new("git").args(args).output(); @@ -61,16 +61,28 @@ pub fn write_cached_commit(commit_message: &str) -> Result<()> { pub fn pre_commit_check(pre_commit_command: Option, message: &str) -> Result<()> { if let Some(command) = pre_commit_command { println!("Running pre-commit command..."); - let output = Command::new(command).env("MSG", message).output()?; - let message = String::from_utf8_lossy(&output.stdout); - println!("{message}"); - if !output.status.success() { - let error_message = String::from_utf8_lossy(&output.stderr); - return Err(anyhow!( - "Pre-commit command failed with code: {}\n{}", - output.status.code().unwrap_or_default(), - error_message - )); + let mut process = Command::new(command) + .env("MSG", message) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn()?; + let stdout = process.stdout.take().expect("Unable to get stdout"); + let stderr = process.stderr.take().expect("Unable to get stderr"); + thread::spawn(move || { + let lines = BufReader::new(stdout).lines(); + for line in lines { + println!("{}", line.expect("Unable to get line")); + } + }); + thread::spawn(move || { + let lines = BufReader::new(stderr).lines(); + for line in lines { + eprintln!("{}", line.expect("Unable to get line")); + } + }); + let status = process.wait()?; + if !status.success() { + return Err(anyhow!("Pre-commit command failed with {}", status)); } } Ok(()) diff --git a/src/config/tests.rs b/src/config/tests.rs index a317210..85cf87f 100644 --- a/src/config/tests.rs +++ b/src/config/tests.rs @@ -1,4 +1,4 @@ -use std::env::{current_dir, set_current_dir}; +use std::env::set_current_dir; use super::*; use assert_fs::prelude::*; @@ -13,7 +13,6 @@ fn select_custom_config_path_test() -> Result<()> { let selected_config_path = select_custom_config_path(config_path.clone())?; assert_eq!(config_path.unwrap().to_str(), selected_config_path.to_str()); - let last_dir = current_dir()?; set_current_dir(temp_dir.path())?; let config_path_default = dirs::config_dir().unwrap().join("commit/commit.json"); let selected_config_path = select_custom_config_path(None)?; @@ -24,23 +23,18 @@ fn select_custom_config_path_test() -> Result<()> { Err(err) => assert_eq!(err.to_string(), "Config file does not exist: "), _ => unreachable!(), } - set_current_dir(last_dir)?; - temp_dir.close()?; Ok(()) } #[test] fn get_config_path_test() -> Result<()> { let temp_dir = assert_fs::TempDir::new()?; - let last_dir = current_dir()?; set_current_dir(temp_dir.path())?; let config_file = dirs::config_dir() .ok_or_else(|| anyhow!("Could not find config directory"))? .join("commit/commit.json"); let config_path = get_config_path(); assert_eq!(config_file.to_str(), config_path?.to_str()); - set_current_dir(last_dir)?; - temp_dir.close()?; Ok(()) } @@ -57,14 +51,12 @@ fn get_config_path_content_test() -> Result<()> { config_file.write_str(expected)?; let content = get_config_path_content(config_path)?; assert_eq!(content, expected); - temp_dir.close()?; Ok(()) } #[test] fn get_pattern_test() -> Result<()> { let temp_dir = assert_fs::TempDir::new()?; - let last_dir = current_dir()?; set_current_dir(temp_dir.path())?; let pattern = get_pattern(None)?; assert_eq!(pattern.config.type_prefix, None); @@ -72,7 +64,5 @@ fn get_pattern_test() -> Result<()> { assert_eq!(pattern.config.subject_separator, ": "); assert_eq!(pattern.config.scope_prefix, "("); assert_eq!(pattern.config.scope_suffix, ")"); - set_current_dir(last_dir)?; - temp_dir.close()?; Ok(()) }