~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/multiparent.py

  • Committer: Aaron Bentley
  • Date: 2007-06-11 14:59:52 UTC
  • mto: (2520.5.2 bzr.mpbundle)
  • mto: This revision was merged to the branch mainline in revision 2631.
  • Revision ID: abentley@panoramicfeedback.com-20070611145952-cwt4480gphdhen6l
Get installation started

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
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
16
 
 
17
1
from bzrlib.lazy_import import lazy_import
18
2
 
19
3
lazy_import(globals(), """
23
7
from StringIO import StringIO
24
8
 
25
9
from bzrlib import (
26
 
    errors,
27
10
    patiencediff,
28
11
    trace,
29
12
    ui,
30
13
    )
31
 
from bzrlib import bencode
 
14
from bzrlib.util import bencode
32
15
""")
33
16
from bzrlib.tuned_gzip import GzipFile
34
17
 
35
18
 
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
 
def topo_iter(vf, versions=None):
43
 
    if versions is None:
44
 
        versions = vf.versions()
45
 
    parents = vf.get_parent_map(versions)
46
 
    return _topo_iter(parents, versions)
47
 
 
48
 
def _topo_iter(parents, versions):
 
19
def topo_iter(vf):
49
20
    seen = set()
50
21
    descendants = {}
51
 
    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
55
 
                v not in seen]
56
 
    for version_id in versions:
57
 
        if parents[version_id] is None:
58
 
            # parentless
59
 
            continue
60
 
        for parent_id in parents[version_id]:
 
22
    for version_id in vf.versions():
 
23
        for parent_id in vf.get_parents(version_id):
61
24
            descendants.setdefault(parent_id, []).append(version_id)
62
 
    cur = [v for v in versions if len(pending_parents(v)) == 0]
 
25
    cur = [v for v in vf.versions() if len(vf.get_parents(v)) == 0]
63
26
    while len(cur) > 0:
64
27
        next = []
65
28
        for version_id in cur:
66
29
            if version_id in seen:
67
30
                continue
68
 
            if len(pending_parents(version_id)) != 0:
 
31
            parents = vf.get_parents(version_id)
 
32
            if not seen.issuperset(parents):
69
33
                continue
70
34
            next.extend(descendants.get(version_id, []))
71
35
            yield version_id
74
38
 
75
39
 
76
40
class MultiParent(object):
77
 
    """A multi-parent diff"""
78
41
 
79
42
    def __init__(self, hunks=None):
80
43
        if hunks is not None:
91
54
        return (self.hunks == other.hunks)
92
55
 
93
56
    @staticmethod
94
 
    def from_lines(text, parents=(), left_blocks=None):
 
57
    def from_lines(text, parents=()):
95
58
        """Produce a MultiParent from a list of lines and parents"""
96
59
        def compare(parent):
97
60
            matcher = patiencediff.PatienceSequenceMatcher(None, parent,
98
61
                                                           text)
99
62
            return matcher.get_matching_blocks()
100
 
        if len(parents) > 0:
101
 
            if left_blocks is None:
102
 
                left_blocks = compare(parents[0])
103
 
            parent_comparisons = [left_blocks] + [compare(p) for p in
104
 
                                                  parents[1:]]
105
 
        else:
106
 
            parent_comparisons = []
 
63
        parent_comparisons = [compare(p) for p in parents]
107
64
        cur_line = 0
108
65
        new_text = NewText([])
109
66
        parent_text = []
121
78
                if block is None:
122
79
                    continue
123
80
                i, j, n = block
124
 
                while j + n <= cur_line:
 
81
                while j + n < cur_line:
125
82
                    block = cur_block[p] = next_block(p)
126
83
                    if block is None:
127
84
                        break
151
108
            diff.hunks.append(new_text)
152
109
        return diff
153
110
 
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
 
    def to_lines(self, parents=()):
162
 
        """Contruct a fulltext from this diff and its parents"""
163
 
        mpvf = MultiMemoryVersionedFile()
164
 
        for num, parent in enumerate(parents):
165
 
            mpvf.add_version(StringIO(parent).readlines(), num, [])
166
 
        mpvf.add_diff(self, 'a', range(len(parents)))
167
 
        return mpvf.get_line_list(['a'])[0]
168
 
 
169
111
    @classmethod
170
112
    def from_texts(cls, text, parents=()):
171
113
        """Produce a MultiParent from a text and list of parent text"""
172
 
        return cls.from_lines(StringIO(text).readlines(),
173
 
                              [StringIO(p).readlines() for p in parents])
 
114
        return cls.from_lines(text.splitlines(True),
 
115
                              [p.splitlines(True) for p in parents])
174
116
 
175
117
    def to_patch(self):
176
118
        """Yield text lines for a patch"""
184
126
    def zipped_patch_len(self):
185
127
        return len(gzip_string(self.to_patch()))
186
128
 
187
 
    @classmethod
188
 
    def from_patch(cls, text):
189
 
        """Create a MultiParent from its string form"""
190
 
        return cls._from_patch(StringIO(text))
191
 
 
192
129
    @staticmethod
193
 
    def _from_patch(lines):
194
 
        """This is private because it is essential to split lines on \n only"""
 
130
    def from_patch(lines):
 
131
        """Produce a MultiParent from a sequence of lines"""
195
132
        line_iter = iter(lines)
196
133
        hunks = []
197
134
        cur_line = None
208
145
            elif cur_line[0] == '\n':
209
146
                hunks[-1].lines[-1] += '\n'
210
147
            else:
211
 
                if not (cur_line[0] == 'c'):
212
 
                    raise AssertionError(cur_line[0])
 
148
                assert cur_line[0] == 'c', cur_line[0]
213
149
                parent, parent_pos, child_pos, num_lines =\
214
150
                    [int(v) for v in cur_line.split(' ')[1:]]
215
151
                hunks.append(ParentText(parent, parent_pos, child_pos,
240
176
            start = end
241
177
 
242
178
    def num_lines(self):
243
 
        """The number of lines in the output text"""
244
179
        extra_n = 0
245
180
        for hunk in reversed(self.hunks):
246
181
            if isinstance(hunk, ParentText):
249
184
        return extra_n
250
185
 
251
186
    def is_snapshot(self):
252
 
        """Return true of this hunk is effectively a fulltext"""
253
187
        if len(self.hunks) != 1:
254
188
            return False
255
189
        return (isinstance(self.hunks[0], NewText))
290
224
            ' %(num_lines)r)' % self.__dict__
291
225
 
292
226
    def __eq__(self, other):
293
 
        if self.__class__ is not other.__class__:
 
227
        if self.__class__ != other.__class__:
294
228
            return False
295
229
        return (self.__dict__ == other.__dict__)
296
230
 
300
234
 
301
235
 
302
236
class BaseVersionedFile(object):
303
 
    """Pseudo-VersionedFile skeleton for MultiParent"""
 
237
    """VersionedFile skeleton for MultiParent"""
304
238
 
305
239
    def __init__(self, snapshot_interval=25, max_snapshots=None):
306
240
        self._lines = {}
312
246
    def versions(self):
313
247
        return iter(self._parents)
314
248
 
315
 
    def has_version(self, version):
316
 
        return version in self._parents
317
 
 
318
249
    def do_snapshot(self, version_id, parent_ids):
319
 
        """Determine whether to perform a snapshot for this version"""
320
250
        if self.snapshot_interval is None:
321
251
            return False
322
252
        if self.max_snapshots is not None and\
337
267
 
338
268
    def add_version(self, lines, version_id, parent_ids,
339
269
                    force_snapshot=None, single_parent=False):
340
 
        """Add a version to the versionedfile
341
 
 
342
 
        :param lines: The list of lines to add.  Must be split on '\n'.
343
 
        :param version_id: The version_id of the version to add
344
 
        :param force_snapshot: If true, force this version to be added as a
345
 
            snapshot version.  If false, force this version to be added as a
346
 
            diff.  If none, determine this automatically.
347
 
        :param single_parent: If true, use a single parent, rather than
348
 
            multiple parents.
349
 
        """
350
270
        if force_snapshot is None:
351
271
            do_snapshot = self.do_snapshot(version_id, parent_ids)
352
272
        else:
384
304
        :param single_parent: If true, omit all but one parent text, (but
385
305
            retain parent metadata).
386
306
        """
387
 
        if not (no_cache or not verify):
388
 
            raise ValueError()
 
307
        assert no_cache or not verify
389
308
        revisions = set(vf.versions())
390
309
        total = len(revisions)
391
310
        pb = ui.ui_factory.nested_progress_bar()
397
316
                    if [p for p in parents if p not in self._parents] != []:
398
317
                        continue
399
318
                    lines = [a + ' ' + l for a, l in
400
 
                             vf.annotate(revision)]
 
319
                             vf.annotate_iter(revision)]
401
320
                    if snapshots is None:
402
321
                        force_snapshot = None
403
322
                    else:
409
328
                        self.clear_cache()
410
329
                        vf.clear_cache()
411
330
                        if verify:
412
 
                            if not (lines == self.get_line_list([revision])[0]):
413
 
                                raise AssertionError()
 
331
                            assert lines == self.get_line_list([revision])[0]
414
332
                            self.clear_cache()
415
333
                    pb.update('Importing revisions',
416
334
                              (total - len(revisions)) + len(added), total)
419
337
            pb.finished()
420
338
 
421
339
    def select_snapshots(self, vf):
422
 
        """Determine which versions to add as snapshots"""
423
340
        build_ancestors = {}
424
341
        descendants = {}
425
342
        snapshots = set()
446
363
        return [v for n, v in new_snapshots]
447
364
 
448
365
    def get_size_ranking(self):
449
 
        """Get versions ranked by size"""
450
366
        versions = []
451
367
        new_snapshots = set()
452
368
        for version_id in self.versions():
458
374
            versions.append((snapshot_len - diff_len, version_id))
459
375
        versions.sort()
460
376
        return versions
 
377
        return [v for n, v in versions]
461
378
 
462
379
    def import_diffs(self, vf):
463
 
        """Import the diffs from another pseudo-versionedfile"""
464
380
        for version_id in vf.versions():
465
381
            self.add_diff(vf.get_diff(version_id), version_id,
466
382
                          vf._parents[version_id])
467
383
 
468
384
    def get_build_ranking(self):
469
 
        """Return revisions sorted by how much they reduce build complexity"""
470
385
        could_avoid = {}
471
386
        referenced_by = {}
472
387
        for version_id in topo_iter(self):
508
423
            pass
509
424
        diff = self.get_diff(version_id)
510
425
        lines = []
511
 
        reconstructor = _Reconstructor(self, self._lines, self._parents)
 
426
        reconstructor = _Reconstructor(self, self._lines,
 
427
                                       self._parents)
512
428
        reconstructor.reconstruct_version(lines, version_id)
513
429
        self._lines[version_id] = lines
514
430
        return lines
515
431
 
516
432
 
517
433
class MultiMemoryVersionedFile(BaseVersionedFile):
518
 
    """Memory-backed pseudo-versionedfile"""
519
434
 
520
435
    def __init__(self, snapshot_interval=25, max_snapshots=None):
521
436
        BaseVersionedFile.__init__(self, snapshot_interval, max_snapshots)
526
441
        self._parents[version_id] = parent_ids
527
442
 
528
443
    def get_diff(self, version_id):
529
 
        try:
530
 
            return self._diffs[version_id]
531
 
        except KeyError:
532
 
            raise errors.RevisionNotPresent(version_id, self)
 
444
        return self._diffs[version_id]
533
445
 
534
446
    def destroy(self):
535
447
        self._diffs = {}
536
448
 
537
449
 
538
450
class MultiVersionedFile(BaseVersionedFile):
539
 
    """Disk-backed pseudo-versionedfile"""
540
451
 
541
452
    def __init__(self, filename, snapshot_interval=25, max_snapshots=None):
542
453
        BaseVersionedFile.__init__(self, snapshot_interval, max_snapshots)
554
465
        zip_file = GzipFile(None, mode='rb', fileobj=sio)
555
466
        try:
556
467
            file_version_id = zip_file.readline()
557
 
            return MultiParent.from_patch(zip_file.read())
 
468
            return MultiParent.from_patch(zip_file.readlines())
558
469
        finally:
559
470
            zip_file.close()
560
471
 
561
472
    def add_diff(self, diff, version_id, parent_ids):
562
473
        outfile = open(self._filename + '.mpknit', 'ab')
563
474
        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
475
            start = outfile.tell()
568
476
            try:
569
477
                zipfile = GzipFile(None, mode='ab', fileobj=outfile)
618
526
    def _reconstruct(self, lines, req_version_id, req_start, req_end):
619
527
        """Append lines for the requested version_id range"""
620
528
        # stack of pending range requests
621
 
        if req_start == req_end:
622
 
            return
623
529
        pending_reqs = [(req_version_id, req_start, req_end)]
624
530
        while len(pending_reqs) > 0:
625
531
            req_version_id, req_start, req_end = pending_reqs.pop()
626
532
            # 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
533
            try:
631
534
                start, end, kind, data, iterator = self.cursor[req_version_id]
632
535
            except KeyError: