Skip to content

Commit e3a5932

Browse files
authored
feat: 新增支持通过-r参数指定差异范围生成提交信息 (#9)
- 在CLI参数中添加--range/-r选项 - 修改get_diff函数支持处理指定范围的git diff - 更新README文档说明新功能用法 Signed-off-by: longjin <longjin@DragonOS.org>
1 parent 9928002 commit e3a5932

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
3333
- `--branch-prefix`: prefix of the generated branch name
3434
- `-v, --verbosity <VERBOSITY>`: Set the detail level of the commit message. Acceptable values are `verbose` (detailed), `normal`, or `quiet` (concise). The default is `quiet`.
3535
- `-p, --prompt <PROMPT>`: Additional prompt to help AI understand the commit context.
36+
- `-r, --range <RANGE>`: Specify diff range for generating commit message (e.g. HEAD~1, abc123..def456).
3637
- `-h, --help`: Print help information.
3738
- `-V, --version`: Print version information.
3839

@@ -62,6 +63,16 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
6263
fastcommit -d changes.diff -p "Fixed login page styling issues, especially button alignment"
6364
```
6465

66+
5. Generate commit message for a specific diff range:
67+
68+
```bash
69+
# Generate commit message for the last commit
70+
fastcommit -r HEAD~1
71+
72+
# Generate commit message for a range of commits
73+
fastcommit -r abc123..def456
74+
```
75+
6576
## Contributing
6677

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

README_CN.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
3131
- `--branch-prefix`: 生成的分支名的前缀
3232
- `-v, --verbosity <VERBOSITY>`: 设置提交信息的详细级别。可选值为 `verbose`(详细)、`normal`(正常)或 `quiet`(简洁)。 默认为 `quiet`
3333
- `-p, --prompt <PROMPT>`: 额外的提示信息,帮助 AI 理解提交上下文。
34+
- `-r, --range <RANGE>`: 指定差异范围以生成提交信息(例如:HEAD~1, abc123..def456)。
3435
- `-h, --help`: 打印帮助信息。
3536
- `-V, --version`: 打印版本信息。
3637

@@ -60,6 +61,16 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
6061
fastcommit -d changes.diff -p "修复了登录页面的样式问题,特别是按钮对齐"
6162
```
6263

64+
5. 为特定的差异范围生成提交信息:
65+
66+
```bash
67+
# 为最近一次提交生成提交信息
68+
fastcommit -r HEAD~1
69+
70+
# 为指定提交范围生成提交信息
71+
fastcommit -r abc123..def456
72+
```
73+
6374
## 贡献
6475

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

src/cli.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ pub struct Args {
2929

3030
#[clap(short, long, help = "Additional prompt to help AI understand the commit context")]
3131
pub prompt: Option<String>,
32+
33+
#[clap(
34+
short = 'r',
35+
long,
36+
help = "Specify diff range (e.g. HEAD~1, abc123..def456)"
37+
)]
38+
pub range: Option<String>,
3239
}

src/generate.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,20 @@ fn extract_aicommit_message(response: &str) -> anyhow::Result<String> {
124124
Ok(commit_message)
125125
}
126126

127-
fn get_diff(diff_file: Option<&str>) -> anyhow::Result<String> {
127+
fn get_diff(diff_file: Option<&str>, range: Option<&str>) -> anyhow::Result<String> {
128128
match diff_file {
129129
Some(path) => std::fs::read_to_string(path).map_err(Into::into),
130130
None => {
131-
let output = Command::new("git").arg("diff").arg("--cached").output()?;
131+
let mut cmd = Command::new("git");
132+
cmd.arg("diff");
133+
134+
if let Some(range_str) = range {
135+
cmd.arg(range_str);
136+
} else {
137+
cmd.arg("--cached");
138+
}
139+
140+
let output = cmd.output()?;
132141
let diff_str = String::from_utf8_lossy(&output.stdout).into_owned();
133142
if diff_str.trim().is_empty() {
134143
Err(anyhow::anyhow!("No changes to commit"))
@@ -140,7 +149,7 @@ fn get_diff(diff_file: Option<&str>) -> anyhow::Result<String> {
140149
}
141150

142151
pub async fn generate(args: &cli::Args, config: &Config) -> anyhow::Result<String> {
143-
let diff = get_diff(args.diff_file.as_deref())?;
152+
let diff = get_diff(args.diff_file.as_deref(), args.range.as_deref())?;
144153
let message = generate_commit_message(&diff, config, args.prompt.as_deref()).await?;
145154
Ok(message)
146155
}
@@ -216,7 +225,7 @@ async fn generate_branch_name_with_ai(
216225
}
217226

218227
pub async fn generate_branch(args: &cli::Args, config: &Config) -> anyhow::Result<String> {
219-
let diff = get_diff(args.diff_file.as_deref())?;
228+
let diff = get_diff(args.diff_file.as_deref(), args.range.as_deref())?;
220229
let prefix = args
221230
.branch_prefix
222231
.as_deref()

0 commit comments

Comments
 (0)