/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2005-2010 Aaron Bentley, Canonical Ltd
0.5.93 by Aaron Bentley
Added patches.py
2
# <aaron.bentley@utoronto.ca>
3
#
2052.3.1 by John Arbash Meinel
Add tests to cleanup the copyright of all source files
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6289.2.1 by Jelmer Vernooij
Move the primary definition of the patches exceptions to bzrlib.errors.
17
6379.6.3 by Jelmer Vernooij
Use absolute_import.
18
from __future__ import absolute_import
19
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
20
from .errors import (
6729.5.1 by Jelmer Vernooij
Move patches errors to breezy.patches.
21
    BzrError,
6289.2.1 by Jelmer Vernooij
Move the primary definition of the patches exceptions to bzrlib.errors.
22
    )
23
4634.80.1 by Aaron Bentley
Parse binary files.
24
import re
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
25
import sys
26
27
28
binary_files_re = b'Binary files (.*) and (.*) differ\n'
4634.98.1 by Aaron Bentley
Improve patch binary section handling.
29
6601.1.6 by Kit Randel
change of plan, don't track modified state, just preserve dirty_heads if requested in parse_patches
30
6729.5.1 by Jelmer Vernooij
Move patches errors to breezy.patches.
31
class PatchSyntax(BzrError):
32
    """Base class for patch syntax errors."""
33
34
35
class BinaryFiles(BzrError):
36
37
    _fmt = 'Binary files section encountered.'
38
39
    def __init__(self, orig_name, mod_name):
40
        self.orig_name = orig_name
41
        self.mod_name = mod_name
42
43
44
class MalformedPatchHeader(PatchSyntax):
45
46
    _fmt = "Malformed patch header.  %(desc)s\n%(line)r"
47
48
    def __init__(self, desc, line):
49
        self.desc = desc
50
        self.line = line
51
52
53
class MalformedLine(PatchSyntax):
54
55
    _fmt = "Malformed line.  %(desc)s\n%(line)r"
56
57
    def __init__(self, desc, line):
58
        self.desc = desc
59
        self.line = line
60
61
62
class PatchConflict(BzrError):
63
64
    _fmt = ('Text contents mismatch at line %(line_no)d.  Original has '
65
            '"%(orig_line)s", but patch says it should be "%(patch_line)s"')
66
67
    def __init__(self, line_no, orig_line, patch_line):
68
        self.line_no = line_no
69
        self.orig_line = orig_line.rstrip('\n')
70
        self.patch_line = patch_line.rstrip('\n')
71
72
73
class MalformedHunkHeader(PatchSyntax):
74
75
    _fmt = "Malformed hunk header.  %(desc)s\n%(line)r"
76
77
    def __init__(self, desc, line):
78
        self.desc = desc
79
        self.line = line
80
81
0.5.93 by Aaron Bentley
Added patches.py
82
def get_patch_names(iter_lines):
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
83
    line = next(iter_lines)
0.5.93 by Aaron Bentley
Added patches.py
84
    try:
4634.98.1 by Aaron Bentley
Improve patch binary section handling.
85
        match = re.match(binary_files_re, line)
4634.80.1 by Aaron Bentley
Parse binary files.
86
        if match is not None:
87
            raise BinaryFiles(match.group(1), match.group(2))
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
88
        if not line.startswith(b"--- "):
0.5.93 by Aaron Bentley
Added patches.py
89
            raise MalformedPatchHeader("No orig name", line)
90
        else:
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
91
            orig_name = line[4:].rstrip(b"\n")
0.5.93 by Aaron Bentley
Added patches.py
92
    except StopIteration:
93
        raise MalformedPatchHeader("No orig line", "")
94
    try:
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
95
        line = next(iter_lines)
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
96
        if not line.startswith(b"+++ "):
0.5.93 by Aaron Bentley
Added patches.py
97
            raise PatchSyntax("No mod name")
98
        else:
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
99
            mod_name = line[4:].rstrip(b"\n")
0.5.93 by Aaron Bentley
Added patches.py
100
    except StopIteration:
101
        raise MalformedPatchHeader("No mod line", "")
6601.1.6 by Kit Randel
change of plan, don't track modified state, just preserve dirty_heads if requested in parse_patches
102
    return (orig_name, mod_name)
0.5.93 by Aaron Bentley
Added patches.py
103
1185.82.123 by Aaron Bentley
Cleanups to prepare for review
104
0.5.93 by Aaron Bentley
Added patches.py
105
def parse_range(textrange):
106
    """Parse a patch range, handling the "1" special-case
107
108
    :param textrange: The text to parse
109
    :type textrange: str
110
    :return: the position and range, as a tuple
111
    :rtype: (int, int)
112
    """
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
113
    tmp = textrange.split(b',')
0.5.93 by Aaron Bentley
Added patches.py
114
    if len(tmp) == 1:
115
        pos = tmp[0]
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
116
        range = b"1"
0.5.93 by Aaron Bentley
Added patches.py
117
    else:
118
        (pos, range) = tmp
119
    pos = int(pos)
120
    range = int(range)
121
    return (pos, range)
122
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
123
0.5.93 by Aaron Bentley
Added patches.py
124
def hunk_from_header(line):
3224.5.1 by Andrew Bennetts
Lots of assorted hackery to reduce the number of imports for common operations. Improves 'rocks', 'st' and 'help' times by ~50ms on my laptop.
125
    import re
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
126
    matches = re.match(br'\@\@ ([^@]*) \@\@( (.*))?\n', line)
1551.18.6 by Aaron Bentley
Add support for diff -p-style diffs to patch parser
127
    if matches is None:
128
        raise MalformedHunkHeader("Does not match format.", line)
0.5.93 by Aaron Bentley
Added patches.py
129
    try:
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
130
        (orig, mod) = matches.group(1).split(b" ")
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
131
    except (ValueError, IndexError) as e:
0.5.93 by Aaron Bentley
Added patches.py
132
        raise MalformedHunkHeader(str(e), line)
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
133
    if not orig.startswith(b'-') or not mod.startswith(b'+'):
0.5.93 by Aaron Bentley
Added patches.py
134
        raise MalformedHunkHeader("Positions don't start with + or -.", line)
135
    try:
136
        (orig_pos, orig_range) = parse_range(orig[1:])
137
        (mod_pos, mod_range) = parse_range(mod[1:])
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
138
    except (ValueError, IndexError) as e:
0.5.93 by Aaron Bentley
Added patches.py
139
        raise MalformedHunkHeader(str(e), line)
140
    if mod_range < 0 or orig_range < 0:
141
        raise MalformedHunkHeader("Hunk range is negative", line)
1551.18.6 by Aaron Bentley
Add support for diff -p-style diffs to patch parser
142
    tail = matches.group(3)
143
    return Hunk(orig_pos, orig_range, mod_pos, mod_range, tail)
0.5.93 by Aaron Bentley
Added patches.py
144
145
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
146
class HunkLine(object):
147
0.5.93 by Aaron Bentley
Added patches.py
148
    def __init__(self, contents):
149
        self.contents = contents
150
151
    def get_str(self, leadchar):
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
152
        if self.contents == b"\n" and leadchar == b" " and False:
153
            return b"\n"
154
        if not self.contents.endswith(b'\n'):
155
            terminator = b'\n' + NO_NL
0.5.93 by Aaron Bentley
Added patches.py
156
        else:
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
157
            terminator = b''
0.5.93 by Aaron Bentley
Added patches.py
158
        return leadchar + self.contents + terminator
159
7029.1.3 by Jelmer Vernooij
Use separate .as_bytes method rather than __bytes__.
160
    def as_bytes(self):
161
        raise NotImplementedError
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
162
0.5.93 by Aaron Bentley
Added patches.py
163
164
class ContextLine(HunkLine):
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
165
0.5.93 by Aaron Bentley
Added patches.py
166
    def __init__(self, contents):
167
        HunkLine.__init__(self, contents)
168
7029.1.3 by Jelmer Vernooij
Use separate .as_bytes method rather than __bytes__.
169
    def as_bytes(self):
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
170
        return self.get_str(b" ")
0.5.93 by Aaron Bentley
Added patches.py
171
172
173
class InsertLine(HunkLine):
174
    def __init__(self, contents):
175
        HunkLine.__init__(self, contents)
176
7029.1.3 by Jelmer Vernooij
Use separate .as_bytes method rather than __bytes__.
177
    def as_bytes(self):
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
178
        return self.get_str(b"+")
0.5.93 by Aaron Bentley
Added patches.py
179
180
181
class RemoveLine(HunkLine):
182
    def __init__(self, contents):
183
        HunkLine.__init__(self, contents)
184
7029.1.3 by Jelmer Vernooij
Use separate .as_bytes method rather than __bytes__.
185
    def as_bytes(self):
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
186
        return self.get_str(b"-")
0.5.93 by Aaron Bentley
Added patches.py
187
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
188
NO_NL = b'\\ No newline at end of file\n'
0.5.93 by Aaron Bentley
Added patches.py
189
__pychecker__="no-returnvalues"
190
191
def parse_line(line):
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
192
    if line.startswith(b"\n"):
0.5.93 by Aaron Bentley
Added patches.py
193
        return ContextLine(line)
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
194
    elif line.startswith(b" "):
0.5.93 by Aaron Bentley
Added patches.py
195
        return ContextLine(line[1:])
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
196
    elif line.startswith(b"+"):
0.5.93 by Aaron Bentley
Added patches.py
197
        return InsertLine(line[1:])
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
198
    elif line.startswith(b"-"):
0.5.93 by Aaron Bentley
Added patches.py
199
        return RemoveLine(line[1:])
200
    else:
201
        raise MalformedLine("Unknown line type", line)
202
__pychecker__=""
203
204
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
205
class Hunk(object):
206
1551.18.6 by Aaron Bentley
Add support for diff -p-style diffs to patch parser
207
    def __init__(self, orig_pos, orig_range, mod_pos, mod_range, tail=None):
0.5.93 by Aaron Bentley
Added patches.py
208
        self.orig_pos = orig_pos
209
        self.orig_range = orig_range
210
        self.mod_pos = mod_pos
211
        self.mod_range = mod_range
1551.18.6 by Aaron Bentley
Add support for diff -p-style diffs to patch parser
212
        self.tail = tail
0.5.93 by Aaron Bentley
Added patches.py
213
        self.lines = []
214
215
    def get_header(self):
1551.18.6 by Aaron Bentley
Add support for diff -p-style diffs to patch parser
216
        if self.tail is None:
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
217
            tail_str = b''
1551.18.6 by Aaron Bentley
Add support for diff -p-style diffs to patch parser
218
        else:
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
219
            tail_str = b' ' + self.tail
220
        return b"@@ -%s +%s @@%s\n" % (self.range_str(self.orig_pos,
1551.18.6 by Aaron Bentley
Add support for diff -p-style diffs to patch parser
221
                                                     self.orig_range),
222
                                      self.range_str(self.mod_pos,
223
                                                     self.mod_range),
224
                                      tail_str)
0.5.93 by Aaron Bentley
Added patches.py
225
226
    def range_str(self, pos, range):
227
        """Return a file range, special-casing for 1-line files.
228
229
        :param pos: The position in the file
230
        :type pos: int
231
        :range: The range in the file
232
        :type range: int
233
        :return: a string in the format 1,4 except when range == pos == 1
234
        """
235
        if range == 1:
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
236
            return b"%i" % pos
0.5.93 by Aaron Bentley
Added patches.py
237
        else:
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
238
            return b"%i,%i" % (pos, range)
