|
1 | 1 | from typing import Any, List, Optional
|
2 |
| - |
3 | 2 | from gitlint.options import ListOption
|
4 | 3 | from gitlint.rules import CommitRule, RuleViolation
|
5 | 4 |
|
6 |
| - |
7 | 5 | class EndsWithDot(CommitRule):
|
| 6 | + """ |
| 7 | + Rule to ensure that the commit message title ends with a period ('.'). |
| 8 | + """ |
8 | 9 | name = "title-doesn't-end-with-dot"
|
9 | 10 | id = "ZT1"
|
10 | 11 |
|
11 | 12 | def validate(self, commit: Any) -> Optional[List[RuleViolation]]:
|
12 |
| - error = "Title does not end with a '.' character" |
13 | 13 | 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)] |
15 | 15 | return None
|
16 | 16 |
|
17 |
| - |
18 | 17 | 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 | + """ |
19 | 24 | name = "area-formatting"
|
20 | 25 | 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")] |
25 | 27 |
|
26 | 28 | def validate(self, commit: Any) -> Optional[List[RuleViolation]]:
|
27 |
| - title_components = commit.message.title.split(": ") |
28 |
| - |
| 29 | + title_parts = commit.message.title.split(": ") |
29 | 30 | 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 | + |
38 | 36 | 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