Gitlint

0.19.1 · active · verified Sun Apr 12

Gitlint is a highly configurable Python library and command-line tool designed to lint Git commit messages for style and compliance with established conventions (e.g., Conventional Commits). It helps enforce consistent commit history quality within development teams. The current version is 0.19.1, and it typically sees several releases per year, addressing bug fixes, new features, and Python version compatibility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `gitlint` programmatically to lint a commit message string. It initializes a `GitlintOptions` object for configuration, creates a `GitContext` from a provided message, and then uses `GitLinter` to find any violations. For typical CLI usage, simply running `gitlint` in a Git repository will lint the latest commit.

import os
from gitlint.lint import GitLinter
from gitlint.git import GitContext
from gitlint.options import GitlintOptions

# Example commit message to lint
commit_message = """feat: Add user authentication

This commit introduces user authentication functionality using OAuth2.0.
It includes new models for users and sessions, and updates the API routes.
"""

# Initialize options (optional, can be customized programmatically)
options = GitlintOptions()
# Example: Disable a specific rule (e.g., body-max-line-length for a longer line)
# options.disabled_rules = ["body-max-line-length"]

# Create a GitContext from the commit message string
# For real-world use in a git repository, you'd typically use:
# GitContext.from_prev_commit() or GitContext.from_staged_commit()
gitcontext = GitContext.from_commit_msg(commit_message)

# Initialize the linter with the options
linter = GitLinter(options)

# Lint the commit message
violations = linter.lint(gitcontext)

# Print any detected violations
if violations:
    print(f"Commit message has {len(violations)} violation(s):")
    for violation in violations:
        print(f"  - [{violation.rule_id}] {violation.message} (Line {violation.line_nr}, Col {violation.violating_line_idx})")
else:
    print("Commit message is clean and adheres to rules!")

view raw JSON →