~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: Sabin Iacob
  • Date: 2009-03-23 14:59:43 UTC
  • mto: (4189.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4193.
  • Revision ID: iacobs@m0n5t3r.info-20090323145943-3s3p1px5q1rkh2e5
update FSF mailing address

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
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
import re
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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__=""
220
218
            return self.shift_to_mod_lines(pos)
221
219
 
222
220
    def shift_to_mod_lines(self, pos):
223
 
        assert (pos >= self.orig_pos-1 and pos <= self.orig_pos+self.orig_range)
224
221
        position = self.orig_pos-1
225
222
        shift = 0
226
223
        for line in self.lines:
269
266
        self.hunks = []
270
267
 
271
268
    def __str__(self):
272
 
        ret = self.get_header() 
 
269
        ret = self.get_header()
273
270
        ret += "".join([str(h) for h in self.hunks])
274
271
        return ret
275
272
 
276
273
    def get_header(self):
277
274
        return "--- %s\n+++ %s\n" % (self.oldname, self.newname)
278
275
 
279
 
    def stats_str(self):
280
 
        """Return a string of patch statistics"""
 
276
    def stats_values(self):
 
277
        """Calculate the number of inserts and removes."""
281
278
        removes = 0
282
279
        inserts = 0
283
280
        for hunk in self.hunks:
286
283
                     inserts+=1;
287
284
                elif isinstance(line, RemoveLine):
288
285
                     removes+=1;
 
286
        return (inserts, removes, len(self.hunks))
 
287
 
 
288
    def stats_str(self):
 
289
        """Return a string of patch statistics"""
289
290
        return "%i inserts, %i removes in %i hunks" % \
290
 
            (inserts, removes, len(self.hunks))
 
291
            self.stats_values()
291
292
 
292
293
    def pos_in_mod(self, position):
293
294
        newpos = position
297
298
                return None
298
299
            newpos += shift
299
300
        return newpos
300
 
            
 
301
 
301
302
    def iter_inserted(self):
302
303
        """Iteraties through inserted lines
303
 
        
 
304
 
304
305
        :return: Pair of line number, line
305
306
        :rtype: iterator of (int, InsertLine)
306
307
        """
315
316
 
316
317
 
317
318
def parse_patch(iter_lines):
 
319
    iter_lines = iter_lines_handle_nl(iter_lines)
318
320
    (orig_name, mod_name) = get_patch_names(iter_lines)
319
321
    patch = Patch(orig_name, mod_name)
320
322
    for hunk in iter_hunks(iter_lines):
355
357
    last_line = None
356
358
    for line in iter_lines:
357
359
        if line == NO_NL:
358
 
            assert last_line.endswith('\n')
 
360
            if not last_line.endswith('\n'):
 
361
                raise AssertionError()
359
362
            last_line = last_line[:-1]
360
363
            line = None
361
364
        if last_line is not None:
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
415
427
                if isinstance(hunk_line, ContextLine):
416
428
                    yield orig_line
417
429
                else:
418
 
                    assert isinstance(hunk_line, RemoveLine)
 
430
                    if not isinstance(hunk_line, RemoveLine):
 
431
                        raise AssertionError(hunk_line)
419
432
                line_no += 1
420
433
    if orig_lines is not None:
421
434
        for line in orig_lines: