Restore a unified diff

N

Nick Allen

After using ndiff from difflib, the function restore
would return the sequence that generated the delta. 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?

Thanks for all your help in advance.

Best Regards,
Nick


Nick Allen <[email protected]>
 
T

Tim Peters

[Nick Allen]
After using ndiff from difflib, the function restore
would return the sequence that generated the delta.

It can actually reproduce either sequence from which the delta was
generated. But this is really trivial: ndiff was intended to produce
diff output for humans to read, and includes the full text of both
input sequences in "the diff" it generates.
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?

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.
 
M

Mike Meyer

Tim Peters said:
[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?

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.

The unix patch utility seems to do a fine job of handling the unix
unified and context diffs. Unified is the preferred format for the
open source projects I contribute patches to.

Possibly this is some other form of "unified diff" than what you get
from "diff -u" on a Unix system?

<mike
 
T

Tim Peters

[Nick Allen]
[Tim Peters]
[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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top