1#!/bin/bash
2
3set -eou pipefail
4
5help() {
6    echo "$0 range [end]"
7    echo "    merges every merge commit missing merge commit."
8    echo "    If the optional [end] commit is provided, only merges up to [end]."
9    echo
10    echo "$0 select <commit> ... <commit>"
11    echo "    merges every selected merge commit"
12    echo
13    echo "This tool creates a branch and a script that can be executed to create the"
14    echo "PR automatically. The script requires the github-cli tool (aka gh)."
15    echo ""
16    echo "Tip: \`git log --oneline upstream/master --merges\` shows merge commits."
17    exit 1
18}
19
20if [ "$#" -lt 1 ]; then
21    help
22fi
23
24REMOTE=upstream
25REMOTE_BRANCH=$REMOTE/master
26# Makes sure you have a remote "upstream" that is up-to-date
27setup() {
28    ret=0
29    git fetch $REMOTE &> /dev/null || ret=$?
30    if [ ${ret} == 0 ]; then
31        return
32    fi
33    echo "Adding remote \"$REMOTE\" with URL git@github.com:bitcoin-core/secp256k1.git. Continue with y"
34    read -r yn
35    case $yn in
36        [Yy]* ) ;;
37        * ) exit 1;;
38    esac
39    git remote add $REMOTE git@github.com:bitcoin-core/secp256k1.git &> /dev/null
40    git fetch $REMOTE &> /dev/null
41}
42
43range() {
44    RANGESTART_COMMIT=$(git merge-base $REMOTE_BRANCH master)
45    RANGEEND_COMMIT=$REMOTE_BRANCH
46    if [ "$#" = 1 ]; then
47        RANGEEND_COMMIT=$1
48    fi
49
50    COMMITS=$(git --no-pager log --oneline "$REMOTE_BRANCH" --merges "$RANGESTART_COMMIT".."$RANGEEND_COMMIT")
51    COMMITS=$(echo "$COMMITS" | tac | awk '{ print $1 }' ORS=' ')
52    echo "Merging $COMMITS. Continue with y"
53    read -r yn
54    case $yn in
55        [Yy]* ) ;;
56        * ) exit 1;;
57    esac
58}
59
60case $1 in
61    range)
62        shift
63        setup
64        range "$@"
65        ;;
66    select)
67        shift
68        setup
69        COMMITS=$*
70        ;;
71    help)
72        help
73        ;;
74    *)
75        help
76esac
77
78TITLE="Upstream PRs "
79BODY=""
80for COMMIT in $COMMITS
81do
82    PRNUM=$(git log -1 "$COMMIT" --pretty=format:%s | sed s/'Merge #\([0-9]*\).*'/'\1'/)
83    TITLE="$TITLE #$PRNUM"
84    BODY=$(printf "%s\n%s" "$BODY" "$(git log -1 "$COMMIT" --pretty=format:%s | sed s/'Merge #\([0-9]*\)'/'[upstream PR #\1]'/)")
85done
86
87BODY=$(printf "%s\n\n%s" "$BODY" "This PR was automatically created with \\\`$0 $*\\\`.")
88
89echo "-----------------------------------"
90echo "$TITLE"
91echo "-----------------------------------"
92echo "$BODY"
93echo "-----------------------------------"
94# Create branch from PR commit and create PR
95git checkout master
96git pull
97git checkout -b temp-merge-"$PRNUM"
98
99BASEDIR=$(dirname "$0")
100FNAME=$BASEDIR/gh-pr-create.sh
101cat <<EOT > "$FNAME"
102#!/bin/sh
103gh pr create -t "$TITLE" -b "$BODY" --web
104# Remove temporary branch
105git checkout master
106git branch -D temp-merge-"$PRNUM"
107EOT
108chmod +x "$FNAME"
109echo Run "$FNAME" after solving the merge conflicts
110
111git merge --no-edit -m "Merge commits '$COMMITS' into temp-merge-$PRNUM" $COMMITS
112