Skip to content

Instantly share code, notes, and snippets.

@Makman2
Last active December 13, 2017 20:00
Show Gist options
  • Save Makman2/549db58430f50909dd15842e36fc726d to your computer and use it in GitHub Desktop.
Save Makman2/549db58430f50909dd15842e36fc726d to your computer and use it in GitHub Desktop.
Fast-forward merge script for GitHub PRs
#!/usr/bin/env bash
# How to use:
# bash merge.sh <PR-id>
set -e
git checkout master
git pull
branch_name=pr$1
git fetch origin pull/$1/head:$branch_name
git checkout $branch_name
# Check if branch is up-to-date with master.
hash1=$(git show-ref --heads -s master)
hash2=$(git merge-base master $branch_name)
if [ "${hash1}" = "${hash2}" ]; then
echo "Branch is up-to-date."
else
echo "Rebase required!"
exit 1
fi
# Check if all CI checks are green.
curl -s https://api.github.com/repos/coala/${PWD##*/}/commits/$(git rev-parse HEAD)/statuses \
| paste -sd "" - \
| python3 -c 'import json, sys; o = json.loads(input()); c = {x["context"] for x in o}; sys.exit({(x["state"], c.remove(x["context"]))[0] for x in o if x["context"] in c} != {"success"})'
# Merge.
git checkout master
git merge --ff-only $branch_name
git branch -d $branch_name
echo "Merge successful."
echo "Please run 'git push' to update the upstream master branch!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment