~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-02-11 04:02:41 UTC
  • mfrom: (5017.2.2 tariff)
  • Revision ID: pqm@pqm.ubuntu.com-20100211040241-w6n021dz0uus341n
(mbp) add import-tariff tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Aaron Bentley, Canonical Ltd
 
1
# Copyright (C) 2004 - 2006, 2008 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
250
250
        return shift
251
251
 
252
252
 
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
 
    '''
 
253
def iter_hunks(iter_lines):
260
254
    hunk = None
261
255
    for line in iter_lines:
262
256
        if line == "\n":
266
260
            continue
267
261
        if hunk is not None:
268
262
            yield hunk
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
 
263
        hunk = hunk_from_header(line)
278
264
        orig_size = 0
279
265
        mod_size = 0
280
266
        while orig_size < hunk.orig_range or mod_size < hunk.mod_range:
353
339
                    pos += 1
354
340
 
355
341
 
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
 
    '''
 
342
def parse_patch(iter_lines):
362
343
    iter_lines = iter_lines_handle_nl(iter_lines)
363
344
    try:
364
345
        (orig_name, mod_name) = get_patch_names(iter_lines)
366
347
        return BinaryPatch(e.orig_name, e.mod_name)
367
348
    else:
368
349
        patch = Patch(orig_name, mod_name)
369
 
        for hunk in iter_hunks(iter_lines, allow_dirty):
 
350
        for hunk in iter_hunks(iter_lines):
370
351
            patch.hunks.append(hunk)
371
352
        return patch
372
353
 
373
354
 
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).
 
355
def iter_file_patch(iter_lines):
388
356
    regex = re.compile(binary_files_re)
389
357
    saved_lines = []
390
358
    orig_range = 0
391
 
    beginning = True
392
359
    for line in iter_lines:
393
360
        if line.startswith('=== ') or line.startswith('*** '):
394
361
            continue
398
365
            if line.startswith('-') or line.startswith(' '):
399
366
                orig_range -= 1
400
367
        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:
 
368
            if len(saved_lines) > 0:
407
369
                yield saved_lines
408
370
            saved_lines = []
409
371
        elif line.startswith('@@'):
435
397
        yield last_line
436
398
 
437
399
 
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)]
 
400
def parse_patches(iter_lines):
 
401
    return [parse_patch(f.__iter__()) for f in iter_file_patch(iter_lines)]
447
402
 
448
403
 
449
404
def difference_index(atext, btext):