~bzr/ubuntu/lucid/bzr/beta-ppa

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: Martin Pool
  • Date: 2010-08-18 04:26:39 UTC
  • mfrom: (129.1.8 packaging-karmic)
  • Revision ID: mbp@sourcefrog.net-20100818042639-mjoxtngyjwiu05fo
* PPA rebuild for lucid.
* PPA rebuild for karmic.
* PPA rebuild onto jaunty.
* New upstream release.

Show diffs side-by-side

added added

removed removed

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