#!/bin/sh

# quilt_refresh_push.sh v0.02 by Con Kolivas <kernel@kolivas.org>
#
# This simple script will use the patch script quilt to refresh a patch
# series as much as possible automated without risk. It is designed to be
# used to resync a patchset with a newer codebase. It will push through every
# patch in a quilt series and refresh it. If it encounters a failed push it
# will stop and output the error message without pushing the patch. If it
# encounters any level of fuzz it will push the patch but stop at that point
# without refreshing and output the message to allow you to examine the
# changes and manually refresh before moving on.

grepout=""
while true
do
	grepout=`quilt push 2>&1`
	if [[ `echo $grepout | grep FAILED` ]]; then
		echo "Patch \"`quilt next`\" fails with following failures and has not been applied; needs manual merge."
		quilt push | grep FAILED
		exit
	fi

	if [[ `echo $grepout | grep fuzz` ]]; then
		quilt pop -q
		echo "Patch \"`quilt next`\" has fuzz; The patch has been applied but it is recommended to examine the code. The following output occurs and you need to use \"quilt refresh\" manually to continue."
		quilt push | grep fuzz
		exit
	fi

	if [[ `echo $grepout | grep "File series fully applied"` ]]; then
		echo $grepout
		exit
	fi

	if [[ `echo $grepout | grep "appears to be empty"` ]]; then
		quilt pop -q
		echo "Patch \"`quilt next`\" has nothing in it. It has not been applied and it is recommended to check that the file exists before continuing"
		exit
	fi

	quilt refresh --diffstat --strip-trailing-whitespace
done

