-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathgit.py
More file actions
342 lines (311 loc) · 11 KB
/
Copy pathgit.py
File metadata and controls
342 lines (311 loc) · 11 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import asyncio
import itertools
import logging
import os.path
import pathlib
import random
import subprocess
import time
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from dataclasses import dataclass
from typing import IO, Any
from django.conf import settings
from pydantic import AnyUrl
logger = logging.getLogger(__name__)
def get_head_sha1(url: AnyUrl, branch: str) -> str:
result = subprocess.run(
["git", "ls-remote", str(url), f"refs/heads/{branch}"],
capture_output=True,
text=True,
check=True,
timeout=settings.NETWORK_REQUEST_TIMEOUT,
)
line = result.stdout.strip()
if not line:
raise ValueError(f"branch {branch!r} not found at {url!r}")
return line.split()[0]
@dataclass
class Worktree:
path: pathlib.Path
revision: str
detached: bool
prunable: bool
@classmethod
def parse_from_porcelain(
cls: type["Worktree"], porcelain: list[str]
) -> "Worktree | None":
path = porcelain[0].split(" ")[1]
if porcelain[1] == "bare":
return None
revision = porcelain[1].split(" ")[1]
detached = porcelain[2] == "detached"
if len(porcelain) >= 4:
prunable = porcelain[3].startswith("prunable")
else:
prunable = False
return Worktree(
path=pathlib.Path(path),
revision=revision,
detached=detached,
prunable=prunable,
)
def name(self) -> str:
"""
By default, `git` uses the basename
of the target path to name a worktree.
"""
return os.path.basename(str(self.path))
class RepositoryError(Exception):
pass
class GitRepo:
def __init__(
self,
repo_path: str,
stdout: int | IO[Any] | None = None,
stderr: int | IO[Any] | None = None,
) -> None:
self.stdout = stdout
self.stderr = stderr
self.repo_path = repo_path
async def execute_git_command(
self,
*cmd: str,
stdout: int | IO[Any] | None = None,
stderr: int | IO[Any] | None = None,
) -> asyncio.subprocess.Process:
final_stdout = stdout or self.stdout or asyncio.subprocess.PIPE
final_stderr = stderr or self.stderr or asyncio.subprocess.PIPE
return await asyncio.create_subprocess_exec(
*cmd, cwd=self.repo_path, stdout=final_stdout, stderr=final_stderr
)
async def clone(
self, reference_repo_path: str | None = None
) -> asyncio.subprocess.Process:
"""
Clones the repository.
If you pass a `reference_repo_path`, the cloning will be much faster.
Otherwise, it will use a shallow clone, since we can fetch commits manually.
"""
repo_clone_url = settings.GIT_CLONE_URL
stdout = self.stdout or asyncio.subprocess.PIPE
stderr = self.stderr or asyncio.subprocess.PIPE
if reference_repo_path is not None:
clone_process = await asyncio.create_subprocess_exec(
*[
"git",
"clone",
"--depth=1",
"--bare",
"--progress",
f"--reference={reference_repo_path}",
"--",
repo_clone_url,
self.repo_path,
],
stdout=stdout,
stderr=stderr,
)
else:
clone_process = await asyncio.create_subprocess_exec(
*[
"git",
"clone",
"--depth=1",
"--bare",
"--progress",
"--",
repo_clone_url,
self.repo_path,
],
stdout=stdout,
stderr=stderr,
)
return clone_process
async def worktrees(self) -> list[Worktree]:
"""
Returns a list of relevant worktrees,
e.g. filter out the `bare` worktree.
"""
process = await self.execute_git_command(
*[
"git",
"worktree",
"list",
"-z",
"--porcelain",
],
stdout=asyncio.subprocess.PIPE,
)
stdout, _ = await process.communicate()
parts = stdout.split(b"\x00")
assert parts[-1] == b"", "Worktrees list are not terminated by a NUL character"
parts = [part.decode("utf8") for part in parts]
return list(
filter(
None,
[
Worktree.parse_from_porcelain(list(group))
for k, group in itertools.groupby(parts, lambda x: x == "")
if not k
],
)
)
async def update_from_ref(
self,
object_sha1: str,
# Age above which a leftover `shallow.lock` is considered stale and removed.
max_lock_age_seconds: int = 300,
# Retries between attempts happen with exponential backoff. Increase with care.
max_attempts: int = 7,
) -> bool:
"""
This checks if `object_sha1` is already present in the repo or not.
If not, perform a fetch to our remote to obtain it.
Returns whether this was fetched or already present.
"""
repo_clone_url = settings.GIT_CLONE_URL
exists = (
await (
await self.execute_git_command(
*[
"git",
"cat-file",
"commit",
object_sha1,
],
stderr=asyncio.subprocess.PIPE,
)
).wait()
== 0
)
if exists:
return False
else:
# Git tries to create `shallow.lock` before attempting a shallow fetch, in order to prevent conflicts on the `shallow` metadata file:
# https://git-scm.com/docs/shallow
lock_path = os.path.join(self.repo_path, "shallow.lock")
for attempt in range(1, max_attempts + 1):
try:
if time.time() - os.path.getmtime(lock_path) > max_lock_age_seconds:
logger.warning("Stale `shallow.lock`, removing: %s", lock_path)
os.remove(lock_path)
except FileNotFoundError:
# The lock is gone, nothing to do.
pass
process = await self.execute_git_command(
*[
"git",
"fetch",
"--porcelain",
"--depth=1",
repo_clone_url,
object_sha1,
],
stderr=asyncio.subprocess.PIPE,
)
_, stderr = await process.communicate()
rc = await process.wait()
stderr_text = stderr.decode("utf8")
if rc == 0:
return True
for err in [
"cannot lock ref",
"shallow file has changed since we read it",
"the lock file may be stale",
]:
if err in stderr_text:
break
else:
logger.error("git fetch stderr: %s", stderr_text)
raise RepositoryError(
f"failed to fetch {object_sha1} while running `git fetch --depth=1 {repo_clone_url} {object_sha1}`"
)
if attempt < max_attempts:
# Exponential backoff with full jitter, per
# https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
backoff = random.uniform(0, 2 ** (attempt - 1))
logger.warning(
"Found `shallow.lock` when trying to fetch %s (attempt %d/%d), retrying in %.1fs",
object_sha1[:8],
attempt,
max_attempts,
backoff,
)
await asyncio.sleep(backoff)
raise RepositoryError(
f"Failed to fetch {object_sha1}: exceeded {max_attempts} attempts due to repeated `shallow.lock` contention"
)
async def remove_working_tree(self, name: str) -> bool:
"""
This deletes the working tree, if it exists.
Returns `True` if it does exist, otherwise `False`.
"""
process = await self.execute_git_command(
*[
"git",
"worktree",
"remove",
name,
]
)
deleted = await process.wait() == 0
return deleted
async def prune_working_trees(self) -> bool:
"""
This prunes all the working trees, if possible.
Returns `True` if it does get pruned, otherwise `False`.
"""
process = await self.execute_git_command(
*[
"git",
"worktree",
"prune",
]
)
pruned = await process.wait() == 0
return pruned
@asynccontextmanager
async def extract_working_tree(
self, commit_sha1: str, target_path: str
) -> AsyncGenerator[Worktree]:
"""
This will extract the working tree represented at the reference
induced by the object's commit SHA1 into the `target_path`.
This returns it as an asynchronous context manager.
"""
path = pathlib.Path(target_path)
worktrees = {wt.path: wt for wt in await self.worktrees()}
existing_wt = worktrees.get(path)
if existing_wt is not None and not existing_wt.prunable:
raise RepositoryError(
f"failed to perform extraction of the worktree at {target_path} for commit {commit_sha1}, such a worktree already exist!"
)
if existing_wt is not None and existing_wt.prunable:
await self.prune_working_trees()
process = await self.execute_git_command(
*[
"git",
"worktree",
"add",
target_path,
commit_sha1,
]
)
created = await process.wait() == 0
if not created:
raise RepositoryError(
f"failed to perform extraction of the worktree at {target_path} for commit {commit_sha1}, cannot create it!"
)
wt = None
try:
wt = Worktree(
path=pathlib.Path(target_path),
revision=commit_sha1,
detached=True,
prunable=False,
)
yield wt
finally:
if wt is not None:
await self.remove_working_tree(wt.name())