Show More
@@ -1,77 +1,77 b'' | |||||
1 | # diffhelpers.py - helper routines for patch |
|
1 | # diffhelpers.py - helper routines for patch | |
2 | # |
|
2 | # | |
3 | # Copyright 2009 Matt Mackall <mpm@selenic.com> and others |
|
3 | # Copyright 2009 Matt Mackall <mpm@selenic.com> and others | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | from __future__ import absolute_import |
|
8 | from __future__ import absolute_import | |
9 |
|
9 | |||
10 | from .i18n import _ |
|
10 | from .i18n import _ | |
11 |
|
11 | |||
12 | from . import ( |
|
12 | from . import ( | |
13 | error, |
|
13 | error, | |
14 | ) |
|
14 | ) | |
15 |
|
15 | |||
16 | def addlines(fp, hunk, lena, lenb, a, b): |
|
16 | def addlines(fp, hunk, lena, lenb, a, b): | |
17 | """Read lines from fp into the hunk |
|
17 | """Read lines from fp into the hunk | |
18 |
|
18 | |||
19 | The hunk is parsed into two arrays, a and b. a gets the old state of |
|
19 | The hunk is parsed into two arrays, a and b. a gets the old state of | |
20 | the text, b gets the new state. The control char from the hunk is saved |
|
20 | the text, b gets the new state. The control char from the hunk is saved | |
21 | when inserting into a, but not b (for performance while deleting files.) |
|
21 | when inserting into a, but not b (for performance while deleting files.) | |
22 | """ |
|
22 | """ | |
23 | while True: |
|
23 | while True: | |
24 | todoa = lena - len(a) |
|
24 | todoa = lena - len(a) | |
25 | todob = lenb - len(b) |
|
25 | todob = lenb - len(b) | |
26 | num = max(todoa, todob) |
|
26 | num = max(todoa, todob) | |
27 | if num == 0: |
|
27 | if num == 0: | |
28 | break |
|
28 | break | |
29 | for i in xrange(num): |
|
29 | for i in xrange(num): | |
30 | s = fp.readline() |
|
30 | s = fp.readline() | |
31 | if not s: |
|
31 | if not s: | |
32 | raise error.ParseError(_('incomplete hunk')) |
|
32 | raise error.ParseError(_('incomplete hunk')) | |
33 | if s == "\\ No newline at end of file\n": |
|
33 | if s == "\\ No newline at end of file\n": | |
34 | fixnewline(hunk, a, b) |
|
34 | fixnewline(hunk, a, b) | |
35 | continue |
|
35 | continue | |
36 | if s == '\n' or s == '\r\n': |
|
36 | if s == '\n' or s == '\r\n': | |
37 | # Some patches may be missing the control char |
|
37 | # Some patches may be missing the control char | |
38 | # on empty lines. Supply a leading space. |
|
38 | # on empty lines. Supply a leading space. | |
39 | s = ' ' + s |
|
39 | s = ' ' + s | |
40 | hunk.append(s) |
|
40 | hunk.append(s) | |
41 | if s.startswith('+'): |
|
41 | if s.startswith('+'): | |
42 | b.append(s[1:]) |
|
42 | b.append(s[1:]) | |
43 | elif s.startswith('-'): |
|
43 | elif s.startswith('-'): | |
44 | a.append(s) |
|
44 | a.append(s) | |
45 | else: |
|
45 | else: | |
46 | b.append(s[1:]) |
|
46 | b.append(s[1:]) | |
47 | a.append(s) |
|
47 | a.append(s) | |
48 |
|
48 | |||
49 | def fixnewline(hunk, a, b): |
|
49 | def fixnewline(hunk, a, b): | |
50 | """Fix up the last lines of a and b when the patch has no newline at EOF""" |
|
50 | """Fix up the last lines of a and b when the patch has no newline at EOF""" | |
51 | l = hunk[-1] |
|
51 | l = hunk[-1] | |
52 | # tolerate CRLF in last line |
|
52 | # tolerate CRLF in last line | |
53 | if l.endswith('\r\n'): |
|
53 | if l.endswith('\r\n'): | |
54 | hline = l[:-2] |
|
54 | hline = l[:-2] | |
55 | else: |
|
55 | else: | |
56 | hline = l[:-1] |
|
56 | hline = l[:-1] | |
57 |
|
57 | |||
58 | if hline.startswith((' ', '+')): |
|
58 | if hline.startswith((' ', '+')): | |
59 | b[-1] = hline[1:] |
|
59 | b[-1] = hline[1:] | |
60 | if hline.startswith((' ', '-')): |
|
60 | if hline.startswith((' ', '-')): | |
61 | a[-1] = hline |
|
61 | a[-1] = hline | |
62 | hunk[-1] = hline |
|
62 | hunk[-1] = hline | |
63 |
|
63 | |||
64 | def testhunk(a, b, bstart): |
|
64 | def testhunk(a, b, bstart): | |
65 | """Compare the lines in a with the lines in b |
|
65 | """Compare the lines in a with the lines in b | |
66 |
|
66 | |||
67 | a is assumed to have a control char at the start of each line, this char |
|
67 | a is assumed to have a control char at the start of each line, this char | |
68 | is ignored in the compare. |
|
68 | is ignored in the compare. | |
69 | """ |
|
69 | """ | |
70 | alen = len(a) |
|
70 | alen = len(a) | |
71 | blen = len(b) |
|
71 | blen = len(b) | |
72 | if alen > blen - bstart: |
|
72 | if alen > blen - bstart or bstart < 0: | |
73 | return False |
|
73 | return False | |
74 | for i in xrange(alen): |
|
74 | for i in xrange(alen): | |
75 | if a[i][1:] != b[i + bstart]: |
|
75 | if a[i][1:] != b[i + bstart]: | |
76 | return False |
|
76 | return False | |
77 | return True |
|
77 | return True |
General Comments 0
You need to be logged in to leave comments.
Login now