Skip to content

Instantly share code, notes, and snippets.

@neclimdul
Last active August 29, 2015 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neclimdul/d611b5033c6d302fab7e to your computer and use it in GitHub Desktop.
Save neclimdul/d611b5033c6d302fab7e to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Description
#
# Many patches don't apply but can easily be reapplied by using git's very
# powerful merge tools. This script finds the last commit that the patch
# applied to, makes a temporary commit and then tries to merge against the
# given branch.
#
# Usage:
#
# 1) Checkout the branch you want to reapply the patch to.
# 2) run reapply.sh https://www.drupal.org/files/issues/foobar.patch origin/8.x
# 3) git diff origin/8.x > new.patch
#
PATCH="$1"
BRANCH="$2"
wget $PATCH -O tmp.patch
# Get a branch to work in.
git branch -D reapply
git checkout -b reapply
git reset --hard $BRANCH
# Find the last commit the patch applies to.
while : ; do
git reset --hard HEAD^
git apply --index tmp.patch > /dev/null
if [ $? -eq 0 ]; then
break
fi
done
git commit -m "Applying $PATCH"
tmp=$(git merge $BRANCH)
if [ $? -eq 0 ]; then
echo 'Reapply successful.'
else
echo 'Reapply failed.'
fi
rm tmp.patch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment