Skip to content

Instantly share code, notes, and snippets.

@paulrobinson
Forked from tomjenkinson/gitreqapply
Last active December 25, 2015 04:09
Show Gist options
  • Save paulrobinson/6915610 to your computer and use it in GitHub Desktop.
Save paulrobinson/6915610 to your computer and use it in GitHub Desktop.
#!/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
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"`
if [ $# -eq 0 ]
then
echo "Checking $upstreamname for $reponame"
wget --no-check-certificate https://github.com/$upstreamname/$reponame/pulls/$1 --no-clobber -O .pulls > /dev/null 2>&1
grep js-navigation-open .pulls | sort | sed 's# <a href="\(.*\)" class="js-navigation-open">\(.*\)</a>#https://github.com\1 https://issues.jboss.org/browse/\2#'
rm .pulls
exit
fi
if [ $# -ne 2 ]
then
echo "Need pull number and branch"
exit
fi
git status | grep "Your branch\|Untracked\|Changes not staged"
if [ $? -eq 0 ]; then
echo Ensure working $2 is clean before applying
exit
fi
echo "Applying pull $1 onto $2"
rm -f $1 $1.patch
git fetch upstream
git checkout $2
git up upstream $2
git status | grep "Your branch\|Untracked\|Changes not staged"
if [ $? -eq 0 ]; then
echo Ensure working $2 is clean before applying
exit
fi
wget --no-check-certificate https://github.com/$upstreamname/$reponame/pull/$1
if [ $? -ne 0 ]; then
echo Could not download pull req info
exit
fi
wget --no-check-certificate https://github.com/$upstreamname/$reponame/pull/$1.patch
if [ $? -ne 0 ]; then
echo Could not download patch
exit
fi
git apply --check $1.patch
if [ $? -ne 0 ]; then
echo patch would not be able to apply
exit
fi
username=$(grep "opened this pull request" $1 | sed 's#.*href="/\(.*\)">.*</a> open.*#\1#')
git remote | grep $username > /dev/null 2>&1
if [ "$?" -ne 0 ]; then
git remote add $username git://github.com/$username/$reponame.git
fi
git fetch $username > /dev/null 2>&1
branchname=$(grep "span.*$username.*css-truncate-target" $1 | 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"
exit
fi
fi
git branch -a | grep $username-$branchname
if [ "$?" -eq 0 ]; then
echo "Local branch already existed"
exit
fi
#Creates merge commit
#git checkout -b $username-$branchname $2
#git pull --rebase --ff-only git://github.com/$username/$reponame.git $branchname
#git checkout $2
#git merge $username-$branchname
git fetch $username
git checkout $2
git reset --hard $username/$branchname
git pull --rebase --ff-only upstream $2
git push upstream $2
git reset --hard upstream/$2
git push origin $2
#git branch -d $username-$branchname
git remote rm $username
rm $1
rm $1.patch
xdg-open https://github.com/$upstreamname/$reponame/pull/$1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment