Restore a unified diff

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nick Allen

    Restore a unified diff

    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 <allen@cse.oh io-state.edu>
  • Tim Peters

    #2
    Re: Restore a unified diff

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

    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.
    [color=blue]
    > 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?[/color]

    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.

    Comment

    • Mike Meyer

      #3
      Re: Restore a unified diff

      Tim Peters <tim.peters@gma il.com> writes:
      [color=blue]
      > [Nick Allen][color=green]
      >> 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?[/color]
      >
      > 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.[/color]

      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 <mwm@mired.or g> http://www.mired.org/home/mwm/
      Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

      Comment

      • Tim Peters

        #4
        Re: Restore a unified diff

        [Nick Allen][color=blue][color=green][color=darkred]
        >>> 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?[/color][/color][/color]

        [Tim Peters][color=blue][color=green]
        >> 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.[/color][/color]

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

        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.

        Comment

        Working...