Skip to content

Commit 26ff0ca

Browse files
committed
docs(cz_check): the 'check' document is refactored and the usage of read commit message from pipe is added
1 parent 26f0b1f commit 26ff0ca

File tree

2 files changed

+89
-57
lines changed

2 files changed

+89
-57
lines changed

docs/auto_check.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Automatically check message before commit
2+
3+
## About
4+
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+
6+
## How to
7+
There are two common methods for installing the hook:
8+
### Method 1: Add git hook through [pre-commit](https://pre-commit.com/)
9+
10+
* Step 1: Install [pre-commit](https://pre-commit.com/)
11+
12+
```sh
13+
python -m pip install pre-commit
14+
```
15+
16+
* Step 2: Create `.pre-commit-config.yaml` at your root directory with the following content
17+
18+
```yaml
19+
---
20+
repos:
21+
- repo: https://github.com/commitizen-tools/commitizen
22+
rev: v1.17.0
23+
hooks:
24+
- id: commitizen
25+
stages: [commit-msg]
26+
```
27+
28+
* Step 3: Install the configuration into git hook through `pre-commit`
29+
30+
```bash
31+
pre-commit install --hook-type commit-msg
32+
```
33+
34+
### Method 2: Manually add git hook
35+
The command might be included inside of a Git hook (inside of `.git/hooks/` at the root of the project).
36+
37+
The selected hook might be the file called commit-msg.
38+
39+
This example shows how to use the check command inside of commit-msg.
40+
41+
At the root of the project:
42+
43+
```bash
44+
cd .git/hooks
45+
touch commit-msg
46+
chmod +x commit-msg
47+
```
48+
49+
Open the file and edit it:
50+
51+
```sh
52+
#!/bin/bash
53+
MSG_FILE=$1
54+
cz check --commit-msg-file $MSG_FILE
55+
```
56+
57+
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.
58+
59+
The `--commit-msg-file` flag is required, not optional.
60+
61+
Each time you create a commit, automatically, this hook will analyze it.
62+
If the commit message is invalid, it'll be rejected.
63+
64+
The commit should follow the given committing rules; otherwise, it won't be accepted.
65+

docs/check.md

Lines changed: 24 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,38 @@
1-
## About
1+
# Check
22

3+
## About
34
This feature checks whether the commit message follows the given committing rules.
5+
If you want to setup an automatic check before every git commit, please refer to
6+
[Automatically check message before commit](auto_check.md)
47

5-
## Checking before the commit
6-
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). There are two common methods for installing the hook:
7-
8-
### Method 1: Add git hook through [pre-commit](https://pre-commit.com/)
9-
10-
* Step 1: Install [pre-commit](https://pre-commit.com/)
11-
12-
```sh
13-
python -m pip install pre-commit
14-
```
15-
16-
* Step 2: Create `.pre-commit-config.yaml` at your root directory with the following content
8+
## Usage
9+
There are three arguments that you can use one of them to check commit message
1710

18-
```yaml
19-
---
20-
repos:
21-
- repo: https://github.com/commitizen-tools/commitizen
22-
rev: v1.17.0
23-
hooks:
24-
- id: commitizen
25-
stages: [commit-msg]
11+
### Commit Message File
12+
```bash
13+
$ cz check --commit-msg-file COMMIT_MSG_FILE
2614
```
15+
In this option, COMMIT_MSG_FILE is the path of the temporal file that contains the commit message
2716

28-
* Step 3: Install the configuration into git hook through `pre-commit`
29-
17+
### Git Rev Range
18+
If you'd like to check a commit's message after it has already been created, then you can specify the range of commits to check with `--rev-range REV_RANGE`
3019
```bash
31-
pre-commit install --hook-type commit-msg
20+
$ cz check --rev-range REV_RANGE
3221
```
22+
For example, if you'd like to check all commits on a branch, you can use `--rev-range master..HEAD`. Or, if you'd like to check all commits starting from when you first implemented commit message linting, you can use `--rev-range <first_commit_sha>..HEAD`.
3323

34-
### Method 2: Manually add git hook
35-
The command might be included inside of a Git hook (inside of `.git/hooks/` at the root of the project).
36-
37-
The selected hook might be the file called commit-msg.
38-
39-
This example shows how to use the check command inside of commit-msg.
40-
41-
At the root of the project:
24+
For more info on how git commit ranges work, you can check the [git documentation](https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#_commit_ranges).
4225

26+
### Commit Message
27+
There are two ways you can provide your plain message and check it
28+
#### Method 1: use -m or --message
4329
```bash
44-
cd .git/hooks
45-
touch commit-msg
46-
chmod +x commit-msg
30+
$ cz check --message MESSAGE
4731
```
32+
In this option, MESSAGE is the commit message to be checked
4833

49-
Open the file and edit it:
50-
51-
```sh
52-
#!/bin/bash
53-
MSG_FILE=$1
54-
cz check --commit-msg-file $MSG_FILE
34+
#### Method 2: use pipe to pipe it to `cz check`
35+
```bash
36+
$ echo MESSAGE | cz check
5537
```
56-
57-
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.
58-
59-
The `--commit-msg-file` flag is required, not optional.
60-
61-
Each time you create a commit, automatically, this hook will analyze it.
62-
If the commit message is invalid, it'll be rejected.
63-
64-
The commit should follow the given committing rules; otherwise, it won't be accepted.
65-
66-
## Checking after the commit
67-
If you'd like to check a commit's message after it has already been created, then you can specify the range of commits to check with `--rev-range REV_RANGE`
68-
69-
For example, if you'd like to check all commits on a branch, you can use `--rev-range master..HEAD`. Or, if you'd like to check all commits starting from when you first implemented commit message linting, you can use `--rev-range <first_commit_sha>..HEAD`.
70-
71-
For more info on how git commit ranges work, you can check the [git documentation](https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#_commit_ranges).
38+
In this option, MESSAGE is piped to cz check and would be checked

0 commit comments

Comments
 (0)