|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +usage() { |
| 6 | + echo "Usage: $0 <PR-number> <release-branch>" |
| 7 | + echo "" |
| 8 | + echo "Cherry-pick a merged PR into a release branch and open a new PR." |
| 9 | + echo "" |
| 10 | + echo "Examples:" |
| 11 | + echo " $0 123 release-v0.1" |
| 12 | + echo " $0 456 release-v0.2" |
| 13 | + echo "" |
| 14 | + echo "Prerequisites:" |
| 15 | + echo " - gh CLI installed and authenticated" |
| 16 | + echo " - Clean working tree" |
| 17 | + exit 1 |
| 18 | +} |
| 19 | + |
| 20 | +if [[ $# -ne 2 ]]; then |
| 21 | + usage |
| 22 | +fi |
| 23 | + |
| 24 | +PR_NUMBER="$1" |
| 25 | +RELEASE_BRANCH="$2" |
| 26 | + |
| 27 | +# Verify gh CLI is available |
| 28 | +if ! command -v gh &>/dev/null; then |
| 29 | + echo "Error: gh CLI is not installed. See https://cli.github.com/" |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | + |
| 33 | +# Verify clean working tree |
| 34 | +if [[ -n "$(git status --porcelain)" ]]; then |
| 35 | + echo "Error: working tree is not clean. Please commit or stash your changes." |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | + |
| 39 | +# Verify the release branch exists on remote |
| 40 | +if ! git ls-remote --exit-code --heads origin "${RELEASE_BRANCH}" &>/dev/null; then |
| 41 | + echo "Error: branch '${RELEASE_BRANCH}' does not exist on remote." |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +# Get the merge commit SHA for the PR |
| 46 | +MERGE_COMMIT=$(gh pr view "${PR_NUMBER}" --json mergeCommit --jq '.mergeCommit.oid') |
| 47 | +if [[ -z "${MERGE_COMMIT}" || "${MERGE_COMMIT}" == "null" ]]; then |
| 48 | + echo "Error: PR #${PR_NUMBER} has no merge commit. Is it merged?" |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +PR_TITLE=$(gh pr view "${PR_NUMBER}" --json title --jq '.title') |
| 53 | +CHERRY_PICK_BRANCH="cherry-pick-${PR_NUMBER}-into-${RELEASE_BRANCH}" |
| 54 | + |
| 55 | +echo "Cherry-picking PR #${PR_NUMBER} (\"${PR_TITLE}\") into ${RELEASE_BRANCH}" |
| 56 | +echo " Merge commit: ${MERGE_COMMIT}" |
| 57 | +echo " Branch: ${CHERRY_PICK_BRANCH}" |
| 58 | +echo "" |
| 59 | + |
| 60 | +# Fetch latest remote state |
| 61 | +git fetch origin "${RELEASE_BRANCH}" |
| 62 | + |
| 63 | +# 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 |
| 70 | +git switch -c "${CHERRY_PICK_BRANCH}" "origin/${RELEASE_BRANCH}" |
| 71 | + |
| 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 |
| 84 | + echo "" |
| 85 | + echo "Cherry-pick has conflicts. Please resolve them, then run:" |
| 86 | + echo " git cherry-pick --continue" |
| 87 | + echo " git push origin ${CHERRY_PICK_BRANCH}" |
| 88 | + echo " gh pr create --base ${RELEASE_BRANCH} --title \"🍒 [${RELEASE_BRANCH}] ${PR_TITLE}\" --body \"Cherry-pick of #${PR_NUMBER} into ${RELEASE_BRANCH}.\"" |
| 89 | + exit 1 |
| 90 | +fi |
| 91 | + |
| 92 | +# Push and create PR |
| 93 | +git push origin "${CHERRY_PICK_BRANCH}" |
| 94 | +gh pr create \ |
| 95 | + --base "${RELEASE_BRANCH}" \ |
| 96 | + --title "🍒 [${RELEASE_BRANCH}] ${PR_TITLE}" \ |
| 97 | + --body "Cherry-pick of #${PR_NUMBER} into \`${RELEASE_BRANCH}\`." |
| 98 | + |
| 99 | +echo "" |
| 100 | +echo "Done." |
0 commit comments