From c9593a5f6ede5c0b1b57072235e73b89f7f9f87d Mon Sep 17 00:00:00 2001 From: staryxchen Date: Wed, 18 Feb 2026 11:08:36 +0800 Subject: [PATCH 1/4] feat: add auto-commit functionality - Add `--commit` and `--commit-args` CLI options - Add `auto_commit` and `commit_args` config fields - Execute `git commit` automatically after message generation Signed-off-by: staryxchen --- src/cli.rs | 15 +++++++++++++++ src/config.rs | 8 ++++++++ src/generate.rs | 21 +++++++++++++++++++++ src/main.rs | 14 ++++++++++++++ 4 files changed, 58 insertions(+) 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(()) } From eab863b011b367d4ef61830927b56a0cea264e03 Mon Sep 17 00:00:00 2001 From: staryxchen Date: Tue, 24 Feb 2026 11:09:35 +0800 Subject: [PATCH 2/4] chore(release): bump version to 0.6.0 - Update version in Cargo.toml and Cargo.lock - Update installation version in README files Signed-off-by: staryxchen --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 2 +- README_CN.md | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0c49e24..3ad6f6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -359,7 +359,7 @@ dependencies = [ [[package]] name = "fastcommit" -version = "0.5.2" +version = "0.6.0" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index c7d1806..72af0c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fastcommit" -version = "0.5.2" +version = "0.6.0" description = "AI-based command line tool to quickly generate standardized commit messages." edition = "2021" authors = ["longjin "] diff --git a/README.md b/README.md index ab6f5bd..265d451 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ You can install `fastcommit` using the following method: ```bash # Install using cargo -cargo install --git https://github.com/fslongjin/fastcommit --tag v0.5.2 +cargo install --git https://github.com/fslongjin/fastcommit --tag v0.6.0 ``` diff --git a/README_CN.md b/README_CN.md index c3473c0..5c1d4fb 100644 --- a/README_CN.md +++ b/README_CN.md @@ -8,7 +8,7 @@ ```bash # 使用 cargo 安装 -cargo install --git https://github.com/fslongjin/fastcommit --tag v0.5.1 +cargo install --git https://github.com/fslongjin/fastcommit --tag v0.6.0 ``` ## 使用 From e28fa18dca80b171b40922e8c1be91e43c91a60d Mon Sep 17 00:00:00 2001 From: staryxchen Date: Tue, 24 Feb 2026 11:17:41 +0800 Subject: [PATCH 3/4] docs: document auto-commit feature - Add documentation for `-c, --commit` and `--commit-args` options - Add usage examples for auto-commit functionality Signed-off-by: staryxchen --- README.md | 12 ++++++++++++ README_CN.md | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/README.md b/README.md index 265d451..78cee2d 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,8 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml` - `-r, --range `: Specify diff range for generating commit message (e.g. HEAD~1, abc123..def456). - `--no-wrap`: Disable text wrapping for long lines. - `--wrap-width `: Set custom line width for text wrapping (default: config file setting or 80). +- `-c, --commit`: Automatically run `git commit` after generating the message. +- `--commit-args `: Extra arguments to pass to `git commit` (can be specified multiple times, e.g. `--commit-args "-s" --commit-args "--no-verify"`). - `-h, --help`: Print help information. - `-V, --version`: Print version information. @@ -102,6 +104,16 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml` fastcommit -b -m --wrap-width 100 ``` +9. Auto-commit after generating the message: + + ```bash + # Generate and auto-commit + fastcommit -c + + # Auto-commit with signoff and skip hooks + fastcommit -c --commit-args "-s" --commit-args "--no-verify" + ``` + ## Contributing Contributions of code or suggestions are welcome! Please read the [Contributing Guide](CONTRIBUTING.md) first. diff --git a/README_CN.md b/README_CN.md index 5c1d4fb..6a560ea 100644 --- a/README_CN.md +++ b/README_CN.md @@ -35,6 +35,8 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml` - `-r, --range `: 指定差异范围以生成提交信息(例如:HEAD~1, abc123..def456)。 - `--no-wrap`: 禁用长行文本换行。 - `--wrap-width `: 设置文本换行的自定义行宽度(默认:配置文件设置或 80)。 +- `-c, --commit`: 生成提交信息后自动执行 `git commit`。 +- `--commit-args `: 传递给 `git commit` 的额外参数(可多次指定,例如 `--commit-args "-s" --commit-args "--no-verify"`)。 - `-h, --help`: 打印帮助信息。 - `-V, --version`: 打印版本信息。 @@ -99,6 +101,16 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml` fastcommit -b -m --wrap-width 100 ``` +9. 生成提交信息后自动提交: + + ```bash + # 生成并自动提交 + fastcommit -c + + # 自动提交并签名、跳过 hook + fastcommit -c --commit-args "-s" --commit-args "--no-verify" + ``` + ## 贡献 欢迎贡献代码或提出建议!请先阅读 [贡献指南](CONTRIBUTING.md)。 From 31bfa6d6dc9538d3871badb01e3ba772711b6713 Mon Sep 17 00:00:00 2001 From: staryxchen Date: Tue, 24 Feb 2026 11:19:16 +0800 Subject: [PATCH 4/4] style(cli): remove trailing comma - Remove trailing comma in `commit_args` attribute definition Signed-off-by: staryxchen --- src/cli.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli.rs b/src/cli.rs index 9076630..c841a76 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -80,7 +80,7 @@ pub struct Args { long = "commit-args", help = "Extra arguments to pass to git commit (can be specified multiple times)", num_args = 1, - allow_hyphen_values = true, + allow_hyphen_values = true )] pub commit_args: Vec, }