Skip to content

Commit ce4cd52

Browse files
committed
This version keeps the logic the same but improves readability with structured comments and simplified condition checks.
1 parent 23a773c commit ce4cd52

File tree

1 file changed

+29
-43
lines changed

1 file changed

+29
-43
lines changed

tools/gitlint-extra-rules.py

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,48 @@
11
from typing import Any, List, Optional
2-
32
from gitlint.options import ListOption
43
from gitlint.rules import CommitRule, RuleViolation
54

6-
75
class EndsWithDot(CommitRule):
6+
"""
7+
Rule to ensure that the commit message title ends with a period ('.').
8+
"""
89
name = "title-doesn't-end-with-dot"
910
id = "ZT1"
1011

1112
def validate(self, commit: Any) -> Optional[List[RuleViolation]]:
12-
error = "Title does not end with a '.' character"
1313
if not commit.message.title.endswith("."):
14-
return [RuleViolation(self.id, error, line_nr=1)]
14+
return [RuleViolation(self.id, "Title must end with a '.'", line_nr=1)]
1515
return None
1616

17-
1817
class AreaFormatting(CommitRule):
18+
"""
19+
Rule to enforce a structured format for commit message titles:
20+
- The title must start with an area (lowercase, followed by ': ')
21+
- Certain exclusions (e.g., 'WIP') are allowed in uppercase.
22+
- The summary (after the colon) must start with an uppercase letter.
23+
"""
1924
name = "area-formatting"
2025
id = "ZT2"
21-
22-
options_spec = [
23-
ListOption("exclusions", ["WIP"], "Exclusions to area lower-case rule")
24-
]
26+
options_spec = [ListOption("exclusions", ["WIP"], "Allowed uppercase exclusions")]
2527

2628
def validate(self, commit: Any) -> Optional[List[RuleViolation]]:
27-
title_components = commit.message.title.split(": ")
28-
29+
title_parts = commit.message.title.split(": ")
2930
violations = []
30-
31-
# Return just this violation, since latter checks assume an area
32-
error = (
33-
"Title should start with at least one area, followed by a colon and space"
34-
)
35-
if len(title_components) < 2:
36-
return [RuleViolation(self.id, error, line_nr=1)]
37-
31+
32+
# Ensure the title contains at least an area and a summary
33+
if len(title_parts) < 2:
34+
return [RuleViolation(self.id, "Title must start with an area, followed by ': '", line_nr=1)]
35+
3836
exclusions = self.options["exclusions"].value
39-
exclusions_text = ", or ".join(exclusions)
40-
if exclusions_text:
41-
exclusions_text = f" (or {exclusions_text})"
42-
error = (
43-
f"Areas at start of title should be lower case{exclusions_text}, "
44-
"followed by ': '"
45-
)
46-
47-
def deny_capital_text(text: str) -> bool:
48-
if text in exclusions:
49-
return False
50-
if not text.islower():
51-
return True
52-
return False
53-
54-
for area in title_components[:-1]:
55-
if any(deny_capital_text(word) for word in area.split("/")) or " " in area:
56-
violations += [RuleViolation(self.id, error, line_nr=1)]
57-
58-
error = "Summary of change, after area(s), should be capitalized"
59-
if not title_components[-1][0].isupper():
60-
violations += [RuleViolation(self.id, error, line_nr=1)]
61-
62-
return violations
37+
area_part = title_parts[0]
38+
summary_part = title_parts[1]
39+
40+
# Check if the area part is correctly formatted
41+
if area_part not in exclusions and not area_part.islower():
42+
violations.append(RuleViolation(self.id, "Area must be lowercase unless excluded", line_nr=1))
43+
44+
# Ensure the summary starts with an uppercase letter
45+
if not summary_part[0].isupper():
46+
violations.append(RuleViolation(self.id, "Summary must start with an uppercase letter", line_nr=1))
47+
48+
return violations if violations else None

0 commit comments

Comments
 (0)