~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/multiparent.py

  • Committer: Daniel Watkins
  • Date: 2007-08-02 18:04:35 UTC
  • mto: This revision was merged to the branch mainline in revision 2705.
  • Revision ID: d.m.watkins@warwick.ac.uk-20070802180435-cs6xalg5tkze209b
tests.blackbox.test_aliases now uses internals where appropriate.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
32
from bzrlib.tuned_gzip import GzipFile
34
33
 
35
34
 
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)
41
 
 
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):
121
106
                if block is None:
122
107
                    continue
123
108
                i, j, n = block
124
 
                while j + n <= cur_line:
 
109
                while j + n < cur_line:
125
110
                    block = cur_block[p] = next_block(p)
126
111
                    if block is None:
127
112
                        break
151
136
            diff.hunks.append(new_text)
152
137
        return diff
153
138
 
154
 
    def get_matching_blocks(self, parent, parent_len):
155
 
        for hunk in self.hunks:
156
 
            if not isinstance(hunk, ParentText) or hunk.parent != parent:
157
 
                continue
158
 
            yield (hunk.parent_pos, hunk.child_pos, hunk.num_lines)
159
 
        yield parent_len, self.num_lines(), 0
160
 
 
161
139
    def to_lines(self, parents=()):
162
140
        """Contruct a fulltext from this diff and its parents"""
163
141
        mpvf = MultiMemoryVersionedFile()
208
186
            elif cur_line[0] == '\n':
209
187
                hunks[-1].lines[-1] += '\n'
210
188
            else:
211
 
                if not (cur_line[0] == 'c'):
212
 
                    raise AssertionError(cur_line[0])
 
189
                assert cur_line[0] == 'c', cur_line[0]
213
190
                parent, parent_pos, child_pos, num_lines =\
214
191
                    [int(v) for v in cur_line.split(' ')[1:]]
215
192
                hunks.append(ParentText(parent, parent_pos, child_pos,
290
267
            ' %(num_lines)r)' % self.__dict__
291
268
 
292
269
    def __eq__(self, other):
293
 
        if self.__class__ is not other.__class__:
 
270
        if self.__class__ != other.__class__:
294
271
            return False
295
272
        return (self.__dict__ == other.__dict__)
296
273
 
316
293
        return version in self._parents
317
294
 
318
295
    def do_snapshot(self, version_id, parent_ids):
319
 
        """Determine whether to perform a snapshot for this version"""
 
296
        """Determine whether to perform a a snapshot for this version"""
320
297
        if self.snapshot_interval is None:
321
298
            return False
322
299
        if self.max_snapshots is not None and\
384
361
        :param single_parent: If true, omit all but one parent text, (but
385
362
            retain parent metadata).
386
363
        """
387
 
        if not (no_cache or not verify):
388
 
            raise ValueError()
 
364
        assert no_cache or not verify
389
365
        revisions = set(vf.versions())
390
366
        total = len(revisions)
391
367
        pb = ui.ui_factory.nested_progress_bar()
397
373
                    if [p for p in parents if p not in self._parents] != []:
398
374
                        continue
399
375
                    lines = [a + ' ' + l for a, l in
400
 
                             vf.annotate(revision)]
 
376
                             vf.annotate_iter(revision)]
401
377
                    if snapshots is None:
402
378
                        force_snapshot = None
403
379
                    else:
409
385
                        self.clear_cache()
410
386
                        vf.clear_cache()
411
387
                        if verify:
412
 
                            if not (lines == self.get_line_list([revision])[0]):
413
 
                                raise AssertionError()
 
388
                            assert lines == self.get_line_list([revision])[0]
414
389
                            self.clear_cache()
415
390
                    pb.update('Importing revisions',
416
391
                              (total - len(revisions)) + len(added), total)
508
483
            pass
509
484
        diff = self.get_diff(version_id)
510
485
        lines = []
511
 
        reconstructor = _Reconstructor(self, self._lines, self._parents)
 
486
        reconstructor = _Reconstructor(self, self._lines,
 
487
                                       self._parents)
512
488
        reconstructor.reconstruct_version(lines, version_id)
513
489
        self._lines[version_id] = lines
514
490
        return lines
526
502
        self._parents[version_id] = parent_ids
527
503
 
528
504
    def get_diff(self, version_id):
529
 
        try:
530
 
            return self._diffs[version_id]
531
 
        except KeyError:
532
 
            raise errors.RevisionNotPresent(version_id, self)
 
505
        return self._diffs[version_id]
533
506
 
534
507
    def destroy(self):
535
508
        self._diffs = {}
561
534
    def add_diff(self, diff, version_id, parent_ids):
562
535
        outfile = open(self._filename + '.mpknit', 'ab')
563
536
        try:
564
 
            outfile.seek(0, 2)      # workaround for windows bug:
565
 
                                    # .tell() for files opened in 'ab' mode
566
 
                                    # before any write returns 0
567
537
            start = outfile.tell()
568
538
            try:
569
539
                zipfile = GzipFile(None, mode='ab', fileobj=outfile)
624
594
        while len(pending_reqs) > 0:
625
595
            req_version_id, req_start, req_end = pending_reqs.pop()
626
596
            # lazily allocate cursors for versions
627
 
            if req_version_id in self.lines:
628
 
                lines.extend(self.lines[req_version_id][req_start:req_end])
629
 
                continue
630
597
            try:
631
598
                start, end, kind, data, iterator = self.cursor[req_version_id]
632
599
            except KeyError: