4444
4545# Get the merge commit SHA for the PR
4646MERGE_COMMIT=$( gh pr view " ${PR_NUMBER} " --json mergeCommit --jq ' .mergeCommit.oid' )
47- if [[ -z " ${MERGE_COMMIT} " ]]; then
47+ if [[ -z " ${MERGE_COMMIT} " || " ${MERGE_COMMIT} " == " null " ]]; then
4848 echo " Error: PR #${PR_NUMBER} has no merge commit. Is it merged?"
4949 exit 1
5050fi
@@ -61,10 +61,26 @@ echo ""
6161git fetch origin " ${RELEASE_BRANCH} "
6262
6363# Create cherry-pick branch from the release branch
64+ if git show-ref --verify --quiet " refs/heads/${CHERRY_PICK_BRANCH} " ; then
65+ echo " Error: local branch '${CHERRY_PICK_BRANCH} ' already exists."
66+ echo " If it is left over from a previous attempt, delete it with:"
67+ echo " git branch -D ${CHERRY_PICK_BRANCH} "
68+ exit 1
69+ fi
6470git switch -c " ${CHERRY_PICK_BRANCH} " " origin/${RELEASE_BRANCH} "
6571
66- # Cherry-pick the merge commit using the first parent
67- if ! git cherry-pick -x -m1 " ${MERGE_COMMIT} " ; then
72+ # Fetch the merge commit (it may not exist locally yet)
73+ git fetch origin " ${MERGE_COMMIT} "
74+
75+ # Use -m1 only for true merge commits (more than one parent)
76+ PARENT_COUNT=$( git rev-list --parents -n1 " ${MERGE_COMMIT} " | wc -w)
77+ # rev-list output includes the commit itself, so >2 words means multiple parents
78+ CHERRY_PICK_ARGS=(" -x" )
79+ if [[ " ${PARENT_COUNT} " -gt 2 ]]; then
80+ CHERRY_PICK_ARGS+=(" -m1" )
81+ fi
82+
83+ if ! git cherry-pick " ${CHERRY_PICK_ARGS[@]} " " ${MERGE_COMMIT} " ; then
6884 echo " "
6985 echo " Cherry-pick has conflicts. Please resolve them, then run:"
7086 echo " git cherry-pick --continue"
0 commit comments