Skip to content

Commit e85e2d4

Browse files
committed
docs(auto_check.md): rewrite to make the doc read more smoothly, update possibly stale information
1 parent 583acf4 commit e85e2d4

File tree

1 file changed

+100
-20
lines changed

1 file changed

+100
-20
lines changed

docs/tutorials/auto_check.md

Lines changed: 100 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,147 @@
22

33
## About
44

5-
To automatically check a commit message prior to committing, you can use a [git hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks).
5+
To automatically check a commit message prior to committing, you can use a [Git hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks). This ensures that all commit messages follow your project's commitizen format before they are accepted into the repository.
6+
7+
When a commit message fails validation, Git will reject the commit and display an error message explaining what went wrong. You'll need to amend your commit message to follow the required format before the commit can proceed.
68

79
## How to
810

911
There are two common methods for installing the hooks:
1012

11-
### Method 1: Add a git hook through [pre-commit](https://pre-commit.com/)
13+
### Method 1: Using [pre-commit](https://pre-commit.com/) (Recommended)
14+
15+
[pre-commit](https://pre-commit.com/) is a framework for managing and maintaining multi-language pre-commit hooks. It's the recommended approach as it handles hook installation, updates, and execution automatically.
1216

13-
- Step 1: Install [pre-commit](https://pre-commit.com/)
17+
#### Step 1: Install pre-commit
1418

1519
```sh
1620
python -m pip install pre-commit
1721
```
1822

19-
- Step 2: Create `.pre-commit-config.yaml` in your root directory with the following content
23+
#### Step 2: Create `.pre-commit-config.yaml`
24+
25+
Create a `.pre-commit-config.yaml` file in your project root directory with the following content:
2026

2127
```yaml
2228
---
2329
repos:
2430
- repo: https://github.com/commitizen-tools/commitizen
25-
rev: v1.17.0
31+
rev: v4.10.0 # Use the latest version or a specific version
2632
hooks:
2733
- id: commitizen
2834
stages: [commit-msg]
2935
```
3036
31-
- Step 3: Install the configuration into the git hook through `pre-commit`
37+
!!! tip "Using the latest version"
38+
Replace `v4.10.0` with the latest commitizen version. You can find the latest version on [GitHub releases](https://github.com/commitizen-tools/commitizen/releases) or use a specific commit SHA for pinning to an exact version.
39+
40+
#### Step 3: Install the hook
41+
42+
Install the configuration into Git's hook system:
3243

3344
```bash
3445
pre-commit install --hook-type commit-msg
3546
```
3647

37-
### Method 2: Manually add a git hook
48+
The hook is now active! Every time you create a commit, commitizen will automatically validate your commit message.
3849

39-
The command might be included inside a Git hook (inside `.git/hooks/` at the root of the project).
50+
### Method 2: Manual Git hook installation
4051

41-
The selected hook might be the file called commit-msg.
52+
If you prefer not to use pre-commit, you can manually create a Git hook. This gives you full control over the hook script but requires manual maintenance.
4253

43-
This example shows how to use the check command inside commit-msg.
54+
#### Step 1: Create the commit-msg hook
4455

45-
At the root of the project:
56+
Navigate to your project's `.git/hooks` directory and create the `commit-msg` hook file:
4657

4758
```bash
4859
cd .git/hooks
4960
touch commit-msg
5061
chmod +x commit-msg
5162
```
5263

53-
Open the file and edit it:
64+
#### Step 2: Add the commitizen check command
5465

55-
```sh
66+
Open the `commit-msg` file in your editor and add the following content:
67+
68+
```bash
5669
#!/bin/bash
57-
MSG_FILE=$1
58-
cz check --allow-abort --commit-msg-file $MSG_FILE
70+
cz check --allow-abort --commit-msg-file $1
71+
```
72+
73+
Where:
74+
75+
- `$1` is the temporary file path that Git provides, containing the current commit message
76+
- `--allow-abort` allows empty commit messages (which typically instruct Git to abort a commit)
77+
- `--commit-msg-file` tells commitizen to read the commit message from the specified file
78+
79+
The hook is now active! Each time you create a commit, this hook will automatically validate your commit message.
80+
81+
## Testing the hook
82+
83+
After installing the hook, you can test it by attempting to commit with an invalid message:
84+
85+
```bash
86+
# This should fail with an invalid message
87+
git commit -m "invalid commit message"
88+
89+
# This should succeed with a valid message
90+
git commit -m "feat: add new feature"
5991
```
6092

61-
Where `$1` is the name of the temporary file that contains the current commit message. To be more explicit, the previous variable is stored in another variable called `$MSG_FILE`, for didactic purposes.
93+
If the hook is working correctly, invalid commit messages will be rejected with an error message explaining what's wrong.
94+
95+
## What happens when validation fails?
96+
97+
When a commit message fails validation:
98+
99+
1. Git will abort the commit
100+
2. An error message will be displayed showing:
101+
- Which commit failed (if checking multiple commits)
102+
- The invalid commit message
103+
- The expected pattern/format
104+
3. Your changes remain staged, so you can simply amend the commit message and try again
105+
106+
Example error output:
107+
108+
<!-- DEPENDENCY: InvalidCommitMessageError -->
109+
110+
```
111+
commit validation: failed!
112+
please enter a commit message in the commitizen format.
113+
commit "abc123": "invalid message"
114+
pattern: ^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.+\))?: .{1,}
115+
```
116+
117+
## Troubleshooting
118+
119+
### Hook not running
120+
121+
- **Verify the hook file exists**: Check that `.git/hooks/commit-msg` exists and is executable
122+
- **Check file permissions**: Ensure the hook has execute permissions (`chmod +x .git/hooks/commit-msg`)
123+
- **Verify commitizen is installed**: Run `cz --version` to confirm commitizen is available in your PATH
124+
- **Check Git version**: Ensure you're using a recent version of Git that supports hooks
125+
126+
### Pre-commit hook not working
127+
128+
- **Verify installation**: Run `pre-commit --version` to confirm pre-commit is installed
129+
- **Reinstall the hook**: Try running `pre-commit install --hook-type commit-msg` again
130+
- **Check configuration**: Verify your `.pre-commit-config.yaml` file is valid YAML and in the project root
131+
- **Update hooks**: Run `pre-commit autoupdate` to update to the latest versions
132+
133+
### Bypassing the hook (when needed)
134+
135+
If you need to bypass the hook temporarily (e.g., for merge commits or special cases), you can use:
136+
137+
```bash
138+
git commit --no-verify -m "your message"
139+
```
62140

63-
The `--commit-msg-file` flag is required, not optional.
141+
!!! warning "Use with caution"
142+
Only bypass hooks when absolutely necessary, as it defeats the purpose of automated validation.
64143

65-
Each time you create a commit, this hook will automatically analyze it.
66-
If the commit message is invalid, it will be rejected.
144+
## Additional resources
67145

68-
The commit should follow the given committing rules; otherwise, it won't be accepted.
146+
- Learn more about [Git hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks)
147+
- See the [check command documentation](../commands/check.md) for more validation options
148+
- Check out [pre-commit documentation](https://pre-commit.com/) for advanced hook management

0 commit comments

Comments
 (0)