commit-check 2.23.0__py3-none-any.whl → 2.24.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- commit_check/__init__.py +11 -0
- commit_check/api.py +62 -36
- commit_check/config.py +44 -11
- commit_check/config_merger.py +63 -23
- commit_check/engine.py +308 -19
- commit_check/main.py +64 -10
- commit_check/rule_builder.py +26 -14
- commit_check/rules_catalog.py +13 -2
- commit_check/util.py +63 -10
- {commit_check-2.23.0.dist-info → commit_check-2.24.0.dist-info}/METADATA +31 -12
- commit_check-2.24.0.dist-info/RECORD +23 -0
- commit_check-2.23.0.dist-info/RECORD +0 -23
- {commit_check-2.23.0.dist-info → commit_check-2.24.0.dist-info}/WHEEL +0 -0
- {commit_check-2.23.0.dist-info → commit_check-2.24.0.dist-info}/entry_points.txt +0 -0
- {commit_check-2.23.0.dist-info → commit_check-2.24.0.dist-info}/licenses/LICENSE +0 -0
- {commit_check-2.23.0.dist-info → commit_check-2.24.0.dist-info}/top_level.txt +0 -0
commit_check/__init__.py
CHANGED
|
@@ -65,6 +65,10 @@ DEFAULT_COMMIT_TYPES = [
|
|
|
65
65
|
"perf",
|
|
66
66
|
"build",
|
|
67
67
|
"ci",
|
|
68
|
+
# The type the Conventional Commits FAQ recommends for a revert written
|
|
69
|
+
# by hand ("revert: ..."), also in commitlint's config-conventional.
|
|
70
|
+
# v1 accepted it; git's own 'Revert "..."' subjects are a separate case.
|
|
71
|
+
"revert",
|
|
68
72
|
]
|
|
69
73
|
# Follow conventional branch (https://conventionalbranch.org/)
|
|
70
74
|
# Includes AI agent prefixes (spec v1.1.0) and bot prefixes
|
|
@@ -131,6 +135,13 @@ GIT_MERGE_SUBJECT_PATTERN = (
|
|
|
131
135
|
r"|[a-z][a-z0-9+.-]*://|[\w.-]+@[\w.-]+:)"
|
|
132
136
|
)
|
|
133
137
|
|
|
138
|
+
# The subjects `git revert` writes: 'Revert "<subject>"', and since git 2.43
|
|
139
|
+
# 'Reapply "<subject>"' when the commit reverted is itself a revert. GitHub's
|
|
140
|
+
# and GitLab's revert buttons write the same 'Revert "' subject. Their length
|
|
141
|
+
# and format are git's doing, not the author's; CC007 alone decides whether
|
|
142
|
+
# they are allowed. Unanchored, like GIT_MERGE_SUBJECT_PATTERN.
|
|
143
|
+
GIT_REVERT_SUBJECT_PATTERN = r'(?:Revert|Reapply) "'
|
|
144
|
+
|
|
134
145
|
# Push-related defaults
|
|
135
146
|
DEFAULT_PUSH_RULES = {
|
|
136
147
|
"allow_force_push": True,
|
commit_check/api.py
CHANGED
|
@@ -21,6 +21,13 @@ Return-value schema (all functions)::
|
|
|
21
21
|
{
|
|
22
22
|
"status": "pass" | "fail" | "skip",
|
|
23
23
|
"warnings": <number of checks with status "warn">,
|
|
24
|
+
"notices": [
|
|
25
|
+
{
|
|
26
|
+
"setting": "<the setting that is not in force, e.g. [files] max_size>",
|
|
27
|
+
"message": "<what was wrong with it, as the CLI prints it>",
|
|
28
|
+
},
|
|
29
|
+
...
|
|
30
|
+
],
|
|
24
31
|
"checks": [
|
|
25
32
|
{
|
|
26
33
|
"check": "<rule name>",
|
|
@@ -39,9 +46,16 @@ Return-value schema (all functions)::
|
|
|
39
46
|
list, or there was nothing to check. It is deliberately not ``"pass"``: a
|
|
40
47
|
skipped rule validated nothing, and collapsing the two makes a bypassed
|
|
41
48
|
policy indistinguishable from an enforced one. The top-level ``status`` is
|
|
42
|
-
``"skip"`` only when *every* check skipped
|
|
43
|
-
reports ``"pass"`` or ``"fail"`` as before. Only
|
|
44
|
-
code branching on ``status == "fail"`` keeps
|
|
49
|
+
``"skip"`` only when *every* check skipped, or when no check ran at all; a
|
|
50
|
+
run with any real verdict reports ``"pass"`` or ``"fail"`` as before. Only
|
|
51
|
+
``"fail"`` is an error, so code branching on ``status == "fail"`` keeps
|
|
52
|
+
working unchanged.
|
|
53
|
+
|
|
54
|
+
``notices`` lists the settings in the ``config`` dict that are not in force,
|
|
55
|
+
such as a ``[files]`` value that cannot be used and so turns its rule off; it
|
|
56
|
+
is an empty list when there are none. The dict's keys are not checked
|
|
57
|
+
against the known settings, as the CLI checks a config file's: an unknown
|
|
58
|
+
key in it is ignored without a notice.
|
|
45
59
|
"""
|
|
46
60
|
|
|
47
61
|
from __future__ import annotations
|
|
@@ -66,28 +80,52 @@ from commit_check.rules_catalog import BRANCH_CHECKS, MESSAGE_CHECKS
|
|
|
66
80
|
# ---------------------------------------------------------------------------
|
|
67
81
|
|
|
68
82
|
|
|
69
|
-
def _build_result(
|
|
83
|
+
def _build_result(
|
|
84
|
+
outcomes: list[CheckOutcome], notices: list[dict[str, str]]
|
|
85
|
+
) -> dict[str, Any]:
|
|
70
86
|
"""Convert a list of :class:`~commit_check.engine.CheckOutcome` into the
|
|
71
87
|
public return-value dict."""
|
|
72
88
|
return {
|
|
73
89
|
"status": overall_status(o.status for o in outcomes),
|
|
74
90
|
"warnings": count_warnings(o.status for o in outcomes),
|
|
91
|
+
"notices": notices,
|
|
75
92
|
"checks": [o.to_dict() for o in outcomes],
|
|
76
93
|
}
|
|
77
94
|
|
|
78
95
|
|
|
96
|
+
def _combine(results: list[dict[str, Any]]) -> dict[str, Any]:
|
|
97
|
+
"""One result from several, for the calls that run more than one pass.
|
|
98
|
+
|
|
99
|
+
Each pass builds its rules from the same config, so a notice about it
|
|
100
|
+
comes back from every pass; it is listed once.
|
|
101
|
+
"""
|
|
102
|
+
checks = [c for r in results for c in r["checks"]]
|
|
103
|
+
notices: list[dict[str, str]] = []
|
|
104
|
+
for r in results:
|
|
105
|
+
notices.extend(n for n in r["notices"] if n not in notices)
|
|
106
|
+
# Shared reducer, not a local "fail or else pass": a combined call in
|
|
107
|
+
# which every nested check skipped is still a skip.
|
|
108
|
+
return {
|
|
109
|
+
"status": overall_status(c["status"] for c in checks),
|
|
110
|
+
"warnings": count_warnings(c["status"] for c in checks),
|
|
111
|
+
"notices": notices,
|
|
112
|
+
"checks": checks,
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
|
|
79
116
|
def _run_checks(
|
|
80
117
|
check_names: Collection[str],
|
|
81
118
|
context: ValidationContext,
|
|
82
119
|
config: dict[str, Any],
|
|
83
120
|
) -> dict[str, Any]:
|
|
84
121
|
"""Build rules, filter to *check_names*, run the engine, return result."""
|
|
85
|
-
|
|
122
|
+
notices: list[dict[str, str]] = []
|
|
123
|
+
rule_builder = RuleBuilder(config, notices)
|
|
86
124
|
all_rules = rule_builder.build_all_rules()
|
|
87
125
|
filtered = [r for r in all_rules if r.check in check_names]
|
|
88
126
|
engine = ValidationEngine(filtered)
|
|
89
127
|
outcomes = engine.validate_all_detailed(context)
|
|
90
|
-
return _build_result(outcomes)
|
|
128
|
+
return _build_result(outcomes, notices)
|
|
91
129
|
|
|
92
130
|
|
|
93
131
|
def _merge_config(user_config: dict[str, Any] | None) -> dict[str, Any]:
|
|
@@ -116,8 +154,8 @@ def validate_message(
|
|
|
116
154
|
override only the keys you care about, e.g.
|
|
117
155
|
``{"commit": {"allow_commit_types": ["feat", "fix"]}}``.
|
|
118
156
|
:returns: A dict with ``"status"`` (``"pass"``/``"fail"``/``"skip"``), a
|
|
119
|
-
``"warnings"`` count
|
|
120
|
-
(list of per-rule outcomes).
|
|
157
|
+
``"warnings"`` count, ``"notices"`` (settings not in force) and
|
|
158
|
+
``"checks"`` (list of per-rule outcomes).
|
|
121
159
|
|
|
122
160
|
Example::
|
|
123
161
|
|
|
@@ -142,7 +180,8 @@ def validate_branch(
|
|
|
142
180
|
:param branch: Branch name to validate. If *None*, the current git branch
|
|
143
181
|
is used (via ``git branch --show-current``).
|
|
144
182
|
:param config: Optional configuration override dict.
|
|
145
|
-
:returns: A dict with ``"status"``, a ``"warnings"`` count
|
|
183
|
+
:returns: A dict with ``"status"``, a ``"warnings"`` count, ``"notices"``
|
|
184
|
+
and ``"checks"``.
|
|
146
185
|
|
|
147
186
|
Example::
|
|
148
187
|
|
|
@@ -175,7 +214,8 @@ def validate_tag(
|
|
|
175
214
|
:param config: Optional configuration override dict. The pattern comes
|
|
176
215
|
from ``config["tag"]["regex"]`` and defaults to SemVer with an
|
|
177
216
|
optional leading ``v`` (``v1.2.3`` or ``1.2.3``).
|
|
178
|
-
:returns: A dict with ``"status"``, a ``"warnings"`` count
|
|
217
|
+
:returns: A dict with ``"status"``, a ``"warnings"`` count, ``"notices"``
|
|
218
|
+
and ``"checks"``.
|
|
179
219
|
|
|
180
220
|
Example::
|
|
181
221
|
|
|
@@ -213,7 +253,7 @@ def validate_push(
|
|
|
213
253
|
the result unless ``no_force_push`` is listed under ``warn``, in which
|
|
214
254
|
case the check reports ``"warn"`` and the result passes.
|
|
215
255
|
:returns: A dict with ``"status"`` (``"pass"``/``"fail"``/``"skip"``), a
|
|
216
|
-
``"warnings"`` count and ``"checks"``.
|
|
256
|
+
``"warnings"`` count, ``"notices"`` and ``"checks"``.
|
|
217
257
|
|
|
218
258
|
Example::
|
|
219
259
|
|
|
@@ -247,7 +287,8 @@ def validate_author(
|
|
|
247
287
|
:param email: Author email to validate. If *None*, the value from
|
|
248
288
|
``git config user.email`` is used.
|
|
249
289
|
:param config: Optional configuration override dict.
|
|
250
|
-
:returns: A dict with ``"status"``, a ``"warnings"`` count
|
|
290
|
+
:returns: A dict with ``"status"``, a ``"warnings"`` count, ``"notices"``
|
|
291
|
+
and ``"checks"``.
|
|
251
292
|
|
|
252
293
|
Example::
|
|
253
294
|
|
|
@@ -279,15 +320,7 @@ def validate_author(
|
|
|
279
320
|
ValidationContext(stdin_text=email.strip(), config=cfg),
|
|
280
321
|
cfg,
|
|
281
322
|
)
|
|
282
|
-
|
|
283
|
-
# Shared reducer, not a local "fail or else pass": a combined call
|
|
284
|
-
# in which every nested check skipped is still a skip.
|
|
285
|
-
overall = overall_status(c["status"] for c in all_checks)
|
|
286
|
-
return {
|
|
287
|
-
"status": overall,
|
|
288
|
-
"warnings": count_warnings(c["status"] for c in all_checks),
|
|
289
|
-
"checks": all_checks,
|
|
290
|
-
}
|
|
323
|
+
return _combine([name_result, email_result])
|
|
291
324
|
|
|
292
325
|
stdin = None
|
|
293
326
|
if name is not None:
|
|
@@ -317,8 +350,9 @@ def validate_all(
|
|
|
317
350
|
:param author_name: Author name to validate, or *None* to skip.
|
|
318
351
|
:param author_email: Author email to validate, or *None* to skip.
|
|
319
352
|
:param config: Optional configuration override dict.
|
|
320
|
-
:returns: A dict with ``"status"``, a ``"warnings"`` count
|
|
321
|
-
combining all requested validations.
|
|
353
|
+
:returns: A dict with ``"status"``, a ``"warnings"`` count, ``"notices"``
|
|
354
|
+
and ``"checks"``, combining all requested validations. A notice that
|
|
355
|
+
more than one of them raised is listed once.
|
|
322
356
|
|
|
323
357
|
Example::
|
|
324
358
|
|
|
@@ -330,23 +364,15 @@ def validate_all(
|
|
|
330
364
|
>>> result["status"]
|
|
331
365
|
'pass'
|
|
332
366
|
"""
|
|
333
|
-
|
|
367
|
+
results: list[dict[str, Any]] = []
|
|
334
368
|
|
|
335
369
|
if message is not None:
|
|
336
|
-
|
|
337
|
-
all_checks.extend(msg_result["checks"])
|
|
370
|
+
results.append(validate_message(message, config=config))
|
|
338
371
|
|
|
339
372
|
if branch is not None:
|
|
340
|
-
|
|
341
|
-
all_checks.extend(branch_result["checks"])
|
|
373
|
+
results.append(validate_branch(branch, config=config))
|
|
342
374
|
|
|
343
375
|
if author_name is not None or author_email is not None:
|
|
344
|
-
|
|
345
|
-
all_checks.extend(author_result["checks"])
|
|
376
|
+
results.append(validate_author(author_name, author_email, config=config))
|
|
346
377
|
|
|
347
|
-
|
|
348
|
-
return {
|
|
349
|
-
"status": overall,
|
|
350
|
-
"warnings": count_warnings(c["status"] for c in all_checks),
|
|
351
|
-
"checks": all_checks,
|
|
352
|
-
}
|
|
378
|
+
return _combine(results)
|
commit_check/config.py
CHANGED
|
@@ -36,6 +36,29 @@ class ConfigError(ValueError):
|
|
|
36
36
|
self.setting = setting
|
|
37
37
|
|
|
38
38
|
|
|
39
|
+
def report_notice(
|
|
40
|
+
notices: list[dict[str, str]] | None,
|
|
41
|
+
setting: str,
|
|
42
|
+
message: str,
|
|
43
|
+
mark: str = "⊘",
|
|
44
|
+
) -> None:
|
|
45
|
+
"""Say that a setting is not in force, on stderr and in *notices*.
|
|
46
|
+
|
|
47
|
+
The line on stderr is what a person reads in a terminal; *notices* is
|
|
48
|
+
what reaches the result, ``--format json`` and the Python API, where a
|
|
49
|
+
line printed beside the output is lost. Each entry is
|
|
50
|
+
``{"setting": ..., "message": ...}``: *setting* names what is not in
|
|
51
|
+
force, as the CLI already writes it (``[files] max_size``,
|
|
52
|
+
``inherit_from``, a ``CCHK_*`` variable, ``--files``), and *message* is
|
|
53
|
+
the stderr line without its leading *mark*.
|
|
54
|
+
|
|
55
|
+
:param notices: The list to add the notice to, or ``None`` to print it only.
|
|
56
|
+
"""
|
|
57
|
+
print(f"{mark} {message}", file=sys.stderr)
|
|
58
|
+
if notices is not None:
|
|
59
|
+
notices.append({"setting": setting, "message": message})
|
|
60
|
+
|
|
61
|
+
|
|
39
62
|
DEFAULT_CONFIG_PATHS = [
|
|
40
63
|
Path("cchk.toml"),
|
|
41
64
|
Path("commit-check.toml"),
|
|
@@ -208,7 +231,9 @@ def _load_parent_config(
|
|
|
208
231
|
|
|
209
232
|
|
|
210
233
|
def _resolve_inherit_from(
|
|
211
|
-
config: dict[str, Any],
|
|
234
|
+
config: dict[str, Any],
|
|
235
|
+
base_dir: Path | None = None,
|
|
236
|
+
notices: list[dict[str, str]] | None = None,
|
|
212
237
|
) -> dict[str, Any]:
|
|
213
238
|
"""Resolve ``inherit_from`` directive, merging parent config with local.
|
|
214
239
|
|
|
@@ -226,6 +251,8 @@ def _resolve_inherit_from(
|
|
|
226
251
|
|
|
227
252
|
:param config: Already-parsed local TOML dict (may contain ``inherit_from``).
|
|
228
253
|
:param base_dir: Directory of the config file, for a relative local path.
|
|
254
|
+
:param notices: Where to add a notice for a parent that was not loaded,
|
|
255
|
+
as well as printing it; see :func:`report_notice`.
|
|
229
256
|
:returns: Merged config with parent settings applied as base.
|
|
230
257
|
"""
|
|
231
258
|
inherit_from = config.pop("inherit_from", None)
|
|
@@ -233,16 +260,17 @@ def _resolve_inherit_from(
|
|
|
233
260
|
return config
|
|
234
261
|
|
|
235
262
|
# Fail open, as documented: a parent that cannot be loaded leaves the
|
|
236
|
-
# local config in force. Say so
|
|
237
|
-
# stdout clean for --format json.
|
|
263
|
+
# local config in force. Say so rather than silently, on stderr so that
|
|
264
|
+
# stdout stays clean for --format json, and in the notices it carries.
|
|
238
265
|
# TOMLDecodeError is a ValueError; URLError is an OSError.
|
|
239
266
|
try:
|
|
240
267
|
parent = _load_parent_config(inherit_from, base_dir)
|
|
241
268
|
except (OSError, ValueError) as e:
|
|
242
|
-
|
|
243
|
-
|
|
269
|
+
report_notice(
|
|
270
|
+
notices,
|
|
271
|
+
"inherit_from",
|
|
272
|
+
f'inherit_from "{inherit_from}" could not be loaded: {e}; '
|
|
244
273
|
"continuing with the local config",
|
|
245
|
-
file=sys.stderr,
|
|
246
274
|
)
|
|
247
275
|
parent = {}
|
|
248
276
|
|
|
@@ -250,10 +278,11 @@ def _resolve_inherit_from(
|
|
|
250
278
|
# unknown setting, which reads as a typo rather than a limit.
|
|
251
279
|
if "inherit_from" in parent:
|
|
252
280
|
nested = parent.pop("inherit_from")
|
|
253
|
-
|
|
254
|
-
|
|
281
|
+
report_notice(
|
|
282
|
+
notices,
|
|
283
|
+
"inherit_from",
|
|
284
|
+
f'inherit_from "{nested}" in the parent config "{inherit_from}" is '
|
|
255
285
|
"ignored: only one level of inherit_from is followed",
|
|
256
|
-
file=sys.stderr,
|
|
257
286
|
)
|
|
258
287
|
|
|
259
288
|
if parent:
|
|
@@ -331,13 +360,17 @@ def find_config_path(path_hint: str = "") -> Path | None:
|
|
|
331
360
|
return None
|
|
332
361
|
|
|
333
362
|
|
|
334
|
-
def load_config(
|
|
363
|
+
def load_config(
|
|
364
|
+
path_hint: str = "", notices: list[dict[str, str]] | None = None
|
|
365
|
+
) -> dict[str, Any]:
|
|
335
366
|
"""Load and validate config from TOML file.
|
|
336
367
|
|
|
337
368
|
Supports ``inherit_from`` at the top level to merge an organization-level
|
|
338
369
|
configuration from a local file path, a ``github:`` shorthand, or an HTTPS
|
|
339
370
|
URL before applying local overrides.
|
|
340
371
|
|
|
372
|
+
:param notices: Where to add a notice for an ``inherit_from`` that was not
|
|
373
|
+
followed, as well as printing it; see :func:`report_notice`.
|
|
341
374
|
:raises FileNotFoundError: If ``path_hint`` names a file that does not exist.
|
|
342
375
|
:raises ConfigError: If the file is not valid TOML; the message names it.
|
|
343
376
|
"""
|
|
@@ -348,5 +381,5 @@ def load_config(path_hint: str = "") -> dict[str, Any]:
|
|
|
348
381
|
# No default config file: run on the built-in defaults.
|
|
349
382
|
return {}
|
|
350
383
|
return _resolve_inherit_from(
|
|
351
|
-
_load_toml_file(path, shown_as=path_hint or None), path.parent
|
|
384
|
+
_load_toml_file(path, shown_as=path_hint or None), path.parent, notices
|
|
352
385
|
)
|
commit_check/config_merger.py
CHANGED
|
@@ -5,7 +5,6 @@ from __future__ import annotations
|
|
|
5
5
|
import argparse
|
|
6
6
|
import difflib
|
|
7
7
|
import os
|
|
8
|
-
import sys
|
|
9
8
|
from collections.abc import Callable
|
|
10
9
|
from typing import Any, ClassVar
|
|
11
10
|
|
|
@@ -20,7 +19,7 @@ from commit_check import (
|
|
|
20
19
|
DEFAULT_PUSH_RULES,
|
|
21
20
|
DEFAULT_TAG_REGEX,
|
|
22
21
|
)
|
|
23
|
-
from commit_check.config import deep_merge
|
|
22
|
+
from commit_check.config import deep_merge, report_notice
|
|
24
23
|
from commit_check.config import load_config as load_toml_config
|
|
25
24
|
|
|
26
25
|
|
|
@@ -82,6 +81,7 @@ def get_default_config() -> dict[str, Any]:
|
|
|
82
81
|
"allow_fixup_commits": DEFAULT_BOOLEAN_RULES["allow_fixup_commits"],
|
|
83
82
|
"allow_wip_commits": DEFAULT_BOOLEAN_RULES["allow_wip_commits"],
|
|
84
83
|
"require_body": DEFAULT_BOOLEAN_RULES["require_body"],
|
|
84
|
+
"body_max_line_length": 0,
|
|
85
85
|
"require_signed_off_by": DEFAULT_BOOLEAN_RULES["require_signed_off_by"],
|
|
86
86
|
"ignore_authors": [],
|
|
87
87
|
"ai_attribution": DEFAULT_AI_ATTRIBUTION,
|
|
@@ -117,38 +117,60 @@ def get_default_config() -> dict[str, Any]:
|
|
|
117
117
|
#: the rest of ``[pull_request]`` by the GitHub App.
|
|
118
118
|
_OTHER_KEYS = frozenset({"warn", "jira", "pull_request"})
|
|
119
119
|
|
|
120
|
-
#:
|
|
121
|
-
#: ``
|
|
120
|
+
#: Of those, the ones whose contents are not checked here: ``warn`` is a
|
|
121
|
+
#: list the rule builder checks itself, and ``[jira]`` is the GitHub App's.
|
|
122
|
+
_UNCHECKED_KEYS = frozenset({"warn", "jira"})
|
|
123
|
+
|
|
124
|
+
#: Keys read from a table besides its defaults. In ``[commit]``,
|
|
125
|
+
#: ``require_verified`` is read only by the GitHub App, which has GitHub's
|
|
126
|
+
#: verdict on a commit's signature. ``[pull_request]`` has no defaults: the
|
|
127
|
+
#: rule builder reads ``ai_declaration_checkboxes`` and the GitHub App the
|
|
128
|
+
#: other three, so any other key in it is read by nothing.
|
|
122
129
|
_OTHER_SETTINGS: dict[str, frozenset[str]] = {
|
|
123
130
|
"commit": frozenset({"require_verified"}),
|
|
131
|
+
"pull_request": frozenset(
|
|
132
|
+
{"check", "ai_label", "body_ai_attribution", "ai_declaration_checkboxes"}
|
|
133
|
+
),
|
|
124
134
|
}
|
|
125
135
|
|
|
126
136
|
|
|
127
|
-
def
|
|
128
|
-
"""
|
|
137
|
+
def _unknown_settings(config: dict[str, Any]) -> list[tuple[str, str]]:
|
|
138
|
+
"""``(setting, message)`` for each setting in a config file that nothing reads.
|
|
129
139
|
|
|
130
|
-
|
|
140
|
+
*setting* is the name as the message shows it, ``[section] key`` or
|
|
141
|
+
``[section]``.
|
|
131
142
|
"""
|
|
132
143
|
defaults = get_default_config()
|
|
133
144
|
names = []
|
|
134
145
|
for section, value in config.items():
|
|
135
|
-
if section in
|
|
146
|
+
if section in _UNCHECKED_KEYS:
|
|
136
147
|
continue
|
|
137
|
-
if section not in defaults:
|
|
148
|
+
if section not in defaults and section not in _OTHER_KEYS:
|
|
138
149
|
names.append((f"[{section}]", section, [*defaults, *_OTHER_KEYS]))
|
|
139
150
|
elif isinstance(value, dict):
|
|
151
|
+
other = _OTHER_SETTINGS.get(section, frozenset())
|
|
152
|
+
# A misspelt key is matched against the defaults, as before,
|
|
153
|
+
# or, in a table that has none, against the keys it can hold.
|
|
154
|
+
candidates = defaults.get(section, other)
|
|
140
155
|
names.extend(
|
|
141
|
-
(f"[{section}] {key}", key,
|
|
156
|
+
(f"[{section}] {key}", key, candidates)
|
|
142
157
|
for key in value
|
|
143
|
-
if key not in defaults
|
|
144
|
-
and key not in _OTHER_SETTINGS.get(section, ())
|
|
158
|
+
if key not in defaults.get(section, ()) and key not in other
|
|
145
159
|
)
|
|
146
|
-
|
|
160
|
+
found = []
|
|
147
161
|
for shown, name, known in names:
|
|
148
162
|
match = difflib.get_close_matches(name, list(known), n=1)
|
|
149
163
|
hint = f"; did you mean {match[0]}?" if match else ""
|
|
150
|
-
|
|
151
|
-
return
|
|
164
|
+
found.append((shown, f"unknown setting {shown} is ignored{hint}"))
|
|
165
|
+
return found
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def unknown_setting_warnings(config: dict[str, Any]) -> list[str]:
|
|
169
|
+
"""One line per setting in a config file that nothing reads.
|
|
170
|
+
|
|
171
|
+
A misspelt key used to be ignored in silence, leaving its rule off.
|
|
172
|
+
"""
|
|
173
|
+
return [f"⚠ {message}" for _setting, message in _unknown_settings(config)]
|
|
152
174
|
|
|
153
175
|
|
|
154
176
|
class ConfigMerger:
|
|
@@ -170,6 +192,7 @@ class ConfigMerger:
|
|
|
170
192
|
"CCHK_ALLOW_FIXUP_COMMITS": ("commit", "allow_fixup_commits", parse_bool),
|
|
171
193
|
"CCHK_ALLOW_WIP_COMMITS": ("commit", "allow_wip_commits", parse_bool),
|
|
172
194
|
"CCHK_REQUIRE_BODY": ("commit", "require_body", parse_bool),
|
|
195
|
+
"CCHK_BODY_MAX_LINE_LENGTH": ("commit", "body_max_line_length", parse_int),
|
|
173
196
|
"CCHK_REQUIRE_SIGNED_OFF_BY": ("commit", "require_signed_off_by", parse_bool),
|
|
174
197
|
"CCHK_IGNORE_AUTHORS": ("commit", "ignore_authors", parse_list),
|
|
175
198
|
"CCHK_AI_ATTRIBUTION": ("commit", "ai_attribution", str),
|
|
@@ -217,6 +240,7 @@ class ConfigMerger:
|
|
|
217
240
|
"allow_fixup_commits": ("commit", "allow_fixup_commits"),
|
|
218
241
|
"allow_wip_commits": ("commit", "allow_wip_commits"),
|
|
219
242
|
"require_body": ("commit", "require_body"),
|
|
243
|
+
"body_max_line_length": ("commit", "body_max_line_length"),
|
|
220
244
|
"require_signed_off_by": ("commit", "require_signed_off_by"),
|
|
221
245
|
"ignore_authors": ("commit", "ignore_authors"),
|
|
222
246
|
"ai_attribution": ("commit", "ai_attribution"),
|
|
@@ -242,8 +266,13 @@ class ConfigMerger:
|
|
|
242
266
|
}
|
|
243
267
|
|
|
244
268
|
@staticmethod
|
|
245
|
-
def parse_env_vars() -> dict[str, Any]:
|
|
246
|
-
"""Parse environment variables with CCHK_ prefix into config dict.
|
|
269
|
+
def parse_env_vars(notices: list[dict[str, str]] | None = None) -> dict[str, Any]:
|
|
270
|
+
"""Parse environment variables with CCHK_ prefix into config dict.
|
|
271
|
+
|
|
272
|
+
:param notices: Where to add a notice for a value that cannot be
|
|
273
|
+
parsed, as well as printing it; see
|
|
274
|
+
:func:`~commit_check.config.report_notice`.
|
|
275
|
+
"""
|
|
247
276
|
config: dict[str, Any] = {
|
|
248
277
|
"commit": {},
|
|
249
278
|
"branch": {},
|
|
@@ -261,7 +290,12 @@ class ConfigMerger:
|
|
|
261
290
|
except (ValueError, TypeError) as e:
|
|
262
291
|
# Log warning but don't fail - just skip invalid env vars.
|
|
263
292
|
# stderr, so --format json output stays parseable.
|
|
264
|
-
|
|
293
|
+
report_notice(
|
|
294
|
+
notices,
|
|
295
|
+
env_var,
|
|
296
|
+
f"Invalid value for {env_var}: {e}",
|
|
297
|
+
mark="Warning:",
|
|
298
|
+
)
|
|
265
299
|
|
|
266
300
|
# Remove empty sections
|
|
267
301
|
config = {k: v for k, v in config.items() if v}
|
|
@@ -290,13 +324,19 @@ class ConfigMerger:
|
|
|
290
324
|
|
|
291
325
|
@staticmethod
|
|
292
326
|
def from_all_sources(
|
|
293
|
-
cli_args: argparse.Namespace,
|
|
327
|
+
cli_args: argparse.Namespace,
|
|
328
|
+
config_path: str | None = None,
|
|
329
|
+
notices: list[dict[str, str]] | None = None,
|
|
294
330
|
) -> dict[str, Any]:
|
|
295
331
|
"""Merge configs from all sources with priority: CLI > Env > TOML > Defaults.
|
|
296
332
|
|
|
297
333
|
Args:
|
|
298
334
|
cli_args: Parsed command line arguments
|
|
299
335
|
config_path: Optional path to TOML config file
|
|
336
|
+
notices: Where to add a notice for each setting that is not in
|
|
337
|
+
force (an ``inherit_from`` not followed, an unknown setting,
|
|
338
|
+
a ``CCHK_*`` value that cannot be parsed), as well as
|
|
339
|
+
printing it; see :func:`~commit_check.config.report_notice`.
|
|
300
340
|
|
|
301
341
|
Returns:
|
|
302
342
|
Merged configuration dictionary
|
|
@@ -306,10 +346,10 @@ class ConfigMerger:
|
|
|
306
346
|
|
|
307
347
|
# 2. Merge TOML config (if exists)
|
|
308
348
|
try:
|
|
309
|
-
toml_config = load_toml_config(config_path or "")
|
|
349
|
+
toml_config = load_toml_config(config_path or "", notices=notices)
|
|
310
350
|
if toml_config:
|
|
311
|
-
for
|
|
312
|
-
|
|
351
|
+
for setting, message in _unknown_settings(toml_config):
|
|
352
|
+
report_notice(notices, setting, message, mark="⚠")
|
|
313
353
|
deep_merge(config, toml_config)
|
|
314
354
|
except FileNotFoundError:
|
|
315
355
|
# If a specific path was provided and not found, this error is already raised
|
|
@@ -318,7 +358,7 @@ class ConfigMerger:
|
|
|
318
358
|
raise
|
|
319
359
|
|
|
320
360
|
# 3. Merge environment variables
|
|
321
|
-
env_config = ConfigMerger.parse_env_vars()
|
|
361
|
+
env_config = ConfigMerger.parse_env_vars(notices)
|
|
322
362
|
if env_config:
|
|
323
363
|
deep_merge(config, env_config)
|
|
324
364
|
|