Gitlint Core
Gitlint-core is the core library for Gitlint, a Python-based linter that enforces style and consistency for Git commit messages. It is an actively maintained project with regular releases, providing robust checks and customizability for commit hooks and CI pipelines.
Warnings
- breaking Python 3.6 is no longer supported since v0.19.0. Ensure your environment uses Python 3.7 or newer. Earlier versions (2.7 and 3.5) were dropped in v0.15.0.
- deprecated The `sh` library, used under-the-hood for git command execution, is no longer used by default since v0.18.0. It was completely removed in v0.19.0 (though v0.19.0 stated it was the last release to *support* re-enabling it). Relying on the `GITLINT_USE_SH_LIB=1` environment variable is no longer supported.
- gotcha Gitlint switched to `re.search` semantics instead of `re.match` for all rules. This means regexes that previously matched only at the start of a string might now match anywhere. Gitlint will print a warning if your rule regexes might need updating.
- gotcha Gitlint split into two packages (`gitlint` and `gitlint-core`) in v0.17.0. `gitlint-core` provides the core linting logic with looser dependency requirements, while `gitlint` is the CLI tool with pinned dependencies. Ensure you install the correct package for your use case.
- gotcha The `commit-msg` git hook was rewritten in Python (around v0.15.0). Existing users of the hook need to reinstall it to benefit from fixes and new features.
Install
-
pip install gitlint-core -
pip install gitlint
Imports
- Gitlint
from gitlint.gitlint import Gitlint
- GitlintConfig
from gitlint.options import GitlintConfig
- CommitRule
from gitlint.rules import CommitRule
Quickstart
from gitlint.gitlint import Gitlint
from gitlint.options import GitlintConfig
from gitlint.display import Display
commit_message = (
"feat: Add new feature\n\n" # Title
"This commit introduces a brand new feature to the application.\n\n"
"It includes several improvements and bug fixes related to the new functionality."
)
# Initialize GitlintConfig (can be customized, or defaults are used)
config = GitlintConfig()
# You can set options programmatically, e.g., config.max_line_length = 100
# Initialize Gitlint with a display (e.g., to print to stdout)
display = Display(config)
gitlint = Gitlint(config, display)
# Lint the commit message
violations = gitlint.lint(commit_msg_str=commit_message)
if violations:
print(f"Found {len(violations)} violations:")
for violation in violations:
print(f"- {violation.rule_id}: {violation.message} (line {violation.line_nr})")
else:
print("Commit message is valid!")