~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: Aaron Bentley
  • Date: 2007-12-09 23:53:50 UTC
  • mto: This revision was merged to the branch mainline in revision 3133.
  • Revision ID: aaron.bentley@utoronto.ca-20071209235350-qp39yk0xzx7a4f6p
Don't use the base if not cherrypicking

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004 - 2006, 2008 Aaron Bentley, Canonical Ltd
 
1
# Copyright (C) 2004 - 2006 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
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
import re
17
18
 
18
19
 
19
20
class PatchSyntax(Exception):
94
95
 
95
96
 
96
97
def hunk_from_header(line):
97
 
    import re
98
98
    matches = re.match(r'\@\@ ([^@]*) \@\@( (.*))?\n', line)
99
99
    if matches is None:
100
100
        raise MalformedHunkHeader("Does not match format.", line)
220
220
            return self.shift_to_mod_lines(pos)
221
221
 
222
222
    def shift_to_mod_lines(self, pos):
 
223
        assert (pos >= self.orig_pos-1 and pos <= self.orig_pos+self.orig_range)
223
224
        position = self.orig_pos-1
224
225
        shift = 0
225
226
        for line in self.lines:
275
276
    def get_header(self):
276
277
        return "--- %s\n+++ %s\n" % (self.oldname, self.newname)
277
278
 
278
 
    def stats_values(self):
279
 
        """Calculate the number of inserts and removes."""
 
279
    def stats_str(self):
 
280
        """Return a string of patch statistics"""
280
281
        removes = 0
281
282
        inserts = 0
282
283
        for hunk in self.hunks:
285
286
                     inserts+=1;
286
287
                elif isinstance(line, RemoveLine):
287
288
                     removes+=1;
288
 
        return (inserts, removes, len(self.hunks))
289
 
 
290
 
    def stats_str(self):
291
 
        """Return a string of patch statistics"""
292
289
        return "%i inserts, %i removes in %i hunks" % \
293
 
            self.stats_values()
 
290
            (inserts, removes, len(self.hunks))
294
291
 
295
292
    def pos_in_mod(self, position):
296
293
        newpos = position
358
355
    last_line = None
359
356
    for line in iter_lines:
360
357
        if line == NO_NL:
361
 
            if not last_line.endswith('\n'):
362
 
                raise AssertionError()
 
358
            assert last_line.endswith('\n')
363
359
            last_line = last_line[:-1]
364
360
            line = None
365
361
        if last_line is not None:
397
393
    """Iterate through a series of lines with a patch applied.
398
394
    This handles a single file, and does exact, not fuzzy patching.
399
395
    """
400
 
    patch_lines = iter_lines_handle_nl(iter(patch_lines))
 
396
    if orig_lines is not None:
 
397
        orig_lines = orig_lines.__iter__()
 
398
    seen_patch = []
 
399
    patch_lines = iter_lines_handle_nl(patch_lines.__iter__())
401
400
    get_patch_names(patch_lines)
402
 
    return iter_patched_from_hunks(orig_lines, iter_hunks(patch_lines))
403
 
 
404
 
 
405
 
def iter_patched_from_hunks(orig_lines, hunks):
406
 
    """Iterate through a series of lines with a patch applied.
407
 
    This handles a single file, and does exact, not fuzzy patching.
408
 
 
409
 
    :param orig_lines: The unpatched lines.
410
 
    :param hunks: An iterable of Hunk instances.
411
 
    """
412
 
    seen_patch = []
413
401
    line_no = 1
414
 
    if orig_lines is not None:
415
 
        orig_lines = iter(orig_lines)
416
 
    for hunk in hunks:
 
402
    for hunk in iter_hunks(patch_lines):
417
403
        while line_no < hunk.orig_pos:
418
404
            orig_line = orig_lines.next()
419
405
            yield orig_line
429
415
                if isinstance(hunk_line, ContextLine):
430
416
                    yield orig_line
431
417
                else:
432
 
                    if not isinstance(hunk_line, RemoveLine):
433
 
                        raise AssertionError(hunk_line)
 
418
                    assert isinstance(hunk_line, RemoveLine)
434
419
                line_no += 1
435
420
    if orig_lines is not None:
436
421
        for line in orig_lines: