-
-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathgitpr.py
More file actions
77 lines (63 loc) · 1.97 KB
/
Copy pathgitpr.py
File metadata and controls
77 lines (63 loc) · 1.97 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
"""Create or update a local branch for an OG-Core upstream pull request."""
from __future__ import annotations
import subprocess
import sys
def git(
*args: str, check: bool = True, capture_output: bool = False
) -> subprocess.CompletedProcess[str]:
return subprocess.run(
("git", *args),
check=check,
text=True,
capture_output=capture_output,
)
def current_branch() -> str:
return git("branch", "--show-current", capture_output=True).stdout.strip()
def main() -> int:
if len(sys.argv) != 2 or not sys.argv[1].isdigit() or int(sys.argv[1]) < 1:
print(
"ERROR: specify one positive pull-request number: make "
"git-pr N=123",
file=sys.stderr,
)
return 1
branch = current_branch()
if branch not in {"master", "main"}:
print(
"STOP: switch to your local master or main branch first.",
file=sys.stderr,
)
return 1
number = sys.argv[1]
pr_branch = f"pr-{number}"
git("fetch", "upstream", f"refs/pull/{number}/head")
exists = (
git(
"show-ref",
"--verify",
"--quiet",
f"refs/heads/{pr_branch}",
check=False,
).returncode
== 0
)
if exists:
git("switch", pr_branch)
result = git("merge", "--ff-only", "FETCH_HEAD", check=False)
if result.returncode:
print(
"STOP: {pr_branch} cannot fast-forward to upstream "
f"PR #{number}. "
"Delete or reconcile the local branch, then try again.",
file=sys.stderr,
)
return result.returncode
else:
git("switch", "--create", pr_branch, "FETCH_HEAD")
git("status", "--short", "--branch")
return 0
if __name__ == "__main__":
try:
raise SystemExit(main())
except subprocess.CalledProcessError as error:
raise SystemExit(error.returncode)