2013-11-21 20:49:51 -08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# This script will locally construct a merge commit for a pull request on a
|
|
|
|
# github repository, inspect it, sign it and optionally push it.
|
|
|
|
|
|
|
|
# The following temporary branches are created/overwritten and deleted:
|
|
|
|
# * pull/$PULL/base (the current master we're merging onto)
|
|
|
|
# * pull/$PULL/head (the current state of the remote pull request)
|
|
|
|
# * pull/$PULL/merge (github's merge)
|
|
|
|
# * pull/$PULL/local-merge (our merge)
|
|
|
|
|
|
|
|
# In case of a clean merge that is accepted by the user, the local branch with
|
|
|
|
# name $BRANCH is overwritten with the merged result, and optionally pushed.
|
|
|
|
|
|
|
|
REPO="$(git config --get githubmerge.repository)"
|
2014-10-07 00:34:46 -07:00
|
|
|
if [[ "d$REPO" == "d" ]]; then
|
2013-11-21 20:49:51 -08:00
|
|
|
echo "ERROR: No repository configured. Use this command to set:" >&2
|
|
|
|
echo "git config githubmerge.repository <owner>/<repo>" >&2
|
|
|
|
echo "In addition, you can set the following variables:" >&2
|
|
|
|
echo "- githubmerge.host (default git@github.com)" >&2
|
|
|
|
echo "- githubmerge.branch (default master)" >&2
|
|
|
|
echo "- githubmerge.testcmd (default none)" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
HOST="$(git config --get githubmerge.host)"
|
2014-10-07 00:34:46 -07:00
|
|
|
if [[ "d$HOST" == "d" ]]; then
|
2013-11-21 20:49:51 -08:00
|
|
|
HOST="git@github.com"
|
|
|
|
fi
|
|
|
|
|
|
|
|
BRANCH="$(git config --get githubmerge.branch)"
|
2014-10-07 00:34:46 -07:00
|
|
|
if [[ "d$BRANCH" == "d" ]]; then
|
2013-11-21 20:49:51 -08:00
|
|
|
BRANCH="master"
|
|
|
|
fi
|
|
|
|
|
|
|
|
TESTCMD="$(git config --get githubmerge.testcmd)"
|
|
|
|
|
|
|
|
PULL="$1"
|
|
|
|
|
2014-10-07 00:34:46 -07:00
|
|
|
if [[ "d$PULL" == "d" ]]; then
|
2013-11-21 20:49:51 -08:00
|
|
|
echo "Usage: $0 pullnumber [branch]" >&2
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
2014-10-07 00:34:46 -07:00
|
|
|
if [[ "d$2" != "d" ]]; then
|
2013-11-21 20:49:51 -08:00
|
|
|
BRANCH="$2"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Initialize source branches.
|
|
|
|
git checkout -q "$BRANCH"
|
|
|
|
if git fetch -q "$HOST":"$REPO" "+refs/pull/$PULL/*:refs/heads/pull/$PULL/*"; then
|
2014-08-19 10:47:37 -07:00
|
|
|
if ! git log -q -1 "refs/heads/pull/$PULL/head" >/dev/null 2>&1; then
|
2013-11-21 20:49:51 -08:00
|
|
|
echo "ERROR: Cannot find head of pull request #$PULL on $HOST:$REPO." >&2
|
|
|
|
exit 3
|
|
|
|
fi
|
2014-08-19 10:47:37 -07:00
|
|
|
if ! git log -q -1 "refs/heads/pull/$PULL/merge" >/dev/null 2>&1; then
|
2013-11-21 20:49:51 -08:00
|
|
|
echo "ERROR: Cannot find merge of pull request #$PULL on $HOST:$REPO." >&2
|
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "ERROR: Cannot find pull request #$PULL on $HOST:$REPO." >&2
|
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
if git fetch -q "$HOST":"$REPO" +refs/heads/"$BRANCH":refs/heads/pull/"$PULL"/base; then
|
|
|
|
true
|
|
|
|
else
|
|
|
|
echo "ERROR: Cannot find branch $BRANCH on $HOST:$REPO." >&2
|
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
git checkout -q pull/"$PULL"/base
|
|
|
|
git branch -q -D pull/"$PULL"/local-merge 2>/dev/null
|
|
|
|
git checkout -q -b pull/"$PULL"/local-merge
|
|
|
|
TMPDIR="$(mktemp -d -t ghmXXXXX)"
|
|
|
|
|
|
|
|
function cleanup() {
|
|
|
|
git checkout -q "$BRANCH"
|
|
|
|
git branch -q -D pull/"$PULL"/head 2>/dev/null
|
|
|
|
git branch -q -D pull/"$PULL"/base 2>/dev/null
|
|
|
|
git branch -q -D pull/"$PULL"/merge 2>/dev/null
|
|
|
|
git branch -q -D pull/"$PULL"/local-merge 2>/dev/null
|
|
|
|
rm -rf "$TMPDIR"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create unsigned merge commit.
|
|
|
|
(
|
2015-02-20 00:54:38 -08:00
|
|
|
echo "Merge pull request #$PULL"
|
2013-11-21 20:49:51 -08:00
|
|
|
echo ""
|
|
|
|
git log --no-merges --topo-order --pretty='format:%h %s (%an)' pull/"$PULL"/base..pull/"$PULL"/head
|
|
|
|
)>"$TMPDIR/message"
|
|
|
|
if git merge -q --commit --no-edit --no-ff -m "$(<"$TMPDIR/message")" pull/"$PULL"/head; then
|
2015-02-20 00:54:38 -08:00
|
|
|
if [ "d$(git log --pretty='format:%s' -n 1)" != "dMerge pull request #$PULL" ]; then
|
2013-11-21 20:49:51 -08:00
|
|
|
echo "ERROR: Creating merge failed (already merged?)." >&2
|
|
|
|
cleanup
|
|
|
|
exit 4
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "ERROR: Cannot be merged cleanly." >&2
|
|
|
|
git merge --abort
|
|
|
|
cleanup
|
|
|
|
exit 4
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Run test command if configured.
|
2014-10-07 00:34:46 -07:00
|
|
|
if [[ "d$TESTCMD" != "d" ]]; then
|
2013-11-21 20:49:51 -08:00
|
|
|
# Go up to the repository's root.
|
|
|
|
while [ ! -d .git ]; do cd ..; done
|
|
|
|
if ! $TESTCMD; then
|
|
|
|
echo "ERROR: Running $TESTCMD failed." >&2
|
|
|
|
cleanup
|
|
|
|
exit 5
|
|
|
|
fi
|
|
|
|
# Show the created merge.
|
|
|
|
git diff pull/"$PULL"/merge..pull/"$PULL"/local-merge >"$TMPDIR"/diff
|
|
|
|
git diff pull/"$PULL"/base..pull/"$PULL"/local-merge
|
2014-10-07 00:34:46 -07:00
|
|
|
if [[ "$(<"$TMPDIR"/diff)" != "" ]]; then
|
2013-11-21 20:49:51 -08:00
|
|
|
echo "WARNING: merge differs from github!" >&2
|
|
|
|
read -p "Type 'ignore' to continue. " -r >&2
|
2014-10-07 00:34:46 -07:00
|
|
|
if [[ "d$REPLY" =~ ^d[iI][gG][nN][oO][rR][eE]$ ]]; then
|
2013-11-21 20:49:51 -08:00
|
|
|
echo "Difference with github ignored." >&2
|
|
|
|
else
|
|
|
|
cleanup
|
|
|
|
exit 6
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
read -p "Press 'd' to accept the diff. " -n 1 -r >&2
|
|
|
|
echo
|
2014-10-07 00:34:46 -07:00
|
|
|
if [[ "d$REPLY" =~ ^d[dD]$ ]]; then
|
2013-11-21 20:49:51 -08:00
|
|
|
echo "Diff accepted." >&2
|
|
|
|
else
|
|
|
|
echo "ERROR: Diff rejected." >&2
|
|
|
|
cleanup
|
|
|
|
exit 6
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# Verify the result.
|
|
|
|
echo "Dropping you on a shell so you can try building/testing the merged source." >&2
|
|
|
|
echo "Run 'git diff HEAD~' to show the changes being merged." >&2
|
|
|
|
echo "Type 'exit' when done." >&2
|
2014-12-09 01:16:58 -08:00
|
|
|
if [[ -f /etc/debian_version ]]; then # Show pull number in prompt on Debian default prompt
|
|
|
|
export debian_chroot="$PULL"
|
|
|
|
fi
|
2013-11-21 20:49:51 -08:00
|
|
|
bash -i
|
|
|
|
read -p "Press 'm' to accept the merge. " -n 1 -r >&2
|
|
|
|
echo
|
2014-10-07 00:34:46 -07:00
|
|
|
if [[ "d$REPLY" =~ ^d[Mm]$ ]]; then
|
2013-11-21 20:49:51 -08:00
|
|
|
echo "Merge accepted." >&2
|
|
|
|
else
|
|
|
|
echo "ERROR: Merge rejected." >&2
|
|
|
|
cleanup
|
|
|
|
exit 7
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Sign the merge commit.
|
|
|
|
read -p "Press 's' to sign off on the merge. " -n 1 -r >&2
|
|
|
|
echo
|
2014-10-07 00:34:46 -07:00
|
|
|
if [[ "d$REPLY" =~ ^d[Ss]$ ]]; then
|
|
|
|
if [[ "$(git config --get user.signingkey)" == "" ]]; then
|
2014-12-20 04:58:51 -08:00
|
|
|
echo "ERROR: No GPG signing key set, not signing. Set one using:" >&2
|
2013-11-21 20:49:51 -08:00
|
|
|
echo "git config --global user.signingkey <key>" >&2
|
2014-12-20 04:58:51 -08:00
|
|
|
cleanup
|
|
|
|
exit 1
|
2013-11-21 20:49:51 -08:00
|
|
|
else
|
|
|
|
git commit -q --gpg-sign --amend --no-edit
|
|
|
|
fi
|
2014-12-20 04:58:51 -08:00
|
|
|
else
|
|
|
|
echo "Not signing off on merge, exiting."
|
|
|
|
cleanup
|
|
|
|
exit 1
|
2013-11-21 20:49:51 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Clean up temporary branches, and put the result in $BRANCH.
|
|
|
|
git checkout -q "$BRANCH"
|
|
|
|
git reset -q --hard pull/"$PULL"/local-merge
|
|
|
|
cleanup
|
|
|
|
|
|
|
|
# Push the result.
|
|
|
|
read -p "Type 'push' to push the result to $HOST:$REPO, branch $BRANCH. " -r >&2
|
2014-10-07 00:34:46 -07:00
|
|
|
if [[ "d$REPLY" =~ ^d[Pp][Uu][Ss][Hh]$ ]]; then
|
2013-11-21 20:49:51 -08:00
|
|
|
git push "$HOST":"$REPO" refs/heads/"$BRANCH"
|
|
|
|
fi
|