Skip to content

Instantly share code, notes, and snippets.

@nico
Created June 26, 2024 11:11
Show Gist options
  • Save nico/22f433560648abbed220f73a68900923 to your computer and use it in GitHub Desktop.
Save nico/22f433560648abbed220f73a68900923 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Assumes:
# * you have a remote 'ladybird' pointing to
# https://github.com/LadybirdBrowser/ladybird.git"
# * both origin/master and ladybird/master are up-to-date
# * you're on a branch where you want the cherry-picked commits to appear
#
# Example invocation:
# Meta/cherry-pick-ladybird-pr.sh 242
set -euo pipefail
if [ $# -eq 0 ]; then
echo "Usage: $0 <PR number>"
exit 1
fi
PR_NUMBER=$1
SINCE=$(git merge-base origin/master ladybird/master)
# Approach:
# 1. Use github rest api to get commit hashes and messages of a PR as json
# 2. Use python to print just commit hashes and first commit message lines
# 3. Use `git log --grep` with first line of commit message to get the merged
# upstream commit hash with that commit message, for each commit
# 4. Cherry-pick that upstream commit, for each commit
curl --silent -L \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/LadybirdBrowser/ladybird/pulls/$PR_NUMBER/commits \
| python3 -c '
import sys
import json
data = json.load(sys.stdin)
for item in data:
sha = item["sha"]
message = item["commit"]["message"]
print(sha, message.splitlines()[0])
' \
| while IFS= read -r line; do
COMMIT_HASH=$(echo $line | cut -d ' ' -f 1)
COMMIT_MESSAGE=$(echo $line | cut -d ' ' -f 2-)
# Find the commit in the base branch by commit message
REBASED_COMMIT=$(git log --since="$SINCE" \
--pretty=format:"%H %s" \
--grep "$COMMIT_MESSAGE" \
ladybird/master)
NUM_HITS=$(echo -n "$REBASED_COMMIT" | grep -c '^')
if [ "$NUM_HITS" -ne "1" ]; then
echo "found $NUM_HITS hits for $COMMIT_MESSAGE, exiting"
exit 1
fi
echo "Original commit $COMMIT_HASH maps to $REBASED_COMMIT"
REBASED_HASH=$(echo "$REBASED_COMMIT" | cut -d ' ' -f 1)
git cherry-pick -x "$REBASED_HASH"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment