Permanently reversing a patch-file Permanently reversing a patch-file linux linux

Permanently reversing a patch-file


You can use the tool interdiff(1) from patchutils. In particular, the man page for interdiff says:

To reverse a patch, use /dev/null for diff2.

So,

$ interdiff -q file.patch /dev/null > reversed.patch

The -q / --quiet prevents the insertion of reverted: lines.


Try:

patch -R file.txt file.patchdiff 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/---/" > headtail orig.diff -n+3 > tailcat head tail > headtailrm head tail

So, finally, our (quick and dirty) script looks like:

#!/usr/bin/env shF="$1"head -2 $F | tac | sed -e "s/+++/PPP/" -e "s/---/+++/" -e "s/PPP/---/" > $F.headtail $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.tailcat $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 shswap() {    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 $filetail $file -n+3 | fix_chunk_header | fix_lines


I had applied a patch patch -N -p0 < path/file.patch but i started facing compilation issues due to incomplete code all i did was to run this command patch -p0 -R < path/file.patch . Referred this link