codeveto 0.1.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.
- codeveto/__init__.py +3 -0
- codeveto/__main__.py +8 -0
- codeveto/ai/__init__.py +19 -0
- codeveto/ai/harness.py +303 -0
- codeveto/ai/packets.py +855 -0
- codeveto/ai/policy.py +222 -0
- codeveto/ai/prompt.py +101 -0
- codeveto/ai/providers/__init__.py +178 -0
- codeveto/ai/providers/claude.py +247 -0
- codeveto/ai/providers/openai_compat.py +246 -0
- codeveto/ai/runner.py +293 -0
- codeveto/ai/verify.py +324 -0
- codeveto/anchor.py +211 -0
- codeveto/cli.py +247 -0
- codeveto/cli_explain.py +49 -0
- codeveto/cli_hook.py +31 -0
- codeveto/cli_init.py +254 -0
- codeveto/cli_mcp.py +29 -0
- codeveto/cli_packets.py +35 -0
- codeveto/cli_publish.py +29 -0
- codeveto/cli_review.py +362 -0
- codeveto/cli_rules.py +78 -0
- codeveto/cli_verify.py +47 -0
- codeveto/config.py +1229 -0
- codeveto/data/README.md +16 -0
- codeveto/data/ai-wire.schema.json +53 -0
- codeveto/data/prompts/system.md +57 -0
- codeveto/data/report-v1.schema.json +268 -0
- codeveto/data/templates/forgejo-codeveto.yml +104 -0
- codeveto/data/templates/github-codeveto.yml +18 -0
- codeveto/embedded.py +311 -0
- codeveto/engine.py +1570 -0
- codeveto/git.py +1549 -0
- codeveto/globs.py +348 -0
- codeveto/integrations/__init__.py +1 -0
- codeveto/integrations/hooks.py +327 -0
- codeveto/integrations/mcp.py +770 -0
- codeveto/integrations/mcp_ai.py +77 -0
- codeveto/integrations/prepush.py +222 -0
- codeveto/jsonsafe.py +60 -0
- codeveto/languages.py +539 -0
- codeveto/lexers.py +2560 -0
- codeveto/model.py +372 -0
- codeveto/publish/__init__.py +28 -0
- codeveto/publish/action.py +330 -0
- codeveto/publish/common.py +84 -0
- codeveto/publish/diff.py +208 -0
- codeveto/publish/event.py +241 -0
- codeveto/publish/forgejo.py +184 -0
- codeveto/publish/github.py +380 -0
- codeveto/publish/http.py +263 -0
- codeveto/publish/pull.py +218 -0
- codeveto/publish/untrusted.py +110 -0
- codeveto/py.typed +0 -0
- codeveto/redact.py +113 -0
- codeveto/render/__init__.py +64 -0
- codeveto/render/agent.py +43 -0
- codeveto/render/github.py +101 -0
- codeveto/render/json.py +23 -0
- codeveto/render/markdown.py +208 -0
- codeveto/render/safe.py +249 -0
- codeveto/render/sarif.py +217 -0
- codeveto/render/text.py +210 -0
- codeveto/report.py +252 -0
- codeveto/rules/__init__.py +84 -0
- codeveto/rules/agent.py +2972 -0
- codeveto/rules/base.py +1026 -0
- codeveto/rules/ci.py +1708 -0
- codeveto/rules/config.py +493 -0
- codeveto/rules/deps.py +1693 -0
- codeveto/rules/docker.py +479 -0
- codeveto/rules/examples/__init__.py +148 -0
- codeveto/rules/examples/agent.py +839 -0
- codeveto/rules/examples/ci.py +1046 -0
- codeveto/rules/examples/config.py +191 -0
- codeveto/rules/examples/deps.py +369 -0
- codeveto/rules/examples/docker.py +157 -0
- codeveto/rules/examples/go.py +241 -0
- codeveto/rules/examples/hygiene.py +466 -0
- codeveto/rules/examples/js.py +462 -0
- codeveto/rules/examples/python.py +595 -0
- codeveto/rules/examples/rust.py +268 -0
- codeveto/rules/examples/secrets.py +561 -0
- codeveto/rules/examples/security.py +608 -0
- codeveto/rules/examples/shell.py +174 -0
- codeveto/rules/examples/sql.py +194 -0
- codeveto/rules/examples/tests.py +497 -0
- codeveto/rules/go.py +328 -0
- codeveto/rules/hygiene.py +1094 -0
- codeveto/rules/js.py +768 -0
- codeveto/rules/python.py +1642 -0
- codeveto/rules/rust.py +209 -0
- codeveto/rules/secrets.py +1335 -0
- codeveto/rules/security.py +3680 -0
- codeveto/rules/shell.py +867 -0
- codeveto/rules/sql.py +629 -0
- codeveto/rules/tests.py +1404 -0
- codeveto/schema.py +384 -0
- codeveto/statements.py +607 -0
- codeveto/worktree.py +1176 -0
- codeveto/yamlish.py +1179 -0
- codeveto-0.1.0.dist-info/METADATA +253 -0
- codeveto-0.1.0.dist-info/RECORD +106 -0
- codeveto-0.1.0.dist-info/WHEEL +4 -0
- codeveto-0.1.0.dist-info/entry_points.txt +2 -0
- codeveto-0.1.0.dist-info/licenses/LICENSE +21 -0
codeveto/anchor.py
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"""Trust-anchor resolution for local reviews (SPEC 7.1).
|
|
2
|
+
|
|
3
|
+
The anchor is a commit the reviewed change cannot have produced; repository
|
|
4
|
+
config and ignore rules are read from it. Sources, in order:
|
|
5
|
+
|
|
6
|
+
1. ``explicit``: a full object ID given by the caller (``--anchor``, or the
|
|
7
|
+
OID ``codeveto mcp`` and agent hooks capture at session start), validated as
|
|
8
|
+
a commit in the isolation repository. Anything else (a tree, blob, tag,
|
|
9
|
+
unknown or abbreviated ID, a ref name) raises reason ``invalid-anchor``.
|
|
10
|
+
2. ``upstream``: the unique merge base of ``HEAD`` with ``@{upstream}``, used
|
|
11
|
+
only when the upstream is a remote-tracking ref (``refs/remotes/*``); a local
|
|
12
|
+
branch as upstream (``branch.<name>.remote = .``) is skipped.
|
|
13
|
+
3. ``origin-head``: the unique merge base of ``HEAD`` with the ref that
|
|
14
|
+
``refs/remotes/origin/HEAD`` points to, used only when that ref lies in
|
|
15
|
+
``refs/remotes/origin/``.
|
|
16
|
+
4. ``head``: ``HEAD`` itself, which the change can have produced, so config is
|
|
17
|
+
unprotected; an unborn ``HEAD`` gives the empty tree.
|
|
18
|
+
|
|
19
|
+
Only ``explicit`` resists a process that can write ``.git``: remote-tracking
|
|
20
|
+
refs are local files too, so ``upstream`` and ``origin-head`` anchors carry
|
|
21
|
+
:data:`LOCAL_REF_NOTE`.
|
|
22
|
+
|
|
23
|
+
Only fixed arguments reach Git (never a user-supplied name), merge bases are
|
|
24
|
+
computed in the isolation repository, and every anchor commit is hash-verified.
|
|
25
|
+
When a source is missing, not acceptable, or its merge base is absent or not
|
|
26
|
+
unique, resolution falls through to the next source and records why in
|
|
27
|
+
``notes``. The notes are fixed strings and never contain repository text.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
from __future__ import annotations
|
|
31
|
+
|
|
32
|
+
import os
|
|
33
|
+
import re
|
|
34
|
+
from dataclasses import dataclass
|
|
35
|
+
|
|
36
|
+
from codeveto import git as gitmod
|
|
37
|
+
from codeveto.git import DEFAULT_LIMITS, Anchor, Git, GitError
|
|
38
|
+
from codeveto.model import AnchorSource
|
|
39
|
+
|
|
40
|
+
__all__ = [
|
|
41
|
+
"HEAD_NOTE",
|
|
42
|
+
"LOCAL_REF_NOTE",
|
|
43
|
+
"UNBORN_NOTE",
|
|
44
|
+
"ResolvedAnchor",
|
|
45
|
+
"anchor_commit",
|
|
46
|
+
"resolve_anchor",
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
#: The visible note for an anchor at ``HEAD`` (SPEC 7.1).
|
|
50
|
+
HEAD_NOTE = (
|
|
51
|
+
"anchored at HEAD, which the change itself can produce: repository config "
|
|
52
|
+
"and ignore rules are unprotected (pass --anchor with a commit captured earlier)"
|
|
53
|
+
)
|
|
54
|
+
#: The note for an unborn ``HEAD``: the empty tree carries no repository config.
|
|
55
|
+
UNBORN_NOTE = (
|
|
56
|
+
"HEAD has no commits yet: anchored at the empty tree, so no repository "
|
|
57
|
+
"config or ignore rules apply"
|
|
58
|
+
)
|
|
59
|
+
#: The note on every ``upstream`` and ``origin-head`` anchor (SPEC 7.1).
|
|
60
|
+
LOCAL_REF_NOTE = (
|
|
61
|
+
"anchored through local remote-tracking refs, which any process that can write "
|
|
62
|
+
".git (such as an agent) can move: pass --anchor with a commit captured before "
|
|
63
|
+
"the agent ran"
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
_NO_UPSTREAM = "the current branch has no upstream with a remote-tracking branch"
|
|
67
|
+
_LOCAL_UPSTREAM = (
|
|
68
|
+
"the upstream of the current branch is not a remote-tracking ref (refs/remotes/*); skipped"
|
|
69
|
+
)
|
|
70
|
+
_NO_ORIGIN_HEAD = "refs/remotes/origin/HEAD is not set"
|
|
71
|
+
_FOREIGN_ORIGIN_HEAD = "refs/remotes/origin/HEAD does not point into refs/remotes/origin/; skipped"
|
|
72
|
+
# A full ref name as Git prints it: no controls, spaces or path tricks.
|
|
73
|
+
_REF = re.compile(
|
|
74
|
+
rb"refs/(?:[^\x00-\x20\x7f/.][^\x00-\x20\x7f/]*/)*[^\x00-\x20\x7f/.][^\x00-\x20\x7f/]*"
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@dataclass(frozen=True, slots=True)
|
|
79
|
+
class ResolvedAnchor:
|
|
80
|
+
"""An anchor and why earlier sources were not used."""
|
|
81
|
+
|
|
82
|
+
anchor: Anchor
|
|
83
|
+
notes: tuple[str, ...] = ()
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def oid(self) -> str:
|
|
87
|
+
return self.anchor.oid
|
|
88
|
+
|
|
89
|
+
@property
|
|
90
|
+
def source(self) -> AnchorSource:
|
|
91
|
+
return self.anchor.source
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def anchor_commit(git: Git, oid: str) -> str:
|
|
95
|
+
"""Validate an explicit anchor: a full object ID of a hash-verified commit.
|
|
96
|
+
|
|
97
|
+
Every way it can fail to be one raises :class:`GitError` with reason
|
|
98
|
+
``invalid-anchor``.
|
|
99
|
+
"""
|
|
100
|
+
try:
|
|
101
|
+
resolved = git.resolve_anchor(oid)
|
|
102
|
+
git.commit(resolved) # hash-verified
|
|
103
|
+
except GitError as exc:
|
|
104
|
+
if exc.reason in ("invalid-anchor", "invalid-ref", "unknown-ref", "ambiguous-ref"):
|
|
105
|
+
raise GitError(
|
|
106
|
+
"anchor must be the full object ID of a commit in the repository",
|
|
107
|
+
reason="invalid-anchor",
|
|
108
|
+
) from exc
|
|
109
|
+
raise
|
|
110
|
+
return resolved
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def resolve_anchor(repo: str | os.PathLike[str], *, explicit: str | None = None) -> ResolvedAnchor:
|
|
114
|
+
"""Resolve the trust anchor of ``repo`` (SPEC 7.1).
|
|
115
|
+
|
|
116
|
+
An invalid ``explicit`` anchor raises :class:`GitError` (reason
|
|
117
|
+
``invalid-anchor``); it never falls through to another source.
|
|
118
|
+
"""
|
|
119
|
+
with gitmod._session(repo, DEFAULT_LIMITS) as git:
|
|
120
|
+
if explicit is not None:
|
|
121
|
+
return ResolvedAnchor(Anchor(anchor_commit(git, explicit), "explicit"))
|
|
122
|
+
head = git.head_or_none()
|
|
123
|
+
if head is None:
|
|
124
|
+
return ResolvedAnchor(Anchor(git.empty_tree, "head"), (UNBORN_NOTE,))
|
|
125
|
+
git.commit(head)
|
|
126
|
+
notes: list[str] = []
|
|
127
|
+
upstream = _upstream(git, notes)
|
|
128
|
+
if upstream is not None:
|
|
129
|
+
base = _merge_base(git, head, upstream, "its upstream", notes)
|
|
130
|
+
if base is not None:
|
|
131
|
+
return ResolvedAnchor(Anchor(base, "upstream"), (*notes, LOCAL_REF_NOTE))
|
|
132
|
+
origin = _origin_head(git, notes)
|
|
133
|
+
if origin is not None:
|
|
134
|
+
base = _merge_base(git, head, origin, "refs/remotes/origin/HEAD", notes)
|
|
135
|
+
if base is not None:
|
|
136
|
+
return ResolvedAnchor(Anchor(base, "origin-head"), (*notes, LOCAL_REF_NOTE))
|
|
137
|
+
notes.append(HEAD_NOTE)
|
|
138
|
+
return ResolvedAnchor(Anchor(head, "head"), tuple(notes))
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _target(git: Git, args: list[str]) -> bytes | None:
|
|
142
|
+
"""A full ref name that a fixed Git query prints, or ``None``."""
|
|
143
|
+
code, out = git.run_status(args, source=True, ok=(0, 1, 128))
|
|
144
|
+
ref = out.removesuffix(b"\n")
|
|
145
|
+
return ref if code == 0 and _REF.fullmatch(ref) else None
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _commit_of(git: Git, ref: bytes) -> str | None:
|
|
149
|
+
"""The commit a full ref name (already validated) points to, or ``None``."""
|
|
150
|
+
spec = ref.decode("utf-8", "surrogateescape") + "^{commit}"
|
|
151
|
+
code, out = git.run_status(
|
|
152
|
+
["rev-parse", "--verify", "--quiet", "--end-of-options", spec],
|
|
153
|
+
source=True,
|
|
154
|
+
ok=(0, 1, 128),
|
|
155
|
+
)
|
|
156
|
+
return git._oid(out.rstrip(b"\n")) if code == 0 else None
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def _upstream(git: Git, notes: list[str]) -> str | None:
|
|
160
|
+
code, _ = git.run_status(["symbolic-ref", "--quiet", "HEAD"], source=True, ok=(0, 1))
|
|
161
|
+
if code != 0:
|
|
162
|
+
notes.append("HEAD is detached, so it has no upstream")
|
|
163
|
+
return None
|
|
164
|
+
ref = _target(
|
|
165
|
+
git,
|
|
166
|
+
["rev-parse", "--verify", "--quiet", "--symbolic-full-name", "--end-of-options", "@{u}"],
|
|
167
|
+
)
|
|
168
|
+
if ref is None:
|
|
169
|
+
notes.append(_NO_UPSTREAM)
|
|
170
|
+
return None
|
|
171
|
+
if not ref.startswith(b"refs/remotes/"):
|
|
172
|
+
notes.append(_LOCAL_UPSTREAM)
|
|
173
|
+
return None
|
|
174
|
+
oid = _commit_of(git, ref)
|
|
175
|
+
if oid is None:
|
|
176
|
+
notes.append(_NO_UPSTREAM)
|
|
177
|
+
return oid
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _origin_head(git: Git, notes: list[str]) -> str | None:
|
|
181
|
+
ref = _target(git, ["symbolic-ref", "--quiet", "refs/remotes/origin/HEAD"])
|
|
182
|
+
if ref is None:
|
|
183
|
+
# Not a symbolic ref: missing, or a plain ref that points nowhere in particular.
|
|
184
|
+
plain = _commit_of(git, b"refs/remotes/origin/HEAD")
|
|
185
|
+
notes.append(_NO_ORIGIN_HEAD if plain is None else _FOREIGN_ORIGIN_HEAD)
|
|
186
|
+
return None
|
|
187
|
+
if not ref.startswith(b"refs/remotes/origin/") or ref == b"refs/remotes/origin/HEAD":
|
|
188
|
+
notes.append(_FOREIGN_ORIGIN_HEAD)
|
|
189
|
+
return None
|
|
190
|
+
oid = _commit_of(git, ref)
|
|
191
|
+
if oid is None:
|
|
192
|
+
notes.append(_NO_ORIGIN_HEAD)
|
|
193
|
+
return oid
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _merge_base(git: Git, head: str, other: str, label: str, notes: list[str]) -> str | None:
|
|
197
|
+
try:
|
|
198
|
+
bases = git.merge_bases(head, other)
|
|
199
|
+
except GitError as exc:
|
|
200
|
+
# A shallow or incomplete history makes the walk fail; anything else
|
|
201
|
+
# (time budget, output cap) is not a reason to pick a weaker anchor.
|
|
202
|
+
if exc.reason != "failed":
|
|
203
|
+
raise
|
|
204
|
+
notes.append(f"the merge base of HEAD and {label} could not be computed")
|
|
205
|
+
return None
|
|
206
|
+
if len(bases) != 1:
|
|
207
|
+
count = "no merge base" if not bases else f"{len(bases)} merge bases"
|
|
208
|
+
notes.append(f"HEAD and {label} have {count}; a unique merge base is required")
|
|
209
|
+
return None
|
|
210
|
+
git.commit(bases[0]) # hash-verified
|
|
211
|
+
return bases[0]
|
codeveto/cli.py
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"""Command-line entry point: the whole SPEC 8 command surface.
|
|
2
|
+
|
|
3
|
+
The parser here is frozen: every subcommand and flag of SPEC 8 exists now,
|
|
4
|
+
and each subcommand runs ``codeveto.cli_<name>.run(args) -> int``, so later
|
|
5
|
+
tasks only edit their own ``cli_<name>.py``. ``packets``, ``verify``,
|
|
6
|
+
``mcp``, ``hook``, ``init`` and ``publish`` are stubs until their tasks land.
|
|
7
|
+
|
|
8
|
+
Exit codes never lie (SPEC 5.6): 0 clear, 1 vetoed, 2 incomplete. A usage
|
|
9
|
+
error is 2 (argparse's own exits included; only ``--help`` and ``--version``
|
|
10
|
+
exit 0), a :class:`~codeveto.config.ConfigError` or
|
|
11
|
+
:class:`~codeveto.git.GitError` is 2 with its sanitized message, and any
|
|
12
|
+
other exception is 2 with one sanitized line on stderr and no traceback
|
|
13
|
+
(set ``CODEVETO_DEBUG=1`` to see it). Nothing maps to 1 except a verdict.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import argparse
|
|
19
|
+
import importlib
|
|
20
|
+
import os
|
|
21
|
+
import sys
|
|
22
|
+
import traceback
|
|
23
|
+
|
|
24
|
+
from codeveto import __version__
|
|
25
|
+
from codeveto.config import ConfigError
|
|
26
|
+
from codeveto.git import GitError
|
|
27
|
+
from codeveto.render import FORMATS
|
|
28
|
+
from codeveto.render import safe as _safe
|
|
29
|
+
|
|
30
|
+
__all__ = ["COMMANDS", "VERSION_STRING", "build_parser", "error_line", "main", "write_stdout"]
|
|
31
|
+
|
|
32
|
+
VERSION_STRING = f"codeveto {__version__}"
|
|
33
|
+
|
|
34
|
+
#: Every subcommand (``version`` is handled here, the rest by ``cli_<name>.py``).
|
|
35
|
+
COMMANDS: tuple[str, ...] = (
|
|
36
|
+
"review",
|
|
37
|
+
"rules",
|
|
38
|
+
"explain",
|
|
39
|
+
"packets",
|
|
40
|
+
"verify",
|
|
41
|
+
"mcp",
|
|
42
|
+
"hook",
|
|
43
|
+
"init",
|
|
44
|
+
"publish",
|
|
45
|
+
"version",
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
_LEVELS = ("error", "warning", "note", "never")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def error_line(message: object) -> None:
|
|
52
|
+
"""Write ``codeveto: <message>`` to stderr as one sanitized line."""
|
|
53
|
+
print(f"codeveto: {_safe.text(message, 2000)}", file=sys.stderr, flush=True)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _reader_gone() -> None:
|
|
57
|
+
# The reader closed the pipe (`codeveto ... | head`). Point stdout at the
|
|
58
|
+
# null device so the interpreter's final flush cannot fail again; the
|
|
59
|
+
# command still exits with its own code (a review keeps its verdict).
|
|
60
|
+
try:
|
|
61
|
+
fd = sys.stdout.fileno()
|
|
62
|
+
except (AttributeError, OSError, ValueError):
|
|
63
|
+
return
|
|
64
|
+
devnull = os.open(os.devnull, os.O_WRONLY)
|
|
65
|
+
try:
|
|
66
|
+
os.dup2(devnull, fd)
|
|
67
|
+
finally:
|
|
68
|
+
os.close(devnull)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def write_stdout(text: str) -> None:
|
|
72
|
+
"""Write ``text`` to stdout; characters the stream cannot encode become escapes.
|
|
73
|
+
|
|
74
|
+
A reader that stops reading early is not an error: the rest is dropped.
|
|
75
|
+
"""
|
|
76
|
+
stream = sys.stdout
|
|
77
|
+
try:
|
|
78
|
+
try:
|
|
79
|
+
stream.write(text)
|
|
80
|
+
except UnicodeEncodeError:
|
|
81
|
+
encoding = getattr(stream, "encoding", None) or "utf-8"
|
|
82
|
+
stream.write(text.encode(encoding, "backslashreplace").decode(encoding))
|
|
83
|
+
stream.flush()
|
|
84
|
+
except BrokenPipeError:
|
|
85
|
+
_reader_gone()
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _add_revision_flags(parser: argparse.ArgumentParser, *, repo: bool = True) -> None:
|
|
89
|
+
group = parser.add_argument_group("revision")
|
|
90
|
+
group.add_argument("--base", metavar="REF", help="review REF..HEAD (or ..--head)")
|
|
91
|
+
group.add_argument("--head", metavar="REF", help="head revision of a --base review")
|
|
92
|
+
group.add_argument(
|
|
93
|
+
"--comparison",
|
|
94
|
+
choices=("merge-base", "direct"),
|
|
95
|
+
help="compare head with the merge base (default) or with base itself",
|
|
96
|
+
)
|
|
97
|
+
group.add_argument(
|
|
98
|
+
"--staged", action="store_true", help="review the index against HEAD (pre-commit)"
|
|
99
|
+
)
|
|
100
|
+
group.add_argument("--commit", metavar="REF", help="review one commit against its parent")
|
|
101
|
+
group.add_argument(
|
|
102
|
+
"--anchor", metavar="OID", help="trust anchor: full commit OID that config is read from"
|
|
103
|
+
)
|
|
104
|
+
if repo:
|
|
105
|
+
group.add_argument("--repo", metavar="PATH", help="repository to review (default: .)")
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
109
|
+
"""The frozen ``codeveto`` argument parser (SPEC 8)."""
|
|
110
|
+
parser = argparse.ArgumentParser(
|
|
111
|
+
prog="codeveto",
|
|
112
|
+
description="Strict, deterministic-first code review for Git changes.",
|
|
113
|
+
)
|
|
114
|
+
parser.add_argument("--version", action="version", version=VERSION_STRING)
|
|
115
|
+
sub = parser.add_subparsers(dest="command", metavar="<command>")
|
|
116
|
+
|
|
117
|
+
review = sub.add_parser(
|
|
118
|
+
"review", help="review a change and exit 0 clear, 1 vetoed, 2 incomplete"
|
|
119
|
+
)
|
|
120
|
+
_add_revision_flags(review)
|
|
121
|
+
review.add_argument("--format", choices=FORMATS, default="text", help="output format")
|
|
122
|
+
review.add_argument("--output", metavar="FILE", help="write the output to FILE (atomically)")
|
|
123
|
+
level = review.add_mutually_exclusive_group()
|
|
124
|
+
level.add_argument("--fail-on", choices=_LEVELS, help="lowest severity that blocks")
|
|
125
|
+
level.add_argument("--strict", action="store_true", help="same as --fail-on warning")
|
|
126
|
+
review.add_argument("--config", metavar="FILE", help="extra trusted config file")
|
|
127
|
+
review.add_argument(
|
|
128
|
+
"--no-repo-config", action="store_true", help="ignore the repository's .codeveto.toml"
|
|
129
|
+
)
|
|
130
|
+
review.add_argument("--ai", choices=("claude", "openai", "none"), help="AI provider")
|
|
131
|
+
review.add_argument("--ai-model", metavar="ID", help="AI model ID")
|
|
132
|
+
review.add_argument(
|
|
133
|
+
"--ai-base-url", metavar="URL", help="model API base URL (https; http only for loopback)"
|
|
134
|
+
)
|
|
135
|
+
review.add_argument(
|
|
136
|
+
"--ai-optional", action="store_true", help="continue without AI if it is unavailable"
|
|
137
|
+
)
|
|
138
|
+
review.add_argument(
|
|
139
|
+
"--ai-response-format",
|
|
140
|
+
choices=("json_schema", "json_object", "prompt"),
|
|
141
|
+
help="how the model is asked for structured output",
|
|
142
|
+
)
|
|
143
|
+
review.add_argument(
|
|
144
|
+
"--color", choices=("auto", "always", "never"), default="auto", help="colour output"
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
rules = sub.add_parser("rules", help="list the rules")
|
|
148
|
+
rules.add_argument("--format", choices=("text", "json"), default="text")
|
|
149
|
+
rules.add_argument("--pack", metavar="NAME", help="only this pack")
|
|
150
|
+
|
|
151
|
+
explain = sub.add_parser("explain", help="explain one rule")
|
|
152
|
+
explain.add_argument("rule_id", metavar="RULE_ID")
|
|
153
|
+
|
|
154
|
+
packets = sub.add_parser("packets", help="write AI review packets (harness step 1)")
|
|
155
|
+
_add_revision_flags(packets)
|
|
156
|
+
packets.add_argument("--output", metavar="FILE")
|
|
157
|
+
|
|
158
|
+
verify = sub.add_parser("verify", help="verify AI responses (harness step 2)")
|
|
159
|
+
_add_revision_flags(verify)
|
|
160
|
+
verify.add_argument("--responses", metavar="FILE", required=True)
|
|
161
|
+
|
|
162
|
+
mcp = sub.add_parser("mcp", help="run the stdio MCP server")
|
|
163
|
+
_add_revision_flags(mcp, repo=False)
|
|
164
|
+
mcp.add_argument("--repo", metavar="PATH", required=True)
|
|
165
|
+
|
|
166
|
+
hook = sub.add_parser("hook", help="install or remove a Git hook")
|
|
167
|
+
hook.add_argument("action", choices=("install", "uninstall"))
|
|
168
|
+
which = hook.add_mutually_exclusive_group(required=True)
|
|
169
|
+
which.add_argument("--pre-commit", action="store_true")
|
|
170
|
+
which.add_argument("--pre-push", action="store_true")
|
|
171
|
+
hook.add_argument("--repo", metavar="PATH")
|
|
172
|
+
hook.add_argument("--force", action="store_true")
|
|
173
|
+
|
|
174
|
+
init = sub.add_parser("init", help="write a CI workflow for a forge")
|
|
175
|
+
init.add_argument("forge", choices=("github", "forgejo"))
|
|
176
|
+
init.add_argument("--repo", metavar="PATH")
|
|
177
|
+
init.add_argument("--force", action="store_true", help="overwrite an existing workflow")
|
|
178
|
+
init.add_argument(
|
|
179
|
+
"--hash",
|
|
180
|
+
metavar="sha256:HEX",
|
|
181
|
+
action="append",
|
|
182
|
+
help="forgejo: wheel hash to pin instead of asking PyPI (repeatable)",
|
|
183
|
+
)
|
|
184
|
+
init.add_argument(
|
|
185
|
+
"--action-sha",
|
|
186
|
+
metavar="SHA",
|
|
187
|
+
help="github: full commit SHA of the CodeVeto release to pin the action to",
|
|
188
|
+
)
|
|
189
|
+
init.add_argument(
|
|
190
|
+
"--allow-tag",
|
|
191
|
+
action="store_true",
|
|
192
|
+
help="github: use the movable vX.Y tag when no release commit SHA is known",
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
publish = sub.add_parser("publish", help="publish a report to a forge")
|
|
196
|
+
publish.add_argument("forge", choices=("github", "forgejo"))
|
|
197
|
+
publish.add_argument("--report", metavar="FILE", required=True)
|
|
198
|
+
|
|
199
|
+
sub.add_parser("version", help="print the CodeVeto version")
|
|
200
|
+
return parser
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def _debug() -> bool:
|
|
204
|
+
return os.environ.get("CODEVETO_DEBUG") == "1"
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def _dispatch(parser: argparse.ArgumentParser, args: argparse.Namespace) -> int:
|
|
208
|
+
if args.command == "version":
|
|
209
|
+
print(VERSION_STRING)
|
|
210
|
+
return 0
|
|
211
|
+
if args.command is None:
|
|
212
|
+
parser.print_help()
|
|
213
|
+
return 2
|
|
214
|
+
module = importlib.import_module(f"codeveto.cli_{args.command}")
|
|
215
|
+
code = module.run(args)
|
|
216
|
+
return code if code in (0, 1, 2) else 2
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def main(argv: list[str] | None = None) -> int:
|
|
220
|
+
"""Run the CLI and return a process exit code (0, 1 or 2)."""
|
|
221
|
+
parser = build_parser()
|
|
222
|
+
try:
|
|
223
|
+
args = parser.parse_args(argv)
|
|
224
|
+
except SystemExit as exc:
|
|
225
|
+
# argparse exits 0 only for --help/--version; every parse error is 2.
|
|
226
|
+
return exc.code if exc.code in (0, 2) else 2
|
|
227
|
+
try:
|
|
228
|
+
return _dispatch(parser, args)
|
|
229
|
+
except ConfigError as exc:
|
|
230
|
+
error_line(f"config error: {exc}")
|
|
231
|
+
return 2
|
|
232
|
+
except GitError as exc:
|
|
233
|
+
error_line(f"git error: {exc}")
|
|
234
|
+
return 2
|
|
235
|
+
except KeyboardInterrupt:
|
|
236
|
+
error_line("interrupted; the review is incomplete")
|
|
237
|
+
return 2
|
|
238
|
+
except BaseException as exc: # every failure must exit 2, never 1 or a traceback
|
|
239
|
+
if isinstance(exc, SystemExit) and exc.code in (0, 2):
|
|
240
|
+
return int(exc.code)
|
|
241
|
+
error_line(
|
|
242
|
+
f"internal error ({type(exc).__name__}); the review is incomplete "
|
|
243
|
+
"(set CODEVETO_DEBUG=1 for a traceback)"
|
|
244
|
+
)
|
|
245
|
+
if _debug():
|
|
246
|
+
traceback.print_exc(file=sys.stderr)
|
|
247
|
+
return 2
|
codeveto/cli_explain.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""``codeveto explain RULE_ID``: one rule's metadata, rationale and hint.
|
|
2
|
+
|
|
3
|
+
An unknown ID exits 2 and suggests the closest known IDs.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import argparse
|
|
9
|
+
import difflib
|
|
10
|
+
|
|
11
|
+
from codeveto import rules
|
|
12
|
+
from codeveto.cli import error_line, write_stdout
|
|
13
|
+
from codeveto.render import safe
|
|
14
|
+
|
|
15
|
+
__all__ = ["run"]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _names(values: frozenset[str] | None, everything: str) -> str:
|
|
19
|
+
return everything if values is None else ", ".join(sorted(values)) or "none"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def run(args: argparse.Namespace) -> int:
|
|
23
|
+
registry = rules.load_rules()
|
|
24
|
+
entry = registry.get(args.rule_id)
|
|
25
|
+
if entry is None:
|
|
26
|
+
close = difflib.get_close_matches(args.rule_id, sorted(registry.ids()), n=3)
|
|
27
|
+
hint = f"; did you mean {', '.join(close)}?" if close else "; see codeveto rules"
|
|
28
|
+
error_line(f"unknown rule {args.rule_id!r}{hint}")
|
|
29
|
+
return 2
|
|
30
|
+
meta = entry.meta
|
|
31
|
+
lines = [
|
|
32
|
+
f"{meta.id} {meta.severity}",
|
|
33
|
+
safe.text(meta.title),
|
|
34
|
+
"",
|
|
35
|
+
safe.text(meta.rationale),
|
|
36
|
+
"",
|
|
37
|
+
f"Hint: {safe.text(meta.hint)}",
|
|
38
|
+
"",
|
|
39
|
+
f"Pack: {meta.pack}",
|
|
40
|
+
f"Scope: {meta.scope}",
|
|
41
|
+
f"Languages: {_names(meta.languages, 'all')}",
|
|
42
|
+
f"Classes: {_names(meta.classes, 'all text files')}",
|
|
43
|
+
f"Tags: {_names(meta.tags, 'none')}",
|
|
44
|
+
f"Default: {'enabled' if meta.default_enabled else 'off (enable in config)'}",
|
|
45
|
+
f"Waivable: {'no (SPEC 6.3)' if meta.non_waivable else 'yes'}",
|
|
46
|
+
f"Introduced-only filter: {'on' if meta.introduced_only else 'off'}",
|
|
47
|
+
]
|
|
48
|
+
write_stdout("\n".join(lines) + "\n")
|
|
49
|
+
return 0
|
codeveto/cli_hook.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""``codeveto hook install|uninstall``: CodeVeto's Git hooks (SPEC 10.2).
|
|
2
|
+
|
|
3
|
+
See :mod:`codeveto.integrations.hooks`. Exit 0 when the hook was installed or
|
|
4
|
+
removed (or there was nothing to remove), 2 when it was refused; each step is
|
|
5
|
+
reported on stderr.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
|
|
12
|
+
from codeveto.cli import error_line
|
|
13
|
+
from codeveto.integrations import hooks
|
|
14
|
+
|
|
15
|
+
__all__ = ["run"]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def run(args: argparse.Namespace) -> int:
|
|
19
|
+
name = "pre-commit" if args.pre_commit else "pre-push"
|
|
20
|
+
repo = args.repo or "."
|
|
21
|
+
try:
|
|
22
|
+
if args.action == "install":
|
|
23
|
+
messages = hooks.install(repo, name, force=args.force)
|
|
24
|
+
else:
|
|
25
|
+
messages = hooks.uninstall(repo, name)
|
|
26
|
+
except hooks.HookError as exc:
|
|
27
|
+
error_line(f"hook: {exc}")
|
|
28
|
+
return 2
|
|
29
|
+
for message in messages:
|
|
30
|
+
error_line(f"hook: {message}")
|
|
31
|
+
return 0
|