Skip to content

Flag to use the program as a hook #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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. <[email protected]>"]
readme = "README.md"
license = "MIT"
Expand Down
55 changes: 55 additions & 0 deletions src/commit.rs
Original file line number Diff line number Diff line change
@@ -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
));
}
};
}
38 changes: 15 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,6 +33,8 @@ struct Opt {
config: Option<PathBuf>,
#[clap(long, help = "Init custom configuration file")]
init: bool,
#[clap(short, long, help = "Use as hook")]
hook: bool,
}

fn main() -> Result<()> {
Expand All @@ -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(())
}