mercurial/mdiff.py
author mpm@selenic.com
Sun, 12 Jun 2005 10:20:07 -0800
changeset 318 2819f63b16bf
parent 317 b18ce742566a
child 325 ad87e19854a6
child 330 27d08c0c2a7e
permissions -rw-r--r--
mdiff: revert grouping optimization for the time being -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 mdiff: revert grouping optimization for the time being This had trouble with Ted T'so import test while the original didn't. manifest hash: e2fc49b5277096bd4c5081558af5efe9964d5310 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) iD8DBQFCrHzXywK+sNU5EO8RArocAJwKlxrnyVpdYaKzgJG/b4gSVOYBTwCgkl2t zD807fsMULRDdDe1k9jVPcU= =Iivz -----END PGP SIGNATURE-----
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
249
619e775aa7f9 import and startup cleanups
mpm@selenic.com
parents: 241
diff changeset
     8
import difflib, struct
127
44538462d3c8 Fix braindamaged import in mdiff.
mpm@selenic.com
parents: 125
diff changeset
     9
from mercurial.mpatch import *
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    10
64
b3e2ddff0159 Diff in subdirectories from Jake Edge
mpm@selenic.com
parents: 35
diff changeset
    11
def unidiff(a, ad, b, bd, fn):
35
9197c26a414b unidiff: punt on comparing empty files
mpm@selenic.com
parents: 0
diff changeset
    12
    if not a and not b: return ""
264
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    13
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    14
    if a == None:
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    15
        b = b.splitlines(1)
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    16
        l1 = "--- %s\t%s\n" % ("/dev/null", ad)
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    17
        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
    18
        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
    19
        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
    20
    elif b == None:
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    21
        a = a.splitlines(1)
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    22
        l1 = "--- %s\t%s\n" % ("a/" + fn, ad)
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    23
        l2 = "+++ %s\t%s\n" % ("/dev/null", bd)
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    24
        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
    25
        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
    26
    else:
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    27
        a = a.splitlines(1)
4c1d7072d5cd Attempt to make diff deal with null sources properly
mpm@selenic.com
parents: 249
diff changeset
    28
        b = b.splitlines(1)
272
467cea2bf2d8 diff: use tab to separate date from filename
mpm@selenic.com
parents: 264
diff changeset
    29
        l = list(difflib.unified_diff(a, b, "a/" + fn, "b/" + fn))
278
777e388c06d6 unidiff: handle empty diffs more gracefully
mpm@selenic.com
parents: 272
diff changeset
    30
        if not l: return ""
272
467cea2bf2d8 diff: use tab to separate date from filename
mpm@selenic.com
parents: 264
diff changeset
    31
        # difflib uses a space, rather than a tab
467cea2bf2d8 diff: use tab to separate date from filename
mpm@selenic.com
parents: 264
diff changeset
    32
        l[0] = l[0][:-2] + "\t" + ad + "\n"
467cea2bf2d8 diff: use tab to separate date from filename
mpm@selenic.com
parents: 264
diff changeset
    33
        l[1] = l[1][:-2] + "\t" + bd + "\n"
170
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
    34
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
    35
    for ln in xrange(len(l)):
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
    36
        if l[ln][-1] != '\n':
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
    37
            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
    38
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    39
    return "".join(l)
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    40
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    41
def textdiff(a, b):
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    42
    return diff(a.splitlines(1), b.splitlines(1))
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    43
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    44
def sortdiff(a, b):
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    45
    la = lb = 0
318
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    46
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    47
    while 1:
318
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    48
        if la >= len(a) or lb >= len(b): break
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    49
        if b[lb] < a[la]:
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    50
            si = lb
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    51
            while lb < len(b) and b[lb] < a[la] : lb += 1
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    52
            yield "insert", la, la, si, lb
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    53
        elif a[la] < b[lb]:
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    54
            si = la
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    55
            while la < len(a) and a[la] < b[lb]: la += 1
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    56
            yield "delete", si, la, lb, lb
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    57
        else:
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    58
            la += 1
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    59
            lb += 1
318
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    60
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    61
    if lb < len(b):
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    62
        yield "insert", la, la, lb, len(b)
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    63
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    64
    if la < len(a):
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    65
        yield "delete", la, len(a), lb, lb
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    66
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    67
def diff(a, b, sorted=0):
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    68
    bin = []
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    69
    p = [0]
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    70
    for i in a: p.append(p[-1] + len(i))
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    71
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    72
    if sorted:
317
b18ce742566a hg commit: user and date options
mpm@selenic.com
parents: 278
diff changeset
    73
        try:
b18ce742566a hg commit: user and date options
mpm@selenic.com
parents: 278
diff changeset
    74
            d = sortdiff(a, b)
b18ce742566a hg commit: user and date options
mpm@selenic.com
parents: 278
diff changeset
    75
        except:
b18ce742566a hg commit: user and date options
mpm@selenic.com
parents: 278
diff changeset
    76
            print a, b
b18ce742566a hg commit: user and date options
mpm@selenic.com
parents: 278
diff changeset
    77
            raise
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    78
    else:
318
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    79
        d = difflib.SequenceMatcher(None, a, b).get_opcodes()
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    80
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    81
    for o, m, n, s, t in d:
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    82
        if o == 'equal': continue
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    83
        s = "".join(b[s:t])
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    84
        bin.append(struct.pack(">lll", p[m], p[n], len(s)) + s)
2819f63b16bf mdiff: revert grouping optimization for the time being
mpm@selenic.com
parents: 317
diff changeset
    85
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    86
    return "".join(bin)
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    87
120
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    88
def patchtext(bin):
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    89
    pos = 0
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    90
    t = []
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    91
    while pos < len(bin):
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    92
        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
    93
        pos += 12
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    94
        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
    95
        pos += l
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    96
    return "".join(t)
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    97
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    98
def patch(a, bin):
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents: 71
diff changeset
    99
    return patches(a, [bin])