Skip to content

Instantly share code, notes, and snippets.

@stephenwaite
Last active December 3, 2025 20:49
Show Gist options
  • Select an option

  • Save stephenwaite/f1b3a5203f9d7be99a32c58d2aea2edb to your computer and use it in GitHub Desktop.

Select an option

Save stephenwaite/f1b3a5203f9d7be99a32c58d2aea2edb to your computer and use it in GitHub Desktop.
backport to rel-704
#!/bin/bash
# Test first with dry-run
# ./backport.sh --dry-run abc123def456
# Single commit
# ./backport.sh abc123def456
# Multiple commits
# ./backport.sh commit1 commit2 commit3
# Dry-run with multiple commits
# ./backport.sh --dry-run commit1 commit2 commit3
# Parse flags
DRY_RUN=false
COMMITS=()
for arg in "$@"; do
if [[ "$arg" == "--dry-run" ]] || [[ "$arg" == "-n" ]]; then
DRY_RUN=true
else
COMMITS+=("$arg")
fi
done
if [ ${#COMMITS[@]} -eq 0 ]; then
echo "Usage: $0 [--dry-run] <commit-hash> [<commit-hash>...]"
exit 1
fi
# Build commit list
COMMIT_LIST=""
echo "Commits to backport:"
for commit in "${COMMITS[@]}"; do
COMMIT_MSG=$(git log -1 --pretty='%h - %s' $commit 2>/dev/null)
if [ $? -ne 0 ]; then
echo "Error: Commit $commit not found"
exit 1
fi
echo " $COMMIT_MSG"
COMMIT_LIST="$COMMIT_LIST
- $COMMIT_MSG"
done
# Determine title
if [ ${#COMMITS[@]} -eq 1 ]; then
TITLE="chore: Backport to rel-704: $(git log -1 --pretty=%s ${COMMITS[0]})"
COMMIT_SHORT=$(echo ${COMMITS[0]} | cut -c1-7)
else
TITLE="Backport multiple commits to rel-704"
COMMIT_SHORT="multi"
fi
# Issue body
ISSUE_BODY="Backport the following commits from master to rel-704:
$COMMIT_LIST"
if [ "$DRY_RUN" = true ]; then
echo ""
echo "=== DRY RUN MODE ==="
echo ""
echo "Would create issue:"
echo " Title: $TITLE"
echo " Body: $ISSUE_BODY"
echo " Label: backport"
echo ""
MOCK_ISSUE_NUM="123"
BRANCH_NAME="backport-$MOCK_ISSUE_NUM-$COMMIT_SHORT"
echo "Would execute:"
echo " git fetch origin"
echo " git checkout -b $BRANCH_NAME origin/rel-704"
echo " git cherry-pick ${COMMITS[@]}"
echo " git push -u origin $BRANCH_NAME"
echo ""
echo "Would create PR:"
echo " Base: rel-704"
echo " Title: $TITLE"
echo " Body: Fixes #$MOCK_ISSUE_NUM"
echo ""
echo "Cherry-pick preview (use git log to see these commits):"
for commit in "${COMMITS[@]}"; do
git log -1 --pretty=full $commit
echo "---"
done
exit 0
fi
# Real execution
echo ""
echo "Creating issue..."
ISSUE_NUM=$(gh issue create \
--title "$TITLE" \
--body "$ISSUE_BODY" \
--label "backport" \
| grep -o '[0-9]*$')
echo "✓ Created issue #$ISSUE_NUM"
# Create branch
BRANCH_NAME="backport-$ISSUE_NUM-$COMMIT_SHORT"
echo "Creating branch $BRANCH_NAME..."
git fetch origin && \
git checkout -b $BRANCH_NAME origin/rel-704 || exit 1
echo "Cherry-picking commits..."
if git cherry-pick "${COMMITS[@]}"; then
echo "✓ Cherry-pick successful"
else
echo "✗ Cherry-pick failed - resolve conflicts and run:"
echo " git cherry-pick --continue"
echo " git push -u origin $BRANCH_NAME"
echo " gh pr create --base rel-704 --title \"$TITLE\" --body \"Fixes #$ISSUE_NUM\""
exit 1
fi
echo "Pushing branch..."
git push -u origin $BRANCH_NAME || exit 1
echo "Creating PR..."
gh pr create \
--base rel-704 \
--title "$TITLE" \
--body "Fixes #$ISSUE_NUM
Cherry-picked commits:
$COMMIT_LIST"
echo ""
echo "✓ Done! Issue #$ISSUE_NUM and PR created."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment