Skip to content

Instantly share code, notes, and snippets.

@tomjenkinson
Last active December 25, 2015 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjenkinson/6916734 to your computer and use it in GitHub Desktop.
Save tomjenkinson/6916734 to your computer and use it in GitHub Desktop.
Allows you to rebase a pull request for example
#!/bin/bash
git remote | grep upstream > /dev/null 2>&1
if [ "$?" -ne 0 ]; then
echo This script assumes you have upstream set as the repo to hand reqs in
exit
fi
if [ ! -d .git ]; then
echo This script assumes you are in the root of a repo clone
exit
fi
if [ $# -lt 2 ]
then
echo "Need pull number and branch"
exit
fi
git status | grep "Untracked\|Changes not staged"
if [ $? -eq 0 ]; then
echo Ensure working $2 is clean before applying
exit -1
fi
upstreamname=`git remote -v | grep upstream | grep fetch | sed "s#upstream.*github.com[:/]\(.*\)/.*#\1#"`
reponame=`git remote -v | grep origin | grep push | sed "s#.*/\(.*\)\.git.*#\1#g"`
gitreqnumber=$1
shift
rm -f $gitreqnumber
wget --no-check-certificate https://github.com/$upstreamname/$reponame/pull/$gitreqnumber -O $gitreqnumber
if [ $? -ne 0 ]; then
echo "Could not download pull req info"
exit -1
fi
username=$(grep "by .* · Pull" $pullNumber | sed 's#.*by \(.*\) · Pull.*#\1#')
git remote -v | grep origin | grep $username > /dev/null 2>&1
if [ "$?" -ne 0 ]; then
echo "This is not one of your pull requests"
rm $gitreqnumber
exit 0
fi
branchname=$(grep "span.*$username.*css-truncate-target" $gitreqnumber | sed 's#.*">\(.*\)<.*#\1#')
expr length $branchname > /dev/null 2>&1
if [ "$?" -ne 0 ]; then
echo "Could not find branch name, assuming first word of title!"
branchname=$(grep "<title>" 1 | sed 's#.*<title>\([a-zA-Z0-9\-]*\) .*#\1#')
expr length $branchname > /dev/null 2>&1
if [ "$?" -ne 0 ]; then
echo "Could not find branch name, aborting"
rm $gitreqnumber
exit -1
fi
fi
rm $gitreqnumber
upstreamBranch=$1
shift
git status | grep "Untracked\|Changes not staged"
if [ $? -eq 0 ]; then
echo Ensure working $upstreamBranch is clean before applying
exit -1
fi
git checkout $branchname
if [ "$?" -ne 0 ]; then
echo "Branch $branchname did not exist"
exit -1
fi
git fetch upstream
echo "Trying to rebase $branchname against $upstreamBranch"
git pull --rebase --ff-only upstream $upstreamBranch
if [ $? -ne 0 ]; then
git rebase --abort
echo "Could not perform rebase"
exit -1
fi
echo "Attempting to push"
git push origin $branchname "$@"
if [ $? -ne 0 ]; then
echo "Could not push to upstream"
exit -1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment