~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: John Arbash Meinel
  • Date: 2012-09-19 07:58:27 UTC
  • mfrom: (6437.63.9 2.5)
  • mto: This revision was merged to the branch mainline in revision 6563.
  • Revision ID: john@arbash-meinel.com-20120919075827-36b2b042kiaps0d3
Merge bzr-2.5.2 into trunk to get the fixes for ConnectionReset.

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
 
 
35
34
def get_patch_names(iter_lines):
36
 
    line = iter_lines.next()
37
35
    try:
 
36
        line = iter_lines.next()
38
37
        match = re.match(binary_files_re, line)
39
38
        if match is not None:
40
39
            raise BinaryFiles(match.group(1), match.group(2))
318
317
                if isinstance(line, ContextLine):
319
318
                    pos += 1
320
319
 
 
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, keep_dirty=False):
 
339
def iter_file_patch(iter_lines, allow_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 = []
356
355
    orig_range = 0
357
356
    beginning = True
358
 
 
359
357
    for line in iter_lines:
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('*** '):
 
358
        if line.startswith('=== ') or line.startswith('*** '):
372
359
            continue
373
360
        if line.startswith('#'):
374
361
            continue
382
369
                # parse the patch
383
370
                beginning = False
384
371
            elif len(saved_lines) > 0:
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
 
372
                yield saved_lines
391
373
            saved_lines = []
392
374
        elif line.startswith('@@'):
393
375
            hunk = hunk_from_header(line)
394
376
            orig_range = hunk.orig_range
395
377
        saved_lines.append(line)
396
378
    if len(saved_lines) > 0:
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
 
379
        yield saved_lines
402
380
 
403
381
 
404
382
def iter_lines_handle_nl(iter_lines):
422
400
        yield last_line
423
401
 
424
402
 
425
 
def parse_patches(iter_lines, allow_dirty=False, keep_dirty=False):
 
403
def parse_patches(iter_lines, allow_dirty=False):
426
404
    '''
427
405
    :arg iter_lines: iterable of lines to parse for patches
428
406
    :kwarg allow_dirty: If True, allow text that's not part of the patch at
429
407
        selected places.  This includes comments before and after a patch
430
408
        for instance.  Default False.
431
 
    :kwarg keep_dirty: If True, returns a dict of patches with dirty headers.
432
 
        Default False.
433
409
    '''
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
 
410
    return [parse_patch(f.__iter__(), allow_dirty) for f in
 
411
                        iter_file_patch(iter_lines, allow_dirty)]
443
412
 
444
413
 
445
414
def difference_index(atext, btext):