@@ -112,6 +112,7 @@ def main() -> None:
112
112
113
113
branch_name = os .environ ["NEW_BRANCH_NAME" ]
114
114
pr_num = None
115
+ merge_base_hash = None
115
116
116
117
source_repo = args .source_repo
117
118
# query to see if a pr already exists
@@ -126,6 +127,7 @@ def main() -> None:
126
127
link = response ["items" ][0 ]["html_url" ]
127
128
response = git_api (f"/repos/{ source_repo } /pulls/{ pr_num } " , {})
128
129
branch_name = response ["head" ]["ref" ]
130
+ merge_base_hash = response ["base" ]["sha" ]
129
131
print (
130
132
f"pr does exist, number is { pr_num } , branch name is { branch_name } , link is { link } "
131
133
)
@@ -139,7 +141,8 @@ def main() -> None:
139
141
.stdout .decode ("utf-8" )
140
142
.strip ()
141
143
)
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 :
143
146
old_hash = f .read ().strip ()
144
147
subprocess .run (f"git checkout { old_hash } " .split (), cwd = args .repo_name )
145
148
f .seek (0 )
@@ -156,32 +159,59 @@ def main() -> None:
156
159
["git" , "submodule" , "foreach" , "--quiet" , "echo $name" ],
157
160
capture_output = True ,
158
161
)
162
+ submodule = ''
159
163
for submodule in submodules .stdout .decode ().strip ().splitlines ():
160
164
if f"/{ args .repo_name } " in submodule :
161
165
has_submodule = True
162
166
break
163
167
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
+
164
191
if submodule and os .path .exists (submodule ):
165
192
has_submodule = True
166
193
subprocess .run (["git" , "fetch" , "origin" ], cwd = submodule )
167
194
subprocess .run (
168
195
f"git checkout { hash } " .split (), cwd = submodule , capture_output = True
169
196
)
170
197
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 ())
174
199
if has_submodule :
175
200
subprocess .run (f"git add { submodule } " .split ())
176
201
subprocess .run (
177
202
["git" , "commit" , "-m" ] + [f"update { args .repo_name } commit hash" ]
178
203
)
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 ())
180
207
print (f"changes pushed to branch { branch_name } " )
208
+
181
209
if pr_num is None :
182
210
# no existing pr, so make a new one and approve it
183
211
pr_num = make_pr (source_repo , args .repo_name , branch_name )
184
212
approve_pr (source_repo , pr_num )
213
+
214
+ if not use_existing_branch :
185
215
make_comment (source_repo , pr_num , "@pytorchbot merge" )
186
216
else :
187
217
# rebase the existing pr onto viable/strict
0 commit comments