Skip to content

Commit 3f182d3

Browse files
authored
feat: add -p/--prompt option to provide additional commit context (#6)
Add -p/--prompt CLI option to allow users to provide additional context for AI-generated commit messages. This helps improve the quality of generated messages by giving the AI more context about the changes. Signed-off-by: staryxchen <[email protected]>
1 parent 342897d commit 3f182d3

File tree

6 files changed

+41
-4
lines changed

6 files changed

+41
-4
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
3232
- `-gb, --generate-branch`: Generate branch name.
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`.
35+
- `-p, --prompt <PROMPT>`: Additional prompt to help AI understand the commit context.
3536
- `-h, --help`: Print help information.
3637
- `-V, --version`: Print version information.
3738

@@ -55,6 +56,12 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
5556
fastcommit -d changes.diff -v verbose
5657
```
5758

59+
4. Provide additional context to help AI understand the commit:
60+
61+
```bash
62+
fastcommit -d changes.diff -p "Fixed login page styling issues, especially button alignment"
63+
```
64+
5865
## Contributing
5966

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

README_CN.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
3030
- `-gb, --generate-branch`: 模式:生成分支名
3131
- `--branch-prefix`: 生成的分支名的前缀
3232
- `-v, --verbosity <VERBOSITY>`: 设置提交信息的详细级别。可选值为 `verbose`(详细)、`normal`(正常)或 `quiet`(简洁)。 默认为 `quiet`
33+
- `-p, --prompt <PROMPT>`: 额外的提示信息,帮助 AI 理解提交上下文。
3334
- `-h, --help`: 打印帮助信息。
3435
- `-V, --version`: 打印版本信息。
3536

@@ -53,6 +54,12 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
5354
fastcommit -d changes.diff -v verbose
5455
```
5556

57+
4. 提供额外上下文帮助 AI 理解提交:
58+
59+
```bash
60+
fastcommit -d changes.diff -p "修复了登录页面的样式问题,特别是按钮对齐"
61+
```
62+
5663
## 贡献
5764

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

src/cli.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ pub struct Args {
2626

2727
#[clap(long, help = "Override branch prefix (default from config)")]
2828
pub branch_prefix: Option<String>,
29+
30+
#[clap(short, long, help = "Additional prompt to help AI understand the commit context")]
31+
pub prompt: Option<String>,
2932
}

src/constants.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub const DEFAULT_PROMPT_TEMPLATE: &str = r#"
1313
- 使用{{language}}编写
1414
- 详细程度:{{verbosity_level}}
1515
16+
{{user_description}}
17+
1618
# 什么是约定式提交规范?
1719
1820
约定式提交 1.0.0
@@ -73,6 +75,7 @@ pub enum PromptTemplateReplaceLabel {
7375
VerbosityLevel,
7476
ConventionalCommit,
7577
Diff,
78+
UserDescription,
7679
}
7780

7881
impl PromptTemplateReplaceLabel {
@@ -82,6 +85,7 @@ impl PromptTemplateReplaceLabel {
8285
PromptTemplateReplaceLabel::VerbosityLevel => "{{verbosity_level}}",
8386
PromptTemplateReplaceLabel::ConventionalCommit => "{{conventional_commit}}",
8487
PromptTemplateReplaceLabel::Diff => "{{diff}}",
88+
PromptTemplateReplaceLabel::UserDescription => "{{user_description}}",
8589
}
8690
}
8791
}

src/generate.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@ use crate::constants::{
1111
};
1212
use crate::template_engine::{render_template, TemplateContext};
1313

14-
async fn generate_commit_message(diff: &str, config: &config::Config) -> anyhow::Result<String> {
14+
async fn generate_commit_message(
15+
diff: &str,
16+
config: &config::Config,
17+
user_description: Option<&str>,
18+
) -> anyhow::Result<String> {
1519
let auth = Auth::new(config.api_key.as_str());
1620

1721
let openai = OpenAI::new(auth, &config.api_base());
1822

19-
let template_ctx =
20-
TemplateContext::new(config.conventional, config.language, config.verbosity, diff);
23+
let template_ctx = TemplateContext::new(
24+
config.conventional,
25+
config.language,
26+
config.verbosity,
27+
diff,
28+
user_description,
29+
);
2130

2231
let messages = vec![
2332
Message {
@@ -123,7 +132,7 @@ fn get_diff(diff_file: Option<&str>) -> anyhow::Result<String> {
123132

124133
pub async fn generate(args: &cli::Args, config: &Config) -> anyhow::Result<String> {
125134
let diff = get_diff(args.diff_file.as_deref())?;
126-
let message = generate_commit_message(&diff, config).await?;
135+
let message = generate_commit_message(&diff, config, args.prompt.as_deref()).await?;
127136
Ok(message)
128137
}
129138

src/template_engine.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub struct TemplateContext<'a> {
88
pub language: CommitLanguage,
99
pub verbosity: Verbosity,
1010
pub diff_content: &'a str,
11+
pub user_description: Option<&'a str>,
1112
}
1213

1314
impl<'a> TemplateContext<'a> {
@@ -16,12 +17,14 @@ impl<'a> TemplateContext<'a> {
1617
language: CommitLanguage,
1718
verbosity: Verbosity,
1819
diff_content: &'a str,
20+
user_description: Option<&'a str>,
1921
) -> Self {
2022
Self {
2123
conventional,
2224
language,
2325
verbosity,
2426
diff_content,
27+
user_description,
2528
}
2629
}
2730
}
@@ -43,6 +46,10 @@ pub fn render_template(template: &str, context: TemplateContext) -> anyhow::Resu
4346
.replace(
4447
PromptTemplateReplaceLabel::Diff.get_label(),
4548
context.diff_content,
49+
)
50+
.replace(
51+
PromptTemplateReplaceLabel::UserDescription.get_label(),
52+
context.user_description.unwrap_or(""),
4653
);
4754

4855
Ok(rendered)

0 commit comments

Comments
 (0)