comparison mercurial/diffhelper.py @ 37803:72f6498c040b stable

diffhelper: rename module to avoid conflicts with ancient C module (issue5846) Historically we had had C extensions in mercurial/, which shadows the pure Python modules of the same name forever unless we do clean build/install. I'm sloppy to think about new name, so just dropped the "s".
author Yuya Nishihara <yuya@tcha.org>
date Fri, 20 Apr 2018 20:48:10 +0900
parents mercurial/diffhelpers.py@090c89a8db32
children 86e7a57449fa
comparison
equal deleted inserted replaced
37802:090c89a8db32 37803:72f6498c040b
1 # diffhelper.py - helper routines for patch
2 #
3 # Copyright 2009 Matt Mackall <mpm@selenic.com> and others
4 #
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.
7
8 from __future__ import absolute_import
9
10 from .i18n import _
11
12 from . import (
13 error,
14 )
15
16 def addlines(fp, hunk, lena, lenb, a, b):
17 """Read lines from fp into the hunk
18
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
21 when inserting into a, but not b (for performance while deleting files.)
22 """
23 while True:
24 todoa = lena - len(a)
25 todob = lenb - len(b)
26 num = max(todoa, todob)
27 if num == 0:
28 break
29 for i in xrange(num):
30 s = fp.readline()
31 if not s:
32 raise error.ParseError(_('incomplete hunk'))
33 if s == "\\ No newline at end of file\n":
34 fixnewline(hunk, a, b)
35 continue
36 if s == '\n' or s == '\r\n':
37 # Some patches may be missing the control char
38 # on empty lines. Supply a leading space.
39 s = ' ' + s
40 hunk.append(s)
41 if s.startswith('+'):
42 b.append(s[1:])
43 elif s.startswith('-'):
44 a.append(s)
45 else:
46 b.append(s[1:])
47 a.append(s)
48
49 def fixnewline(hunk, a, b):
50 """Fix up the last lines of a and b when the patch has no newline at EOF"""
51 l = hunk[-1]
52 # tolerate CRLF in last line
53 if l.endswith('\r\n'):
54 hline = l[:-2]
55 else:
56 hline = l[:-1]
57
58 if hline.startswith((' ', '+')):
59 b[-1] = hline[1:]
60 if hline.startswith((' ', '-')):
61 a[-1] = hline
62 hunk[-1] = hline
63
64 def testhunk(a, b, bstart):
65 """Compare the lines in a with the lines in b
66
67 a is assumed to have a control char at the start of each line, this char
68 is ignored in the compare.
69 """
70 alen = len(a)
71 blen = len(b)
72 if alen > blen - bstart or bstart < 0:
73 return False
74 for i in xrange(alen):
75 if a[i][1:] != b[i + bstart]:
76 return False
77 return True