@@ -224,21 +224,46 @@ info "Will cherry-pick $COMMIT_COUNT commit(s)"
224224
225225# --- Handle existing backport branch -----------------------------------------
226226BACKPORT_BRANCH=" $USER /backport-pr-$PR_NUMBER "
227- if git show-ref --verify --quiet " refs/remotes/origin/$BACKPORT_BRANCH " 2> /dev/null; then
228- warn " Remote branch $BACKPORT_BRANCH already exists."
229- echo -n " Delete it and start fresh? (y/n) "
227+ SKIP_CHERRY_PICK=0
228+
229+ EXISTING_REMOTE=0
230+ EXISTING_LOCAL=0
231+ EXISTING_PR=0
232+ git show-ref --verify --quiet " refs/remotes/origin/$BACKPORT_BRANCH " 2> /dev/null && EXISTING_REMOTE=1
233+ git show-ref --verify --quiet " refs/heads/$BACKPORT_BRANCH " 2> /dev/null && EXISTING_LOCAL=1
234+ if [ $EXISTING_REMOTE -eq 1 ]; then
235+ gh pr view " $BACKPORT_BRANCH " --json url 1> /dev/null 2>&1 && EXISTING_PR=1
236+ fi
237+
238+ if [ $EXISTING_REMOTE -eq 1 ] && [ $EXISTING_PR -eq 1 ]; then
239+ EXISTING_PR_URL=$( gh pr view " $BACKPORT_BRANCH " --json url --jq ' .url' )
240+ error " A backport PR already exists: $EXISTING_PR_URL "
241+ exit 1
242+ fi
243+
244+ if [ $EXISTING_REMOTE -eq 1 ] && [ $EXISTING_PR -eq 0 ]; then
245+ warn " Remote branch $BACKPORT_BRANCH exists but has no open PR."
246+ echo -n " Create the PR from the existing branch? (y/n) "
230247 read -r ANSWER
231248 if [ " $ANSWER " == " y" ]; then
232- if [ $DRY_RUN -eq 0 ]; then
233- git push origin --delete " $BACKPORT_BRANCH " 2> /dev/null || true
234- fi
235- info " Deleted remote branch $BACKPORT_BRANCH "
249+ SKIP_CHERRY_PICK=1
250+ info " Will reuse existing branch"
236251 else
237- echo " Aborting."
238- exit 1
252+ echo -n " Delete it and start fresh instead? (y/n) "
253+ read -r ANSWER
254+ if [ " $ANSWER " == " y" ]; then
255+ if [ $DRY_RUN -eq 0 ]; then
256+ git push origin --delete " $BACKPORT_BRANCH " 2> /dev/null || true
257+ fi
258+ info " Deleted remote branch $BACKPORT_BRANCH "
259+ else
260+ echo " Aborting."
261+ exit 1
262+ fi
239263 fi
240264fi
241- if git show-ref --verify --quiet " refs/heads/$BACKPORT_BRANCH " 2> /dev/null; then
265+
266+ if [ $SKIP_CHERRY_PICK -eq 0 ] && [ $EXISTING_LOCAL -eq 1 ]; then
242267 warn " Local branch $BACKPORT_BRANCH already exists."
243268 echo -n " Delete it and start fresh? (y/n) "
244269 read -r ANSWER
@@ -267,41 +292,51 @@ if [ $DRY_RUN -eq 1 ]; then
267292 done
268293 echo -e " Labels: ${PR_LABELS:- <none>} "
269294 echo -e " PR title: 🍒 $PR_NUMBER - $PR_TITLE "
295+ if [ $SKIP_CHERRY_PICK -eq 1 ]; then
296+ echo -e " Mode: ${YELLOW} Resume${RESET} (reuse existing remote branch)"
297+ fi
270298 echo " "
271299 info " Dry run complete. Re-run without --dry-run to execute."
272300 exit 0
273301fi
274302
275303# --- Backport ----------------------------------------------------------------
276- step " Creating backport"
304+ if [ $SKIP_CHERRY_PICK -eq 1 ]; then
305+ step " Skipping cherry-pick (reusing existing branch)"
306+ else
307+ step " Creating backport"
277308
278- git checkout " $RELEASE_BRANCH "
279- git pull --quiet
280- git checkout -b " $BACKPORT_BRANCH "
309+ git checkout " $RELEASE_BRANCH "
310+ git pull --quiet
311+ git checkout -b " $BACKPORT_BRANCH "
281312
282- CHERRY_PICK_IN_PROGRESS=1
283- for PR_COMMIT in $PR_COMMITS ; do
284- git cherry-pick -x " $PR_COMMIT "
285- done
286- CHERRY_PICK_IN_PROGRESS=0
313+ CHERRY_PICK_IN_PROGRESS=1
314+ for PR_COMMIT in $PR_COMMITS ; do
315+ git cherry-pick -x " $PR_COMMIT "
316+ done
317+ CHERRY_PICK_IN_PROGRESS=0
287318
288- git push -u origin " $BACKPORT_BRANCH "
289- info " Pushed $BACKPORT_BRANCH "
319+ git push -u origin " $BACKPORT_BRANCH "
320+ info " Pushed $BACKPORT_BRANCH "
321+ fi
290322
291323# --- Create PR ---------------------------------------------------------------
292324step " Creating pull request"
293325
294- LABEL_ARGS=" "
326+ LABEL_ARGS=()
295327if [ -n " $PR_LABELS " ]; then
296- LABEL_ARGS=" --label $PR_LABELS "
328+ LABEL_ARGS=( --label " $PR_LABELS " )
297329fi
298330
299- # shellcheck disable=SC2086
300331BACKPORT_PR_URL=$( gh pr create --base " $RELEASE_BRANCH " \
301332 --head " $BACKPORT_BRANCH " \
302333 --title " 🍒 $PR_NUMBER - $PR_TITLE " \
303334 --body " Backport of #$PR_NUMBER to \` $RELEASE_BRANCH \` " \
304- $LABEL_ARGS 2>&1 )
335+ " ${LABEL_ARGS[@]+" ${LABEL_ARGS[@]} " } " )
336+ if [ -z " $BACKPORT_PR_URL " ]; then
337+ error " gh pr create did not return a URL"
338+ exit 1
339+ fi
305340info " Created: $BACKPORT_PR_URL "
306341
307342# Comment on the original PR for traceability
@@ -313,4 +348,5 @@ git checkout "$CURRENT_BRANCH"
313348info " Back on $CURRENT_BRANCH "
314349
315350echo " "
316- echo -e " ${GREEN}${BOLD} Done!${RESET} Backport PR: $BACKPORT_PR_URL "
351+ echo -e " ${GREEN}${BOLD} Done!${RESET} "
352+ echo -e " ${BOLD} Backport PR:${RESET} $BACKPORT_PR_URL "
0 commit comments