From e6526ec2c7d2f80d439b4dcfdc85c83cc82d617f Mon Sep 17 00:00:00 2001 From: atheeq-rhxn Date: Tue, 8 Jul 2025 14:52:48 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Automatically=20stage=20all?= =?UTF-8?q?=20changes=20and=20improve=20error=20handling.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 2 +- src/parser.rs | 4 ---- src/util.rs | 57 +++++++++++++++++++++++++++------------------------ 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 16120b2..b9e2a45 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/src/parser.rs b/src/parser.rs index 48c4743..89a44bd 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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, diff --git a/src/util.rs b/src/util.rs index 02bbcac..b04def8 100644 --- a/src/util.rs +++ b/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"); - run(cli, "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.dry_run { + return; + } - 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); } }