From 8d77c739cededa4c4993e90e80ccb0fba33d0289 Mon Sep 17 00:00:00 2001 From: Lorenzo Vagliano Date: Tue, 29 Oct 2024 12:52:35 +0100 Subject: [PATCH] feat(pre-commit): Add commit message linting Signed-off-by: Lorenzo Vagliano --- .gitlint | 12 ++++++++++ .pre-commit-config.yaml | 9 ++++++++ scripts/gitlint_rules/rules.py | 41 ++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 .gitlint create mode 100644 scripts/gitlint_rules/rules.py diff --git a/.gitlint b/.gitlint new file mode 100644 index 000000000..58b994946 --- /dev/null +++ b/.gitlint @@ -0,0 +1,12 @@ +[general] +ignore=body-is-missing +extra-path=./scripts/gitlint_rules/ +debug=true +verbosity=3 +contrib = contrib-body-requires-signed-off-by, contrib-title-conventional-commits + +[title-max-length] +line-length=50 + +[body-max-line-length] +line-length=72 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 073df732e..895a65bf7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -32,6 +32,15 @@ repos: args: ["--config=setup.cfg"] additional_dependencies: [flake8-isort] + - repo: https://github.com/jorisroovers/gitlint + rev: "v0.17.0" + hooks: + - id: gitlint + language: python + entry: gitlint + stages: [commit-msg] + args: [--msg-filename] + # sets up .pre-commit-ci.yaml to ensure pre-commit dependencies stay up to date ci: autoupdate_schedule: weekly diff --git a/scripts/gitlint_rules/rules.py b/scripts/gitlint_rules/rules.py new file mode 100644 index 000000000..5e6790c5a --- /dev/null +++ b/scripts/gitlint_rules/rules.py @@ -0,0 +1,41 @@ +# import re + +# from gitlint.rules import CommitRule, RuleViolation + +# EXAMPLE GITLINT CONFIGURATION + +# class SignedOffBy(CommitRule): +# """This rule will enforce that each commit contains a "Signed-off-by" line.""" + +# name = "body-requires-signed-off-by" +# id = "Workflows1" + +# def validate(self, commit): +# for line in commit.message.body: +# if line.startswith("Signed-off-by"): +# return + +# msg = "Body does not contain a 'Signed-Off-By' line" +# return [RuleViolation(self.id, msg, line_nr=1)] + + +# class ApprovedSubject(CommitRule): +# """Validate subject of each commit. + +# This rule will enforce that each commit starts with a "module: text" format. +# The 'module' can be any alphanumeric word, and the message must start with a colon followed by a space. +# """ + +# name = "approved-subject-in-title" +# id = "Workflows2" + +# MODULE_PATTERN = re.compile(r"^[a-zA-Z0-9_-]+: .+") + +# def validate(self, commit): +# title = commit.message.title + +# if not self.MODULE_PATTERN.match(title): +# msg = "Subject does not follow 'module: text' format" +# return [RuleViolation(self.id, msg, line_nr=1)] + +# return