29 if num == 0: |
29 if num == 0: |
30 break |
30 break |
31 for i in pycompat.xrange(num): |
31 for i in pycompat.xrange(num): |
32 s = fp.readline() |
32 s = fp.readline() |
33 if not s: |
33 if not s: |
34 raise error.ParseError(_('incomplete hunk')) |
34 raise error.ParseError(_(b'incomplete hunk')) |
35 if s == "\\ No newline at end of file\n": |
35 if s == b"\\ No newline at end of file\n": |
36 fixnewline(hunk, a, b) |
36 fixnewline(hunk, a, b) |
37 continue |
37 continue |
38 if s == '\n' or s == '\r\n': |
38 if s == b'\n' or s == b'\r\n': |
39 # Some patches may be missing the control char |
39 # Some patches may be missing the control char |
40 # on empty lines. Supply a leading space. |
40 # on empty lines. Supply a leading space. |
41 s = ' ' + s |
41 s = b' ' + s |
42 hunk.append(s) |
42 hunk.append(s) |
43 if s.startswith('+'): |
43 if s.startswith(b'+'): |
44 b.append(s[1:]) |
44 b.append(s[1:]) |
45 elif s.startswith('-'): |
45 elif s.startswith(b'-'): |
46 a.append(s) |
46 a.append(s) |
47 else: |
47 else: |
48 b.append(s[1:]) |
48 b.append(s[1:]) |
49 a.append(s) |
49 a.append(s) |
50 |
50 |
51 |
51 |
52 def fixnewline(hunk, a, b): |
52 def fixnewline(hunk, a, b): |
53 """Fix up the last lines of a and b when the patch has no newline at EOF""" |
53 """Fix up the last lines of a and b when the patch has no newline at EOF""" |
54 l = hunk[-1] |
54 l = hunk[-1] |
55 # tolerate CRLF in last line |
55 # tolerate CRLF in last line |
56 if l.endswith('\r\n'): |
56 if l.endswith(b'\r\n'): |
57 hline = l[:-2] |
57 hline = l[:-2] |
58 else: |
58 else: |
59 hline = l[:-1] |
59 hline = l[:-1] |
60 |
60 |
61 if hline.startswith((' ', '+')): |
61 if hline.startswith((b' ', b'+')): |
62 b[-1] = hline[1:] |
62 b[-1] = hline[1:] |
63 if hline.startswith((' ', '-')): |
63 if hline.startswith((b' ', b'-')): |
64 a[-1] = hline |
64 a[-1] = hline |
65 hunk[-1] = hline |
65 hunk[-1] = hline |
66 |
66 |
67 |
67 |
68 def testhunk(a, b, bstart): |
68 def testhunk(a, b, bstart): |