0.5.93 by Aaron Bentley
Added patches.py
239
7029.1.3 by Jelmer Vernooij
Use separate .as_bytes method rather than __bytes__.
240
    def as_bytes(self):
0.5.93 by Aaron Bentley
Added patches.py
241
        lines = [self.get_header()]
242
        for line in self.lines:
7029.1.3 by Jelmer Vernooij
Use separate .as_bytes method rather than __bytes__.
243
            lines.append(line.as_bytes())
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
244
        return b"".join(lines)
245
0.5.93 by Aaron Bentley
Added patches.py
246
    def shift_to_mod(self, pos):
247
        if pos < self.orig_pos-1:
248
            return 0
249
        elif pos > self.orig_pos+self.orig_range:
250
            return self.mod_range - self.orig_range
251
        else:
252
            return self.shift_to_mod_lines(pos)
253
254
    def shift_to_mod_lines(self, pos):
255
        position = self.orig_pos-1
256
        shift = 0
257
        for line in self.lines:
258
            if isinstance(line, InsertLine):
259
                shift += 1
260
            elif isinstance(line, RemoveLine):
261
                if position == pos:
262
                    return None
263
                shift -= 1
264
                position += 1
265
            elif isinstance(line, ContextLine):
266
                position += 1
267
            if position > pos:
268
                break
269
        return shift
270
1185.82.123 by Aaron Bentley
Cleanups to prepare for review
271
5016.3.1 by Toshio Kuratomi
iAdd an allow_dirty parameter that allows patch files with non-patch data to be used.
272
def iter_hunks(iter_lines, allow_dirty=False):
273
    '''
274
    :arg iter_lines: iterable of lines to parse for hunks
275
    :kwarg allow_dirty: If True, when we encounter something that is not
276
        a hunk header when we're looking for one, assume the rest of the lines
277
        are not part of the patch (comments or other junk).  Default False
278
    '''
0.5.93 by Aaron Bentley
Added patches.py
279
    hunk = None
280
    for line in iter_lines:
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
281
        if line == b"\n":
0.5.93 by Aaron Bentley
Added patches.py
282
            if hunk is not None:
283
                yield hunk
284
                hunk = None
285
            continue
286
        if hunk is not None:
287
            yield hunk
5016.3.1 by Toshio Kuratomi
iAdd an allow_dirty parameter that allows patch files with non-patch data to be used.
288
        try:
289
            hunk = hunk_from_header(line)
290
        except MalformedHunkHeader:
291
            if allow_dirty:
292
                # If the line isn't a hunk header, then we've reached the end
293
                # of this patch and there's "junk" at the end.  Ignore the
294
                # rest of this patch.
295
                return
296
            raise
0.5.93 by Aaron Bentley
Added patches.py
297
        orig_size = 0
298
        mod_size = 0
299
        while orig_size < hunk.orig_range or mod_size < hunk.mod_range:
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
300
            hunk_line = parse_line(next(iter_lines))
0.5.96 by Aaron Bentley
Cleaned up handling of files with no terminating \n
301
            hunk.lines.append(hunk_line)
0.5.93 by Aaron Bentley
Added patches.py
302
            if isinstance(hunk_line, (RemoveLine, ContextLine)):
303
                orig_size += 1
304
            if isinstance(hunk_line, (InsertLine, ContextLine)):
305
                mod_size += 1
306
    if hunk is not None:
307
        yield hunk
308
1185.82.123 by Aaron Bentley
Cleanups to prepare for review
309
4634.80.1 by Aaron Bentley
Parse binary files.
310
class BinaryPatch(object):
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
311
6601.1.6 by Kit Randel
change of plan, don't track modified state, just preserve dirty_heads if requested in parse_patches
312
    def __init__(self, oldname, newname):
0.5.93 by Aaron Bentley
Added patches.py
313
        self.oldname = oldname
314
        self.newname = newname
