Try:
patch -R file.txt file.patch
diff file.txt.orig file.txt > file.patch.rev
// you can then `rm file.txt.orig file.patch`
EDIT:
To reverse a unified diff, you need to change three things:
- the patch header
- the chunk header
- the + to - and - to +
So here's how a patch header for a looks like:
--- b.asm 2010-09-24 12:03:43.000000000 +1000
+++ a.asm 2010-09-24 23:28:43.000000000 +1000
you need to reverse it so it looks like this:
--- a.asm 2010-09-24 23:28:43.000000000 +1000
+++ b.asm 2010-09-24 12:03:43.000000000 +1000
basically switch the order, and switch +++ to --- and vice versa.
Next, the chunk header:
@@ -29,5 +27,7 @@
You need to reverse the numbers, so it look like this:
@@ -27,7 +29,5 @@
basically, switch the number pairs
and last, switch every line beginning with + and every line beginning with -.
EDIT:
to switch the chunk header, you can do:
sed -e "s/@@ -\([0-9]\+,[0-9]\+\) +\([0-9]\+,[0-9]\+\) @@/@@ -\2 +\1 @@/"
to switch + to - and - to +, you can do:
sed -e "s/^+/P/" -e "s/^-/+/" -e "s/^P/-/"
FINALLY:
to reverse the patch header, do:
head -2 orig.diff | tac | sed -e "s/+++/PPP/" -e "s/---/+++/" -e "s/PPP/---/" > head
tail orig.diff -n+3 > tail
cat head tail > headtail
rm head tail
So, finally, our (quick and dirty) script looks like:
#!/usr/bin/env sh
F="$1"
head -2 $F | tac | sed -e "s/+++/PPP/" -e "s/---/+++/" -e "s/PPP/---/" > $F.head
tail $F -n+3 | sed -e "s/@@ -\([0-9]\+,[0-9]\+\) +\([0-9]\+,[0-9]\+\) @@/@@ -\2 +\1 @@/" -e "s/^+/P/" -e "s/^-/+/" -e "s/^P/-/" > $F.tail
cat $F.head $F.tail
rm $F.head $F.tail
I tested it, and it seems to work.
though, to make things more maintainable, and cleaner:
#!/usr/bin/env sh
swap() {
sed -e "s/^$1/PPP/" -e "s/^$2/$1/" -e "s/^PPP/$2/"
}
file_header() {
head -2 $1 | tac | swap +++ ---
}
fix_chunk_header() {
sed -e "s/@@ -\([0-9]\+,[0-9]\+\) +\([0-9]\+,[0-9]\+\) @@/@@ -\2 +\1 @@/"
}
fix_lines() {
swap + -
}
file="$1"
file_header $file
tail $file -n+3 | fix_chunk_header | fix_lines