~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: John Arbash Meinel
  • Date: 2010-02-17 17:11:16 UTC
  • mfrom: (4797.2.17 2.1)
  • mto: (4797.2.18 2.1)
  • mto: This revision was merged to the branch mainline in revision 5055.
  • Revision ID: john@arbash-meinel.com-20100217171116-h7t9223ystbnx5h8
merge bzr.2.1 in preparation for NEWS entry.

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) 2005-2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
import re
 
18
 
 
19
 
 
20
binary_files_re = 'Binary files (.*) and (.*) differ\n'
 
21
 
 
22
 
 
23
class BinaryFiles(Exception):
 
24
 
 
25
    def __init__(self, orig_name, mod_name):
 
26
        self.orig_name = orig_name
 
27
        self.mod_name = mod_name
 
28
        Exception.__init__(self, 'Binary files section encountered.')
17
29
 
18
30
 
19
31
class PatchSyntax(Exception):
57
69
def get_patch_names(iter_lines):
58
70
    try:
59
71
        line = iter_lines.next()
 
72
        match = re.match(binary_files_re, line)
 
73
        if match is not None:
 
74
            raise BinaryFiles(match.group(1), match.group(2))
60
75
        if not line.startswith("--- "):
61
76
            raise MalformedPatchHeader("No orig name", line)
62
77
        else:
259
274
        yield hunk
260
275
 
261
276
 
262
 
class Patch:
 
277
class BinaryPatch(object):
263
278
    def __init__(self, oldname, newname):
264
279
        self.oldname = oldname
265
280
        self.newname = newname
 
281
 
 
282
    def __str__(self):
 
283
        return 'Binary files %s and %s differ\n' % (self.oldname, self.newname)
 
284
 
 
285
 
 
286
class Patch(BinaryPatch):
 
287
 
 
288
    def __init__(self, oldname, newname):
 
289
        BinaryPatch.__init__(self, oldname, newname)
266
290
        self.hunks = []
267
291
 
268
292
    def __str__(self):
317
341
 
318
342
def parse_patch(iter_lines):
319
343
    iter_lines = iter_lines_handle_nl(iter_lines)
320
 
    (orig_name, mod_name) = get_patch_names(iter_lines)
321
 
    patch = Patch(orig_name, mod_name)
322
 
    for hunk in iter_hunks(iter_lines):
323
 
        patch.hunks.append(hunk)
324
 
    return patch
 
344
    try:
 
345
        (orig_name, mod_name) = get_patch_names(iter_lines)
 
346
    except BinaryFiles, e:
 
347
        return BinaryPatch(e.orig_name, e.mod_name)
 
348
    else:
 
349
        patch = Patch(orig_name, mod_name)
 
350
        for hunk in iter_hunks(iter_lines):
 
351
            patch.hunks.append(hunk)
 
352
        return patch
325
353
 
326
354
 
327
355
def iter_file_patch(iter_lines):
 
356
    regex = re.compile(binary_files_re)
328
357
    saved_lines = []
329
358
    orig_range = 0
330
359
    for line in iter_lines:
335
364
        elif orig_range > 0:
336
365
            if line.startswith('-') or line.startswith(' '):
337
366
                orig_range -= 1
338
 
        elif line.startswith('--- '):
 
367
        elif line.startswith('--- ') or regex.match(line):
339
368
            if len(saved_lines) > 0:
340
369
                yield saved_lines
341
370
            saved_lines = []