mercurial/mdiff.py
author mpm@selenic.com
Sat, 04 Jun 2005 14:16:32 -0800
changeset 249 619e775aa7f9
parent 241 afe895fcc0d0
child 264 4c1d7072d5cd
permissions -rw-r--r--
import and startup cleanups -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 import and startup cleanups add commands:run() add copyright notice to commands eliminate/reorganize imports to speed up start time: 0.5b: $ time bash -c 'for i in `seq 100`; do ~/bin/hg > /dev/null; done' real 0m7.718s user 0m6.719s sys 0m0.794s new: $ time bash -c 'for i in `seq 100`; do hg > /dev/null; done' real 0m2.171s user 0m1.684s sys 0m0.444s just python: $ time bash -c 'for i in `seq 100`; do python -c pass; done' real 0m0.988s user 0m0.771s sys 0m0.207s Ignoring the fixed cost of loading the Python interpreter, we're 5.6 times faster. With the Python load time, we're still 3.5 times faster. manifest hash: acce5882a55c76eb165316f5741724c8ce4ef587 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) iD8DBQFCoihAywK+sNU5EO8RAqMdAJwMe6Ur0R9G6jjayNa5hH2C3c4k/gCeIYvc N178vaWWGciX9zq+g5qCAls= =buhv -----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 ""
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    13
    a = a.splitlines(1)
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    14
    b = b.splitlines(1)
64
b3e2ddff0159 Diff in subdirectories from Jake Edge
mpm@selenic.com
parents: 35
diff changeset
    15
    l = list(difflib.unified_diff(a, b, "a/" + fn, "b/" + fn, ad, bd))
170
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
    16
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
    17
    for ln in xrange(len(l)):
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
    18
        if l[ln][-1] != '\n':
e6c621a825f2 hg diff: fix missing final newline bug
mpm@selenic.com
parents: 127
diff changeset
    19
            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
    20
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    21
    return "".join(l)
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    22
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    23
def textdiff(a, b):
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    24
    return diff(a.splitlines(1), b.splitlines(1))
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    25
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    26
def sortdiff(a, b):
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    27
    la = lb = 0
184
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    28
    lena = len(a)
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    29
    lenb = len(b)
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    30
    while 1:
184
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    31
        am, bm, = la, lb
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    32
        while lb < lenb and la < len and a[la] == b[lb] :
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    33
            la += 1
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    34
            lb += 1
184
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    35
        if la>am: yield (am, bm, la-am)
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    36
        while lb < lenb and b[lb] < a[la]: lb += 1
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    37
        if lb>=lenb: break
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    38
        while la < lena and b[lb] > a[la]: la += 1
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    39
        if la>=lena: break
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    40
    yield (lena, lenb, 0)
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    41
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    42
def diff(a, b, sorted=0):
184
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    43
    if not a:
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    44
        s = "".join(b)
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    45
        return s and (struct.pack(">lll", 0, 0, len(s)) + s)
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    46
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    47
    bin = []
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    48
    p = [0]
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    49
    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
    50
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    51
    if sorted:
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    52
        d = sortdiff(a, b)
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    53
    else:
184
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    54
        d = difflib.SequenceMatcher(None, a, b).get_matching_blocks()
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    55
    la = 0
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    56
    lb = 0
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    57
    for am, bm, size in d:
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    58
        s = "".join(b[lb:bm])
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    59
        if am > la or s:
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    60
            bin.append(struct.pack(">lll", p[la], p[am], len(s)) + s)
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    61
        la = am + size
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    62
        lb = bm + size
697f05bfe976 Improved binary diff from Christopher Li
mpm@selenic.com
parents: 170
diff changeset
    63
    
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    64
    return "".join(bin)
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    65
120
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    66
def patchtext(bin):
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    67
    pos = 0
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    68
    t = []
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    69
    while pos < len(bin):
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    70
        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
    71
        pos += 12
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    72
        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
    73
        pos += l
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    74
    return "".join(t)
bae6f0328f63 Add a function to return the new text from a binary diff
mpm@selenic.com
parents: 75
diff changeset
    75
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    76
def patch(a, bin):
72
4a6ab4d80dc4 Add an O(m + nlog n) patching extension
mpm@selenic.com
parents: 71
diff changeset
    77
    return patches(a, [bin])