~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

Merged bzr.dev into shelve-editor.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
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.')
17
26
 
18
27
 
19
28
class PatchSyntax(Exception):
57
66
def get_patch_names(iter_lines):
58
67
    try:
59
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))
60
72
        if not line.startswith("--- "):
61
73
            raise MalformedPatchHeader("No orig name", line)
62
74
        else:
259
271
        yield hunk
260
272
 
261
273
 
262
 
class Patch:
 
274
class BinaryPatch(object):
263
275
    def __init__(self, oldname, newname):
264
276
        self.oldname = oldname
265
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)
266
287
        self.hunks = []
267
288
 
268
289
    def __str__(self):
317
338
 
318
339
def parse_patch(iter_lines):
319
340
    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
 
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
325
350
 
326
351
 
327
352
def iter_file_patch(iter_lines):