~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/multiparent.py

  • Committer: Martin Pool
  • Date: 2010-02-25 06:17:27 UTC
  • mfrom: (5055 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5057.
  • Revision ID: mbp@sourcefrog.net-20100225061727-4sd9lt0qmdc6087t
merge news

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2011 Canonical Ltd
 
1
# Copyright (C) 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
18
18
 
19
19
lazy_import(globals(), """
20
20
import errno
21
 
import gzip
22
21
import itertools
23
22
import os
24
23
from StringIO import StringIO
25
24
 
26
25
from bzrlib import (
27
 
    bencode,
28
26
    errors,
29
27
    patiencediff,
 
28
    trace,
30
29
    ui,
31
30
    )
 
31
from bzrlib import bencode
32
32
""")
 
33
from bzrlib.tuned_gzip import GzipFile
33
34
 
34
35
 
35
36
def topo_iter_keys(vf, keys=None):
75
76
class MultiParent(object):
76
77
    """A multi-parent diff"""
77
78
 
78
 
    __slots__ = ['hunks']
79
 
 
80
79
    def __init__(self, hunks=None):
81
80
        if hunks is not None:
82
81
            self.hunks = hunks
259
258
class NewText(object):
260
259
    """The contents of text that is introduced by this text"""
261
260
 
262
 
    __slots__ = ['lines']
263
 
 
264
261
    def __init__(self, lines):
265
262
        self.lines = lines
266
263
 
282
279
class ParentText(object):
283
280
    """A reference to text present in a parent text"""
284
281
 
285
 
    __slots__ = ['parent', 'parent_pos', 'child_pos', 'num_lines']
286
 
 
287
282
    def __init__(self, parent, parent_pos, child_pos, num_lines):
288
283
        self.parent = parent
289
284
        self.parent_pos = parent_pos
290
285
        self.child_pos = child_pos
291
286
        self.num_lines = num_lines
292
287
 
293
 
    def _as_dict(self):
294
 
        return dict(parent=self.parent, parent_pos=self.parent_pos,
295
 
                    child_pos=self.child_pos, num_lines=self.num_lines)
296
 
 
297
288
    def __repr__(self):
298
 
        return ('ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'
299
 
                ' %(num_lines)r)' % self._as_dict())
 
289
        return 'ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'\
 
290
            ' %(num_lines)r)' % self.__dict__
300
291
 
301
292
    def __eq__(self, other):
302
293
        if self.__class__ is not other.__class__:
303
294
            return False
304
 
        return self._as_dict() == other._as_dict()
 
295
        return (self.__dict__ == other.__dict__)
305
296
 
306
297
    def to_patch(self):
307
 
        yield ('c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'
308
 
               % self._as_dict())
 
298
        yield 'c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'\
 
299
            % self.__dict__
309
300
 
310
301
 
311
302
class BaseVersionedFile(object):
421
412
                            if not (lines == self.get_line_list([revision])[0]):
422
413
                                raise AssertionError()
423
414
                            self.clear_cache()
424
 
                    pb.update(gettext('Importing revisions'),
 
415
                    pb.update('Importing revisions',
425
416
                              (total - len(revisions)) + len(added), total)
426
417
                revisions = [r for r in revisions if r not in added]
427
418
        finally:
560
551
            sio = StringIO(infile.read(count))
561
552
        finally:
562
553
            infile.close()
563
 
        zip_file = gzip.GzipFile(None, mode='rb', fileobj=sio)
 
554
        zip_file = GzipFile(None, mode='rb', fileobj=sio)
564
555
        try:
565
556
            file_version_id = zip_file.readline()
566
 
            content = zip_file.read()
567
 
            return MultiParent.from_patch(content)
 
557
            return MultiParent.from_patch(zip_file.read())
568
558
        finally:
569
559
            zip_file.close()
570
560
 
576
566
                                    # before any write returns 0
577
567
            start = outfile.tell()
578
568
            try:
579
 
                zipfile = gzip.GzipFile(None, mode='ab', fileobj=outfile)
 
569
                zipfile = GzipFile(None, mode='ab', fileobj=outfile)
580
570
                zipfile.writelines(itertools.chain(
581
571
                    ['version %s\n' % version_id], diff.to_patch()))
582
572
            finally:
673
663
 
674
664
def gzip_string(lines):
675
665
    sio = StringIO()
676
 
    data_file = gzip.GzipFile(None, mode='wb', fileobj=sio)
 
666
    data_file = GzipFile(None, mode='wb', fileobj=sio)
677
667
    data_file.writelines(lines)
678
668
    data_file.close()
679
669
    return sio.getvalue()