Add lefthook hooks for commit message linting

What does this MR do and why?

Add commit-msg and pre-push lefthook hooks that lint commit messages using the Gitlab::Dangerfiles::CommitLinter class from the gitlab-dangerfiles gem — the same linter that Dangerbot runs in CI.

Currently, the commit message linting rules are only enforced by Dangerbot at CI time (via the danger-review job). This means developers don't get feedback until the MR pipeline runs. This MR adds local lefthook checks to provide earlier feedback:

  • commit-msg hook: Immediate feedback when creating a commit.
  • pre-push command: Blocks pushes containing commits with linting errors, preventing them from reaching the MR.

Both hooks share a single script (scripts/lint/lint_commits.rb) that uses CommitLinter directly, enforcing the exact same rules as Dangerbot (72-char line length, minimum 3 words, capitalization, no trailing period, no emoji, no short references, etc.) with zero duplicated logic.

The script auto-detects its mode based on arguments:

  • With a file path (commit-msg): lints the message from the file.
  • Without arguments (pre-push): lints all commits from merge-base to HEAD.

Both modes skip auto-generated commits (fixup!, squash!, merge, revert, suggestion).

References

Screenshots or screen recordings

Not applicable — no UI changes.

How to set up and validate locally

  1. Ensure lefthook is installed (lefthook install).
  2. Try creating a commit with a subject longer than 72 characters:
    git commit --allow-empty -m 'This is a very long commit message subject line that definitely exceeds seventy-two characters'
    The commit-msg hook should reject it with an error.
  3. Try creating a commit with a long body line:
    git commit --allow-empty -m 'Short subject line here' -m 'This is a very long body line that definitely exceeds seventy-two characters in the commit message body'
    The commit-msg hook should reject it with an error.
  4. Verify that fixup! commits are allowed regardless of length:
    git commit --allow-empty -m 'fixup! This is a very long fixup commit message subject line that definitely exceeds seventy-two characters'
    The hook should allow it.
  5. Verify that URL-only body lines are allowed:
    git commit --allow-empty -m 'A valid subject line' -m 'https://docs.gitlab.com/development/contributing/merge_request_workflow/#commit-messages-guidelines-which-is-a-very-long-url'
    The hook should allow it.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Pedro Pombeiro

Merge request reports

Loading