/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3830.3.20 by John Arbash Meinel
Minor PEP8 and copyright updates.
1
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
2
#
3
# Authors:
4
#   Johan Rydberg <jrydberg@gnu.org>
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
10
#
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
15
#
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
"""Versioned text file storage api."""
21
3350.8.2 by Robert Collins
stacked get_parent_map.
22
from copy import copy
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
23
from cStringIO import StringIO
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
24
import os
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
25
from zlib import adler32
26
1996.3.7 by John Arbash Meinel
lazy import versionedfile, late-load bzrlib.merge
27
from bzrlib.lazy_import import lazy_import
28
lazy_import(globals(), """
3224.5.20 by Andrew Bennetts
Remove or lazyify a couple more imports.
29
import urllib
1996.3.7 by John Arbash Meinel
lazy import versionedfile, late-load bzrlib.merge
30
31
from bzrlib import (
32
    errors,
3830.3.12 by Martin Pool
Review cleanups: unify has_key impls, add missing_keys(), clean up exception blocks
33
    index,
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
34
    osutils,
2520.4.3 by Aaron Bentley
Implement plain strategy for extracting and installing multiparent diffs
35
    multiparent,
1996.3.7 by John Arbash Meinel
lazy import versionedfile, late-load bzrlib.merge
36
    tsort,
2229.2.1 by Aaron Bentley
Reject reserved ids in versiondfile, tree, branch and repository
37
    revision,
1996.3.7 by John Arbash Meinel
lazy import versionedfile, late-load bzrlib.merge
38
    ui,
39
    )
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
40
from bzrlib.graph import DictParentsProvider, Graph, _StackedParentsProvider
1996.3.7 by John Arbash Meinel
lazy import versionedfile, late-load bzrlib.merge
41
from bzrlib.transport.memory import MemoryTransport
42
""")
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
43
from bzrlib.inter import InterObject
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
44
from bzrlib.registry import Registry
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
45
from bzrlib.symbol_versioning import *
1551.6.7 by Aaron Bentley
Implemented two-way merge, refactored weave merge
46
from bzrlib.textmerge import TextMerge
1563.2.11 by Robert Collins
Consolidate reweave and join as we have no separate usage, make reweave tests apply to all versionedfile implementations and deprecate the old reweave apis.
47
48
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
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')
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
62
# adapter_registry.register_lazy(('knit-annotated-ft-gz', 'chunked'),
63
#     'bzrlib.knit', 'FTAnnotatedToChunked')
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
64
65
3350.3.3 by Robert Collins
Functional get_record_stream interface tests covering full interface.
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).
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
79
    """
3350.3.3 by Robert Collins
Functional get_record_stream interface tests covering full interface.
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
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
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
3890.2.2 by John Arbash Meinel
Change the signature to report the storage kind as 'chunked'
98
        'chunked'
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
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
3890.2.2 by John Arbash Meinel
Change the signature to report the storage kind as 'chunked'
109
        self.storage_kind = 'chunked'
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
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
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
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').
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
128
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
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
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
150
        elif storage_kind == 'chunked':
151
            return (self._text,)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
152
        raise errors.UnavailableRepresentation(self.key, storage_kind,
153
            self.storage_kind)
154
155
156
class AbsentContentFactory(ContentFactory):
3350.3.12 by Robert Collins
Generate streams with absent records.
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
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
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
3350.3.14 by Robert Collins
Deprecate VersionedFile.join.
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
196
197
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
198
class VersionedFile(object):
199
    """Versioned text file storage.
200
    
201
    A versioned file manages versions of line-based text files,
202
    keeping track of the originating version for each line.
203
204
    To clients the "lines" of the file are represented as a list of
205
    strings. These strings will typically have terminal newline
206
    characters, but this is not required.  In particular files commonly
207
    do not have a newline at the end of the file.
208
209
    Texts are identified by a version-id string.
210
    """
211
2229.2.1 by Aaron Bentley
Reject reserved ids in versiondfile, tree, branch and repository
212
    @staticmethod
2229.2.3 by Aaron Bentley
change reserved_id to is_reserved_id, add check_not_reserved for DRY
213
    def check_not_reserved_id(version_id):
214
        revision.check_not_reserved_id(version_id)
2229.2.1 by Aaron Bentley
Reject reserved ids in versiondfile, tree, branch and repository
215
1563.2.15 by Robert Collins
remove the weavestore assumptions about the number and nature of files it manages.
216
    def copy_to(self, name, transport):
217
        """Copy this versioned file to name on transport."""
218
        raise NotImplementedError(self.copy_to)
1863.1.1 by John Arbash Meinel
Allow Versioned files to do caching if explicitly asked, and implement for Knit
219
3350.3.3 by Robert Collins
Functional get_record_stream interface tests covering full interface.
220
    def get_record_stream(self, versions, ordering, include_delta_closure):
221
        """Get a stream of records for versions.
222
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
3350.3.22 by Robert Collins
Review feedback.
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.
3350.3.3 by Robert Collins
Functional get_record_stream interface tests covering full interface.
232
        :return: An iterator of ContentFactory objects, each of which is only
233
            valid until the iterator is advanced.
234
        """
235
        raise NotImplementedError(self.get_record_stream)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
236
237
    def has_version(self, version_id):
238
        """Returns whether version is present."""
239
        raise NotImplementedError(self.has_version)
240
3350.3.8 by Robert Collins
Basic stream insertion, no fast path yet for knit to knit.
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
2520.4.140 by Aaron Bentley
Use matching blocks from mpdiff for knit delta creation
250
    def add_lines(self, version_id, parents, lines, parent_texts=None,
2805.6.7 by Robert Collins
Review feedback.
251
        left_matching_blocks=None, nostore_sha=None, random_id=False,
252
        check_content=True):
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
253
        """Add a single text on top of the versioned file.
254
255
        Must raise RevisionAlreadyPresent if the new version is
256
        already present in file history.
257
258
        Must raise RevisionNotPresent if any of the given parents are
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
259
        not present in file history.
2805.6.3 by Robert Collins
* The ``VersionedFile`` interface no longer protects against misuse when
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
2805.6.7 by Robert Collins
Review feedback.
267
            correctly is expensive and extremely unlikely to catch bugs so it
268
            is not done at runtime unless check_content is True.)
1596.2.32 by Robert Collins
Reduce re-extraction of texts during weave to knit joins by providing a memoisation facility.
269
        :param parent_texts: An optional dictionary containing the opaque 
2805.6.3 by Robert Collins
* The ``VersionedFile`` interface no longer protects against misuse when
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.
2520.4.148 by Aaron Bentley
Updates from review
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.
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
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.
2805.6.4 by Robert Collins
Don't check for existing versions when adding texts with random revision ids.
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.
2805.6.7 by Robert Collins
Review feedback.
284
        :param check_content: If True, the lines supplied are verified to be
285
            bytestrings that are correctly formed lines.
2776.1.1 by Robert Collins
* The ``add_lines`` methods on ``VersionedFile`` implementations has changed
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.
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
289
        """
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
290
        self._check_write_ok()
2520.4.140 by Aaron Bentley
Use matching blocks from mpdiff for knit delta creation
291
        return self._add_lines(version_id, parents, lines, parent_texts,
2805.6.7 by Robert Collins
Review feedback.
292
            left_matching_blocks, nostore_sha, random_id, check_content)
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
293
2520.4.140 by Aaron Bentley
Use matching blocks from mpdiff for knit delta creation
294
    def _add_lines(self, version_id, parents, lines, parent_texts,
2805.6.7 by Robert Collins
Review feedback.
295
        left_matching_blocks, nostore_sha, random_id, check_content):
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
296
        """Helper to do the class specific add_lines."""
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
297
        raise NotImplementedError(self.add_lines)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
298
1596.2.32 by Robert Collins
Reduce re-extraction of texts during weave to knit joins by providing a memoisation facility.
299
    def add_lines_with_ghosts(self, version_id, parents, lines,
2805.6.7 by Robert Collins
Review feedback.
300
        parent_texts=None, nostore_sha=None, random_id=False,
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
301
        check_content=True, left_matching_blocks=None):
1596.2.32 by Robert Collins
Reduce re-extraction of texts during weave to knit joins by providing a memoisation facility.
302
        """Add lines to the versioned file, allowing ghosts to be present.
303
        
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
304
        This takes the same parameters as add_lines and returns the same.
1596.2.32 by Robert Collins
Reduce re-extraction of texts during weave to knit joins by providing a memoisation facility.
305
        """
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
306
        self._check_write_ok()
1596.2.32 by Robert Collins
Reduce re-extraction of texts during weave to knit joins by providing a memoisation facility.
307
        return self._add_lines_with_ghosts(version_id, parents, lines,
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
308
            parent_texts, nostore_sha, random_id, check_content, left_matching_blocks)
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
309
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
310
    def _add_lines_with_ghosts(self, version_id, parents, lines, parent_texts,
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
311
        nostore_sha, random_id, check_content, left_matching_blocks):
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
312
        """Helper to do class specific add_lines_with_ghosts."""
1594.2.8 by Robert Collins
add ghost aware apis to knits.
313
        raise NotImplementedError(self.add_lines_with_ghosts)
314
1563.2.19 by Robert Collins
stub out a check for knits.
315
    def check(self, progress_bar=None):
316
        """Check the versioned file for integrity."""
317
        raise NotImplementedError(self.check)
318
1666.1.6 by Robert Collins
Make knit the default format.
319
    def _check_lines_not_unicode(self, lines):
320
        """Check that lines being added to a versioned file are not unicode."""
321
        for line in lines:
322
            if line.__class__ is not str:
323
                raise errors.BzrBadParameterUnicode("lines")
324
325
    def _check_lines_are_lines(self, lines):
326
        """Check that the lines really are full lines without inline EOL."""
327
        for line in lines:
328
            if '\n' in line[:-1]:
329
                raise errors.BzrBadParameterContainsNewline("lines")
330
2535.3.1 by Andrew Bennetts
Add get_format_signature to VersionedFile
331
    def get_format_signature(self):
332
        """Get a text description of the data encoding in this file.
333
        
2831.7.1 by Ian Clatworthy
versionedfile.py code cleanups
334
        :since: 0.90
2535.3.1 by Andrew Bennetts
Add get_format_signature to VersionedFile
335
        """
336
        raise NotImplementedError(self.get_format_signature)
337
2520.4.41 by Aaron Bentley
Accelerate mpdiff generation
338
    def make_mpdiffs(self, version_ids):
2831.7.1 by Ian Clatworthy
versionedfile.py code cleanups
339
        """Create multiparent diffs for specified versions."""
2520.4.41 by Aaron Bentley
Accelerate mpdiff generation
340
        knit_versions = set()
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
341
        knit_versions.update(version_ids)
342
        parent_map = self.get_parent_map(version_ids)
2520.4.41 by Aaron Bentley
Accelerate mpdiff generation
343
        for version_id in version_ids:
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
344
            try:
345
                knit_versions.update(parent_map[version_id])
346
            except KeyError:
3453.3.1 by Daniel Fischer
Raise the right exception in make_mpdiffs (bug #235687)
347
                raise errors.RevisionNotPresent(version_id, self)
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
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())
2520.4.90 by Aaron Bentley
Handle \r terminated lines in Weaves properly
350
        lines = dict(zip(knit_versions,
351
            self._get_lf_split_line_list(knit_versions)))
2520.4.41 by Aaron Bentley
Accelerate mpdiff generation
352
        diffs = []
353
        for version_id in version_ids:
354
            target = lines[version_id]
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
355
            try:
356
                parents = [lines[p] for p in parent_map[version_id] if p in
357
                    knit_versions]
358
            except KeyError:
3453.3.2 by John Arbash Meinel
Add a test case for the first loop, unable to find a way to trigger the second loop
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.
3453.3.1 by Daniel Fischer
Raise the right exception in make_mpdiffs (bug #235687)
363
                raise errors.RevisionNotPresent(version_id, self)
2520.4.48 by Aaron Bentley
Support getting blocks from knit deltas with no final EOL
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
2520.4.41 by Aaron Bentley
Accelerate mpdiff generation
369
            diffs.append(multiparent.MultiParent.from_lines(target, parents,
370
                         left_parent_blocks))
371
        return diffs
372
2520.4.48 by Aaron Bentley
Support getting blocks from knit deltas with no final EOL
373
    def _extract_blocks(self, version_id, source, target):
2520.4.41 by Aaron Bentley
Accelerate mpdiff generation
374
        return None
2520.4.3 by Aaron Bentley
Implement plain strategy for extracting and installing multiparent diffs
375
2520.4.61 by Aaron Bentley
Do bulk insertion of records
376
    def add_mpdiffs(self, records):
2831.7.1 by Ian Clatworthy
versionedfile.py code cleanups
377
        """Add mpdiffs to this VersionedFile.
2520.4.126 by Aaron Bentley
Add more docs
378
379
        Records should be iterables of version, parents, expected_sha1,
2831.7.1 by Ian Clatworthy
versionedfile.py code cleanups
380
        mpdiff. mpdiff should be a MultiParent instance.
