Skip to content

Commit 3998103

Browse files
committed
Commit to existing PR if the pin file is not changed by others
1 parent f21d72d commit 3998103

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

.github/scripts/update_commit_hashes.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def main() -> None:
112112

113113
branch_name = os.environ["NEW_BRANCH_NAME"]
114114
pr_num = None
115+
merge_base_hash = None
115116

116117
source_repo = args.source_repo
117118
# query to see if a pr already exists
@@ -126,6 +127,7 @@ def main() -> None:
126127
link = response["items"][0]["html_url"]
127128
response = git_api(f"/repos/{source_repo}/pulls/{pr_num}", {})
128129
branch_name = response["head"]["ref"]
130+
merge_base_hash = response["base"]["sha"]
129131
print(
130132
f"pr does exist, number is {pr_num}, branch name is {branch_name}, link is {link}"
131133
)
@@ -139,7 +141,8 @@ def main() -> None:
139141
.stdout.decode("utf-8")
140142
.strip()
141143
)
142-
with open(f"{args.pin_folder}/{args.repo_name}.txt", "r+") as f:
144+
pin_file = f"{args.pin_folder}/{args.repo_name}.txt"
145+
with open(pin_file, "r+") as f:
143146
old_hash = f.read().strip()
144147
subprocess.run(f"git checkout {old_hash}".split(), cwd=args.repo_name)
145148
f.seek(0)
@@ -156,32 +159,59 @@ def main() -> None:
156159
["git", "submodule", "foreach", "--quiet", "echo $name"],
157160
capture_output=True,
158161
)
162+
submodule = ''
159163
for submodule in submodules.stdout.decode().strip().splitlines():
160164
if f"/{args.repo_name}" in submodule:
161165
has_submodule = True
162166
break
163167

168+
use_existing_branch = False
169+
if pr_num is not None:
170+
# if there is a pr, and the pin file of the merge base is the same as the
171+
# current HEAD, then we can push to the existing branch and do not need to
172+
# use a force push
173+
assert merge_base_hash is not None
174+
if (
175+
subprocess.run(
176+
["git", "diff", "--check", merge_base_hash, "HEAD", "--", pin_file],
177+
).returncode
178+
== 0
179+
):
180+
print(
181+
f"the pin file {pin_file} is the same as the merge base {merge_base_hash}"
182+
)
183+
print("commit to the existing pr")
184+
use_existing_branch = True
185+
186+
if use_existing_branch:
187+
subprocess.run(f"git checkout {branch_name}".split())
188+
else:
189+
subprocess.run(f"git checkout -b {branch_name}".split())
190+
164191
if submodule and os.path.exists(submodule):
165192
has_submodule = True
166193
subprocess.run(["git", "fetch", "origin"], cwd=submodule)
167194
subprocess.run(
168195
f"git checkout {hash}".split(), cwd=submodule, capture_output=True
169196
)
170197

171-
# if there was an update, push to branch
172-
subprocess.run(f"git checkout -b {branch_name}".split())
173-
subprocess.run(f"git add {args.pin_folder}/{args.repo_name}.txt".split())
198+
subprocess.run(f"git add {pin_file}".split())
174199
if has_submodule:
175200
subprocess.run(f"git add {submodule}".split())
176201
subprocess.run(
177202
["git", "commit", "-m"] + [f"update {args.repo_name} commit hash"]
178203
)
179-
subprocess.run(f"git push --set-upstream origin {branch_name} -f".split())
204+
205+
# if there was an update, push to branch
206+
subprocess.run(f"git push --force --set-upstream origin {branch_name}".split())
180207
print(f"changes pushed to branch {branch_name}")
208+
181209
if pr_num is None:
182210
# no existing pr, so make a new one and approve it
183211
pr_num = make_pr(source_repo, args.repo_name, branch_name)
184212
approve_pr(source_repo, pr_num)
213+
214+
if not use_existing_branch:
185215
make_comment(source_repo, pr_num, "@pytorchbot merge")
186216
else:
187217
# rebase the existing pr onto viable/strict

0 commit comments

Comments
 (0)