feat: Automatically stage all changes and improve error handling.

This commit is contained in:
atheeq-rhxn 2025-07-08 14:52:48 +05:30
parent fca5c0f5bc
commit e6526ec2c7
3 changed files with 31 additions and 32 deletions

View file

@ -10,4 +10,4 @@ path = "src/main.rs"
[dependencies] [dependencies]
arboard = "3.4.1" arboard = "3.4.1"
clap = { version = "4.4.12", features = ["derive"] } clap = { version = "4.4.12", features = ["derive"] }
inquire = { version = "0.6.2" } inquire = { version = "0.7.5" }

View file

@ -7,10 +7,6 @@ use clap::Parser;
about = "Run multiple commands related to `git commit` in succession" about = "Run multiple commands related to `git commit` in succession"
)] )]
pub struct Cli { 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 /// Copy commit message to clipboard instead of committing
#[arg(short = 'c', long = "clipboard")] #[arg(short = 'c', long = "clipboard")]
pub clipboard: bool, pub clipboard: bool,

View file

@ -1,54 +1,57 @@
use super::parser::Cli; use super::parser::Cli;
use arboard::Clipboard; 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) { pub fn orchestrate_commit(cli: &Cli, message: &str) {
if cli.add { // Automatically run git add -A
println!("Running git add -A"); println!("➡️ Running git add -A");
run(cli, "git", &["add", "-A"]); run(cli, "git", &["add", "-A"]);
}
if cli.clipboard { if cli.clipboard {
println!("Copying commit message to clipboard"); println!("📋 Copying commit message to clipboard");
if !cli.dry_run { if !cli.dry_run {
let mut clipboard = Clipboard::new().unwrap(); let mut clipboard = Clipboard::new().unwrap();
clipboard.set_text(message).unwrap(); clipboard.set_text(message).unwrap();
} }
} else { } else {
println!("Running git commit -m \"{}\"", message);
let args = if cli.sign { let args = if cli.sign {
vec!["commit", "-m", message]
} else {
vec!["commit", "-S", "-m", message] 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); run(cli, "git", &args);
if cli.push { if cli.push {
println!("Running git push"); println!("➡️ Running git push");
run(cli, "git", &["push"]); run(cli, "git", &["push"]);
} }
} }
println!("Done 🎉"); println!("\n✅ 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);
} }
pub fn run(cli: &Cli, command: &str, args: &[&str]) { pub fn run(cli: &Cli, command: &str, args: &[&str]) {
if !cli.dry_run { if cli.dry_run {
let output = Command::new(command) return;
.args(args)
.output()
.expect("failed to execute process");
if cli.debug {
print_debug_info(output);
} }
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);
} }
} }