[Mageia-dev] Script for rediffing patches

Anssi Hannula anssi at mageia.org
Sun Mar 31 23:17:17 CEST 2013


Hi all,

Colin's question reminded me of another hacky script I use for rediffing
patches. I had planned to clean it up but clearly that is never going to
happen, so I may as well provide it here in case someone else finds it
useful or even wants to make a clean version...

It has accumulated hacks on top of hacks over the years to handle lots
of special cases (%apply_patches, Patch: foo.patch, concatenated
patches, tarballs containing .orig files etc.) with little regard to
readability, but it has worked well for me...

Usage, in pkg checkout:
$ rediffpatches SOURCES/foobar-1.2.tar.bz2

It rediffs all patches automatically, and in case of nonmergeable
conflict it will drop you to shell to apply the patch manually, and will
continue after you exit.

Of course you still *have to* manually review *all* the changes made as
always, but it still makes the work progress faster (especially in pkgs
with lots of patches).

-- 
Anssi Hannula
-------------- next part --------------
#!/bin/bash
allow_fastmode=1
[ -f "$1" ] || exit 1
tarball=$(readlink -f $1)
[ -d ../SPECS ] && cd ../SPECS
[ -d SPECS ] && cd SPECS
name=$(dirname $PWD | sed s,.*/,,)
spec=$(readlink -f $name.spec)
[ -e $spec ] || exit 1
sourcedir=$(readlink -f $PWD/../SOURCES)
#source=$sourcedir/$(awk '/^Source.*:/ { print $2 }' $spec | head -n1)
source=$tarball
rm -rf .rediff
mkdir .rediff
cd .rediff
tar -xf $source
for file in ./*; do
	if [ -L "$file" ]; then
		rm -vf $file
	fi
done
workdir=$(echo */)
workdir=${workdir%/}
plain_magic=9999924
for patch in $(sed "s/^[Pp]atch:/Patch$plain_magic:/" $spec | sed -n -r "/^[Pp]atch.*:/s,^[Pp]atch([0-9]+):\s*(.+)$,\1=\2,p" | sed "s,%{name},${name},"); do
	eval patch$patch
done
ext=.orig
[ $(find */ -name '*.orig' | wc -l) -eq 0 ] || ext=.orig2
apply_magic=
egrep -q '^%{?apply_patches}?' $spec && apply_magic=1
for patch in $(sed -r $([ -n "$apply_magic" ] && echo "-e s/^[Pp]atch([0-9]*):.*$/%patch\1/") -e "s/^%patch[$ ]/%patch$plain_magic /" $spec | sed -n -r "/^%patch[0-9]+/s,^%patch([0-9]+)\s*(\-p[0-9])?.*$,\1:\2,p"); do
	pvar=${patch#*:}
	[ -n "$pvar" ] || pvar=-p1
	patch=${patch%:*}
	eval patch=\$patch$patch
	patch="$sourcedir/${patch##*/}"
	while echo "$patch" | grep -q '%'; do
		variable=$(echo "$patch" | sed -r 's,^.*%\{?([a-zA-Z0-9_]+)\}?.*$,\1,')
		value=$(awk "/^%define[[:space:]]+$variable[[:space:]]+/ { print \$3 }" $spec)
		patch=$(echo "$patch" | sed -r "s,%\{?$variable\}?,$value,")
	done
	if patch -d */ -F0 --dry-run -i $patch $pvar >/dev/null; then
		echo "Up-to-date: $patch"
		patch -d */ -F0 --no-backup-if-mismatch -i $patch $pvar -s || exit 1
		continue
	fi
	echo -n "Rediffing $patch... "
	gawk '{ if ($1 == "Index:" || $1 == "diff" || $1 == "---") exit; print; }' $patch > pretext.txt
	fastmode=1
	if [ -n "$allow_fastmode" ]; then
		if [ $(cat $patch | egrep '^(\+\+\+|---) ' | grep -v '^... /dev/null' | cut -f1 | sed -r 's, [^/]+/, ,' | sort | uniq -d | wc -l) -ne 0 ]; then
			echo -n "combined patch detected, disabling fast mode... "
			fastmode=
		elif [ $(cat $patch | egrep '^(\+\+\+|---) ' | grep -c '^... /dev/null') -ne 0 ]; then
			echo -n "files added or removed, disabling fast mode... "
			fastmode=
	
		elif ! patch -d $workdir -F2 -i $patch --dry-run $pvar >/dev/null; then
			echo -n "patch does not apply, disabling fast mode... "
			fastmode=
		fi
	fi

	pstatus=0
	if [ -n "$fastmode" -a -n "$allow_fastmode" ]; then
		patch -d */ -F2 -V simple -b -z $ext -i $patch $pvar -s || exit 1
	else
		cp -a $workdir newdir
		patch -d newdir -F2 --merge=diff3 -V simple -z $ext -i $patch $pvar > out.txt
		pstatus=$?
	fi
	if [ $pstatus -ne 0 ]; then
		echo
		echo "Patch $patch did not apply cleanly, dropping to shell."
		echo "Apply manually and then exit, or abort with \"exit 1\"."
		cd newdir
		cat ../out.txt
		bash
		ret=$?
		[ $ret -ne 0 ] && echo "Aborted." && exit $ret
		cd ..
		echo -n "Assuming clean state, continuing... "
	fi
	rm -f out.txt
	if [ -n "$fastmode" -a -n "$allow_fastmode" ]; then
		gendiff $workdir $ext > $patch.new
	else
		diff -Nurpa -x '*~' -x '*.orig' -x '*.rej' -x '*.swp' $workdir newdir | sed -e "s,^+++ newdir/,+++ $workdir/," -e "s,^\(diff.* \)newdir/,\1$workdir/,"  > $patch.new
		rm -rf newdir
		patch -d $workdir -F0 -i $patch.new -p1 -s || exit 1
	fi
	backupname=$patch.origsvn
	if [ -e $backupname ]; then
		i=1
		while [ -e $backupname.$i ]; do
			i=$((i+1))
		done
		backupname=$backupname.$i
	fi
	mv $patch $backupname
	cat pretext.txt $patch.new > $patch
	rm $patch.new
	find . -name "*${ext}" -delete
	echo "OK"
done
cd ..
rm -rf .rediff


More information about the Mageia-dev mailing list