|
1 |
| -## About |
| 1 | +# Check |
2 | 2 |
|
| 3 | +## About |
3 | 4 | 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). |
4 | 7 |
|
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/) |
| 8 | +## Usage |
| 9 | +There are three arguments that you can use one of them to check commit message. |
9 | 10 |
|
10 |
| -* Step 1: Install [pre-commit](https://pre-commit.com/) |
| 11 | +### Git Rev Range |
| 12 | +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`. |
11 | 13 |
|
12 |
| -```sh |
13 |
| -python -m pip install pre-commit |
| 14 | +```bash |
| 15 | +$ cz check --rev-range REV_RANGE |
14 | 16 | ```
|
15 | 17 |
|
16 |
| -* Step 2: Create `.pre-commit-config.yaml` at your root directory with the following content |
| 18 | +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`. |
17 | 19 |
|
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 |
| -``` |
| 20 | +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). |
27 | 21 |
|
28 |
| -* Step 3: Install the configuration into git hook through `pre-commit` |
| 22 | +### Commit Message |
| 23 | +There are two ways you can provide your plain message and check it. |
| 24 | +#### Method 1: use -m or --message |
29 | 25 |
|
30 | 26 | ```bash
|
31 |
| -pre-commit install --hook-type commit-msg |
| 27 | +$ cz check --message MESSAGE |
32 | 28 | ```
|
33 | 29 |
|
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. |
| 30 | +In this option, MESSAGE is the commit message to be checked. |
38 | 31 |
|
39 |
| -This example shows how to use the check command inside of commit-msg. |
40 |
| - |
41 |
| -At the root of the project: |
| 32 | +#### Method 2: use pipe to pipe it to `cz check` |
42 | 33 |
|
43 | 34 | ```bash
|
44 |
| -cd .git/hooks |
45 |
| -touch commit-msg |
46 |
| -chmod +x commit-msg |
| 35 | +$ echo MESSAGE | cz check |
47 | 36 | ```
|
48 | 37 |
|
49 |
| -Open the file and edit it: |
| 38 | +In this option, MESSAGE is piped to cz check and would be checked. |
50 | 39 |
|
51 |
| -```sh |
52 |
| -#!/bin/bash |
53 |
| -MSG_FILE=$1 |
54 |
| -cz check --commit-msg-file $MSG_FILE |
55 |
| -``` |
| 40 | +### Commit Message File |
56 | 41 |
|
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`. |
| 42 | +```bash |
| 43 | +$ cz check --commit-msg-file COMMIT_MSG_FILE |
| 44 | +``` |
70 | 45 |
|
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). |
| 46 | +In this option, COMMIT_MSG_FILE is the path of the temporal file that contains the commit message. |
| 47 | +This argument can be useful when cooperating with git hook, please check [Automatically check message before commit](auto_check.md) for more information about how to use this argument with git hook. |
0 commit comments