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-msghook: Immediate feedback when creating a commit. -
pre-pushcommand: 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
- Discussed in Slack about making commit message linting a blocking lefthook check (Dangerbot would still catch commits from Web IDE)
-
gitlab-dangerfilesCommitLinter— the linter class used by the new script -
commit_messagesDanger rule — the CI-side rule we're replicating locally - Commit messages guidelines
Screenshots or screen recordings
Not applicable — no UI changes.
How to set up and validate locally
- Ensure lefthook is installed (
lefthook install). - Try creating a commit with a subject longer than 72 characters:
The
git commit --allow-empty -m 'This is a very long commit message subject line that definitely exceeds seventy-two characters'commit-msghook should reject it with an error. - Try creating a commit with a long body line:
The
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'commit-msghook should reject it with an error. - Verify that
fixup!commits are allowed regardless of length:The hook should allow it.git commit --allow-empty -m 'fixup! This is a very long fixup commit message subject line that definitely exceeds seventy-two characters' - Verify that URL-only body lines are allowed:
The hook should allow it.
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'
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.