forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitutils.py
453 lines (382 loc) · 15.6 KB
/
gitutils.py
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/env python3
import os
import re
import tempfile
from collections import defaultdict
from datetime import datetime
from functools import wraps
from typing import (
Any,
Callable,
cast,
Dict,
Iterator,
List,
Optional,
Tuple,
TypeVar,
Union,
)
T = TypeVar("T")
RE_GITHUB_URL_MATCH = re.compile("^https://.*@?github.com/(.+)/(.+)$")
def get_git_remote_name() -> str:
return os.getenv("GIT_REMOTE_NAME", "origin")
def get_git_repo_dir() -> str:
from pathlib import Path
return os.getenv("GIT_REPO_DIR", str(Path(__file__).resolve().parent.parent.parent))
def fuzzy_list_to_dict(items: List[Tuple[str, str]]) -> Dict[str, List[str]]:
"""
Converts list to dict preserving elements with duplicate keys
"""
rc: Dict[str, List[str]] = defaultdict(list)
for key, val in items:
rc[key].append(val)
return dict(rc)
def _check_output(items: List[str], encoding: str = "utf-8") -> str:
from subprocess import CalledProcessError, check_output, STDOUT
try:
return check_output(items, stderr=STDOUT).decode(encoding)
except CalledProcessError as e:
msg = f"Command `{' '.join(e.cmd)}` returned non-zero exit code {e.returncode}"
stdout = e.stdout.decode(encoding) if e.stdout is not None else ""
stderr = e.stderr.decode(encoding) if e.stderr is not None else ""
# These get swallowed up, so print them here for debugging
print(f"stdout: \n{stdout}")
print(f"stderr: \n{stderr}")
if len(stderr) == 0:
msg += f"\n```\n{stdout}```"
else:
msg += f"\nstdout:\n```\n{stdout}```\nstderr:\n```\n{stderr}```"
raise RuntimeError(msg) from e
class GitCommit:
commit_hash: str
title: str
body: str
author: str
author_date: datetime
commit_date: Optional[datetime]
def __init__(
self,
commit_hash: str,
author: str,
author_date: datetime,
title: str,
body: str,
commit_date: Optional[datetime] = None,
) -> None:
self.commit_hash = commit_hash
self.author = author
self.author_date = author_date
self.commit_date = commit_date
self.title = title
self.body = body
def __repr__(self) -> str:
return f"{self.title} ({self.commit_hash})"
def __contains__(self, item: Any) -> bool:
return item in self.body or item in self.title
def parse_fuller_format(lines: Union[str, List[str]]) -> GitCommit:
"""
Expect commit message generated using `--format=fuller --date=unix` format, i.e.:
commit <sha1>
Author: <author>
AuthorDate: <author date>
Commit: <committer>
CommitDate: <committer date>
<title line>
<full commit message>
"""
if isinstance(lines, str):
lines = lines.split("\n")
# TODO: Handle merge commits correctly
if len(lines) > 1 and lines[1].startswith("Merge:"):
del lines[1]
assert len(lines) > 7
assert lines[0].startswith("commit")
assert lines[1].startswith("Author: ")
assert lines[2].startswith("AuthorDate: ")
assert lines[3].startswith("Commit: ")
assert lines[4].startswith("CommitDate: ")
assert len(lines[5]) == 0
return GitCommit(
commit_hash=lines[0].split()[1].strip(),
author=lines[1].split(":", 1)[1].strip(),
author_date=datetime.fromtimestamp(int(lines[2].split(":", 1)[1].strip())),
commit_date=datetime.fromtimestamp(int(lines[4].split(":", 1)[1].strip())),
title=lines[6].strip(),
body="\n".join(lines[7:]),
)
class GitRepo:
def __init__(self, path: str, remote: str = "origin", debug: bool = False) -> None:
self.repo_dir = path
self.remote = remote
self.debug = debug
def _run_git(self, *args: Any) -> str:
if self.debug:
print(f"+ git -C {self.repo_dir} {' '.join(args)}")
return _check_output(["git", "-C", self.repo_dir] + list(args))
def revlist(self, revision_range: str) -> List[str]:
rc = self._run_git("rev-list", revision_range, "--", ".").strip()
return rc.split("\n") if len(rc) > 0 else []
def branches_containing_ref(
self, ref: str, *, include_remote: bool = True
) -> List[str]:
rc = (
self._run_git("branch", "--remote", "--contains", ref)
if include_remote
else self._run_git("branch", "--contains", ref)
)
return [x.strip() for x in rc.split("\n") if x.strip()] if len(rc) > 0 else []
def current_branch(self) -> Optional[str]:
try:
return self._run_git("symbolic-ref", "--short", "HEAD").strip()
except RuntimeError:
# we are in detached HEAD state
return None
def checkout(self, branch: str) -> None:
self._run_git("checkout", branch)
def create_branch_and_checkout(self, branch: str) -> None:
self._run_git("checkout", "-b", branch)
def fetch(self, ref: Optional[str] = None, branch: Optional[str] = None) -> None:
if branch is None and ref is None:
self._run_git("fetch", self.remote)
elif branch is None:
self._run_git("fetch", self.remote, ref)
else:
self._run_git("fetch", self.remote, f"{ref}:{branch}")
def show_ref(self, name: str) -> str:
refs = self._run_git("show-ref", "-s", name).strip().split("\n")
if not all(refs[i] == refs[0] for i in range(1, len(refs))):
raise RuntimeError(f"reference {name} is ambiguous")
return refs[0]
def rev_parse(self, name: str) -> str:
return self._run_git("rev-parse", "--verify", name).strip()
def get_merge_base(self, from_ref: str, to_ref: str) -> str:
return self._run_git("merge-base", from_ref, to_ref).strip()
def patch_id(self, ref: Union[str, List[str]]) -> List[Tuple[str, str]]:
is_list = isinstance(ref, list)
if is_list:
if len(ref) == 0:
return []
ref = " ".join(ref)
rc = _check_output(
["sh", "-c", f"git -C {self.repo_dir} show {ref}|git patch-id --stable"]
).strip()
return [cast(Tuple[str, str], x.split(" ", 1)) for x in rc.split("\n")]
def commits_resolving_gh_pr(self, pr_num: int) -> List[str]:
owner, name = self.gh_owner_and_name()
msg = f"Pull Request resolved: https://github.com/{owner}/{name}/pull/{pr_num}"
rc = self._run_git("log", "--format=%H", "--grep", msg).strip()
return rc.split("\n") if len(rc) > 0 else []
def get_commit(self, ref: str) -> GitCommit:
return parse_fuller_format(
self._run_git("show", "--format=fuller", "--date=unix", "--shortstat", ref)
)
def cherry_pick(self, ref: str) -> None:
self._run_git("cherry-pick", "-x", ref)
def revert(self, ref: str) -> None:
self._run_git("revert", "--no-edit", ref)
def compute_branch_diffs(
self, from_branch: str, to_branch: str
) -> Tuple[List[str], List[str]]:
"""
Returns list of commmits that are missing in each other branch since their merge base
Might be slow if merge base is between two branches is pretty far off
"""
from_ref = self.rev_parse(from_branch)
to_ref = self.rev_parse(to_branch)
merge_base = self.get_merge_base(from_ref, to_ref)
from_commits = self.revlist(f"{merge_base}..{from_ref}")
to_commits = self.revlist(f"{merge_base}..{to_ref}")
from_ids = fuzzy_list_to_dict(self.patch_id(from_commits))
to_ids = fuzzy_list_to_dict(self.patch_id(to_commits))
for patch_id in set(from_ids).intersection(set(to_ids)):
from_values = from_ids[patch_id]
to_values = to_ids[patch_id]
if len(from_values) != len(to_values):
# Eliminate duplicate commits+reverts from the list
while len(from_values) > 0 and len(to_values) > 0:
frc = self.get_commit(from_values.pop())
toc = self.get_commit(to_values.pop())
# FRC branch might have PR number added to the title
if frc.title != toc.title or frc.author_date != toc.author_date:
# HACK: Same commit were merged, reverted and landed again
# which creates a tracking problem
if (
"pytorch/pytorch" not in self.remote_url()
or frc.commit_hash
not in {
"0a6a1b27a464ba5be5f587cce2ee12ab8c504dbf",
"6d0f4a1d545a8f161df459e8d4ccafd4b9017dbe",
"edf909e58f06150f7be41da2f98a3b9de3167bca",
"a58c6aea5a0c9f8759a4154e46f544c8b03b8db1",
"7106d216c29ca16a3504aa2bedad948ebcf4abc2",
}
):
raise RuntimeError(
f"Unexpected differences between {frc} and {toc}"
)
from_commits.remove(frc.commit_hash)
to_commits.remove(toc.commit_hash)
continue
for commit in from_values:
from_commits.remove(commit)
for commit in to_values:
to_commits.remove(commit)
# Another HACK: Patch-id is not stable for commits with binary files or for big changes across commits
# I.e. cherry-picking those from one branch into another will change patchid
if "pytorch/pytorch" in self.remote_url():
for excluded_commit in {
"8e09e20c1dafcdbdb45c2d1574da68a32e54a3a5",
"5f37e5c2a39c3acb776756a17730b865f0953432",
"b5222584e6d6990c6585981a936defd1af14c0ba",
"84d9a2e42d5ed30ec3b8b4140c38dd83abbce88d",
"f211ec90a6cdc8a2a5795478b5b5c8d7d7896f7e",
}:
if excluded_commit in from_commits:
from_commits.remove(excluded_commit)
return (from_commits, to_commits)
def cherry_pick_commits(self, from_branch: str, to_branch: str) -> None:
orig_branch = self.current_branch()
assert orig_branch is not None, "Must be on a branch"
self.checkout(to_branch)
from_commits, to_commits = self.compute_branch_diffs(from_branch, to_branch)
if len(from_commits) == 0:
print("Nothing to do")
self.checkout(orig_branch)
return
for commit in reversed(from_commits):
print(f"Cherry picking commit {commit}")
self.cherry_pick(commit)
self.checkout(orig_branch)
def push(self, branch: str, dry_run: bool, retry: int = 3) -> None:
for cnt in range(retry):
try:
if dry_run:
self._run_git("push", "--dry-run", self.remote, branch)
else:
self._run_git("push", self.remote, branch)
except RuntimeError as e:
print(f"{cnt} push attempt failed with {e}")
self.fetch()
self._run_git("rebase", f"{self.remote}/{branch}")
def head_hash(self) -> str:
return self._run_git("show-ref", "--hash", "HEAD").strip()
def remote_url(self) -> str:
return self._run_git("remote", "get-url", self.remote)
def gh_owner_and_name(self) -> Tuple[str, str]:
url = os.getenv("GIT_REMOTE_URL", None)
if url is None:
url = self.remote_url()
rc = RE_GITHUB_URL_MATCH.match(url)
if rc is None:
raise RuntimeError(f"Unexpected url format {url}")
return cast(Tuple[str, str], rc.groups())
def commit_message(self, ref: str) -> str:
return self._run_git("log", "-1", "--format=%B", ref)
def amend_commit_message(self, msg: str) -> None:
self._run_git("commit", "--amend", "-m", msg)
def diff(self, from_ref: str, to_ref: Optional[str] = None) -> str:
if to_ref is None:
return self._run_git("diff", f"{from_ref}^!")
return self._run_git("diff", f"{from_ref}..{to_ref}")
def clone_repo(username: str, password: str, org: str, project: str) -> GitRepo:
path = tempfile.mkdtemp()
_check_output(
[
"git",
"clone",
f"https://{username}:{password}@github.com/{org}/{project}",
path,
]
).strip()
return GitRepo(path=path)
class PeekableIterator(Iterator[str]):
def __init__(self, val: str) -> None:
self._val = val
self._idx = -1
def peek(self) -> Optional[str]:
if self._idx + 1 >= len(self._val):
return None
return self._val[self._idx + 1]
def __iter__(self) -> "PeekableIterator":
return self
def __next__(self) -> str:
rc = self.peek()
if rc is None:
raise StopIteration
self._idx += 1
return rc
def patterns_to_regex(allowed_patterns: List[str]) -> Any:
"""
pattern is glob-like, i.e. the only special sequences it has are:
- ? - matches single character
- * - matches any non-folder separator characters or no character
- ** - matches any characters or no character
Assuming that patterns are free of braces and backslashes
the only character that needs to be escaped are dot and plus
"""
rc = "("
for idx, pattern in enumerate(allowed_patterns):
if idx > 0:
rc += "|"
pattern_ = PeekableIterator(pattern)
assert not any(c in pattern for c in "{}()[]\\")
for c in pattern_:
if c == ".":
rc += "\\."
elif c == "+":
rc += "\\+"
elif c == "*":
if pattern_.peek() == "*":
next(pattern_)
rc += ".*"
else:
rc += "[^/]*"
else:
rc += c
rc += ")"
return re.compile(rc)
def _shasum(value: str) -> str:
import hashlib
m = hashlib.sha256()
m.update(value.encode("utf-8"))
return m.hexdigest()
def is_commit_hash(ref: str) -> bool:
"True if ref is hexadecimal number, else false"
try:
int(ref, 16)
except ValueError:
return False
return True
def are_ghstack_branches_in_sync(
repo: GitRepo, head_ref: str, base_ref: Optional[str] = None
) -> bool:
"""Checks that diff between base and head is the same as diff between orig and its parent"""
orig_ref = re.sub(r"/head$", "/orig", head_ref)
if base_ref is None:
base_ref = re.sub(r"/head$", "/base", head_ref)
orig_diff_sha = _shasum(repo.diff(f"{repo.remote}/{orig_ref}"))
head_diff_sha = _shasum(
repo.diff(
base_ref if is_commit_hash(base_ref) else f"{repo.remote}/{base_ref}",
f"{repo.remote}/{head_ref}",
)
)
return orig_diff_sha == head_diff_sha
def retries_decorator(
rc: Any = None, num_retries: int = 3
) -> Callable[[Callable[..., T]], Callable[..., T]]:
def decorator(f: Callable[..., T]) -> Callable[..., T]:
@wraps(f)
def wrapper(*args: List[Any], **kwargs: Dict[str, Any]) -> T:
for idx in range(num_retries):
try:
return f(*args, **kwargs)
except Exception as e:
print(
f'Attempt {idx} of {num_retries} to call {f.__name__} failed with "{e}"'
)
pass
return cast(T, rc)
return wrapper
return decorator