comphead commented on issue #21079:
URL: https://github.com/apache/datafusion/issues/21079#issuecomment-4217091317

   I clauded a small helper script you can find below and use.
   
   ```
   # What it does:
   #   1. Fetches the latest <UPSTREAM_REMOTE>/<BASE_BRANCH>
   #   2. Creates a local branch pr-<PR_NUMBER>-backport from it
   #   3. Fetches the PR commits and cherry-picks them onto the branch
   #   4. Pushes the branch to origin
   ```
   
   The script
   
   ```
   #!/usr/bin/env bash
   #
   # Backport a GitHub PR onto a target branch via cherry-pick.
   #
   # Usage:
   #   backport-pr.sh <PR_NUMBER> <UPSTREAM_REMOTE> <BASE_BRANCH>
   #
   # Example:
   #   backport-pr.sh 20900 upstream branch-53
   #
   # What it does:
   #   1. Fetches the latest <UPSTREAM_REMOTE>/<BASE_BRANCH>
   #   2. Creates a local branch pr-<PR_NUMBER>-backport from it
   #   3. Fetches the PR commits and cherry-picks them onto the branch
   #   4. Pushes the branch to origin
   
   set -euo pipefail
   
   if [ "$#" -ne 3 ]; then
     echo "Usage: $0 <PR_NUMBER> <UPSTREAM_REMOTE> <BASE_BRANCH>"
     echo "Example: $0 20900 upstream branch-53"
     exit 1
   fi
   
   PR_NUMBER="$1"
   UPSTREAM_REMOTE="$2"
   BASE_BRANCH="$3"
   
   BACKPORT_BRANCH="pr-${PR_NUMBER}-backport"
   
   # -- Stash any local changes 
--------------------------------------------------
   STASH_CREATED=false
   if ! git diff --quiet || ! git diff --cached --quiet; then
     echo ">> Stashing local changes"
     git stash push -m "auto-backport-${PR_NUMBER}"
     STASH_CREATED=true
   fi
   
   cleanup() {
     if [ "${STASH_CREATED}" = true ]; then
       echo ">> Restoring stashed changes"
       git stash pop || true
     fi
   }
   trap cleanup EXIT
   
   # -- Fetch upstream and create backport branch 
---------------------------------
   echo ">> Fetching ${UPSTREAM_REMOTE}"
   git fetch "${UPSTREAM_REMOTE}"
   
   echo ">> Creating branch ${BACKPORT_BRANCH} from 
${UPSTREAM_REMOTE}/${BASE_BRANCH}"
   git checkout -B "${BACKPORT_BRANCH}" "${UPSTREAM_REMOTE}/${BASE_BRANCH}"
   
   # -- Fetch PR commits and determine range 
--------------------------------------
   echo ">> Fetching PR #${PR_NUMBER} from ${UPSTREAM_REMOTE}"
   git fetch "${UPSTREAM_REMOTE}" "pull/${PR_NUMBER}/head:pr-${PR_NUMBER}-tmp"
   
   # Find the merge base between upstream main and the PR head to get only PR 
commits
   MERGE_BASE=$(git merge-base "${UPSTREAM_REMOTE}/main" "pr-${PR_NUMBER}-tmp")
   COMMITS=$(git log --reverse --format=%H "${MERGE_BASE}..pr-${PR_NUMBER}-tmp")
   
   if [ -z "${COMMITS}" ]; then
     echo "No commits found for PR #${PR_NUMBER}"
     git branch -D "pr-${PR_NUMBER}-tmp"
     exit 1
   fi
   
   COMMIT_COUNT=$(echo "${COMMITS}" | wc -l | tr -d ' ')
   echo ">> Cherry-picking ${COMMIT_COUNT} commit(s)"
   
   # -- Cherry-pick 
---------------------------------------------------------------
   PICKED=0
   for commit in ${COMMITS}; do
     SHORT=$(git log -1 --format='%h %s' "${commit}")
     echo "   picking: ${SHORT}"
   
     if git cherry-pick -x "${commit}"; then
       PICKED=$((PICKED + 1))
       continue
     fi
   
     # Skip empty commits (already applied upstream)
     if git diff --quiet && git diff --cached --quiet; then
       echo "   -> empty after cherry-pick, skipping"
       git cherry-pick --skip
       continue
     fi
   
     echo ""
     echo "!! Conflict encountered while cherry-picking ${SHORT}"
     echo "   Resolve conflicts, then run:"
     echo "     git cherry-pick --continue"
     echo "     git push -u origin ${BACKPORT_BRANCH}"
     git branch -D "pr-${PR_NUMBER}-tmp" 2>/dev/null || true
     exit 1
   done
   
   # -- Cleanup temp branch and push 
----------------------------------------------
   git branch -D "pr-${PR_NUMBER}-tmp"
   
   if [ "${PICKED}" -eq 0 ]; then
     echo ">> All commits were already applied — no new changes"
   fi
   
   echo ">> Pushing ${BACKPORT_BRANCH} to origin (${PICKED} new commit(s))"
   git push -u origin "${BACKPORT_BRANCH}"
   
   echo ""
   echo "Done! Branch ${BACKPORT_BRANCH} pushed to origin."
   echo "Create a PR targeting ${BASE_BRANCH} when ready."
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to