~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/multiparent.py

  • Committer: Ian Clatworthy
  • Date: 2007-12-17 04:49:20 UTC
  • mfrom: (3089.3.17 bzr.ug-tweaks)
  • mto: This revision was merged to the branch mainline in revision 3120.
  • Revision ID: ian.clatworthy@internode.on.net-20071217044920-8fjh9v6m1t93c8dc
Move material out of User Guide into User Reference (Ian Clatworthy)

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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
from bzrlib.lazy_import import lazy_import
18
18
 
23
23
from StringIO import StringIO
24
24
 
25
25
from bzrlib import (
26
 
    errors,
27
26
    patiencediff,
28
27
    trace,
29
28
    ui,
30
29
    )
31
 
from bzrlib import bencode
 
30
from bzrlib.util import bencode
32
31
""")
33
 
from gzip import GzipFile
34
 
 
35
 
 
36
 
def topo_iter_keys(vf, keys=None):
37
 
    if keys is None:
38
 
        keys = vf.keys()
39
 
    parents = vf.get_parent_map(keys)
40
 
    return _topo_iter(parents, keys)
 
32
from bzrlib.tuned_gzip import GzipFile
 
33
 
41
34
 
42
35
def topo_iter(vf, versions=None):
 
36
    seen = set()
 
37
    descendants = {}
43
38
    if versions is None:
44
39
        versions = vf.versions()
45
 
    parents = vf.get_parent_map(versions)
46
 
    return _topo_iter(parents, versions)
47
 
 
48
 
def _topo_iter(parents, versions):
49
 
    seen = set()
50
 
    descendants = {}
51
40
    def pending_parents(version):
52
 
        if parents[version] is None:
53
 
            return []
54
 
        return [v for v in parents[version] if v in versions and
 
41
        return [v for v in vf.get_parents(version) if v in versions and
55
42
                v not in seen]
56
43
    for version_id in versions:
57
 
        if parents[version_id] is None:
58
 
            # parentless
59
 
            continue
60
 
        for parent_id in parents[version_id]:
 
44
        for parent_id in vf.get_parents(version_id):
61
45
            descendants.setdefault(parent_id, []).append(version_id)
62
46
    cur = [v for v in versions if len(pending_parents(v)) == 0]
63
47
    while len(cur) > 0:
71
55
            yield version_id
72
56
            seen.add(version_id)
73
57
        cur = next
 
58
    assert len(seen) == len(versions)
74
59
 
75
60
 
76
61
class MultiParent(object):
77
62
    """A multi-parent diff"""
78
63
 
79
 
    __slots__ = ['hunks']
80
 
 
81
64
    def __init__(self, hunks=None):
82
65
        if hunks is not None:
83
66
            self.hunks = hunks
210
193
            elif cur_line[0] == '\n':
211
194
                hunks[-1].lines[-1] += '\n'
212
195
            else:
213
 
                if not (cur_line[0] == 'c'):
214
 
                    raise AssertionError(cur_line[0])
 
196
                assert cur_line[0] == 'c', cur_line[0]
215
197
                parent, parent_pos, child_pos, num_lines =\
216
198
                    [int(v) for v in cur_line.split(' ')[1:]]
217
199
                hunks.append(ParentText(parent, parent_pos, child_pos,
260
242
class NewText(object):
261
243
    """The contents of text that is introduced by this text"""
262
244
 
263
 
    __slots__ = ['lines']
264
 
 
265
245
    def __init__(self, lines):
266
246
        self.lines = lines
267
247
 
283
263
class ParentText(object):
284
264
    """A reference to text present in a parent text"""
285
265
 
286
 
    __slots__ = ['parent', 'parent_pos', 'child_pos', 'num_lines']
287
 
 
288
266
    def __init__(self, parent, parent_pos, child_pos, num_lines):
289
267
        self.parent = parent
290
268
        self.parent_pos = parent_pos
291
269
        self.child_pos = child_pos
292
270
        self.num_lines = num_lines
293
271
 
294
 
    def _as_dict(self):
295
 
        return dict(parent=self.parent, parent_pos=self.parent_pos,
296
 
                    child_pos=self.child_pos, num_lines=self.num_lines)
297
 
 
298
272
    def __repr__(self):
299
 
        return ('ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'
300
 
                ' %(num_lines)r)' % self._as_dict())
 
273
        return 'ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'\
 
274
            ' %(num_lines)r)' % self.__dict__
301
275
 
302
276
    def __eq__(self, other):
303
 
        if self.__class__ is not other.__class__:
 
277
        if self.__class__ != other.__class__:
304
278
            return False
305
 
        return self._as_dict() == other._as_dict()
 
279
        return (self.__dict__ == other.__dict__)
306
280
 
307
281
    def to_patch(self):
308
 
        yield ('c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'
309
 
               % self._as_dict())
 
282
        yield 'c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'\
 
283
            % self.__dict__
310
284
 
311
285
 
312
286
class BaseVersionedFile(object):
326
300
        return version in self._parents
327
301
 
328
302
    def do_snapshot(self, version_id, parent_ids):
329
 
        """Determine whether to perform a snapshot for this version"""
 
303
        """Determine whether to perform a a snapshot for this version"""
330
304
        if self.snapshot_interval is None:
331
305
            return False
332
306
        if self.max_snapshots is not None and\
394
368
        :param single_parent: If true, omit all but one parent text, (but
395
369
            retain parent metadata).
396
370
        """
397
 
        if not (no_cache or not verify):
398
 
            raise ValueError()
 
371
        assert no_cache or not verify
399
372
        revisions = set(vf.versions())
400
373
        total = len(revisions)
401
374
        pb = ui.ui_factory.nested_progress_bar()
407
380
                    if [p for p in parents if p not in self._parents] != []:
408
381
                        continue
409
382
                    lines = [a + ' ' + l for a, l in
410
 
                             vf.annotate(revision)]
 
383
                             vf.annotate_iter(revision)]
411
384
                    if snapshots is None:
412
385
                        force_snapshot = None
413
386
                    else:
419
392
                        self.clear_cache()
420
393
                        vf.clear_cache()
421
394
                        if verify:
422
 
                            if not (lines == self.get_line_list([revision])[0]):
423
 
                                raise AssertionError()
 
395
                            assert lines == self.get_line_list([revision])[0]
424
396
                            self.clear_cache()
425
397
                    pb.update('Importing revisions',
426
398
                              (total - len(revisions)) + len(added), total)
536
508
        self._parents[version_id] = parent_ids
537
509
 
538
510
    def get_diff(self, version_id):
539
 
        try:
540
 
            return self._diffs[version_id]
541
 
        except KeyError:
542
 
            raise errors.RevisionNotPresent(version_id, self)
 
511
        return self._diffs[version_id]
543
512
 
544
513
    def destroy(self):
545
514
        self._diffs = {}
564
533
        zip_file = GzipFile(None, mode='rb', fileobj=sio)
565
534
        try:
566
535
            file_version_id = zip_file.readline()
567
 
            content = zip_file.read()
568
 
            return MultiParent.from_patch(content)
 
536
            return MultiParent.from_patch(zip_file.read())
569
537
        finally:
570
538
            zip_file.close()
571
539