~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: Naoki INADA
  • Date: 2009-10-29 10:01:19 UTC
  • mto: (4634.97.3 2.0)
  • mto: This revision was merged to the branch mainline in revision 4798.
  • Revision ID: inada-n@klab.jp-20091029100119-uckv9t7ej2qrghw3
import doc-ja rev90

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
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
import re
18
18
 
19
19
 
 
20
class BinaryFiles(Exception):
 
21
 
 
22
    def __init__(self, orig_name, mod_name):
 
23
        self.orig_name = orig_name
 
24
        self.mod_name = mod_name
 
25
        Exception.__init__(self, 'Binary files section encountered.')
 
26
 
 
27
 
20
28
class PatchSyntax(Exception):
21
29
    def __init__(self, msg):
22
30
        Exception.__init__(self, msg)
58
66
def get_patch_names(iter_lines):
59
67
    try:
60
68
        line = iter_lines.next()
 
69
        match = re.match('Binary files (.*) and (.*) differ\n', line)
 
70
        if match is not None:
 
71
            raise BinaryFiles(match.group(1), match.group(2))
61
72
        if not line.startswith("--- "):
62
73
            raise MalformedPatchHeader("No orig name", line)
63
74
        else:
93
104
    range = int(range)
94
105
    return (pos, range)
95
106
 
96
 
 
 
107
 
97
108
def hunk_from_header(line):
 
109
    import re
98
110
    matches = re.match(r'\@\@ ([^@]*) \@\@( (.*))?\n', line)
99
111
    if matches is None:
100
112
        raise MalformedHunkHeader("Does not match format.", line)
164
176
        return InsertLine(line[1:])
165
177
    elif line.startswith("-"):
166
178
        return RemoveLine(line[1:])
167
 
    elif line == NO_NL:
168
 
        return NO_NL
169
179
    else:
170
180
        raise MalformedLine("Unknown line type", line)
171
181
__pychecker__=""
220
230
            return self.shift_to_mod_lines(pos)
221
231
 
222
232
    def shift_to_mod_lines(self, pos):
223
 
        assert (pos >= self.orig_pos-1 and pos <= self.orig_pos+self.orig_range)
224
233
        position = self.orig_pos-1
225
234
        shift = 0
226
235
        for line in self.lines:
262
271
        yield hunk
263
272
 
264
273
 
265
 
class Patch:
 
274
class BinaryPatch(object):
266
275
    def __init__(self, oldname, newname):
267
276
        self.oldname = oldname
268
277
        self.newname = newname
 
278
 
 
279
    def __str__(self):
 
280
        return 'Binary files %s and %s differ\n' % (self.oldname, self.newname)
 
281
 
 
282
 
 
283
class Patch(BinaryPatch):
 
284
 
 
285
    def __init__(self, oldname, newname):
 
286
        BinaryPatch.__init__(self, oldname, newname)
269
287
        self.hunks = []
270
288
 
271
289
    def __str__(self):
272
 
        ret = self.get_header() 
 
290
        ret = self.get_header()
273
291
        ret += "".join([str(h) for h in self.hunks])
274
292
        return ret
275
293
 
276
294
    def get_header(self):
277
295
        return "--- %s\n+++ %s\n" % (self.oldname, self.newname)
278
296
 
279
 
    def stats_str(self):
280
 
        """Return a string of patch statistics"""
 
297
    def stats_values(self):
 
298
        """Calculate the number of inserts and removes."""
281
299
        removes = 0
282
300
        inserts = 0
283
301
        for hunk in self.hunks:
286
304
                     inserts+=1;
287
305
                elif isinstance(line, RemoveLine):
288
306
                     removes+=1;
 
307
        return (inserts, removes, len(self.hunks))
 
308
 
 
309
    def stats_str(self):
 
310
        """Return a string of patch statistics"""
289
311
        return "%i inserts, %i removes in %i hunks" % \
290
 
            (inserts, removes, len(self.hunks))
 
312
            self.stats_values()
291
313
 
292
314
    def pos_in_mod(self, position):
293
315
        newpos = position
297
319
                return None
298
320
            newpos += shift
299
321
        return newpos
300
 
            
 
322
 
301
323
    def iter_inserted(self):
302
324
        """Iteraties through inserted lines
303
 
        
 
325
 
304
326
        :return: Pair of line number, line
305
327
        :rtype: iterator of (int, InsertLine)
306
328
        """
315
337
 
316
338
 
317
339
def parse_patch(iter_lines):
318
 
    (orig_name, mod_name) = get_patch_names(iter_lines)
319
 
    patch = Patch(orig_name, mod_name)
320
 
    for hunk in iter_hunks(iter_lines):
321
 
        patch.hunks.append(hunk)
322
 
    return patch
 
340
    iter_lines = iter_lines_handle_nl(iter_lines)
 
341
    try:
 
342
        (orig_name, mod_name) = get_patch_names(iter_lines)
 
343
    except BinaryFiles, e:
 
344
        return BinaryPatch(e.orig_name, e.mod_name)
 
345
    else:
 
346
        patch = Patch(orig_name, mod_name)
 
347
        for hunk in iter_hunks(iter_lines):
 
348
            patch.hunks.append(hunk)
 
349
        return patch
323
350
 
324
351
 
325
352
def iter_file_patch(iter_lines):
355
382
    last_line = None
356
383
    for line in iter_lines:
357
384
        if line == NO_NL:
358
 
            assert last_line.endswith('\n')
 
385
            if not last_line.endswith('\n'):
 
386
                raise AssertionError()
359
387
            last_line = last_line[:-1]
360
388
            line = None
361
389
        if last_line is not None:
366
394
 
367
395
 
368
396
def parse_patches(iter_lines):
369
 
    iter_lines = iter_lines_handle_nl(iter_lines)
370
397
    return [parse_patch(f.__iter__()) for f in iter_file_patch(iter_lines)]
371
398
 
372
399
 
393
420
    """Iterate through a series of lines with a patch applied.
394
421
    This handles a single file, and does exact, not fuzzy patching.
395
422
    """
396
 
    if orig_lines is not None:
397
 
        orig_lines = orig_lines.__iter__()
 
423
    patch_lines = iter_lines_handle_nl(iter(patch_lines))
 
424
    get_patch_names(patch_lines)
 
425
    return iter_patched_from_hunks(orig_lines, iter_hunks(patch_lines))
 
426
 
 
427
 
 
428
def iter_patched_from_hunks(orig_lines, hunks):
 
429
    """Iterate through a series of lines with a patch applied.
 
430
    This handles a single file, and does exact, not fuzzy patching.
 
431
 
 
432
    :param orig_lines: The unpatched lines.
 
433
    :param hunks: An iterable of Hunk instances.
 
434
    """
398
435
    seen_patch = []
399
 
    patch_lines = iter_lines_handle_nl(patch_lines.__iter__())
400
 
    get_patch_names(patch_lines)
401
436
    line_no = 1
402
 
    for hunk in iter_hunks(patch_lines):
 
437
    if orig_lines is not None:
 
438
        orig_lines = iter(orig_lines)
 
439
    for hunk in hunks:
403
440
        while line_no < hunk.orig_pos:
404
441
            orig_line = orig_lines.next()
405
442
            yield orig_line
415
452
                if isinstance(hunk_line, ContextLine):
416
453
                    yield orig_line
417
454
                else:
418
 
                    assert isinstance(hunk_line, RemoveLine)
 
455
                    if not isinstance(hunk_line, RemoveLine):
 
456
                        raise AssertionError(hunk_line)
419
457
                line_no += 1
420
458
    if orig_lines is not None:
421
459
        for line in orig_lines: