Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/diffhelpers.py @ 37571:0ea8b9576d7c
diffhelpers: move out of pure package
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Mon, 09 Apr 2018 20:54:00 +0900 |
parents | mercurial/pure/diffhelpers.py@c4c8d0d1267f |
children | c6b8d614690a |
comparison
equal
deleted
inserted
replaced
37570:c4c8d0d1267f | 37571:0ea8b9576d7c |
---|---|
1 # diffhelpers.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 def addlines(fp, hunk, lena, lenb, a, b): | |
11 """Read lines from fp into the hunk | |
12 | |
13 The hunk is parsed into two arrays, a and b. a gets the old state of | |
14 the text, b gets the new state. The control char from the hunk is saved | |
15 when inserting into a, but not b (for performance while deleting files.) | |
16 """ | |
17 while True: | |
18 todoa = lena - len(a) | |
19 todob = lenb - len(b) | |
20 num = max(todoa, todob) | |
21 if num == 0: | |
22 break | |
23 for i in xrange(num): | |
24 s = fp.readline() | |
25 if s == "\\ No newline at end of file\n": | |
26 fixnewline(hunk, a, b) | |
27 continue | |
28 if s == "\n": | |
29 # Some patches may be missing the control char | |
30 # on empty lines. Supply a leading space. | |
31 s = " \n" | |
32 hunk.append(s) | |
33 if s.startswith('+'): | |
34 b.append(s[1:]) | |
35 elif s.startswith('-'): | |
36 a.append(s) | |
37 else: | |
38 b.append(s[1:]) | |
39 a.append(s) | |
40 return 0 | |
41 | |
42 def fixnewline(hunk, a, b): | |
43 """Fix up the last lines of a and b when the patch has no newline at EOF""" | |
44 l = hunk[-1] | |
45 # tolerate CRLF in last line | |
46 if l.endswith('\r\n'): | |
47 hline = l[:-2] | |
48 else: | |
49 hline = l[:-1] | |
50 | |
51 if hline.startswith((' ', '+')): | |
52 b[-1] = hline[1:] | |
53 if hline.startswith((' ', '-')): | |
54 a[-1] = hline | |
55 hunk[-1] = hline | |
56 return 0 | |
57 | |
58 def testhunk(a, b, bstart): | |
59 """Compare the lines in a with the lines in b | |
60 | |
61 a is assumed to have a control char at the start of each line, this char | |
62 is ignored in the compare. | |
63 """ | |
64 alen = len(a) | |
65 blen = len(b) | |
66 if alen > blen - bstart: | |
67 return -1 | |
68 for i in xrange(alen): | |
69 if a[i][1:] != b[i + bstart]: | |
70 return -1 | |
71 return 0 |