diff --git a/src/cli.rs b/src/cli.rs index 8af73cb..9076630 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -68,4 +68,19 @@ pub struct Args { help = "Set custom line width for text wrapping (default: terminal width)" )] pub wrap_width: Option, + + #[clap( + short = 'c', + long = "commit", + help = "Automatically run git commit after generating the message" + )] + pub commit: bool, + + #[clap( + long = "commit-args", + help = "Extra arguments to pass to git commit (can be specified multiple times)", + num_args = 1, + allow_hyphen_values = true, + )] + pub commit_args: Vec, } diff --git a/src/config.rs b/src/config.rs index c3e3b5a..116a055 100644 --- a/src/config.rs +++ b/src/config.rs @@ -84,6 +84,12 @@ pub struct Config { /// Text wrapping configuration #[serde(default)] pub text_wrap: TextWrapConfig, + /// Automatically run git commit after generating the message + #[serde(default)] + pub auto_commit: bool, + /// Extra arguments to pass to git commit + #[serde(default)] + pub commit_args: Vec, } impl Config { @@ -175,6 +181,8 @@ impl Default for Config { sanitize_secrets: true, custom_sanitize_patterns: Vec::new(), text_wrap: TextWrapConfig::default(), + auto_commit: false, + commit_args: Vec::new(), } } } diff --git a/src/generate.rs b/src/generate.rs index 8c1758e..85ea5a7 100644 --- a/src/generate.rs +++ b/src/generate.rs @@ -265,3 +265,24 @@ pub async fn generate_both(args: &cli::Args, config: &Config) -> anyhow::Result< let commit_message = generate_commit_message(&diff, config, args.prompt.as_deref()).await?; Ok((branch_name, commit_message)) } + +/// 执行 git commit,将生成的 message 作为 commit message +pub fn execute_git_commit(message: &str, extra_args: &[String]) -> anyhow::Result<()> { + let mut cmd = Command::new("git"); + cmd.args(["commit", "-m", message]); + for arg in extra_args { + cmd.arg(arg); + } + let output = cmd.output()?; + if output.status.success() { + let stdout = String::from_utf8_lossy(&output.stdout); + if !stdout.trim().is_empty() { + eprintln!("{}", stdout.trim()); + } + eprintln!("\x1b[32mSuccessfully committed!\x1b[0m"); + Ok(()) + } else { + let stderr = String::from_utf8_lossy(&output.stderr); + Err(anyhow::anyhow!("git commit failed:\n{}", stderr.trim())) + } +} diff --git a/src/main.rs b/src/main.rs index 0e8060c..b612515 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,6 +38,14 @@ async fn main() -> anyhow::Result<()> { config.sanitize_secrets = false; } + // 合并 auto_commit 参数 + let auto_commit = args.commit || config.auto_commit; + let commit_args = if args.commit_args.is_empty() { + &config.commit_args + } else { + &args.commit_args + }; + // 确定是否启用文本包装 (CLI 参数优先级高于配置) let enable_wrapping = !args.no_wrap && config.text_wrap.enabled; @@ -68,6 +76,9 @@ async fn main() -> anyhow::Result<()> { spinner.finish(); print_wrapped_content(&wrapper, &branch_name, Some("Generated branch name:")); print_wrapped_content(&commit_wrapper, &msg, None); + if auto_commit { + generate::execute_git_commit(&msg, commit_args)?; + } } else if args.generate_branch { // 仅生成分支名 let branch_name = generate::generate_branch(&args, &config).await?; @@ -78,6 +89,9 @@ async fn main() -> anyhow::Result<()> { let msg = generate::generate(&args, &config).await?; spinner.finish(); print_wrapped_content(&commit_wrapper, &msg, None); + if auto_commit { + generate::execute_git_commit(&msg, commit_args)?; + } } Ok(()) }