Skip to content
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

Added retry flag to try commit with the last commit #12

Merged
merged 1 commit into from
Dec 21, 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
23 changes: 17 additions & 6 deletions src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ pub fn git_exec(args: &[&str]) -> Result<Output> {
}
}

pub fn commit_as_hook(commit_message: &str) -> Result<()> {
pub fn get_git_path() -> Result<PathBuf> {
let output = git_exec(&["rev-parse", "--absolute-git-dir"])?;
if !output.status.success() {
return Err(anyhow!("Could not get git directory"));
return Err(anyhow!(
"Failed to get git path. Make sure you are in a git repository"
));
}
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(())
let path = String::from_utf8(output.stdout)?;
Ok(PathBuf::from(path.trim()))
}

pub fn commit(commit_message: &str) -> Result<()> {
Expand All @@ -46,3 +46,14 @@ pub fn check_staged_files() -> Result<()> {
}
Ok(())
}

pub fn read_cached_commit() -> Result<String> {
let commit_file_path = get_git_path()?.join("COMMIT_EDITMSG");
let commit_message = fs::read_to_string(commit_file_path)?;
Ok(commit_message)
}

pub fn write_cached_commit(commit_message: &str) -> Result<()> {
fs::write(get_git_path()?.join("COMMIT_EDITMSG"), commit_message)?;
Ok(())
}
14 changes: 12 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use clap::Parser;
use std::io::Write;
use std::path::PathBuf;

use commit::{check_staged_files, commit, commit_as_hook};
use commit::{check_staged_files, commit, read_cached_commit, write_cached_commit};
use commit_message::make_message_commit;

const DEFAULT_CONFIG_FILE: &str = include_str!("../commit-default.json");
Expand All @@ -33,6 +33,9 @@ struct Args {
/// Use as hook
#[arg(long)]
hook: bool,
/// Retry commit with the same message as the last one
#[arg(short, long)]
retry: bool,
}

fn main() -> Result<()> {
Expand All @@ -45,17 +48,24 @@ fn main() -> Result<()> {
check_staged_files()?;

let args = Args::parse();

if args.init {
let mut file = std::fs::File::create("commit.json")?;
file.write_all(DEFAULT_CONFIG_FILE.as_bytes())?;
return Ok(());
}

if args.retry {
let commit_message = read_cached_commit()?;
commit(&commit_message)?;
return Ok(());
}

let pattern = config::get_pattern(args.config)?;
let commit_message = make_message_commit(pattern)?;
write_cached_commit(&commit_message)?;

if args.hook {
commit_as_hook(&commit_message)?;
return Ok(());
}

Expand Down