Repository navigation
Expand file tree
/
Copy pathworkspace_git.py
More file actions
310 lines (286 loc) · 12.4 KB
/
Copy pathworkspace_git.py
File metadata and controls
310 lines (286 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""Bounded Git views for a retained workspace.
The lightweight view fingerprints names, Git object identities and filesystem
metadata without constructing the binary patch or commit log. The browser can
therefore invalidate its on-demand full view without paying for that full view
on every job-detail refresh.
"""
from __future__ import annotations
import hashlib
import re
import secrets
from collections.abc import Callable, Mapping
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from pathlib import Path
from fastapi import HTTPException
RunSandbox = Callable[..., tuple[int, str, str]]
COMMIT_SHA = re.compile(r"^[0-9a-fA-F]{7,64}$")
# Git reads the member-writable repository configuration. These command-line
# overrides disable every configured command the read-only views do not need.
GIT_VIEW_ENV = {
"GIT_CONFIG_COUNT": "3",
"GIT_CONFIG_KEY_0": "core.fsmonitor",
"GIT_CONFIG_VALUE_0": "",
"GIT_CONFIG_KEY_1": "core.hooksPath",
"GIT_CONFIG_VALUE_1": "/dev/null",
"GIT_CONFIG_KEY_2": "log.showSignature",
"GIT_CONFIG_VALUE_2": "false",
"GIT_ALLOW_PROTOCOL": "",
}
# Filter names are repository-controlled, so neutralize them after discovery.
# Keep only Git LFS's canonical driver: a smudged file must still compare equal
# to its committed pointer. LFS extensions remain disabled.
NEUTRALIZE_GIT_FILTERS = """set -f; IFS='
'; n="${GIT_CONFIG_COUNT:-0}"
for key in $(git config --name-only --get-regexp '^(filter|lfs[.]extension)[.]'); do
value=$(git config --get "$key")
case "$key=$value" in
'filter.lfs.clean=git-lfs clean -- %f') continue ;;
'filter.lfs.smudge=git-lfs smudge -- %f'|'filter.lfs.smudge=git-lfs smudge --skip -- %f')
continue ;;
'filter.lfs.process=git-lfs filter-process'|'filter.lfs.process=git-lfs filter-process --skip')
continue ;;
esac
export "GIT_CONFIG_KEY_$n=$key" "GIT_CONFIG_VALUE_$n="
n=$((n + 1))
done
export GIT_CONFIG_COUNT="$n"; unset IFS; set +f
"""
def _status_view(status_text: str) -> tuple[str, list[dict[str, str]]]:
"""Decode porcelain ``-z`` records, keeping destination paths exact."""
records = status_text.split("\0")
branch = ""
if records and records[0].startswith("## "):
branch = records.pop(0)[3:].split("...", 1)[0]
changes: list[dict[str, str]] = []
index = 0
while index < len(records):
record = records[index]
index += 1
if len(record) < 4:
continue
code = record[:2]
changes.append({"code": code, "path": record[3:]})
# Under -z, rename/copy destinations precede a second original-path record.
if "R" in code or "C" in code:
index += 1
return branch, changes
def _raw_changes(raw_text: str) -> list[dict[str, str]]:
"""Decode base-relative ``--raw -z`` records."""
records = raw_text.split("\0")
changes: list[dict[str, str]] = []
index = 0
while index < len(records):
header = records[index]
index += 1
fields = header.split()
if len(fields) < 5 or not fields[0].startswith(":") or index >= len(records):
continue
status = fields[-1]
path = records[index]
index += 1
if status.startswith(("R", "C")):
if index >= len(records):
continue
path = records[index]
index += 1
changes.append({"code": f"{status[0]} ", "path": path})
return changes
def _revision_commands(target: str, raw_tag: str, stat_tag: str) -> str:
"""Write a content-sensitive but patch-free fingerprint transcript."""
return (
"git rev-parse --verify HEAD; revision_status=$?; "
'revision_index="${TMPDIR:-/tmp}/hyi-browser-revision-index.$$"; '
'rm -f "$revision_index"; '
f'GIT_INDEX_FILE="$revision_index" git read-tree {target}; read_tree_status=$?; '
'[ "$revision_status" -eq 0 ] && revision_status=$read_tree_status; '
f"printf '\n{raw_tag}\n'; "
f'GIT_INDEX_FILE="$revision_index" git --no-pager diff --raw -z '
f"--no-ext-diff --no-textconv --ignore-submodules=dirty --submodule=short {target} HEAD; "
"raw_status=$?; "
'[ "$revision_status" -eq 0 ] && revision_status=$raw_status; '
f"printf '\n{stat_tag}\n'; "
'GIT_INDEX_FILE="$revision_index" git ls-files -m -o --exclude-standard -z | '
"xargs -0 -r stat --printf='%n\\0%s\\0%y\\0%z\\0%i\\0%f\\0' -- 2>/dev/null || :; "
'rm -f "$revision_index"; '
)
def _revision_parts(revision_text: str, raw_tag: str, stat_tag: str) -> tuple[str, str]:
head_text, separator, raw_and_stat = revision_text.partition(f"\n{raw_tag}\n")
if not separator:
raise ValueError("missing raw revision section")
raw_text, separator, stat_text = raw_and_stat.partition(f"\n{stat_tag}\n")
if not separator:
raise ValueError("missing filesystem revision section")
return f"{head_text}\0{raw_text}\0{stat_text}", raw_text
def _revision(status_text: str, fingerprint: str) -> str:
return hashlib.sha256(f"{status_text}\0{fingerprint}".encode()).hexdigest()
def run_git_metadata(
run_sandbox: RunSandbox,
backend: Any,
workdir: Path | str,
base_sha: str | None = None,
*,
timeout_s: float,
max_output_bytes: int,
env: Mapping[str, str] = GIT_VIEW_ENV,
) -> dict[str, Any]:
"""Read branch/change names and a revision without constructing a patch."""
if base_sha and not COMMIT_SHA.fullmatch(base_sha):
raise HTTPException(status_code=400, detail="base SHA is not a commit hash")
target = base_sha or "HEAD"
marker = secrets.token_hex(16)
revision_tag = f"__HYI_{marker}_REVISION__"
raw_tag = f"__HYI_{marker}_RAW__"
stat_tag = f"__HYI_{marker}_STAT__"
status_tag = f"__HYI_{marker}_STATUS__"
script = NEUTRALIZE_GIT_FILTERS + (
"git --no-pager status --porcelain=v1 --branch -z "
"--untracked-files=all --ignore-submodules=dirty; "
"status_status=$?; "
f"printf '\n{revision_tag}\n'; "
+ _revision_commands(target, raw_tag, stat_tag)
+ "overall_status=$status_status; "
'[ "$overall_status" -eq 0 ] && overall_status=$revision_status; '
f"printf '\n{status_tag}%s\n' \"$overall_status\""
)
process_status, stdout, stderr = run_sandbox(
backend,
workdir=workdir,
script=script,
env={**env, "GIT_OPTIONAL_LOCKS": "0"},
timeout_s=timeout_s,
max_output_bytes=max_output_bytes,
)
status_text, separator, revision_and_status = stdout.partition(f"\n{revision_tag}\n")
if not separator:
raise HTTPException(status_code=502, detail=stderr or "git workspace inspection failed")
revision_text, status_separator, git_status_text = revision_and_status.rpartition(
f"\n{status_tag}"
)
if not status_separator:
raise HTTPException(status_code=502, detail=stderr or "git workspace inspection failed")
try:
fingerprint, raw_text = _revision_parts(revision_text, raw_tag, stat_tag)
except ValueError as error:
raise HTTPException(
status_code=502, detail=stderr or "git workspace inspection failed"
) from error
try:
git_status = int(git_status_text.strip().splitlines()[0])
except (ValueError, IndexError):
git_status = process_status
if git_status != 0:
raise HTTPException(status_code=502, detail=stderr or "git workspace inspection failed")
branch, worktree_changes = _status_view(status_text)
changes_by_path = {change["path"]: change for change in _raw_changes(raw_text)}
changes_by_path.update({change["path"]: change for change in worktree_changes})
changes = sorted(changes_by_path.values(), key=lambda change: change["path"])
return {
"available": True,
"revision": _revision(status_text, fingerprint),
"branch": branch,
"changes": changes,
"change_count": len(changes),
}
def run_git(
run_sandbox: RunSandbox,
backend: Any,
workdir: Path | str,
base_sha: str | None = None,
*,
timeout_s: float,
max_output_bytes: int,
exact_patch: bool = False,
env: Mapping[str, str] = GIT_VIEW_ENV,
) -> dict[str, Any]:
"""Read status, a revision, the base-relative patch, and recent commits."""
if base_sha and not COMMIT_SHA.fullmatch(base_sha):
raise HTTPException(status_code=400, detail="base SHA is not a commit hash")
target = base_sha or "HEAD"
marker = secrets.token_hex(16)
revision_tag = f"__HYI_{marker}_REVISION__"
raw_tag = f"__HYI_{marker}_RAW__"
stat_tag = f"__HYI_{marker}_STAT__"
diff_tag = f"__HYI_{marker}_DIFF__"
commits_tag = f"__HYI_{marker}_COMMITS__"
status_tag = f"__HYI_{marker}_STATUS__"
script = NEUTRALIZE_GIT_FILTERS + (
"git --no-pager status --porcelain=v1 --branch -z "
"--untracked-files=all --ignore-submodules=dirty; "
"status_status=$?; "
f"printf '\n{revision_tag}\n'; "
+ _revision_commands(target, raw_tag, stat_tag)
+ f"printf '\n{diff_tag}\n'; "
'index_path="${TMPDIR:-/tmp}/hyi-browser-index.$$"; '
'rm -f "$index_path"; trap \'rm -f "$index_path"\' EXIT; '
f'GIT_INDEX_FILE="$index_path" git read-tree {target} && '
"{ printf '.\\0'; GIT_INDEX_FILE=\"$index_path\" git ls-files -z --stage | "
"sed -z -n 's/^160000 [0-9a-f]* [0-3]\\t/:(exclude,literal)/p'; } | "
'GIT_INDEX_FILE="$index_path" git add -A -N --pathspec-from-file=- '
"--pathspec-file-nul >/dev/null 2>&1 && "
f'GIT_INDEX_FILE="$index_path" git --no-pager diff --no-ext-diff --no-textconv '
"--no-color --src-prefix=a/ --dst-prefix=b/ --unified=3 "
f"--ignore-submodules=dirty --submodule=short --binary {target}; diff_status=$?; "
f"printf '\n{commits_tag}\n'; "
"git --no-pager log -20 --pretty=format:'%H%x09%h%x09%s%x09%an%x09%aI'; "
"log_status=$?; overall_status=$status_status; "
'[ "$overall_status" -eq 0 ] && overall_status=$revision_status; '
'[ "$overall_status" -eq 0 ] && overall_status=$diff_status; '
'[ "$overall_status" -eq 0 ] && overall_status=$log_status; '
f"printf '\n{status_tag}%s\n' \"$overall_status\""
)
process_status, stdout, stderr = run_sandbox(
backend,
workdir=workdir,
script=script,
env={**env, "GIT_OPTIONAL_LOCKS": "0"},
timeout_s=timeout_s,
max_output_bytes=max_output_bytes,
)
status_text, separator, remainder = stdout.partition(f"\n{revision_tag}\n")
if not separator:
raise HTTPException(status_code=502, detail=stderr or "git workspace inspection failed")
revision_text, separator, remainder = remainder.partition(f"\n{diff_tag}\n")
if not separator:
raise HTTPException(status_code=502, detail=stderr or "git workspace inspection failed")
try:
fingerprint, _raw_text = _revision_parts(revision_text, raw_tag, stat_tag)
except ValueError as error:
raise HTTPException(
status_code=502, detail=stderr or "git workspace inspection failed"
) from error
patch, separator, commits_and_status = remainder.partition(f"\n{commits_tag}\n")
if not separator:
raise HTTPException(status_code=502, detail=stderr or "git workspace inspection failed")
commits_text, _, git_status_text = commits_and_status.rpartition(f"\n{status_tag}")
try:
git_status = int(git_status_text.strip().splitlines()[0])
except (ValueError, IndexError):
git_status = process_status
if git_status != 0:
raise HTTPException(status_code=502, detail=stderr or "git workspace inspection failed")
if exact_patch and "\ufffd" in patch:
raise HTTPException(status_code=502, detail="workspace patch is not exact UTF-8 text")
branch, changes = _status_view(status_text)
commits = []
for line in commits_text.splitlines():
parts = line.split("\t", 4)
if len(parts) == 5:
commits.append(
{
"sha": parts[0],
"short_sha": parts[1],
"subject": parts[2],
"author": parts[3],
"authored_at": parts[4],
}
)
return {
"available": True,
"revision": _revision(status_text, fingerprint),
"branch": branch,
"changes": changes,
"change_count": len(changes),
"patch": patch,
"commits": commits,
}