Skip to content

Commit aa13e1f

Browse files
committed
docs: Expand README with comprehensive installation and usage guides 📖
- add step-by-step installation options including crates.io, source, and Docker - include detailed usage examples, command-line options, and workflow integration Signed-off-by: mingcheng <mingcheng@apache.org>
1 parent 0413644 commit aa13e1f

File tree

1 file changed

+206
-69
lines changed

1 file changed

+206
-69
lines changed

README.md

Lines changed: 206 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,76 @@ It inspects your diffs, summarizes the intent of your changes, and produces clea
1414

1515
## References
1616

17-
- https://www.conventionalcommits.org/en/v1.0.0/
18-
- https://nitayneeman.com/blog/understanding-semantic-commit-messages-using-git-and-angular/
19-
- https://ssshooter.com/2020-09-30-commit-message/
17+
- [Conventional Commits Specification](https://www.conventionalcommits.org/en/v1.0.0/)
18+
- [Understanding Semantic Commit Messages](https://nitayneeman.com/blog/understanding-semantic-commit-messages-using-git-and-angular/)
19+
- [Commit Message Best Practices](https://ssshooter.com/2020-09-30-commit-message/)
20+
21+
## Contributing
22+
23+
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests on [GitHub](https://github.com/mingcheng/aigitcommit).
24+
25+
## License
26+
27+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
2028

2129
## Features
2230

23-
- Generates meaningful, semantic commit messages from staged changes.
24-
- Commit directly to the repository with the `--commit` flag or copy the generated message with `--copy`.
25-
- Output formats: human-readable text, JSON (machine-readable) and table view. JSON output is useful for CI integrations and automation; table view makes it easy to scan multiple suggested lines.
26-
- Easy-to-use command-line interface with sensible defaults and confirm prompts (can be skipped with `--yes`).
27-
- Uses libgit2 via the `git2` crate, avoiding external git commands for improved security and performance.
28-
- Supports multiple OpenAI-compatible models and configurable API base, token, and proxy settings.
29-
- Optional auto sign-off of commits when `AIGITCOMMIT_SIGNOFF=true` or `git config --bool aigitcommit.signoff true`.
30-
- Proxy support: HTTP and SOCKS5 (set via `OPENAI_API_PROXY`).
31+
- **AI-Powered Commit Messages**: Automatically generates meaningful, semantic commit messages from staged Git changes
32+
- **Conventional Commits**: Follows the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) specification for consistent, structured messages
33+
- **Multiple Output Formats**:
34+
- Human-readable table view (default)
35+
- JSON format for CI/CD integration and automation
36+
- Plain text output
37+
- **Flexible Workflow**:
38+
- Direct commit with `--commit` flag
39+
- Copy to clipboard with `--copy-to-clipboard`
40+
- Git hook integration for automatic message generation
41+
- **Interactive & Non-Interactive**: Confirmation prompts by default, skip with `--yes` for scripting
42+
- **Security & Performance**: Uses libgit2 via the `git2` crate, avoiding external git command execution
43+
- **Multi-Provider Support**: Compatible with OpenAI and other OpenAI-compatible APIs (Azure OpenAI, local models, etc.)
44+
- **Flexible Configuration**:
45+
- Environment variables for API settings
46+
- Git config for repository-specific or global settings
47+
- Configurable API base URL, token, proxy, and timeouts
48+
- **Sign-off Support**: Auto sign-off via `AIGITCOMMIT_SIGNOFF` environment variable or `git config aigitcommit.signoff`
49+
- **Proxy Support**: HTTP and SOCKS5 proxies via `OPENAI_API_PROXY`
3150

3251

3352
## How It Works
3453

35-
AIGitCommit inspects your staged Git changes, summarizes the intent of those changes, and generates clear semantic commit messages. It examines diffs and uses an AI model to infer intent and produce concise, useful commit lines.
54+
AIGitCommit streamlines your commit workflow by:
3655

37-
## Install
56+
1. **Analyzing Changes**: Inspects staged changes using `git diff --cached`
57+
2. **Understanding Context**: Examines recent commit history for stylistic consistency
58+
3. **AI Generation**: Sends diffs to an OpenAI-compatible model with carefully crafted prompts
59+
4. **Structured Output**: Generates commit messages following Conventional Commits specification
60+
5. **User Review**: Presents the message for review and optional editing
3861

39-
AIGitCommit is still in the early stages of development, I suggest you to install it using the git URL using the commands below:
62+
The tool uses libgit2 for secure, efficient Git operations without spawning external processes. It automatically filters out common noise files (lock files, generated code) to focus on meaningful changes.
4063

41-
```
42-
cargo install --git https://github.com/mingcheng/aigitcommit.git
43-
```
64+
## Installation
4465

45-
or, You can install from [crates.io](https://crates.io/crates/aigitcommit)
66+
### From crates.io (Recommended)
4667

47-
```
68+
```bash
4869
cargo install aigitcommit
4970
```
5071

51-
Those command will auto-download the latest version of the project and install it to your cargo bin directory.
72+
### From Source
73+
74+
For the latest development version:
75+
76+
```bash
77+
cargo install --git https://github.com/mingcheng/aigitcommit.git
78+
```
79+
80+
Both commands will download, compile, and install the binary to your Cargo bin directory (typically `~/.cargo/bin`). Ensure this directory is in your `PATH`.
5281

53-
### Docker image
82+
### Docker Image
5483

55-
AIGitCommit can run in Docker if you prefer not to install the binary locally. Example (read-only repository):
84+
Run AIGitCommit in Docker without installing the binary locally.
85+
86+
**Read-only mode** (generate message only):
5687

5788
```bash
5889
docker run \
@@ -65,7 +96,7 @@ docker run \
6596
ghcr.io/mingcheng/aigitcommit
6697
```
6798

68-
If you want to use `--commit` from inside the container, mount the repo as writable and run interactively:
99+
**Interactive mode** (with `--commit` flag):
69100

70101
```bash
71102
docker run \
@@ -76,103 +107,209 @@ docker run \
76107
-e OPENAI_API_TOKEN='<api token>' \
77108
-e OPENAI_MODEL_NAME='<model name>' \
78109
-e OPENAI_API_PROXY='<proxy if needed>' \
79-
ghcr.io/mingcheng/aigitcommit --commit
110+
ghcr.io/mingcheng/aigitcommit --commit --yes
80111
```
81112

82-
Use `--yes` to skip interactive confirmations.
113+
Note: Use `--yes` to skip interactive confirmations in non-TTY environments.
83114

84-
### Git hook
115+
### Git Hook
85116

86-
AIGitCommit ships a `hooks/prepare-commit-msg` hook that pauses your commit workflow, looks at the staged diff, and pre-populates `COMMIT_EDITMSG` with an AI-generated summary. This lets you fine-tune the final message instead of writing it from scratch.
117+
AIGitCommit includes a `prepare-commit-msg` hook that automatically generates commit messages during your workflow. The hook triggers when you run `git commit` or `git commit -m ""`, generates a message from staged changes, and opens your editor for review.
87118

88119
**Prerequisites**
89-
- `aigitcommit` must be installed and discoverable on your `PATH`.
90-
- Required environment variables (`OPENAI_API_TOKEN`, `OPENAI_API_BASE`, etc.) should be configured in your shell before running `git commit`.
91120

92-
**Project-level installation**
121+
- `aigitcommit` must be installed and available in your `PATH`
122+
- Configure required environment variables before committing (see [Configuration](#configuration))
123+
124+
**Per-Repository Installation**
93125

94-
Install the hook in the current repository only:
126+
Install the hook for a single repository:
95127

96128
```bash
97129
cp hooks/prepare-commit-msg .git/hooks/prepare-commit-msg
98130
chmod +x .git/hooks/prepare-commit-msg
99131
```
100132

101-
After copying, stage some changes and run `git commit`. The hook prints progress messages, writes the suggested commit text, and drops you into your editor so you can adjust the result. To verify the hook without creating a new commit, try `git commit --amend` against a throwaway repository.
133+
After installation, the hook runs automatically when you execute `git commit`. You can review and edit the generated message before finalizing the commit.
102134

103-
If you need to disable the hook for a single commit, use `git commit --no-verify`.
135+
**Disable for a single commit**: Use `git commit --no-verify` to bypass the hook.
104136

105-
**Global installation**
137+
**Global Installation**
106138

107-
Install once and reuse across repositories:
139+
Set up the hook for all new and existing repositories using Git templates:
108140

109141
```bash
110-
mkdir -p ~/.git-hooks
111-
cp hooks/prepare-commit-msg ~/.git-hooks/prepare-commit-msg
112-
chmod +x ~/.git-hooks/prepare-commit-msg
113-
git config --global core.hooksPath ~/.git-hooks
142+
# Create template directory structure
143+
mkdir -p ~/.git-template/hooks
144+
cp hooks/prepare-commit-msg ~/.git-template/hooks/prepare-commit-msg
145+
chmod +x ~/.git-template/hooks/prepare-commit-msg
146+
147+
# Configure Git to use this template for new repositories
148+
git config --global init.templateDir ~/.git-template
149+
150+
# Apply to existing repositories
151+
# Option 1: Copy manually
152+
cp ~/.git-template/hooks/prepare-commit-msg <repo>/.git/hooks/
153+
154+
# Option 2: Re-initialize (safe, preserves existing data)
155+
cd <repo> && git init
114156
```
115157

116-
This approach lets every repository automatically pick up the hook as long as `core.hooksPath` remains set.
158+
**Important**: Setting `core.hooksPath` globally overrides all repository hooks. The template approach is more flexible and recommended.
159+
160+
**Hook Behavior**
161+
162+
The hook only runs when:
163+
- You execute `git commit` (interactive mode) with no pre-written message
164+
- You execute `git commit -m ""` (explicit empty message)
165+
166+
The hook skips execution for:
167+
- Commits with pre-written messages (`git commit -m "message"`)
168+
- Merge commits, rebase, cherry-pick, or other automated commits
169+
- When the commit message file already contains non-comment content
117170

118171
**Troubleshooting**
119-
- If the hook exits early with a warning about missing staged changes, make sure you have run `git add`.
120-
- A message about missing configuration usually means the OpenAI-related environment variables are not exported in your shell session.
121-
- Hook output is written to stderr; if you prefer a quieter experience, redirect or silence stderr in your Git configuration.
172+
173+
- **"No staged changes detected"**: Run `git add` to stage your changes before committing
174+
- **"aigitcommit is not installed"**: Ensure the binary is in your `PATH` or install it first
175+
- **Missing configuration error**: Export required environment variables (`OPENAI_API_TOKEN`, etc.) in your shell
176+
- **Hook output too verbose**: Redirect stderr in your Git configuration: `git config core.hookStderr false`
122177

123178
## Configuration
124179

125-
Before using AIGitCommit, export the following environment variables (for example in your shell profile):
180+
### Environment Variables
181+
182+
Configure AIGitCommit by setting these environment variables (in your shell profile, `.bashrc`, `.zshrc`, etc.):
126183

127-
- `OPENAI_API_TOKEN`: Your OpenAI-compatible API token.
128-
- `OPENAI_API_BASE`: The API base URL (useful for alternative providers or local proxies).
129-
- `OPENAI_MODEL_NAME`: The model name to query (e.g., a GPT-compatible model).
130-
- `OPENAI_API_PROXY`: Optional. Proxy address for network access (e.g., `http://127.0.0.1:1080` or `socks://127.0.0.1:1086`).
131-
- `AIGITCOMMIT_SIGNOFF`: Optional. Set to `true` (or any truthy value) to append a Signed-off-by line to commits.
184+
**Required:**
185+
- `OPENAI_API_TOKEN`: Your OpenAI-compatible API authentication token
186+
- `OPENAI_API_BASE`: API endpoint URL (e.g., `https://api.openai.com/v1` or your provider's URL)
187+
- `OPENAI_MODEL_NAME`: Model identifier (e.g., `gpt-4`, `gpt-3.5-turbo`, or provider-specific models)
132188

133-
You can also enable sign-off via Git configuration:
189+
**Optional:**
190+
- `OPENAI_API_PROXY`: HTTP/SOCKS5 proxy URL (e.g., `http://127.0.0.1:1080`, `socks5://127.0.0.1:1086`)
191+
- `OPENAI_API_TIMEOUT`: Request timeout in seconds (default: 30)
192+
- `OPENAI_API_MAX_TOKENS`: Maximum tokens in response (default: model-specific)
193+
- `AIGITCOMMIT_SIGNOFF`: Enable auto sign-off (`true`, `1`, `yes`, `on`)
194+
195+
**Example configuration:**
134196

135197
```bash
136-
git config aigitcommit.signoff true # repository only
137-
git config --global aigitcommit.signoff true
198+
# ~/.bashrc or ~/.zshrc
199+
export OPENAI_API_TOKEN="sk-..."
200+
export OPENAI_API_BASE="https://api.openai.com/v1"
201+
export OPENAI_MODEL_NAME="gpt-4"
202+
export OPENAI_API_PROXY="http://127.0.0.1:1080" # Optional
203+
export AIGITCOMMIT_SIGNOFF="true" # Optional
138204
```
139205

140-
The Git configuration takes precedence over the environment variable.
206+
### Git Configuration
207+
208+
You can also enable sign-off via Git configuration (takes precedence over environment variables):
209+
210+
```bash
211+
# Repository-specific
212+
git config aigitcommit.signoff true
213+
214+
# Global (all repositories)
215+
git config --global aigitcommit.signoff true
216+
```
141217

142-
### Check the configuration
218+
### Verify Configuration
143219

144-
After setting the environment variables, you can check if they are set correctly by running:
220+
Check your environment setup:
145221

146222
```bash
223+
# Verify all environment variables
147224
aigitcommit --check-env
225+
226+
# Test API connectivity and model availability
227+
aigitcommit --check-model
228+
229+
# Show all available options
230+
aigitcommit --help
148231
```
149232

150-
This will print the current configuration and verify that the required variables are set.
233+
## Usage
234+
235+
### Basic Usage
151236

152-
Then you can run
237+
Run AIGitCommit in a Git repository with staged changes:
153238

154239
```bash
155-
aigitcommit --check-model
240+
# In the current repository
241+
aigitcommit
242+
243+
# Specify a different repository path
244+
aigitcommit /path/to/repo
156245
```
157246

158-
to check if the specified model is available and can be queried successfully.
247+
The tool will:
248+
1. Analyze your staged changes (`git diff --cached`)
249+
2. Generate a Conventional Commit message using AI
250+
3. Display the result in table format (default)
159251

160-
You can also run `aigitcommit --help` to see the available options and usage instructions.
252+
### Command-Line Options
161253

162-
## Usage
254+
**Output Formats:**
255+
- Default: Table view (easy to read)
256+
- `--json`: JSON output (for CI/automation)
257+
- `--no-table`: Plain text output
258+
259+
**Actions:**
260+
- `--commit`: Automatically commit with the generated message
261+
- `--copy-to-clipboard`: Copy the message to clipboard
262+
- `--yes`: Skip confirmation prompts (useful for scripting)
263+
- `--signoff`: Append `Signed-off-by` line to the commit
163264

164-
Run `aigitcommit` in a repository with staged changes. Optionally provide a path to the git directory: `aigitcommit <dir>`.
265+
**Diagnostics:**
266+
- `--check-env`: Verify environment variable configuration
267+
- `--check-model`: Test API connectivity and model availability
268+
- `--help`: Show all available options
165269

166-
Common flags:
270+
### Examples
167271

168-
1. `--commit` commit generated message directly to the repository.
169-
2. `--copy-to-clipboard` copy the generated message to the clipboard.
170-
3. `--json` print the suggestions as JSON for CI or automation.
171-
4. `--yes` skip confirmation prompts and apply the default action.
172-
5. `--signoff` append a Signed-off-by line to the commit message.
272+
**Generate and review message:**
273+
```bash
274+
aigitcommit
275+
```
173276

174-
See `aigitcommit --help` for the full list of options.
277+
**Auto-commit without confirmation:**
278+
```bash
279+
aigitcommit --commit --yes
280+
```
175281

282+
**Copy message to clipboard:**
283+
```bash
284+
aigitcommit --copy-to-clipboard
285+
```
286+
287+
**JSON output for CI pipelines:**
288+
```bash
289+
aigitcommit --json | jq '.title'
290+
```
291+
292+
**Commit with sign-off:**
293+
```bash
294+
aigitcommit --commit --signoff
295+
```
296+
297+
### Workflow Integration
298+
299+
**Typical workflow:**
300+
```bash
301+
# Stage your changes
302+
git add .
303+
304+
# Generate and review commit message
305+
aigitcommit
306+
307+
# Or commit directly
308+
aigitcommit --commit
309+
310+
# Or use the Git hook (if installed)
311+
git commit # Hook generates message automatically
312+
```
176313

177314
## License
178315

0 commit comments

Comments
 (0)