Skip to content

Commit e1bce7b

Browse files
authored
feat: add GitHub PR integration for commit message generation (#23)
- Add`pr`subcommand to generate commit messages for GitHub Pull Requests - Implement PR diff retrieval using GitHub CLI (`gh`) - Add auto-detection of current branch's associated PR - Create comprehensive documentation for PR integration feature - Refactor CLI structure to support subcommands (commit/pr) - Update README files with usage examples and options Signed-off-by: jinlong <jinlong@tencent.com>
1 parent 13c506d commit e1bce7b

7 files changed

Lines changed: 520 additions & 52 deletions

File tree

README.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
8686
```bash
8787
# Generate commit message for the last commit
8888
fastcommit -r HEAD~1
89-
89+
9090
# Generate commit message for a range of commits
9191
fastcommit -r abc123..def456
9292
```
@@ -96,10 +96,10 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
9696
```bash
9797
# Disable text wrapping
9898
fastcommit --no-wrap
99-
99+
100100
# Set custom line width
101101
fastcommit --wrap-width 60
102-
102+
103103
# Combine with other options
104104
fastcommit -b -m --wrap-width 100
105105
```
@@ -114,9 +114,49 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
114114
fastcommit -c --commit-args "-s" --commit-args "--no-verify"
115115
```
116116

117+
## GitHub PR Integration
118+
119+
`fastcommit` can generate commit messages for GitHub Pull Requests, which is useful when merging PRs.
120+
121+
### Prerequisites
122+
123+
- [GitHub CLI (`gh`)](https://cli.github.com/) must be installed and authenticated
124+
125+
### Usage
126+
127+
```bash
128+
# Auto-detect PR from current branch
129+
fastcommit pr
130+
131+
# Generate commit message for a specific PR
132+
fastcommit pr 123
133+
134+
# Specify repository (when not in a git directory)
135+
fastcommit pr 123 --repo owner/repo
136+
137+
# Use conventional commit style
138+
fastcommit pr 123 --conventional true
139+
140+
# Specify language
141+
fastcommit pr 123 -l zh
142+
```
143+
144+
### PR Command Options
145+
146+
- `[PR_NUMBER]`: PR number to generate commit message for. If not specified, auto-detects from current branch.
147+
- `--repo <REPO>`: Specify repository in `owner/repo` format.
148+
- `--conventional <CONVENTIONAL>`: Enable conventional commit style.
149+
- `-l, --language <LANGUAGE>`: Specify language (`en` or `zh`).
150+
- `-v, --verbosity <VERBOSITY>`: Set detail level (`verbose`, `normal`, `quiet`).
151+
- `-p, --prompt <PROMPT>`: Additional context for AI.
152+
- `--no-sanitize`: Disable sensitive info sanitizer.
153+
- `--no-wrap`: Disable text wrapping.
154+
155+
For more details, see [GitHub PR Integration Guide](docs/github-pr-integration.md).
156+
117157
## Contributing
118158

119-
Contributions of code or suggestions are welcome! Please read the [Contributing Guide](CONTRIBUTING.md) first.
159+
Contributions of code or suggestions are welcome! Please read the [Contributing Guide](CONTRIBUTING.md) first.
120160

121161
## License
122162

README_CN.md

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ cargo install --git https://github.com/fslongjin/fastcommit --tag v0.6.0
1717

1818
```bash
1919
git add .
20-
fastcommit
20+
fastcommit
2121
```
2222

2323
### 选项
@@ -29,7 +29,7 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
2929
- `-l, --language <LANGUAGE>`: 指定提交信息的语言。可选值为 `en`(英文)或 `zh`(中文)。
3030
- `-b, --generate-branch`: 生成分支名
3131
- `--branch-prefix`: 生成的分支名的前缀
32-
- `-m, --message`: 生成提交信息(与 -b 一起使用可同时输出)
32+
- `-m, --message`: 生成提交信息(与 -b 一起使用可同时输出)
3333
- `-v, --verbosity <VERBOSITY>`: 设置提交信息的详细级别。可选值为 `verbose`(详细)、`normal`(正常)或 `quiet`(简洁)。 默认为 `quiet`
3434
- `-p, --prompt <PROMPT>`: 额外的提示信息,帮助 AI 理解提交上下文。
3535
- `-r, --range <RANGE>`: 指定差异范围以生成提交信息(例如:HEAD~1, abc123..def456)。
@@ -83,7 +83,7 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
8383
```bash
8484
# 为最近一次提交生成提交信息
8585
fastcommit -r HEAD~1
86-
86+
8787
# 为指定提交范围生成提交信息
8888
fastcommit -r abc123..def456
8989
```
@@ -93,10 +93,10 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
9393
```bash
9494
# 禁用文本换行
9595
fastcommit --no-wrap
96-
96+
9797
# 设置自定义行宽度
9898
fastcommit --wrap-width 60
99-
99+
100100
# 与其他选项组合使用
101101
fastcommit -b -m --wrap-width 100
102102
```
@@ -111,6 +111,46 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
111111
fastcommit -c --commit-args "-s" --commit-args "--no-verify"
112112
```
113113

114+
## GitHub PR 集成
115+
116+
`fastcommit` 可以为 GitHub Pull Request 生成提交信息,适用于合并 PR 时使用。
117+
118+
### 前置条件
119+
120+
- 需要安装并登录 [GitHub CLI (`gh`)](https://cli.github.com/)
121+
122+
### 使用方法
123+
124+
```bash
125+
# 自动检测当前分支关联的 PR
126+
fastcommit pr
127+
128+
# 为指定 PR 生成提交信息
129+
fastcommit pr 123
130+
131+
# 指定仓库(不在 git 目录中时)
132+
fastcommit pr 123 --repo owner/repo
133+
134+
# 使用约定式提交风格
135+
fastcommit pr 123 --conventional true
136+
137+
# 指定语言
138+
fastcommit pr 123 -l zh
139+
```
140+
141+
### PR 命令选项
142+
143+
- `[PR_NUMBER]`: PR 编号,不指定则自动检测当前分支关联的 PR
144+
- `--repo <REPO>`: 指定仓库,格式为 `owner/repo`
145+
- `--conventional <CONVENTIONAL>`: 启用约定式提交风格
146+
- `-l, --language <LANGUAGE>`: 指定语言(`en``zh`
147+
- `-v, --verbosity <VERBOSITY>`: 设置详细级别(`verbose``normal``quiet`
148+
- `-p, --prompt <PROMPT>`: 额外的提示信息
149+
- `--no-sanitize`: 禁用敏感信息清理
150+
- `--no-wrap`: 禁用文本换行
151+
152+
更多详情请参阅 [GitHub PR 集成指南](docs/github-pr-integration.md)
153+
114154
## 贡献
115155

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

docs/github-pr-integration.md

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# GitHub PR Integration
2+
3+
This document describes how to use `fastcommit` to generate commit messages for GitHub Pull Requests.
4+
5+
## Overview
6+
7+
The `fastcommit pr` command allows you to generate a commit message for an entire Pull Request. This is particularly useful when you're ready to merge a PR and need a comprehensive commit message that summarizes all the changes.
8+
9+
## Prerequisites
10+
11+
Before using the PR integration, ensure you have:
12+
13+
1. **GitHub CLI (`gh`) installed**
14+
15+
```bash
16+
# Check if gh is installed
17+
gh --version
18+
19+
# Install gh on macOS
20+
brew install gh
21+
22+
# Install gh on Linux
23+
# See: https://github.com/cli/cli/blob/trunk/docs/install_linux.md
24+
```
25+
26+
2. **GitHub CLI authenticated**
27+
28+
```bash
29+
gh auth login
30+
```
31+
32+
## Usage
33+
34+
### Basic Usage
35+
36+
```bash
37+
# Auto-detect PR from current branch
38+
fastcommit pr
39+
40+
# Generate commit message for a specific PR
41+
fastcommit pr 123
42+
```
43+
44+
### Specify Repository
45+
46+
When running `fastcommit pr` outside the target repository, use the `--repo` flag:
47+
48+
```bash
49+
fastcommit pr 123 --repo owner/repo
50+
```
51+
52+
### All Options
53+
54+
```
55+
fastcommit pr [PR_NUMBER] [OPTIONS]
56+
57+
Arguments:
58+
[PR_NUMBER] PR number to generate commit message for.
59+
If not specified, auto-detects from current branch.
60+
61+
Options:
62+
--repo <REPO> Specify repository (format: owner/repo)
63+
--conventional <BOOL> Enable conventional commit style (true/false)
64+
-l, --language <LANG> Specify language (en/zh)
65+
-v, --verbosity <LEVEL> Set detail level (verbose/normal/quiet)
66+
-p, --prompt <TEXT> Additional context for AI
67+
--no-sanitize Disable sensitive info sanitizer
68+
--no-wrap Disable text wrapping
69+
--wrap-width <WIDTH> Set custom line width for wrapping
70+
```
71+
72+
## Examples
73+
74+
### Generate commit message for current branch's PR
75+
76+
```bash
77+
# Assuming you're on a branch with an open PR
78+
fastcommit pr
79+
```
80+
81+
Output:
82+
```
83+
feat: add user authentication system
84+
85+
- Implement JWT-based authentication
86+
- Add login/logout endpoints
87+
- Integrate with existing user service
88+
- Add rate limiting for auth endpoints
89+
```
90+
91+
### Generate with conventional commit style
92+
93+
```bash
94+
fastcommit pr 456 --conventional true
95+
```
96+
97+
Output:
98+
```
99+
fix(auth): resolve token refresh issue
100+
101+
- Fix improper token validation on refresh
102+
- Add proper error handling for expired tokens
103+
- Update token storage to use secure cookies
104+
```
105+
106+
### Add context for better results
107+
108+
```bash
109+
fastcommit pr 789 -p "This PR fixes the performance issues reported in issue #123"
110+
```
111+
112+
### Generate in Chinese
113+
114+
```bash
115+
fastcommit pr 123 -l zh
116+
```
117+
118+
Output:
119+
```
120+
feat: 添加用户认证系统
121+
122+
- 实现 JWT 认证机制
123+
- 添加登录/登出接口
124+
- 与现有用户服务集成
125+
- 添加认证接口的速率限制
126+
```
127+
128+
## How It Works
129+
130+
1. **PR Detection**: If no PR number is specified, `fastcommit` uses `gh pr view` to detect the PR associated with the current branch.
131+
132+
2. **Diff Retrieval**: The tool fetches the PR diff using `gh pr diff`.
133+
134+
3. **Message Generation**: The diff is processed by the AI to generate a commit message, using the same logic as the standard `fastcommit` command.
135+
136+
## Troubleshooting
137+
138+
### "GitHub CLI (gh) is not installed"
139+
140+
Install GitHub CLI:
141+
- **macOS**: `brew install gh`
142+
- **Linux**: See [installation guide](https://github.com/cli/cli/blob/trunk/docs/install_linux.md)
143+
- **Windows**: `winget install GitHub.cli`
144+
145+
### "Failed to detect current PR"
146+
147+
This happens when:
148+
- You're not on a branch with an open PR
149+
- The PR is in a different repository
150+
151+
**Solution**: Specify the PR number explicitly:
152+
```bash
153+
fastcommit pr 123
154+
```
155+
156+
### "gh pr diff failed"
157+
158+
This usually indicates:
159+
- You're not authenticated with GitHub CLI
160+
161+
**Solution**: Run `gh auth login`
162+
163+
### "No diff found for PR"
164+
165+
This happens when the PR has no changes (empty PR).
166+
167+
## Tips
168+
169+
1. **Use with PR merge**: Generate the commit message before merging:
170+
```bash
171+
# Generate the message
172+
fastcommit pr 123
173+
174+
# Then merge with the generated message
175+
gh pr merge 123 --merge
176+
```
177+
178+
2. **Combine with conventional commits**: For projects following conventional commit conventions:
179+
```bash
180+
fastcommit pr 123 --conventional true
181+
```
182+
183+
3. **Add merge context**: Provide additional context about the PR's purpose:
184+
```bash
185+
fastcommit pr 123 -p "Closes #456, implements feature requested by users"
186+
```
187+
188+
## Related Documentation
189+
190+
- [README.md](../README.md) - Main documentation
191+
- [GitHub CLI Documentation](https://cli.github.com/manual/)

0 commit comments

Comments
 (0)