summaryrefslogtreecommitdiffstats
path: root/docs/commit_hooks.md
blob: b43050f4a17fb1aa21c943e56022008fb82157d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
## Commit-msg hook
[:octicons-tag-24: v0.4.0][v0.4.0]

You can install gitlint as a git `commit-msg` hook so that gitlint checks your commit messages automatically
after each commit.

```sh
gitlint install-hook

# Remove the hook
gitlint uninstall-hook
```

!!! important

    Gitlint cannot work together with an existing hook. If you already have a `.git/hooks/commit-msg`
    file in your local repository, gitlint will refuse to install the `commit-msg` hook. Gitlint will also only
    uninstall unmodified commit-msg hooks that were installed by gitlint.
    If you're looking to use gitlint in conjunction with other hooks, you should consider
    [using gitlint with pre-commit](#pre-commit).

## Pre-commit

`gitlint` can be configured as a plugin for the [pre-commit](https://pre-commit.com) git hooks
framework.  Simply add the following configuration to your `.pre-commit-config.yaml`:

```yaml
-   repo: https://github.com/jorisroovers/gitlint
    rev:  # Fill in a tag / sha here (e.g. v0.19.1)
    hooks:
    -   id: gitlint
```

You then need to install the pre-commit hook like so:
```sh
pre-commit install --hook-type commit-msg
```
!!! important

    It's important that you run `pre-commit install --hook-type commit-msg`, even if you've already used
    `pre-commit install` before. `pre-commit install` does **not** install commit-msg hooks by default!

To manually trigger gitlint using `pre-commit` for your last commit message, use the following command:
```sh
pre-commit run gitlint --hook-stage commit-msg --commit-msg-filename .git/COMMIT_EDITMSG
```

In case you want to change gitlint's behavior, you should either [use a `.gitlint` file](configuration/gitlint_file.md)
or modify the gitlint invocation in your `.pre-commit-config.yaml` file like so:
```yaml
-   repo: https://github.com/jorisroovers/gitlint
    rev:  # Fill in a tag / sha here (e.g. v0.19.1)
    hooks:
    -   id: gitlint
        args: [--contrib=CT1, --msg-filename]
```

!!! important

    You need to add `--msg-filename` at the end of your custom `args` list as the gitlint-hook will fail otherwise.


### gitlint and pre-commit in CI
gitlint also supports a `gitlint-ci` pre-commit hook that can be used in CI environments.

Configure it like so:
```yaml
-   repo: https://github.com/jorisroovers/gitlint
    rev:  # Fill in a tag / sha here (e.g. v0.19.1)
    hooks:
    -   id: gitlint    # this is the regular commit-msg hook
    -   id: gitlint-ci # hook for CI environments
```

And invoke it in your CI environment like this:

```sh
pre-commit run --hook-stage manual gitlint-ci
```

By default this will only lint the latest commit.
If you want to lint more commits you can modify the `gitlint-ci` hook like so:

```yaml
-   repo: https://github.com/jorisroovers/gitlint
    rev:  # Fill in a tag / sha here (e.g. v0.19.1)
    hooks:
    -   id: gitlint
    -   id: gitlint-ci
        args: [--debug, --commits, mybranch] # enable debug mode, lint all commits in mybranch
```