Repository navigation
Expand file tree
/
Copy pathgitops.py
More file actions
233 lines (211 loc) · 8.47 KB
/
Copy pathgitops.py
File metadata and controls
233 lines (211 loc) · 8.47 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
"""Git operations performed by the orchestrator (never by the agent).
The agent edits the working tree inside the sandbox, so everything under it is untrusted. Therefore:
- nested repositories (any `.git` below the top level) are rejected before git looks at the tree — git would
otherwise run a nested repo's own config (e.g. filter drivers) when checking it as a submodule;
- submodules are ignored by every command; repo hooks are disabled (empty `core.hooksPath`);
- git runs with a minimal environment (no orchestrator secrets), no system/global config, and `safe.directory`
limited to the one repo;
- pushes go to the known clone URL, never `origin` (which could be repointed);
- diffs are forced to text without textconv/external drivers, so secret scanning sees every byte.
Credentials are passed only through `GIT_CONFIG_*` env for clone/ls-remote/push and are masked in errors.
"""
import os
import subprocess
import tempfile
from collections.abc import Mapping, Sequence
from pathlib import Path
from devagent.core.guardrails import Redactor
_ENV_ALLOWLIST = (
"PATH", "SYSTEMROOT", "SystemRoot", "WINDIR", "COMSPEC", "PATHEXT", "TEMP", "TMP", "TMPDIR",
"LANG", "LC_ALL", "SSL_CERT_FILE", "SSL_CERT_DIR",
"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", "http_proxy", "https_proxy", "no_proxy",
) # fmt: skip
class GitError(Exception):
pass
def nested_git_entries(repo: Path) -> list[str]:
"""Paths (relative, posix) of `.git` files/dirs below the repo root. Symlinks are not followed."""
found: list[str] = []
for root, dirs, files in os.walk(repo):
here = Path(root)
if here == repo and ".git" in dirs:
dirs.remove(".git") # the real repository
for name in [*dirs, *files]:
if name == ".git":
found.append((here / name).relative_to(repo).as_posix())
dirs[:] = [d for d in dirs if d != ".git"]
return sorted(found)
class GitOps:
def __init__(
self, *, author_name: str, author_email: str, timeout_seconds: float = 600
) -> None:
self._author = (author_name, author_email)
self._timeout = timeout_seconds
scratch = Path(tempfile.mkdtemp(prefix="devagent-git-"))
# Empty hooks dir, empty HOME and an empty global config: nothing outside our flags configures git.
self._no_hooks = scratch / "hooks"
self._no_hooks.mkdir()
self._home = scratch / "home"
self._home.mkdir()
self._global_config = scratch / "gitconfig"
self._global_config.write_text("", encoding="utf-8")
def child_env(self, extra: Mapping[str, str] | None = None) -> dict[str, str]:
env = {k: os.environ[k] for k in _ENV_ALLOWLIST if k in os.environ}
env.update(
HOME=str(self._home),
USERPROFILE=str(self._home),
GIT_CONFIG_NOSYSTEM="1",
GIT_CONFIG_GLOBAL=str(self._global_config),
GIT_TERMINAL_PROMPT="0",
)
env.update(extra or {})
return env
def _run(
self,
args: Sequence[str],
*,
cwd: Path | None = None,
env: Mapping[str, str] | None = None,
) -> str:
secrets = list((env or {}).values())
safe_dir = cwd.resolve().as_posix() if cwd is not None else ""
command = [
"git",
"-c", f"core.hooksPath={self._no_hooks.as_posix()}",
"-c", "core.fsmonitor=false",
"-c", "credential.helper=",
"-c", "protocol.ext.allow=never",
"-c", "diff.ignoreSubmodules=all",
"-c", "status.submoduleSummary=false",
"-c", "submodule.recurse=false",
"-c", "core.quotePath=false",
# The sandbox writes files as its own uid; trust exactly this repo, nothing else.
"-c", f"safe.directory={safe_dir}",
*args,
] # fmt: skip
try:
result = subprocess.run(
command,
cwd=cwd,
env=self.child_env(env),
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
timeout=self._timeout,
check=False,
)
except subprocess.TimeoutExpired:
raise GitError(f"git {args[0]} timed out after {self._timeout}s") from None
if result.returncode != 0:
detail = Redactor(secrets).redact((result.stderr or result.stdout).strip())[
-2000:
]
raise GitError(f"git {args[0]} failed (exit {result.returncode}): {detail}")
return result.stdout.strip()
def _reject_nested_repos(self, repo: Path) -> None:
nested = nested_git_entries(repo)
if nested:
raise GitError(
"nested git repository in the working tree (not allowed): "
+ ", ".join(nested[:10])
)
def clone(self, url: str, dest: Path, *, base: str, env: Mapping[str, str]) -> None:
dest.parent.mkdir(parents=True, exist_ok=True)
self._run(
["clone", "--quiet", "--no-tags", "--no-recurse-submodules", "--branch", base, "--single-branch",
url, str(dest)],
env=env,
) # fmt: skip
def create_branch(self, repo: Path, name: str) -> None:
self._run(["switch", "--quiet", "--create", name], cwd=repo)
def head_sha(self, repo: Path) -> str:
return self._run(["rev-parse", "HEAD"], cwd=repo)
def commit_all(self, repo: Path, message: str) -> bool:
"""Stage and commit everything in the working tree; returns False when there was nothing to commit."""
self._reject_nested_repos(repo)
self._run(["add", "--all"], cwd=repo)
if not self._run(
["status", "--porcelain", "--ignore-submodules=all"], cwd=repo
):
return False
name, email = self._author
self._run(
[
"-c",
f"user.name={name}",
"-c",
f"user.email={email}",
"commit",
"--quiet",
"--no-verify",
"-m",
message,
],
cwd=repo,
)
return True
def changed_files(self, repo: Path, *, base: str) -> list[str]:
# -z gives exact, unquoted paths; git's default quoting turns non-ASCII names into "\303\251"-style
# strings that would slip past protected-path globs.
out = self._run(
["diff", "-z", "--name-only", "--no-renames", f"origin/{base}...HEAD"],
cwd=repo,
)
return [path for path in out.split("\0") if path]
def changed_entries(self, repo: Path, *, base: str) -> list[tuple[str, str]]:
"""(new git mode, path) for every changed path, e.g. ("100644", "a.py"); gitlinks are 160000."""
out = self._run(
[
"diff",
"-z",
"--raw",
"--no-renames",
"--no-abbrev",
f"origin/{base}...HEAD",
],
cwd=repo,
)
parts = out.split("\0")
entries: list[tuple[str, str]] = []
for meta, path in zip(parts[0::2], parts[1::2], strict=False):
fields = meta.split()
if len(fields) >= 2 and path:
entries.append((fields[1], path))
return entries
def diff(self, repo: Path, *, base: str) -> str:
return self._run(
[
"diff",
"--no-color",
"--text",
"--no-ext-diff",
"--no-textconv",
f"origin/{base}...HEAD",
],
cwd=repo,
)
def remote_branch_sha(
self, url: str, branch: str, *, env: Mapping[str, str]
) -> str | None:
out = self._run(["ls-remote", "--heads", url, f"refs/heads/{branch}"], env=env)
return out.split()[0] if out else None
def push(
self, repo: Path, url: str, refspec: str, *, env: Mapping[str, str]
) -> None:
if refspec.startswith("+") or refspec.startswith("-"):
raise GitError(
"refusing to push: force pushes and push options are never allowed"
)
self._run(
[
"push",
"--quiet",
"--no-verify",
"--porcelain",
"--no-recurse-submodules",
url,
refspec,
],
cwd=repo,
env=env,
)