mirror of
https://github.com/rzmk/commit-helper.git
synced 2025-12-19 05:29:24 +00:00
✨ feat: Automatically stage all changes and improve error handling.
This commit is contained in:
parent
fca5c0f5bc
commit
e6526ec2c7
3 changed files with 31 additions and 32 deletions
|
|
@ -10,4 +10,4 @@ path = "src/main.rs"
|
|||
[dependencies]
|
||||
arboard = "3.4.1"
|
||||
clap = { version = "4.4.12", features = ["derive"] }
|
||||
inquire = { version = "0.6.2" }
|
||||
inquire = { version = "0.7.5" }
|
||||
|
|
|
|||
|
|
@ -7,10 +7,6 @@ use clap::Parser;
|
|||
about = "Run multiple commands related to `git commit` in succession"
|
||||
)]
|
||||
pub struct Cli {
|
||||
/// Run `git add -A` before committing
|
||||
#[arg(short = 'a', long = "add")]
|
||||
pub add: bool,
|
||||
|
||||
/// Copy commit message to clipboard instead of committing
|
||||
#[arg(short = 'c', long = "clipboard")]
|
||||
pub clipboard: bool,
|
||||
|
|
|
|||
55
src/util.rs
55
src/util.rs
|
|
@ -1,54 +1,57 @@
|
|||
use super::parser::Cli;
|
||||
use arboard::Clipboard;
|
||||
use std::process::Command;
|
||||
use std::process::{Command, exit};
|
||||
|
||||
// Run the commands in succession
|
||||
pub fn orchestrate_commit(cli: &Cli, message: &str) {
|
||||
if cli.add {
|
||||
println!("Running git add -A");
|
||||
// Automatically run git add -A
|
||||
println!("➡️ Running git add -A");
|
||||
run(cli, "git", &["add", "-A"]);
|
||||
}
|
||||
|
||||
if cli.clipboard {
|
||||
println!("Copying commit message to clipboard");
|
||||
println!("📋 Copying commit message to clipboard");
|
||||
if !cli.dry_run {
|
||||
let mut clipboard = Clipboard::new().unwrap();
|
||||
clipboard.set_text(message).unwrap();
|
||||
}
|
||||
} else {
|
||||
println!("Running git commit -m \"{}\"", message);
|
||||
let args = if cli.sign {
|
||||
vec!["commit", "-m", message]
|
||||
} else {
|
||||
vec!["commit", "-S", "-m", message]
|
||||
} else {
|
||||
vec!["commit", "-m", message]
|
||||
};
|
||||
|
||||
let command_string = format!("git {}", args.join(" "));
|
||||
println!("➡️ Running {}", command_string);
|
||||
run(cli, "git", &args);
|
||||
|
||||
if cli.push {
|
||||
println!("Running git push");
|
||||
println!("➡️ Running git push");
|
||||
run(cli, "git", &["push"]);
|
||||
}
|
||||
}
|
||||
|
||||
println!("Done 🎉");
|
||||
}
|
||||
|
||||
pub fn print_debug_info(output: std::process::Output) {
|
||||
println!("Debug info:");
|
||||
println!("stdout:\n{}", String::from_utf8_lossy(&output.stdout));
|
||||
println!("stderr:\n{}", String::from_utf8_lossy(&output.stderr));
|
||||
println!("Exit status:\n{}", output.status);
|
||||
println!("\n✅ Done!");
|
||||
}
|
||||
|
||||
pub fn run(cli: &Cli, command: &str, args: &[&str]) {
|
||||
if !cli.dry_run {
|
||||
let output = Command::new(command)
|
||||
.args(args)
|
||||
.output()
|
||||
.expect("failed to execute process");
|
||||
|
||||
if cli.debug {
|
||||
print_debug_info(output);
|
||||
if cli.dry_run {
|
||||
return;
|
||||
}
|
||||
|
||||
let status = Command::new(command)
|
||||
.args(args)
|
||||
.status()
|
||||
.expect("❌ Failed to execute command. Is git installed and in your PATH?");
|
||||
|
||||
if !status.success() {
|
||||
let full_command = format!("`{} {}`", command, args.join(" "));
|
||||
|
||||
eprintln!(
|
||||
"\n❌ Error: Command {} failed with exit code {}. Halting execution.",
|
||||
full_command,
|
||||
status.code().unwrap_or(1)
|
||||
);
|
||||
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue