Skip to content

Instantly share code, notes, and snippets.

@mx-moth
Created December 12, 2015 02:02
Show Gist options
  • Save mx-moth/463eca06558341f834f3 to your computer and use it in GitHub Desktop.
Save mx-moth/463eca06558341f834f3 to your computer and use it in GitHub Desktop.
Automatically rebase a collection of branches on upstream/master, run the tests, and push to origin if the tests pass
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
upstream='upstream/master'
if ! ( git diff --exit-code && git diff --cached --exit-code ) ; then
echo "Working tree is dirty, aborting"
exit 1
fi
git fetch upstream
for pr in $( cat pr-branches.txt ) ; do
bpr=$( printf "\e[1;32m%s\e[0m" "${pr}" )
echo "-----"
echo "$bpr"
git checkout -qf "$pr"
initial_sha=$( git rev-parse "${pr}" )
origin_sha=$( git rev-parse "origin/${pr}" )
if ! git rebase -q "${upstream}" ; then
git rebase --abort
echo "Error while rebasing ${bpr} on ${upstream}"
continue
fi
new_sha=$( git rev-parse "${pr}" )
if [[ "${new_sha}" = "${origin_sha}" ]] ; then
echo "up-to-date"
continue
fi
test_output="test-$( echo "$pr" | tr '/' '-' ).log"
echo "Running tests"
if ./runtests.py &>"${test_output}" ; then
echo "Tests passed! Pushing ${bpr} to origin"
rm -f "${test_output}"
git push origin -qf "${pr}"
else
echo "Tests failed! Check ${test_output}, fix ${bpr} yourself and try again"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment