Skip to content

Commit 13c506d

Browse files
authored
feat: add auto-commit functionality with customizable git commit args (#21)
* 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 <staryxchen@tencent.com> * 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 <staryxchen@tencent.com> * 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 <staryxchen@tencent.com> * style(cli): remove trailing comma - Remove trailing comma in `commit_args` attribute definition Signed-off-by: staryxchen <staryxchen@tencent.com> --------- Signed-off-by: staryxchen <staryxchen@tencent.com>
1 parent 51c37e7 commit 13c506d

8 files changed

Lines changed: 86 additions & 4 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fastcommit"
3-
version = "0.5.2"
3+
version = "0.6.0"
44
description = "AI-based command line tool to quickly generate standardized commit messages."
55
edition = "2021"
66
authors = ["longjin <fslongjin@vip.qq.com>"]

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ You can install `fastcommit` using the following method:
1010

1111
```bash
1212
# Install using cargo
13-
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.5.2
13+
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.6.0
1414
```
1515

1616

@@ -38,6 +38,8 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
3838
- `-r, --range <RANGE>`: Specify diff range for generating commit message (e.g. HEAD~1, abc123..def456).
3939
- `--no-wrap`: Disable text wrapping for long lines.
4040
- `--wrap-width <WIDTH>`: Set custom line width for text wrapping (default: config file setting or 80).
41+
- `-c, --commit`: Automatically run `git commit` after generating the message.
42+
- `--commit-args <ARG>`: Extra arguments to pass to `git commit` (can be specified multiple times, e.g. `--commit-args "-s" --commit-args "--no-verify"`).
4143
- `-h, --help`: Print help information.
4244
- `-V, --version`: Print version information.
4345

@@ -102,6 +104,16 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
102104
fastcommit -b -m --wrap-width 100
103105
```
104106

107+
9. Auto-commit after generating the message:
108+
109+
```bash
110+
# Generate and auto-commit
111+
fastcommit -c
112+
113+
# Auto-commit with signoff and skip hooks
114+
fastcommit -c --commit-args "-s" --commit-args "--no-verify"
115+
```
116+
105117
## Contributing
106118

107119
Contributions of code or suggestions are welcome! Please read the [Contributing Guide](CONTRIBUTING.md) first.

README_CN.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
```bash
1010
# 使用 cargo 安装
11-
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.5.1
11+
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.6.0
1212
```
1313

1414
## 使用
@@ -35,6 +35,8 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
3535
- `-r, --range <RANGE>`: 指定差异范围以生成提交信息(例如:HEAD~1, abc123..def456)。
3636
- `--no-wrap`: 禁用长行文本换行。
3737
- `--wrap-width <WIDTH>`: 设置文本换行的自定义行宽度(默认:配置文件设置或 80)。
38+
- `-c, --commit`: 生成提交信息后自动执行 `git commit`
39+
- `--commit-args <ARG>`: 传递给 `git commit` 的额外参数(可多次指定,例如 `--commit-args "-s" --commit-args "--no-verify"`)。
3840
- `-h, --help`: 打印帮助信息。
3941
- `-V, --version`: 打印版本信息。
4042

@@ -99,6 +101,16 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
99101
fastcommit -b -m --wrap-width 100
100102
```
101103

104+
9. 生成提交信息后自动提交:
105+
106+
```bash
107+
# 生成并自动提交
108+
fastcommit -c
109+
110+
# 自动提交并签名、跳过 hook
111+
fastcommit -c --commit-args "-s" --commit-args "--no-verify"
112+
```
113+
102114
## 贡献
103115

104116
欢迎贡献代码或提出建议!请先阅读 [贡献指南](CONTRIBUTING.md)

src/cli.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,19 @@ pub struct Args {
6868
help = "Set custom line width for text wrapping (default: terminal width)"
6969
)]
7070
pub wrap_width: Option<usize>,
71+
72+
#[clap(
73+
short = 'c',
74+
long = "commit",
75+
help = "Automatically run git commit after generating the message"
76+
)]
77+
pub commit: bool,
78+
79+
#[clap(
80+
long = "commit-args",
81+
help = "Extra arguments to pass to git commit (can be specified multiple times)",
82+
num_args = 1,
83+
allow_hyphen_values = true
84+
)]
85+
pub commit_args: Vec<String>,
7186
}

src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ pub struct Config {
8484
/// Text wrapping configuration
8585
#[serde(default)]
8686
pub text_wrap: TextWrapConfig,
87+
/// Automatically run git commit after generating the message
88+
#[serde(default)]
89+
pub auto_commit: bool,
90+
/// Extra arguments to pass to git commit
91+
#[serde(default)]
92+
pub commit_args: Vec<String>,
8793
}
8894

8995
impl Config {
@@ -175,6 +181,8 @@ impl Default for Config {
175181
sanitize_secrets: true,
176182
custom_sanitize_patterns: Vec::new(),
177183
text_wrap: TextWrapConfig::default(),
184+
auto_commit: false,
185+
commit_args: Vec::new(),
178186
}
179187
}
180188
}

src/generate.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,24 @@ pub async fn generate_both(args: &cli::Args, config: &Config) -> anyhow::Result<
265265
let commit_message = generate_commit_message(&diff, config, args.prompt.as_deref()).await?;
266266
Ok((branch_name, commit_message))
267267
}
268+
269+
/// 执行 git commit,将生成的 message 作为 commit message
270+
pub fn execute_git_commit(message: &str, extra_args: &[String]) -> anyhow::Result<()> {
271+
let mut cmd = Command::new("git");
272+
cmd.args(["commit", "-m", message]);
273+
for arg in extra_args {
274+
cmd.arg(arg);
275+
}
276+
let output = cmd.output()?;
277+
if output.status.success() {
278+
let stdout = String::from_utf8_lossy(&output.stdout);
279+
if !stdout.trim().is_empty() {
280+
eprintln!("{}", stdout.trim());
281+
}
282+
eprintln!("\x1b[32mSuccessfully committed!\x1b[0m");
283+
Ok(())
284+
} else {
285+
let stderr = String::from_utf8_lossy(&output.stderr);
286+
Err(anyhow::anyhow!("git commit failed:\n{}", stderr.trim()))
287+
}
288+
}

src/main.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ async fn main() -> anyhow::Result<()> {
3838
config.sanitize_secrets = false;
3939
}
4040

41+
// 合并 auto_commit 参数
42+
let auto_commit = args.commit || config.auto_commit;
43+
let commit_args = if args.commit_args.is_empty() {
44+
&config.commit_args
45+
} else {
46+
&args.commit_args
47+
};
48+
4149
// 确定是否启用文本包装 (CLI 参数优先级高于配置)
4250
let enable_wrapping = !args.no_wrap && config.text_wrap.enabled;
4351

@@ -68,6 +76,9 @@ async fn main() -> anyhow::Result<()> {
6876
spinner.finish();
6977
print_wrapped_content(&wrapper, &branch_name, Some("Generated branch name:"));
7078
print_wrapped_content(&commit_wrapper, &msg, None);
79+
if auto_commit {
80+
generate::execute_git_commit(&msg, commit_args)?;
81+
}
7182
} else if args.generate_branch {
7283
// 仅生成分支名
7384
let branch_name = generate::generate_branch(&args, &config).await?;
@@ -78,6 +89,9 @@ async fn main() -> anyhow::Result<()> {
7889
let msg = generate::generate(&args, &config).await?;
7990
spinner.finish();
8091
print_wrapped_content(&commit_wrapper, &msg, None);
92+
if auto_commit {
93+
generate::execute_git_commit(&msg, commit_args)?;
94+
}
8195
}
8296
Ok(())
8397
}

0 commit comments

Comments
 (0)