mercurial/mdiff.py
author Thomas Arendsen Hein <thomas@intevation.de>
Fri, 03 Feb 2006 10:54:58 +0100
changeset 1689 c6c76ead1cc1
parent 1637 3b1b44b917f4
child 1723 fde8fb2cbede
permissions -rw-r--r--
Fall back to asciidoc6 html backend if html4 doesn't work. This reenables building documentation on Debian sarge, which broke after 9346a7fb3fe2.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
239
75840796e8e2 mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents: 184
diff changeset
     1
# mdiff.py - diff and patch routines for mercurial
75840796e8e2 mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents: 184
diff changeset
     2
#
75840796e8e2 mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents: 184
diff changeset
     3
# Copyright 2005 Matt Mackall <mpm@selenic.com>
75840796e8e2 mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents: 184
diff changeset
     4
#
75840796e8e2 mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents: 184
diff changeset
     5
# This software may be used and distributed according to the terms
75840796e8e2 mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents: 184
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
75840796e8e2 mdiff.py: kill #! line, add copyright notice
mpm@selenic.com
parents: 184
diff changeset
     7
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
     8
from demandload import demandload
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
     9
import struct, bdiff, util, mpatch
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    10
demandload(globals(), "re")
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    11
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    12
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    13
def unidiff(a, ad, b, bd, fn, r=None, text=False,
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    14
            showfunc=False, ignorews=False):
396
8f8bb77d560e Show revisions in diffs like CVS, based on a patch from Goffredo Baroncelli.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 361
diff changeset
    15
35
9197c26a414b unidiff: punt on comparing empty files
mpm@selenic.com
parents: 0
diff changeset
    16
    if not a and not b: return ""
1379
8ee7ce877be2 Clean up mdiff imports
Matt Mackall <mpm@selenic.com>
parents: 1378
diff changeset
    17
    epoch = util.datestr((0, 0))
264
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    18
1379
8ee7ce877be2 Clean up mdiff imports
Matt Mackall <mpm@selenic.com>
parents: 1378
diff changeset
    19
    if not text and (util.binary(a) or util.binary(b)):
1015
22571b8d35d3 Add automatic binary file detection to diff and export
mpm@selenic.com
parents: 582
diff changeset
    20
        l = ['Binary file %s has changed\n' % fn]
22571b8d35d3 Add automatic binary file detection to diff and export
mpm@selenic.com
parents: 582
diff changeset
    21
    elif a == None:
264
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    22
        b = b.splitlines(1)
1378
a0fcfbbf52bb make diff dates be epoch for add/remove
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1015
diff changeset
    23
        l1 = "--- %s\t%s\n" % ("/dev/null", epoch)
264
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    24
        l2 = "+++ %s\t%s\n" % ("b/" + fn, bd)
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    25
        l3 = "@@ -0,0 +1,%d @@\n" % len(b)
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    26
        l = [l1, l2, l3] + ["+" + e for e in b]
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    27
    elif b == None:
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    28
        a = a.splitlines(1)
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    29
        l1 = "--- %s\t%s\n" % ("a/" + fn, ad)
1378
a0fcfbbf52bb make diff dates be epoch for add/remove
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1015
diff changeset
    30
        l2 = "+++ %s\t%s\n" % ("/dev/null", epoch)
264
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    31
        l3 = "@@ -1,%d +0,0 @@\n" % len(a)
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    32
        l = [l1, l2, l3] + ["-" + e for e in a]
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    33
    else:
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    34
        al = a.splitlines(1)
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    35
        bl = b.splitlines(1)
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    36
        l = list(bunidiff(a, b, al, bl, "a/" + fn, "b/" + fn,
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    37
                          showfunc=showfunc, ignorews=ignorews))
278
777e388c06d6 unidiff: handle empty diffs more gracefully
mpm@selenic.com
parents: 272
diff changeset
    38
        if not l: return ""
272
467cea2bf2d8 diff: use tab to separate date from filename
mpm@selenic.com
parents: 264
diff changeset
    39
        # difflib uses a space, rather than a tab
1540
8ca9f5b17257 minor optimization: save some string trash
twaldmann@thinkmo.de
parents: 1452
diff changeset
    40
        l[0] = "%s\t%s\n" % (l[0][:-2], ad)
8ca9f5b17257 minor optimization: save some string trash
twaldmann@thinkmo.de
parents: 1452
diff changeset
    41
        l[1] = "%s\t%s\n" % (l[1][:-2], bd)
170
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
    42
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
    43
    for ln in xrange(len(l)):
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
    44
        if l[ln][-1] != '\n':
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
    45
            l[ln] += "\n\ No newline at end of file\n"
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
    46
396
8f8bb77d560e Show revisions in diffs like CVS, based on a patch from Goffredo Baroncelli.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 361
diff changeset
    47
    if r:
8f8bb77d560e Show revisions in diffs like CVS, based on a patch from Goffredo Baroncelli.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 361
diff changeset
    48
        l.insert(0, "diff %s %s\n" %
8f8bb77d560e Show revisions in diffs like CVS, based on a patch from Goffredo Baroncelli.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 361
diff changeset
    49
                    (' '.join(["-r %s" % rev for rev in r]), fn))
8f8bb77d560e Show revisions in diffs like CVS, based on a patch from Goffredo Baroncelli.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 361
diff changeset
    50
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    51
    return "".join(l)
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    52
1637
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    53
# somewhat self contained replacement for difflib.unified_diff
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    54
# t1 and t2 are the text to be diffed
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    55
# l1 and l2 are the text broken up into lines
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    56
# header1 and header2 are the filenames for the diff output
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    57
# context is the number of context lines
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    58
# showfunc enables diff -p output
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    59
# ignorews ignores all whitespace changes in the diff
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    60
def bunidiff(t1, t2, l1, l2, header1, header2, context=3, showfunc=False,
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    61
             ignorews=False):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    62
    def contextend(l, len):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    63
        ret = l + context
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    64
        if ret > len:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    65
            ret = len
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    66
        return ret
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    67
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    68
    def contextstart(l):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    69
        ret = l - context
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    70
        if ret < 0:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    71
            return 0
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    72
        return ret
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    73
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    74
    def yieldhunk(hunk, header):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    75
        if header:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    76
            for x in header:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    77
                yield x
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    78
        (astart, a2, bstart, b2, delta) = hunk
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    79
        aend = contextend(a2, len(l1))
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    80
        alen = aend - astart
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    81
        blen = b2 - bstart + aend - a2
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    82
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    83
        func = ""
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    84
        if showfunc:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    85
            # walk backwards from the start of the context
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    86
            # to find a line starting with an alphanumeric char.
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    87
            for x in xrange(astart, -1, -1):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    88
                t = l1[x].rstrip()
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    89
                if funcre.match(t):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    90
                    func = ' ' + t[:40]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    91
                    break
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    92
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    93
        yield "@@ -%d,%d +%d,%d @@%s\n" % (astart + 1, alen,
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    94
                                           bstart + 1, blen, func)
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    95
        for x in delta:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    96
            yield x
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    97
        for x in xrange(a2, aend):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    98
            yield ' ' + l1[x]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
    99
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   100
    header = [ "--- %s\t\n" % header1, "+++ %s\t\n" % header2 ]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   101
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   102
    if showfunc:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   103
        funcre = re.compile('\w')
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   104
    if ignorews:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   105
        wsre = re.compile('[ \t]')
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   106
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   107
    # bdiff.blocks gives us the matching sequences in the files.  The loop
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   108
    # below finds the spaces between those matching sequences and translates
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   109
    # them into diff output.
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   110
    #
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   111
    diff = bdiff.blocks(t1, t2)
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   112
    hunk = None
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   113
    for i in xrange(len(diff)):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   114
        # The first match is special.
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   115
        # we've either found a match starting at line 0 or a match later
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   116
        # in the file.  If it starts later, old and new below will both be
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   117
        # empty and we'll continue to the next match.
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   118
        if i > 0:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   119
            s = diff[i-1]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   120
        else:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   121
            s = [0, 0, 0, 0]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   122
        delta = []
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   123
        s1 = diff[i]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   124
        a1 = s[1]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   125
        a2 = s1[0]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   126
        b1 = s[3]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   127
        b2 = s1[2]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   128
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   129
        old = l1[a1:a2]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   130
        new = l2[b1:b2]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   131
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   132
        # bdiff sometimes gives huge matches past eof, this check eats them,
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   133
        # and deals with the special first match case described above
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   134
        if not old and not new:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   135
            continue
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   136
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   137
        if ignorews:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   138
            wsold = wsre.sub('', "".join(old))
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   139
            wsnew = wsre.sub('', "".join(new))
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   140
            if wsold == wsnew:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   141
                continue
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   142
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   143
        astart = contextstart(a1)
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   144
        bstart = contextstart(b1)
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   145
        prev = None
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   146
        if hunk:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   147
            # join with the previous hunk if it falls inside the context
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   148
            if astart < hunk[1] + context + 1:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   149
                prev = hunk
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   150
                astart = hunk[1]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   151
                bstart = hunk[3]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   152
            else:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   153
                for x in yieldhunk(hunk, header):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   154
                    yield x
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   155
                # we only want to yield the header if the files differ, and
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   156
                # we only want to yield it once.
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   157
                header = None
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   158
        if prev:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   159
            # we've joined the previous hunk, record the new ending points.
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   160
            hunk[1] = a2
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   161
            hunk[3] = b2
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   162
            delta = hunk[4]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   163
        else:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   164
            # create a new hunk
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   165
            hunk = [ astart, a2, bstart, b2, delta ]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   166
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   167
        delta[len(delta):] = [ ' ' + x for x in l1[astart:a1] ]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   168
        delta[len(delta):] = [ '-' + x for x in old ]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   169
        delta[len(delta):] = [ '+' + x for x in new ]
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   170
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   171
    if hunk:
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   172
        for x in yieldhunk(hunk, header):
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   173
            yield x
3b1b44b917f4 Add new bdiff based unidiff generation.
mason@suse.com
parents: 1540
diff changeset
   174
120
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
   175
def patchtext(bin):
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
   176
    pos = 0
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
   177
    t = []
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
   178
    while pos < len(bin):
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
   179
        p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12])
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
   180
        pos += 12
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
   181
        t.append(bin[pos:pos + l])
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
   182
        pos += l
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
   183
    return "".join(t)
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
   184
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
   185
def patch(a, bin):
1379
8ee7ce877be2 Clean up mdiff imports
Matt Mackall <mpm@selenic.com>
parents: 1378
diff changeset
   186
    return mpatch.patches(a, [bin])
432
3b9e3d3d2810 Start using bdiff for generating deltas
mpm@selenic.com
parents: 396
diff changeset
   187
1379
8ee7ce877be2 Clean up mdiff imports
Matt Mackall <mpm@selenic.com>
parents: 1378
diff changeset
   188
patches = mpatch.patches
432
3b9e3d3d2810 Start using bdiff for generating deltas
mpm@selenic.com
parents: 396
diff changeset
   189
textdiff = bdiff.bdiff