~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-11-06 16:01:48 UTC
  • mfrom: (5527.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20101106160148-8maemz21jbrhpzky
(vila) Move NEWS entry into the correct section (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004 - 2006 Aaron Bentley, Canonical Ltd
 
1
# Copyright (C) 2005-2010 Aaron Bentley, Canonical Ltd
2
2
# <aaron.bentley@utoronto.ca>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
import re
18
18
 
19
19
 
 
20
binary_files_re = 'Binary files (.*) and (.*) differ\n'
 
21
 
 
22
 
 
23
class BinaryFiles(Exception):
 
24
 
 
25
    def __init__(self, orig_name, mod_name):
 
26
        self.orig_name = orig_name
 
27
        self.mod_name = mod_name
 
28
        Exception.__init__(self, 'Binary files section encountered.')
 
29
 
 
30
 
20
31
class PatchSyntax(Exception):
21
32
    def __init__(self, msg):
22
33
        Exception.__init__(self, msg)
58
69
def get_patch_names(iter_lines):
59
70
    try:
60
71
        line = iter_lines.next()
 
72
        match = re.match(binary_files_re, line)
 
73
        if match is not None:
 
74
            raise BinaryFiles(match.group(1), match.group(2))
61
75
        if not line.startswith("--- "):
62
76
            raise MalformedPatchHeader("No orig name", line)
63
77
        else:
93
107
    range = int(range)
94
108
    return (pos, range)
95
109
 
96
 
 
 
110
 
97
111
def hunk_from_header(line):
 
112
    import re
98
113
    matches = re.match(r'\@\@ ([^@]*) \@\@( (.*))?\n', line)
99
114
    if matches is None:
100
115
        raise MalformedHunkHeader("Does not match format.", line)
164
179
        return InsertLine(line[1:])
165
180
    elif line.startswith("-"):
166
181
        return RemoveLine(line[1:])
167
 
    elif line == NO_NL:
168
 
        return NO_NL
169
182
    else:
170
183
        raise MalformedLine("Unknown line type", line)
171
184
__pychecker__=""
220
233
            return self.shift_to_mod_lines(pos)
221
234
 
222
235
    def shift_to_mod_lines(self, pos):
223
 
        assert (pos >= self.orig_pos-1 and pos <= self.orig_pos+self.orig_range)
224
236
        position = self.orig_pos-1
225
237
        shift = 0
226
238
        for line in self.lines:
238
250
        return shift
239
251
 
240
252
 
241
 
def iter_hunks(iter_lines):
 
253
def iter_hunks(iter_lines, allow_dirty=False):
 
254
    '''
 
255
    :arg iter_lines: iterable of lines to parse for hunks
 
256
    :kwarg allow_dirty: If True, when we encounter something that is not
 
257
        a hunk header when we're looking for one, assume the rest of the lines
 
258
        are not part of the patch (comments or other junk).  Default False
 
259
    '''
242
260
    hunk = None
243
261
    for line in iter_lines:
244
262
        if line == "\n":
248
266
            continue
249
267
        if hunk is not None:
250
268
            yield hunk
251
 
        hunk = hunk_from_header(line)
 
269
        try:
 
270
            hunk = hunk_from_header(line)
 
271
        except MalformedHunkHeader:
 
272
            if allow_dirty:
 
273
                # If the line isn't a hunk header, then we've reached the end
 
274
                # of this patch and there's "junk" at the end.  Ignore the
 
275
                # rest of this patch.
 
276
                return
 
277
            raise
252
278
        orig_size = 0
253
279
        mod_size = 0
254
280
        while orig_size < hunk.orig_range or mod_size < hunk.mod_range:
262
288
        yield hunk
263
289
 
264
290
 
265
 
class Patch:
 
291
class BinaryPatch(object):
266
292
    def __init__(self, oldname, newname):
267
293
        self.oldname = oldname
268
294
        self.newname = newname
 
295
 
 
296
    def __str__(self):
 
297
        return 'Binary files %s and %s differ\n' % (self.oldname, self.newname)
 
298
 
 
299
 
 
300
class Patch(BinaryPatch):
 
301
 
 
302
    def __init__(self, oldname, newname):
 
303
        BinaryPatch.__init__(self, oldname, newname)
269
304
        self.hunks = []
270
305
 
271
306
    def __str__(self):
272
 
        ret = self.get_header() 
 
307
        ret = self.get_header()
273
308
        ret += "".join([str(h) for h in self.hunks])
274
309
        return ret
275
310
 
276
311
    def get_header(self):
277
312
        return "--- %s\n+++ %s\n" % (self.oldname, self.newname)
278
313
 
279
 
    def stats_str(self):
280
 
        """Return a string of patch statistics"""
 
314
    def stats_values(self):
 
315
        """Calculate the number of inserts and removes."""
281
316
        removes = 0
282
317
        inserts = 0
283
318
        for hunk in self.hunks:
286
321
                     inserts+=1;
287
322
                elif isinstance(line, RemoveLine):
288
323
                     removes+=1;
 
324
        return (inserts, removes, len(self.hunks))
 
325
 
 
326
    def stats_str(self):
 
327
        """Return a string of patch statistics"""
289
328
        return "%i inserts, %i removes in %i hunks" % \
290
 
            (inserts, removes, len(self.hunks))
 
329
            self.stats_values()
291
330
 
292
331
    def pos_in_mod(self, position):
293
332
        newpos = position
297
336
                return None
298
337
            newpos += shift
299
338
        return newpos
300
 
            
 
339
 
301
340
    def iter_inserted(self):
302
341
        """Iteraties through inserted lines
303
 
        
 
342
 
304
343
        :return: Pair of line number, line
305
344
        :rtype: iterator of (int, InsertLine)
306
345
        """
314
353
                    pos += 1
315
354
 
316
355
 
317
 
def parse_patch(iter_lines):
318
 
    (orig_name, mod_name) = get_patch_names(iter_lines)
319
 
    patch = Patch(orig_name, mod_name)
320
 
    for hunk in iter_hunks(iter_lines):
321
 
        patch.hunks.append(hunk)
322
 
    return patch
323
 
 
324
 
 
325
 
def iter_file_patch(iter_lines):
 
356
def parse_patch(iter_lines, allow_dirty=False):
 
357
    '''
 
358
    :arg iter_lines: iterable of lines to parse
 
359
    :kwarg allow_dirty: If True, allow the patch to have trailing junk.
 
360
        Default False
 
361
    '''
 
362
    iter_lines = iter_lines_handle_nl(iter_lines)
 
363
    try:
 
364
        (orig_name, mod_name) = get_patch_names(iter_lines)
 
365
    except BinaryFiles, e:
 
366
        return BinaryPatch(e.orig_name, e.mod_name)
 
367
    else:
 
368
        patch = Patch(orig_name, mod_name)
 
369
        for hunk in iter_hunks(iter_lines, allow_dirty):
 
370
            patch.hunks.append(hunk)
 
371
        return patch
 
372
 
 
373
 
 
374
def iter_file_patch(iter_lines, allow_dirty=False):
 
375
    '''
 
376
    :arg iter_lines: iterable of lines to parse for patches
 
377
    :kwarg allow_dirty: If True, allow comments and other non-patch text
 
378
        before the first patch.  Note that the algorithm here can only find
 
379
        such text before any patches have been found.  Comments after the
 
380
        first patch are stripped away in iter_hunks() if it is also passed
 
381
        allow_dirty=True.  Default False.
 
382
    '''
 
383
    ### FIXME: Docstring is not quite true.  We allow certain comments no
 
384
    # matter what, If they startwith '===', '***', or '#' Someone should
 
385
    # reexamine this logic and decide if we should include those in
 
386
    # allow_dirty or restrict those to only being before the patch is found
 
387
    # (as allow_dirty does).
 
388
    regex = re.compile(binary_files_re)
326
389
    saved_lines = []
327
390
    orig_range = 0
 
391
    beginning = True
328
392
    for line in iter_lines:
329
393
        if line.startswith('=== ') or line.startswith('*** '):
330
394
            continue
333
397
        elif orig_range > 0:
334
398
            if line.startswith('-') or line.startswith(' '):
335
399
                orig_range -= 1
336
 
        elif line.startswith('--- '):
337
 
            if len(saved_lines) > 0:
 
400
        elif line.startswith('--- ') or regex.match(line):
 
401
            if allow_dirty and beginning:
 
402
                # Patches can have "junk" at the beginning
 
403
                # Stripping junk from the end of patches is handled when we
 
404
                # parse the patch
 
405
                beginning = False
 
406
            elif len(saved_lines) > 0:
338
407
                yield saved_lines
339
408
            saved_lines = []
340
409
        elif line.startswith('@@'):
355
424
    last_line = None
356
425
    for line in iter_lines:
357
426
        if line == NO_NL:
358
 
            assert last_line.endswith('\n')
 
427
            if not last_line.endswith('\n'):
 
428
                raise AssertionError()
359
429
            last_line = last_line[:-1]
360
430
            line = None
361
431
        if last_line is not None:
365
435
        yield last_line
366
436
 
367
437
 
368
 
def parse_patches(iter_lines):
369
 
    iter_lines = iter_lines_handle_nl(iter_lines)
370
 
    return [parse_patch(f.__iter__()) for f in iter_file_patch(iter_lines)]
 
438
def parse_patches(iter_lines, allow_dirty=False):
 
439
    '''
 
440
    :arg iter_lines: iterable of lines to parse for patches
 
441
    :kwarg allow_dirty: If True, allow text that's not part of the patch at
 
442
        selected places.  This includes comments before and after a patch
 
443
        for instance.  Default False.
 
444
    '''
 
445
    return [parse_patch(f.__iter__(), allow_dirty) for f in
 
446
                        iter_file_patch(iter_lines, allow_dirty)]
371
447
 
372
448
 
373
449
def difference_index(atext, btext):
393
469
    """Iterate through a series of lines with a patch applied.
394
470
    This handles a single file, and does exact, not fuzzy patching.
395
471
    """
396
 
    if orig_lines is not None:
397
 
        orig_lines = orig_lines.__iter__()
 
472
    patch_lines = iter_lines_handle_nl(iter(patch_lines))
 
473
    get_patch_names(patch_lines)
 
474
    return iter_patched_from_hunks(orig_lines, iter_hunks(patch_lines))
 
475
 
 
476
 
 
477
def iter_patched_from_hunks(orig_lines, hunks):
 
478
    """Iterate through a series of lines with a patch applied.
 
479
    This handles a single file, and does exact, not fuzzy patching.
 
480
 
 
481
    :param orig_lines: The unpatched lines.
 
482
    :param hunks: An iterable of Hunk instances.
 
483
    """
398
484
    seen_patch = []
399
 
    patch_lines = iter_lines_handle_nl(patch_lines.__iter__())
400
 
    get_patch_names(patch_lines)
401
485
    line_no = 1
402
 
    for hunk in iter_hunks(patch_lines):
 
486
    if orig_lines is not None:
 
487
        orig_lines = iter(orig_lines)
 
488
    for hunk in hunks:
403
489
        while line_no < hunk.orig_pos:
404
490
            orig_line = orig_lines.next()
405
491
            yield orig_line
415
501
                if isinstance(hunk_line, ContextLine):
416
502
                    yield orig_line
417
503
                else:
418
 
                    assert isinstance(hunk_line, RemoveLine)
 
504
                    if not isinstance(hunk_line, RemoveLine):
 
505
                        raise AssertionError(hunk_line)
419
506
                line_no += 1
420
507
    if orig_lines is not None:
421
508
        for line in orig_lines: