Skip to content

Commit 73c2117

Browse files
committed
Add script to assist in moving PR's
1 parent ea0f3a1 commit 73c2117

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

scripts/move-pr-to-intel-llvm.py

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright (C) 2024 Intel Corporation
4+
#
5+
# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
6+
# See LICENSE.TXT
7+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
9+
"""
10+
Use this script to move an existing pull request in the
11+
https://github.com/oneapi-src/unified-runtime project to a new pull request
12+
targeting the top-level unified-runtime directory of the
13+
https://github.com/intel/llvm project.
14+
15+
Git operations are performed on both repositories.
16+
17+
GitHub interactions are performed using the gh command-line tool which must
18+
reside on the PATH and be authenticated.
19+
"""
20+
21+
import argparse
22+
from os import remove
23+
import shutil
24+
import subprocess
25+
from subprocess import PIPE
26+
27+
verbose = False
28+
29+
30+
def run(command, **kwargs):
31+
if verbose:
32+
print(command)
33+
result = subprocess.run(command, **kwargs)
34+
result.check_returncode()
35+
return result
36+
37+
38+
def main():
39+
global verbose
40+
41+
cli = argparse.ArgumentParser(
42+
description=__doc__, formatter_class=argparse.RawTextHelpFormatter
43+
)
44+
cli.add_argument(
45+
"-v",
46+
"--verbose",
47+
action="store_true",
48+
help="enable printing verbose output",
49+
)
50+
cli.add_argument(
51+
"--ur-dir",
52+
default=".",
53+
help="local directory containing a clone of oneapi-src/unified-runtime",
54+
)
55+
cli.add_argument(
56+
"--ur-remote",
57+
default="origin",
58+
help="unified-runtime remote to fetch from, defaults to 'origin'",
59+
)
60+
cli.add_argument(
61+
"--ur-branch",
62+
default=None,
63+
help="unified-runtime branch to move, defaults to current branch",
64+
)
65+
cli.add_argument(
66+
"intel_llvm_dir",
67+
help="local directory containing a clone of intel/llvm",
68+
)
69+
cli.add_argument(
70+
"--intel-llvm-remote",
71+
default="origin",
72+
help="intel/llvm remote to fetch from, defaults to 'origin'",
73+
)
74+
cli.add_argument(
75+
"--intel-llvm-base-branch",
76+
default=None,
77+
help="intel/llvm branch to base new branch upon, defaults to 'sycl'",
78+
)
79+
cli.add_argument(
80+
"--intel-llvm-branch",
81+
default=None,
82+
help="intel/llvm remote branch to create, defaults to --ur-branch",
83+
)
84+
85+
args = cli.parse_args()
86+
verbose = args.verbose
87+
88+
git = shutil.which("git")
89+
if not git:
90+
raise FileNotFoundError("git command not found")
91+
if not args.ur_branch:
92+
ur_branch = run(
93+
[git, "-C", args.ur_dir, "rev-parse", "--abbrev-ref", "HEAD"],
94+
stdout=PIPE,
95+
)
96+
args.ur_branch = ur_branch.stdout.decode().strip()
97+
if not args.intel_llvm_branch:
98+
args.intel_llvm_branch = args.ur_branch
99+
100+
gh = shutil.which("gh")
101+
if not gh:
102+
raise FileNotFoundError("gh command not found")
103+
gh_auth_status = run([gh, "auth", "status"], stdout=PIPE)
104+
if "Logged in" not in gh_auth_status.stdout.decode():
105+
raise ValueError("gh is not authenticated, run gh auth login")
106+
107+
# TODO: check if ur local branch and remote tracking branch are in sync
108+
# TODO: check how far behind origin/main the feature branch is, error out if a rebase is required
109+
110+
run([git, "-C", args.ur_dir, "fetch", args.ur_remote])
111+
112+
# Use git format-patch to create a list of patches from the feature branch
113+
patches = run(
114+
[
115+
git,
116+
"-C",
117+
args.ur_dir,
118+
"format-patch",
119+
"--output-directory",
120+
args.intel_llvm_dir,
121+
f"{args.ur_remote}/main..{args.ur_branch}",
122+
],
123+
stdout=PIPE,
124+
)
125+
patches = patches.stdout.decode().strip()
126+
print(patches)
127+
patches = patches.split("\n")
128+
129+
# TODO: Sycl with upstream
130+
# run([git, "-C", args.intel_llvm_dir, "fetch", args.intel_llvm_remote])
131+
132+
# TODO: check if feature branch already exists
133+
134+
# create feature branch on intel/llvm
135+
run(
136+
[
137+
git,
138+
"-C",
139+
args.intel_llvm_dir,
140+
"branch",
141+
args.intel_llvm_branch,
142+
f"{args.intel_llvm_remote}/{args.intel_llvm_base_branch}",
143+
]
144+
)
145+
run([git, "-C", args.intel_llvm_dir, "checkout", args.intel_llvm_branch])
146+
147+
# Use git am to apply list of patches to intel/llvm
148+
run(
149+
[git, "-C", args.intel_llvm_dir, "am", "--directory=unified-runtime"]
150+
+ patches
151+
)
152+
153+
# clean up patch files
154+
for patch in patches:
155+
remove(patch)
156+
157+
# TODO: create pull request with feature branch
158+
159+
if __name__ == "__main__":
160+
try:
161+
main()
162+
except KeyboardInterrupt:
163+
exit(130)

0 commit comments

Comments
 (0)