2021-10-14 21:21:30 +00:00
#!/usr/bin/env bash
2020-09-27 20:17:29 +00:00
set -eou pipefail
help( ) {
2023-07-17 13:29:59 +00:00
echo " $0 [-b <branch>] range [end] "
echo " merges every merge commit present in upstream and missing in <branch> (default: master)."
2020-09-27 20:17:29 +00:00
echo " If the optional [end] commit is provided, only merges up to [end]."
2023-07-17 13:29:59 +00:00
echo " If the optional [-b branch] provided, then ."
2020-09-27 20:17:29 +00:00
echo
2023-07-17 13:29:59 +00:00
echo " $0 [-b <branch>] select <commit> ... <commit> "
echo " merges every selected merge commit into <branch> (default: master)"
2020-09-27 20:17:29 +00:00
echo
echo "This tool creates a branch and a script that can be executed to create the"
echo "PR automatically. The script requires the github-cli tool (aka gh)."
echo ""
echo "Tip: \`git log --oneline upstream/master --merges\` shows merge commits."
exit 1
}
REMOTE = upstream
2021-07-12 18:23:18 +02:00
REMOTE_BRANCH = " $REMOTE /master "
2023-07-17 13:29:59 +00:00
LOCAL_BRANCH = "master"
2020-09-27 20:17:29 +00:00
# Makes sure you have a remote "upstream" that is up-to-date
setup( ) {
ret = 0
2021-07-12 18:23:18 +02:00
git fetch " $REMOTE " & > /dev/null || ret = " $? "
2020-09-27 20:17:29 +00:00
if [ ${ ret } = = 0 ] ; then
return
fi
echo " Adding remote \" $REMOTE \" with URL git@github.com:bitcoin-core/secp256k1.git. Continue with y "
read -r yn
case $yn in
[ Yy] * ) ; ;
* ) exit 1; ;
esac
2021-07-12 18:23:18 +02:00
git remote add " $REMOTE " git@github.com:bitcoin-core/secp256k1.git & > /dev/null
git fetch " $REMOTE " & > /dev/null
2020-09-27 20:17:29 +00:00
}
range( ) {
2023-07-17 13:29:59 +00:00
RANGESTART_COMMIT = $( git merge-base " $REMOTE_BRANCH " " $LOCAL_BRANCH " )
2021-07-12 18:23:18 +02:00
RANGEEND_COMMIT = $( git rev-parse " $REMOTE_BRANCH " )
2020-09-27 20:17:29 +00:00
if [ " $# " = 1 ] ; then
RANGEEND_COMMIT = $1
fi
2021-07-12 18:24:04 +02:00
COMMITS = $( git --no-pager log --oneline --merges " $RANGESTART_COMMIT " .." $RANGEEND_COMMIT " )
2020-09-27 20:17:29 +00:00
COMMITS = $( echo " $COMMITS " | tac | awk '{ print $1 }' ORS = ' ' )
echo " Merging $COMMITS . Continue with y "
read -r yn
case $yn in
[ Yy] * ) ; ;
* ) exit 1; ;
esac
}
2023-07-17 13:29:59 +00:00
# Process -b <branch> argument
while getopts "b:" opt; do
case $opt in
b)
LOCAL_BRANCH = $OPTARG
; ;
\? )
echo " Invalid option: - $OPTARG " >& 2
; ;
esac
done
# Shift off the processed options
shift $(( OPTIND - 1 ))
if [ " $# " -lt 1 ] ; then
help
fi
2020-09-27 20:17:29 +00:00
case $1 in
range)
shift
setup
range " $@ "
2023-07-17 18:42:25 +02:00
REPRODUCE_COMMAND = " $0 -b $LOCAL_BRANCH range $RANGEEND_COMMIT "
2020-09-27 20:17:29 +00:00
; ;
select )
shift
setup
COMMITS = $*
2023-07-17 18:42:25 +02:00
REPRODUCE_COMMAND = " $0 -b $LOCAL_BRANCH select $@ "
2020-09-27 20:17:29 +00:00
; ;
help )
help
; ;
*)
help
esac
2021-03-10 15:07:07 +01:00
TITLE = "Upstream PRs"
2020-09-27 20:17:29 +00:00
BODY = ""
for COMMIT in $COMMITS
do
2021-07-12 15:12:50 +02:00
PRNUM = $( git log -1 " $COMMIT " --pretty= format:%s | sed s/'Merge \(bitcoin-core\/secp256k1\)\?#\([0-9]*\).*' /'\2' /)
2021-03-10 15:07:07 +01:00
TITLE = " $TITLE $PRNUM , "
2021-07-12 15:12:50 +02:00
BODY = $( printf "%s\n%s" " $BODY " " $( git log -1 " $COMMIT " --pretty= format:%s | sed s/'Merge \(bitcoin-core\/secp256k1\)\?#\([0-9]*\)' /'[bitcoin-core\/secp256k1#\2]' /) " )
2020-09-27 20:17:29 +00:00
done
2021-03-10 15:07:07 +01:00
# Remove trailing ","
TITLE = ${ TITLE %? }
2023-07-18 15:05:27 +02:00
BODY = $( printf "%s\n\n%s\n%s" " $BODY " " This PR can be recreated with \` $REPRODUCE_COMMAND \`. " "Tip: Use \`git show --remerge-diff\` to show the changes manually added to the merge commit." )
2020-09-27 20:17:29 +00:00
echo "-----------------------------------"
echo " $TITLE "
echo "-----------------------------------"
echo " $BODY "
echo "-----------------------------------"
# Create branch from PR commit and create PR
2023-07-17 13:29:59 +00:00
git checkout " $LOCAL_BRANCH "
2023-04-11 12:21:14 +02:00
git pull --autostash
2020-09-27 20:17:29 +00:00
git checkout -b temp-merge-" $PRNUM "
2021-09-15 20:09:35 +00:00
# Escape single quote
# ' -> '\''
quote( ) {
local quoted = ${ 1 // \' / \' \\ \' \' }
printf "%s" " $quoted "
}
TITLE = $( quote " $TITLE " )
BODY = $( quote " $BODY " )
2020-09-27 20:17:29 +00:00
BASEDIR = $( dirname " $0 " )
2021-07-12 18:23:18 +02:00
FNAME = " $BASEDIR /gh-pr-create.sh "
2020-09-27 20:17:29 +00:00
cat <<EOT > "$FNAME "
#!/bin/sh
2021-09-15 20:09:35 +00:00
gh pr create -t '$TITLE' -b '$BODY' --web
2020-09-27 20:17:29 +00:00
# Remove temporary branch
2023-07-17 13:29:59 +00:00
git checkout " $LOCAL_BRANCH "
2020-09-27 20:17:29 +00:00
git branch -D temp-merge-" $PRNUM "
EOT
chmod +x " $FNAME "
echo Run " $FNAME " after solving the merge conflicts
git merge --no-edit -m " Merge commits ' $COMMITS ' into temp-merge- $PRNUM " $COMMITS