Restore a unified diff

Discussion in 'Python' started by Nick Allen, Jan 4, 2005.

  1. Nick Allen

    Nick Allen Guest

    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 <-state.edu>
     
    Nick Allen, Jan 4, 2005
    #1
    1. Advertising

  2. Nick Allen

    Tim Peters Guest

    [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.
     
    Tim Peters, Jan 5, 2005
    #2
    1. Advertising

  3. Nick Allen

    Mike Meyer Guest

    Tim Peters <> writes:

    > [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
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Jan 5, 2005
    #3
  4. Nick Allen

    Tim Peters Guest

    [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.
     
    Tim Peters, Jan 5, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cyril Vi?ville

    diff Process under diff users

    Cyril Vi?ville, Jun 29, 2004, in forum: Perl
    Replies:
    1
    Views:
    513
    Joe Smith
    Jun 29, 2004
  2. Berrucho
    Replies:
    2
    Views:
    642
    Infant Newbie
    Dec 5, 2003
  3. A Traveler

    Diff CSS styles for diff INPUT TYPE='s?

    A Traveler, Aug 31, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,930
    Steve Fulton
    Aug 31, 2004
  4. Bijoy Naick

    Rational Unified Process

    Bijoy Naick, Dec 3, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    316
    Bijoy Naick
    Dec 3, 2004
  5. Austin Ziegler

    [ANN] Diff::LCS 1.1.0, Diff::LCS 1.0.4

    Austin Ziegler, Aug 8, 2004, in forum: Ruby
    Replies:
    3
    Views:
    196
    Austin Ziegler
    Aug 9, 2004
Loading...

Share This Page