~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

  • Committer: Matthew Fuller
  • Date: 2009-08-18 08:10:44 UTC
  • mto: (4772.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4773.
  • Revision ID: fullermd@over-yonder.net-20090818081044-2due6ius01c4pwjl
Fix up some doctests to handle things ending up as RevisionSpec_dwim's
instead of RS_revno, and ending up as _dwim's (which may error
eventually, but won't until we try to evaluate them) instead of
insta-errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 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., 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.')
29
17
 
30
18
 
31
19
class PatchSyntax(Exception):
69
57
def get_patch_names(iter_lines):
70
58
    try:
71
59
        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))
75
60
        if not line.startswith("--- "):
76
61
            raise MalformedPatchHeader("No orig name", line)
77
62
        else:
274
259
        yield hunk
275
260
 
276
261
 
277
 
class BinaryPatch(object):
 
262
class Patch:
278
263
    def __init__(self, oldname, newname):
279
264
        self.oldname = oldname
280
265
        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)
290
266
        self.hunks = []
291
267
 
292
268
    def __str__(self):
341
317
 
342
318
def parse_patch(iter_lines):
343
319
    iter_lines = iter_lines_handle_nl(iter_lines)
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
 
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
353
325
 
354
326
 
355
327
def iter_file_patch(iter_lines):
356
 
    regex = re.compile(binary_files_re)
357
328
    saved_lines = []
358
329
    orig_range = 0
359
330
    for line in iter_lines:
364
335
        elif orig_range > 0:
365
336
            if line.startswith('-') or line.startswith(' '):
366
337
                orig_range -= 1
367
 
        elif line.startswith('--- ') or regex.match(line):
 
338
        elif line.startswith('--- '):
368
339
            if len(saved_lines) > 0:
369
340
                yield saved_lines
370
341
            saved_lines = []