Restore a unified diff

Tim Peters tim.peters at
Wed Jan 5 20:53:22 CET 2005

[Nick Allen]
>>> Unfortunately, restore does not do the same for unified_diff.  I do
>>> not see any similar function that is intended for unified_diff.
>>> Does anyone know how to "restore" from a unified diff generated
>>> delta?

[Tim Peters]
>> That's in general impossible, since unified diffs generally omit
>> most lines that compared equal to begin with.  Unified and
>> context diffs are, in part, compression gimmicks, showing only
>> what changed plus a bit of context.  ndiff shows everything, so
>> can restore everything too.

[Mike Meyer]
> The unix patch utility seems to do a fine job of handling the unix
> unified and context diffs.

Of course it does, but "the diff" isn't the only input to `patch`, you
also need to give `patch` the original source file (or one closely
related to it).  `patch` would be deep magic indeed if it could deduce
either the "before" or "after" file from a context or unified diff
*alone*.  But both the "before" and "after" files *can* be deduced
from an ndiff diff alone.

This remains a truly trivial observation:  ndiff can do this because
the full text of both input files is part of the output it produces. 
unified/context diffs cannot generally do this because they don't
generally contain the full text of either input file.

More information about the Python-list mailing list