2520.4.126 by Aaron Bentley
Add more docs
381
        """
2831.7.1 by Ian Clatworthy
versionedfile.py code cleanups
382
        # Does this need to call self._check_write_ok()? (IanC 20070919)
2520.4.61 by Aaron Bentley
Do bulk insertion of records
383
        vf_parents = {}
2520.4.141 by Aaron Bentley
More batch operations adding mpdiffs
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()
2520.4.142 by Aaron Bentley
Clean up installation of inventory records
390
        for version, parent_ids, expected_sha1, mpdiff in records:
2520.4.141 by Aaron Bentley
More batch operations adding mpdiffs
391
            needed_parents.update(p for p in parent_ids
392
                                  if not mpvf.has_version(p))
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
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)):
2520.4.141 by Aaron Bentley
More batch operations adding mpdiffs
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:
2520.4.140 by Aaron Bentley
Use matching blocks from mpdiff for knit delta creation
400
                left_matching_blocks = list(mpdiff.get_matching_blocks(0,
2520.4.141 by Aaron Bentley
More batch operations adding mpdiffs
401
                    mpvf.get_diff(parent_ids[0]).num_lines()))
2520.4.140 by Aaron Bentley
Use matching blocks from mpdiff for knit delta creation
402
            else:
403
                left_matching_blocks = None
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
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)
2520.4.61 by Aaron Bentley
Do bulk insertion of records
414
            vf_parents[version] = version_text
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
415
        sha1s = self.get_sha1s(versions)
416
        for version, parent_ids, expected_sha1, mpdiff in records:
417
            if expected_sha1 != sha1s[version]:
2520.4.71 by Aaron Bentley
Update test to accept VersionedFileInvalidChecksum instead of TestamentMismatch
418
                raise errors.VersionedFileInvalidChecksum(version)
2520.4.3 by Aaron Bentley
Implement plain strategy for extracting and installing multiparent diffs
419
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
420
    def get_text(self, version_id):
421
        """Return version contents as a text string.
422
423
        Raises RevisionNotPresent if version is not present in
424
        file history.
425
        """
426
        return ''.join(self.get_lines(version_id))
427
    get_string = get_text
428
1756.2.1 by Aaron Bentley
Implement get_texts
429
    def get_texts(self, version_ids):
430
        """Return the texts of listed versions as a list of strings.
431
432
        Raises RevisionNotPresent if version is not present in
433
        file history.
434
        """
435
        return [''.join(self.get_lines(v)) for v in version_ids]
436
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
437
    def get_lines(self, version_id):
438
        """Return version contents as a sequence of lines.
439
440
        Raises RevisionNotPresent if version is not present in
441
        file history.
442
        """
443
        raise NotImplementedError(self.get_lines)
444
2520.4.90 by Aaron Bentley
Handle \r terminated lines in Weaves properly
445
    def _get_lf_split_line_list(self, version_ids):
446
        return [StringIO(t).readlines() for t in self.get_texts(version_ids)]
2520.4.3 by Aaron Bentley
Implement plain strategy for extracting and installing multiparent diffs
447
2530.1.1 by Aaron Bentley
Make topological sorting optional for get_ancestry
448
    def get_ancestry(self, version_ids, topo_sorted=True):
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
449
        """Return a list of all ancestors of given version(s). This
450
        will not include the null revision.
451
2490.2.32 by Aaron Bentley
Merge of not-sorting-ancestry branch
452
        This list will not be topologically sorted if topo_sorted=False is
453
        passed.
2530.1.1 by Aaron Bentley
Make topological sorting optional for get_ancestry
454
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
455
        Must raise RevisionNotPresent if any of the given versions are
456
        not present in file history."""
457
        if isinstance(version_ids, basestring):
458
            version_ids = [version_ids]
459
        raise NotImplementedError(self.get_ancestry)
460
        
1594.2.8 by Robert Collins
add ghost aware apis to knits.
461
    def get_ancestry_with_ghosts(self, version_ids):
462
        """Return a list of all ancestors of given version(s). This
463
        will not include the null revision.
464
465
        Must raise RevisionNotPresent if any of the given versions are
466
        not present in file history.
467
        
468
        Ghosts that are known about will be included in ancestry list,
469
        but are not explicitly marked.
470
        """
471
        raise NotImplementedError(self.get_ancestry_with_ghosts)
3316.2.7 by Robert Collins
Actually deprecated VersionedFile.get_graph.
472
    
3287.5.1 by Robert Collins
Add VersionedFile.get_parent_map.
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)
480
1594.2.8 by Robert Collins
add ghost aware apis to knits.
481
    def get_parents_with_ghosts(self, version_id):
482
        """Return version names for parents of version_id.
483
484
        Will raise RevisionNotPresent if version_id is not present
485
        in the history.
486
487
        Ghosts that are known about will be included in the parent list,
488
        but are not explicitly marked.
489
        """
3287.5.1 by Robert Collins
Add VersionedFile.get_parent_map.
490
        try:
491
            return list(self.get_parent_map([version_id])[version_id])
492
        except KeyError:
493
            raise errors.RevisionNotPresent(version_id, self)
1594.2.8 by Robert Collins
add ghost aware apis to knits.
494
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
495
    def annotate(self, version_id):
3316.2.13 by Robert Collins
* ``VersionedFile.annotate_iter`` is deprecated. While in principal this
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.
500
        """
501
        raise NotImplementedError(self.annotate)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
502
2975.3.2 by Robert Collins
Review feedback - document the API change and improve readability in pack's _do_copy_nodes.
503
    def iter_lines_added_or_present_in_versions(self, version_ids=None,
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
504
                                                pb=None):
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
505
        """Iterate over the lines in the versioned file from version_ids.
506
2975.3.2 by Robert Collins
Review feedback - document the API change and improve readability in pack's _do_copy_nodes.
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.
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
513
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
514
        If a progress bar is supplied, it may be used to indicate progress.
515
        The caller is responsible for cleaning up progress bars (because this
516
        is an iterator).
517
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
518
        NOTES: Lines are normalised: they will all have \n terminators.
519
               Lines are returned in arbitrary order.
2975.3.2 by Robert Collins
Review feedback - document the API change and improve readability in pack's _do_copy_nodes.
520
521
        :return: An iterator over (line, version_id).
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
522
        """
523
        raise NotImplementedError(self.iter_lines_added_or_present_in_versions)
524
1551.6.15 by Aaron Bentley
Moved plan_merge into Weave
525
    def plan_merge(self, ver_a, ver_b):
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
526
        """Return pseudo-annotation indicating how the two versions merge.
527
528
        This is computed between versions a and b and their common
529
        base.
530
531
        Weave lines present in none of them are skipped entirely.
1664.2.2 by Aaron Bentley
Added legend for plan-merge output
532
533
        Legend:
534
        killed-base Dead in base revision
535
        killed-both Killed in each revision
536
        killed-a    Killed in a
537
        killed-b    Killed in b
538
        unchanged   Alive in both a and b (possibly created in both)
539
        new-a       Created in a
540
        new-b       Created in b
1664.2.5 by Aaron Bentley
Update plan-merge legend
541
        ghost-a     Killed in a, unborn in b    
542
        ghost-b     Killed in b, unborn in a
1664.2.2 by Aaron Bentley
Added legend for plan-merge output
543
        irrelevant  Not in either revision
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
544
        """
1551.6.15 by Aaron Bentley
Moved plan_merge into Weave
545
        raise NotImplementedError(VersionedFile.plan_merge)
546
        
1996.3.7 by John Arbash Meinel
lazy import versionedfile, late-load bzrlib.merge
547
    def weave_merge(self, plan, a_marker=TextMerge.A_MARKER,
1551.6.14 by Aaron Bentley
Tweaks from merge review
548
                    b_marker=TextMerge.B_MARKER):
1551.6.12 by Aaron Bentley
Indicate conflicts from merge_lines, insead of guessing
549
        return PlanWeaveMerge(plan, a_marker, b_marker).merge_lines()[0]
1551.6.10 by Aaron Bentley
Renamed WeaveMerge to PlanMerge, added plan method, created planless WeaveMerge
550
1664.2.7 by Aaron Bentley
Merge bzr.dev
551
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
552
class RecordingVersionedFilesDecorator(object):
553
    """A minimal versioned files that records calls made on it.
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
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):
3871.4.1 by John Arbash Meinel
Add a VFDecorator that can yield records in a specified order
562
        """Create a RecordingVersionedFilesDecorator decorating backing_vf.
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
563
        
564
        :param backing_vf: The versioned file to answer all methods.
565
        """
566
        self._backing_vf = backing_vf
567
        self.calls = []
568
3350.8.2 by Robert Collins
stacked get_parent_map.
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
3517.4.19 by Martin Pool
Update test for knit.check() to expect it to recurse into fallback vfs
577
    def check(self):
578
        self._backing_vf.check()
579
3350.8.2 by Robert Collins
stacked get_parent_map.
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
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
584
    def get_record_stream(self, keys, sort_order, include_delta_closure):
3350.8.7 by Robert Collins
get_record_stream for fulltexts working (but note extreme memory use!).
585
        self.calls.append(("get_record_stream", list(keys), sort_order,
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
586
            include_delta_closure))
587
        return self._backing_vf.get_record_stream(keys, sort_order,
588
            include_delta_closure)
589
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
590
    def get_sha1s(self, keys):
591
        self.calls.append(("get_sha1s", copy(keys)))
592
        return self._backing_vf.get_sha1s(keys)
593
3350.8.5 by Robert Collins
Iter_lines_added_or_present_in_keys stacks.
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)))
3350.8.14 by Robert Collins
Review feedback.
596
        return self._backing_vf.iter_lines_added_or_present_in_keys(keys, pb=pb)
3350.8.5 by Robert Collins
Iter_lines_added_or_present_in_keys stacks.
597
3350.8.4 by Robert Collins
Vf.keys() stacking support.
598
    def keys(self):
599
        self.calls.append(("keys",))
600
        return self._backing_vf.keys()
601
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
602
3871.4.1 by John Arbash Meinel
Add a VFDecorator that can yield records in a specified order
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
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
641
class KeyMapper(object):
3350.6.10 by Martin Pool
VersionedFiles review cleanups
642
    """KeyMappers map between keys and underlying partitioned storage."""
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
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.
3350.6.10 by Martin Pool
VersionedFiles review cleanups
657
        :return: As much of a key (or prefix) as is derivable from the partition
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
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.
3350.6.7 by Robert Collins
Review feedback, making things more clear, adding documentation on what is used where.
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.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
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
3735.2.5 by Robert Collins
Teach VersionedFiles how to allocate keys based on content hashes.
794
        :param key: The key tuple of the text to add. If the last element is
795
            None, a CHK string will be generated during the addition.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
796
        :param parents: The parents key tuples of the text to add.
797
        :param lines: A list of lines. Each line must be a bytestring. And all
798
            of them except the last must be terminated with \n and contain no
799
            other \n's. The last line may either contain no \n's or a single
800
            terminating \n. If the lines list does meet this constraint the add
801
            routine may error or may succeed - but you will be unable to read
802
            the data back accurately. (Checking the lines have been split
803
            correctly is expensive and extremely unlikely to catch bugs so it
804
            is not done at runtime unless check_content is True.)
805
        :param parent_texts: An optional dictionary containing the opaque 
806
            representations of some or all of the parents of version_id to
807
            allow delta optimisations.  VERY IMPORTANT: the texts must be those
808
            returned by add_lines or data corruption can be caused.
809
        :param left_matching_blocks: a hint about which areas are common
810
            between the text and its left-hand-parent.  The format is
811
            the SequenceMatcher.get_matching_blocks format.
812
        :param nostore_sha: Raise ExistingContent and do not add the lines to
813
            the versioned file if the digest of the lines matches this.
814
        :param random_id: If True a random id has been selected rather than
815
            an id determined by some deterministic process such as a converter
816
            from a foreign VCS. When True the backend may choose not to check
817
            for uniqueness of the resulting key within the versioned file, so
818
            this should only be done when the result is expected to be unique
819
            anyway.
820
        :param check_content: If True, the lines supplied are verified to be
821
            bytestrings that are correctly formed lines.
822
        :return: The text sha1, the number of bytes in the text, and an opaque
823
                 representation of the inserted version which can be provided
824
                 back to future add_lines calls in the parent_texts dictionary.
825
        """
826
        raise NotImplementedError(self.add_lines)
827
828
    def add_mpdiffs(self, records):
829
        """Add mpdiffs to this VersionedFile.
830
831
        Records should be iterables of version, parents, expected_sha1,
832
        mpdiff. mpdiff should be a MultiParent instance.
833
        """
834
        vf_parents = {}
835
        mpvf = multiparent.MultiMemoryVersionedFile()
836
        versions = []
837
        for version, parent_ids, expected_sha1, mpdiff in records:
838
            versions.append(version)
839
            mpvf.add_diff(mpdiff, version, parent_ids)
840
        needed_parents = set()
841
        for version, parent_ids, expected_sha1, mpdiff in records:
842
            needed_parents.update(p for p in parent_ids
843
                                  if not mpvf.has_version(p))
844
        # It seems likely that adding all the present parents as fulltexts can
845
        # easily exhaust memory.
3890.2.9 by John Arbash Meinel
Start using osutils.chunks_as_lines rather than osutils.split_lines.
846
        chunks_to_lines = osutils.chunks_to_lines
3350.8.11 by Robert Collins
Stacked add_mpdiffs.
847
        for record in self.get_record_stream(needed_parents, 'unordered',
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
848
            True):
3350.8.11 by Robert Collins
Stacked add_mpdiffs.
849
            if record.storage_kind == 'absent':
850
                continue
3890.2.9 by John Arbash Meinel
Start using osutils.chunks_as_lines rather than osutils.split_lines.
851
            mpvf.add_version(chunks_to_lines(record.get_bytes_as('chunked')),
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
852
                record.key, [])
853
        for (key, parent_keys, expected_sha1, mpdiff), lines in\
854
            zip(records, mpvf.get_line_list(versions)):
855
            if len(parent_keys) == 1:
856
                left_matching_blocks = list(mpdiff.get_matching_blocks(0,
857
                    mpvf.get_diff(parent_keys[0]).num_lines()))
858
            else:
859
                left_matching_blocks = None
860
            version_sha1, _, version_text = self.add_lines(key,
861
                parent_keys, lines, vf_parents,
862
                left_matching_blocks=left_matching_blocks)
863
            if version_sha1 != expected_sha1:
864
                raise errors.VersionedFileInvalidChecksum(version)
865
            vf_parents[key] = version_text
866
867
    def annotate(self, key):
868
        """Return a list of (version-key, line) tuples for the text of key.
869
870
        :raise RevisionNotPresent: If the key is not present.
871
        """
872
        raise NotImplementedError(self.annotate)
873
874
    def check(self, progress_bar=None):
875
        """Check this object for integrity."""
876
        raise NotImplementedError(self.check)
877
878
    @staticmethod
879
    def check_not_reserved_id(version_id):
880
        revision.check_not_reserved_id(version_id)
881
882
    def _check_lines_not_unicode(self, lines):
883
        """Check that lines being added to a versioned file are not unicode."""
884
        for line in lines:
885
            if line.__class__ is not str:
886
                raise errors.BzrBadParameterUnicode("lines")
887
888
    def _check_lines_are_lines(self, lines):
889
        """Check that the lines really are full lines without inline EOL."""
890
        for line in lines:
891
            if '\n' in line[:-1]:
892
                raise errors.BzrBadParameterContainsNewline("lines")
893
894
    def get_parent_map(self, keys):
895
        """Get a map of the parents of keys.
896
897
        :param keys: The keys to look up parents for.
898
        :return: A mapping from keys to parents. Absent keys are absent from
899
            the mapping.
900
        """
901
        raise NotImplementedError(self.get_parent_map)
902
903
    def get_record_stream(self, keys, ordering, include_delta_closure):
904
        """Get a stream of records for keys.
905
906
        :param keys: The keys to include.
907
        :param ordering: Either 'unordered' or 'topological'. A topologically
908
            sorted stream has compression parents strictly before their
909
            children.
910
        :param include_delta_closure: If True then the closure across any
911
            compression parents will be included (in the opaque data).
912
        :return: An iterator of ContentFactory objects, each of which is only
913
            valid until the iterator is advanced.
914
        """
915
        raise NotImplementedError(self.get_record_stream)
916
917
    def get_sha1s(self, keys):
918
        """Get the sha1's of the texts for the given keys.
919
920
        :param keys: The names of the keys to lookup
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
921
        :return: a dict from key to sha1 digest. Keys of texts which are not
3350.8.14 by Robert Collins
Review feedback.
922
            present in the store are not present in the returned
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
923
            dictionary.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
924
        """
925
        raise NotImplementedError(self.get_sha1s)
926
3830.3.12 by Martin Pool
Review cleanups: unify has_key impls, add missing_keys(), clean up exception blocks
927
    has_key = index._has_key_from_parent_map
928
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
929
    def insert_record_stream(self, stream):
930
        """Insert a record stream into this container.
931
932
        :param stream: A stream of records to insert. 
933
        :return: None
934
        :seealso VersionedFile.get_record_stream:
935
        """
936
        raise NotImplementedError
937
938
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
939
        """Iterate over the lines in the versioned files from keys.
940
941
        This may return lines from other keys. Each item the returned
942
        iterator yields is a tuple of a line and a text version that that line
943
        is present in (not introduced in).
944
945
        Ordering of results is in whatever order is most suitable for the
946
        underlying storage format.
947
948
        If a progress bar is supplied, it may be used to indicate progress.
949
        The caller is responsible for cleaning up progress bars (because this
950
        is an iterator).
951
952
        NOTES:
953
         * Lines are normalised by the underlying store: they will all have \n
954
           terminators.
955
         * Lines are returned in arbitrary order.
956
957
        :return: An iterator over (line, key).
958
        """
959
        raise NotImplementedError(self.iter_lines_added_or_present_in_keys)
960
961
    def keys(self):
962
        """Return a iterable of the keys for all the contained texts."""
963
        raise NotImplementedError(self.keys)
964
965
    def make_mpdiffs(self, keys):
966
        """Create multiparent diffs for specified keys."""
967
        keys_order = tuple(keys)
968
        keys = frozenset(keys)
969
        knit_keys = set(keys)
970
        parent_map = self.get_parent_map(keys)
971
        for parent_keys in parent_map.itervalues():
972
            if parent_keys:
973
                knit_keys.update(parent_keys)
974
        missing_keys = keys - set(parent_map)
975
        if missing_keys:
3530.3.2 by Robert Collins
Handling frozen set inputs in mpdiff generation when a key is missing
976
            raise errors.RevisionNotPresent(list(missing_keys)[0], self)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
977
        # We need to filter out ghosts, because we can't diff against them.
978
        maybe_ghosts = knit_keys - keys
979
        ghosts = maybe_ghosts - set(self.get_parent_map(maybe_ghosts))
980
        knit_keys.difference_update(ghosts)
981
        lines = {}
3890.2.9 by John Arbash Meinel
Start using osutils.chunks_as_lines rather than osutils.split_lines.
982
        chunks_to_lines = osutils.chunks_to_lines
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
983
        for record in self.get_record_stream(knit_keys, 'topological', True):
3890.2.9 by John Arbash Meinel
Start using osutils.chunks_as_lines rather than osutils.split_lines.
984
            lines[record.key] = chunks_to_lines(record.get_bytes_as('chunked'))
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
985
            # line_block_dict = {}
986
            # for parent, blocks in record.extract_line_blocks():
987
            #   line_blocks[parent] = blocks
988
            # line_blocks[record.key] = line_block_dict
989
        diffs = []
990
        for key in keys_order:
991
            target = lines[key]
992
            parents = parent_map[key] or []
993
            # Note that filtering knit_keys can lead to a parent difference
994
            # between the creation and the application of the mpdiff.
995
            parent_lines = [lines[p] for p in parents if p in knit_keys]
996
            if len(parent_lines) > 0:
997
                left_parent_blocks = self._extract_blocks(key, parent_lines[0],
998
                    target)
999
            else:
1000
                left_parent_blocks = None
1001
            diffs.append(multiparent.MultiParent.from_lines(target,
1002
                parent_lines, left_parent_blocks))
1003
        return diffs
1004
3830.3.12 by Martin Pool
Review cleanups: unify has_key impls, add missing_keys(), clean up exception blocks
1005
    missing_keys = index._missing_keys_from_parent_map
1006
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1007
    def _extract_blocks(self, version_id, source, target):
1008
        return None
1009
1010
1011
class ThunkedVersionedFiles(VersionedFiles):
1012
    """Storage for many versioned files thunked onto a 'VersionedFile' class.
1013
1014
    This object allows a single keyspace for accessing the history graph and
1015
    contents of named bytestrings.
1016
1017
    Currently no implementation allows the graph of different key prefixes to
1018
    intersect, but the API does allow such implementations in the future.
1019
    """
1020
1021
    def __init__(self, transport, file_factory, mapper, is_locked):
1022
        """Create a ThunkedVersionedFiles."""
1023
        self._transport = transport
1024
        self._file_factory = file_factory
1025
        self._mapper = mapper
1026
        self._is_locked = is_locked
1027
1028
    def add_lines(self, key, parents, lines, parent_texts=None,
1029
        left_matching_blocks=None, nostore_sha=None, random_id=False,
1030
        check_content=True):
1031
        """See VersionedFiles.add_lines()."""
1032
        path = self._mapper.map(key)
1033
        version_id = key[-1]
1034
        parents = [parent[-1] for parent in parents]
1035
        vf = self._get_vf(path)
1036
        try:
1037
            try:
1038
                return vf.add_lines_with_ghosts(version_id, parents, lines,
1039
                    parent_texts=parent_texts,
1040
                    left_matching_blocks=left_matching_blocks,
1041
                    nostore_sha=nostore_sha, random_id=random_id,
1042
                    check_content=check_content)
1043
            except NotImplementedError:
1044
                return vf.add_lines(version_id, parents, lines,
1045
                    parent_texts=parent_texts,
1046
                    left_matching_blocks=left_matching_blocks,
1047
                    nostore_sha=nostore_sha, random_id=random_id,
1048
                    check_content=check_content)
1049
        except errors.NoSuchFile:
1050
            # parent directory may be missing, try again.
1051
            self._transport.mkdir(osutils.dirname(path))
1052
            try:
1053
                return vf.add_lines_with_ghosts(version_id, parents, lines,
1054
                    parent_texts=parent_texts,
1055
                    left_matching_blocks=left_matching_blocks,
1056
                    nostore_sha=nostore_sha, random_id=random_id,
1057
                    check_content=check_content)
1058
            except NotImplementedError:
1059
                return vf.add_lines(version_id, parents, lines,
1060
                    parent_texts=parent_texts,
1061
                    left_matching_blocks=left_matching_blocks,
1062
                    nostore_sha=nostore_sha, random_id=random_id,
1063
                    check_content=check_content)
1064
1065
    def annotate(self, key):
1066
        """Return a list of (version-key, line) tuples for the text of key.
1067
1068
        :raise RevisionNotPresent: If the key is not present.
1069
        """
1070
        prefix = key[:-1]
1071
        path = self._mapper.map(prefix)
1072
        vf = self._get_vf(path)
1073
        origins = vf.annotate(key[-1])
1074
        result = []
1075
        for origin, line in origins:
1076
            result.append((prefix + (origin,), line))
1077
        return result
1078
1079
    def check(self, progress_bar=None):
1080
        """See VersionedFiles.check()."""
1081
        for prefix, vf in self._iter_all_components():
1082
            vf.check()
1083
1084
    def get_parent_map(self, keys):
1085
        """Get a map of the parents of keys.
1086
1087
        :param keys: The keys to look up parents for.
1088
        :return: A mapping from keys to parents. Absent keys are absent from
1089
            the mapping.
1090
        """
1091
        prefixes = self._partition_keys(keys)
1092
        result = {}
1093
        for prefix, suffixes in prefixes.items():
1094
            path = self._mapper.map(prefix)
1095
            vf = self._get_vf(path)
1096
            parent_map = vf.get_parent_map(suffixes)
1097
            for key, parents in parent_map.items():
1098
                result[prefix + (key,)] = tuple(
1099
                    prefix + (parent,) for parent in parents)
1100
        return result
1101
1102
    def _get_vf(self, path):
1103
        if not self._is_locked():
1104
            raise errors.ObjectNotLocked(self)
1105
        return self._file_factory(path, self._transport, create=True,
1106
            get_scope=lambda:None)
1107
1108
    def _partition_keys(self, keys):
1109
        """Turn keys into a dict of prefix:suffix_list."""
1110
        result = {}
1111
        for key in keys:
1112
            prefix_keys = result.setdefault(key[:-1], [])
1113
            prefix_keys.append(key[-1])
1114
        return result
1115
1116
    def _get_all_prefixes(self):
1117
        # Identify all key prefixes.
1118
        # XXX: A bit hacky, needs polish.
1119
        if type(self._mapper) == ConstantMapper:
1120
            paths = [self._mapper.map(())]
1121
            prefixes = [()]
1122
        else:
1123
            relpaths = set()
1124
            for quoted_relpath in self._transport.iter_files_recursive():
1125
                path, ext = os.path.splitext(quoted_relpath)
1126
                relpaths.add(path)
1127
            paths = list(relpaths)
1128
            prefixes = [self._mapper.unmap(path) for path in paths]
1129
        return zip(paths, prefixes)
1130
1131
    def get_record_stream(self, keys, ordering, include_delta_closure):
1132
        """See VersionedFiles.get_record_stream()."""
1133
        # Ordering will be taken care of by each partitioned store; group keys
1134
        # by partition.
1135
        keys = sorted(keys)
1136
        for prefix, suffixes, vf in self._iter_keys_vf(keys):
1137
            suffixes = [(suffix,) for suffix in suffixes]
1138
            for record in vf.get_record_stream(suffixes, ordering,
1139
                include_delta_closure):
1140
                if record.parents is not None:
1141
                    record.parents = tuple(
1142
                        prefix + parent for parent in record.parents)
1143
                record.key = prefix + record.key
1144
                yield record
1145
1146
    def _iter_keys_vf(self, keys):
1147
        prefixes = self._partition_keys(keys)
1148
        sha1s = {}
1149
        for prefix, suffixes in prefixes.items():
1150
            path = self._mapper.map(prefix)
1151
            vf = self._get_vf(path)
1152
            yield prefix, suffixes, vf
1153
1154
    def get_sha1s(self, keys):
1155
        """See VersionedFiles.get_sha1s()."""
1156
        sha1s = {}
1157
        for prefix,suffixes, vf in self._iter_keys_vf(keys):
1158
            vf_sha1s = vf.get_sha1s(suffixes)
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
1159
            for suffix, sha1 in vf_sha1s.iteritems():
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1160
                sha1s[prefix + (suffix,)] = sha1
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
1161
        return sha1s
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1162
1163
    def insert_record_stream(self, stream):
1164
        """Insert a record stream into this container.
1165
1166
        :param stream: A stream of records to insert. 
1167
        :return: None
1168
        :seealso VersionedFile.get_record_stream:
1169
        """
1170
        for record in stream:
1171
            prefix = record.key[:-1]
1172
            key = record.key[-1:]
1173
            if record.parents is not None:
1174
                parents = [parent[-1:] for parent in record.parents]
1175
            else:
1176
                parents = None
1177
            thunk_record = AdapterFactory(key, parents, record)
1178
            path = self._mapper.map(prefix)
1179
            # Note that this parses the file many times; we can do better but
1180
            # as this only impacts weaves in terms of performance, it is
1181
            # tolerable.
1182
            vf = self._get_vf(path)
1183
            vf.insert_record_stream([thunk_record])
1184
1185
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
1186
        """Iterate over the lines in the versioned files from keys.
1187
1188
        This may return lines from other keys. Each item the returned
1189
        iterator yields is a tuple of a line and a text version that that line
1190
        is present in (not introduced in).
1191
1192
        Ordering of results is in whatever order is most suitable for the
1193
        underlying storage format.
1194
1195
        If a progress bar is supplied, it may be used to indicate progress.
1196
        The caller is responsible for cleaning up progress bars (because this
1197
        is an iterator).
1198
1199
        NOTES:
1200
         * Lines are normalised by the underlying store: they will all have \n
1201
           terminators.
1202
         * Lines are returned in arbitrary order.
1203
1204
        :return: An iterator over (line, key).
1205
        """
1206
        for prefix, suffixes, vf in self._iter_keys_vf(keys):
1207
            for line, version in vf.iter_lines_added_or_present_in_versions(suffixes):
1208
                yield line, prefix + (version,)
1209
1210
    def _iter_all_components(self):
1211
        for path, prefix in self._get_all_prefixes():
1212
            yield prefix, self._get_vf(path)
1213
1214
    def keys(self):
1215
        """See VersionedFiles.keys()."""
1216
        result = set()
1217
        for prefix, vf in self._iter_all_components():
1218
            for suffix in vf.versions():
1219
                result.add(prefix + (suffix,))
1220
        return result
1221
1222
1223
class _PlanMergeVersionedFile(VersionedFiles):
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1224
    """A VersionedFile for uncommitted and committed texts.
1225
1226
    It is intended to allow merges to be planned with working tree texts.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1227
    It implements only the small part of the VersionedFiles interface used by
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1228
    PlanMerge.  It falls back to multiple versionedfiles for data not stored in
1229
    _PlanMergeVersionedFile itself.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1230
1231
    :ivar: fallback_versionedfiles a list of VersionedFiles objects that can be
1232
        queried for missing texts.
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1233
    """
1234
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1235
    def __init__(self, file_id):
1236
        """Create a _PlanMergeVersionedFile.
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1237
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1238
        :param file_id: Used with _PlanMerge code which is not yet fully
1239
            tuple-keyspace aware.
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1240
        """
1241
        self._file_id = file_id
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1242
        # fallback locations
1243
        self.fallback_versionedfiles = []
1244
        # Parents for locally held keys.
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1245
        self._parents = {}
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1246
        # line data for locally held keys.
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1247
        self._lines = {}
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1248
        # key lookup providers
1249
        self._providers = [DictParentsProvider(self._parents)]
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1250
3062.2.3 by Aaron Bentley
Sync up with bzr.dev API changes
1251
    def plan_merge(self, ver_a, ver_b, base=None):
3062.1.13 by Aaron Bentley
Make _PlanMerge an implementation detail of _PlanMergeVersionedFile
1252
        """See VersionedFile.plan_merge"""
3144.3.7 by Aaron Bentley
Update from review
1253
        from bzrlib.merge import _PlanMerge
3062.2.3 by Aaron Bentley
Sync up with bzr.dev API changes
1254
        if base is None:
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1255
            return _PlanMerge(ver_a, ver_b, self, (self._file_id,)).plan_merge()
1256
        old_plan = list(_PlanMerge(ver_a, base, self, (self._file_id,)).plan_merge())
1257
        new_plan = list(_PlanMerge(ver_a, ver_b, self, (self._file_id,)).plan_merge())
3062.2.3 by Aaron Bentley
Sync up with bzr.dev API changes
1258
        return _PlanMerge._subtract_plans(old_plan, new_plan)
1259
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
1260
    def plan_lca_merge(self, ver_a, ver_b, base=None):
3144.3.7 by Aaron Bentley
Update from review
1261
        from bzrlib.merge import _PlanLCAMerge
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1262
        graph = Graph(self)
1263
        new_plan = _PlanLCAMerge(ver_a, ver_b, self, (self._file_id,), graph).plan_merge()
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
1264
        if base is None:
1265
            return new_plan
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1266
        old_plan = _PlanLCAMerge(ver_a, base, self, (self._file_id,), graph).plan_merge()
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
1267
        return _PlanLCAMerge._subtract_plans(list(old_plan), list(new_plan))
3062.1.13 by Aaron Bentley
Make _PlanMerge an implementation detail of _PlanMergeVersionedFile
1268
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1269
    def add_lines(self, key, parents, lines):
1270
        """See VersionedFiles.add_lines
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1271
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1272
        Lines are added locally, not to fallback versionedfiles.  Also, ghosts
1273
        are permitted.  Only reserved ids are permitted.
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1274
        """
3350.6.8 by Martin Pool
Change stray pdb calls to exceptions
1275
        if type(key) is not tuple:
1276
            raise TypeError(key)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1277
        if not revision.is_reserved_id(key[-1]):
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1278
            raise ValueError('Only reserved ids may be used')
1279
        if parents is None:
1280
            raise ValueError('Parents may not be None')
1281
        if lines is None:
1282
            raise ValueError('Lines may not be None')
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1283
        self._parents[key] = tuple(parents)
1284
        self._lines[key] = lines
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1285
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1286
    def get_record_stream(self, keys, ordering, include_delta_closure):
1287
        pending = set(keys)
1288
        for key in keys:
1289
            if key in self._lines:
1290
                lines = self._lines[key]
1291
                parents = self._parents[key]
1292
                pending.remove(key)
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
1293
                yield ChunkedContentFactory(key, parents, None, lines)
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1294
        for versionedfile in self.fallback_versionedfiles:
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1295
            for record in versionedfile.get_record_stream(
1296
                pending, 'unordered', True):
1297
                if record.storage_kind == 'absent':
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1298
                    continue
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1299
                else:
1300
                    pending.remove(record.key)
1301
                    yield record
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
1302
            if not pending:
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1303
                return
1304
        # report absent entries
1305
        for key in pending:
1306
            yield AbsentContentFactory(key)
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1307
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1308
    def get_parent_map(self, keys):
1309
        """See VersionedFiles.get_parent_map"""
1310
        # We create a new provider because a fallback may have been added.
1311
        # If we make fallbacks private we can update a stack list and avoid
1312
        # object creation thrashing.
3350.6.6 by Robert Collins
Fix test_plan_file_merge
1313
        keys = set(keys)
1314
        result = {}
1315
        if revision.NULL_REVISION in keys:
1316
            keys.remove(revision.NULL_REVISION)
1317
            result[revision.NULL_REVISION] = ()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1318
        self._providers = self._providers[:1] + self.fallback_versionedfiles
3350.6.6 by Robert Collins
Fix test_plan_file_merge
1319
        result.update(
1320
            _StackedParentsProvider(self._providers).get_parent_map(keys))
3350.6.5 by Robert Collins
Update to bzr.dev.
1321
        for key, parents in result.iteritems():
1322
            if parents == ():
1323
                result[key] = (revision.NULL_REVISION,)
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
1324
        return result
3144.3.1 by Aaron Bentley
Implement LCA merge, with problematic conflict markers
1325
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
1326
1551.6.10 by Aaron Bentley
Renamed WeaveMerge to PlanMerge, added plan method, created planless WeaveMerge
1327
class PlanWeaveMerge(TextMerge):
1551.6.13 by Aaron Bentley
Cleanup
1328
    """Weave merge that takes a plan as its input.
1329
    
1551.6.14 by Aaron Bentley
Tweaks from merge review
1330
    This exists so that VersionedFile.plan_merge is implementable.
1331
    Most callers will want to use WeaveMerge instead.
1551.6.13 by Aaron Bentley
Cleanup
1332
    """
1333
1551.6.14 by Aaron Bentley
Tweaks from merge review
1334
    def __init__(self, plan, a_marker=TextMerge.A_MARKER,
1335
                 b_marker=TextMerge.B_MARKER):
1551.6.10 by Aaron Bentley
Renamed WeaveMerge to PlanMerge, added plan method, created planless WeaveMerge
1336
        TextMerge.__init__(self, a_marker, b_marker)
1337
        self.plan = plan
1338
1551.6.7 by Aaron Bentley
Implemented two-way merge, refactored weave merge
1339
    def _merge_struct(self):
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
1340
        lines_a = []
1341
        lines_b = []
1342
        ch_a = ch_b = False
1664.2.8 by Aaron Bentley
Fix WeaveMerge when plan doesn't end with unchanged lines
1343
1344
        def outstanding_struct():
1345
            if not lines_a and not lines_b:
1346
                return
1347
            elif ch_a and not ch_b:
1348
                # one-sided change:
1349
                yield(lines_a,)
1350
            elif ch_b and not ch_a:
1351
                yield (lines_b,)
1352
            elif lines_a == lines_b:
1353
                yield(lines_a,)
1354
            else:
1355
                yield (lines_a, lines_b)
1551.6.13 by Aaron Bentley
Cleanup
1356
       
1616.1.18 by Martin Pool
(weave-merge) don't treat killed-both lines as points of agreement;
1357
        # We previously considered either 'unchanged' or 'killed-both' lines
1358
        # to be possible places to resynchronize.  However, assuming agreement
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1359
        # on killed-both lines may be too aggressive. -- mbp 20060324
1551.6.7 by Aaron Bentley
Implemented two-way merge, refactored weave merge
1360
        for state, line in self.plan:
1616.1.18 by Martin Pool
(weave-merge) don't treat killed-both lines as points of agreement;
1361
            if state == 'unchanged':
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
1362
                # resync and flush queued conflicts changes if any
1664.2.8 by Aaron Bentley
Fix WeaveMerge when plan doesn't end with unchanged lines
1363
                for struct in outstanding_struct():
1364
                    yield struct
1551.6.11 by Aaron Bentley
Switched TextMerge_lines to work on a list
1365
                lines_a = []
1366
                lines_b = []
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
1367
                ch_a = ch_b = False
1368
                
1369
            if state == 'unchanged':
1370
                if line:
1551.6.5 by Aaron Bentley
Got weave merge producing structural output
1371
                    yield ([line],)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
1372
            elif state == 'killed-a':
1373
                ch_a = True
1374
                lines_b.append(line)
1375
            elif state == 'killed-b':
1376
                ch_b = True
1377
                lines_a.append(line)
1378
            elif state == 'new-a':
1379
                ch_a = True
1380
                lines_a.append(line)
1381
            elif state == 'new-b':
1382
                ch_b = True
1383
                lines_b.append(line)
3144.3.2 by Aaron Bentley
Get conflict handling working
1384
            elif state == 'conflicted-a':
1385
                ch_b = ch_a = True
1386
                lines_a.append(line)
1387
            elif state == 'conflicted-b':
1388
                ch_b = ch_a = True
1389
                lines_b.append(line)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
1390
            else:
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
1391
                if state not in ('irrelevant', 'ghost-a', 'ghost-b',
1392
                        'killed-base', 'killed-both'):
1393
                    raise AssertionError(state)
1664.2.8 by Aaron Bentley
Fix WeaveMerge when plan doesn't end with unchanged lines
1394
        for struct in outstanding_struct():
1395
            yield struct
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
1396
1664.2.14 by Aaron Bentley
spacing fix
1397
1551.6.10 by Aaron Bentley
Renamed WeaveMerge to PlanMerge, added plan method, created planless WeaveMerge
1398
class WeaveMerge(PlanWeaveMerge):
2831.7.1 by Ian Clatworthy
versionedfile.py code cleanups
1399
    """Weave merge that takes a VersionedFile and two versions as its input."""
1551.6.13 by Aaron Bentley
Cleanup
1400
1551.6.10 by Aaron Bentley
Renamed WeaveMerge to PlanMerge, added plan method, created planless WeaveMerge
1401
    def __init__(self, versionedfile, ver_a, ver_b, 
1551.6.14 by Aaron Bentley
Tweaks from merge review
1402
        a_marker=PlanWeaveMerge.A_MARKER, b_marker=PlanWeaveMerge.B_MARKER):
1551.6.15 by Aaron Bentley
Moved plan_merge into Weave
1403
        plan = versionedfile.plan_merge(ver_a, ver_b)
1551.6.10 by Aaron Bentley
Renamed WeaveMerge to PlanMerge, added plan method, created planless WeaveMerge
1404
        PlanWeaveMerge.__init__(self, plan, a_marker, b_marker)
1405
1406
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
1407
class VirtualVersionedFiles(VersionedFiles):
1408
    """Dummy implementation for VersionedFiles that uses other functions for 
1409
    obtaining fulltexts and parent maps.
1410
1411
    This is always on the bottom of the stack and uses string keys 
1412
    (rather than tuples) internally.
1413
    """
1414
1415
    def __init__(self, get_parent_map, get_lines):
1416
        """Create a VirtualVersionedFiles.
1417
1418
        :param get_parent_map: Same signature as Repository.get_parent_map.
1419
        :param get_lines: Should return lines for specified key or None if 
1420
                          not available.
1421
        """
1422
        super(VirtualVersionedFiles, self).__init__()
1423
        self._get_parent_map = get_parent_map
1424
        self._get_lines = get_lines
1425
        
1426
    def check(self, progressbar=None):
1427
        """See VersionedFiles.check.
1428
1429
        :note: Always returns True for VirtualVersionedFiles.
1430
        """
1431
        return True
1432
1433
    def add_mpdiffs(self, records):
1434
        """See VersionedFiles.mpdiffs.
1435
1436
        :note: Not implemented for VirtualVersionedFiles.
1437
        """
1438
        raise NotImplementedError(self.add_mpdiffs)
1439
1440
    def get_parent_map(self, keys):
1441
        """See VersionedFiles.get_parent_map."""
3518.1.2 by Jelmer Vernooij
Fix some stylistic issues pointed out by Ian.
1442
        return dict([((k,), tuple([(p,) for p in v]))
1443
            for k,v in self._get_parent_map([k for (k,) in keys]).iteritems()])
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
1444
1445
    def get_sha1s(self, keys):
1446
        """See VersionedFiles.get_sha1s."""
1447
        ret = {}
1448
        for (k,) in keys:
1449
            lines = self._get_lines(k)
1450
            if lines is not None:
3518.1.2 by Jelmer Vernooij
Fix some stylistic issues pointed out by Ian.
1451
                if not isinstance(lines, list):
1452
                    raise AssertionError
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
1453
                ret[(k,)] = osutils.sha_strings(lines)
1454
        return ret
1455
1456
    def get_record_stream(self, keys, ordering, include_delta_closure):
1457
        """See VersionedFiles.get_record_stream."""
1458
        for (k,) in list(keys):
1459
            lines = self._get_lines(k)
1460
            if lines is not None:
3518.1.2 by Jelmer Vernooij
Fix some stylistic issues pointed out by Ian.
1461
                if not isinstance(lines, list):
1462
                    raise AssertionError
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
1463
                yield ChunkedContentFactory((k,), None,
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
1464
                        sha1=osutils.sha_strings(lines),
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
1465
                        chunks=lines)
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
1466
            else:
1467
                yield AbsentContentFactory((k,))
1468
1469
1470