jarbin-c-coding-style 2.0.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.
- jarbin_c_coding_style-2.0.0.dist-info/METADATA +150 -0
- jarbin_c_coding_style-2.0.0.dist-info/RECORD +186 -0
- jarbin_c_coding_style-2.0.0.dist-info/WHEEL +4 -0
- jarbin_c_coding_style-2.0.0.dist-info/entry_points.txt +2 -0
- jarbin_c_coding_style-2.0.0.dist-info/licenses/LICENSE +674 -0
- sources/__init__.py +0 -0
- sources/__main__.py +6 -0
- sources/analysis/__init__.py +0 -0
- sources/analysis/directives.py +258 -0
- sources/analysis/lexer.py +288 -0
- sources/analysis/parser.py +1754 -0
- sources/analysis/project.py +286 -0
- sources/analysis/source.py +326 -0
- sources/analysis/syntax.py +238 -0
- sources/analysis/tokens.py +116 -0
- sources/analysis/unit.py +700 -0
- sources/application.py +118 -0
- sources/build_info.json +6 -0
- sources/cli/__init__.py +0 -0
- sources/cli/arguments.py +507 -0
- sources/cli/completion.py +610 -0
- sources/cli/context.py +258 -0
- sources/cli/converters.py +204 -0
- sources/cli/help.py +162 -0
- sources/cli/parser.py +659 -0
- sources/configs/__init__.py +0 -0
- sources/configs/editor.py +207 -0
- sources/configs/ini.py +327 -0
- sources/configs/project.py +735 -0
- sources/configs/schema/__init__.py +0 -0
- sources/configs/schema/project.py +68 -0
- sources/constants.py +801 -0
- sources/diagnostics/__init__.py +0 -0
- sources/diagnostics/fingerprint.py +82 -0
- sources/diagnostics/model.py +146 -0
- sources/engine/__init__.py +0 -0
- sources/engine/api.py +355 -0
- sources/engine/examples.py +157 -0
- sources/engine/outcome.py +114 -0
- sources/engine/plan.py +440 -0
- sources/engine/runner.py +345 -0
- sources/engine/scanner.py +179 -0
- sources/enums.py +838 -0
- sources/errors/__init__.py +0 -0
- sources/errors/exceptions.py +727 -0
- sources/fixers/__init__.py +0 -0
- sources/fixers/base.py +209 -0
- sources/fixers/conflicts.py +129 -0
- sources/fixers/diff.py +73 -0
- sources/fixers/engine.py +434 -0
- sources/fixers/journal.py +290 -0
- sources/fixers/kinds.py +30 -0
- sources/fixers/layout.py +411 -0
- sources/fixers/model.py +151 -0
- sources/fixers/registry.py +54 -0
- sources/fixers/text.py +353 -0
- sources/fixers/verify.py +123 -0
- sources/fixers/writer.py +88 -0
- sources/integrations/__init__.py +0 -0
- sources/integrations/ci/__init__.py +0 -0
- sources/integrations/ci/github.py +183 -0
- sources/integrations/editors/__init__.py +0 -0
- sources/integrations/git/__init__.py +0 -0
- sources/integrations/git/repository.py +176 -0
- sources/integrations/update/__init__.py +0 -0
- sources/integrations/update/installers.py +529 -0
- sources/integrations/update/releases.py +397 -0
- sources/integrations/update/state.py +143 -0
- sources/outputs/__init__.py +0 -0
- sources/outputs/github/__init__.py +0 -0
- sources/outputs/github/renderer.py +135 -0
- sources/outputs/html/__init__.py +0 -0
- sources/outputs/html/renderer.py +694 -0
- sources/outputs/json/__init__.py +0 -0
- sources/outputs/json/renderer.py +154 -0
- sources/outputs/json/report.schema.json +121 -0
- sources/outputs/junit/__init__.py +0 -0
- sources/outputs/junit/renderer.py +151 -0
- sources/outputs/registry.py +135 -0
- sources/outputs/report.py +224 -0
- sources/outputs/sarif/__init__.py +0 -0
- sources/outputs/sarif/renderer.py +182 -0
- sources/outputs/terminal/__init__.py +0 -0
- sources/outputs/terminal/renderer.py +184 -0
- sources/outputs/terminal/style.py +159 -0
- sources/rules/__init__.py +0 -0
- sources/rules/arguments.py +362 -0
- sources/rules/catalog.py +227 -0
- sources/rules/categories/advanced/constant_pointers.ini +46 -0
- sources/rules/categories/advanced/line_break_at_end_of_file.ini +44 -0
- sources/rules/categories/advanced/static.ini +65 -0
- sources/rules/categories/advanced/typing.ini +47 -0
- sources/rules/categories/control_structures/conditional_branching.ini +89 -0
- sources/rules/categories/control_structures/goto.ini +45 -0
- sources/rules/categories/control_structures/ternary_operators.ini +57 -0
- sources/rules/categories/extensions/language_extensions.ini +71 -0
- sources/rules/categories/files_organization/contents_of_the_repository.ini +85 -0
- sources/rules/categories/files_organization/file_coherence.ini +166 -0
- sources/rules/categories/files_organization/file_extension.ini +68 -0
- sources/rules/categories/files_organization/fixtures/program +0 -0
- sources/rules/categories/files_organization/naming_files_and_folders.ini +74 -0
- sources/rules/categories/functions/coherence_of_functions.ini +45 -0
- sources/rules/categories/functions/comments_inside_a_function.ini +45 -0
- sources/rules/categories/functions/functions_without_parameters.ini +39 -0
- sources/rules/categories/functions/naming_functions.ini +79 -0
- sources/rules/categories/functions/number_of_columns.ini +61 -0
- sources/rules/categories/functions/number_of_lines.ini +70 -0
- sources/rules/categories/functions/number_of_parameters.ini +64 -0
- sources/rules/categories/functions/structures_as_parameters.ini +44 -0
- sources/rules/categories/global_scope/constant_values.ini +49 -0
- sources/rules/categories/global_scope/file_header.ini +92 -0
- sources/rules/categories/global_scope/global_variables.ini +53 -0
- sources/rules/categories/global_scope/include.ini +39 -0
- sources/rules/categories/global_scope/indentation_of_preprocessor.ini +62 -0
- sources/rules/categories/global_scope/inline_assembly.ini +43 -0
- sources/rules/categories/global_scope/leading_trailing_lines.ini +58 -0
- sources/rules/categories/global_scope/line_endings.ini +53 -0
- sources/rules/categories/global_scope/separation_of_functions.ini +61 -0
- sources/rules/categories/global_scope/trailing_spaces.ini +45 -0
- sources/rules/categories/header_files/content.ini +104 -0
- sources/rules/categories/header_files/include_guard.ini +47 -0
- sources/rules/categories/header_files/macros.ini +57 -0
- sources/rules/categories/layout_inside_a_function_scope/blank_lines.ini +71 -0
- sources/rules/categories/layout_inside_a_function_scope/code_line_content.ini +112 -0
- sources/rules/categories/layout_inside_a_function_scope/curly_brackets.ini +94 -0
- sources/rules/categories/layout_inside_a_function_scope/indentation.ini +96 -0
- sources/rules/categories/layout_inside_a_function_scope/spaces.ini +113 -0
- sources/rules/categories/layout_inside_a_function_scope/variable_declarations.ini +65 -0
- sources/rules/categories/variables_and_types/naming_identifiers.ini +74 -0
- sources/rules/categories/variables_and_types/pointers.ini +67 -0
- sources/rules/categories/variables_and_types/structures.ini +47 -0
- sources/rules/data/verbs.txt +182 -0
- sources/rules/definition.py +430 -0
- sources/rules/loader.py +925 -0
- sources/rules/paths.py +6 -0
- sources/rules/primitives/__init__.py +0 -0
- sources/rules/primitives/base.py +297 -0
- sources/rules/primitives/braces.py +211 -0
- sources/rules/primitives/directives.py +203 -0
- sources/rules/primitives/indentation.py +376 -0
- sources/rules/primitives/layout.py +199 -0
- sources/rules/primitives/naming.py +277 -0
- sources/rules/primitives/project.py +62 -0
- sources/rules/primitives/registry.py +49 -0
- sources/rules/primitives/repository.py +129 -0
- sources/rules/primitives/review.py +87 -0
- sources/rules/primitives/semantic.py +319 -0
- sources/rules/primitives/spacing.py +210 -0
- sources/rules/primitives/structure.py +586 -0
- sources/rules/primitives/syntax.py +200 -0
- sources/rules/primitives/text.py +408 -0
- sources/rules/primitives/tokens.py +84 -0
- sources/rules/template.py +138 -0
- sources/selftest/__init__.py +0 -0
- sources/selftest/cases.py +292 -0
- sources/services.py +90 -0
- sources/subcommands/__init__.py +0 -0
- sources/subcommands/base.py +45 -0
- sources/subcommands/check/__init__.py +0 -0
- sources/subcommands/check/command.py +45 -0
- sources/subcommands/check/fixing.py +224 -0
- sources/subcommands/check/handler.py +757 -0
- sources/subcommands/info/__init__.py +0 -0
- sources/subcommands/info/command.py +45 -0
- sources/subcommands/info/handler.py +527 -0
- sources/subcommands/registry.py +37 -0
- sources/subcommands/rule/__init__.py +0 -0
- sources/subcommands/rule/command.py +45 -0
- sources/subcommands/rule/handler.py +1067 -0
- sources/subcommands/rule/render.py +273 -0
- sources/subcommands/runner.py +124 -0
- sources/subcommands/test/__init__.py +0 -0
- sources/subcommands/test/command.py +45 -0
- sources/subcommands/test/handler.py +166 -0
- sources/subcommands/test/render.py +219 -0
- sources/subcommands/test/runner.py +430 -0
- sources/subcommands/update/__init__.py +0 -0
- sources/subcommands/update/command.py +45 -0
- sources/subcommands/update/handler.py +408 -0
- sources/utils/__init__.py +0 -0
- sources/utils/build_info.py +188 -0
- sources/utils/git.py +73 -0
- sources/utils/globs.py +98 -0
- sources/utils/logging.py +154 -0
- sources/utils/schema.py +77 -0
- sources/utils/versions.py +87 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
from collections.abc import Mapping
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# Variable GitHub Actions sets to "true".
|
|
7
|
+
GITHUB_ACTIONS_VARIABLE: str = "GITHUB_ACTIONS"
|
|
8
|
+
|
|
9
|
+
# File GitHub renders as the job summary (markdown, appended).
|
|
10
|
+
STEP_SUMMARY_VARIABLE: str = "GITHUB_STEP_SUMMARY"
|
|
11
|
+
|
|
12
|
+
# File of the step outputs (`key=value` lines, appended).
|
|
13
|
+
OUTPUT_VARIABLE: str = "GITHUB_OUTPUT"
|
|
14
|
+
|
|
15
|
+
# Rules listed in the step summary, most findings first.
|
|
16
|
+
SUMMARY_RULE_LIMIT: int = 20
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def in_github_actions(environment: Mapping[str, str]) -> bool:
|
|
20
|
+
"""
|
|
21
|
+
Whether the process runs in a GitHub Actions job.
|
|
22
|
+
|
|
23
|
+
Parameters
|
|
24
|
+
----------
|
|
25
|
+
environment : Mapping[str, str]
|
|
26
|
+
Process environment.
|
|
27
|
+
|
|
28
|
+
Returns
|
|
29
|
+
----------
|
|
30
|
+
bool
|
|
31
|
+
True under GitHub Actions.
|
|
32
|
+
"""
|
|
33
|
+
return environment.get(GITHUB_ACTIONS_VARIABLE) == "true"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _cell(text: Any) -> str:
|
|
37
|
+
"""
|
|
38
|
+
Text safe inside a markdown table cell.
|
|
39
|
+
|
|
40
|
+
Parameters
|
|
41
|
+
----------
|
|
42
|
+
text : Any
|
|
43
|
+
Raw value.
|
|
44
|
+
|
|
45
|
+
Returns
|
|
46
|
+
----------
|
|
47
|
+
str
|
|
48
|
+
The escaped text.
|
|
49
|
+
"""
|
|
50
|
+
return str(text).replace("\\", "\\\\").replace("|", "\\|").replace("\n", " ").replace("\r", " ")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _one_line(text: Any) -> str:
|
|
54
|
+
"""
|
|
55
|
+
Value safe for a `key=value` output line.
|
|
56
|
+
|
|
57
|
+
Parameters
|
|
58
|
+
----------
|
|
59
|
+
text : Any
|
|
60
|
+
Raw value.
|
|
61
|
+
|
|
62
|
+
Returns
|
|
63
|
+
----------
|
|
64
|
+
str
|
|
65
|
+
The value on one line.
|
|
66
|
+
"""
|
|
67
|
+
return str(text).replace("\r", " ").replace("\n", " ")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def step_summary(report: Mapping[str, Any], codes: Mapping[str, str]) -> str:
|
|
71
|
+
"""
|
|
72
|
+
Markdown job summary of a check report (plan 10, section 5).
|
|
73
|
+
|
|
74
|
+
Parameters
|
|
75
|
+
----------
|
|
76
|
+
report : Mapping[str, Any]
|
|
77
|
+
The JSON report (contract v1, plan 08).
|
|
78
|
+
codes : Mapping[str, str]
|
|
79
|
+
Display code by rule ID.
|
|
80
|
+
|
|
81
|
+
Returns
|
|
82
|
+
----------
|
|
83
|
+
str
|
|
84
|
+
The markdown.
|
|
85
|
+
"""
|
|
86
|
+
run: Mapping[str, Any] = report["run"]
|
|
87
|
+
summary: Mapping[str, Any] = report["summary"]
|
|
88
|
+
totals: Mapping[str, int] = summary["diagnostics"]
|
|
89
|
+
passed: bool = run["status"] == "ok"
|
|
90
|
+
lines: list[str] = [
|
|
91
|
+
f"## jccs: {'passed' if passed else 'failed'}",
|
|
92
|
+
"",
|
|
93
|
+
f"{_cell(run['exit_reason'])}",
|
|
94
|
+
"",
|
|
95
|
+
"| Files analysed | Findings | Fatal | Major | Minor | Info | To review | Fixed |",
|
|
96
|
+
"|---:|---:|---:|---:|---:|---:|---:|---:|",
|
|
97
|
+
f"| {summary['files']['scanned']} | {totals['total']} | {totals['fatal']} | {totals['major']} | {totals['minor']} | {totals['info']} | {summary['review']} | {summary['fixed']} |",
|
|
98
|
+
]
|
|
99
|
+
rules: list[tuple[str, int]] = sorted(summary["by_rule"].items(), key=lambda item: (-item[1], item[0]))
|
|
100
|
+
if rules:
|
|
101
|
+
lines += ["", "| Rule | Findings |", "|---|---:|"]
|
|
102
|
+
lines += [f"| `{_cell(codes.get(rule_id, rule_id))}` {_cell(rule_id)} | {count} |" for rule_id, count in rules[:SUMMARY_RULE_LIMIT]]
|
|
103
|
+
if len(rules) > SUMMARY_RULE_LIMIT:
|
|
104
|
+
lines.append(f"| ... {len(rules) - SUMMARY_RULE_LIMIT} more rule(s) | |")
|
|
105
|
+
return "\n".join(lines) + "\n\n"
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def step_outputs(report: Mapping[str, Any]) -> str:
|
|
109
|
+
"""
|
|
110
|
+
Step outputs of a check report: findings, exit_code, exit_reason.
|
|
111
|
+
|
|
112
|
+
Parameters
|
|
113
|
+
----------
|
|
114
|
+
report : Mapping[str, Any]
|
|
115
|
+
The JSON report (contract v1, plan 08).
|
|
116
|
+
|
|
117
|
+
Returns
|
|
118
|
+
----------
|
|
119
|
+
str
|
|
120
|
+
`key=value` lines.
|
|
121
|
+
"""
|
|
122
|
+
run: Mapping[str, Any] = report["run"]
|
|
123
|
+
values: dict[str, Any] = {
|
|
124
|
+
"findings": report["summary"]["diagnostics"]["total"],
|
|
125
|
+
"counted": report["summary"]["counted"],
|
|
126
|
+
"exit_code": run["exit_code"],
|
|
127
|
+
"exit_reason": run["exit_reason"],
|
|
128
|
+
}
|
|
129
|
+
return "".join(f"{key}={_one_line(value)}\n" for key, value in values.items())
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _append(path_text: str | None, content: str) -> str | None:
|
|
133
|
+
"""
|
|
134
|
+
Append to a file named by an environment variable.
|
|
135
|
+
|
|
136
|
+
Parameters
|
|
137
|
+
----------
|
|
138
|
+
path_text : str | None
|
|
139
|
+
The file, or None when the variable is not set.
|
|
140
|
+
content : str
|
|
141
|
+
Text to append.
|
|
142
|
+
|
|
143
|
+
Returns
|
|
144
|
+
----------
|
|
145
|
+
str | None
|
|
146
|
+
The problem, or None on success (or when there is no file).
|
|
147
|
+
"""
|
|
148
|
+
if not path_text:
|
|
149
|
+
return None
|
|
150
|
+
try:
|
|
151
|
+
with Path(path_text).open("a", encoding="utf-8") as stream:
|
|
152
|
+
stream.write(content)
|
|
153
|
+
except OSError as error:
|
|
154
|
+
return f"cannot write {path_text}: {error.strerror}"
|
|
155
|
+
return None
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def write_github_files(environment: Mapping[str, str], report: Mapping[str, Any], codes: Mapping[str, str]) -> list[str]:
|
|
159
|
+
"""
|
|
160
|
+
Under GitHub Actions, append the job summary to $GITHUB_STEP_SUMMARY and the outputs to $GITHUB_OUTPUT
|
|
161
|
+
(only the files that are defined; nothing outside GitHub Actions).
|
|
162
|
+
|
|
163
|
+
Parameters
|
|
164
|
+
----------
|
|
165
|
+
environment : Mapping[str, str]
|
|
166
|
+
Process environment.
|
|
167
|
+
report : Mapping[str, Any]
|
|
168
|
+
The JSON report (contract v1).
|
|
169
|
+
codes : Mapping[str, str]
|
|
170
|
+
Display code by rule ID.
|
|
171
|
+
|
|
172
|
+
Returns
|
|
173
|
+
----------
|
|
174
|
+
list[str]
|
|
175
|
+
Problems met (empty on success).
|
|
176
|
+
"""
|
|
177
|
+
if not in_github_actions(environment):
|
|
178
|
+
return []
|
|
179
|
+
problems: list[str | None] = [
|
|
180
|
+
_append(environment.get(STEP_SUMMARY_VARIABLE), step_summary(report, codes)),
|
|
181
|
+
_append(environment.get(OUTPUT_VARIABLE), step_outputs(report)),
|
|
182
|
+
]
|
|
183
|
+
return [problem for problem in problems if problem is not None]
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import subprocess
|
|
2
|
+
from collections.abc import Mapping
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from sources.errors.exceptions import ProjectEnvironmentError
|
|
6
|
+
from sources.utils.git import git_run
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# Longest time given to one git call, in seconds.
|
|
10
|
+
GIT_TIMEOUT: float = 30.0
|
|
11
|
+
|
|
12
|
+
# `--changed-only` value selecting the files staged in the index (git hooks).
|
|
13
|
+
STAGED_REF: str = ":staged"
|
|
14
|
+
|
|
15
|
+
# Changes kept: added, copied, modified, renamed (reported under the new path); deletions are not files to check.
|
|
16
|
+
DIFF_FILTER: str = "--diff-filter=ACMR"
|
|
17
|
+
|
|
18
|
+
def run_git(directory: Path, *arguments: str, environment: Mapping[str, str] | None = None) -> bytes:
|
|
19
|
+
"""
|
|
20
|
+
Run git with an argument list (never through a shell), a timeout and a sanitised environment.
|
|
21
|
+
|
|
22
|
+
Parameters
|
|
23
|
+
----------
|
|
24
|
+
directory : Path
|
|
25
|
+
Directory git runs in.
|
|
26
|
+
*arguments : str
|
|
27
|
+
git arguments.
|
|
28
|
+
environment : Mapping[str, str] | None
|
|
29
|
+
Process environment (default: os.environ).
|
|
30
|
+
|
|
31
|
+
Returns
|
|
32
|
+
----------
|
|
33
|
+
bytes
|
|
34
|
+
Standard output.
|
|
35
|
+
|
|
36
|
+
Raises
|
|
37
|
+
----------
|
|
38
|
+
ProjectEnvironmentError
|
|
39
|
+
git is missing, timed out or failed.
|
|
40
|
+
"""
|
|
41
|
+
try:
|
|
42
|
+
completed: subprocess.CompletedProcess[bytes] = git_run(directory, *arguments, environment=environment, timeout=GIT_TIMEOUT)
|
|
43
|
+
except FileNotFoundError:
|
|
44
|
+
raise ProjectEnvironmentError("git is not installed.", "--changed-only needs the git executable.") from None
|
|
45
|
+
except subprocess.TimeoutExpired:
|
|
46
|
+
raise ProjectEnvironmentError(f"git {arguments[0]} timed out after {GIT_TIMEOUT:g} s.") from None
|
|
47
|
+
except OSError as error:
|
|
48
|
+
raise ProjectEnvironmentError(f"Cannot run git: {error.strerror}.") from None
|
|
49
|
+
if completed.returncode != 0:
|
|
50
|
+
lines: list[str] = [line.strip() for line in completed.stderr.decode("utf-8", "replace").splitlines() if line.strip()]
|
|
51
|
+
primary: list[str] = [line for line in lines if line.startswith(("fatal:", "error:"))]
|
|
52
|
+
message: str = (primary or lines or [f"exit {completed.returncode}"])[0]
|
|
53
|
+
raise ProjectEnvironmentError(f"git {arguments[0]} failed: {message}")
|
|
54
|
+
return completed.stdout
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _paths(output: bytes) -> list[str]:
|
|
58
|
+
"""
|
|
59
|
+
Paths of a NUL-separated git output.
|
|
60
|
+
|
|
61
|
+
Parameters
|
|
62
|
+
----------
|
|
63
|
+
output : bytes
|
|
64
|
+
git output (`-z`).
|
|
65
|
+
|
|
66
|
+
Returns
|
|
67
|
+
----------
|
|
68
|
+
list[str]
|
|
69
|
+
The paths.
|
|
70
|
+
"""
|
|
71
|
+
return [item for item in output.decode("utf-8", "surrogateescape").split("\0") if item]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def repository_root(directory: Path, environment: Mapping[str, str] | None = None) -> Path:
|
|
75
|
+
"""
|
|
76
|
+
Top-level directory of the git work tree containing a directory.
|
|
77
|
+
|
|
78
|
+
Parameters
|
|
79
|
+
----------
|
|
80
|
+
directory : Path
|
|
81
|
+
A directory.
|
|
82
|
+
environment : Mapping[str, str] | None
|
|
83
|
+
Process environment.
|
|
84
|
+
|
|
85
|
+
Returns
|
|
86
|
+
----------
|
|
87
|
+
Path
|
|
88
|
+
The work tree root.
|
|
89
|
+
|
|
90
|
+
Raises
|
|
91
|
+
----------
|
|
92
|
+
ProjectEnvironmentError
|
|
93
|
+
git is missing, or the directory is not in a work tree.
|
|
94
|
+
"""
|
|
95
|
+
try:
|
|
96
|
+
output: bytes = run_git(directory, "rev-parse", "--show-toplevel", environment=environment)
|
|
97
|
+
except ProjectEnvironmentError as error:
|
|
98
|
+
raise ProjectEnvironmentError(f"'{directory}' is not inside a git work tree ({error.args[0]})", "--changed-only only works in a git repository.") from None
|
|
99
|
+
return Path(output.decode("utf-8", "surrogateescape").strip())
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def verify_ref(root: Path, ref: str, environment: Mapping[str, str] | None = None) -> str:
|
|
103
|
+
"""
|
|
104
|
+
Commit a ref names, checked without letting a ref that starts with `-` act as an option.
|
|
105
|
+
|
|
106
|
+
Parameters
|
|
107
|
+
----------
|
|
108
|
+
root : Path
|
|
109
|
+
Work tree root.
|
|
110
|
+
ref : str
|
|
111
|
+
A branch, tag, SHA or expression.
|
|
112
|
+
environment : Mapping[str, str] | None
|
|
113
|
+
Process environment.
|
|
114
|
+
|
|
115
|
+
Returns
|
|
116
|
+
----------
|
|
117
|
+
str
|
|
118
|
+
The commit SHA.
|
|
119
|
+
|
|
120
|
+
Raises
|
|
121
|
+
----------
|
|
122
|
+
ProjectEnvironmentError
|
|
123
|
+
Unknown ref (with the shallow-clone remediation when relevant).
|
|
124
|
+
"""
|
|
125
|
+
try:
|
|
126
|
+
return run_git(root, "rev-parse", "--verify", "--quiet", "--end-of-options", f"{ref}^{{commit}}", environment=environment).decode().strip()
|
|
127
|
+
except ProjectEnvironmentError:
|
|
128
|
+
pass
|
|
129
|
+
shallow: bool = False
|
|
130
|
+
try:
|
|
131
|
+
shallow = run_git(root, "rev-parse", "--is-shallow-repository", environment=environment).decode().strip() == "true"
|
|
132
|
+
except ProjectEnvironmentError:
|
|
133
|
+
pass
|
|
134
|
+
hint: str = f"Fetch it first: git fetch origin {ref}"
|
|
135
|
+
if shallow:
|
|
136
|
+
hint = f"This is a shallow clone: fetch the history (actions/checkout with fetch-depth: 0) or run git fetch origin {ref}."
|
|
137
|
+
raise ProjectEnvironmentError(f"Unknown git ref '{ref}'.", hint)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def changed_files(directory: Path, ref: str, environment: Mapping[str, str] | None = None) -> list[Path]:
|
|
141
|
+
"""
|
|
142
|
+
Files changed for `--changed-only` (plan 10, section 3), as absolute paths:
|
|
143
|
+
- `:staged`: files staged in the index;
|
|
144
|
+
- a ref: files changed between the merge base of the ref and HEAD, plus staged and unstaged changes and
|
|
145
|
+
untracked files (not ignored).
|
|
146
|
+
Deleted files are left out, renamed files appear under their new path, submodules are ignored.
|
|
147
|
+
|
|
148
|
+
Parameters
|
|
149
|
+
----------
|
|
150
|
+
directory : Path
|
|
151
|
+
A directory inside the work tree.
|
|
152
|
+
ref : str
|
|
153
|
+
`:staged` or a git ref.
|
|
154
|
+
environment : Mapping[str, str] | None
|
|
155
|
+
Process environment.
|
|
156
|
+
|
|
157
|
+
Returns
|
|
158
|
+
----------
|
|
159
|
+
list[Path]
|
|
160
|
+
The files, sorted.
|
|
161
|
+
|
|
162
|
+
Raises
|
|
163
|
+
----------
|
|
164
|
+
ProjectEnvironmentError
|
|
165
|
+
No git, not a work tree, or unknown ref.
|
|
166
|
+
"""
|
|
167
|
+
root: Path = repository_root(directory, environment)
|
|
168
|
+
names: set[str] = set()
|
|
169
|
+
if ref == STAGED_REF:
|
|
170
|
+
names.update(_paths(run_git(root, "diff", "--cached", "--name-only", "-z", DIFF_FILTER, "--ignore-submodules", environment=environment)))
|
|
171
|
+
else:
|
|
172
|
+
commit: str = verify_ref(root, ref, environment)
|
|
173
|
+
names.update(_paths(run_git(root, "diff", "--name-only", "-z", DIFF_FILTER, "--ignore-submodules", f"{commit}...HEAD", "--", environment=environment)))
|
|
174
|
+
names.update(_paths(run_git(root, "diff", "--name-only", "-z", DIFF_FILTER, "--ignore-submodules", "HEAD", "--", environment=environment)))
|
|
175
|
+
names.update(_paths(run_git(root, "ls-files", "-z", "--others", "--exclude-standard", environment=environment)))
|
|
176
|
+
return sorted(root / name for name in names)
|
|
File without changes
|