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/languages.py
ADDED
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
"""Language IDs and file classes (SPEC 5.4): a frozen table and :func:`classify`.
|
|
2
|
+
|
|
3
|
+
Every changed file gets exactly one language and one file class. The class is
|
|
4
|
+
the first that matches, in this order: ``agent-config``, ``workflow``,
|
|
5
|
+
``lockfile``, ``manifest``, ``vendored``/``generated``/``minified``, ``test``,
|
|
6
|
+
``docs``; otherwise ``source`` for code languages, ``config`` for YAML, JSON,
|
|
7
|
+
JSONC and TOML, and ``text`` for everything else. Detection uses only the path
|
|
8
|
+
(and, for the language, the first line and ``<script>`` tags of the text and
|
|
9
|
+
the executable bit): in-file markers such as ``@generated`` and
|
|
10
|
+
``.gitattributes`` never change the class.
|
|
11
|
+
|
|
12
|
+
:func:`test_regions` finds in-file test code: Rust ``#[cfg(test)]`` items and
|
|
13
|
+
Go ``Test``/``Benchmark``/``Fuzz``/``Example`` functions.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import re
|
|
19
|
+
from dataclasses import dataclass
|
|
20
|
+
from functools import lru_cache
|
|
21
|
+
|
|
22
|
+
from codeveto import lexers
|
|
23
|
+
from codeveto.globs import Glob, compile_glob
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"CODE_LANGUAGES",
|
|
27
|
+
"CONFIG_LANGUAGES",
|
|
28
|
+
"FILE_CLASSES",
|
|
29
|
+
"LANGUAGES",
|
|
30
|
+
"FileInfo",
|
|
31
|
+
"classify",
|
|
32
|
+
"language_of",
|
|
33
|
+
"shebang_language",
|
|
34
|
+
"test_regions",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
LANGUAGES: tuple[str, ...] = (
|
|
38
|
+
"python",
|
|
39
|
+
"javascript",
|
|
40
|
+
"typescript",
|
|
41
|
+
"go",
|
|
42
|
+
"rust",
|
|
43
|
+
"java",
|
|
44
|
+
"kotlin",
|
|
45
|
+
"shell",
|
|
46
|
+
"dockerfile",
|
|
47
|
+
"sql",
|
|
48
|
+
"yaml",
|
|
49
|
+
"json",
|
|
50
|
+
"jsonc",
|
|
51
|
+
"toml",
|
|
52
|
+
"markdown",
|
|
53
|
+
"text",
|
|
54
|
+
)
|
|
55
|
+
CODE_LANGUAGES = frozenset(
|
|
56
|
+
{
|
|
57
|
+
"python",
|
|
58
|
+
"javascript",
|
|
59
|
+
"typescript",
|
|
60
|
+
"go",
|
|
61
|
+
"rust",
|
|
62
|
+
"java",
|
|
63
|
+
"kotlin",
|
|
64
|
+
"shell",
|
|
65
|
+
"dockerfile",
|
|
66
|
+
"sql",
|
|
67
|
+
}
|
|
68
|
+
)
|
|
69
|
+
CONFIG_LANGUAGES = frozenset({"yaml", "json", "jsonc", "toml"})
|
|
70
|
+
|
|
71
|
+
FILE_CLASSES: tuple[str, ...] = (
|
|
72
|
+
"agent-config",
|
|
73
|
+
"workflow",
|
|
74
|
+
"lockfile",
|
|
75
|
+
"manifest",
|
|
76
|
+
"vendored",
|
|
77
|
+
"generated",
|
|
78
|
+
"minified",
|
|
79
|
+
"test",
|
|
80
|
+
"docs",
|
|
81
|
+
"source",
|
|
82
|
+
"config",
|
|
83
|
+
"text",
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@dataclass(frozen=True, slots=True)
|
|
88
|
+
class FileInfo:
|
|
89
|
+
"""The language ID and file class of one path."""
|
|
90
|
+
|
|
91
|
+
language: str
|
|
92
|
+
file_class: str
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# --- Language table --------------------------------------------------------------
|
|
96
|
+
|
|
97
|
+
_EXTENSIONS: dict[str, str] = {
|
|
98
|
+
".py": "python",
|
|
99
|
+
".pyi": "python",
|
|
100
|
+
".pyw": "python",
|
|
101
|
+
".js": "javascript",
|
|
102
|
+
".mjs": "javascript",
|
|
103
|
+
".cjs": "javascript",
|
|
104
|
+
".jsx": "javascript",
|
|
105
|
+
".ts": "typescript",
|
|
106
|
+
".mts": "typescript",
|
|
107
|
+
".cts": "typescript",
|
|
108
|
+
".tsx": "typescript",
|
|
109
|
+
".go": "go",
|
|
110
|
+
".rs": "rust",
|
|
111
|
+
".java": "java",
|
|
112
|
+
".kt": "kotlin",
|
|
113
|
+
".kts": "kotlin",
|
|
114
|
+
".sh": "shell",
|
|
115
|
+
".bash": "shell",
|
|
116
|
+
".zsh": "shell",
|
|
117
|
+
".ksh": "shell",
|
|
118
|
+
".bats": "shell",
|
|
119
|
+
".sql": "sql",
|
|
120
|
+
".yml": "yaml",
|
|
121
|
+
".yaml": "yaml",
|
|
122
|
+
".json": "json",
|
|
123
|
+
".geojson": "json",
|
|
124
|
+
".webmanifest": "json",
|
|
125
|
+
".jsonc": "jsonc",
|
|
126
|
+
".toml": "toml",
|
|
127
|
+
".md": "markdown",
|
|
128
|
+
".markdown": "markdown",
|
|
129
|
+
".mdx": "markdown",
|
|
130
|
+
".txt": "text",
|
|
131
|
+
".rst": "text",
|
|
132
|
+
".mk": "text",
|
|
133
|
+
".dockerfile": "dockerfile",
|
|
134
|
+
".containerfile": "dockerfile",
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
_NAMES: dict[str, str] = {
|
|
138
|
+
"Dockerfile": "dockerfile",
|
|
139
|
+
"dockerfile": "dockerfile",
|
|
140
|
+
"Containerfile": "dockerfile",
|
|
141
|
+
"containerfile": "dockerfile",
|
|
142
|
+
"Makefile": "text",
|
|
143
|
+
"makefile": "text",
|
|
144
|
+
"GNUmakefile": "text",
|
|
145
|
+
"Pipfile": "toml",
|
|
146
|
+
"Pipfile.lock": "json",
|
|
147
|
+
"Cargo.lock": "toml",
|
|
148
|
+
"uv.lock": "toml",
|
|
149
|
+
"poetry.lock": "toml",
|
|
150
|
+
"composer.lock": "json",
|
|
151
|
+
"go.mod": "text",
|
|
152
|
+
"go.sum": "text",
|
|
153
|
+
"Gemfile": "text",
|
|
154
|
+
"Gemfile.lock": "text",
|
|
155
|
+
"yarn.lock": "text",
|
|
156
|
+
".bashrc": "shell",
|
|
157
|
+
".bash_profile": "shell",
|
|
158
|
+
".bash_login": "shell",
|
|
159
|
+
".bash_logout": "shell",
|
|
160
|
+
".profile": "shell",
|
|
161
|
+
".zshrc": "shell",
|
|
162
|
+
".zshenv": "shell",
|
|
163
|
+
".zprofile": "shell",
|
|
164
|
+
".zlogin": "shell",
|
|
165
|
+
".zlogout": "shell",
|
|
166
|
+
".kshrc": "shell",
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
# JSON files that are JSON with comments by convention.
|
|
170
|
+
_JSONC = tuple(
|
|
171
|
+
compile_glob(p)
|
|
172
|
+
for p in (
|
|
173
|
+
"**/tsconfig*.json",
|
|
174
|
+
"**/jsconfig*.json",
|
|
175
|
+
"**/.vscode/**",
|
|
176
|
+
"**/.devcontainer/devcontainer.json",
|
|
177
|
+
"**/.devcontainer/*/devcontainer.json",
|
|
178
|
+
"**/.devcontainer.json",
|
|
179
|
+
)
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
_INTERPRETERS: dict[str, str] = {
|
|
183
|
+
"python": "python",
|
|
184
|
+
"pypy": "python",
|
|
185
|
+
"node": "javascript",
|
|
186
|
+
"nodejs": "javascript",
|
|
187
|
+
"bun": "javascript",
|
|
188
|
+
"deno": "typescript",
|
|
189
|
+
"ts-node": "typescript",
|
|
190
|
+
"tsx": "typescript",
|
|
191
|
+
"sh": "shell",
|
|
192
|
+
"bash": "shell",
|
|
193
|
+
"dash": "shell",
|
|
194
|
+
"ash": "shell",
|
|
195
|
+
"zsh": "shell",
|
|
196
|
+
"ksh": "shell",
|
|
197
|
+
"mksh": "shell",
|
|
198
|
+
}
|
|
199
|
+
_ENV_OPTIONS_WITH_ARG = frozenset({"-u", "-C", "-P", "--unset", "--chdir"})
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _basename(path: str) -> str:
|
|
203
|
+
return path.rsplit("/", 1)[-1]
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _extension(name: str) -> str:
|
|
207
|
+
dot = name.rfind(".")
|
|
208
|
+
return name[dot:] if dot > 0 else ""
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def shebang_language(text: str | None) -> str | None:
|
|
212
|
+
"""Language named by a ``#!`` first line, ``None`` if there is none or it is unknown.
|
|
213
|
+
|
|
214
|
+
Handles ``/usr/bin/env`` with options, ``-S`` split strings and variable
|
|
215
|
+
assignments, and versioned names such as ``python3.12``.
|
|
216
|
+
"""
|
|
217
|
+
if text and text.startswith("\ufeff"):
|
|
218
|
+
text = text[1:]
|
|
219
|
+
if not text or not text.startswith("#!"):
|
|
220
|
+
return None
|
|
221
|
+
newline = text.find("\n", 0, 512)
|
|
222
|
+
first = text[2 : newline if newline >= 0 else min(len(text), 512)]
|
|
223
|
+
words = first.replace("\t", " ").split()
|
|
224
|
+
if not words:
|
|
225
|
+
return None
|
|
226
|
+
program = _basename(words[0])
|
|
227
|
+
rest = words[1:]
|
|
228
|
+
if program == "env":
|
|
229
|
+
program = ""
|
|
230
|
+
i = 0
|
|
231
|
+
while i < len(rest):
|
|
232
|
+
word = rest[i]
|
|
233
|
+
if word == "--":
|
|
234
|
+
i += 1
|
|
235
|
+
break
|
|
236
|
+
if word in ("-S", "--split-string"):
|
|
237
|
+
i += 1
|
|
238
|
+
continue
|
|
239
|
+
if word.startswith("-S") and len(word) > 2:
|
|
240
|
+
rest[i] = word[2:]
|
|
241
|
+
continue
|
|
242
|
+
if word.startswith("--split-string="):
|
|
243
|
+
rest[i] = word.split("=", 1)[1]
|
|
244
|
+
continue
|
|
245
|
+
if word in _ENV_OPTIONS_WITH_ARG:
|
|
246
|
+
i += 2
|
|
247
|
+
continue
|
|
248
|
+
if word.startswith("-"):
|
|
249
|
+
i += 1
|
|
250
|
+
continue
|
|
251
|
+
if "=" in word:
|
|
252
|
+
i += 1
|
|
253
|
+
continue
|
|
254
|
+
break
|
|
255
|
+
if i < len(rest):
|
|
256
|
+
program = _basename(rest[i])
|
|
257
|
+
name = program.rstrip("0123456789.")
|
|
258
|
+
return _INTERPRETERS.get(name)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def _sfc_language(text: str | None) -> str:
|
|
262
|
+
if text:
|
|
263
|
+
for block in lexers.script_blocks(text):
|
|
264
|
+
if block.is_code and block.lang in ("ts", "typescript", "tsx"):
|
|
265
|
+
return "typescript"
|
|
266
|
+
return "javascript"
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _path_language(path: str, text: str | None) -> str | None:
|
|
270
|
+
name = _basename(path)
|
|
271
|
+
known = _NAMES.get(name)
|
|
272
|
+
if known is not None:
|
|
273
|
+
return known
|
|
274
|
+
if name.startswith(
|
|
275
|
+
("Dockerfile.", "Containerfile.", "dockerfile.", "containerfile.")
|
|
276
|
+
) and not name.endswith(".dockerignore"):
|
|
277
|
+
return "dockerfile"
|
|
278
|
+
if name.endswith((".Dockerfile", ".Containerfile")):
|
|
279
|
+
return "dockerfile"
|
|
280
|
+
ext = _extension(name)
|
|
281
|
+
if ext in (".vue", ".svelte"):
|
|
282
|
+
return _sfc_language(text)
|
|
283
|
+
if ext == ".jsonc" or (ext == ".json" and any(g.match(path) for g in _JSONC)):
|
|
284
|
+
return "jsonc"
|
|
285
|
+
language = _EXTENSIONS.get(ext)
|
|
286
|
+
if language is None and ext:
|
|
287
|
+
language = _EXTENSIONS.get(ext.lower())
|
|
288
|
+
return language
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def language_of(path: str, *, text: str | None = None, executable: bool = False) -> str:
|
|
292
|
+
"""Language ID of ``path``: name and extension, then shebang, then ``text``.
|
|
293
|
+
|
|
294
|
+
An executable file whose path and first line name no language is ``shell``,
|
|
295
|
+
since that is what ``exec`` runs it with.
|
|
296
|
+
"""
|
|
297
|
+
language = _path_language(path, text)
|
|
298
|
+
if language is not None:
|
|
299
|
+
return language
|
|
300
|
+
from_shebang = shebang_language(text)
|
|
301
|
+
if from_shebang is not None:
|
|
302
|
+
return from_shebang
|
|
303
|
+
if executable and text and not text.startswith("#!") and not _extension(_basename(path)):
|
|
304
|
+
return "shell"
|
|
305
|
+
return "text"
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
# --- File classes -------------------------------------------------------------------
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def _globs(*patterns: str) -> tuple[Glob, ...]:
|
|
312
|
+
return tuple(compile_glob(p) for p in patterns)
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
_CLASS_TABLE: tuple[tuple[str, tuple[Glob, ...]], ...] = (
|
|
316
|
+
(
|
|
317
|
+
"agent-config",
|
|
318
|
+
_globs(
|
|
319
|
+
"**/AGENTS.md",
|
|
320
|
+
"**/CLAUDE.md",
|
|
321
|
+
"**/GEMINI.md",
|
|
322
|
+
".github/copilot-instructions.md",
|
|
323
|
+
"**/.cursorrules",
|
|
324
|
+
"**/.cursor/rules/**",
|
|
325
|
+
"**/.windsurfrules",
|
|
326
|
+
"**/.clinerules",
|
|
327
|
+
"**/.clinerules/**",
|
|
328
|
+
"**/.mcp.json",
|
|
329
|
+
"**/.claude/**",
|
|
330
|
+
"**/.codex/**",
|
|
331
|
+
"**/.vscode/settings.json",
|
|
332
|
+
"**/.vscode/tasks.json",
|
|
333
|
+
"**/.devcontainer/**",
|
|
334
|
+
),
|
|
335
|
+
),
|
|
336
|
+
(
|
|
337
|
+
"workflow",
|
|
338
|
+
_globs(
|
|
339
|
+
".github/workflows/*.y*ml",
|
|
340
|
+
".forgejo/workflows/*.y*ml",
|
|
341
|
+
".gitea/workflows/*.y*ml",
|
|
342
|
+
"**/action.y*ml",
|
|
343
|
+
),
|
|
344
|
+
),
|
|
345
|
+
(
|
|
346
|
+
"lockfile",
|
|
347
|
+
_globs(
|
|
348
|
+
"**/package-lock.json",
|
|
349
|
+
"**/yarn.lock",
|
|
350
|
+
"**/pnpm-lock.yaml",
|
|
351
|
+
"**/uv.lock",
|
|
352
|
+
"**/poetry.lock",
|
|
353
|
+
"**/Cargo.lock",
|
|
354
|
+
"**/go.sum",
|
|
355
|
+
"**/Gemfile.lock",
|
|
356
|
+
"**/composer.lock",
|
|
357
|
+
),
|
|
358
|
+
),
|
|
359
|
+
(
|
|
360
|
+
"manifest",
|
|
361
|
+
_globs(
|
|
362
|
+
"**/package.json",
|
|
363
|
+
"**/pyproject.toml",
|
|
364
|
+
"**/requirements*.txt",
|
|
365
|
+
"**/Pipfile",
|
|
366
|
+
"**/Cargo.toml",
|
|
367
|
+
"**/go.mod",
|
|
368
|
+
"**/Gemfile",
|
|
369
|
+
"**/composer.json",
|
|
370
|
+
),
|
|
371
|
+
),
|
|
372
|
+
("vendored", _globs("**/vendor/**", "**/third_party/**", "**/node_modules/**")),
|
|
373
|
+
("generated", _globs("**/dist/**", "**/*_pb2.py", "**/*.pb.go")),
|
|
374
|
+
("minified", _globs("**/*.min.js")),
|
|
375
|
+
(
|
|
376
|
+
"test",
|
|
377
|
+
_globs(
|
|
378
|
+
"**/test_*.py",
|
|
379
|
+
"**/*_test.py",
|
|
380
|
+
"**/conftest.py",
|
|
381
|
+
"**/tests/**",
|
|
382
|
+
"**/test/**",
|
|
383
|
+
"**/*_test.go",
|
|
384
|
+
"**/*.test.*",
|
|
385
|
+
"**/*.spec.*",
|
|
386
|
+
"**/__tests__/**",
|
|
387
|
+
"**/src/test/**",
|
|
388
|
+
),
|
|
389
|
+
),
|
|
390
|
+
("docs", _globs("**/*.md", "**/*.rst", "**/*.txt", "**/docs/**")),
|
|
391
|
+
)
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
@lru_cache(maxsize=8192)
|
|
395
|
+
def _path_class(path: str) -> str | None:
|
|
396
|
+
for name, patterns in _CLASS_TABLE:
|
|
397
|
+
if any(g.match(path) for g in patterns):
|
|
398
|
+
return name
|
|
399
|
+
return None
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def classify(path: str, *, text: str | None = None, executable: bool = False) -> FileInfo:
|
|
403
|
+
"""Language and file class of ``path`` (a Git POSIX path); pure and deterministic.
|
|
404
|
+
|
|
405
|
+
``text`` is the file content when available (for shebangs and Vue/Svelte
|
|
406
|
+
``<script lang>``); ``executable`` is the Git executable bit.
|
|
407
|
+
"""
|
|
408
|
+
language = language_of(path, text=text, executable=executable)
|
|
409
|
+
file_class = _path_class(path)
|
|
410
|
+
if file_class is None:
|
|
411
|
+
if language in CODE_LANGUAGES:
|
|
412
|
+
file_class = "source"
|
|
413
|
+
elif language in CONFIG_LANGUAGES:
|
|
414
|
+
file_class = "config"
|
|
415
|
+
else:
|
|
416
|
+
file_class = "text"
|
|
417
|
+
return FileInfo(language, file_class)
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
# --- In-file test regions ------------------------------------------------------------
|
|
421
|
+
|
|
422
|
+
_RUST_CFG = re.compile(r"#(!?)[ \t]*\[[ \t]*cfg[ \t]*\(")
|
|
423
|
+
_CFG_TOKEN = re.compile(r"[A-Za-z_][A-Za-z0-9_]*|\"[^\"\n]*\"|[(),=\]]|[ \t\r\n]+|.")
|
|
424
|
+
_CFG_MAX = 1024
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def _cfg_test(masked: str, pos: int) -> tuple[bool, int] | None:
|
|
428
|
+
"""Read the cfg predicate whose ``(`` ends at ``pos``.
|
|
429
|
+
|
|
430
|
+
Returns whether it implies ``test`` (``test`` itself, or inside ``all(...)``
|
|
431
|
+
or ``any(...)``, never under ``not(...)``) and the index after the
|
|
432
|
+
attribute's ``]``; ``None`` if it is malformed or longer than 1024
|
|
433
|
+
characters.
|
|
434
|
+
"""
|
|
435
|
+
funcs: list[str] = []
|
|
436
|
+
implied = False
|
|
437
|
+
last_word = ""
|
|
438
|
+
depth = 1
|
|
439
|
+
for m in _CFG_TOKEN.finditer(masked, pos, min(len(masked), pos + _CFG_MAX)):
|
|
440
|
+
tok = m.group()
|
|
441
|
+
if tok[0] in " \t\r\n":
|
|
442
|
+
continue
|
|
443
|
+
if tok in ("#", "{", "}", ";", "]"):
|
|
444
|
+
return None # not a cfg predicate: stop early, keeping this linear
|
|
445
|
+
if tok == "(":
|
|
446
|
+
funcs.append(last_word)
|
|
447
|
+
depth += 1
|
|
448
|
+
last_word = ""
|
|
449
|
+
continue
|
|
450
|
+
if last_word == "test" and tok != "=" and all(f in ("all", "any") for f in funcs):
|
|
451
|
+
implied = True
|
|
452
|
+
last_word = ""
|
|
453
|
+
if tok == ")":
|
|
454
|
+
depth -= 1
|
|
455
|
+
if depth == 0:
|
|
456
|
+
close = masked.find("]", m.end(), m.end() + 64)
|
|
457
|
+
return (implied, close + 1) if close >= 0 else None
|
|
458
|
+
if funcs:
|
|
459
|
+
funcs.pop()
|
|
460
|
+
elif tok[0].isalpha() or tok[0] == "_":
|
|
461
|
+
last_word = tok
|
|
462
|
+
return None
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
_GO_TEST_FUNC = re.compile(
|
|
466
|
+
r"^func[ \t]*(?:\([^)\n]*\)[ \t]*)?(?:Test|Benchmark|Fuzz|Example)(?![a-z])", re.MULTILINE
|
|
467
|
+
)
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
def _item_end(masked: str, pos: int) -> int:
|
|
471
|
+
"""End offset of the item starting at ``pos``: its matching ``}`` or ``;``."""
|
|
472
|
+
depth = 0
|
|
473
|
+
i = pos
|
|
474
|
+
n = len(masked)
|
|
475
|
+
while i < n:
|
|
476
|
+
c = masked[i]
|
|
477
|
+
if c in "([":
|
|
478
|
+
depth += 1
|
|
479
|
+
elif c in ")]":
|
|
480
|
+
depth = max(depth - 1, 0)
|
|
481
|
+
elif c == ";" and depth == 0:
|
|
482
|
+
return i + 1
|
|
483
|
+
elif c == "{" and depth == 0:
|
|
484
|
+
braces = 1
|
|
485
|
+
i += 1
|
|
486
|
+
while i < n:
|
|
487
|
+
c = masked[i]
|
|
488
|
+
if c == "{":
|
|
489
|
+
braces += 1
|
|
490
|
+
elif c == "}":
|
|
491
|
+
braces -= 1
|
|
492
|
+
if braces == 0:
|
|
493
|
+
return i + 1
|
|
494
|
+
i += 1
|
|
495
|
+
return n
|
|
496
|
+
i += 1
|
|
497
|
+
return n
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
def test_regions(
|
|
501
|
+
language: str, text: str | None, *, masked: str | None = None
|
|
502
|
+
) -> tuple[tuple[int, int], ...]:
|
|
503
|
+
"""1-based inclusive line ranges of in-file test code.
|
|
504
|
+
|
|
505
|
+
Rust: each ``#[cfg(test)]`` attribute (also ``cfg(all(test, …))`` and
|
|
506
|
+
``cfg(any(test, …))``) through the end of the item after it; an inner
|
|
507
|
+
``#![cfg(test)]`` makes the whole file a test region. Go: each top-level
|
|
508
|
+
``func Test…``/``Benchmark…``/``Fuzz…``/``Example…`` (methods included)
|
|
509
|
+
through its closing brace. Strings and comments are skipped via the
|
|
510
|
+
lexer; pass ``masked`` to reuse an existing masked text.
|
|
511
|
+
"""
|
|
512
|
+
if not text or language not in ("rust", "go"):
|
|
513
|
+
return ()
|
|
514
|
+
if masked is None:
|
|
515
|
+
masked = lexers.mask(text, lexers.lex(text, language))
|
|
516
|
+
index = lexers.LineIndex(text)
|
|
517
|
+
regions: list[tuple[int, int]] = []
|
|
518
|
+
pos = 0
|
|
519
|
+
while True:
|
|
520
|
+
if language == "go":
|
|
521
|
+
m = _GO_TEST_FUNC.search(masked, pos)
|
|
522
|
+
if m is None:
|
|
523
|
+
break
|
|
524
|
+
item = m.end()
|
|
525
|
+
else:
|
|
526
|
+
m = _RUST_CFG.search(masked, pos)
|
|
527
|
+
if m is None:
|
|
528
|
+
break
|
|
529
|
+
found = _cfg_test(masked, m.end())
|
|
530
|
+
if found is None or not found[0]:
|
|
531
|
+
pos = m.end()
|
|
532
|
+
continue
|
|
533
|
+
if m.group(1): # #![cfg(test)]: the whole module is test code
|
|
534
|
+
return ((1, max(index.line_count, 1)),)
|
|
535
|
+
item = found[1]
|
|
536
|
+
end = _item_end(masked, item)
|
|
537
|
+
regions.append((index.line_of(m.start()), index.line_of(max(end - 1, m.start()))))
|
|
538
|
+
pos = max(end, m.end())
|
|
539
|
+
return tuple(regions)
|