| •1 |
#!/usr/bin/bash |
| 2 |
|
| 3 |
set -e |
| 4 |
|
| 5 |
if [[ $# == 0 ]]; then |
| 6 |
echo "Usage: $0 [--no-conflict] [--no-signature-check] [--stop-before-merging] prnum" |
| 7 |
exit 1 |
| 8 |
fi |
| 9 |
|
| 10 |
while [[ $# -gt 0 ]]; do |
| 11 |
case "$1" in |
| 12 |
--no-conflict) |
| 13 |
NO_CONFLICTS="true" |
| 14 |
shift |
| 15 |
;; |
| 16 |
--no-signature-check) |
| 17 |
NO_SIGNATURE_CHECK="true" |
| 18 |
shift |
| 19 |
;; |
| 20 |
--stop-before-merging) |
| 21 |
STOP_BEFORE_MERGING="true" |
| 22 |
shift |
| 23 |
;; |
| 24 |
*) |
| 25 |
if [[ "$PRNUM" != "" ]]; then |
| 26 |
echo "PRNUM was already set to $PRNUM and is now being overridden with $1." |
| 27 |
fi |
| 28 |
PRNUM="$1" |
| 29 |
shift |
| 30 |
esac |
| 31 |
done |
| 32 |
|
| 33 |
REMOTE=$(git config --get "branch.master.remote" || true) |
| 34 |
if [ -z "$REMOTE" ]; then |
| 35 |
echo "Branch master has no remote. Using the local state of the master branch instead." |
| 36 |
MASTER=master |
| 37 |
else |
| 38 |
MASTER="$REMOTE/master" |
| 39 |
fi |
| 40 |
|
| 41 |
if ! git log $MASTER --grep "Merge PR #$PRNUM" | grep "." > /dev/null; then |
| 42 |
echo "PR #${PRNUM} does not exist." |
| 43 |
exit 1 |
| 44 |
fi |
| 45 |
|
| 46 |
SIGNATURE_STATUS=$(git log $MASTER --grep "Merge PR #$PRNUM" --format="%G?") |
| 47 |
git log $MASTER --grep "Merge PR #$PRNUM" --format="%GG" |
| 48 |
if [[ "$NO_SIGNATURE_CHECK" != "true" && "$SIGNATURE_STATUS" != "G" ]]; then |
| 49 |
echo |
| 50 |
read -p "Merge commit does not have a good (valid) signature. Bypass? [y/N] " -n 1 -r |
| 51 |
echo |
| 52 |
if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
| 53 |
exit 1 |
| 54 |
fi |
| 55 |
fi |
| 56 |
|
| 57 |
BRANCH=backport-pr-${PRNUM} |
| 58 |
RANGE=$(git log $MASTER --grep "Merge PR #$PRNUM" --format="%P" | sed 's/ /../') |
| 59 |
MESSAGE=$(git log $MASTER --grep "Merge PR #$PRNUM" --format="%s" | sed 's/Merge/Backport/') |
| 60 |
|
| 61 |
if [[ "$(git rev-parse --abbrev-ref HEAD)" == "$BRANCH" ]]; then |
| 62 |
|
| 63 |
if ! git cherry-pick --continue; then |
| 64 |
echo "Please fix the conflicts, then relaunch the script." |
| 65 |
exit 1 |
| 66 |
fi |
| 67 |
git checkout - |
| 68 |
|
| 69 |
elif git checkout -b "$BRANCH"; then |
| 70 |
|
| 71 |
if ! git cherry-pick -x "${RANGE}"; then |
| 72 |
if [[ "$NO_CONFLICTS" == "true" ]]; then |
| 73 |
git status |
| 74 |
echo "Conflicts! Aborting..." |
| 75 |
git cherry-pick --abort |
| 76 |
git checkout - |
| 77 |
git branch -d "$BRANCH" |
| 78 |
exit 1 |
| 79 |
fi |
| 80 |
echo "Please fix the conflicts, then relaunch the script." |
| 81 |
exit 1 |
| 82 |
fi |
| 83 |
git checkout - |
| 84 |
|
| 85 |
else |
| 86 |
|
| 87 |
echo |
| 88 |
read -p "Skip directly to merging phase? [y/N] " -n 1 -r |
| 89 |
echo |
| 90 |
if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
| 91 |
exit 1 |
| 92 |
fi |
| 93 |
|
| 94 |
fi |
| 95 |
|
| 96 |
if ! git diff --exit-code HEAD "${BRANCH}" -- "*.mli"; then |
| 97 |
echo |
| 98 |
read -p "Some mli files are modified. Bypass? [y/N] " -n 1 -r |
| 99 |
echo |
| 100 |
if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
| 101 |
exit 1 |
| 102 |
fi |
| 103 |
fi |
| 104 |
|
| 105 |
if [[ "$STOP_BEFORE_MERGING" == "true" ]]; then |
| 106 |
exit 0 |
| 107 |
fi |
| 108 |
|
| 109 |
git merge -S --no-ff "${BRANCH}" -m "${MESSAGE}" |
| 110 |
git branch -d "${BRANCH}" |
| 111 |
|
| 112 |
# To-Do: |
| 113 |
# - Support for backporting a PR before it is merged |
| 114 |
# - Automatically backport all PRs in the "Waiting to be backported" column using a command like: |
| 115 |
# $ curl -s -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.inertia-preview+json" https://api.github.com/projects/columns/1358120/cards | jq -r '.[].content_url' | grep issue | sed 's/^.*issues\/\([0-9]*\)$/\1/' | tac |
| 116 |
# (The ID of the column must first be obtained through https://api.github.com/repos/coq/coq/projects then https://api.github.com/projects/819866/columns.) |
| 117 |
# - Then move each of the backported PR to the subsequent columns automatically as well... |