~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: Patch Queue Manager
  • Date: 2014-12-17 18:12:50 UTC
  • mfrom: (6601.1.8 bug-1400567)
  • Revision ID: pqm@pqm.ubuntu.com-20141217181250-ue0ku3dibz7vle02
(richard-wilbur) Added keep_dirty kwarg to bzrlib.patches.parse_patches()
 allowing preservation of dirty patch headers. (Bayard 'kit' Randel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 
32
32
binary_files_re = 'Binary files (.*) and (.*) differ\n'
33
33
 
 
34
 
34
35
def get_patch_names(iter_lines):
 
36
    line = iter_lines.next()
35
37
    try:
36
 
        line = iter_lines.next()
37
38
        match = re.match(binary_files_re, line)
38
39
        if match is not None:
39
40
            raise BinaryFiles(match.group(1), match.group(2))
317
318
                if isinstance(line, ContextLine):
318
319
                    pos += 1
319
320
 
320
 
 
321
321
def parse_patch(iter_lines, allow_dirty=False):
322
322
    '''
323
323
    :arg iter_lines: iterable of lines to parse
336
336
        return patch
337
337
 
338
338
 
339
 
def iter_file_patch(iter_lines, allow_dirty=False):
 
339
def iter_file_patch(iter_lines, allow_dirty=False, keep_dirty=False):
340
340
    '''
341
341
    :arg iter_lines: iterable of lines to parse for patches
342
342
    :kwarg allow_dirty: If True, allow comments and other non-patch text
352
352
    # (as allow_dirty does).
353
353
    regex = re.compile(binary_files_re)
354
354
    saved_lines = []
 
355
    dirty_head = []
355
356
    orig_range = 0
356
357
    beginning = True
 
358
 
357
359
    for line in iter_lines:
358
 
        if line.startswith('=== ') or line.startswith('*** '):
 
360
        if line.startswith('=== '):
 
361
            dirty_head.append(line)
 
362
            continue
 
363
        if line.startswith('*** '):
359
364
            continue
360
365
        if line.startswith('#'):
361
366
            continue
369
374
                # parse the patch
370
375
                beginning = False
371
376
            elif len(saved_lines) > 0:
372
 
                yield saved_lines
 
377
                if keep_dirty and len(dirty_head) > 0:
 
378
                    yield {'saved_lines': saved_lines,
 
379
                           'dirty_head': dirty_head}
 
380
                    dirty_head = []
 
381
                else:
 
382
                    yield saved_lines
373
383
            saved_lines = []
374
384
        elif line.startswith('@@'):
375
385
            hunk = hunk_from_header(line)
376
386
            orig_range = hunk.orig_range
377
387
        saved_lines.append(line)
378
388
    if len(saved_lines) > 0:
379
 
        yield saved_lines
 
389
        if keep_dirty and len(dirty_head) > 0:
 
390
            yield {'saved_lines': saved_lines,
 
391
                   'dirty_head': dirty_head}
 
392
        else:
 
393
            yield saved_lines
380
394
 
381
395
 
382
396
def iter_lines_handle_nl(iter_lines):
400
414
        yield last_line
401
415
 
402
416
 
403
 
def parse_patches(iter_lines, allow_dirty=False):
 
417
def parse_patches(iter_lines, allow_dirty=False, keep_dirty=False):
404
418
    '''
405
419
    :arg iter_lines: iterable of lines to parse for patches
406
420
    :kwarg allow_dirty: If True, allow text that's not part of the patch at
407
421
        selected places.  This includes comments before and after a patch
408
422
        for instance.  Default False.
 
423
    :kwarg keep_dirty: If True, returns a dict of patches with dirty headers.
 
424
        Default False.
409
425
    '''
410
 
    return [parse_patch(f.__iter__(), allow_dirty) for f in
411
 
                        iter_file_patch(iter_lines, allow_dirty)]
 
426
    patches = []
 
427
    for patch_lines in iter_file_patch(iter_lines, allow_dirty, keep_dirty):
 
428
        if 'dirty_head' in patch_lines:
 
429
            patches.append({'patch': parse_patch(
 
430
                patch_lines['saved_lines'], allow_dirty),
 
431
                            'dirty_head': patch_lines['dirty_head']})
 
432
        else:
 
433
            patches.append(parse_patch(patch_lines, allow_dirty))
 
434
    return patches
412
435
 
413
436
 
414
437
def difference_index(atext, btext):