author | Matt Mackall <mpm@selenic.com> |
Mon, 16 Feb 2009 17:37:23 -0600 | |
changeset 7782 | 140429276b63 |
parent 7702 | f6bb40554e34 |
child 8225 | 46293a0c7e9f |
permissions | -rw-r--r-- |
7702
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
1 |
# diffhelpers.py - pure Python implementation of diffhelpers.c |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
2 |
# |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
3 |
# Copyright 2009 Matt Mackall <mpm@selenic.com> and others |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
4 |
# |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
6 |
# of the GNU General Public License, incorporated herein by reference. |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
7 |
|
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
8 |
def addlines(fp, hunk, lena, lenb, a, b): |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
9 |
while True: |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
10 |
todoa = lena - len(a) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
11 |
todob = lenb - len(b) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
12 |
num = max(todoa, todob) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
13 |
if num == 0: |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
14 |
break |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
15 |
for i in xrange(num): |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
16 |
s = fp.readline() |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
17 |
c = s[0] |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
18 |
if s == "\\ No newline at end of file\n": |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
19 |
fix_newline(hunk, a, b) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
20 |
continue |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
21 |
if c == "\n": |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
22 |
# Some patches may be missing the control char |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
23 |
# on empty lines. Supply a leading space. |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
24 |
s = " \n" |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
25 |
hunk.append(s) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
26 |
if c == "+": |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
27 |
b.append(s[1:]) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
28 |
elif c == "-": |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
29 |
a.append(s) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
30 |
else: |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
31 |
b.append(s[1:]) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
32 |
a.append(s) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
33 |
return 0 |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
34 |
|
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
35 |
def fix_newline(hunk, a, b): |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
36 |
l = hunk[-1] |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
37 |
c = l[0] |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
38 |
hline = l[:-1] |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
39 |
|
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
40 |
if c == " " or c == "+": |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
41 |
b[-1] = l[1:-1] |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
42 |
if c == " " or c == "-": |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
43 |
a[-1] = hline |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
44 |
hunk[-1] = hline |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
45 |
return 0 |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
46 |
|
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
47 |
|
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
48 |
def testhunk(a, b, bstart): |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
49 |
alen = len(a) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
50 |
blen = len(b) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
51 |
if alen > blen - bstart: |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
52 |
return -1 |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
53 |
for i in xrange(alen): |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
54 |
if a[i][1:] != b[i + bstart]: |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
55 |
return -1 |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
56 |
return 0 |