Skip to content

Commit bf0b912

Browse files
committed
Make cherry picking idempotent and more resiliant
1 parent 1cad67d commit bf0b912

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

.github/workflows/create-release-branch.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ jobs:
1818
TAG="${GITHUB_REF#refs/tags/}"
1919
# Extract major.minor from tag (e.g. v0.1.0 -> release-v0.1)
2020
BRANCH="release-${TAG%.*}"
21-
git switch -c "${BRANCH}"
21+
git switch "${BRANCH}" 2>/dev/null || git switch -c "${BRANCH}"
2222
git push origin "${BRANCH}"

hack/cherry-pick.sh

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fi
4444

4545
# Get the merge commit SHA for the PR
4646
MERGE_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
5050
fi
@@ -61,10 +61,26 @@ echo ""
6161
git 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
6470
git 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

Comments
 (0)