diff --git a/Cargo.lock b/Cargo.lock index fc90d0e..d0d2dc7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -99,17 +99,6 @@ dependencies = [ "syn", ] -[[package]] -name = "colored" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" -dependencies = [ - "atty", - "lazy_static", - "winapi", -] - [[package]] name = "commit" version = "0.4.0" @@ -117,7 +106,6 @@ dependencies = [ "anyhow", "assert_fs", "clap", - "colored", "dirs", "inquire", "serde", diff --git a/Cargo.toml b/Cargo.toml index f13bf39..efcb9c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,6 @@ serde_json = "1.0.79" clap = { version = "3.1.6", features = ["derive"] } dirs = "4.0.0" anyhow = "1.0.56" -colored = "2.0.0" [dev-dependencies] assert_fs = "1.0.7" diff --git a/src/commit.rs b/src/commit.rs index c68d9e1..c5caf63 100644 --- a/src/commit.rs +++ b/src/commit.rs @@ -3,53 +3,46 @@ use anyhow::{anyhow, Result}; use std::fs; use std::io::Write; use std::path::PathBuf; -use std::process::{exit, Command}; +use std::process::{exit, Command, Output}; -pub fn commit_as_hook(commit_message: &str) -> Result<()> { - let output = Command::new("git") - .args(&["rev-parse", "--absolute-git-dir"]) - .output(); +pub fn git_exec(args: &[&str]) -> Result { + let output = Command::new("git").args(args).output(); match output { - Ok(output) => { - if !output.status.success() { - return Err(anyhow!("Could not get git directory")); - } - let git_dir = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim()); - let commit_file_path = git_dir.join("COMMIT_EDITMSG"); - fs::write(commit_file_path, commit_message)?; - } - Err(e) => { - return Err(anyhow!( - "Failed to run git. Make sure git is installed\nAdditional info: {}", - e - )); - } + Ok(output) => Ok(output), + Err(e) => Err(anyhow!( + "Failed to run git. Make sure git is installed\nAdditional info: {}", + e + )), + } +} + +pub fn commit_as_hook(commit_message: &str) -> Result<()> { + let output = git_exec(&["rev-parse", "--absolute-git-dir"])?; + if !output.status.success() { + return Err(anyhow!("Could not get git directory")); } + let git_dir = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim()); + let commit_file_path = git_dir.join("COMMIT_EDITMSG"); + fs::write(commit_file_path, commit_message)?; Ok(()) } pub fn commit(commit_message: &str) -> Result<()> { - let output = Command::new("git") - .arg("commit") - .arg("-m") - .arg(commit_message) - .output(); - match output { - Ok(output) => { - std::io::stdout().write_all(&output.stdout)?; - std::io::stderr().write_all(&output.stderr)?; - exit( - output - .status - .code() - .ok_or_else(|| anyhow!("Could not get exit code"))?, - ); - } - Err(e) => { - return Err(anyhow::anyhow!( - "Failed to run git. Make sure git is installed\nAdditional info: {}", - e - )); - } - }; + let output = git_exec(&["commit", "-m", commit_message])?; + std::io::stdout().write_all(&output.stdout)?; + std::io::stderr().write_all(&output.stderr)?; + exit( + output + .status + .code() + .ok_or_else(|| anyhow!("Signal terminated"))?, + ); +} + +pub fn check_staged_files() -> Result<()> { + let output = git_exec(&["diff", "--cached", "--quiet"])?; + if output.status.code() == Some(0) { + return Err(anyhow!("You have not added anything please do `git add`")); + } + Ok(()) } diff --git a/src/main.rs b/src/main.rs index f5466ff..3ceda6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,14 +11,12 @@ mod commit; mod commit_message; mod config; -use anyhow::anyhow; use anyhow::Result; use clap::Parser; -use colored::Colorize; use std::io::Write; use std::path::PathBuf; -use std::process::Command; +use commit::{check_staged_files, commit, commit_as_hook}; use commit_message::make_message_commit; const DEFAULT_CONFIG_FILE: &str = include_str!("../commit-default.json"); @@ -46,18 +44,7 @@ fn main() -> Result<()> { std::env::set_current_dir(current_dir)?; } - if Command::new("git") - .args(["diff", "--cached", "--quiet"]) - .output() - .expect("failed to execute process") - .status - .code() - == Some(0) - { - return Err(anyhow!( - "You have not added anything please do `git add`".red() - )); - } + check_staged_files()?; let opt = Opt::parse(); if opt.init { @@ -70,10 +57,10 @@ fn main() -> Result<()> { let commit_message = make_message_commit(pattern)?; if opt.hook { - commit::commit_as_hook(&commit_message)?; + commit_as_hook(&commit_message)?; return Ok(()); } - commit::commit(&commit_message)?; + commit(&commit_message)?; Ok(()) }