summaryrefslogtreecommitdiffstats
path: root/gitlint-core/gitlint/contrib/rules
diff options
context:
space:
mode:
Diffstat (limited to 'gitlint-core/gitlint/contrib/rules')
-rw-r--r--gitlint-core/gitlint/contrib/rules/__init__.py0
-rw-r--r--gitlint-core/gitlint/contrib/rules/authors_commit.py45
-rw-r--r--gitlint-core/gitlint/contrib/rules/conventional_commit.py37
-rw-r--r--gitlint-core/gitlint/contrib/rules/disallow_cleanup_commits.py22
-rw-r--r--gitlint-core/gitlint/contrib/rules/signedoff_by.py17
5 files changed, 121 insertions, 0 deletions
diff --git a/gitlint-core/gitlint/contrib/rules/__init__.py b/gitlint-core/gitlint/contrib/rules/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gitlint-core/gitlint/contrib/rules/__init__.py
diff --git a/gitlint-core/gitlint/contrib/rules/authors_commit.py b/gitlint-core/gitlint/contrib/rules/authors_commit.py
new file mode 100644
index 0000000..5c4a150
--- /dev/null
+++ b/gitlint-core/gitlint/contrib/rules/authors_commit.py
@@ -0,0 +1,45 @@
+import re
+from pathlib import Path
+from typing import Tuple
+
+from gitlint.rules import CommitRule, RuleViolation
+
+
+class AllowedAuthors(CommitRule):
+ """Enforce that only authors listed in the AUTHORS file are allowed to commit."""
+
+ authors_file_names = ("AUTHORS", "AUTHORS.txt", "AUTHORS.md")
+ parse_authors = re.compile(r"^(?P<name>.*) <(?P<email>.*)>$", re.MULTILINE)
+
+ name = "contrib-allowed-authors"
+
+ id = "CC3"
+
+ @classmethod
+ def _read_authors_from_file(cls, git_ctx) -> Tuple[str, str]:
+ for file_name in cls.authors_file_names:
+ path = Path(git_ctx.repository_path) / file_name
+ if path.exists():
+ authors_file = path
+ break
+ else:
+ raise FileNotFoundError("No AUTHORS file found!")
+
+ authors_file_content = authors_file.read_text("utf-8")
+ authors = re.findall(cls.parse_authors, authors_file_content)
+
+ return set(authors), authors_file.name
+
+ def validate(self, commit):
+ registered_authors, authors_file_name = AllowedAuthors._read_authors_from_file(commit.message.context)
+
+ author = (commit.author_name, commit.author_email.lower())
+
+ if author not in registered_authors:
+ return [
+ RuleViolation(
+ self.id,
+ f"Author not in '{authors_file_name}' file: " f'"{commit.author_name} <{commit.author_email}>"',
+ )
+ ]
+ return []
diff --git a/gitlint-core/gitlint/contrib/rules/conventional_commit.py b/gitlint-core/gitlint/contrib/rules/conventional_commit.py
new file mode 100644
index 0000000..705b083
--- /dev/null
+++ b/gitlint-core/gitlint/contrib/rules/conventional_commit.py
@@ -0,0 +1,37 @@
+import re
+
+from gitlint.options import ListOption
+from gitlint.rules import CommitMessageTitle, LineRule, RuleViolation
+
+RULE_REGEX = re.compile(r"([^(]+?)(\([^)]+?\))?!?: .+")
+
+
+class ConventionalCommit(LineRule):
+ """This rule enforces the spec at https://www.conventionalcommits.org/."""
+
+ name = "contrib-title-conventional-commits"
+ id = "CT1"
+ target = CommitMessageTitle
+
+ options_spec = [
+ ListOption(
+ "types",
+ ["fix", "feat", "chore", "docs", "style", "refactor", "perf", "test", "revert", "ci", "build"],
+ "Comma separated list of allowed commit types.",
+ )
+ ]
+
+ def validate(self, line, _commit):
+ violations = []
+ match = RULE_REGEX.match(line)
+
+ if not match:
+ msg = "Title does not follow ConventionalCommits.org format 'type(optional-scope): description'"
+ violations.append(RuleViolation(self.id, msg, line))
+ else:
+ line_commit_type = match.group(1)
+ if line_commit_type not in self.options["types"].value:
+ opt_str = ", ".join(self.options["types"].value)
+ violations.append(RuleViolation(self.id, f"Title does not start with one of {opt_str}", line))
+
+ return violations
diff --git a/gitlint-core/gitlint/contrib/rules/disallow_cleanup_commits.py b/gitlint-core/gitlint/contrib/rules/disallow_cleanup_commits.py
new file mode 100644
index 0000000..7f62dee
--- /dev/null
+++ b/gitlint-core/gitlint/contrib/rules/disallow_cleanup_commits.py
@@ -0,0 +1,22 @@
+from gitlint.rules import CommitRule, RuleViolation
+
+
+class DisallowCleanupCommits(CommitRule):
+ """This rule checks the commits for "fixup!"/"squash!"/"amend!" commits
+ and rejects them.
+ """
+
+ name = "contrib-disallow-cleanup-commits"
+ id = "CC2"
+
+ def validate(self, commit):
+ if commit.is_fixup_commit:
+ return [RuleViolation(self.id, "Fixup commits are not allowed", line_nr=1)]
+
+ if commit.is_squash_commit:
+ return [RuleViolation(self.id, "Squash commits are not allowed", line_nr=1)]
+
+ if commit.is_fixup_amend_commit:
+ return [RuleViolation(self.id, "Amend commits are not allowed", line_nr=1)]
+
+ return []
diff --git a/gitlint-core/gitlint/contrib/rules/signedoff_by.py b/gitlint-core/gitlint/contrib/rules/signedoff_by.py
new file mode 100644
index 0000000..5ea8217
--- /dev/null
+++ b/gitlint-core/gitlint/contrib/rules/signedoff_by.py
@@ -0,0 +1,17 @@
+from gitlint.rules import CommitRule, RuleViolation
+
+
+class SignedOffBy(CommitRule):
+ """This rule will enforce that each commit body contains a "Signed-off-by" line.
+ We keep things simple here and just check whether the commit body contains a line that starts with "Signed-off-by".
+ """
+
+ name = "contrib-body-requires-signed-off-by"
+ id = "CC1"
+
+ def validate(self, commit):
+ for line in commit.message.body:
+ if line.lower().startswith("signed-off-by"):
+ return []
+
+ return [RuleViolation(self.id, "Body does not contain a 'Signed-off-by' line", line_nr=1)]