diff --git a/Cargo.toml b/Cargo.toml index fb223d3..989f733 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,8 @@ homepage = "https://github.com/alt-art/commit" repository = "https://github.com/alt-art/commit" documentation = "https://github.com/alt-art/commit/wiki" description = "A tool to make patterned (conventional) commit messages" +categories = ["development-tools", "command-line-utilities"] +keywords = ["git", "commit","message", "conventional"] authors = ["Pedro H. M. "] readme = "README.md" license = "MIT" diff --git a/src/commit.rs b/src/commit.rs new file mode 100644 index 0000000..c1a0cbd --- /dev/null +++ b/src/commit.rs @@ -0,0 +1,55 @@ +use anyhow::{anyhow, Result}; + +use std::fs; +use std::io::Write; +use std::path::PathBuf; +use std::process::{exit, Command}; + +pub fn commit_as_hook(commit_message: &str) -> Result<()> { + let output = Command::new("git") + .args(&["rev-parse", "--absolute-git-dir"]) + .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(()) +} + +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(anyhow!("Could not get exit code"))?, + ); + } + Err(e) => { + return Err(anyhow::anyhow!( + "Failed to run git. Make sure git is installed\nAdditional info: {}", + e + )); + } + }; +} diff --git a/src/main.rs b/src/main.rs index 0948afd..7e36b95 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,14 +7,15 @@ )] #![allow(clippy::module_name_repetitions)] +mod commit; mod commit_message; mod config; use anyhow::Result; use clap::Parser; use std::io::Write; + use std::path::PathBuf; -use std::process::{exit, Command}; use commit_message::make_message_commit; @@ -32,6 +33,8 @@ struct Opt { config: Option, #[clap(long, help = "Init custom configuration file")] init: bool, + #[clap(short, long, help = "Use as hook")] + hook: bool, } fn main() -> Result<()> { @@ -45,28 +48,17 @@ fn main() -> Result<()> { if opt.init { let mut file = std::fs::File::create("commit.json")?; file.write_all(DEFAULT_CONFIG_FILE.as_bytes())?; - Ok(()) - } else { - let pattern = config::get_pattern(opt.config)?; - let commit_message = make_message_commit(pattern)?; + return Ok(()); + } - let output = Command::new("git") - .arg("commit") - .arg("-m") - .arg(commit_message) - .output(); - match output { - Ok(output) => { - std::io::stdout().write_all(&output.stdout).unwrap(); - std::io::stderr().write_all(&output.stderr).unwrap(); - exit(output.status.code().unwrap()); - } - Err(e) => { - return Err(anyhow::anyhow!( - "Failed to run git. Make sure git is installed\nAdditional info: {}", - e - )); - } - }; + let pattern = config::get_pattern(opt.config)?; + let commit_message = make_message_commit(pattern)?; + + if opt.hook { + commit::commit_as_hook(&commit_message)?; + return Ok(()); } + + commit::commit(&commit_message)?; + Ok(()) }