~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: Ian Clatworthy
  • Date: 2009-03-20 04:22:44 UTC
  • mfrom: (3368.2.54 bzr.content-filters)
  • mto: This revision was merged to the branch mainline in revision 4173.
  • Revision ID: ian.clatworthy@canonical.com-20090320042244-xwqpk6gc4t9y9pbi
Content filters (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004 - 2006 Aaron Bentley, Canonical Ltd
 
1
# Copyright (C) 2004 - 2006, 2008 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
18
17
 
19
18
 
20
19
class PatchSyntax(Exception):
93
92
    range = int(range)
94
93
    return (pos, range)
95
94
 
96
 
 
 
95
 
97
96
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)
164
164
        return InsertLine(line[1:])
165
165
    elif line.startswith("-"):
166
166
        return RemoveLine(line[1:])
167
 
    elif line == NO_NL:
168
 
        return NO_NL
169
167
    else:
170
168
        raise MalformedLine("Unknown line type", line)
171
169
__pychecker__=""
268
266
        self.hunks = []
269
267
 
270
268
    def __str__(self):
271
 
        ret = self.get_header() 
 
269
        ret = self.get_header()
272
270
        ret += "".join([str(h) for h in self.hunks])
273
271
        return ret
274
272
 
275
273
    def get_header(self):
276
274
        return "--- %s\n+++ %s\n" % (self.oldname, self.newname)
277
275
 
278
 
    def stats_str(self):
279
 
        """Return a string of patch statistics"""
 
276
    def stats_values(self):
 
277
        """Calculate the number of inserts and removes."""
280
278
        removes = 0
281
279
        inserts = 0
282
280
        for hunk in self.hunks:
285
283
                     inserts+=1;
286
284
                elif isinstance(line, RemoveLine):
287
285
                     removes+=1;
 
286
        return (inserts, removes, len(self.hunks))
 
287
 
 
288
    def stats_str(self):
 
289
        """Return a string of patch statistics"""
288
290
        return "%i inserts, %i removes in %i hunks" % \
289
 
            (inserts, removes, len(self.hunks))
 
291
            self.stats_values()
290
292
 
291
293
    def pos_in_mod(self, position):
292
294
        newpos = position
296
298
                return None
297
299
            newpos += shift
298
300
        return newpos
299
 
            
 
301
 
300
302
    def iter_inserted(self):
301
303
        """Iteraties through inserted lines
302
 
        
 
304
 
303
305
        :return: Pair of line number, line
304
306
        :rtype: iterator of (int, InsertLine)
305
307
        """
314
316
 
315
317
 
316
318
def parse_patch(iter_lines):
 
319
    iter_lines = iter_lines_handle_nl(iter_lines)
317
320
    (orig_name, mod_name) = get_patch_names(iter_lines)
318
321
    patch = Patch(orig_name, mod_name)
319
322
    for hunk in iter_hunks(iter_lines):
366
369
 
367
370
 
368
371
def parse_patches(iter_lines):
369
 
    iter_lines = iter_lines_handle_nl(iter_lines)
370
372
    return [parse_patch(f.__iter__()) for f in iter_file_patch(iter_lines)]
371
373
 
372
374
 
393
395
    """Iterate through a series of lines with a patch applied.
394
396
    This handles a single file, and does exact, not fuzzy patching.
395
397
    """
396
 
    if orig_lines is not None:
397
 
        orig_lines = orig_lines.__iter__()
 
398
    patch_lines = iter_lines_handle_nl(iter(patch_lines))
 
399
    get_patch_names(patch_lines)
 
400
    return iter_patched_from_hunks(orig_lines, iter_hunks(patch_lines))
 
401
 
 
402
 
 
403
def iter_patched_from_hunks(orig_lines, hunks):
 
404
    """Iterate through a series of lines with a patch applied.
 
405
    This handles a single file, and does exact, not fuzzy patching.
 
406
 
 
407
    :param orig_lines: The unpatched lines.
 
408
    :param hunks: An iterable of Hunk instances.
 
409
    """
398
410
    seen_patch = []
399
 
    patch_lines = iter_lines_handle_nl(patch_lines.__iter__())
400
 
    get_patch_names(patch_lines)
401
411
    line_no = 1
402
 
    for hunk in iter_hunks(patch_lines):
 
412
    if orig_lines is not None:
 
413
        orig_lines = iter(orig_lines)
 
414
    for hunk in hunks:
403
415
        while line_no < hunk.orig_pos:
404
416
            orig_line = orig_lines.next()
405
417
            yield orig_line