1

I am trying to add a gitlint pre-commit hook in a repository. The .pre-commit-config.yaml file looks lke this:

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
  - repo: local
    hooks:
      - id: gitlint
        name: gitlint
        entry: gitlint
        language: system

However, I keep getting:

- hook id: gitlint
- exit code: 253

Usage: gitlint [OPTIONS] COMMAND [ARGS]...
Try 'gitlint --help' for help.

Error: No such command '.pre-commit-config.yaml'.

This is after I run:

pip install gitlint
pre-commit install --hook-type commit-msg

What am I doing wrong?

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
Christos Hadjinikolis
  • 2,099
  • 3
  • 20
  • 46

2 Answers2

4

Your configuration is broken because of a few reasons:

  • you've configured gitlint for all stages, meaning it will run on other git hooks you don't want (such as pre-commit, pre-push, etc.). to fix this you'll set stages: [commit-msg]
  • you're missing a few other settings as well such as the proper arguments to gitlint etc.
  • additionally you're using language: system which means you're depending on your contributors to set up tooling -- this misses the point of pre-commit and is the unsupported escape hatch. the usual way to have managed tools is to reuse an existing repository (see below) or to use additional_dependencies to install the tool in a managed way

The supported way to use gitlint is from the repository itself

repos:
-   repo: https://github.com/jorisroovers/gitlint
    rev: ''  # pick a tag / sha to use
    hooks:
    -   id: gitlint

disclaimer: I'm the creator of pre-commit and contributed the pre-commit support to gitlint

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • Thanks. That was very helpful. I still want to set it up locally and so I did. Your comments helped me to it. – Christos Hadjinikolis Feb 28 '21 at 18:49
  • i am sorry i still dont get it even after reading the documentation on pre-commit.com what is the difference between language python and language system. Docs say "This hook type will not be given a virtual environment to work with " what happens if you set language python? does it automatically pick poetry from the pyproject.toml file to find out dependencies? – PirateApp Jul 05 '22 at 11:25
  • @PirateApp did you read [this](https://pre-commit.com/#python) ? – anthony sottile Jul 05 '22 at 13:08
0

The error message is not very helpful. I was just missing args and stages fields:

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
---
repos:
   - repo: local
     hooks:
       - id: gitlint
         name: gitlint
         language: python
         entry: gitlint
         args: [--staged, --msg-filename]
         stages: [commit-msg]
Christos Hadjinikolis
  • 2,099
  • 3
  • 20
  • 46