4634.80.1 by Aaron Bentley
Parse binary files.
315
7029.1.3 by Jelmer Vernooij
Use separate .as_bytes method rather than __bytes__.
316
    def as_bytes(self):
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
317
        return b'Binary files %s and %s differ\n' % (self.oldname, self.newname)
318
4634.80.1 by Aaron Bentley
Parse binary files.
319
320
class Patch(BinaryPatch):
321
6601.1.6 by Kit Randel
change of plan, don't track modified state, just preserve dirty_heads if requested in parse_patches
322
    def __init__(self, oldname, newname):
323
        BinaryPatch.__init__(self, oldname, newname)
0.5.93 by Aaron Bentley
Added patches.py
324
        self.hunks = []
325
7029.1.3 by Jelmer Vernooij
Use separate .as_bytes method rather than __bytes__.
326
    def as_bytes(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
327
        ret = self.get_header()
7029.1.3 by Jelmer Vernooij
Use separate .as_bytes method rather than __bytes__.
328
        ret += b"".join([h.as_bytes() for h in self.hunks])
0.5.93 by Aaron Bentley
Added patches.py
329
        return ret
330
0.5.95 by Aaron Bentley
Updated patch to match bzrtools
331
    def get_header(self):
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
332
        return b"--- %s\n+++ %s\n" % (self.oldname, self.newname)
0.5.95 by Aaron Bentley
Updated patch to match bzrtools
333
3946.4.1 by Tim Penhey
Extract out the counting of the stats values.
334
    def stats_values(self):
335
        """Calculate the number of inserts and removes."""
0.5.93 by Aaron Bentley
Added patches.py
336
        removes = 0
337
        inserts = 0
338
        for hunk in self.hunks:
339
            for line in hunk.lines:
340
                if isinstance(line, InsertLine):
341
                     inserts+=1;
342
                elif isinstance(line, RemoveLine):
343
                     removes+=1;
3946.4.1 by Tim Penhey
Extract out the counting of the stats values.
344
        return (inserts, removes, len(self.hunks))
345
346
    def stats_str(self):
347
        """Return a string of patch statistics"""
0.5.93 by Aaron Bentley
Added patches.py
348
        return "%i inserts, %i removes in %i hunks" % \
3946.4.1 by Tim Penhey
Extract out the counting of the stats values.
349
            self.stats_values()
0.5.93 by Aaron Bentley
Added patches.py
350
351
    def pos_in_mod(self, position):
352
        newpos = position
353
        for hunk in self.hunks:
354
            shift = hunk.shift_to_mod(position)
355
            if shift is None:
356
                return None
357
            newpos += shift
358
        return newpos
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
359
0.5.93 by Aaron Bentley
Added patches.py
360
    def iter_inserted(self):
361
        """Iteraties through inserted lines
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
362
0.5.93 by Aaron Bentley
Added patches.py
363
        :return: Pair of line number, line
364
        :rtype: iterator of (int, InsertLine)
365
        """
366
        for hunk in self.hunks:
367
            pos = hunk.mod_pos - 1;
368
            for line in hunk.lines:
369
                if isinstance(line, InsertLine):
370
                    yield (pos, line)
371
                    pos += 1
372
                if isinstance(line, ContextLine):
373
                    pos += 1
374
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
375
5016.3.1 by Toshio Kuratomi
iAdd an allow_dirty parameter that allows patch files with non-patch data to be used.
376
def parse_patch(iter_lines, allow_dirty=False):
377
    '''
378
    :arg iter_lines: iterable of lines to parse
379
    :kwarg allow_dirty: If True, allow the patch to have trailing junk.
380
        Default False
381
    '''
3873.1.8 by Benoît Pierre
Fix regressions in other parts of the testsuite.
382
    iter_lines = iter_lines_handle_nl(iter_lines)
4634.80.1 by Aaron Bentley
Parse binary files.
383
    try:
6601.1.6 by Kit Randel
change of plan, don't track modified state, just preserve dirty_heads if requested in parse_patches
384
        (orig_name, mod_name) = get_patch_names(iter_lines)
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
385
    except BinaryFiles as e:
4634.80.1 by Aaron Bentley
Parse binary files.
386
        return BinaryPatch(e.orig_name, e.mod_name)
387
    else:
6601.1.6 by Kit Randel
change of plan, don't track modified state, just preserve dirty_heads if requested in parse_patches
388
        patch = Patch(orig_name, mod_name)
5016.3.1 by Toshio Kuratomi
iAdd an allow_dirty parameter that allows patch files with non-patch data to be used.
389
        for hunk in iter_hunks(iter_lines, allow_dirty):
4634.80.1 by Aaron Bentley
Parse binary files.
390
            patch.hunks.append(hunk)
391
        return patch
0.5.93 by Aaron Bentley
Added patches.py
392
393
6601.1.6 by Kit Randel
change of plan, don't track modified state, just preserve dirty_heads if requested in parse_patches
394
def iter_file_patch(iter_lines, allow_dirty=False, keep_dirty=False):
5016.3.1 by Toshio Kuratomi
iAdd an allow_dirty parameter that allows patch files with non-patch data to be used.
395
    '''
396
    :arg iter_lines: iterable of lines to parse for patches
397
    :kwarg allow_dirty: If True, allow comments and other non-patch text
398
        before the first patch.  Note that the algorithm here can only find
399
        such text before any patches have been found.  Comments after the
400
        first patch are stripped away in iter_hunks() if it is also passed
401
        allow_dirty=True.  Default False.
402
    '''
403
    ### FIXME: Docstring is not quite true.  We allow certain comments no
404
    # matter what, If they startwith '===', '***', or '#' Someone should
405
    # reexamine this logic and decide if we should include those in
406
    # allow_dirty or restrict those to only being before the patch is found
407
    # (as allow_dirty does).
4634.98.1 by Aaron Bentley
Improve patch binary section handling.
408
    regex = re.compile(binary_files_re)
0.5.93 by Aaron Bentley
Added patches.py
409
    saved_lines = []
6601.1.6 by Kit Randel
change of plan, don't track modified state, just preserve dirty_heads if requested in parse_patches
410
    dirty_head = []
2298.6.1 by Johan Dahlberg
Fix bzrtools shelve command for removed lines beginning with "--"
411
    orig_range = 0
5016.3.1 by Toshio Kuratomi
iAdd an allow_dirty parameter that allows patch files with non-patch data to be used.
412
    beginning = True
6601.1.7 by Kit Randel
fixed dirty_head logic in iter_file_patch
413
0.5.93 by Aaron Bentley
Added patches.py
414
    for line in iter_lines:
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
415
        if line.startswith(b'=== '):
6603.2.1 by Colin Watson
Avoid associating dirty patch headers with the previous file in the patch.
416
            if len(saved_lines) > 0:
417
                if keep_dirty and len(dirty_head) > 0:
418
                    yield {'saved_lines': saved_lines,
419
                           'dirty_head': dirty_head}
420
                    dirty_head = []
421
                else:
422
                    yield saved_lines
423
                saved_lines = []
6601.1.6 by Kit Randel
change of plan, don't track modified state, just preserve dirty_heads if requested in parse_patches
424
            dirty_head.append(line)
425
            continue
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
426
        if line.startswith(b'*** '):
0.5.93 by Aaron Bentley
Added patches.py
427
            continue
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
428
        if line.startswith(b'#'):
1770.1.1 by Aaron Bentley
Ignore lines that start with '#' in patch parser
429
            continue
2298.6.1 by Johan Dahlberg
Fix bzrtools shelve command for removed lines beginning with "--"
430
        elif orig_range > 0:
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
431
            if line.startswith(b'-') or line.startswith(b' '):
2298.6.1 by Johan Dahlberg
Fix bzrtools shelve command for removed lines beginning with "--"
432
                orig_range -= 1
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
433
        elif line.startswith(b'--- ') or regex.match(line):
5016.3.1 by Toshio Kuratomi
iAdd an allow_dirty parameter that allows patch files with non-patch data to be used.
434
            if allow_dirty and beginning:
435
                # Patches can have "junk" at the beginning
436
                # Stripping junk from the end of patches is handled when we
437
                # parse the patch
438
                beginning = False
439
            elif len(saved_lines) > 0:
6601.1.7 by Kit Randel
fixed dirty_head logic in iter_file_patch
440
                if keep_dirty and len(dirty_head) > 0:
441
                    yield {'saved_lines': saved_lines,
442
                           'dirty_head': dirty_head}
443
                    dirty_head = []
444
                else:
445
                    yield saved_lines
6601.1.6 by Kit Randel
change of plan, don't track modified state, just preserve dirty_heads if requested in parse_patches
446
            saved_lines = []
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
447
        elif line.startswith(b'@@'):
2298.6.1 by Johan Dahlberg
Fix bzrtools shelve command for removed lines beginning with "--"
448
            hunk = hunk_from_header(line)
449
            orig_range = hunk.orig_range
0.5.93 by Aaron Bentley
Added patches.py
450
        saved_lines.append(line)
451
    if len(saved_lines) > 0:
6601.1.7 by Kit Randel
fixed dirty_head logic in iter_file_patch
452
        if keep_dirty and len(dirty_head) > 0:
453
            yield {'saved_lines': saved_lines,
454
                   'dirty_head': dirty_head}
6601.1.6 by Kit Randel
change of plan, don't track modified state, just preserve dirty_heads if requested in parse_patches
455
        else:
456
            yield saved_lines
0.5.93 by Aaron Bentley
Added patches.py
457
458
3873.1.6 by Benoît Pierre
OK, so now patches should handle '\ No newline at end of file' in both
459
def iter_lines_handle_nl(iter_lines):
460
    """
461
    Iterates through lines, ensuring that lines that originally had no
462
    terminating \n are produced without one.  This transformation may be
463
    applied at any point up until hunk line parsing, and is safe to apply
464
    repeatedly.
465
    """
466
    last_line = None
467
    for line in iter_lines:
468
        if line == NO_NL:
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
469
            if not last_line.endswith(b'\n'):
3873.1.6 by Benoît Pierre
OK, so now patches should handle '\ No newline at end of file' in both
470
                raise AssertionError()
471
            last_line = last_line[:-1]
472
            line = None
473
        if last_line is not None:
474
            yield last_line
475
        last_line = line
476
    if last_line is not None:
477
        yield last_line
478
479
6601.1.6 by Kit Randel
change of plan, don't track modified state, just preserve dirty_heads if requested in parse_patches
480
def parse_patches(iter_lines, allow_dirty=False, keep_dirty=False):
5016.3.1 by Toshio Kuratomi
iAdd an allow_dirty parameter that allows patch files with non-patch data to be used.
481
    '''
482
    :arg iter_lines: iterable of lines to parse for patches
483
    :kwarg allow_dirty: If True, allow text that's not part of the patch at
484
        selected places.  This includes comments before and after a patch
485
        for instance.  Default False.
6601.1.6 by Kit Randel
change of plan, don't track modified state, just preserve dirty_heads if requested in parse_patches
486
    :kwarg keep_dirty: If True, returns a dict of patches with dirty headers.
487
        Default False.
5016.3.1 by Toshio Kuratomi
iAdd an allow_dirty parameter that allows patch files with non-patch data to be used.
488
    '''
6601.1.7 by Kit Randel
fixed dirty_head logic in iter_file_patch
489
    for patch_lines in iter_file_patch(iter_lines, allow_dirty, keep_dirty):
490
        if 'dirty_head' in patch_lines:
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
491
            yield ({'patch': parse_patch(patch_lines['saved_lines'], allow_dirty),
492
                    'dirty_head': patch_lines['dirty_head']})
6601.1.7 by Kit Randel
fixed dirty_head logic in iter_file_patch
493
        else:
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
494
            yield parse_patch(patch_lines, allow_dirty)
0.5.93 by Aaron Bentley
Added patches.py
495
496
497
def difference_index(atext, btext):
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
498
    """Find the indext of the first character that differs between two texts
0.5.93 by Aaron Bentley
Added patches.py
499
500
    :param atext: The first text
501
    :type atext: str
502
    :param btext: The second text
503
    :type str: str
504
    :return: The index, or None if there are no differences within the range
505
    :rtype: int or NoneType
506
    """
507
    length = len(atext)
508
    if len(btext) < length:
509
        length = len(btext)
510
    for i in range(length):
511
        if atext[i] != btext[i]:
512
            return i;
513
    return None
514
1185.82.123 by Aaron Bentley
Cleanups to prepare for review
515
0.5.93 by Aaron Bentley
Added patches.py
516
def iter_patched(orig_lines, patch_lines):
517
    """Iterate through a series of lines with a patch applied.
518
    This handles a single file, and does exact, not fuzzy patching.
519
    """
3873.1.8 by Benoît Pierre
Fix regressions in other parts of the testsuite.
520
    patch_lines = iter_lines_handle_nl(iter(patch_lines))
0.5.93 by Aaron Bentley
Added patches.py
521
    get_patch_names(patch_lines)
3363.18.1 by Aaron Bentley
Allow patching directly from parsed hunks
522
    return iter_patched_from_hunks(orig_lines, iter_hunks(patch_lines))
523
3363.18.4 by Aaron Bentley
Updates from review (and a doc update)
524
3363.18.1 by Aaron Bentley
Allow patching directly from parsed hunks
525
def iter_patched_from_hunks(orig_lines, hunks):
3363.18.4 by Aaron Bentley
Updates from review (and a doc update)
526
    """Iterate through a series of lines with a patch applied.
527
    This handles a single file, and does exact, not fuzzy patching.
528
529
    :param orig_lines: The unpatched lines.
530
    :param hunks: An iterable of Hunk instances.
531
    """
3363.18.1 by Aaron Bentley
Allow patching directly from parsed hunks
532
    seen_patch = []
0.5.93 by Aaron Bentley
Added patches.py
533
    line_no = 1
3363.18.1 by Aaron Bentley
Allow patching directly from parsed hunks
534
    if orig_lines is not None:
3363.18.4 by Aaron Bentley
Updates from review (and a doc update)
535
        orig_lines = iter(orig_lines)
3363.18.1 by Aaron Bentley
Allow patching directly from parsed hunks
536
    for hunk in hunks:
0.5.93 by Aaron Bentley
Added patches.py
537
        while line_no < hunk.orig_pos:
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
538
            orig_line = next(orig_lines)
0.5.93 by Aaron Bentley
Added patches.py
539
            yield orig_line
540
            line_no += 1
541
        for hunk_line in hunk.lines:
542
            seen_patch.append(str(hunk_line))
543
            if isinstance(hunk_line, InsertLine):
544
                yield hunk_line.contents
545
            elif isinstance(hunk_line, (ContextLine, RemoveLine)):
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
546
                orig_line = next(orig_lines)
0.5.93 by Aaron Bentley
Added patches.py
547
                if orig_line != hunk_line.contents:
7029.1.1 by Jelmer Vernooij
Port breezy.patches to Python3.
548
                    raise PatchConflict(line_no, orig_line, b"".join(seen_patch))
0.5.93 by Aaron Bentley
Added patches.py
549
                if isinstance(hunk_line, ContextLine):
550
                    yield orig_line
551
                else:
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
552
                    if not isinstance(hunk_line, RemoveLine):
553
                        raise AssertionError(hunk_line)
0.5.93 by Aaron Bentley
Added patches.py
554
                line_no += 1
0.5.105 by John Arbash Meinel
Adding more test patches to the test suite.
555
    if orig_lines is not None:
556
        for line in orig_lines:
557
            yield line