Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/pure/diffhelpers.py @ 37567:53021c4ef0b2
diffhelpers: port docstrings from cext to pure
I'll remove the C implementation.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Mon, 09 Apr 2018 20:47:43 +0900 |
parents | f53b55b162f4 |
children | c4c8d0d1267f |
rev | line source |
---|---|
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 # |
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
7702
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
10263 | 6 # GNU General Public License version 2 or any later version. |
7702
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
7 |
27336
80214358ac88
diffhelpers: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
12387
diff
changeset
|
8 from __future__ import absolute_import |
80214358ac88
diffhelpers: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
12387
diff
changeset
|
9 |
7702
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
10 def addlines(fp, hunk, lena, lenb, a, b): |
37567
53021c4ef0b2
diffhelpers: port docstrings from cext to pure
Yuya Nishihara <yuya@tcha.org>
parents:
37566
diff
changeset
|
11 """Read lines from fp into the hunk |
53021c4ef0b2
diffhelpers: port docstrings from cext to pure
Yuya Nishihara <yuya@tcha.org>
parents:
37566
diff
changeset
|
12 |
53021c4ef0b2
diffhelpers: port docstrings from cext to pure
Yuya Nishihara <yuya@tcha.org>
parents:
37566
diff
changeset
|
13 The hunk is parsed into two arrays, a and b. a gets the old state of |
53021c4ef0b2
diffhelpers: port docstrings from cext to pure
Yuya Nishihara <yuya@tcha.org>
parents:
37566
diff
changeset
|
14 the text, b gets the new state. The control char from the hunk is saved |
53021c4ef0b2
diffhelpers: port docstrings from cext to pure
Yuya Nishihara <yuya@tcha.org>
parents:
37566
diff
changeset
|
15 when inserting into a, but not b (for performance while deleting files.) |
53021c4ef0b2
diffhelpers: port docstrings from cext to pure
Yuya Nishihara <yuya@tcha.org>
parents:
37566
diff
changeset
|
16 """ |
7702
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
17 while True: |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
18 todoa = lena - len(a) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
19 todob = lenb - len(b) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
20 num = max(todoa, todob) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
21 if num == 0: |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
22 break |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
23 for i in xrange(num): |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
24 s = fp.readline() |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
25 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
|
26 fix_newline(hunk, a, b) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
27 continue |
37566
f53b55b162f4
py3: get rid of character access from pure.diffhelpers
Yuya Nishihara <yuya@tcha.org>
parents:
27336
diff
changeset
|
28 if s == "\n": |
7702
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
29 # Some patches may be missing the control char |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
30 # on empty lines. Supply a leading space. |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
31 s = " \n" |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
32 hunk.append(s) |
37566
f53b55b162f4
py3: get rid of character access from pure.diffhelpers
Yuya Nishihara <yuya@tcha.org>
parents:
27336
diff
changeset
|
33 if s.startswith('+'): |
7702
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
34 b.append(s[1:]) |
37566
f53b55b162f4
py3: get rid of character access from pure.diffhelpers
Yuya Nishihara <yuya@tcha.org>
parents:
27336
diff
changeset
|
35 elif s.startswith('-'): |
7702
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
36 a.append(s) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
37 else: |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
38 b.append(s[1:]) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
39 a.append(s) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
40 return 0 |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
41 |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
42 def fix_newline(hunk, a, b): |
37567
53021c4ef0b2
diffhelpers: port docstrings from cext to pure
Yuya Nishihara <yuya@tcha.org>
parents:
37566
diff
changeset
|
43 """Fix up the last lines of a and b when the patch has no newline at EOF""" |
7702
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
44 l = hunk[-1] |
10551
f61dced1367a
fix test-mq-eol under --pure (mimic diffhelper.c behaviour)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10263
diff
changeset
|
45 # tolerate CRLF in last line |
f61dced1367a
fix test-mq-eol under --pure (mimic diffhelper.c behaviour)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10263
diff
changeset
|
46 if l.endswith('\r\n'): |
f61dced1367a
fix test-mq-eol under --pure (mimic diffhelper.c behaviour)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10263
diff
changeset
|
47 hline = l[:-2] |
f61dced1367a
fix test-mq-eol under --pure (mimic diffhelper.c behaviour)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10263
diff
changeset
|
48 else: |
f61dced1367a
fix test-mq-eol under --pure (mimic diffhelper.c behaviour)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10263
diff
changeset
|
49 hline = l[:-1] |
7702
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
50 |
37566
f53b55b162f4
py3: get rid of character access from pure.diffhelpers
Yuya Nishihara <yuya@tcha.org>
parents:
27336
diff
changeset
|
51 if hline.startswith((' ', '+')): |
10551
f61dced1367a
fix test-mq-eol under --pure (mimic diffhelper.c behaviour)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10263
diff
changeset
|
52 b[-1] = hline[1:] |
37566
f53b55b162f4
py3: get rid of character access from pure.diffhelpers
Yuya Nishihara <yuya@tcha.org>
parents:
27336
diff
changeset
|
53 if hline.startswith((' ', '-')): |
7702
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
54 a[-1] = hline |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
55 hunk[-1] = hline |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
56 return 0 |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
57 |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
58 |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
59 def testhunk(a, b, bstart): |
37567
53021c4ef0b2
diffhelpers: port docstrings from cext to pure
Yuya Nishihara <yuya@tcha.org>
parents:
37566
diff
changeset
|
60 """Compare the lines in a with the lines in b |
53021c4ef0b2
diffhelpers: port docstrings from cext to pure
Yuya Nishihara <yuya@tcha.org>
parents:
37566
diff
changeset
|
61 |
53021c4ef0b2
diffhelpers: port docstrings from cext to pure
Yuya Nishihara <yuya@tcha.org>
parents:
37566
diff
changeset
|
62 a is assumed to have a control char at the start of each line, this char |
53021c4ef0b2
diffhelpers: port docstrings from cext to pure
Yuya Nishihara <yuya@tcha.org>
parents:
37566
diff
changeset
|
63 is ignored in the compare. |
53021c4ef0b2
diffhelpers: port docstrings from cext to pure
Yuya Nishihara <yuya@tcha.org>
parents:
37566
diff
changeset
|
64 """ |
7702
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
65 alen = len(a) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
66 blen = len(b) |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
67 if alen > blen - bstart: |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
68 return -1 |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
69 for i in xrange(alen): |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
70 if a[i][1:] != b[i + bstart]: |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
71 return -1 |
f6bb40554e34
pure Python implementation of diffhelpers.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
72 return 0 |