#!/bin/bash DIR="$(dirname $1)" FILE="$1" BRANCH="${2:-$(git describe --contains --all HEAD)}" git branch |grep ${BRANCH}- | xargs git branch -D git tag |grep before-${BRANCH} | xargs git tag -d git tag "before-$BRANCH" (grep %patch "$FILE" || grep "^Patch.*:" "$FILE") | awk '{ print $1 " " $2 }' | sed -e 's/%p/P/' -e 's/:.*//' | while read patch_line patch_level do if [ -z "$patch_level" ]; then patch_level="-p1" fi grep $(echo $patch_line:) "$FILE" | awk '{ print $2 }' | while read patch do git tag "before-$BRANCH-$patch" git checkout -b "$BRANCH-$patch" git am -3 --ignore-whitespace $patch_level "$DIR/$patch" cat "$DIR/$patch" | git am -3 --ignore-whitespace $patch_level - if [ $? != 0 ]; then git am --abort git reset --hard "before-$BRANCH-$patch" echo "git-am failed, falling back to patch" patch --fuzz=999 -l $patch_level < "$DIR/$patch" && git add -u git commit -a -m "$patch" fi git checkout $BRANCH git rebase "$BRANCH-$patch" done done