Repository navigation
Expand file tree
/
Copy pathrules_catalog.py
More file actions
316 lines (293 loc) · 11.3 KB
/
Copy pathrules_catalog.py
File metadata and controls
316 lines (293 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""Centralized catalog of all commit-check rules, regexes, and error messages.
Every user-facing rule has a **stable rule ID** (e.g. ``CC003``) that never
changes once released. Rule IDs give users a durable handle to reference in
documentation, error output, and machine-readable results.
ID ranges
---------
========= ==================================
``CC0xx`` Commit message rules
``CC1xx`` Author (name / email) rules
``CC2xx`` Branch rules
``CC3xx`` Push and file rules
``CC4xx`` Tag rules
========= ==================================
Internal bookkeeping entries that never produce a diagnostic (such as
``ignore_authors``) intentionally have no rule ID.
"""
from __future__ import annotations
from dataclasses import dataclass
#: Base URL of the rules reference documentation.
RULES_DOCS_URL = "https://commit-check.com/rules/"
def display_name(check: str) -> str:
"""Human-readable form of a check name, e.g. ``subject-imperative``.
Config files and the JSON output carry the snake_case key, because that is
what a reader sets in ``cchk.toml`` and what a consumer maps back to an
option. Text written for a person uses the kebab-case form instead: it is
how the rules reference titles each rule, so a name printed to a terminal
can be searched for there verbatim.
Every text surface goes through here so the two forms cannot drift apart
again — the compact output once printed the config key while the default
output printed this one.
"""
return check.replace("_", "-")
@dataclass(frozen=True)
class RuleCatalogEntry:
check: str
regex: str | None = None
error: str | None = None
suggest: str | None = None
rule_id: str | None = None
# The specification the rule enforces. ``error`` must mention ``spec_name``
# and end with ``. See {spec_url}``: terminals with hyperlink support link
# the name and drop the trailing address, everything else prints ``error``
# verbatim so the URL still reaches CI logs and pipes.
spec_name: str | None = None
spec_url: str | None = None
@property
def name(self) -> str:
"""Human-readable rule name, e.g. ``subject-imperative``."""
return display_name(self.check)
@property
def docs_url(self) -> str | None:
"""Link to this rule's section in the rules reference, if it has an ID."""
if not self.rule_id:
return None
return f"{RULES_DOCS_URL}#{self.rule_id.lower()}"
# Commit message rules
COMMIT_RULES = [
RuleCatalogEntry(
rule_id="CC001",
check="message",
regex=None, # Built dynamically from config
error="The commit message should follow Conventional Commits. See https://www.conventionalcommits.org",
suggest="Use <type>(<scope>): <description> with allowed types",
spec_name="Conventional Commits",
spec_url="https://www.conventionalcommits.org",
),
RuleCatalogEntry(
rule_id="CC002",
check="subject_capitalized",
regex=None,
error="Subject must start with a capital letter",
suggest="Capitalize the first word of the subject",
),
RuleCatalogEntry(
rule_id="CC003",
check="subject_imperative",
regex=None,
error="Commit message should use imperative mood (e.g., 'fix bug' not 'fixed bug', 'add feature' not 'adding feature')",
suggest="Change the first verb to imperative form, e.g., 'fix' instead of 'fixed'/'fixes'/'fixing'",
),
RuleCatalogEntry(
rule_id="CC004",
check="subject_max_length",
regex=None,
error="Subject must be at most {max_len} characters",
suggest="Shorten the subject to {max_len} characters or fewer",
),
RuleCatalogEntry(
rule_id="CC005",
check="subject_min_length",
regex=None,
error="Subject must be at least {min_len} characters",
suggest="Write a subject of at least {min_len} characters",
),
RuleCatalogEntry(
rule_id="CC006",
check="allow_merge_commits",
regex=None,
error="Merge commits are not allowed",
suggest="Rebase or squash your changes instead of merging",
),
RuleCatalogEntry(
rule_id="CC007",
check="allow_revert_commits",
regex=None,
error="Revert commits are not allowed",
suggest="Avoid using 'revert' commits; rewrite history if necessary",
),
RuleCatalogEntry(
rule_id="CC008",
check="allow_empty_commits",
regex=None,
error="Empty commit messages are not allowed",
suggest="Provide a non-empty subject",
),
RuleCatalogEntry(
rule_id="CC009",
check="allow_fixup_commits",
regex=None,
error="Fixup commits are not allowed",
suggest="Use interactive rebase to clean up fixup commits",
),
RuleCatalogEntry(
rule_id="CC010",
check="allow_wip_commits",
regex=None,
error="WIP commits are not allowed",
suggest="Complete the work before committing or remove 'WIP'",
),
RuleCatalogEntry(
rule_id="CC011",
check="require_body",
regex=None,
error="Commit body is required",
suggest="Add a body explaining the change",
),
RuleCatalogEntry(
rule_id="CC101",
check="author_name",
# A name starts with a letter of any script (``\w`` is Unicode-aware
# in Python's ``re``) and continues with letters, digits, spaces and
# the punctuation names carry. U+0300-036F are combining diacritics,
# so a name typed in decomposed form (macOS does this) still counts
# as letters. The alternative admits bracketed bot accounts.
regex=r"^[^\W\d_][\w ,.'\-\u0300-\u036F]*$|.*(\[bot])",
error="The committer name seems invalid",
suggest="git config user.name 'Your Name'",
),
RuleCatalogEntry(
rule_id="CC102",
check="author_email",
regex=r"^.+@.+$",
error="The committer's email seems invalid",
suggest="git config user.email yourname@example.com",
),
RuleCatalogEntry(
# Internal bookkeeping entry - never produces a diagnostic.
check="ignore_authors",
regex=None,
error=None,
suggest=None,
),
RuleCatalogEntry(
rule_id="CC012",
check="require_signed_off_by",
regex=r"Signed-off-by: .+ <.+@.+>",
error="Signed-off-by trailer not found in the latest commit",
suggest="git commit --amend --signoff or use --signoff on commit",
),
RuleCatalogEntry(
rule_id="CC013",
check="ai_attribution",
regex=None,
error="AI attribution policy violation",
suggest="This project does not accept AI attribution in commit messages. Remove AI trailers and re-commit.",
),
# The three conditions of ai_attribution = "disclose", one rule each so a
# project can warn on one while enforcing the others. ``{trailer}`` is
# the first accepted disclosure trailer, filled in by the rule builder.
RuleCatalogEntry(
rule_id="CC014",
check="ai_disclosure",
regex=None,
error="AI assistance is not disclosed with an accepted trailer",
suggest="Disclose the AI tool with a {trailer}: trailer",
),
RuleCatalogEntry(
rule_id="CC015",
check="ai_co_author",
regex=None,
error="An AI tool is credited as a co-author",
suggest="Disclose the tool with a {trailer}: trailer instead of crediting it as a co-author",
),
RuleCatalogEntry(
rule_id="CC016",
check="ai_signoff",
regex=None,
error="An AI tool signed off the commit",
suggest="Only a person can certify the Developer Certificate of Origin: remove the AI Signed-off-by line and sign off yourself (git commit --signoff)",
),
]
# Push rules
PUSH_RULES = [
RuleCatalogEntry(
rule_id="CC301",
check="no_force_push",
regex=None,
error="Force push is not allowed",
suggest="Use a normal push instead of --force or --force-with-lease",
),
]
# File rules: metadata about the files a commit touches (path, size) —
# deliberately never their contents.
FILES_RULES = [
RuleCatalogEntry(
rule_id="CC302",
check="file_size",
regex=None,
error="File exceeds the maximum size of {max_size}",
suggest="Keep files under {max_size}, use Git LFS for large assets, or raise max_size in the [files] config",
),
RuleCatalogEntry(
rule_id="CC303",
check="file_pattern",
regex=None,
error="File path matches a prohibited pattern",
suggest="Remove the file from the commit (rotate any committed secrets), or adjust prohibited_patterns in the [files] config",
),
RuleCatalogEntry(
rule_id="CC304",
check="path_length",
regex=None,
error="File path exceeds {max_len} characters",
suggest="Shorten the path to {max_len} characters or fewer",
),
]
# Branch rules
BRANCH_RULES = [
RuleCatalogEntry(
rule_id="CC201",
check="branch",
regex=None, # Built dynamically from config
error="The branch should follow Conventional Branch. See https://conventionalbranch.org",
suggest="Use <type>/<description> with an allowed type, or add the branch to allow_branch_names in config",
spec_name="Conventional Branch",
spec_url="https://conventionalbranch.org",
),
RuleCatalogEntry(
rule_id="CC202",
check="merge_base",
regex=None, # Provided by config
error="Current branch is not rebased onto target branch",
suggest="Rebase or merge with the target branch",
),
RuleCatalogEntry(
# Internal bookkeeping entry - never produces a diagnostic.
check="ignore_authors",
regex=None,
error=None,
suggest=None,
),
]
# Tag rules
TAG_RULES = [
RuleCatalogEntry(
rule_id="CC401",
check="tag",
regex=None, # Provided by config, defaulting to SemVer with optional v
error="The tag name does not match the required pattern",
suggest="Use a SemVer tag like v1.2.3, or set a custom pattern via regex in the [tag] config section",
),
]
#: All catalog entries that represent a user-facing, documented rule.
ALL_RULES = [
entry
for entry in (*COMMIT_RULES, *BRANCH_RULES, *PUSH_RULES, *FILES_RULES, *TAG_RULES)
if entry.rule_id is not None
]
#: Lookup from check name to its catalog entry, for rules that have an ID.
#: Rule identity lives only here, so built rules can never carry a stale copy.
RULES_BY_CHECK = {entry.check: entry for entry in ALL_RULES}
#: The checks each CLI flag or API function requests. The catalog is the only
#: place that knows which checks exist, so the grouping lives beside it rather
#: than as a list copied into ``main.py`` and ``api.py`` that could drift.
#: ``--message`` runs every commit rule except the author ones (they have
#: their own flags) and the ``ignore_authors`` bookkeeping entry.
MESSAGE_CHECKS: frozenset[str] = frozenset(
entry.check
for entry in COMMIT_RULES
if entry.check not in {"author_name", "author_email", "ignore_authors"}
)
BRANCH_CHECKS: frozenset[str] = frozenset({"branch", "merge_base"})
FILES_CHECKS: frozenset[str] = frozenset(entry.check for entry in FILES_RULES)