~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

(vila) Fix bzrlib.tests.test_gpg.TestVerify.test_verify_revoked_signature
 with recent versions of gpg. (Vincent Ladeuil)

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
            if len(saved_lines) > 0:
 
362
                if keep_dirty and len(dirty_head) > 0:
 
363
                    yield {'saved_lines': saved_lines,
 
364
                           'dirty_head': dirty_head}
 
365
                    dirty_head = []
 
366
                else:
 
367
                    yield saved_lines
 
368
                saved_lines = []
 
369
            dirty_head.append(line)
 
370
            continue
 
371
        if line.startswith('*** '):
359
372
            continue
360
373
        if line.startswith('#'):
361
374
            continue
369
382
                # parse the patch
370
383
                beginning = False
371
384
            elif len(saved_lines) > 0:
372
 
                yield saved_lines
 
385
                if keep_dirty and len(dirty_head) > 0:
 
386
                    yield {'saved_lines': saved_lines,
 
387
                           'dirty_head': dirty_head}
 
388
                    dirty_head = []
 
389
                else:
 
390
                    yield saved_lines
373
391
            saved_lines = []
374
392
        elif line.startswith('@@'):
375
393
            hunk = hunk_from_header(line)
376
394
            orig_range = hunk.orig_range
377
395
        saved_lines.append(line)
378
396
    if len(saved_lines) > 0:
379
 
        yield saved_lines
 
397
        if keep_dirty and len(dirty_head) > 0:
 
398
            yield {'saved_lines': saved_lines,
 
399
                   'dirty_head': dirty_head}
 
400
        else:
 
401
            yield saved_lines
380
402
 
381
403
 
382
404
def iter_lines_handle_nl(iter_lines):
400
422
        yield last_line
401
423
 
402
424
 
403
 
def parse_patches(iter_lines, allow_dirty=False):
 
425
def parse_patches(iter_lines, allow_dirty=False, keep_dirty=False):
404
426
    '''
405
427
    :arg iter_lines: iterable of lines to parse for patches
406
428
    :kwarg allow_dirty: If True, allow text that's not part of the patch at
407
429
        selected places.  This includes comments before and after a patch
408
430
        for instance.  Default False.
 
431
    :kwarg keep_dirty: If True, returns a dict of patches with dirty headers.
 
432
        Default False.
409
433
    '''
410
 
    return [parse_patch(f.__iter__(), allow_dirty) for f in
411
 
                        iter_file_patch(iter_lines, allow_dirty)]
 
434
    patches = []
 
435
    for patch_lines in iter_file_patch(iter_lines, allow_dirty, keep_dirty):
 
436
        if 'dirty_head' in patch_lines:
 
437
            patches.append({'patch': parse_patch(
 
438
                patch_lines['saved_lines'], allow_dirty),
 
439
                            'dirty_head': patch_lines['dirty_head']})
 
440
        else:
 
441
            patches.append(parse_patch(patch_lines, allow_dirty))
 
442
    return patches
412
443
 
413
444
 
414
445
def difference_index(atext, btext):