~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-03-16 14:01:20 UTC
  • mfrom: (3280.2.5 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080316140120-i3yq8yr1l66m11h7
Start 1.4 development

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
13
13
#
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
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
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):
92
93
    range = int(range)
93
94
    return (pos, range)
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)
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
167
169
    else:
168
170
        raise MalformedLine("Unknown line type", line)
169
171
__pychecker__=""
218
220
            return self.shift_to_mod_lines(pos)
219
221
 
220
222
    def shift_to_mod_lines(self, pos):
 
223
        assert (pos >= self.orig_pos-1 and pos <= self.orig_pos+self.orig_range)
221
224
        position = self.orig_pos-1
222
225
        shift = 0
223
226
        for line in self.lines:
266
269
        self.hunks = []
267
270
 
268
271
    def __str__(self):
269
 
        ret = self.get_header()
 
272
        ret = self.get_header() 
270
273
        ret += "".join([str(h) for h in self.hunks])
271
274
        return ret
272
275
 
273
276
    def get_header(self):
274
277
        return "--- %s\n+++ %s\n" % (self.oldname, self.newname)
275
278
 
276
 
    def stats_values(self):
277
 
        """Calculate the number of inserts and removes."""
 
279
    def stats_str(self):
 
280
        """Return a string of patch statistics"""
278
281
        removes = 0
279
282
        inserts = 0
280
283
        for hunk in self.hunks:
283
286
                     inserts+=1;
284
287
                elif isinstance(line, RemoveLine):
285
288
                     removes+=1;
286
 
        return (inserts, removes, len(self.hunks))
287
 
 
288
 
    def stats_str(self):
289
 
        """Return a string of patch statistics"""
290
289
        return "%i inserts, %i removes in %i hunks" % \
291
 
            self.stats_values()
 
290
            (inserts, removes, len(self.hunks))
292
291
 
293
292
    def pos_in_mod(self, position):
294
293
        newpos = position
298
297
                return None
299
298
            newpos += shift
300
299
        return newpos
301
 
 
 
300
            
302
301
    def iter_inserted(self):
303
302
        """Iteraties through inserted lines
304
 
 
 
303
        
305
304
        :return: Pair of line number, line
306
305
        :rtype: iterator of (int, InsertLine)
307
306
        """
316
315
 
317
316
 
318
317
def parse_patch(iter_lines):
319
 
    iter_lines = iter_lines_handle_nl(iter_lines)
320
318
    (orig_name, mod_name) = get_patch_names(iter_lines)
321
319
    patch = Patch(orig_name, mod_name)
322
320
    for hunk in iter_hunks(iter_lines):
357
355
    last_line = None
358
356
    for line in iter_lines:
359
357
        if line == NO_NL:
360
 
            if not last_line.endswith('\n'):
361
 
                raise AssertionError()
 
358
            assert last_line.endswith('\n')
362
359
            last_line = last_line[:-1]
363
360
            line = None
364
361
        if last_line is not None:
369
366
 
370
367
 
371
368
def parse_patches(iter_lines):
 
369
    iter_lines = iter_lines_handle_nl(iter_lines)
372
370
    return [parse_patch(f.__iter__()) for f in iter_file_patch(iter_lines)]
373
371
 
374
372
 
395
393
    """Iterate through a series of lines with a patch applied.
396
394
    This handles a single file, and does exact, not fuzzy patching.
397
395
    """
398
 
    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__())
399
400
    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
 
    """
410
 
    seen_patch = []
411
401
    line_no = 1
412
 
    if orig_lines is not None:
413
 
        orig_lines = iter(orig_lines)
414
 
    for hunk in hunks:
 
402
    for hunk in iter_hunks(patch_lines):
415
403
        while line_no < hunk.orig_pos:
416
404
            orig_line = orig_lines.next()
417
405
            yield orig_line
427
415
                if isinstance(hunk_line, ContextLine):
428
416
                    yield orig_line
429
417
                else:
430
 
                    if not isinstance(hunk_line, RemoveLine):
431
 
                        raise AssertionError(hunk_line)
 
418
                    assert isinstance(hunk_line, RemoveLine)
432
419
                line_no += 1
433
420
    if orig_lines is not None:
434
421
        for line in orig_lines: