~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/versionedfile.py

  • Committer: Martin Pool
  • Date: 2007-04-04 06:17:31 UTC
  • mto: This revision was merged to the branch mainline in revision 2397.
  • Revision ID: mbp@sourcefrog.net-20070404061731-tt2xrzllqhbodn83
Contents of TODO file moved into bug tracker

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
3
3
# Authors:
4
4
#   Johan Rydberg <jrydberg@gnu.org>
19
19
 
20
20
"""Versioned text file storage api."""
21
21
 
22
 
from copy import copy
23
 
from cStringIO import StringIO
24
 
import os
25
 
from zlib import adler32
26
 
 
27
22
from bzrlib.lazy_import import lazy_import
28
23
lazy_import(globals(), """
29
 
import urllib
 
24
from copy import deepcopy
 
25
import unittest
30
26
 
31
27
from bzrlib import (
32
28
    errors,
33
 
    index,
34
29
    osutils,
35
 
    multiparent,
36
30
    tsort,
37
31
    revision,
38
32
    ui,
39
33
    )
40
 
from bzrlib.graph import DictParentsProvider, Graph, _StackedParentsProvider
41
34
from bzrlib.transport.memory import MemoryTransport
42
35
""")
 
36
 
43
37
from bzrlib.inter import InterObject
44
 
from bzrlib.registry import Registry
45
 
from bzrlib.symbol_versioning import *
46
38
from bzrlib.textmerge import TextMerge
47
 
 
48
 
 
49
 
adapter_registry = Registry()
50
 
adapter_registry.register_lazy(('knit-delta-gz', 'fulltext'), 'bzrlib.knit',
51
 
    'DeltaPlainToFullText')
52
 
adapter_registry.register_lazy(('knit-ft-gz', 'fulltext'), 'bzrlib.knit',
53
 
    'FTPlainToFullText')
54
 
adapter_registry.register_lazy(('knit-annotated-delta-gz', 'knit-delta-gz'),
55
 
    'bzrlib.knit', 'DeltaAnnotatedToUnannotated')
56
 
adapter_registry.register_lazy(('knit-annotated-delta-gz', 'fulltext'),
57
 
    'bzrlib.knit', 'DeltaAnnotatedToFullText')
58
 
adapter_registry.register_lazy(('knit-annotated-ft-gz', 'knit-ft-gz'),
59
 
    'bzrlib.knit', 'FTAnnotatedToUnannotated')
60
 
adapter_registry.register_lazy(('knit-annotated-ft-gz', 'fulltext'),
61
 
    'bzrlib.knit', 'FTAnnotatedToFullText')
62
 
# adapter_registry.register_lazy(('knit-annotated-ft-gz', 'chunked'),
63
 
#     'bzrlib.knit', 'FTAnnotatedToChunked')
64
 
 
65
 
 
66
 
class ContentFactory(object):
67
 
    """Abstract interface for insertion and retrieval from a VersionedFile.
68
 
    
69
 
    :ivar sha1: None, or the sha1 of the content fulltext.
70
 
    :ivar storage_kind: The native storage kind of this factory. One of
71
 
        'mpdiff', 'knit-annotated-ft', 'knit-annotated-delta', 'knit-ft',
72
 
        'knit-delta', 'fulltext', 'knit-annotated-ft-gz',
73
 
        'knit-annotated-delta-gz', 'knit-ft-gz', 'knit-delta-gz'.
74
 
    :ivar key: The key of this content. Each key is a tuple with a single
75
 
        string in it.
76
 
    :ivar parents: A tuple of parent keys for self.key. If the object has
77
 
        no parent information, None (as opposed to () for an empty list of
78
 
        parents).
79
 
    """
80
 
 
81
 
    def __init__(self):
82
 
        """Create a ContentFactory."""
83
 
        self.sha1 = None
84
 
        self.storage_kind = None
85
 
        self.key = None
86
 
        self.parents = None
87
 
 
88
 
 
89
 
class ChunkedContentFactory(ContentFactory):
90
 
    """Static data content factory.
91
 
 
92
 
    This takes a 'chunked' list of strings. The only requirement on 'chunked' is
93
 
    that ''.join(lines) becomes a valid fulltext. A tuple of a single string
94
 
    satisfies this, as does a list of lines.
95
 
 
96
 
    :ivar sha1: None, or the sha1 of the content fulltext.
97
 
    :ivar storage_kind: The native storage kind of this factory. Always
98
 
        'chunked'
99
 
    :ivar key: The key of this content. Each key is a tuple with a single
100
 
        string in it.
101
 
    :ivar parents: A tuple of parent keys for self.key. If the object has
102
 
        no parent information, None (as opposed to () for an empty list of
103
 
        parents).
104
 
     """
105
 
 
106
 
    def __init__(self, key, parents, sha1, chunks):
107
 
        """Create a ContentFactory."""
108
 
        self.sha1 = sha1
109
 
        self.storage_kind = 'chunked'
110
 
        self.key = key
111
 
        self.parents = parents
112
 
        self._chunks = chunks
113
 
 
114
 
    def get_bytes_as(self, storage_kind):
115
 
        if storage_kind == 'chunked':
116
 
            return self._chunks
117
 
        elif storage_kind == 'fulltext':
118
 
            return ''.join(self._chunks)
119
 
        raise errors.UnavailableRepresentation(self.key, storage_kind,
120
 
            self.storage_kind)
121
 
 
122
 
 
123
 
class FulltextContentFactory(ContentFactory):
124
 
    """Static data content factory.
125
 
 
126
 
    This takes a fulltext when created and just returns that during
127
 
    get_bytes_as('fulltext').
128
 
 
129
 
    :ivar sha1: None, or the sha1 of the content fulltext.
130
 
    :ivar storage_kind: The native storage kind of this factory. Always
131
 
        'fulltext'.
132
 
    :ivar key: The key of this content. Each key is a tuple with a single
133
 
        string in it.
134
 
    :ivar parents: A tuple of parent keys for self.key. If the object has
135
 
        no parent information, None (as opposed to () for an empty list of
136
 
        parents).
137
 
     """
138
 
 
139
 
    def __init__(self, key, parents, sha1, text):
140
 
        """Create a ContentFactory."""
141
 
        self.sha1 = sha1
142
 
        self.storage_kind = 'fulltext'
143
 
        self.key = key
144
 
        self.parents = parents
145
 
        self._text = text
146
 
 
147
 
    def get_bytes_as(self, storage_kind):
148
 
        if storage_kind == self.storage_kind:
149
 
            return self._text
150
 
        elif storage_kind == 'chunked':
151
 
            return [self._text]
152
 
        raise errors.UnavailableRepresentation(self.key, storage_kind,
153
 
            self.storage_kind)
154
 
 
155
 
 
156
 
class AbsentContentFactory(ContentFactory):
157
 
    """A placeholder content factory for unavailable texts.
158
 
    
159
 
    :ivar sha1: None.
160
 
    :ivar storage_kind: 'absent'.
161
 
    :ivar key: The key of this content. Each key is a tuple with a single
162
 
        string in it.
163
 
    :ivar parents: None.
164
 
    """
165
 
 
166
 
    def __init__(self, key):
167
 
        """Create a ContentFactory."""
168
 
        self.sha1 = None
169
 
        self.storage_kind = 'absent'
170
 
        self.key = key
171
 
        self.parents = None
172
 
 
173
 
 
174
 
class AdapterFactory(ContentFactory):
175
 
    """A content factory to adapt between key prefix's."""
176
 
 
177
 
    def __init__(self, key, parents, adapted):
178
 
        """Create an adapter factory instance."""
179
 
        self.key = key
180
 
        self.parents = parents
181
 
        self._adapted = adapted
182
 
 
183
 
    def __getattr__(self, attr):
184
 
        """Return a member from the adapted object."""
185
 
        if attr in ('key', 'parents'):
186
 
            return self.__dict__[attr]
187
 
        else:
188
 
            return getattr(self._adapted, attr)
189
 
 
190
 
 
191
 
def filter_absent(record_stream):
192
 
    """Adapt a record stream to remove absent records."""
193
 
    for record in record_stream:
194
 
        if record.storage_kind != 'absent':
195
 
            yield record
 
39
from bzrlib.symbol_versioning import (deprecated_function,
 
40
        deprecated_method,
 
41
        zero_eight,
 
42
        )
196
43
 
197
44
 
198
45
class VersionedFile(object):
209
56
    Texts are identified by a version-id string.
210
57
    """
211
58
 
 
59
    def __init__(self, access_mode):
 
60
        self.finished = False
 
61
        self._access_mode = access_mode
 
62
 
212
63
    @staticmethod
213
64
    def check_not_reserved_id(version_id):
214
65
        revision.check_not_reserved_id(version_id)
217
68
        """Copy this versioned file to name on transport."""
218
69
        raise NotImplementedError(self.copy_to)
219
70
 
220
 
    def get_record_stream(self, versions, ordering, include_delta_closure):
221
 
        """Get a stream of records for versions.
 
71
    @deprecated_method(zero_eight)
 
72
    def names(self):
 
73
        """Return a list of all the versions in this versioned file.
222
74
 
223
 
        :param versions: The versions to include. Each version is a tuple
224
 
            (version,).
225
 
        :param ordering: Either 'unordered' or 'topological'. A topologically
226
 
            sorted stream has compression parents strictly before their
227
 
            children.
228
 
        :param include_delta_closure: If True then the closure across any
229
 
            compression parents will be included (in the data content of the
230
 
            stream, not in the emitted records). This guarantees that
231
 
            'fulltext' can be used successfully on every record.
232
 
        :return: An iterator of ContentFactory objects, each of which is only
233
 
            valid until the iterator is advanced.
 
75
        Please use versionedfile.versions() now.
234
76
        """
235
 
        raise NotImplementedError(self.get_record_stream)
 
77
        return self.versions()
 
78
 
 
79
    def versions(self):
 
80
        """Return a unsorted list of versions."""
 
81
        raise NotImplementedError(self.versions)
 
82
 
 
83
    def has_ghost(self, version_id):
 
84
        """Returns whether version is present as a ghost."""
 
85
        raise NotImplementedError(self.has_ghost)
236
86
 
237
87
    def has_version(self, version_id):
238
88
        """Returns whether version is present."""
239
89
        raise NotImplementedError(self.has_version)
240
90
 
241
 
    def insert_record_stream(self, stream):
242
 
        """Insert a record stream into this versioned file.
243
 
 
244
 
        :param stream: A stream of records to insert. 
245
 
        :return: None
246
 
        :seealso VersionedFile.get_record_stream:
247
 
        """
248
 
        raise NotImplementedError
249
 
 
250
 
    def add_lines(self, version_id, parents, lines, parent_texts=None,
251
 
        left_matching_blocks=None, nostore_sha=None, random_id=False,
252
 
        check_content=True):
 
91
    def add_delta(self, version_id, parents, delta_parent, sha1, noeol, delta):
 
92
        """Add a text to the versioned file via a pregenerated delta.
 
93
 
 
94
        :param version_id: The version id being added.
 
95
        :param parents: The parents of the version_id.
 
96
        :param delta_parent: The parent this delta was created against.
 
97
        :param sha1: The sha1 of the full text.
 
98
        :param delta: The delta instructions. See get_delta for details.
 
99
        """
 
100
        version_id = osutils.safe_revision_id(version_id)
 
101
        parents = [osutils.safe_revision_id(v) for v in parents]
 
102
        self._check_write_ok()
 
103
        if self.has_version(version_id):
 
104
            raise errors.RevisionAlreadyPresent(version_id, self)
 
105
        return self._add_delta(version_id, parents, delta_parent, sha1, noeol, delta)
 
106
 
 
107
    def _add_delta(self, version_id, parents, delta_parent, sha1, noeol, delta):
 
108
        """Class specific routine to add a delta.
 
109
 
 
110
        This generic version simply applies the delta to the delta_parent and
 
111
        then inserts it.
 
112
        """
 
113
        # strip annotation from delta
 
114
        new_delta = []
 
115
        for start, stop, delta_len, delta_lines in delta:
 
116
            new_delta.append((start, stop, delta_len, [text for origin, text in delta_lines]))
 
117
        if delta_parent is not None:
 
118
            parent_full = self.get_lines(delta_parent)
 
119
        else:
 
120
            parent_full = []
 
121
        new_full = self._apply_delta(parent_full, new_delta)
 
122
        # its impossible to have noeol on an empty file
 
123
        if noeol and new_full[-1][-1] == '\n':
 
124
            new_full[-1] = new_full[-1][:-1]
 
125
        self.add_lines(version_id, parents, new_full)
 
126
 
 
127
    def add_lines(self, version_id, parents, lines, parent_texts=None):
253
128
        """Add a single text on top of the versioned file.
254
129
 
255
130
        Must raise RevisionAlreadyPresent if the new version is
257
132
 
258
133
        Must raise RevisionNotPresent if any of the given parents are
259
134
        not present in file history.
260
 
 
261
 
        :param lines: A list of lines. Each line must be a bytestring. And all
262
 
            of them except the last must be terminated with \n and contain no
263
 
            other \n's. The last line may either contain no \n's or a single
264
 
            terminated \n. If the lines list does meet this constraint the add
265
 
            routine may error or may succeed - but you will be unable to read
266
 
            the data back accurately. (Checking the lines have been split
267
 
            correctly is expensive and extremely unlikely to catch bugs so it
268
 
            is not done at runtime unless check_content is True.)
269
135
        :param parent_texts: An optional dictionary containing the opaque 
270
 
            representations of some or all of the parents of version_id to
271
 
            allow delta optimisations.  VERY IMPORTANT: the texts must be those
272
 
            returned by add_lines or data corruption can be caused.
273
 
        :param left_matching_blocks: a hint about which areas are common
274
 
            between the text and its left-hand-parent.  The format is
275
 
            the SequenceMatcher.get_matching_blocks format.
276
 
        :param nostore_sha: Raise ExistingContent and do not add the lines to
277
 
            the versioned file if the digest of the lines matches this.
278
 
        :param random_id: If True a random id has been selected rather than
279
 
            an id determined by some deterministic process such as a converter
280
 
            from a foreign VCS. When True the backend may choose not to check
281
 
            for uniqueness of the resulting key within the versioned file, so
282
 
            this should only be done when the result is expected to be unique
283
 
            anyway.
284
 
        :param check_content: If True, the lines supplied are verified to be
285
 
            bytestrings that are correctly formed lines.
286
 
        :return: The text sha1, the number of bytes in the text, and an opaque
287
 
                 representation of the inserted version which can be provided
288
 
                 back to future add_lines calls in the parent_texts dictionary.
 
136
             representations of some or all of the parents of 
 
137
             version_id to allow delta optimisations. 
 
138
             VERY IMPORTANT: the texts must be those returned
 
139
             by add_lines or data corruption can be caused.
 
140
        :return: An opaque representation of the inserted version which can be
 
141
                 provided back to future add_lines calls in the parent_texts
 
142
                 dictionary.
289
143
        """
 
144
        version_id = osutils.safe_revision_id(version_id)
 
145
        parents = [osutils.safe_revision_id(v) for v in parents]
290
146
        self._check_write_ok()
291
 
        return self._add_lines(version_id, parents, lines, parent_texts,
292
 
            left_matching_blocks, nostore_sha, random_id, check_content)
 
147
        return self._add_lines(version_id, parents, lines, parent_texts)
293
148
 
294
 
    def _add_lines(self, version_id, parents, lines, parent_texts,
295
 
        left_matching_blocks, nostore_sha, random_id, check_content):
 
149
    def _add_lines(self, version_id, parents, lines, parent_texts):
296
150
        """Helper to do the class specific add_lines."""
297
151
        raise NotImplementedError(self.add_lines)
298
152
 
299
153
    def add_lines_with_ghosts(self, version_id, parents, lines,
300
 
        parent_texts=None, nostore_sha=None, random_id=False,
301
 
        check_content=True, left_matching_blocks=None):
 
154
                              parent_texts=None):
302
155
        """Add lines to the versioned file, allowing ghosts to be present.
303
156
        
304
 
        This takes the same parameters as add_lines and returns the same.
 
157
        This takes the same parameters as add_lines.
305
158
        """
 
159
        version_id = osutils.safe_revision_id(version_id)
 
160
        parents = [osutils.safe_revision_id(v) for v in parents]
306
161
        self._check_write_ok()
307
162
        return self._add_lines_with_ghosts(version_id, parents, lines,
308
 
            parent_texts, nostore_sha, random_id, check_content, left_matching_blocks)
 
163
                                           parent_texts)
309
164
 
310
 
    def _add_lines_with_ghosts(self, version_id, parents, lines, parent_texts,
311
 
        nostore_sha, random_id, check_content, left_matching_blocks):
 
165
    def _add_lines_with_ghosts(self, version_id, parents, lines, parent_texts):
312
166
        """Helper to do class specific add_lines_with_ghosts."""
313
167
        raise NotImplementedError(self.add_lines_with_ghosts)
314
168
 
328
182
            if '\n' in line[:-1]:
329
183
                raise errors.BzrBadParameterContainsNewline("lines")
330
184
 
331
 
    def get_format_signature(self):
332
 
        """Get a text description of the data encoding in this file.
333
 
        
334
 
        :since: 0.90
335
 
        """
336
 
        raise NotImplementedError(self.get_format_signature)
337
 
 
338
 
    def make_mpdiffs(self, version_ids):
339
 
        """Create multiparent diffs for specified versions."""
340
 
        knit_versions = set()
341
 
        knit_versions.update(version_ids)
342
 
        parent_map = self.get_parent_map(version_ids)
343
 
        for version_id in version_ids:
344
 
            try:
345
 
                knit_versions.update(parent_map[version_id])
346
 
            except KeyError:
347
 
                raise errors.RevisionNotPresent(version_id, self)
348
 
        # We need to filter out ghosts, because we can't diff against them.
349
 
        knit_versions = set(self.get_parent_map(knit_versions).keys())
350
 
        lines = dict(zip(knit_versions,
351
 
            self._get_lf_split_line_list(knit_versions)))
352
 
        diffs = []
353
 
        for version_id in version_ids:
354
 
            target = lines[version_id]
355
 
            try:
356
 
                parents = [lines[p] for p in parent_map[version_id] if p in
357
 
                    knit_versions]
358
 
            except KeyError:
359
 
                # I don't know how this could ever trigger.
360
 
                # parent_map[version_id] was already triggered in the previous
361
 
                # for loop, and lines[p] has the 'if p in knit_versions' check,
362
 
                # so we again won't have a KeyError.
363
 
                raise errors.RevisionNotPresent(version_id, self)
364
 
            if len(parents) > 0:
365
 
                left_parent_blocks = self._extract_blocks(version_id,
366
 
                                                          parents[0], target)
367
 
            else:
368
 
                left_parent_blocks = None
369
 
            diffs.append(multiparent.MultiParent.from_lines(target, parents,
370
 
                         left_parent_blocks))
371
 
        return diffs
372
 
 
373
 
    def _extract_blocks(self, version_id, source, target):
374
 
        return None
375
 
 
376
 
    def add_mpdiffs(self, records):
377
 
        """Add mpdiffs to this VersionedFile.
378
 
 
379
 
        Records should be iterables of version, parents, expected_sha1,
380
 
        mpdiff. mpdiff should be a MultiParent instance.
381
 
        """
382
 
        # Does this need to call self._check_write_ok()? (IanC 20070919)
383
 
        vf_parents = {}
384
 
        mpvf = multiparent.MultiMemoryVersionedFile()
385
 
        versions = []
386
 
        for version, parent_ids, expected_sha1, mpdiff in records:
387
 
            versions.append(version)
388
 
            mpvf.add_diff(mpdiff, version, parent_ids)
389
 
        needed_parents = set()
390
 
        for version, parent_ids, expected_sha1, mpdiff in records:
391
 
            needed_parents.update(p for p in parent_ids
392
 
                                  if not mpvf.has_version(p))
393
 
        present_parents = set(self.get_parent_map(needed_parents).keys())
394
 
        for parent_id, lines in zip(present_parents,
395
 
                                 self._get_lf_split_line_list(present_parents)):
396
 
            mpvf.add_version(lines, parent_id, [])
397
 
        for (version, parent_ids, expected_sha1, mpdiff), lines in\
398
 
            zip(records, mpvf.get_line_list(versions)):
399
 
            if len(parent_ids) == 1:
400
 
                left_matching_blocks = list(mpdiff.get_matching_blocks(0,
401
 
                    mpvf.get_diff(parent_ids[0]).num_lines()))
402
 
            else:
403
 
                left_matching_blocks = None
404
 
            try:
405
 
                _, _, version_text = self.add_lines_with_ghosts(version,
406
 
                    parent_ids, lines, vf_parents,
407
 
                    left_matching_blocks=left_matching_blocks)
408
 
            except NotImplementedError:
409
 
                # The vf can't handle ghosts, so add lines normally, which will
410
 
                # (reasonably) fail if there are ghosts in the data.
411
 
                _, _, version_text = self.add_lines(version,
412
 
                    parent_ids, lines, vf_parents,
413
 
                    left_matching_blocks=left_matching_blocks)
414
 
            vf_parents[version] = version_text
415
 
        sha1s = self.get_sha1s(versions)
416
 
        for version, parent_ids, expected_sha1, mpdiff in records:
417
 
            if expected_sha1 != sha1s[version]:
418
 
                raise errors.VersionedFileInvalidChecksum(version)
419
 
 
 
185
    def _check_write_ok(self):
 
186
        """Is the versioned file marked as 'finished' ? Raise if it is."""
 
187
        if self.finished:
 
188
            raise errors.OutSideTransaction()
 
189
        if self._access_mode != 'w':
 
190
            raise errors.ReadOnlyObjectDirtiedError(self)
 
191
 
 
192
    def enable_cache(self):
 
193
        """Tell this versioned file that it should cache any data it reads.
 
194
        
 
195
        This is advisory, implementations do not have to support caching.
 
196
        """
 
197
        pass
 
198
    
 
199
    def clear_cache(self):
 
200
        """Remove any data cached in the versioned file object.
 
201
 
 
202
        This only needs to be supported if caches are supported
 
203
        """
 
204
        pass
 
205
 
 
206
    def clone_text(self, new_version_id, old_version_id, parents):
 
207
        """Add an identical text to old_version_id as new_version_id.
 
208
 
 
209
        Must raise RevisionNotPresent if the old version or any of the
 
210
        parents are not present in file history.
 
211
 
 
212
        Must raise RevisionAlreadyPresent if the new version is
 
213
        already present in file history."""
 
214
        new_version_id = osutils.safe_revision_id(new_version_id)
 
215
        old_version_id = osutils.safe_revision_id(old_version_id)
 
216
        self._check_write_ok()
 
217
        return self._clone_text(new_version_id, old_version_id, parents)
 
218
 
 
219
    def _clone_text(self, new_version_id, old_version_id, parents):
 
220
        """Helper function to do the _clone_text work."""
 
221
        raise NotImplementedError(self.clone_text)
 
222
 
 
223
    def create_empty(self, name, transport, mode=None):
 
224
        """Create a new versioned file of this exact type.
 
225
 
 
226
        :param name: the file name
 
227
        :param transport: the transport
 
228
        :param mode: optional file mode.
 
229
        """
 
230
        raise NotImplementedError(self.create_empty)
 
231
 
 
232
    def fix_parents(self, version_id, new_parents):
 
233
        """Fix the parents list for version.
 
234
        
 
235
        This is done by appending a new version to the index
 
236
        with identical data except for the parents list.
 
237
        the parents list must be a superset of the current
 
238
        list.
 
239
        """
 
240
        version_id = osutils.safe_revision_id(version_id)
 
241
        new_parents = [osutils.safe_revision_id(p) for p in new_parents]
 
242
        self._check_write_ok()
 
243
        return self._fix_parents(version_id, new_parents)
 
244
 
 
245
    def _fix_parents(self, version_id, new_parents):
 
246
        """Helper for fix_parents."""
 
247
        raise NotImplementedError(self.fix_parents)
 
248
 
 
249
    def get_delta(self, version):
 
250
        """Get a delta for constructing version from some other version.
 
251
        
 
252
        :return: (delta_parent, sha1, noeol, delta)
 
253
        Where delta_parent is a version id or None to indicate no parent.
 
254
        """
 
255
        raise NotImplementedError(self.get_delta)
 
256
 
 
257
    def get_deltas(self, version_ids):
 
258
        """Get multiple deltas at once for constructing versions.
 
259
        
 
260
        :return: dict(version_id:(delta_parent, sha1, noeol, delta))
 
261
        Where delta_parent is a version id or None to indicate no parent, and
 
262
        version_id is the version_id created by that delta.
 
263
        """
 
264
        result = {}
 
265
        for version_id in version_ids:
 
266
            result[version_id] = self.get_delta(version_id)
 
267
        return result
 
268
 
 
269
    def get_sha1(self, version_id):
 
270
        """Get the stored sha1 sum for the given revision.
 
271
        
 
272
        :param name: The name of the version to lookup
 
273
        """
 
274
        raise NotImplementedError(self.get_sha1)
 
275
 
 
276
    def get_suffixes(self):
 
277
        """Return the file suffixes associated with this versioned file."""
 
278
        raise NotImplementedError(self.get_suffixes)
 
279
    
420
280
    def get_text(self, version_id):
421
281
        """Return version contents as a text string.
422
282
 
442
302
        """
443
303
        raise NotImplementedError(self.get_lines)
444
304
 
445
 
    def _get_lf_split_line_list(self, version_ids):
446
 
        return [StringIO(t).readlines() for t in self.get_texts(version_ids)]
447
 
 
448
 
    def get_ancestry(self, version_ids, topo_sorted=True):
 
305
    def get_ancestry(self, version_ids):
449
306
        """Return a list of all ancestors of given version(s). This
450
307
        will not include the null revision.
451
308
 
452
 
        This list will not be topologically sorted if topo_sorted=False is
453
 
        passed.
454
 
 
455
309
        Must raise RevisionNotPresent if any of the given versions are
456
310
        not present in file history."""
457
311
        if isinstance(version_ids, basestring):
469
323
        but are not explicitly marked.
470
324
        """
471
325
        raise NotImplementedError(self.get_ancestry_with_ghosts)
472
 
    
473
 
    def get_parent_map(self, version_ids):
474
 
        """Get a map of the parents of version_ids.
475
 
 
476
 
        :param version_ids: The version ids to look up parents for.
477
 
        :return: A mapping from version id to parents.
478
 
        """
479
 
        raise NotImplementedError(self.get_parent_map)
 
326
        
 
327
    def get_graph(self, version_ids=None):
 
328
        """Return a graph from the versioned file. 
 
329
        
 
330
        Ghosts are not listed or referenced in the graph.
 
331
        :param version_ids: Versions to select.
 
332
                            None means retrieve all versions.
 
333
        """
 
334
        result = {}
 
335
        if version_ids is None:
 
336
            for version in self.versions():
 
337
                result[version] = self.get_parents(version)
 
338
        else:
 
339
            pending = set(osutils.safe_revision_id(v) for v in version_ids)
 
340
            while pending:
 
341
                version = pending.pop()
 
342
                if version in result:
 
343
                    continue
 
344
                parents = self.get_parents(version)
 
345
                for parent in parents:
 
346
                    if parent in result:
 
347
                        continue
 
348
                    pending.add(parent)
 
349
                result[version] = parents
 
350
        return result
 
351
 
 
352
    def get_graph_with_ghosts(self):
 
353
        """Return a graph for the entire versioned file.
 
354
        
 
355
        Ghosts are referenced in parents list but are not
 
356
        explicitly listed.
 
357
        """
 
358
        raise NotImplementedError(self.get_graph_with_ghosts)
 
359
 
 
360
    @deprecated_method(zero_eight)
 
361
    def parent_names(self, version):
 
362
        """Return version names for parents of a version.
 
363
        
 
364
        See get_parents for the current api.
 
365
        """
 
366
        return self.get_parents(version)
 
367
 
 
368
    def get_parents(self, version_id):
 
369
        """Return version names for parents of a version.
 
370
 
 
371
        Must raise RevisionNotPresent if version is not present in
 
372
        file history.
 
373
        """
 
374
        raise NotImplementedError(self.get_parents)
480
375
 
481
376
    def get_parents_with_ghosts(self, version_id):
482
377
        """Return version names for parents of version_id.
487
382
        Ghosts that are known about will be included in the parent list,
488
383
        but are not explicitly marked.
489
384
        """
490
 
        try:
491
 
            return list(self.get_parent_map([version_id])[version_id])
492
 
        except KeyError:
493
 
            raise errors.RevisionNotPresent(version_id, self)
 
385
        raise NotImplementedError(self.get_parents_with_ghosts)
 
386
 
 
387
    def annotate_iter(self, version_id):
 
388
        """Yield list of (version-id, line) pairs for the specified
 
389
        version.
 
390
 
 
391
        Must raise RevisionNotPresent if any of the given versions are
 
392
        not present in file history.
 
393
        """
 
394
        raise NotImplementedError(self.annotate_iter)
494
395
 
495
396
    def annotate(self, version_id):
496
 
        """Return a list of (version-id, line) tuples for version_id.
497
 
 
498
 
        :raise RevisionNotPresent: If the given version is
499
 
        not present in file history.
 
397
        return list(self.annotate_iter(version_id))
 
398
 
 
399
    def _apply_delta(self, lines, delta):
 
400
        """Apply delta to lines."""
 
401
        lines = list(lines)
 
402
        offset = 0
 
403
        for start, end, count, delta_lines in delta:
 
404
            lines[offset+start:offset+end] = delta_lines
 
405
            offset = offset + (start - end) + count
 
406
        return lines
 
407
 
 
408
    def join(self, other, pb=None, msg=None, version_ids=None,
 
409
             ignore_missing=False):
 
410
        """Integrate versions from other into this versioned file.
 
411
 
 
412
        If version_ids is None all versions from other should be
 
413
        incorporated into this versioned file.
 
414
 
 
415
        Must raise RevisionNotPresent if any of the specified versions
 
416
        are not present in the other files history unless ignore_missing
 
417
        is supplied when they are silently skipped.
500
418
        """
501
 
        raise NotImplementedError(self.annotate)
 
419
        self._check_write_ok()
 
420
        return InterVersionedFile.get(other, self).join(
 
421
            pb,
 
422
            msg,
 
423
            version_ids,
 
424
            ignore_missing)
502
425
 
503
 
    def iter_lines_added_or_present_in_versions(self, version_ids=None,
 
426
    def iter_lines_added_or_present_in_versions(self, version_ids=None, 
504
427
                                                pb=None):
505
428
        """Iterate over the lines in the versioned file from version_ids.
506
429
 
507
 
        This may return lines from other versions. Each item the returned
508
 
        iterator yields is a tuple of a line and a text version that that line
509
 
        is present in (not introduced in).
510
 
 
511
 
        Ordering of results is in whatever order is most suitable for the
512
 
        underlying storage format.
 
430
        This may return lines from other versions, and does not return the
 
431
        specific version marker at this point. The api may be changed
 
432
        during development to include the version that the versioned file
 
433
        thinks is relevant, but given that such hints are just guesses,
 
434
        its better not to have it if we don't need it.
513
435
 
514
436
        If a progress bar is supplied, it may be used to indicate progress.
515
437
        The caller is responsible for cleaning up progress bars (because this
517
439
 
518
440
        NOTES: Lines are normalised: they will all have \n terminators.
519
441
               Lines are returned in arbitrary order.
520
 
 
521
 
        :return: An iterator over (line, version_id).
522
442
        """
523
443
        raise NotImplementedError(self.iter_lines_added_or_present_in_versions)
524
444
 
 
445
    def transaction_finished(self):
 
446
        """The transaction that this file was opened in has finished.
 
447
 
 
448
        This records self.finished = True and should cause all mutating
 
449
        operations to error.
 
450
        """
 
451
        self.finished = True
 
452
 
 
453
    @deprecated_method(zero_eight)
 
454
    def walk(self, version_ids=None):
 
455
        """Walk the versioned file as a weave-like structure, for
 
456
        versions relative to version_ids.  Yields sequence of (lineno,
 
457
        insert, deletes, text) for each relevant line.
 
458
 
 
459
        Must raise RevisionNotPresent if any of the specified versions
 
460
        are not present in the file history.
 
461
 
 
462
        :param version_ids: the version_ids to walk with respect to. If not
 
463
                            supplied the entire weave-like structure is walked.
 
464
 
 
465
        walk is deprecated in favour of iter_lines_added_or_present_in_versions
 
466
        """
 
467
        raise NotImplementedError(self.walk)
 
468
 
 
469
    @deprecated_method(zero_eight)
 
470
    def iter_names(self):
 
471
        """Walk the names list."""
 
472
        return iter(self.versions())
 
473
 
525
474
    def plan_merge(self, ver_a, ver_b):
526
475
        """Return pseudo-annotation indicating how the two versions merge.
527
476
 
549
498
        return PlanWeaveMerge(plan, a_marker, b_marker).merge_lines()[0]
550
499
 
551
500
 
552
 
class RecordingVersionedFilesDecorator(object):
553
 
    """A minimal versioned files that records calls made on it.
554
 
    
555
 
    Only enough methods have been added to support tests using it to date.
556
 
 
557
 
    :ivar calls: A list of the calls made; can be reset at any time by
558
 
        assigning [] to it.
559
 
    """
560
 
 
561
 
    def __init__(self, backing_vf):
562
 
        """Create a RecordingVersionedFilesDecorator decorating backing_vf.
563
 
        
564
 
        :param backing_vf: The versioned file to answer all methods.
565
 
        """
566
 
        self._backing_vf = backing_vf
567
 
        self.calls = []
568
 
 
569
 
    def add_lines(self, key, parents, lines, parent_texts=None,
570
 
        left_matching_blocks=None, nostore_sha=None, random_id=False,
571
 
        check_content=True):
572
 
        self.calls.append(("add_lines", key, parents, lines, parent_texts,
573
 
            left_matching_blocks, nostore_sha, random_id, check_content))
574
 
        return self._backing_vf.add_lines(key, parents, lines, parent_texts,
575
 
            left_matching_blocks, nostore_sha, random_id, check_content)
576
 
 
577
 
    def check(self):
578
 
        self._backing_vf.check()
579
 
 
580
 
    def get_parent_map(self, keys):
581
 
        self.calls.append(("get_parent_map", copy(keys)))
582
 
        return self._backing_vf.get_parent_map(keys)
583
 
 
584
 
    def get_record_stream(self, keys, sort_order, include_delta_closure):
585
 
        self.calls.append(("get_record_stream", list(keys), sort_order,
586
 
            include_delta_closure))
587
 
        return self._backing_vf.get_record_stream(keys, sort_order,
588
 
            include_delta_closure)
589
 
 
590
 
    def get_sha1s(self, keys):
591
 
        self.calls.append(("get_sha1s", copy(keys)))
592
 
        return self._backing_vf.get_sha1s(keys)
593
 
 
594
 
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
595
 
        self.calls.append(("iter_lines_added_or_present_in_keys", copy(keys)))
596
 
        return self._backing_vf.iter_lines_added_or_present_in_keys(keys, pb=pb)
597
 
 
598
 
    def keys(self):
599
 
        self.calls.append(("keys",))
600
 
        return self._backing_vf.keys()
601
 
 
602
 
 
603
 
class OrderingVersionedFilesDecorator(RecordingVersionedFilesDecorator):
604
 
    """A VF that records calls, and returns keys in specific order.
605
 
 
606
 
    :ivar calls: A list of the calls made; can be reset at any time by
607
 
        assigning [] to it.
608
 
    """
609
 
 
610
 
    def __init__(self, backing_vf, key_priority):
611
 
        """Create a RecordingVersionedFilesDecorator decorating backing_vf.
612
 
 
613
 
        :param backing_vf: The versioned file to answer all methods.
614
 
        :param key_priority: A dictionary defining what order keys should be
615
 
            returned from an 'unordered' get_record_stream request.
616
 
            Keys with lower priority are returned first, keys not present in
617
 
            the map get an implicit priority of 0, and are returned in
618
 
            lexicographical order.
619
 
        """
620
 
        RecordingVersionedFilesDecorator.__init__(self, backing_vf)
621
 
        self._key_priority = key_priority
622
 
 
623
 
    def get_record_stream(self, keys, sort_order, include_delta_closure):
624
 
        self.calls.append(("get_record_stream", list(keys), sort_order,
625
 
            include_delta_closure))
626
 
        if sort_order == 'unordered':
627
 
            def sort_key(key):
628
 
                return (self._key_priority.get(key, 0), key)
629
 
            # Use a defined order by asking for the keys one-by-one from the
630
 
            # backing_vf
631
 
            for key in sorted(keys, key=sort_key):
632
 
                for record in self._backing_vf.get_record_stream([key],
633
 
                                'unordered', include_delta_closure):
634
 
                    yield record
635
 
        else:
636
 
            for record in self._backing_vf.get_record_stream(keys, sort_order,
637
 
                            include_delta_closure):
638
 
                yield record
639
 
 
640
 
 
641
 
class KeyMapper(object):
642
 
    """KeyMappers map between keys and underlying partitioned storage."""
643
 
 
644
 
    def map(self, key):
645
 
        """Map key to an underlying storage identifier.
646
 
 
647
 
        :param key: A key tuple e.g. ('file-id', 'revision-id').
648
 
        :return: An underlying storage identifier, specific to the partitioning
649
 
            mechanism.
650
 
        """
651
 
        raise NotImplementedError(self.map)
652
 
 
653
 
    def unmap(self, partition_id):
654
 
        """Map a partitioned storage id back to a key prefix.
655
 
        
656
 
        :param partition_id: The underlying partition id.
657
 
        :return: As much of a key (or prefix) as is derivable from the partition
658
 
            id.
659
 
        """
660
 
        raise NotImplementedError(self.unmap)
661
 
 
662
 
 
663
 
class ConstantMapper(KeyMapper):
664
 
    """A key mapper that maps to a constant result."""
665
 
 
666
 
    def __init__(self, result):
667
 
        """Create a ConstantMapper which will return result for all maps."""
668
 
        self._result = result
669
 
 
670
 
    def map(self, key):
671
 
        """See KeyMapper.map()."""
672
 
        return self._result
673
 
 
674
 
 
675
 
class URLEscapeMapper(KeyMapper):
676
 
    """Base class for use with transport backed storage.
677
 
 
678
 
    This provides a map and unmap wrapper that respectively url escape and
679
 
    unescape their outputs and inputs.
680
 
    """
681
 
 
682
 
    def map(self, key):
683
 
        """See KeyMapper.map()."""
684
 
        return urllib.quote(self._map(key))
685
 
 
686
 
    def unmap(self, partition_id):
687
 
        """See KeyMapper.unmap()."""
688
 
        return self._unmap(urllib.unquote(partition_id))
689
 
 
690
 
 
691
 
class PrefixMapper(URLEscapeMapper):
692
 
    """A key mapper that extracts the first component of a key.
693
 
    
694
 
    This mapper is for use with a transport based backend.
695
 
    """
696
 
 
697
 
    def _map(self, key):
698
 
        """See KeyMapper.map()."""
699
 
        return key[0]
700
 
 
701
 
    def _unmap(self, partition_id):
702
 
        """See KeyMapper.unmap()."""
703
 
        return (partition_id,)
704
 
 
705
 
 
706
 
class HashPrefixMapper(URLEscapeMapper):
707
 
    """A key mapper that combines the first component of a key with a hash.
708
 
 
709
 
    This mapper is for use with a transport based backend.
710
 
    """
711
 
 
712
 
    def _map(self, key):
713
 
        """See KeyMapper.map()."""
714
 
        prefix = self._escape(key[0])
715
 
        return "%02x/%s" % (adler32(prefix) & 0xff, prefix)
716
 
 
717
 
    def _escape(self, prefix):
718
 
        """No escaping needed here."""
719
 
        return prefix
720
 
 
721
 
    def _unmap(self, partition_id):
722
 
        """See KeyMapper.unmap()."""
723
 
        return (self._unescape(osutils.basename(partition_id)),)
724
 
 
725
 
    def _unescape(self, basename):
726
 
        """No unescaping needed for HashPrefixMapper."""
727
 
        return basename
728
 
 
729
 
 
730
 
class HashEscapedPrefixMapper(HashPrefixMapper):
731
 
    """Combines the escaped first component of a key with a hash.
732
 
    
733
 
    This mapper is for use with a transport based backend.
734
 
    """
735
 
 
736
 
    _safe = "abcdefghijklmnopqrstuvwxyz0123456789-_@,."
737
 
 
738
 
    def _escape(self, prefix):
739
 
        """Turn a key element into a filesystem safe string.
740
 
 
741
 
        This is similar to a plain urllib.quote, except
742
 
        it uses specific safe characters, so that it doesn't
743
 
        have to translate a lot of valid file ids.
744
 
        """
745
 
        # @ does not get escaped. This is because it is a valid
746
 
        # filesystem character we use all the time, and it looks
747
 
        # a lot better than seeing %40 all the time.
748
 
        r = [((c in self._safe) and c or ('%%%02x' % ord(c)))
749
 
             for c in prefix]
750
 
        return ''.join(r)
751
 
 
752
 
    def _unescape(self, basename):
753
 
        """Escaped names are easily unescaped by urlutils."""
754
 
        return urllib.unquote(basename)
755
 
 
756
 
 
757
 
def make_versioned_files_factory(versioned_file_factory, mapper):
758
 
    """Create a ThunkedVersionedFiles factory.
759
 
 
760
 
    This will create a callable which when called creates a
761
 
    ThunkedVersionedFiles on a transport, using mapper to access individual
762
 
    versioned files, and versioned_file_factory to create each individual file.
763
 
    """
764
 
    def factory(transport):
765
 
        return ThunkedVersionedFiles(transport, versioned_file_factory, mapper,
766
 
            lambda:True)
767
 
    return factory
768
 
 
769
 
 
770
 
class VersionedFiles(object):
771
 
    """Storage for many versioned files.
772
 
 
773
 
    This object allows a single keyspace for accessing the history graph and
774
 
    contents of named bytestrings.
775
 
 
776
 
    Currently no implementation allows the graph of different key prefixes to
777
 
    intersect, but the API does allow such implementations in the future.
778
 
 
779
 
    The keyspace is expressed via simple tuples. Any instance of VersionedFiles
780
 
    may have a different length key-size, but that size will be constant for
781
 
    all texts added to or retrieved from it. For instance, bzrlib uses
782
 
    instances with a key-size of 2 for storing user files in a repository, with
783
 
    the first element the fileid, and the second the version of that file.
784
 
 
785
 
    The use of tuples allows a single code base to support several different
786
 
    uses with only the mapping logic changing from instance to instance.
787
 
    """
788
 
 
789
 
    def add_lines(self, key, parents, lines, parent_texts=None,
790
 
        left_matching_blocks=None, nostore_sha=None, random_id=False,
791
 
        check_content=True):
792
 
        """Add a text to the store.
793
 
 
794
 
        :param key: The key tuple of the text to add.
795
 
        :param parents: The parents key tuples of the text to add.
796
 
        :param lines: A list of lines. Each line must be a bytestring. And all
797
 
            of them except the last must be terminated with \n and contain no
798
 
            other \n's. The last line may either contain no \n's or a single
799
 
            terminating \n. If the lines list does meet this constraint the add
800
 
            routine may error or may succeed - but you will be unable to read
801
 
            the data back accurately. (Checking the lines have been split
802
 
            correctly is expensive and extremely unlikely to catch bugs so it
803
 
            is not done at runtime unless check_content is True.)
804
 
        :param parent_texts: An optional dictionary containing the opaque 
805
 
            representations of some or all of the parents of version_id to
806
 
            allow delta optimisations.  VERY IMPORTANT: the texts must be those
807
 
            returned by add_lines or data corruption can be caused.
808
 
        :param left_matching_blocks: a hint about which areas are common
809
 
            between the text and its left-hand-parent.  The format is
810
 
            the SequenceMatcher.get_matching_blocks format.
811
 
        :param nostore_sha: Raise ExistingContent and do not add the lines to
812
 
            the versioned file if the digest of the lines matches this.
813
 
        :param random_id: If True a random id has been selected rather than
814
 
            an id determined by some deterministic process such as a converter
815
 
            from a foreign VCS. When True the backend may choose not to check
816
 
            for uniqueness of the resulting key within the versioned file, so
817
 
            this should only be done when the result is expected to be unique
818
 
            anyway.
819
 
        :param check_content: If True, the lines supplied are verified to be
820
 
            bytestrings that are correctly formed lines.
821
 
        :return: The text sha1, the number of bytes in the text, and an opaque
822
 
                 representation of the inserted version which can be provided
823
 
                 back to future add_lines calls in the parent_texts dictionary.
824
 
        """
825
 
        raise NotImplementedError(self.add_lines)
826
 
 
827
 
    def add_mpdiffs(self, records):
828
 
        """Add mpdiffs to this VersionedFile.
829
 
 
830
 
        Records should be iterables of version, parents, expected_sha1,
831
 
        mpdiff. mpdiff should be a MultiParent instance.
832
 
        """
833
 
        vf_parents = {}
834
 
        mpvf = multiparent.MultiMemoryVersionedFile()
835
 
        versions = []
836
 
        for version, parent_ids, expected_sha1, mpdiff in records:
837
 
            versions.append(version)
838
 
            mpvf.add_diff(mpdiff, version, parent_ids)
839
 
        needed_parents = set()
840
 
        for version, parent_ids, expected_sha1, mpdiff in records:
841
 
            needed_parents.update(p for p in parent_ids
842
 
                                  if not mpvf.has_version(p))
843
 
        # It seems likely that adding all the present parents as fulltexts can
844
 
        # easily exhaust memory.
845
 
        chunks_to_lines = osutils.chunks_to_lines
846
 
        for record in self.get_record_stream(needed_parents, 'unordered',
847
 
            True):
848
 
            if record.storage_kind == 'absent':
849
 
                continue
850
 
            mpvf.add_version(chunks_to_lines(record.get_bytes_as('chunked')),
851
 
                record.key, [])
852
 
        for (key, parent_keys, expected_sha1, mpdiff), lines in\
853
 
            zip(records, mpvf.get_line_list(versions)):
854
 
            if len(parent_keys) == 1:
855
 
                left_matching_blocks = list(mpdiff.get_matching_blocks(0,
856
 
                    mpvf.get_diff(parent_keys[0]).num_lines()))
857
 
            else:
858
 
                left_matching_blocks = None
859
 
            version_sha1, _, version_text = self.add_lines(key,
860
 
                parent_keys, lines, vf_parents,
861
 
                left_matching_blocks=left_matching_blocks)
862
 
            if version_sha1 != expected_sha1:
863
 
                raise errors.VersionedFileInvalidChecksum(version)
864
 
            vf_parents[key] = version_text
865
 
 
866
 
    def annotate(self, key):
867
 
        """Return a list of (version-key, line) tuples for the text of key.
868
 
 
869
 
        :raise RevisionNotPresent: If the key is not present.
870
 
        """
871
 
        raise NotImplementedError(self.annotate)
872
 
 
873
 
    def check(self, progress_bar=None):
874
 
        """Check this object for integrity."""
875
 
        raise NotImplementedError(self.check)
876
 
 
877
 
    @staticmethod
878
 
    def check_not_reserved_id(version_id):
879
 
        revision.check_not_reserved_id(version_id)
880
 
 
881
 
    def _check_lines_not_unicode(self, lines):
882
 
        """Check that lines being added to a versioned file are not unicode."""
883
 
        for line in lines:
884
 
            if line.__class__ is not str:
885
 
                raise errors.BzrBadParameterUnicode("lines")
886
 
 
887
 
    def _check_lines_are_lines(self, lines):
888
 
        """Check that the lines really are full lines without inline EOL."""
889
 
        for line in lines:
890
 
            if '\n' in line[:-1]:
891
 
                raise errors.BzrBadParameterContainsNewline("lines")
892
 
 
893
 
    def get_parent_map(self, keys):
894
 
        """Get a map of the parents of keys.
895
 
 
896
 
        :param keys: The keys to look up parents for.
897
 
        :return: A mapping from keys to parents. Absent keys are absent from
898
 
            the mapping.
899
 
        """
900
 
        raise NotImplementedError(self.get_parent_map)
901
 
 
902
 
    def get_record_stream(self, keys, ordering, include_delta_closure):
903
 
        """Get a stream of records for keys.
904
 
 
905
 
        :param keys: The keys to include.
906
 
        :param ordering: Either 'unordered' or 'topological'. A topologically
907
 
            sorted stream has compression parents strictly before their
908
 
            children.
909
 
        :param include_delta_closure: If True then the closure across any
910
 
            compression parents will be included (in the opaque data).
911
 
        :return: An iterator of ContentFactory objects, each of which is only
912
 
            valid until the iterator is advanced.
913
 
        """
914
 
        raise NotImplementedError(self.get_record_stream)
915
 
 
916
 
    def get_sha1s(self, keys):
917
 
        """Get the sha1's of the texts for the given keys.
918
 
 
919
 
        :param keys: The names of the keys to lookup
920
 
        :return: a dict from key to sha1 digest. Keys of texts which are not
921
 
            present in the store are not present in the returned
922
 
            dictionary.
923
 
        """
924
 
        raise NotImplementedError(self.get_sha1s)
925
 
 
926
 
    has_key = index._has_key_from_parent_map
927
 
 
928
 
    def insert_record_stream(self, stream):
929
 
        """Insert a record stream into this container.
930
 
 
931
 
        :param stream: A stream of records to insert. 
932
 
        :return: None
933
 
        :seealso VersionedFile.get_record_stream:
934
 
        """
935
 
        raise NotImplementedError
936
 
 
937
 
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
938
 
        """Iterate over the lines in the versioned files from keys.
939
 
 
940
 
        This may return lines from other keys. Each item the returned
941
 
        iterator yields is a tuple of a line and a text version that that line
942
 
        is present in (not introduced in).
943
 
 
944
 
        Ordering of results is in whatever order is most suitable for the
945
 
        underlying storage format.
946
 
 
947
 
        If a progress bar is supplied, it may be used to indicate progress.
948
 
        The caller is responsible for cleaning up progress bars (because this
949
 
        is an iterator).
950
 
 
951
 
        NOTES:
952
 
         * Lines are normalised by the underlying store: they will all have \n
953
 
           terminators.
954
 
         * Lines are returned in arbitrary order.
955
 
 
956
 
        :return: An iterator over (line, key).
957
 
        """
958
 
        raise NotImplementedError(self.iter_lines_added_or_present_in_keys)
959
 
 
960
 
    def keys(self):
961
 
        """Return a iterable of the keys for all the contained texts."""
962
 
        raise NotImplementedError(self.keys)
963
 
 
964
 
    def make_mpdiffs(self, keys):
965
 
        """Create multiparent diffs for specified keys."""
966
 
        keys_order = tuple(keys)
967
 
        keys = frozenset(keys)
968
 
        knit_keys = set(keys)
969
 
        parent_map = self.get_parent_map(keys)
970
 
        for parent_keys in parent_map.itervalues():
971
 
            if parent_keys:
972
 
                knit_keys.update(parent_keys)
973
 
        missing_keys = keys - set(parent_map)
974
 
        if missing_keys:
975
 
            raise errors.RevisionNotPresent(list(missing_keys)[0], self)
976
 
        # We need to filter out ghosts, because we can't diff against them.
977
 
        maybe_ghosts = knit_keys - keys
978
 
        ghosts = maybe_ghosts - set(self.get_parent_map(maybe_ghosts))
979
 
        knit_keys.difference_update(ghosts)
980
 
        lines = {}
981
 
        chunks_to_lines = osutils.chunks_to_lines
982
 
        for record in self.get_record_stream(knit_keys, 'topological', True):
983
 
            lines[record.key] = chunks_to_lines(record.get_bytes_as('chunked'))
984
 
            # line_block_dict = {}
985
 
            # for parent, blocks in record.extract_line_blocks():
986
 
            #   line_blocks[parent] = blocks
987
 
            # line_blocks[record.key] = line_block_dict
988
 
        diffs = []
989
 
        for key in keys_order:
990
 
            target = lines[key]
991
 
            parents = parent_map[key] or []
992
 
            # Note that filtering knit_keys can lead to a parent difference
993
 
            # between the creation and the application of the mpdiff.
994
 
            parent_lines = [lines[p] for p in parents if p in knit_keys]
995
 
            if len(parent_lines) > 0:
996
 
                left_parent_blocks = self._extract_blocks(key, parent_lines[0],
997
 
                    target)
998
 
            else:
999
 
                left_parent_blocks = None
1000
 
            diffs.append(multiparent.MultiParent.from_lines(target,
1001
 
                parent_lines, left_parent_blocks))
1002
 
        return diffs
1003
 
 
1004
 
    missing_keys = index._missing_keys_from_parent_map
1005
 
 
1006
 
    def _extract_blocks(self, version_id, source, target):
1007
 
        return None
1008
 
 
1009
 
 
1010
 
class ThunkedVersionedFiles(VersionedFiles):
1011
 
    """Storage for many versioned files thunked onto a 'VersionedFile' class.
1012
 
 
1013
 
    This object allows a single keyspace for accessing the history graph and
1014
 
    contents of named bytestrings.
1015
 
 
1016
 
    Currently no implementation allows the graph of different key prefixes to
1017
 
    intersect, but the API does allow such implementations in the future.
1018
 
    """
1019
 
 
1020
 
    def __init__(self, transport, file_factory, mapper, is_locked):
1021
 
        """Create a ThunkedVersionedFiles."""
1022
 
        self._transport = transport
1023
 
        self._file_factory = file_factory
1024
 
        self._mapper = mapper
1025
 
        self._is_locked = is_locked
1026
 
 
1027
 
    def add_lines(self, key, parents, lines, parent_texts=None,
1028
 
        left_matching_blocks=None, nostore_sha=None, random_id=False,
1029
 
        check_content=True):
1030
 
        """See VersionedFiles.add_lines()."""
1031
 
        path = self._mapper.map(key)
1032
 
        version_id = key[-1]
1033
 
        parents = [parent[-1] for parent in parents]
1034
 
        vf = self._get_vf(path)
1035
 
        try:
1036
 
            try:
1037
 
                return vf.add_lines_with_ghosts(version_id, parents, lines,
1038
 
                    parent_texts=parent_texts,
1039
 
                    left_matching_blocks=left_matching_blocks,
1040
 
                    nostore_sha=nostore_sha, random_id=random_id,
1041
 
                    check_content=check_content)
1042
 
            except NotImplementedError:
1043
 
                return vf.add_lines(version_id, parents, lines,
1044
 
                    parent_texts=parent_texts,
1045
 
                    left_matching_blocks=left_matching_blocks,
1046
 
                    nostore_sha=nostore_sha, random_id=random_id,
1047
 
                    check_content=check_content)
1048
 
        except errors.NoSuchFile:
1049
 
            # parent directory may be missing, try again.
1050
 
            self._transport.mkdir(osutils.dirname(path))
1051
 
            try:
1052
 
                return vf.add_lines_with_ghosts(version_id, parents, lines,
1053
 
                    parent_texts=parent_texts,
1054
 
                    left_matching_blocks=left_matching_blocks,
1055
 
                    nostore_sha=nostore_sha, random_id=random_id,
1056
 
                    check_content=check_content)
1057
 
            except NotImplementedError:
1058
 
                return vf.add_lines(version_id, parents, lines,
1059
 
                    parent_texts=parent_texts,
1060
 
                    left_matching_blocks=left_matching_blocks,
1061
 
                    nostore_sha=nostore_sha, random_id=random_id,
1062
 
                    check_content=check_content)
1063
 
 
1064
 
    def annotate(self, key):
1065
 
        """Return a list of (version-key, line) tuples for the text of key.
1066
 
 
1067
 
        :raise RevisionNotPresent: If the key is not present.
1068
 
        """
1069
 
        prefix = key[:-1]
1070
 
        path = self._mapper.map(prefix)
1071
 
        vf = self._get_vf(path)
1072
 
        origins = vf.annotate(key[-1])
1073
 
        result = []
1074
 
        for origin, line in origins:
1075
 
            result.append((prefix + (origin,), line))
1076
 
        return result
1077
 
 
1078
 
    def check(self, progress_bar=None):
1079
 
        """See VersionedFiles.check()."""
1080
 
        for prefix, vf in self._iter_all_components():
1081
 
            vf.check()
1082
 
 
1083
 
    def get_parent_map(self, keys):
1084
 
        """Get a map of the parents of keys.
1085
 
 
1086
 
        :param keys: The keys to look up parents for.
1087
 
        :return: A mapping from keys to parents. Absent keys are absent from
1088
 
            the mapping.
1089
 
        """
1090
 
        prefixes = self._partition_keys(keys)
1091
 
        result = {}
1092
 
        for prefix, suffixes in prefixes.items():
1093
 
            path = self._mapper.map(prefix)
1094
 
            vf = self._get_vf(path)
1095
 
            parent_map = vf.get_parent_map(suffixes)
1096
 
            for key, parents in parent_map.items():
1097
 
                result[prefix + (key,)] = tuple(
1098
 
                    prefix + (parent,) for parent in parents)
1099
 
        return result
1100
 
 
1101
 
    def _get_vf(self, path):
1102
 
        if not self._is_locked():
1103
 
            raise errors.ObjectNotLocked(self)
1104
 
        return self._file_factory(path, self._transport, create=True,
1105
 
            get_scope=lambda:None)
1106
 
 
1107
 
    def _partition_keys(self, keys):
1108
 
        """Turn keys into a dict of prefix:suffix_list."""
1109
 
        result = {}
1110
 
        for key in keys:
1111
 
            prefix_keys = result.setdefault(key[:-1], [])
1112
 
            prefix_keys.append(key[-1])
1113
 
        return result
1114
 
 
1115
 
    def _get_all_prefixes(self):
1116
 
        # Identify all key prefixes.
1117
 
        # XXX: A bit hacky, needs polish.
1118
 
        if type(self._mapper) == ConstantMapper:
1119
 
            paths = [self._mapper.map(())]
1120
 
            prefixes = [()]
1121
 
        else:
1122
 
            relpaths = set()
1123
 
            for quoted_relpath in self._transport.iter_files_recursive():
1124
 
                path, ext = os.path.splitext(quoted_relpath)
1125
 
                relpaths.add(path)
1126
 
            paths = list(relpaths)
1127
 
            prefixes = [self._mapper.unmap(path) for path in paths]
1128
 
        return zip(paths, prefixes)
1129
 
 
1130
 
    def get_record_stream(self, keys, ordering, include_delta_closure):
1131
 
        """See VersionedFiles.get_record_stream()."""
1132
 
        # Ordering will be taken care of by each partitioned store; group keys
1133
 
        # by partition.
1134
 
        keys = sorted(keys)
1135
 
        for prefix, suffixes, vf in self._iter_keys_vf(keys):
1136
 
            suffixes = [(suffix,) for suffix in suffixes]
1137
 
            for record in vf.get_record_stream(suffixes, ordering,
1138
 
                include_delta_closure):
1139
 
                if record.parents is not None:
1140
 
                    record.parents = tuple(
1141
 
                        prefix + parent for parent in record.parents)
1142
 
                record.key = prefix + record.key
1143
 
                yield record
1144
 
 
1145
 
    def _iter_keys_vf(self, keys):
1146
 
        prefixes = self._partition_keys(keys)
1147
 
        sha1s = {}
1148
 
        for prefix, suffixes in prefixes.items():
1149
 
            path = self._mapper.map(prefix)
1150
 
            vf = self._get_vf(path)
1151
 
            yield prefix, suffixes, vf
1152
 
 
1153
 
    def get_sha1s(self, keys):
1154
 
        """See VersionedFiles.get_sha1s()."""
1155
 
        sha1s = {}
1156
 
        for prefix,suffixes, vf in self._iter_keys_vf(keys):
1157
 
            vf_sha1s = vf.get_sha1s(suffixes)
1158
 
            for suffix, sha1 in vf_sha1s.iteritems():
1159
 
                sha1s[prefix + (suffix,)] = sha1
1160
 
        return sha1s
1161
 
 
1162
 
    def insert_record_stream(self, stream):
1163
 
        """Insert a record stream into this container.
1164
 
 
1165
 
        :param stream: A stream of records to insert. 
1166
 
        :return: None
1167
 
        :seealso VersionedFile.get_record_stream:
1168
 
        """
1169
 
        for record in stream:
1170
 
            prefix = record.key[:-1]
1171
 
            key = record.key[-1:]
1172
 
            if record.parents is not None:
1173
 
                parents = [parent[-1:] for parent in record.parents]
1174
 
            else:
1175
 
                parents = None
1176
 
            thunk_record = AdapterFactory(key, parents, record)
1177
 
            path = self._mapper.map(prefix)
1178
 
            # Note that this parses the file many times; we can do better but
1179
 
            # as this only impacts weaves in terms of performance, it is
1180
 
            # tolerable.
1181
 
            vf = self._get_vf(path)
1182
 
            vf.insert_record_stream([thunk_record])
1183
 
 
1184
 
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
1185
 
        """Iterate over the lines in the versioned files from keys.
1186
 
 
1187
 
        This may return lines from other keys. Each item the returned
1188
 
        iterator yields is a tuple of a line and a text version that that line
1189
 
        is present in (not introduced in).
1190
 
 
1191
 
        Ordering of results is in whatever order is most suitable for the
1192
 
        underlying storage format.
1193
 
 
1194
 
        If a progress bar is supplied, it may be used to indicate progress.
1195
 
        The caller is responsible for cleaning up progress bars (because this
1196
 
        is an iterator).
1197
 
 
1198
 
        NOTES:
1199
 
         * Lines are normalised by the underlying store: they will all have \n
1200
 
           terminators.
1201
 
         * Lines are returned in arbitrary order.
1202
 
 
1203
 
        :return: An iterator over (line, key).
1204
 
        """
1205
 
        for prefix, suffixes, vf in self._iter_keys_vf(keys):
1206
 
            for line, version in vf.iter_lines_added_or_present_in_versions(suffixes):
1207
 
                yield line, prefix + (version,)
1208
 
 
1209
 
    def _iter_all_components(self):
1210
 
        for path, prefix in self._get_all_prefixes():
1211
 
            yield prefix, self._get_vf(path)
1212
 
 
1213
 
    def keys(self):
1214
 
        """See VersionedFiles.keys()."""
1215
 
        result = set()
1216
 
        for prefix, vf in self._iter_all_components():
1217
 
            for suffix in vf.versions():
1218
 
                result.add(prefix + (suffix,))
1219
 
        return result
1220
 
 
1221
 
 
1222
 
class _PlanMergeVersionedFile(VersionedFiles):
1223
 
    """A VersionedFile for uncommitted and committed texts.
1224
 
 
1225
 
    It is intended to allow merges to be planned with working tree texts.
1226
 
    It implements only the small part of the VersionedFiles interface used by
1227
 
    PlanMerge.  It falls back to multiple versionedfiles for data not stored in
1228
 
    _PlanMergeVersionedFile itself.
1229
 
 
1230
 
    :ivar: fallback_versionedfiles a list of VersionedFiles objects that can be
1231
 
        queried for missing texts.
1232
 
    """
1233
 
 
1234
 
    def __init__(self, file_id):
1235
 
        """Create a _PlanMergeVersionedFile.
1236
 
 
1237
 
        :param file_id: Used with _PlanMerge code which is not yet fully
1238
 
            tuple-keyspace aware.
1239
 
        """
1240
 
        self._file_id = file_id
1241
 
        # fallback locations
1242
 
        self.fallback_versionedfiles = []
1243
 
        # Parents for locally held keys.
1244
 
        self._parents = {}
1245
 
        # line data for locally held keys.
1246
 
        self._lines = {}
1247
 
        # key lookup providers
1248
 
        self._providers = [DictParentsProvider(self._parents)]
1249
 
 
1250
 
    def plan_merge(self, ver_a, ver_b, base=None):
1251
 
        """See VersionedFile.plan_merge"""
1252
 
        from bzrlib.merge import _PlanMerge
1253
 
        if base is None:
1254
 
            return _PlanMerge(ver_a, ver_b, self, (self._file_id,)).plan_merge()
1255
 
        old_plan = list(_PlanMerge(ver_a, base, self, (self._file_id,)).plan_merge())
1256
 
        new_plan = list(_PlanMerge(ver_a, ver_b, self, (self._file_id,)).plan_merge())
1257
 
        return _PlanMerge._subtract_plans(old_plan, new_plan)
1258
 
 
1259
 
    def plan_lca_merge(self, ver_a, ver_b, base=None):
1260
 
        from bzrlib.merge import _PlanLCAMerge
1261
 
        graph = Graph(self)
1262
 
        new_plan = _PlanLCAMerge(ver_a, ver_b, self, (self._file_id,), graph).plan_merge()
1263
 
        if base is None:
1264
 
            return new_plan
1265
 
        old_plan = _PlanLCAMerge(ver_a, base, self, (self._file_id,), graph).plan_merge()
1266
 
        return _PlanLCAMerge._subtract_plans(list(old_plan), list(new_plan))
1267
 
 
1268
 
    def add_lines(self, key, parents, lines):
1269
 
        """See VersionedFiles.add_lines
1270
 
 
1271
 
        Lines are added locally, not to fallback versionedfiles.  Also, ghosts
1272
 
        are permitted.  Only reserved ids are permitted.
1273
 
        """
1274
 
        if type(key) is not tuple:
1275
 
            raise TypeError(key)
1276
 
        if not revision.is_reserved_id(key[-1]):
1277
 
            raise ValueError('Only reserved ids may be used')
1278
 
        if parents is None:
1279
 
            raise ValueError('Parents may not be None')
1280
 
        if lines is None:
1281
 
            raise ValueError('Lines may not be None')
1282
 
        self._parents[key] = tuple(parents)
1283
 
        self._lines[key] = lines
1284
 
 
1285
 
    def get_record_stream(self, keys, ordering, include_delta_closure):
1286
 
        pending = set(keys)
1287
 
        for key in keys:
1288
 
            if key in self._lines:
1289
 
                lines = self._lines[key]
1290
 
                parents = self._parents[key]
1291
 
                pending.remove(key)
1292
 
                yield ChunkedContentFactory(key, parents, None, lines)
1293
 
        for versionedfile in self.fallback_versionedfiles:
1294
 
            for record in versionedfile.get_record_stream(
1295
 
                pending, 'unordered', True):
1296
 
                if record.storage_kind == 'absent':
1297
 
                    continue
1298
 
                else:
1299
 
                    pending.remove(record.key)
1300
 
                    yield record
1301
 
            if not pending:
1302
 
                return
1303
 
        # report absent entries
1304
 
        for key in pending:
1305
 
            yield AbsentContentFactory(key)
1306
 
 
1307
 
    def get_parent_map(self, keys):
1308
 
        """See VersionedFiles.get_parent_map"""
1309
 
        # We create a new provider because a fallback may have been added.
1310
 
        # If we make fallbacks private we can update a stack list and avoid
1311
 
        # object creation thrashing.
1312
 
        keys = set(keys)
1313
 
        result = {}
1314
 
        if revision.NULL_REVISION in keys:
1315
 
            keys.remove(revision.NULL_REVISION)
1316
 
            result[revision.NULL_REVISION] = ()
1317
 
        self._providers = self._providers[:1] + self.fallback_versionedfiles
1318
 
        result.update(
1319
 
            _StackedParentsProvider(self._providers).get_parent_map(keys))
1320
 
        for key, parents in result.iteritems():
1321
 
            if parents == ():
1322
 
                result[key] = (revision.NULL_REVISION,)
1323
 
        return result
1324
 
 
1325
 
 
1326
501
class PlanWeaveMerge(TextMerge):
1327
502
    """Weave merge that takes a plan as its input.
1328
503
    
1380
555
            elif state == 'new-b':
1381
556
                ch_b = True
1382
557
                lines_b.append(line)
1383
 
            elif state == 'conflicted-a':
1384
 
                ch_b = ch_a = True
1385
 
                lines_a.append(line)
1386
 
            elif state == 'conflicted-b':
1387
 
                ch_b = ch_a = True
1388
 
                lines_b.append(line)
1389
558
            else:
1390
 
                if state not in ('irrelevant', 'ghost-a', 'ghost-b',
1391
 
                        'killed-base', 'killed-both'):
1392
 
                    raise AssertionError(state)
 
559
                assert state in ('irrelevant', 'ghost-a', 'ghost-b', 
 
560
                                 'killed-base', 'killed-both'), state
1393
561
        for struct in outstanding_struct():
1394
562
            yield struct
1395
563
 
1396
564
 
1397
565
class WeaveMerge(PlanWeaveMerge):
1398
 
    """Weave merge that takes a VersionedFile and two versions as its input."""
 
566
    """Weave merge that takes a VersionedFile and two versions as its input"""
1399
567
 
1400
568
    def __init__(self, versionedfile, ver_a, ver_b, 
1401
569
        a_marker=PlanWeaveMerge.A_MARKER, b_marker=PlanWeaveMerge.B_MARKER):
1403
571
        PlanWeaveMerge.__init__(self, plan, a_marker, b_marker)
1404
572
 
1405
573
 
1406
 
class VirtualVersionedFiles(VersionedFiles):
1407
 
    """Dummy implementation for VersionedFiles that uses other functions for 
1408
 
    obtaining fulltexts and parent maps.
1409
 
 
1410
 
    This is always on the bottom of the stack and uses string keys 
1411
 
    (rather than tuples) internally.
 
574
class InterVersionedFile(InterObject):
 
575
    """This class represents operations taking place between two versionedfiles..
 
576
 
 
577
    Its instances have methods like join, and contain
 
578
    references to the source and target versionedfiles these operations can be 
 
579
    carried out on.
 
580
 
 
581
    Often we will provide convenience methods on 'versionedfile' which carry out
 
582
    operations with another versionedfile - they will always forward to
 
583
    InterVersionedFile.get(other).method_name(parameters).
1412
584
    """
1413
585
 
1414
 
    def __init__(self, get_parent_map, get_lines):
1415
 
        """Create a VirtualVersionedFiles.
1416
 
 
1417
 
        :param get_parent_map: Same signature as Repository.get_parent_map.
1418
 
        :param get_lines: Should return lines for specified key or None if 
1419
 
                          not available.
1420
 
        """
1421
 
        super(VirtualVersionedFiles, self).__init__()
1422
 
        self._get_parent_map = get_parent_map
1423
 
        self._get_lines = get_lines
1424
 
        
1425
 
    def check(self, progressbar=None):
1426
 
        """See VersionedFiles.check.
1427
 
 
1428
 
        :note: Always returns True for VirtualVersionedFiles.
1429
 
        """
1430
 
        return True
1431
 
 
1432
 
    def add_mpdiffs(self, records):
1433
 
        """See VersionedFiles.mpdiffs.
1434
 
 
1435
 
        :note: Not implemented for VirtualVersionedFiles.
1436
 
        """
1437
 
        raise NotImplementedError(self.add_mpdiffs)
1438
 
 
1439
 
    def get_parent_map(self, keys):
1440
 
        """See VersionedFiles.get_parent_map."""
1441
 
        return dict([((k,), tuple([(p,) for p in v]))
1442
 
            for k,v in self._get_parent_map([k for (k,) in keys]).iteritems()])
1443
 
 
1444
 
    def get_sha1s(self, keys):
1445
 
        """See VersionedFiles.get_sha1s."""
1446
 
        ret = {}
1447
 
        for (k,) in keys:
1448
 
            lines = self._get_lines(k)
1449
 
            if lines is not None:
1450
 
                if not isinstance(lines, list):
1451
 
                    raise AssertionError
1452
 
                ret[(k,)] = osutils.sha_strings(lines)
1453
 
        return ret
1454
 
 
1455
 
    def get_record_stream(self, keys, ordering, include_delta_closure):
1456
 
        """See VersionedFiles.get_record_stream."""
1457
 
        for (k,) in list(keys):
1458
 
            lines = self._get_lines(k)
1459
 
            if lines is not None:
1460
 
                if not isinstance(lines, list):
1461
 
                    raise AssertionError
1462
 
                yield ChunkedContentFactory((k,), None,
1463
 
                        sha1=osutils.sha_strings(lines),
1464
 
                        chunks=lines)
 
586
    _optimisers = []
 
587
    """The available optimised InterVersionedFile types."""
 
588
 
 
589
    def join(self, pb=None, msg=None, version_ids=None, ignore_missing=False):
 
590
        """Integrate versions from self.source into self.target.
 
591
 
 
592
        If version_ids is None all versions from source should be
 
593
        incorporated into this versioned file.
 
594
 
 
595
        Must raise RevisionNotPresent if any of the specified versions
 
596
        are not present in the other files history unless ignore_missing is 
 
597
        supplied when they are silently skipped.
 
598
        """
 
599
        # the default join: 
 
600
        # - if the target is empty, just add all the versions from 
 
601
        #   source to target, otherwise:
 
602
        # - make a temporary versioned file of type target
 
603
        # - insert the source content into it one at a time
 
604
        # - join them
 
605
        if not self.target.versions():
 
606
            target = self.target
 
607
        else:
 
608
            # Make a new target-format versioned file. 
 
609
            temp_source = self.target.create_empty("temp", MemoryTransport())
 
610
            target = temp_source
 
611
        version_ids = self._get_source_version_ids(version_ids, ignore_missing)
 
612
        graph = self.source.get_graph(version_ids)
 
613
        order = tsort.topo_sort(graph.items())
 
614
        pb = ui.ui_factory.nested_progress_bar()
 
615
        parent_texts = {}
 
616
        try:
 
617
            # TODO for incremental cross-format work:
 
618
            # make a versioned file with the following content:
 
619
            # all revisions we have been asked to join
 
620
            # all their ancestors that are *not* in target already.
 
621
            # the immediate parents of the above two sets, with 
 
622
            # empty parent lists - these versions are in target already
 
623
            # and the incorrect version data will be ignored.
 
624
            # TODO: for all ancestors that are present in target already,
 
625
            # check them for consistent data, this requires moving sha1 from
 
626
            # 
 
627
            # TODO: remove parent texts when they are not relevant any more for 
 
628
            # memory pressure reduction. RBC 20060313
 
629
            # pb.update('Converting versioned data', 0, len(order))
 
630
            # deltas = self.source.get_deltas(order)
 
631
            for index, version in enumerate(order):
 
632
                pb.update('Converting versioned data', index, len(order))
 
633
                parent_text = target.add_lines(version,
 
634
                                               self.source.get_parents(version),
 
635
                                               self.source.get_lines(version),
 
636
                                               parent_texts=parent_texts)
 
637
                parent_texts[version] = parent_text
 
638
                #delta_parent, sha1, noeol, delta = deltas[version]
 
639
                #target.add_delta(version,
 
640
                #                 self.source.get_parents(version),
 
641
                #                 delta_parent,
 
642
                #                 sha1,
 
643
                #                 noeol,
 
644
                #                 delta)
 
645
                #target.get_lines(version)
 
646
            
 
647
            # this should hit the native code path for target
 
648
            if target is not self.target:
 
649
                return self.target.join(temp_source,
 
650
                                        pb,
 
651
                                        msg,
 
652
                                        version_ids,
 
653
                                        ignore_missing)
 
654
        finally:
 
655
            pb.finished()
 
656
 
 
657
    def _get_source_version_ids(self, version_ids, ignore_missing):
 
658
        """Determine the version ids to be used from self.source.
 
659
 
 
660
        :param version_ids: The caller-supplied version ids to check. (None 
 
661
                            for all). If None is in version_ids, it is stripped.
 
662
        :param ignore_missing: if True, remove missing ids from the version 
 
663
                               list. If False, raise RevisionNotPresent on
 
664
                               a missing version id.
 
665
        :return: A set of version ids.
 
666
        """
 
667
        if version_ids is None:
 
668
            # None cannot be in source.versions
 
669
            return set(self.source.versions())
 
670
        else:
 
671
            version_ids = [osutils.safe_revision_id(v) for v in version_ids]
 
672
            if ignore_missing:
 
673
                return set(self.source.versions()).intersection(set(version_ids))
1465
674
            else:
1466
 
                yield AbsentContentFactory((k,))
1467
 
 
1468
 
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
1469
 
        """See VersionedFile.iter_lines_added_or_present_in_versions()."""
1470
 
        for i, (key,) in enumerate(keys):
1471
 
            if pb is not None:
1472
 
                pb.update("iterating texts", i, len(keys))
1473
 
            for l in self._get_lines(key):
1474
 
                yield (l, key)
 
675
                new_version_ids = set()
 
676
                for version in version_ids:
 
677
                    if version is None:
 
678
                        continue
 
679
                    if not self.source.has_version(version):
 
680
                        raise errors.RevisionNotPresent(version, str(self.source))
 
681
                    else:
 
682
                        new_version_ids.add(version)
 
683
                return new_version_ids
 
684
 
 
685
 
 
686
class InterVersionedFileTestProviderAdapter(object):
 
687
    """A tool to generate a suite testing multiple inter versioned-file classes.
 
688
 
 
689
    This is done by copying the test once for each InterVersionedFile provider
 
690
    and injecting the transport_server, transport_readonly_server,
 
691
    versionedfile_factory and versionedfile_factory_to classes into each copy.
 
692
    Each copy is also given a new id() to make it easy to identify.
 
693
    """
 
694
 
 
695
    def __init__(self, transport_server, transport_readonly_server, formats):
 
696
        self._transport_server = transport_server
 
697
        self._transport_readonly_server = transport_readonly_server
 
698
        self._formats = formats
 
699
    
 
700
    def adapt(self, test):
 
701
        result = unittest.TestSuite()
 
702
        for (interversionedfile_class,
 
703
             versionedfile_factory,
 
704
             versionedfile_factory_to) in self._formats:
 
705
            new_test = deepcopy(test)
 
706
            new_test.transport_server = self._transport_server
 
707
            new_test.transport_readonly_server = self._transport_readonly_server
 
708
            new_test.interversionedfile_class = interversionedfile_class
 
709
            new_test.versionedfile_factory = versionedfile_factory
 
710
            new_test.versionedfile_factory_to = versionedfile_factory_to
 
711
            def make_new_test_id():
 
712
                new_id = "%s(%s)" % (new_test.id(), interversionedfile_class.__name__)
 
713
                return lambda: new_id
 
714
            new_test.id = make_new_test_id()
 
715
            result.addTest(new_test)
 
716
        return result
 
717
 
 
718
    @staticmethod
 
719
    def default_test_list():
 
720
        """Generate the default list of interversionedfile permutations to test."""
 
721
        from bzrlib.weave import WeaveFile
 
722
        from bzrlib.knit import KnitVersionedFile
 
723
        result = []
 
724
        # test the fallback InterVersionedFile from annotated knits to weave
 
725
        result.append((InterVersionedFile, 
 
726
                       KnitVersionedFile,
 
727
                       WeaveFile))
 
728
        for optimiser in InterVersionedFile._optimisers:
 
729
            result.append((optimiser,
 
730
                           optimiser._matching_file_from_factory,
 
731
                           optimiser._matching_file_to_factory
 
732
                           ))
 
733
        # if there are specific combinations we want to use, we can add them 
 
734
        # here.
 
735
        return result