~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/repository.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-08-17 07:52:09 UTC
  • mfrom: (1910.3.4 trivial)
  • Revision ID: pqm@pqm.ubuntu.com-20060817075209-e85a1f9e05ff8b87
(andrew) Trivial fixes to NotImplemented errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
 
17
from binascii import hexlify
 
18
from copy import deepcopy
17
19
from cStringIO import StringIO
18
 
 
19
 
from bzrlib.lazy_import import lazy_import
20
 
lazy_import(globals(), """
21
20
import re
22
21
import time
 
22
from unittest import TestSuite
23
23
 
24
 
from bzrlib import (
25
 
    bzrdir,
26
 
    check,
27
 
    debug,
28
 
    deprecated_graph,
29
 
    errors,
30
 
    generate_ids,
31
 
    gpg,
32
 
    graph,
33
 
    lazy_regex,
34
 
    lockable_files,
35
 
    lockdir,
36
 
    osutils,
37
 
    registry,
38
 
    remote,
39
 
    revision as _mod_revision,
40
 
    symbol_versioning,
41
 
    transactions,
42
 
    ui,
43
 
    )
44
 
from bzrlib.bundle import serializer
 
24
from bzrlib import bzrdir, check, delta, gpg, errors, xml5, ui, transactions, osutils
 
25
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
26
from bzrlib.errors import InvalidRevisionId
 
27
from bzrlib.graph import Graph
 
28
from bzrlib.inter import InterObject
 
29
from bzrlib.inventory import Inventory, InventoryDirectory, ROOT_ID
 
30
from bzrlib.knit import KnitVersionedFile, KnitPlainFactory
 
31
from bzrlib.lockable_files import LockableFiles, TransportLock
 
32
from bzrlib.lockdir import LockDir
 
33
from bzrlib.osutils import (safe_unicode, rand_bytes, compact_date, 
 
34
                            local_time_offset)
 
35
from bzrlib.revision import NULL_REVISION, Revision
45
36
from bzrlib.revisiontree import RevisionTree
46
 
from bzrlib.store.versioned import VersionedFileStore
 
37
from bzrlib.store.versioned import VersionedFileStore, WeaveStore
47
38
from bzrlib.store.text import TextStore
 
39
from bzrlib import symbol_versioning
 
40
from bzrlib.symbol_versioning import (deprecated_method,
 
41
        zero_nine, 
 
42
        )
48
43
from bzrlib.testament import Testament
49
 
from bzrlib.util import bencode
50
 
""")
51
 
 
52
 
from bzrlib.decorators import needs_read_lock, needs_write_lock
53
 
from bzrlib.inter import InterObject
54
 
from bzrlib.inventory import Inventory, InventoryDirectory, ROOT_ID
55
 
from bzrlib.symbol_versioning import (
56
 
        deprecated_method,
57
 
        )
58
 
from bzrlib.trace import mutter, mutter_callsite, note, warning
 
44
from bzrlib.trace import mutter, note, warning
 
45
from bzrlib.tsort import topo_sort
 
46
from bzrlib.weave import WeaveFile
59
47
 
60
48
 
61
49
# Old formats display a warning, but only once
62
50
_deprecation_warning_done = False
63
51
 
64
52
 
65
 
class CommitBuilder(object):
66
 
    """Provides an interface to build up a commit.
67
 
 
68
 
    This allows describing a tree to be committed without needing to 
69
 
    know the internals of the format of the repository.
70
 
    """
71
 
    
72
 
    # all clients should supply tree roots.
73
 
    record_root_entry = True
74
 
    # the default CommitBuilder does not manage trees whose root is versioned.
75
 
    _versioned_root = False
76
 
 
77
 
    def __init__(self, repository, parents, config, timestamp=None, 
78
 
                 timezone=None, committer=None, revprops=None, 
79
 
                 revision_id=None):
80
 
        """Initiate a CommitBuilder.
81
 
 
82
 
        :param repository: Repository to commit to.
83
 
        :param parents: Revision ids of the parents of the new revision.
84
 
        :param config: Configuration to use.
85
 
        :param timestamp: Optional timestamp recorded for commit.
86
 
        :param timezone: Optional timezone for timestamp.
87
 
        :param committer: Optional committer to set for commit.
88
 
        :param revprops: Optional dictionary of revision properties.
89
 
        :param revision_id: Optional revision id.
90
 
        """
91
 
        self._config = config
92
 
 
93
 
        if committer is None:
94
 
            self._committer = self._config.username()
95
 
        else:
96
 
            assert isinstance(committer, basestring), type(committer)
97
 
            self._committer = committer
98
 
 
99
 
        self.new_inventory = Inventory(None)
100
 
        self._new_revision_id = revision_id
101
 
        self.parents = parents
102
 
        self.repository = repository
103
 
 
104
 
        self._revprops = {}
105
 
        if revprops is not None:
106
 
            self._revprops.update(revprops)
107
 
 
108
 
        if timestamp is None:
109
 
            timestamp = time.time()
110
 
        # Restrict resolution to 1ms
111
 
        self._timestamp = round(timestamp, 3)
112
 
 
113
 
        if timezone is None:
114
 
            self._timezone = osutils.local_time_offset()
115
 
        else:
116
 
            self._timezone = int(timezone)
117
 
 
118
 
        self._generate_revision_if_needed()
119
 
        self._heads = graph.HeadsCache(repository.get_graph()).heads
120
 
 
121
 
    def commit(self, message):
122
 
        """Make the actual commit.
123
 
 
124
 
        :return: The revision id of the recorded revision.
125
 
        """
126
 
        rev = _mod_revision.Revision(
127
 
                       timestamp=self._timestamp,
128
 
                       timezone=self._timezone,
129
 
                       committer=self._committer,
130
 
                       message=message,
131
 
                       inventory_sha1=self.inv_sha1,
132
 
                       revision_id=self._new_revision_id,
133
 
                       properties=self._revprops)
134
 
        rev.parent_ids = self.parents
135
 
        self.repository.add_revision(self._new_revision_id, rev,
136
 
            self.new_inventory, self._config)
137
 
        self.repository.commit_write_group()
138
 
        return self._new_revision_id
139
 
 
140
 
    def abort(self):
141
 
        """Abort the commit that is being built.
142
 
        """
143
 
        self.repository.abort_write_group()
144
 
 
145
 
    def revision_tree(self):
146
 
        """Return the tree that was just committed.
147
 
 
148
 
        After calling commit() this can be called to get a RevisionTree
149
 
        representing the newly committed tree. This is preferred to
150
 
        calling Repository.revision_tree() because that may require
151
 
        deserializing the inventory, while we already have a copy in
152
 
        memory.
153
 
        """
154
 
        return RevisionTree(self.repository, self.new_inventory,
155
 
                            self._new_revision_id)
156
 
 
157
 
    def finish_inventory(self):
158
 
        """Tell the builder that the inventory is finished."""
159
 
        if self.new_inventory.root is None:
160
 
            raise AssertionError('Root entry should be supplied to'
161
 
                ' record_entry_contents, as of bzr 0.10.',
162
 
                 DeprecationWarning, stacklevel=2)
163
 
            self.new_inventory.add(InventoryDirectory(ROOT_ID, '', None))
164
 
        self.new_inventory.revision_id = self._new_revision_id
165
 
        self.inv_sha1 = self.repository.add_inventory(
166
 
            self._new_revision_id,
167
 
            self.new_inventory,
168
 
            self.parents
169
 
            )
170
 
 
171
 
    def _gen_revision_id(self):
172
 
        """Return new revision-id."""
173
 
        return generate_ids.gen_revision_id(self._config.username(),
174
 
                                            self._timestamp)
175
 
 
176
 
    def _generate_revision_if_needed(self):
177
 
        """Create a revision id if None was supplied.
178
 
        
179
 
        If the repository can not support user-specified revision ids
180
 
        they should override this function and raise CannotSetRevisionId
181
 
        if _new_revision_id is not None.
182
 
 
183
 
        :raises: CannotSetRevisionId
184
 
        """
185
 
        if self._new_revision_id is None:
186
 
            self._new_revision_id = self._gen_revision_id()
187
 
            self.random_revid = True
188
 
        else:
189
 
            self.random_revid = False
190
 
 
191
 
    def _check_root(self, ie, parent_invs, tree):
192
 
        """Helper for record_entry_contents.
193
 
 
194
 
        :param ie: An entry being added.
195
 
        :param parent_invs: The inventories of the parent revisions of the
196
 
            commit.
197
 
        :param tree: The tree that is being committed.
198
 
        """
199
 
        # In this revision format, root entries have no knit or weave When
200
 
        # serializing out to disk and back in root.revision is always
201
 
        # _new_revision_id
202
 
        ie.revision = self._new_revision_id
203
 
 
204
 
    def _get_delta(self, ie, basis_inv, path):
205
 
        """Get a delta against the basis inventory for ie."""
206
 
        if ie.file_id not in basis_inv:
207
 
            # add
208
 
            return (None, path, ie.file_id, ie)
209
 
        elif ie != basis_inv[ie.file_id]:
210
 
            # common but altered
211
 
            # TODO: avoid tis id2path call.
212
 
            return (basis_inv.id2path(ie.file_id), path, ie.file_id, ie)
213
 
        else:
214
 
            # common, unaltered
215
 
            return None
216
 
 
217
 
    def record_entry_contents(self, ie, parent_invs, path, tree,
218
 
        content_summary):
219
 
        """Record the content of ie from tree into the commit if needed.
220
 
 
221
 
        Side effect: sets ie.revision when unchanged
222
 
 
223
 
        :param ie: An inventory entry present in the commit.
224
 
        :param parent_invs: The inventories of the parent revisions of the
225
 
            commit.
226
 
        :param path: The path the entry is at in the tree.
227
 
        :param tree: The tree which contains this entry and should be used to 
228
 
            obtain content.
229
 
        :param content_summary: Summary data from the tree about the paths
230
 
            content - stat, length, exec, sha/link target. This is only
231
 
            accessed when the entry has a revision of None - that is when it is
232
 
            a candidate to commit.
233
 
        :return: A tuple (change_delta, version_recorded). change_delta is 
234
 
            an inventory_delta change for this entry against the basis tree of
235
 
            the commit, or None if no change occured against the basis tree.
236
 
            version_recorded is True if a new version of the entry has been
237
 
            recorded. For instance, committing a merge where a file was only
238
 
            changed on the other side will return (delta, False).
239
 
        """
240
 
        if self.new_inventory.root is None:
241
 
            if ie.parent_id is not None:
242
 
                raise errors.RootMissing()
243
 
            self._check_root(ie, parent_invs, tree)
244
 
        if ie.revision is None:
245
 
            kind = content_summary[0]
246
 
        else:
247
 
            # ie is carried over from a prior commit
248
 
            kind = ie.kind
249
 
        # XXX: repository specific check for nested tree support goes here - if
250
 
        # the repo doesn't want nested trees we skip it ?
251
 
        if (kind == 'tree-reference' and
252
 
            not self.repository._format.supports_tree_reference):
253
 
            # mismatch between commit builder logic and repository:
254
 
            # this needs the entry creation pushed down into the builder.
255
 
            raise NotImplementedError('Missing repository subtree support.')
256
 
        self.new_inventory.add(ie)
257
 
 
258
 
        # TODO: slow, take it out of the inner loop.
259
 
        try:
260
 
            basis_inv = parent_invs[0]
261
 
        except IndexError:
262
 
            basis_inv = Inventory(root_id=None)
263
 
 
264
 
        # ie.revision is always None if the InventoryEntry is considered
265
 
        # for committing. We may record the previous parents revision if the
266
 
        # content is actually unchanged against a sole head.
267
 
        if ie.revision is not None:
268
 
            if not self._versioned_root and path == '':
269
 
                # repositories that do not version the root set the root's
270
 
                # revision to the new commit even when no change occurs, and
271
 
                # this masks when a change may have occurred against the basis,
272
 
                # so calculate if one happened.
273
 
                if ie.file_id in basis_inv:
274
 
                    delta = (basis_inv.id2path(ie.file_id), path,
275
 
                        ie.file_id, ie)
276
 
                else:
277
 
                    # add
278
 
                    delta = (None, path, ie.file_id, ie)
279
 
                return delta, False
280
 
            else:
281
 
                # we don't need to commit this, because the caller already
282
 
                # determined that an existing revision of this file is
283
 
                # appropriate.
284
 
                return None, (ie.revision == self._new_revision_id)
285
 
        # XXX: Friction: parent_candidates should return a list not a dict
286
 
        #      so that we don't have to walk the inventories again.
287
 
        parent_candiate_entries = ie.parent_candidates(parent_invs)
288
 
        head_set = self._heads(parent_candiate_entries.keys())
289
 
        heads = []
290
 
        for inv in parent_invs:
291
 
            if ie.file_id in inv:
292
 
                old_rev = inv[ie.file_id].revision
293
 
                if old_rev in head_set:
294
 
                    heads.append(inv[ie.file_id].revision)
295
 
                    head_set.remove(inv[ie.file_id].revision)
296
 
 
297
 
        store = False
298
 
        # now we check to see if we need to write a new record to the
299
 
        # file-graph.
300
 
        # We write a new entry unless there is one head to the ancestors, and
301
 
        # the kind-derived content is unchanged.
302
 
 
303
 
        # Cheapest check first: no ancestors, or more the one head in the
304
 
        # ancestors, we write a new node.
305
 
        if len(heads) != 1:
306
 
            store = True
307
 
        if not store:
308
 
            # There is a single head, look it up for comparison
309
 
            parent_entry = parent_candiate_entries[heads[0]]
310
 
            # if the non-content specific data has changed, we'll be writing a
311
 
            # node:
312
 
            if (parent_entry.parent_id != ie.parent_id or
313
 
                parent_entry.name != ie.name):
314
 
                store = True
315
 
        # now we need to do content specific checks:
316
 
        if not store:
317
 
            # if the kind changed the content obviously has
318
 
            if kind != parent_entry.kind:
319
 
                store = True
320
 
        if kind == 'file':
321
 
            assert content_summary[2] is not None, \
322
 
                "Files must not have executable = None"
323
 
            if not store:
324
 
                if (# if the file length changed we have to store:
325
 
                    parent_entry.text_size != content_summary[1] or
326
 
                    # if the exec bit has changed we have to store:
327
 
                    parent_entry.executable != content_summary[2]):
328
 
                    store = True
329
 
                elif parent_entry.text_sha1 == content_summary[3]:
330
 
                    # all meta and content is unchanged (using a hash cache
331
 
                    # hit to check the sha)
332
 
                    ie.revision = parent_entry.revision
333
 
                    ie.text_size = parent_entry.text_size
334
 
                    ie.text_sha1 = parent_entry.text_sha1
335
 
                    ie.executable = parent_entry.executable
336
 
                    return self._get_delta(ie, basis_inv, path), False
337
 
                else:
338
 
                    # Either there is only a hash change(no hash cache entry,
339
 
                    # or same size content change), or there is no change on
340
 
                    # this file at all.
341
 
                    # Provide the parent's hash to the store layer, so that the
342
 
                    # content is unchanged we will not store a new node.
343
 
                    nostore_sha = parent_entry.text_sha1
344
 
            if store:
345
 
                # We want to record a new node regardless of the presence or
346
 
                # absence of a content change in the file.
347
 
                nostore_sha = None
348
 
            ie.executable = content_summary[2]
349
 
            lines = tree.get_file(ie.file_id, path).readlines()
350
 
            try:
351
 
                ie.text_sha1, ie.text_size = self._add_text_to_weave(
352
 
                    ie.file_id, lines, heads, nostore_sha)
353
 
            except errors.ExistingContent:
354
 
                # Turns out that the file content was unchanged, and we were
355
 
                # only going to store a new node if it was changed. Carry over
356
 
                # the entry.
357
 
                ie.revision = parent_entry.revision
358
 
                ie.text_size = parent_entry.text_size
359
 
                ie.text_sha1 = parent_entry.text_sha1
360
 
                ie.executable = parent_entry.executable
361
 
                return self._get_delta(ie, basis_inv, path), False
362
 
        elif kind == 'directory':
363
 
            if not store:
364
 
                # all data is meta here, nothing specific to directory, so
365
 
                # carry over:
366
 
                ie.revision = parent_entry.revision
367
 
                return self._get_delta(ie, basis_inv, path), False
368
 
            lines = []
369
 
            self._add_text_to_weave(ie.file_id, lines, heads, None)
370
 
        elif kind == 'symlink':
371
 
            current_link_target = content_summary[3]
372
 
            if not store:
373
 
                # symlink target is not generic metadata, check if it has
374
 
                # changed.
375
 
                if current_link_target != parent_entry.symlink_target:
376
 
                    store = True
377
 
            if not store:
378
 
                # unchanged, carry over.
379
 
                ie.revision = parent_entry.revision
380
 
                ie.symlink_target = parent_entry.symlink_target
381
 
                return self._get_delta(ie, basis_inv, path), False
382
 
            ie.symlink_target = current_link_target
383
 
            lines = []
384
 
            self._add_text_to_weave(ie.file_id, lines, heads, None)
385
 
        elif kind == 'tree-reference':
386
 
            if not store:
387
 
                if content_summary[3] != parent_entry.reference_revision:
388
 
                    store = True
389
 
            if not store:
390
 
                # unchanged, carry over.
391
 
                ie.reference_revision = parent_entry.reference_revision
392
 
                ie.revision = parent_entry.revision
393
 
                return self._get_delta(ie, basis_inv, path), False
394
 
            ie.reference_revision = content_summary[3]
395
 
            lines = []
396
 
            self._add_text_to_weave(ie.file_id, lines, heads, None)
397
 
        else:
398
 
            raise NotImplementedError('unknown kind')
399
 
        ie.revision = self._new_revision_id
400
 
        return self._get_delta(ie, basis_inv, path), True
401
 
 
402
 
    def _add_text_to_weave(self, file_id, new_lines, parents, nostore_sha):
403
 
        versionedfile = self.repository.weave_store.get_weave_or_empty(
404
 
            file_id, self.repository.get_transaction())
405
 
        # Don't change this to add_lines - add_lines_with_ghosts is cheaper
406
 
        # than add_lines, and allows committing when a parent is ghosted for
407
 
        # some reason.
408
 
        # Note: as we read the content directly from the tree, we know its not
409
 
        # been turned into unicode or badly split - but a broken tree
410
 
        # implementation could give us bad output from readlines() so this is
411
 
        # not a guarantee of safety. What would be better is always checking
412
 
        # the content during test suite execution. RBC 20070912
413
 
        try:
414
 
            return versionedfile.add_lines_with_ghosts(
415
 
                self._new_revision_id, parents, new_lines,
416
 
                nostore_sha=nostore_sha, random_id=self.random_revid,
417
 
                check_content=False)[0:2]
418
 
        finally:
419
 
            versionedfile.clear_cache()
420
 
 
421
 
 
422
 
class RootCommitBuilder(CommitBuilder):
423
 
    """This commitbuilder actually records the root id"""
424
 
    
425
 
    # the root entry gets versioned properly by this builder.
426
 
    _versioned_root = True
427
 
 
428
 
    def _check_root(self, ie, parent_invs, tree):
429
 
        """Helper for record_entry_contents.
430
 
 
431
 
        :param ie: An entry being added.
432
 
        :param parent_invs: The inventories of the parent revisions of the
433
 
            commit.
434
 
        :param tree: The tree that is being committed.
435
 
        """
436
 
 
437
 
 
438
 
######################################################################
439
 
# Repositories
440
 
 
441
53
class Repository(object):
442
54
    """Repository holding history for one or more branches.
443
55
 
450
62
    remote) disk.
451
63
    """
452
64
 
453
 
    # What class to use for a CommitBuilder. Often its simpler to change this
454
 
    # in a Repository class subclass rather than to override
455
 
    # get_commit_builder.
456
 
    _commit_builder_class = CommitBuilder
457
 
    # The search regex used by xml based repositories to determine what things
458
 
    # where changed in a single commit.
459
 
    _file_ids_altered_regex = lazy_regex.lazy_compile(
460
 
        r'file_id="(?P<file_id>[^"]+)"'
461
 
        r'.* revision="(?P<revision_id>[^"]+)"'
462
 
        )
463
 
 
464
 
    def abort_write_group(self):
465
 
        """Commit the contents accrued within the current write group.
466
 
 
467
 
        :seealso: start_write_group.
468
 
        """
469
 
        if self._write_group is not self.get_transaction():
470
 
            # has an unlock or relock occured ?
471
 
            raise errors.BzrError('mismatched lock context and write group.')
472
 
        self._abort_write_group()
473
 
        self._write_group = None
474
 
 
475
 
    def _abort_write_group(self):
476
 
        """Template method for per-repository write group cleanup.
477
 
        
478
 
        This is called during abort before the write group is considered to be 
479
 
        finished and should cleanup any internal state accrued during the write
480
 
        group. There is no requirement that data handed to the repository be
481
 
        *not* made available - this is not a rollback - but neither should any
482
 
        attempt be made to ensure that data added is fully commited. Abort is
483
 
        invoked when an error has occured so futher disk or network operations
484
 
        may not be possible or may error and if possible should not be
485
 
        attempted.
486
 
        """
487
 
 
488
65
    @needs_write_lock
489
 
    def add_inventory(self, revision_id, inv, parents):
490
 
        """Add the inventory inv to the repository as revision_id.
 
66
    def add_inventory(self, revid, inv, parents):
 
67
        """Add the inventory inv to the repository as revid.
491
68
        
492
 
        :param parents: The revision ids of the parents that revision_id
 
69
        :param parents: The revision ids of the parents that revid
493
70
                        is known to have and are in the repository already.
494
71
 
495
72
        returns the sha1 of the serialized inventory.
496
73
        """
497
 
        assert self.is_in_write_group()
498
 
        _mod_revision.check_not_reserved_id(revision_id)
499
 
        assert inv.revision_id is None or inv.revision_id == revision_id, \
 
74
        assert inv.revision_id is None or inv.revision_id == revid, \
500
75
            "Mismatch between inventory revision" \
501
 
            " id and insertion revid (%r, %r)" % (inv.revision_id, revision_id)
 
76
            " id and insertion revid (%r, %r)" % (inv.revision_id, revid)
502
77
        assert inv.root is not None
503
 
        inv_lines = self._serialise_inventory_to_lines(inv)
504
 
        inv_vf = self.get_inventory_weave()
505
 
        return self._inventory_add_lines(inv_vf, revision_id, parents,
506
 
            inv_lines, check_content=False)
 
78
        inv_text = xml5.serializer_v5.write_inventory_to_string(inv)
 
79
        inv_sha1 = osutils.sha_string(inv_text)
 
80
        inv_vf = self.control_weaves.get_weave('inventory',
 
81
                                               self.get_transaction())
 
82
        self._inventory_add_lines(inv_vf, revid, parents, osutils.split_lines(inv_text))
 
83
        return inv_sha1
507
84
 
508
 
    def _inventory_add_lines(self, inv_vf, revision_id, parents, lines,
509
 
        check_content=True):
510
 
        """Store lines in inv_vf and return the sha1 of the inventory."""
 
85
    def _inventory_add_lines(self, inv_vf, revid, parents, lines):
511
86
        final_parents = []
512
87
        for parent in parents:
513
88
            if parent in inv_vf:
514
89
                final_parents.append(parent)
515
 
        return inv_vf.add_lines(revision_id, final_parents, lines,
516
 
            check_content=check_content)[0]
 
90
 
 
91
        inv_vf.add_lines(revid, final_parents, lines)
517
92
 
518
93
    @needs_write_lock
519
 
    def add_revision(self, revision_id, rev, inv=None, config=None):
520
 
        """Add rev to the revision store as revision_id.
 
94
    def add_revision(self, rev_id, rev, inv=None, config=None):
 
95
        """Add rev to the revision store as rev_id.
521
96
 
522
 
        :param revision_id: the revision id to use.
 
97
        :param rev_id: the revision id to use.
523
98
        :param rev: The revision object.
524
99
        :param inv: The inventory for the revision. if None, it will be looked
525
100
                    up in the inventory storer
527
102
                       If supplied its signature_needed method will be used
528
103
                       to determine if a signature should be made.
529
104
        """
530
 
        # TODO: jam 20070210 Shouldn't we check rev.revision_id and
531
 
        #       rev.parent_ids?
532
 
        _mod_revision.check_not_reserved_id(revision_id)
533
105
        if config is not None and config.signature_needed():
534
106
            if inv is None:
535
 
                inv = self.get_inventory(revision_id)
 
107
                inv = self.get_inventory(rev_id)
536
108
            plaintext = Testament(rev, inv).as_short_text()
537
109
            self.store_revision_signature(
538
 
                gpg.GPGStrategy(config), plaintext, revision_id)
539
 
        if not revision_id in self.get_inventory_weave():
 
110
                gpg.GPGStrategy(config), plaintext, rev_id)
 
111
        if not rev_id in self.get_inventory_weave():
540
112
            if inv is None:
541
 
                raise errors.WeaveRevisionNotPresent(revision_id,
 
113
                raise errors.WeaveRevisionNotPresent(rev_id,
542
114
                                                     self.get_inventory_weave())
543
115
            else:
544
116
                # yes, this is not suitable for adding with ghosts.
545
 
                self.add_inventory(revision_id, inv, rev.parent_ids)
 
117
                self.add_inventory(rev_id, inv, rev.parent_ids)
546
118
        self._revision_store.add_revision(rev, self.get_transaction())
547
119
 
548
 
    def _add_revision_text(self, revision_id, text):
549
 
        revision = self._revision_store._serializer.read_revision_from_string(
550
 
            text)
551
 
        self._revision_store._add_revision(revision, StringIO(text),
552
 
                                           self.get_transaction())
 
120
    @needs_read_lock
 
121
    def _all_possible_ids(self):
 
122
        """Return all the possible revisions that we could find."""
 
123
        return self.get_inventory_weave().versions()
553
124
 
554
125
    def all_revision_ids(self):
555
126
        """Returns a list of all the revision ids in the repository. 
558
129
        reachable from a particular revision, and ignore any other revisions
559
130
        that might be present.  There is no direct replacement method.
560
131
        """
561
 
        if 'evil' in debug.debug_flags:
562
 
            mutter_callsite(2, "all_revision_ids is linear with history.")
563
132
        return self._all_revision_ids()
564
133
 
 
134
    @needs_read_lock
565
135
    def _all_revision_ids(self):
566
136
        """Returns a list of all the revision ids in the repository. 
567
137
 
568
138
        These are in as much topological order as the underlying store can 
569
 
        present.
 
139
        present: for weaves ghosts may lead to a lack of correctness until
 
140
        the reweave updates the parents list.
570
141
        """
571
 
        raise NotImplementedError(self._all_revision_ids)
 
142
        if self._revision_store.text_store.listable():
 
143
            return self._revision_store.all_revision_ids(self.get_transaction())
 
144
        result = self._all_possible_ids()
 
145
        return self._eliminate_revisions_not_present(result)
572
146
 
573
147
    def break_lock(self):
574
148
        """Break a lock if one is present from another instance.
611
185
        self.bzrdir = a_bzrdir
612
186
        self.control_files = control_files
613
187
        self._revision_store = _revision_store
 
188
        self.text_store = text_store
614
189
        # backwards compatibility
615
190
        self.weave_store = text_store
616
 
        # for tests
617
 
        self._reconcile_does_inventory_gc = True
618
 
        self._reconcile_fixes_text_parents = False
619
191
        # not right yet - should be more semantically clear ? 
620
192
        # 
621
193
        self.control_store = control_store
623
195
        # TODO: make sure to construct the right store classes, etc, depending
624
196
        # on whether escaping is required.
625
197
        self._warn_if_deprecated()
626
 
        self._write_group = None
627
 
        self.base = control_files._transport.base
628
198
 
629
199
    def __repr__(self):
630
 
        return '%s(%r)' % (self.__class__.__name__,
631
 
                           self.base)
632
 
 
633
 
    def has_same_location(self, other):
634
 
        """Returns a boolean indicating if this repository is at the same
635
 
        location as another repository.
636
 
 
637
 
        This might return False even when two repository objects are accessing
638
 
        the same physical repository via different URLs.
639
 
        """
640
 
        if self.__class__ is not other.__class__:
641
 
            return False
642
 
        return (self.control_files._transport.base ==
643
 
                other.control_files._transport.base)
644
 
 
645
 
    def is_in_write_group(self):
646
 
        """Return True if there is an open write group.
647
 
 
648
 
        :seealso: start_write_group.
649
 
        """
650
 
        return self._write_group is not None
 
200
        return '%s(%r)' % (self.__class__.__name__, 
 
201
                           self.bzrdir.transport.base)
651
202
 
652
203
    def is_locked(self):
653
204
        return self.control_files.is_locked()
654
205
 
655
 
    def is_write_locked(self):
656
 
        """Return True if this object is write locked."""
657
 
        return self.is_locked() and self.control_files._lock_mode == 'w'
658
 
 
659
 
    def lock_write(self, token=None):
660
 
        """Lock this repository for writing.
661
 
 
662
 
        This causes caching within the repository obejct to start accumlating
663
 
        data during reads, and allows a 'write_group' to be obtained. Write
664
 
        groups must be used for actual data insertion.
665
 
        
666
 
        :param token: if this is already locked, then lock_write will fail
667
 
            unless the token matches the existing lock.
668
 
        :returns: a token if this instance supports tokens, otherwise None.
669
 
        :raises TokenLockingNotSupported: when a token is given but this
670
 
            instance doesn't support using token locks.
671
 
        :raises MismatchedToken: if the specified token doesn't match the token
672
 
            of the existing lock.
673
 
        :seealso: start_write_group.
674
 
 
675
 
        A token should be passed in if you know that you have locked the object
676
 
        some other way, and need to synchronise this object's state with that
677
 
        fact.
678
 
 
679
 
        XXX: this docstring is duplicated in many places, e.g. lockable_files.py
680
 
        """
681
 
        result = self.control_files.lock_write(token=token)
682
 
        self._refresh_data()
683
 
        return result
 
206
    def lock_write(self):
 
207
        self.control_files.lock_write()
684
208
 
685
209
    def lock_read(self):
686
210
        self.control_files.lock_read()
687
 
        self._refresh_data()
688
211
 
689
212
    def get_physical_lock_status(self):
690
213
        return self.control_files.get_physical_lock_status()
691
214
 
692
 
    def leave_lock_in_place(self):
693
 
        """Tell this repository not to release the physical lock when this
694
 
        object is unlocked.
695
 
        
696
 
        If lock_write doesn't return a token, then this method is not supported.
697
 
        """
698
 
        self.control_files.leave_in_place()
699
 
 
700
 
    def dont_leave_lock_in_place(self):
701
 
        """Tell this repository to release the physical lock when this
702
 
        object is unlocked, even if it didn't originally acquire it.
703
 
 
704
 
        If lock_write doesn't return a token, then this method is not supported.
705
 
        """
706
 
        self.control_files.dont_leave_in_place()
707
 
 
708
 
    @needs_read_lock
709
 
    def gather_stats(self, revid=None, committers=None):
710
 
        """Gather statistics from a revision id.
711
 
 
712
 
        :param revid: The revision id to gather statistics from, if None, then
713
 
            no revision specific statistics are gathered.
714
 
        :param committers: Optional parameter controlling whether to grab
715
 
            a count of committers from the revision specific statistics.
716
 
        :return: A dictionary of statistics. Currently this contains:
717
 
            committers: The number of committers if requested.
718
 
            firstrev: A tuple with timestamp, timezone for the penultimate left
719
 
                most ancestor of revid, if revid is not the NULL_REVISION.
720
 
            latestrev: A tuple with timestamp, timezone for revid, if revid is
721
 
                not the NULL_REVISION.
722
 
            revisions: The total revision count in the repository.
723
 
            size: An estimate disk size of the repository in bytes.
724
 
        """
725
 
        result = {}
726
 
        if revid and committers:
727
 
            result['committers'] = 0
728
 
        if revid and revid != _mod_revision.NULL_REVISION:
729
 
            if committers:
730
 
                all_committers = set()
731
 
            revisions = self.get_ancestry(revid)
732
 
            # pop the leading None
733
 
            revisions.pop(0)
734
 
            first_revision = None
735
 
            if not committers:
736
 
                # ignore the revisions in the middle - just grab first and last
737
 
                revisions = revisions[0], revisions[-1]
738
 
            for revision in self.get_revisions(revisions):
739
 
                if not first_revision:
740
 
                    first_revision = revision
741
 
                if committers:
742
 
                    all_committers.add(revision.committer)
743
 
            last_revision = revision
744
 
            if committers:
745
 
                result['committers'] = len(all_committers)
746
 
            result['firstrev'] = (first_revision.timestamp,
747
 
                first_revision.timezone)
748
 
            result['latestrev'] = (last_revision.timestamp,
749
 
                last_revision.timezone)
750
 
 
751
 
        # now gather global repository information
752
 
        if self.bzrdir.root_transport.listable():
753
 
            c, t = self._revision_store.total_size(self.get_transaction())
754
 
            result['revisions'] = c
755
 
            result['size'] = t
756
 
        return result
757
 
 
758
 
    def get_data_stream(self, revision_ids):
759
 
        raise NotImplementedError(self.get_data_stream)
760
 
 
761
 
    def insert_data_stream(self, stream):
762
 
        """XXX What does this really do? 
763
 
        
764
 
        Is it a substitute for fetch? 
765
 
        Should it manage its own write group ?
766
 
        """
767
 
        for item_key, bytes in stream:
768
 
            if item_key[0] == 'file':
769
 
                (file_id,) = item_key[1:]
770
 
                knit = self.weave_store.get_weave_or_empty(
771
 
                    file_id, self.get_transaction())
772
 
            elif item_key == ('inventory',):
773
 
                knit = self.get_inventory_weave()
774
 
            elif item_key == ('revisions',):
775
 
                knit = self._revision_store.get_revision_file(
776
 
                    self.get_transaction())
777
 
            elif item_key == ('signatures',):
778
 
                knit = self._revision_store.get_signature_file(
779
 
                    self.get_transaction())
780
 
            else:
781
 
                raise RepositoryDataStreamError(
782
 
                    "Unrecognised data stream key '%s'" % (item_key,))
783
 
            decoded_list = bencode.bdecode(bytes)
784
 
            format = decoded_list.pop(0)
785
 
            data_list = []
786
 
            knit_bytes = ''
787
 
            for version, options, parents, some_bytes in decoded_list:
788
 
                data_list.append((version, options, len(some_bytes), parents))
789
 
                knit_bytes += some_bytes
790
 
            knit.insert_data_stream(
791
 
                (format, data_list, StringIO(knit_bytes).read))
792
 
 
793
215
    @needs_read_lock
794
216
    def missing_revision_ids(self, other, revision_id=None):
795
217
        """Return the revision ids that other has that this does not.
810
232
        control = bzrdir.BzrDir.open(base)
811
233
        return control.open_repository()
812
234
 
813
 
    def copy_content_into(self, destination, revision_id=None):
 
235
    def copy_content_into(self, destination, revision_id=None, basis=None):
814
236
        """Make a complete copy of the content in self into destination.
815
237
        
816
238
        This is a destructive operation! Do not use it on existing 
817
239
        repositories.
818
240
        """
819
 
        return InterRepository.get(self, destination).copy_content(revision_id)
820
 
 
821
 
    def commit_write_group(self):
822
 
        """Commit the contents accrued within the current write group.
823
 
 
824
 
        :seealso: start_write_group.
825
 
        """
826
 
        if self._write_group is not self.get_transaction():
827
 
            # has an unlock or relock occured ?
828
 
            raise errors.BzrError('mismatched lock context %r and '
829
 
                'write group %r.' %
830
 
                (self.get_transaction(), self._write_group))
831
 
        self._commit_write_group()
832
 
        self._write_group = None
833
 
 
834
 
    def _commit_write_group(self):
835
 
        """Template method for per-repository write group cleanup.
836
 
        
837
 
        This is called before the write group is considered to be 
838
 
        finished and should ensure that all data handed to the repository
839
 
        for writing during the write group is safely committed (to the 
840
 
        extent possible considering file system caching etc).
841
 
        """
 
241
        return InterRepository.get(self, destination).copy_content(revision_id, basis)
842
242
 
843
243
    def fetch(self, source, revision_id=None, pb=None):
844
244
        """Fetch the content required to construct revision_id from source.
845
245
 
846
246
        If revision_id is None all content is copied.
847
247
        """
848
 
        # fast path same-url fetch operations
849
 
        if self.has_same_location(source):
850
 
            # check that last_revision is in 'from' and then return a
851
 
            # no-operation.
852
 
            if (revision_id is not None and
853
 
                not _mod_revision.is_null(revision_id)):
854
 
                self.get_revision(revision_id)
855
 
            return 0, []
856
 
        inter = InterRepository.get(source, self)
857
 
        try:
858
 
            return inter.fetch(revision_id=revision_id, pb=pb)
859
 
        except NotImplementedError:
860
 
            raise errors.IncompatibleRepositories(source, self)
861
 
 
862
 
    def create_bundle(self, target, base, fileobj, format=None):
863
 
        return serializer.write_bundle(self, target, base, fileobj, format)
864
 
 
865
 
    def get_commit_builder(self, branch, parents, config, timestamp=None,
866
 
                           timezone=None, committer=None, revprops=None,
 
248
        return InterRepository.get(source, self).fetch(revision_id=revision_id,
 
249
                                                       pb=pb)
 
250
 
 
251
    def get_commit_builder(self, branch, parents, config, timestamp=None, 
 
252
                           timezone=None, committer=None, revprops=None, 
867
253
                           revision_id=None):
868
254
        """Obtain a CommitBuilder for this repository.
869
255
        
876
262
        :param revprops: Optional dictionary of revision properties.
877
263
        :param revision_id: Optional revision id.
878
264
        """
879
 
        result = self._commit_builder_class(self, parents, config,
880
 
            timestamp, timezone, committer, revprops, revision_id)
881
 
        self.start_write_group()
882
 
        return result
 
265
        return _CommitBuilder(self, parents, config, timestamp, timezone,
 
266
                              committer, revprops, revision_id)
883
267
 
884
268
    def unlock(self):
885
 
        if (self.control_files._lock_count == 1 and
886
 
            self.control_files._lock_mode == 'w'):
887
 
            if self._write_group is not None:
888
 
                raise errors.BzrError(
889
 
                    'Must end write groups before releasing write locks.')
890
269
        self.control_files.unlock()
891
270
 
892
271
    @needs_read_lock
893
 
    def clone(self, a_bzrdir, revision_id=None):
 
272
    def clone(self, a_bzrdir, revision_id=None, basis=None):
894
273
        """Clone this repository into a_bzrdir using the current format.
895
274
 
896
275
        Currently no check is made that the format of this repository and
897
276
        the bzrdir format are compatible. FIXME RBC 20060201.
898
 
 
899
 
        :return: The newly created destination repository.
900
 
        """
901
 
        # TODO: deprecate after 0.16; cloning this with all its settings is
902
 
        # probably not very useful -- mbp 20070423
903
 
        dest_repo = self._create_sprouting_repo(a_bzrdir, shared=self.is_shared())
904
 
        self.copy_content_into(dest_repo, revision_id)
905
 
        return dest_repo
906
 
 
907
 
    def start_write_group(self):
908
 
        """Start a write group in the repository.
909
 
 
910
 
        Write groups are used by repositories which do not have a 1:1 mapping
911
 
        between file ids and backend store to manage the insertion of data from
912
 
        both fetch and commit operations.
913
 
 
914
 
        A write lock is required around the start_write_group/commit_write_group
915
 
        for the support of lock-requiring repository formats.
916
 
 
917
 
        One can only insert data into a repository inside a write group.
918
 
 
919
 
        :return: None.
920
 
        """
921
 
        if not self.is_write_locked():
922
 
            raise errors.NotWriteLocked(self)
923
 
        if self._write_group:
924
 
            raise errors.BzrError('already in a write group')
925
 
        self._start_write_group()
926
 
        # so we can detect unlock/relock - the write group is now entered.
927
 
        self._write_group = self.get_transaction()
928
 
 
929
 
    def _start_write_group(self):
930
 
        """Template method for per-repository write group startup.
931
 
        
932
 
        This is called before the write group is considered to be 
933
 
        entered.
934
 
        """
935
 
 
936
 
    @needs_read_lock
937
 
    def sprout(self, to_bzrdir, revision_id=None):
938
 
        """Create a descendent repository for new development.
939
 
 
940
 
        Unlike clone, this does not copy the settings of the repository.
941
 
        """
942
 
        dest_repo = self._create_sprouting_repo(to_bzrdir, shared=False)
943
 
        dest_repo.fetch(self, revision_id=revision_id)
944
 
        return dest_repo
945
 
 
946
 
    def _create_sprouting_repo(self, a_bzrdir, shared):
 
277
        """
947
278
        if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
948
279
            # use target default format.
949
 
            dest_repo = a_bzrdir.create_repository()
 
280
            result = a_bzrdir.create_repository()
 
281
        # FIXME RBC 20060209 split out the repository type to avoid this check ?
 
282
        elif isinstance(a_bzrdir._format,
 
283
                      (bzrdir.BzrDirFormat4,
 
284
                       bzrdir.BzrDirFormat5,
 
285
                       bzrdir.BzrDirFormat6)):
 
286
            result = a_bzrdir.open_repository()
950
287
        else:
951
 
            # Most control formats need the repository to be specifically
952
 
            # created, but on some old all-in-one formats it's not needed
953
 
            try:
954
 
                dest_repo = self._format.initialize(a_bzrdir, shared=shared)
955
 
            except errors.UninitializableFormat:
956
 
                dest_repo = a_bzrdir.open_repository()
957
 
        return dest_repo
 
288
            result = self._format.initialize(a_bzrdir, shared=self.is_shared())
 
289
        self.copy_content_into(result, revision_id, basis)
 
290
        return result
958
291
 
959
292
    @needs_read_lock
960
293
    def has_revision(self, revision_id):
961
294
        """True if this repository has a copy of the revision."""
962
 
        if 'evil' in debug.debug_flags:
963
 
            mutter_callsite(3, "has_revision is a LBYL symptom.")
964
295
        return self._revision_store.has_revision_id(revision_id,
965
296
                                                    self.get_transaction())
966
297
 
967
298
    @needs_read_lock
968
 
    def get_revision(self, revision_id):
969
 
        """Return the Revision object for a named revision."""
970
 
        return self.get_revisions([revision_id])[0]
971
 
 
972
 
    @needs_read_lock
973
299
    def get_revision_reconcile(self, revision_id):
974
300
        """'reconcile' helper routine that allows access to a revision always.
975
301
        
978
304
        be used by reconcile, or reconcile-alike commands that are correcting
979
305
        or testing the revision graph.
980
306
        """
981
 
        return self._get_revisions([revision_id])[0]
982
 
 
 
307
        if not revision_id or not isinstance(revision_id, basestring):
 
308
            raise InvalidRevisionId(revision_id=revision_id, branch=self)
 
309
        return self._revision_store.get_revisions([revision_id],
 
310
                                                  self.get_transaction())[0]
983
311
    @needs_read_lock
984
312
    def get_revisions(self, revision_ids):
985
 
        """Get many revisions at once."""
986
 
        return self._get_revisions(revision_ids)
987
 
 
988
 
    @needs_read_lock
989
 
    def _get_revisions(self, revision_ids):
990
 
        """Core work logic to get many revisions without sanity checks."""
991
 
        for rev_id in revision_ids:
992
 
            if not rev_id or not isinstance(rev_id, basestring):
993
 
                raise errors.InvalidRevisionId(revision_id=rev_id, branch=self)
994
 
        revs = self._revision_store.get_revisions(revision_ids,
 
313
        return self._revision_store.get_revisions(revision_ids,
995
314
                                                  self.get_transaction())
996
 
        for rev in revs:
997
 
            assert not isinstance(rev.revision_id, unicode)
998
 
            for parent_id in rev.parent_ids:
999
 
                assert not isinstance(parent_id, unicode)
1000
 
        return revs
1001
315
 
1002
316
    @needs_read_lock
1003
317
    def get_revision_xml(self, revision_id):
1004
 
        # TODO: jam 20070210 This shouldn't be necessary since get_revision
1005
 
        #       would have already do it.
1006
 
        # TODO: jam 20070210 Just use _serializer.write_revision_to_string()
1007
 
        rev = self.get_revision(revision_id)
 
318
        rev = self.get_revision(revision_id) 
1008
319
        rev_tmp = StringIO()
1009
320
        # the current serializer..
1010
321
        self._revision_store._serializer.write_revision(rev, rev_tmp)
1012
323
        return rev_tmp.getvalue()
1013
324
 
1014
325
    @needs_read_lock
 
326
    def get_revision(self, revision_id):
 
327
        """Return the Revision object for a named revision"""
 
328
        r = self.get_revision_reconcile(revision_id)
 
329
        # weave corruption can lead to absent revision markers that should be
 
330
        # present.
 
331
        # the following test is reasonably cheap (it needs a single weave read)
 
332
        # and the weave is cached in read transactions. In write transactions
 
333
        # it is not cached but typically we only read a small number of
 
334
        # revisions. For knits when they are introduced we will probably want
 
335
        # to ensure that caching write transactions are in use.
 
336
        inv = self.get_inventory_weave()
 
337
        self._check_revision_parents(r, inv)
 
338
        return r
 
339
 
 
340
    @needs_read_lock
1015
341
    def get_deltas_for_revisions(self, revisions):
1016
342
        """Produce a generator of revision deltas.
1017
343
        
1042
368
        r = self.get_revision(revision_id)
1043
369
        return list(self.get_deltas_for_revisions([r]))[0]
1044
370
 
 
371
    def _check_revision_parents(self, revision, inventory):
 
372
        """Private to Repository and Fetch.
 
373
        
 
374
        This checks the parentage of revision in an inventory weave for 
 
375
        consistency and is only applicable to inventory-weave-for-ancestry
 
376
        using repository formats & fetchers.
 
377
        """
 
378
        weave_parents = inventory.get_parents(revision.revision_id)
 
379
        weave_names = inventory.versions()
 
380
        for parent_id in revision.parent_ids:
 
381
            if parent_id in weave_names:
 
382
                # this parent must not be a ghost.
 
383
                if not parent_id in weave_parents:
 
384
                    # but it is a ghost
 
385
                    raise errors.CorruptRepository(self)
 
386
 
1045
387
    @needs_write_lock
1046
388
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
1047
389
        signature = gpg_strategy.sign(plaintext)
1049
391
                                                         signature,
1050
392
                                                         self.get_transaction())
1051
393
 
1052
 
    def _find_file_ids_from_xml_inventory_lines(self, line_iterator,
1053
 
        revision_ids):
1054
 
        """Helper routine for fileids_altered_by_revision_ids.
1055
 
 
1056
 
        This performs the translation of xml lines to revision ids.
1057
 
 
1058
 
        :param line_iterator: An iterator of lines
1059
 
        :param revision_ids: The revision ids to filter for.
 
394
    def fileids_altered_by_revision_ids(self, revision_ids):
 
395
        """Find the file ids and versions affected by revisions.
 
396
 
 
397
        :param revisions: an iterable containing revision ids.
1060
398
        :return: a dictionary mapping altered file-ids to an iterable of
1061
399
        revision_ids. Each altered file-ids has the exact revision_ids that
1062
400
        altered it listed explicitly.
1063
401
        """
 
402
        assert isinstance(self._format, (RepositoryFormat5,
 
403
                                         RepositoryFormat6,
 
404
                                         RepositoryFormat7,
 
405
                                         RepositoryFormatKnit1)), \
 
406
            ("fileids_altered_by_revision_ids only supported for branches " 
 
407
             "which store inventory as unnested xml, not on %r" % self)
 
408
        selected_revision_ids = set(revision_ids)
 
409
        w = self.get_inventory_weave()
1064
410
        result = {}
1065
411
 
1066
412
        # this code needs to read every new line in every inventory for the
1071
417
        # revisions. We don't need to see all lines in the inventory because
1072
418
        # only those added in an inventory in rev X can contain a revision=X
1073
419
        # line.
1074
 
        unescape_revid_cache = {}
1075
 
        unescape_fileid_cache = {}
1076
 
 
1077
 
        # jam 20061218 In a big fetch, this handles hundreds of thousands
1078
 
        # of lines, so it has had a lot of inlining and optimizing done.
1079
 
        # Sorry that it is a little bit messy.
1080
 
        # Move several functions to be local variables, since this is a long
1081
 
        # running loop.
1082
 
        search = self._file_ids_altered_regex.search
1083
 
        unescape = _unescape_xml
1084
 
        setdefault = result.setdefault
1085
 
        for line in line_iterator:
1086
 
            match = search(line)
1087
 
            if match is None:
1088
 
                continue
1089
 
            # One call to match.group() returning multiple items is quite a
1090
 
            # bit faster than 2 calls to match.group() each returning 1
1091
 
            file_id, revision_id = match.group('file_id', 'revision_id')
1092
 
 
1093
 
            # Inlining the cache lookups helps a lot when you make 170,000
1094
 
            # lines and 350k ids, versus 8.4 unique ids.
1095
 
            # Using a cache helps in 2 ways:
1096
 
            #   1) Avoids unnecessary decoding calls
1097
 
            #   2) Re-uses cached strings, which helps in future set and
1098
 
            #      equality checks.
1099
 
            # (2) is enough that removing encoding entirely along with
1100
 
            # the cache (so we are using plain strings) results in no
1101
 
            # performance improvement.
1102
 
            try:
1103
 
                revision_id = unescape_revid_cache[revision_id]
1104
 
            except KeyError:
1105
 
                unescaped = unescape(revision_id)
1106
 
                unescape_revid_cache[revision_id] = unescaped
1107
 
                revision_id = unescaped
1108
 
 
1109
 
            if revision_id in revision_ids:
1110
 
                try:
1111
 
                    file_id = unescape_fileid_cache[file_id]
1112
 
                except KeyError:
1113
 
                    unescaped = unescape(file_id)
1114
 
                    unescape_fileid_cache[file_id] = unescaped
1115
 
                    file_id = unescaped
1116
 
                setdefault(file_id, set()).add(revision_id)
 
420
        for line in w.iter_lines_added_or_present_in_versions(selected_revision_ids):
 
421
            start = line.find('file_id="')+9
 
422
            if start < 9: continue
 
423
            end = line.find('"', start)
 
424
            assert end>= 0
 
425
            file_id = _unescape_xml(line[start:end])
 
426
 
 
427
            start = line.find('revision="')+10
 
428
            if start < 10: continue
 
429
            end = line.find('"', start)
 
430
            assert end>= 0
 
431
            revision_id = _unescape_xml(line[start:end])
 
432
            if revision_id in selected_revision_ids:
 
433
                result.setdefault(file_id, set()).add(revision_id)
1117
434
        return result
1118
435
 
1119
 
    def fileids_altered_by_revision_ids(self, revision_ids):
1120
 
        """Find the file ids and versions affected by revisions.
1121
 
 
1122
 
        :param revisions: an iterable containing revision ids.
1123
 
        :return: a dictionary mapping altered file-ids to an iterable of
1124
 
        revision_ids. Each altered file-ids has the exact revision_ids that
1125
 
        altered it listed explicitly.
1126
 
        """
1127
 
        assert self._serializer.support_altered_by_hack, \
1128
 
            ("fileids_altered_by_revision_ids only supported for branches " 
1129
 
             "which store inventory as unnested xml, not on %r" % self)
1130
 
        selected_revision_ids = set(revision_ids)
1131
 
        w = self.get_inventory_weave()
1132
 
        pb = ui.ui_factory.nested_progress_bar()
1133
 
        try:
1134
 
            return self._find_file_ids_from_xml_inventory_lines(
1135
 
                w.iter_lines_added_or_present_in_versions(
1136
 
                    selected_revision_ids, pb=pb),
1137
 
                selected_revision_ids)
1138
 
        finally:
1139
 
            pb.finished()
1140
 
 
1141
 
    def iter_files_bytes(self, desired_files):
1142
 
        """Iterate through file versions.
1143
 
 
1144
 
        Files will not necessarily be returned in the order they occur in
1145
 
        desired_files.  No specific order is guaranteed.
1146
 
 
1147
 
        Yields pairs of identifier, bytes_iterator.  identifier is an opaque
1148
 
        value supplied by the caller as part of desired_files.  It should
1149
 
        uniquely identify the file version in the caller's context.  (Examples:
1150
 
        an index number or a TreeTransform trans_id.)
1151
 
 
1152
 
        bytes_iterator is an iterable of bytestrings for the file.  The
1153
 
        kind of iterable and length of the bytestrings are unspecified, but for
1154
 
        this implementation, it is a list of lines produced by
1155
 
        VersionedFile.get_lines().
1156
 
 
1157
 
        :param desired_files: a list of (file_id, revision_id, identifier)
1158
 
            triples
1159
 
        """
1160
 
        transaction = self.get_transaction()
1161
 
        for file_id, revision_id, callable_data in desired_files:
1162
 
            try:
1163
 
                weave = self.weave_store.get_weave(file_id, transaction)
1164
 
            except errors.NoSuchFile:
1165
 
                raise errors.NoSuchIdInRepository(self, file_id)
1166
 
            yield callable_data, weave.get_lines(revision_id)
1167
 
 
1168
 
    def item_keys_introduced_by(self, revision_ids, _files_pb=None):
1169
 
        """Get an iterable listing the keys of all the data introduced by a set
1170
 
        of revision IDs.
1171
 
 
1172
 
        The keys will be ordered so that the corresponding items can be safely
1173
 
        fetched and inserted in that order.
1174
 
 
1175
 
        :returns: An iterable producing tuples of (knit-kind, file-id,
1176
 
            versions).  knit-kind is one of 'file', 'inventory', 'signatures',
1177
 
            'revisions'.  file-id is None unless knit-kind is 'file'.
1178
 
        """
1179
 
        # XXX: it's a bit weird to control the inventory weave caching in this
1180
 
        # generator.  Ideally the caching would be done in fetch.py I think.  Or
1181
 
        # maybe this generator should explicitly have the contract that it
1182
 
        # should not be iterated until the previously yielded item has been
1183
 
        # processed?
1184
 
        self.lock_read()
1185
 
        inv_w = self.get_inventory_weave()
1186
 
        inv_w.enable_cache()
1187
 
 
1188
 
        # file ids that changed
1189
 
        file_ids = self.fileids_altered_by_revision_ids(revision_ids)
1190
 
        count = 0
1191
 
        num_file_ids = len(file_ids)
1192
 
        for file_id, altered_versions in file_ids.iteritems():
1193
 
            if _files_pb is not None:
1194
 
                _files_pb.update("fetch texts", count, num_file_ids)
1195
 
            count += 1
1196
 
            yield ("file", file_id, altered_versions)
1197
 
        # We're done with the files_pb.  Note that it finished by the caller,
1198
 
        # just as it was created by the caller.
1199
 
        del _files_pb
1200
 
 
1201
 
        # inventory
1202
 
        yield ("inventory", None, revision_ids)
1203
 
        inv_w.clear_cache()
1204
 
 
1205
 
        # signatures
1206
 
        revisions_with_signatures = set()
1207
 
        for rev_id in revision_ids:
1208
 
            try:
1209
 
                self.get_signature_text(rev_id)
1210
 
            except errors.NoSuchRevision:
1211
 
                # not signed.
1212
 
                pass
1213
 
            else:
1214
 
                revisions_with_signatures.add(rev_id)
1215
 
        self.unlock()
1216
 
        yield ("signatures", None, revisions_with_signatures)
1217
 
 
1218
 
        # revisions
1219
 
        yield ("revisions", None, revision_ids)
1220
 
 
1221
436
    @needs_read_lock
1222
437
    def get_inventory_weave(self):
1223
438
        return self.control_weaves.get_weave('inventory',
1235
450
        :param revision_id: The expected revision id of the inventory.
1236
451
        :param xml: A serialised inventory.
1237
452
        """
1238
 
        return self._serializer.read_inventory_from_string(xml, revision_id)
1239
 
 
1240
 
    def serialise_inventory(self, inv):
1241
 
        return self._serializer.write_inventory_to_string(inv)
1242
 
 
1243
 
    def _serialise_inventory_to_lines(self, inv):
1244
 
        return self._serializer.write_inventory_to_lines(inv)
1245
 
 
1246
 
    def get_serializer_format(self):
1247
 
        return self._serializer.format_num
 
453
        result = xml5.serializer_v5.read_inventory_from_string(xml)
 
454
        result.root.revision = revision_id
 
455
        return result
1248
456
 
1249
457
    @needs_read_lock
1250
458
    def get_inventory_xml(self, revision_id):
1251
459
        """Get inventory XML as a file object."""
1252
460
        try:
1253
 
            assert isinstance(revision_id, str), type(revision_id)
 
461
            assert isinstance(revision_id, basestring), type(revision_id)
1254
462
            iw = self.get_inventory_weave()
1255
463
            return iw.get_text(revision_id)
1256
464
        except IndexError:
1265
473
    @needs_read_lock
1266
474
    def get_revision_graph(self, revision_id=None):
1267
475
        """Return a dictionary containing the revision graph.
1268
 
 
1269
 
        NB: This method should not be used as it accesses the entire graph all
1270
 
        at once, which is much more data than most operations should require.
1271
 
 
 
476
        
1272
477
        :param revision_id: The revision_id to get a graph from. If None, then
1273
478
        the entire revision graph is returned. This is a deprecated mode of
1274
479
        operation and will be removed in the future.
1275
480
        :return: a dictionary of revision_id->revision_parents_list.
1276
481
        """
1277
 
        raise NotImplementedError(self.get_revision_graph)
 
482
        # special case NULL_REVISION
 
483
        if revision_id == NULL_REVISION:
 
484
            return {}
 
485
        weave = self.get_inventory_weave()
 
486
        all_revisions = self._eliminate_revisions_not_present(weave.versions())
 
487
        entire_graph = dict([(node, weave.get_parents(node)) for 
 
488
                             node in all_revisions])
 
489
        if revision_id is None:
 
490
            return entire_graph
 
491
        elif revision_id not in entire_graph:
 
492
            raise errors.NoSuchRevision(self, revision_id)
 
493
        else:
 
494
            # add what can be reached from revision_id
 
495
            result = {}
 
496
            pending = set([revision_id])
 
497
            while len(pending) > 0:
 
498
                node = pending.pop()
 
499
                result[node] = entire_graph[node]
 
500
                for revision_id in result[node]:
 
501
                    if revision_id not in result:
 
502
                        pending.add(revision_id)
 
503
            return result
1278
504
 
1279
505
    @needs_read_lock
1280
506
    def get_revision_graph_with_ghosts(self, revision_ids=None):
1283
509
        :param revision_ids: an iterable of revisions to graph or None for all.
1284
510
        :return: a Graph object with the graph reachable from revision_ids.
1285
511
        """
1286
 
        if 'evil' in debug.debug_flags:
1287
 
            mutter_callsite(3,
1288
 
                "get_revision_graph_with_ghosts scales with size of history.")
1289
 
        result = deprecated_graph.Graph()
 
512
        result = Graph()
1290
513
        if not revision_ids:
1291
514
            pending = set(self.all_revision_ids())
1292
515
            required = set([])
1293
516
        else:
1294
517
            pending = set(revision_ids)
1295
518
            # special case NULL_REVISION
1296
 
            if _mod_revision.NULL_REVISION in pending:
1297
 
                pending.remove(_mod_revision.NULL_REVISION)
 
519
            if NULL_REVISION in pending:
 
520
                pending.remove(NULL_REVISION)
1298
521
            required = set(pending)
1299
522
        done = set([])
1300
523
        while len(pending):
1317
540
            done.add(revision_id)
1318
541
        return result
1319
542
 
1320
 
    def _get_history_vf(self):
1321
 
        """Get a versionedfile whose history graph reflects all revisions.
1322
 
 
1323
 
        For weave repositories, this is the inventory weave.
1324
 
        """
1325
 
        return self.get_inventory_weave()
1326
 
 
1327
 
    def iter_reverse_revision_history(self, revision_id):
1328
 
        """Iterate backwards through revision ids in the lefthand history
1329
 
 
1330
 
        :param revision_id: The revision id to start with.  All its lefthand
1331
 
            ancestors will be traversed.
1332
 
        """
1333
 
        if revision_id in (None, _mod_revision.NULL_REVISION):
1334
 
            return
1335
 
        next_id = revision_id
1336
 
        versionedfile = self._get_history_vf()
1337
 
        while True:
1338
 
            yield next_id
1339
 
            parents = versionedfile.get_parents(next_id)
1340
 
            if len(parents) == 0:
1341
 
                return
1342
 
            else:
1343
 
                next_id = parents[0]
1344
 
 
1345
543
    @needs_read_lock
1346
544
    def get_revision_inventory(self, revision_id):
1347
545
        """Return inventory of a past revision."""
1370
568
        reconciler = RepoReconciler(self, thorough=thorough)
1371
569
        reconciler.reconcile()
1372
570
        return reconciler
1373
 
 
1374
 
    def _refresh_data(self):
1375
 
        """Helper called from lock_* to ensure coherency with disk.
1376
 
 
1377
 
        The default implementation does nothing; it is however possible
1378
 
        for repositories to maintain loaded indices across multiple locks
1379
 
        by checking inside their implementation of this method to see
1380
 
        whether their indices are still valid. This depends of course on
1381
 
        the disk format being validatable in this manner.
1382
 
        """
1383
 
 
 
571
    
1384
572
    @needs_read_lock
1385
573
    def revision_tree(self, revision_id):
1386
574
        """Return Tree for a revision on this branch.
1389
577
        """
1390
578
        # TODO: refactor this to use an existing revision object
1391
579
        # so we don't need to read it in twice.
1392
 
        if revision_id is None or revision_id == _mod_revision.NULL_REVISION:
1393
 
            return RevisionTree(self, Inventory(root_id=None), 
1394
 
                                _mod_revision.NULL_REVISION)
 
580
        if revision_id is None or revision_id == NULL_REVISION:
 
581
            return RevisionTree(self, Inventory(), NULL_REVISION)
1395
582
        else:
1396
583
            inv = self.get_revision_inventory(revision_id)
1397
584
            return RevisionTree(self, inv, revision_id)
1402
589
 
1403
590
        `revision_id` may not be None or 'null:'"""
1404
591
        assert None not in revision_ids
1405
 
        assert _mod_revision.NULL_REVISION not in revision_ids
 
592
        assert NULL_REVISION not in revision_ids
1406
593
        texts = self.get_inventory_weave().get_texts(revision_ids)
1407
594
        for text, revision_id in zip(texts, revision_ids):
1408
595
            inv = self.deserialise_inventory(revision_id, text)
1409
596
            yield RevisionTree(self, inv, revision_id)
1410
597
 
1411
598
    @needs_read_lock
1412
 
    def get_ancestry(self, revision_id, topo_sorted=True):
 
599
    def get_ancestry(self, revision_id):
1413
600
        """Return a list of revision-ids integrated by a revision.
1414
601
 
1415
602
        The first element of the list is always None, indicating the origin 
1418
605
        
1419
606
        This is topologically sorted.
1420
607
        """
1421
 
        if _mod_revision.is_null(revision_id):
 
608
        if revision_id is None:
1422
609
            return [None]
1423
610
        if not self.has_revision(revision_id):
1424
611
            raise errors.NoSuchRevision(self, revision_id)
1425
612
        w = self.get_inventory_weave()
1426
 
        candidates = w.get_ancestry(revision_id, topo_sorted)
 
613
        candidates = w.get_ancestry(revision_id)
1427
614
        return [None] + candidates # self._eliminate_revisions_not_present(candidates)
1428
615
 
1429
 
    def pack(self):
1430
 
        """Compress the data within the repository.
1431
 
 
1432
 
        This operation only makes sense for some repository types. For other
1433
 
        types it should be a no-op that just returns.
1434
 
 
1435
 
        This stub method does not require a lock, but subclasses should use
1436
 
        @needs_write_lock as this is a long running call its reasonable to 
1437
 
        implicitly lock for the user.
1438
 
        """
1439
 
 
1440
616
    @needs_read_lock
1441
617
    def print_file(self, file, revision_id):
1442
618
        """Print `file` to stdout.
1458
634
    def get_transaction(self):
1459
635
        return self.control_files.get_transaction()
1460
636
 
1461
 
    def revision_parents(self, revision_id):
1462
 
        return self.get_inventory_weave().parent_names(revision_id)
1463
 
 
1464
 
    def get_parents(self, revision_ids):
1465
 
        """See StackedParentsProvider.get_parents"""
1466
 
        parents_list = []
1467
 
        for revision_id in revision_ids:
1468
 
            if revision_id == _mod_revision.NULL_REVISION:
1469
 
                parents = []
1470
 
            else:
1471
 
                try:
1472
 
                    parents = self.get_revision(revision_id).parent_ids
1473
 
                except errors.NoSuchRevision:
1474
 
                    parents = None
1475
 
                else:
1476
 
                    if len(parents) == 0:
1477
 
                        parents = [_mod_revision.NULL_REVISION]
1478
 
            parents_list.append(parents)
1479
 
        return parents_list
1480
 
 
1481
 
    def _make_parents_provider(self):
1482
 
        return self
1483
 
 
1484
 
    def get_graph(self, other_repository=None):
1485
 
        """Return the graph walker for this repository format"""
1486
 
        parents_provider = self._make_parents_provider()
1487
 
        if (other_repository is not None and
1488
 
            other_repository.bzrdir.transport.base !=
1489
 
            self.bzrdir.transport.base):
1490
 
            parents_provider = graph._StackedParentsProvider(
1491
 
                [parents_provider, other_repository._make_parents_provider()])
1492
 
        return graph.Graph(parents_provider)
1493
 
 
1494
 
    def get_versioned_file_checker(self, revisions, revision_versions_cache):
1495
 
        return VersionedFileChecker(revisions, revision_versions_cache, self)
 
637
    def revision_parents(self, revid):
 
638
        return self.get_inventory_weave().parent_names(revid)
1496
639
 
1497
640
    @needs_write_lock
1498
641
    def set_make_working_trees(self, new_value):
1528
671
                                                       self.get_transaction())
1529
672
 
1530
673
    @needs_read_lock
1531
 
    def check(self, revision_ids=None):
 
674
    def check(self, revision_ids):
1532
675
        """Check consistency of all history of given revision_ids.
1533
676
 
1534
677
        Different repository implementations should override _check().
1536
679
        :param revision_ids: A non-empty list of revision_ids whose ancestry
1537
680
             will be checked.  Typically the last revision_id of a branch.
1538
681
        """
 
682
        if not revision_ids:
 
683
            raise ValueError("revision_ids must be non-empty in %s.check" 
 
684
                    % (self,))
1539
685
        return self._check(revision_ids)
1540
686
 
1541
687
    def _check(self, revision_ids):
1551
697
        warning("Format %s for %s is deprecated - please use 'bzr upgrade' to get better performance"
1552
698
                % (self._format, self.bzrdir.transport.base))
1553
699
 
1554
 
    def supports_rich_root(self):
1555
 
        return self._format.rich_root_data
1556
 
 
1557
 
    def _check_ascii_revisionid(self, revision_id, method):
1558
 
        """Private helper for ascii-only repositories."""
1559
 
        # weave repositories refuse to store revisionids that are non-ascii.
1560
 
        if revision_id is not None:
1561
 
            # weaves require ascii revision ids.
1562
 
            if isinstance(revision_id, unicode):
1563
 
                try:
1564
 
                    revision_id.encode('ascii')
1565
 
                except UnicodeEncodeError:
1566
 
                    raise errors.NonAsciiRevisionId(method, self)
1567
 
            else:
1568
 
                try:
1569
 
                    revision_id.decode('ascii')
1570
 
                except UnicodeDecodeError:
1571
 
                    raise errors.NonAsciiRevisionId(method, self)
 
700
 
 
701
class AllInOneRepository(Repository):
 
702
    """Legacy support - the repository behaviour for all-in-one branches."""
 
703
 
 
704
    def __init__(self, _format, a_bzrdir, _revision_store, control_store, text_store):
 
705
        # we reuse one control files instance.
 
706
        dir_mode = a_bzrdir._control_files._dir_mode
 
707
        file_mode = a_bzrdir._control_files._file_mode
 
708
 
 
709
        def get_store(name, compressed=True, prefixed=False):
 
710
            # FIXME: This approach of assuming stores are all entirely compressed
 
711
            # or entirely uncompressed is tidy, but breaks upgrade from 
 
712
            # some existing branches where there's a mixture; we probably 
 
713
            # still want the option to look for both.
 
714
            relpath = a_bzrdir._control_files._escape(name)
 
715
            store = TextStore(a_bzrdir._control_files._transport.clone(relpath),
 
716
                              prefixed=prefixed, compressed=compressed,
 
717
                              dir_mode=dir_mode,
 
718
                              file_mode=file_mode)
 
719
            #if self._transport.should_cache():
 
720
            #    cache_path = os.path.join(self.cache_root, name)
 
721
            #    os.mkdir(cache_path)
 
722
            #    store = bzrlib.store.CachedStore(store, cache_path)
 
723
            return store
 
724
 
 
725
        # not broken out yet because the controlweaves|inventory_store
 
726
        # and text_store | weave_store bits are still different.
 
727
        if isinstance(_format, RepositoryFormat4):
 
728
            # cannot remove these - there is still no consistent api 
 
729
            # which allows access to this old info.
 
730
            self.inventory_store = get_store('inventory-store')
 
731
            text_store = get_store('text-store')
 
732
        super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, _revision_store, control_store, text_store)
 
733
 
 
734
    @needs_read_lock
 
735
    def is_shared(self):
 
736
        """AllInOne repositories cannot be shared."""
 
737
        return False
 
738
 
 
739
    @needs_write_lock
 
740
    def set_make_working_trees(self, new_value):
 
741
        """Set the policy flag for making working trees when creating branches.
 
742
 
 
743
        This only applies to branches that use this repository.
 
744
 
 
745
        The default is 'True'.
 
746
        :param new_value: True to restore the default, False to disable making
 
747
                          working trees.
 
748
        """
 
749
        raise NotImplementedError(self.set_make_working_trees)
1572
750
    
1573
 
    def revision_graph_can_have_wrong_parents(self):
1574
 
        """Is it possible for this repository to have a revision graph with
1575
 
        incorrect parents?
1576
 
 
1577
 
        If True, then this repository must also implement
1578
 
        _find_inconsistent_revision_parents so that check and reconcile can
1579
 
        check for inconsistencies before proceeding with other checks that may
1580
 
        depend on the revision index being consistent.
1581
 
        """
1582
 
        raise NotImplementedError(self.revision_graph_can_have_wrong_parents)
1583
 
        
1584
 
# remove these delegates a while after bzr 0.15
1585
 
def __make_delegated(name, from_module):
1586
 
    def _deprecated_repository_forwarder():
1587
 
        symbol_versioning.warn('%s moved to %s in bzr 0.15'
1588
 
            % (name, from_module),
1589
 
            DeprecationWarning,
1590
 
            stacklevel=2)
1591
 
        m = __import__(from_module, globals(), locals(), [name])
1592
 
        try:
1593
 
            return getattr(m, name)
1594
 
        except AttributeError:
1595
 
            raise AttributeError('module %s has no name %s'
1596
 
                    % (m, name))
1597
 
    globals()[name] = _deprecated_repository_forwarder
1598
 
 
1599
 
for _name in [
1600
 
        'AllInOneRepository',
1601
 
        'WeaveMetaDirRepository',
1602
 
        'PreSplitOutRepositoryFormat',
1603
 
        'RepositoryFormat4',
1604
 
        'RepositoryFormat5',
1605
 
        'RepositoryFormat6',
1606
 
        'RepositoryFormat7',
1607
 
        ]:
1608
 
    __make_delegated(_name, 'bzrlib.repofmt.weaverepo')
1609
 
 
1610
 
for _name in [
1611
 
        'KnitRepository',
1612
 
        'RepositoryFormatKnit',
1613
 
        'RepositoryFormatKnit1',
1614
 
        ]:
1615
 
    __make_delegated(_name, 'bzrlib.repofmt.knitrepo')
 
751
    def make_working_trees(self):
 
752
        """Returns the policy for making working trees on new branches."""
 
753
        return True
1616
754
 
1617
755
 
1618
756
def install_revision(repository, rev, revision_tree):
1619
757
    """Install all revision data into a repository."""
1620
 
    repository.start_write_group()
1621
 
    try:
1622
 
        _install_revision(repository, rev, revision_tree)
1623
 
    except:
1624
 
        repository.abort_write_group()
1625
 
        raise
1626
 
    else:
1627
 
        repository.commit_write_group()
1628
 
 
1629
 
 
1630
 
def _install_revision(repository, rev, revision_tree):
1631
 
    """Install all revision data into a repository."""
1632
758
    present_parents = []
1633
759
    parent_trees = {}
1634
760
    for p_id in rev.parent_ids:
1639
765
            parent_trees[p_id] = repository.revision_tree(None)
1640
766
 
1641
767
    inv = revision_tree.inventory
 
768
    
 
769
    # backwards compatability hack: skip the root id.
1642
770
    entries = inv.iter_entries()
1643
 
    # backwards compatibility hack: skip the root id.
1644
 
    if not repository.supports_rich_root():
1645
 
        path, root = entries.next()
1646
 
        if root.revision != rev.revision_id:
1647
 
            raise errors.IncompatibleRevision(repr(repository))
 
771
    entries.next()
1648
772
    # Add the texts that are not already present
1649
773
    for path, ie in entries:
1650
774
        w = repository.weave_store.get_weave_or_empty(ie.file_id,
1716
840
        return not self.control_files._transport.has('no-working-trees')
1717
841
 
1718
842
 
1719
 
class RepositoryFormatRegistry(registry.Registry):
1720
 
    """Registry of RepositoryFormats."""
1721
 
 
1722
 
    def get(self, format_string):
1723
 
        r = registry.Registry.get(self, format_string)
1724
 
        if callable(r):
1725
 
            r = r()
1726
 
        return r
 
843
class KnitRepository(MetaDirRepository):
 
844
    """Knit format repository."""
 
845
 
 
846
    def _warn_if_deprecated(self):
 
847
        # This class isn't deprecated
 
848
        pass
 
849
 
 
850
    def _inventory_add_lines(self, inv_vf, revid, parents, lines):
 
851
        inv_vf.add_lines_with_ghosts(revid, parents, lines)
 
852
 
 
853
    @needs_read_lock
 
854
    def _all_revision_ids(self):
 
855
        """See Repository.all_revision_ids()."""
 
856
        # Knits get the revision graph from the index of the revision knit, so
 
857
        # it's always possible even if they're on an unlistable transport.
 
858
        return self._revision_store.all_revision_ids(self.get_transaction())
 
859
 
 
860
    def fileid_involved_between_revs(self, from_revid, to_revid):
 
861
        """Find file_id(s) which are involved in the changes between revisions.
 
862
 
 
863
        This determines the set of revisions which are involved, and then
 
864
        finds all file ids affected by those revisions.
 
865
        """
 
866
        vf = self._get_revision_vf()
 
867
        from_set = set(vf.get_ancestry(from_revid))
 
868
        to_set = set(vf.get_ancestry(to_revid))
 
869
        changed = to_set.difference(from_set)
 
870
        return self._fileid_involved_by_set(changed)
 
871
 
 
872
    def fileid_involved(self, last_revid=None):
 
873
        """Find all file_ids modified in the ancestry of last_revid.
 
874
 
 
875
        :param last_revid: If None, last_revision() will be used.
 
876
        """
 
877
        if not last_revid:
 
878
            changed = set(self.all_revision_ids())
 
879
        else:
 
880
            changed = set(self.get_ancestry(last_revid))
 
881
        if None in changed:
 
882
            changed.remove(None)
 
883
        return self._fileid_involved_by_set(changed)
 
884
 
 
885
    @needs_read_lock
 
886
    def get_ancestry(self, revision_id):
 
887
        """Return a list of revision-ids integrated by a revision.
 
888
        
 
889
        This is topologically sorted.
 
890
        """
 
891
        if revision_id is None:
 
892
            return [None]
 
893
        vf = self._get_revision_vf()
 
894
        try:
 
895
            return [None] + vf.get_ancestry(revision_id)
 
896
        except errors.RevisionNotPresent:
 
897
            raise errors.NoSuchRevision(self, revision_id)
 
898
 
 
899
    @needs_read_lock
 
900
    def get_revision(self, revision_id):
 
901
        """Return the Revision object for a named revision"""
 
902
        return self.get_revision_reconcile(revision_id)
 
903
 
 
904
    @needs_read_lock
 
905
    def get_revision_graph(self, revision_id=None):
 
906
        """Return a dictionary containing the revision graph.
 
907
 
 
908
        :param revision_id: The revision_id to get a graph from. If None, then
 
909
        the entire revision graph is returned. This is a deprecated mode of
 
910
        operation and will be removed in the future.
 
911
        :return: a dictionary of revision_id->revision_parents_list.
 
912
        """
 
913
        # special case NULL_REVISION
 
914
        if revision_id == NULL_REVISION:
 
915
            return {}
 
916
        weave = self._get_revision_vf()
 
917
        entire_graph = weave.get_graph()
 
918
        if revision_id is None:
 
919
            return weave.get_graph()
 
920
        elif revision_id not in weave:
 
921
            raise errors.NoSuchRevision(self, revision_id)
 
922
        else:
 
923
            # add what can be reached from revision_id
 
924
            result = {}
 
925
            pending = set([revision_id])
 
926
            while len(pending) > 0:
 
927
                node = pending.pop()
 
928
                result[node] = weave.get_parents(node)
 
929
                for revision_id in result[node]:
 
930
                    if revision_id not in result:
 
931
                        pending.add(revision_id)
 
932
            return result
 
933
 
 
934
    @needs_read_lock
 
935
    def get_revision_graph_with_ghosts(self, revision_ids=None):
 
936
        """Return a graph of the revisions with ghosts marked as applicable.
 
937
 
 
938
        :param revision_ids: an iterable of revisions to graph or None for all.
 
939
        :return: a Graph object with the graph reachable from revision_ids.
 
940
        """
 
941
        result = Graph()
 
942
        vf = self._get_revision_vf()
 
943
        versions = set(vf.versions())
 
944
        if not revision_ids:
 
945
            pending = set(self.all_revision_ids())
 
946
            required = set([])
 
947
        else:
 
948
            pending = set(revision_ids)
 
949
            # special case NULL_REVISION
 
950
            if NULL_REVISION in pending:
 
951
                pending.remove(NULL_REVISION)
 
952
            required = set(pending)
 
953
        done = set([])
 
954
        while len(pending):
 
955
            revision_id = pending.pop()
 
956
            if not revision_id in versions:
 
957
                if revision_id in required:
 
958
                    raise errors.NoSuchRevision(self, revision_id)
 
959
                # a ghost
 
960
                result.add_ghost(revision_id)
 
961
                # mark it as done so we don't try for it again.
 
962
                done.add(revision_id)
 
963
                continue
 
964
            parent_ids = vf.get_parents_with_ghosts(revision_id)
 
965
            for parent_id in parent_ids:
 
966
                # is this queued or done ?
 
967
                if (parent_id not in pending and
 
968
                    parent_id not in done):
 
969
                    # no, queue it.
 
970
                    pending.add(parent_id)
 
971
            result.add_node(revision_id, parent_ids)
 
972
            done.add(revision_id)
 
973
        return result
 
974
 
 
975
    def _get_revision_vf(self):
 
976
        """:return: a versioned file containing the revisions."""
 
977
        vf = self._revision_store.get_revision_file(self.get_transaction())
 
978
        return vf
 
979
 
 
980
    @needs_write_lock
 
981
    def reconcile(self, other=None, thorough=False):
 
982
        """Reconcile this repository."""
 
983
        from bzrlib.reconcile import KnitReconciler
 
984
        reconciler = KnitReconciler(self, thorough=thorough)
 
985
        reconciler.reconcile()
 
986
        return reconciler
1727
987
    
1728
 
 
1729
 
format_registry = RepositoryFormatRegistry()
1730
 
"""Registry of formats, indexed by their identifying format string.
1731
 
 
1732
 
This can contain either format instances themselves, or classes/factories that
1733
 
can be called to obtain one.
1734
 
"""
1735
 
 
1736
 
 
1737
 
#####################################################################
1738
 
# Repository Formats
 
988
    def revision_parents(self, revision_id):
 
989
        return self._get_revision_vf().get_parents(revision_id)
 
990
 
1739
991
 
1740
992
class RepositoryFormat(object):
1741
993
    """A repository format.
1746
998
       children.
1747
999
     * an open routine which returns a Repository instance.
1748
1000
 
1749
 
    There is one and only one Format subclass for each on-disk format. But
1750
 
    there can be one Repository subclass that is used for several different
1751
 
    formats. The _format attribute on a Repository instance can be used to
1752
 
    determine the disk format.
1753
 
 
1754
1001
    Formats are placed in an dict by their format string for reference 
1755
1002
    during opening. These should be subclasses of RepositoryFormat
1756
1003
    for consistency.
1766
1013
    parameterisation.
1767
1014
    """
1768
1015
 
 
1016
    _default_format = None
 
1017
    """The default format used for new repositories."""
 
1018
 
 
1019
    _formats = {}
 
1020
    """The known formats."""
 
1021
 
1769
1022
    def __str__(self):
1770
1023
        return "<%s>" % self.__class__.__name__
1771
1024
 
1772
 
    def __eq__(self, other):
1773
 
        # format objects are generally stateless
1774
 
        return isinstance(other, self.__class__)
1775
 
 
1776
 
    def __ne__(self, other):
1777
 
        return not self == other
1778
 
 
1779
1025
    @classmethod
1780
1026
    def find_format(klass, a_bzrdir):
1781
 
        """Return the format for the repository object in a_bzrdir.
1782
 
        
1783
 
        This is used by bzr native formats that have a "format" file in
1784
 
        the repository.  Other methods may be used by different types of 
1785
 
        control directory.
1786
 
        """
 
1027
        """Return the format for the repository object in a_bzrdir."""
1787
1028
        try:
1788
1029
            transport = a_bzrdir.get_repository_transport(None)
1789
1030
            format_string = transport.get("format").read()
1790
 
            return format_registry.get(format_string)
 
1031
            return klass._formats[format_string]
1791
1032
        except errors.NoSuchFile:
1792
1033
            raise errors.NoRepositoryPresent(a_bzrdir)
1793
1034
        except KeyError:
1794
1035
            raise errors.UnknownFormatError(format=format_string)
1795
1036
 
1796
 
    @classmethod
1797
 
    def register_format(klass, format):
1798
 
        format_registry.register(format.get_format_string(), format)
1799
 
 
1800
 
    @classmethod
1801
 
    def unregister_format(klass, format):
1802
 
        format_registry.remove(format.get_format_string())
 
1037
    def _get_control_store(self, repo_transport, control_files):
 
1038
        """Return the control store for this repository."""
 
1039
        raise NotImplementedError(self._get_control_store)
1803
1040
    
1804
1041
    @classmethod
1805
1042
    def get_default_format(klass):
1806
1043
        """Return the current default format."""
1807
 
        from bzrlib import bzrdir
1808
 
        return bzrdir.format_registry.make_bzrdir('default').repository_format
1809
 
 
1810
 
    def _get_control_store(self, repo_transport, control_files):
1811
 
        """Return the control store for this repository."""
1812
 
        raise NotImplementedError(self._get_control_store)
 
1044
        return klass._default_format
1813
1045
 
1814
1046
    def get_format_string(self):
1815
1047
        """Return the ASCII format string that identifies this format.
1842
1074
        from bzrlib.store.revision.text import TextRevisionStore
1843
1075
        dir_mode = control_files._dir_mode
1844
1076
        file_mode = control_files._file_mode
1845
 
        text_store = TextStore(transport.clone(name),
 
1077
        text_store =TextStore(transport.clone(name),
1846
1078
                              prefixed=prefixed,
1847
1079
                              compressed=compressed,
1848
1080
                              dir_mode=dir_mode,
1850
1082
        _revision_store = TextRevisionStore(text_store, serializer)
1851
1083
        return _revision_store
1852
1084
 
1853
 
    # TODO: this shouldn't be in the base class, it's specific to things that
1854
 
    # use weaves or knits -- mbp 20070207
1855
1085
    def _get_versioned_file_store(self,
1856
1086
                                  name,
1857
1087
                                  transport,
1858
1088
                                  control_files,
1859
1089
                                  prefixed=True,
1860
 
                                  versionedfile_class=None,
1861
 
                                  versionedfile_kwargs={},
 
1090
                                  versionedfile_class=WeaveFile,
1862
1091
                                  escaped=False):
1863
 
        if versionedfile_class is None:
1864
 
            versionedfile_class = self._versionedfile_class
1865
1092
        weave_transport = control_files._transport.clone(name)
1866
1093
        dir_mode = control_files._dir_mode
1867
1094
        file_mode = control_files._file_mode
1869
1096
                                  dir_mode=dir_mode,
1870
1097
                                  file_mode=file_mode,
1871
1098
                                  versionedfile_class=versionedfile_class,
1872
 
                                  versionedfile_kwargs=versionedfile_kwargs,
1873
1099
                                  escaped=escaped)
1874
1100
 
1875
1101
    def initialize(self, a_bzrdir, shared=False):
1877
1103
 
1878
1104
        :param a_bzrdir: The bzrdir to put the new repository in it.
1879
1105
        :param shared: The repository should be initialized as a sharable one.
1880
 
        :returns: The new repository object.
1881
 
        
 
1106
 
1882
1107
        This may raise UninitializableFormat if shared repository are not
1883
1108
        compatible the a_bzrdir.
1884
1109
        """
1885
 
        raise NotImplementedError(self.initialize)
1886
1110
 
1887
1111
    def is_supported(self):
1888
1112
        """Is this format supported?
1893
1117
        """
1894
1118
        return True
1895
1119
 
1896
 
    def check_conversion_target(self, target_format):
1897
 
        raise NotImplementedError(self.check_conversion_target)
1898
 
 
1899
1120
    def open(self, a_bzrdir, _found=False):
1900
1121
        """Return an instance of this format for the bzrdir a_bzrdir.
1901
1122
        
1903
1124
        """
1904
1125
        raise NotImplementedError(self.open)
1905
1126
 
 
1127
    @classmethod
 
1128
    def register_format(klass, format):
 
1129
        klass._formats[format.get_format_string()] = format
 
1130
 
 
1131
    @classmethod
 
1132
    def set_default_format(klass, format):
 
1133
        klass._default_format = format
 
1134
 
 
1135
    @classmethod
 
1136
    def unregister_format(klass, format):
 
1137
        assert klass._formats[format.get_format_string()] is format
 
1138
        del klass._formats[format.get_format_string()]
 
1139
 
 
1140
 
 
1141
class PreSplitOutRepositoryFormat(RepositoryFormat):
 
1142
    """Base class for the pre split out repository formats."""
 
1143
 
 
1144
    def initialize(self, a_bzrdir, shared=False, _internal=False):
 
1145
        """Create a weave repository.
 
1146
        
 
1147
        TODO: when creating split out bzr branch formats, move this to a common
 
1148
        base for Format5, Format6. or something like that.
 
1149
        """
 
1150
        from bzrlib.weavefile import write_weave_v5
 
1151
        from bzrlib.weave import Weave
 
1152
 
 
1153
        if shared:
 
1154
            raise errors.IncompatibleFormat(self, a_bzrdir._format)
 
1155
 
 
1156
        if not _internal:
 
1157
            # always initialized when the bzrdir is.
 
1158
            return self.open(a_bzrdir, _found=True)
 
1159
        
 
1160
        # Create an empty weave
 
1161
        sio = StringIO()
 
1162
        write_weave_v5(Weave(), sio)
 
1163
        empty_weave = sio.getvalue()
 
1164
 
 
1165
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
1166
        dirs = ['revision-store', 'weaves']
 
1167
        files = [('inventory.weave', StringIO(empty_weave)),
 
1168
                 ]
 
1169
        
 
1170
        # FIXME: RBC 20060125 don't peek under the covers
 
1171
        # NB: no need to escape relative paths that are url safe.
 
1172
        control_files = LockableFiles(a_bzrdir.transport, 'branch-lock',
 
1173
                                      TransportLock)
 
1174
        control_files.create_lock()
 
1175
        control_files.lock_write()
 
1176
        control_files._transport.mkdir_multi(dirs,
 
1177
                mode=control_files._dir_mode)
 
1178
        try:
 
1179
            for file, content in files:
 
1180
                control_files.put(file, content)
 
1181
        finally:
 
1182
            control_files.unlock()
 
1183
        return self.open(a_bzrdir, _found=True)
 
1184
 
 
1185
    def _get_control_store(self, repo_transport, control_files):
 
1186
        """Return the control store for this repository."""
 
1187
        return self._get_versioned_file_store('',
 
1188
                                              repo_transport,
 
1189
                                              control_files,
 
1190
                                              prefixed=False)
 
1191
 
 
1192
    def _get_text_store(self, transport, control_files):
 
1193
        """Get a store for file texts for this format."""
 
1194
        raise NotImplementedError(self._get_text_store)
 
1195
 
 
1196
    def open(self, a_bzrdir, _found=False):
 
1197
        """See RepositoryFormat.open()."""
 
1198
        if not _found:
 
1199
            # we are being called directly and must probe.
 
1200
            raise NotImplementedError
 
1201
 
 
1202
        repo_transport = a_bzrdir.get_repository_transport(None)
 
1203
        control_files = a_bzrdir._control_files
 
1204
        text_store = self._get_text_store(repo_transport, control_files)
 
1205
        control_store = self._get_control_store(repo_transport, control_files)
 
1206
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
1207
        return AllInOneRepository(_format=self,
 
1208
                                  a_bzrdir=a_bzrdir,
 
1209
                                  _revision_store=_revision_store,
 
1210
                                  control_store=control_store,
 
1211
                                  text_store=text_store)
 
1212
 
 
1213
 
 
1214
class RepositoryFormat4(PreSplitOutRepositoryFormat):
 
1215
    """Bzr repository format 4.
 
1216
 
 
1217
    This repository format has:
 
1218
     - flat stores
 
1219
     - TextStores for texts, inventories,revisions.
 
1220
 
 
1221
    This format is deprecated: it indexes texts using a text id which is
 
1222
    removed in format 5; initialization and write support for this format
 
1223
    has been removed.
 
1224
    """
 
1225
 
 
1226
    def __init__(self):
 
1227
        super(RepositoryFormat4, self).__init__()
 
1228
        self._matchingbzrdir = bzrdir.BzrDirFormat4()
 
1229
 
 
1230
    def get_format_description(self):
 
1231
        """See RepositoryFormat.get_format_description()."""
 
1232
        return "Repository format 4"
 
1233
 
 
1234
    def initialize(self, url, shared=False, _internal=False):
 
1235
        """Format 4 branches cannot be created."""
 
1236
        raise errors.UninitializableFormat(self)
 
1237
 
 
1238
    def is_supported(self):
 
1239
        """Format 4 is not supported.
 
1240
 
 
1241
        It is not supported because the model changed from 4 to 5 and the
 
1242
        conversion logic is expensive - so doing it on the fly was not 
 
1243
        feasible.
 
1244
        """
 
1245
        return False
 
1246
 
 
1247
    def _get_control_store(self, repo_transport, control_files):
 
1248
        """Format 4 repositories have no formal control store at this point.
 
1249
        
 
1250
        This will cause any control-file-needing apis to fail - this is desired.
 
1251
        """
 
1252
        return None
 
1253
    
 
1254
    def _get_revision_store(self, repo_transport, control_files):
 
1255
        """See RepositoryFormat._get_revision_store()."""
 
1256
        from bzrlib.xml4 import serializer_v4
 
1257
        return self._get_text_rev_store(repo_transport,
 
1258
                                        control_files,
 
1259
                                        'revision-store',
 
1260
                                        serializer=serializer_v4)
 
1261
 
 
1262
    def _get_text_store(self, transport, control_files):
 
1263
        """See RepositoryFormat._get_text_store()."""
 
1264
 
 
1265
 
 
1266
class RepositoryFormat5(PreSplitOutRepositoryFormat):
 
1267
    """Bzr control format 5.
 
1268
 
 
1269
    This repository format has:
 
1270
     - weaves for file texts and inventory
 
1271
     - flat stores
 
1272
     - TextStores for revisions and signatures.
 
1273
    """
 
1274
 
 
1275
    def __init__(self):
 
1276
        super(RepositoryFormat5, self).__init__()
 
1277
        self._matchingbzrdir = bzrdir.BzrDirFormat5()
 
1278
 
 
1279
    def get_format_description(self):
 
1280
        """See RepositoryFormat.get_format_description()."""
 
1281
        return "Weave repository format 5"
 
1282
 
 
1283
    def _get_revision_store(self, repo_transport, control_files):
 
1284
        """See RepositoryFormat._get_revision_store()."""
 
1285
        """Return the revision store object for this a_bzrdir."""
 
1286
        return self._get_text_rev_store(repo_transport,
 
1287
                                        control_files,
 
1288
                                        'revision-store',
 
1289
                                        compressed=False)
 
1290
 
 
1291
    def _get_text_store(self, transport, control_files):
 
1292
        """See RepositoryFormat._get_text_store()."""
 
1293
        return self._get_versioned_file_store('weaves', transport, control_files, prefixed=False)
 
1294
 
 
1295
 
 
1296
class RepositoryFormat6(PreSplitOutRepositoryFormat):
 
1297
    """Bzr control format 6.
 
1298
 
 
1299
    This repository format has:
 
1300
     - weaves for file texts and inventory
 
1301
     - hash subdirectory based stores.
 
1302
     - TextStores for revisions and signatures.
 
1303
    """
 
1304
 
 
1305
    def __init__(self):
 
1306
        super(RepositoryFormat6, self).__init__()
 
1307
        self._matchingbzrdir = bzrdir.BzrDirFormat6()
 
1308
 
 
1309
    def get_format_description(self):
 
1310
        """See RepositoryFormat.get_format_description()."""
 
1311
        return "Weave repository format 6"
 
1312
 
 
1313
    def _get_revision_store(self, repo_transport, control_files):
 
1314
        """See RepositoryFormat._get_revision_store()."""
 
1315
        return self._get_text_rev_store(repo_transport,
 
1316
                                        control_files,
 
1317
                                        'revision-store',
 
1318
                                        compressed=False,
 
1319
                                        prefixed=True)
 
1320
 
 
1321
    def _get_text_store(self, transport, control_files):
 
1322
        """See RepositoryFormat._get_text_store()."""
 
1323
        return self._get_versioned_file_store('weaves', transport, control_files)
 
1324
 
1906
1325
 
1907
1326
class MetaDirRepositoryFormat(RepositoryFormat):
1908
1327
    """Common base class for the new repositories using the metadir layout."""
1909
1328
 
1910
 
    rich_root_data = False
1911
 
    supports_tree_reference = False
1912
 
    _matchingbzrdir = bzrdir.BzrDirMetaFormat1()
1913
 
 
1914
1329
    def __init__(self):
1915
1330
        super(MetaDirRepositoryFormat, self).__init__()
 
1331
        self._matchingbzrdir = bzrdir.BzrDirMetaFormat1()
1916
1332
 
1917
1333
    def _create_control_files(self, a_bzrdir):
1918
1334
        """Create the required files and the initial control_files object."""
1919
1335
        # FIXME: RBC 20060125 don't peek under the covers
1920
1336
        # NB: no need to escape relative paths that are url safe.
1921
1337
        repository_transport = a_bzrdir.get_repository_transport(self)
1922
 
        control_files = lockable_files.LockableFiles(repository_transport,
1923
 
                                'lock', lockdir.LockDir)
 
1338
        control_files = LockableFiles(repository_transport, 'lock', LockDir)
1924
1339
        control_files.create_lock()
1925
1340
        return control_files
1926
1341
 
1941
1356
            control_files.unlock()
1942
1357
 
1943
1358
 
 
1359
class RepositoryFormat7(MetaDirRepositoryFormat):
 
1360
    """Bzr repository 7.
 
1361
 
 
1362
    This repository format has:
 
1363
     - weaves for file texts and inventory
 
1364
     - hash subdirectory based stores.
 
1365
     - TextStores for revisions and signatures.
 
1366
     - a format marker of its own
 
1367
     - an optional 'shared-storage' flag
 
1368
     - an optional 'no-working-trees' flag
 
1369
    """
 
1370
 
 
1371
    def _get_control_store(self, repo_transport, control_files):
 
1372
        """Return the control store for this repository."""
 
1373
        return self._get_versioned_file_store('',
 
1374
                                              repo_transport,
 
1375
                                              control_files,
 
1376
                                              prefixed=False)
 
1377
 
 
1378
    def get_format_string(self):
 
1379
        """See RepositoryFormat.get_format_string()."""
 
1380
        return "Bazaar-NG Repository format 7"
 
1381
 
 
1382
    def get_format_description(self):
 
1383
        """See RepositoryFormat.get_format_description()."""
 
1384
        return "Weave repository format 7"
 
1385
 
 
1386
    def _get_revision_store(self, repo_transport, control_files):
 
1387
        """See RepositoryFormat._get_revision_store()."""
 
1388
        return self._get_text_rev_store(repo_transport,
 
1389
                                        control_files,
 
1390
                                        'revision-store',
 
1391
                                        compressed=False,
 
1392
                                        prefixed=True,
 
1393
                                        )
 
1394
 
 
1395
    def _get_text_store(self, transport, control_files):
 
1396
        """See RepositoryFormat._get_text_store()."""
 
1397
        return self._get_versioned_file_store('weaves',
 
1398
                                              transport,
 
1399
                                              control_files)
 
1400
 
 
1401
    def initialize(self, a_bzrdir, shared=False):
 
1402
        """Create a weave repository.
 
1403
 
 
1404
        :param shared: If true the repository will be initialized as a shared
 
1405
                       repository.
 
1406
        """
 
1407
        from bzrlib.weavefile import write_weave_v5
 
1408
        from bzrlib.weave import Weave
 
1409
 
 
1410
        # Create an empty weave
 
1411
        sio = StringIO()
 
1412
        write_weave_v5(Weave(), sio)
 
1413
        empty_weave = sio.getvalue()
 
1414
 
 
1415
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
1416
        dirs = ['revision-store', 'weaves']
 
1417
        files = [('inventory.weave', StringIO(empty_weave)), 
 
1418
                 ]
 
1419
        utf8_files = [('format', self.get_format_string())]
 
1420
 
 
1421
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
 
1422
        return self.open(a_bzrdir=a_bzrdir, _found=True)
 
1423
 
 
1424
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
1425
        """See RepositoryFormat.open().
 
1426
        
 
1427
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
1428
                                    repository at a slightly different url
 
1429
                                    than normal. I.e. during 'upgrade'.
 
1430
        """
 
1431
        if not _found:
 
1432
            format = RepositoryFormat.find_format(a_bzrdir)
 
1433
            assert format.__class__ ==  self.__class__
 
1434
        if _override_transport is not None:
 
1435
            repo_transport = _override_transport
 
1436
        else:
 
1437
            repo_transport = a_bzrdir.get_repository_transport(None)
 
1438
        control_files = LockableFiles(repo_transport, 'lock', LockDir)
 
1439
        text_store = self._get_text_store(repo_transport, control_files)
 
1440
        control_store = self._get_control_store(repo_transport, control_files)
 
1441
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
1442
        return MetaDirRepository(_format=self,
 
1443
                                 a_bzrdir=a_bzrdir,
 
1444
                                 control_files=control_files,
 
1445
                                 _revision_store=_revision_store,
 
1446
                                 control_store=control_store,
 
1447
                                 text_store=text_store)
 
1448
 
 
1449
 
 
1450
class RepositoryFormatKnit1(MetaDirRepositoryFormat):
 
1451
    """Bzr repository knit format 1.
 
1452
 
 
1453
    This repository format has:
 
1454
     - knits for file texts and inventory
 
1455
     - hash subdirectory based stores.
 
1456
     - knits for revisions and signatures
 
1457
     - TextStores for revisions and signatures.
 
1458
     - a format marker of its own
 
1459
     - an optional 'shared-storage' flag
 
1460
     - an optional 'no-working-trees' flag
 
1461
     - a LockDir lock
 
1462
 
 
1463
    This format was introduced in bzr 0.8.
 
1464
    """
 
1465
 
 
1466
    def _get_control_store(self, repo_transport, control_files):
 
1467
        """Return the control store for this repository."""
 
1468
        return VersionedFileStore(
 
1469
            repo_transport,
 
1470
            prefixed=False,
 
1471
            file_mode=control_files._file_mode,
 
1472
            versionedfile_class=KnitVersionedFile,
 
1473
            versionedfile_kwargs={'factory':KnitPlainFactory()},
 
1474
            )
 
1475
 
 
1476
    def get_format_string(self):
 
1477
        """See RepositoryFormat.get_format_string()."""
 
1478
        return "Bazaar-NG Knit Repository Format 1"
 
1479
 
 
1480
    def get_format_description(self):
 
1481
        """See RepositoryFormat.get_format_description()."""
 
1482
        return "Knit repository format 1"
 
1483
 
 
1484
    def _get_revision_store(self, repo_transport, control_files):
 
1485
        """See RepositoryFormat._get_revision_store()."""
 
1486
        from bzrlib.store.revision.knit import KnitRevisionStore
 
1487
        versioned_file_store = VersionedFileStore(
 
1488
            repo_transport,
 
1489
            file_mode=control_files._file_mode,
 
1490
            prefixed=False,
 
1491
            precious=True,
 
1492
            versionedfile_class=KnitVersionedFile,
 
1493
            versionedfile_kwargs={'delta':False, 'factory':KnitPlainFactory()},
 
1494
            escaped=True,
 
1495
            )
 
1496
        return KnitRevisionStore(versioned_file_store)
 
1497
 
 
1498
    def _get_text_store(self, transport, control_files):
 
1499
        """See RepositoryFormat._get_text_store()."""
 
1500
        return self._get_versioned_file_store('knits',
 
1501
                                              transport,
 
1502
                                              control_files,
 
1503
                                              versionedfile_class=KnitVersionedFile,
 
1504
                                              escaped=True)
 
1505
 
 
1506
    def initialize(self, a_bzrdir, shared=False):
 
1507
        """Create a knit format 1 repository.
 
1508
 
 
1509
        :param a_bzrdir: bzrdir to contain the new repository; must already
 
1510
            be initialized.
 
1511
        :param shared: If true the repository will be initialized as a shared
 
1512
                       repository.
 
1513
        """
 
1514
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
1515
        dirs = ['revision-store', 'knits']
 
1516
        files = []
 
1517
        utf8_files = [('format', self.get_format_string())]
 
1518
        
 
1519
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
 
1520
        repo_transport = a_bzrdir.get_repository_transport(None)
 
1521
        control_files = LockableFiles(repo_transport, 'lock', LockDir)
 
1522
        control_store = self._get_control_store(repo_transport, control_files)
 
1523
        transaction = transactions.WriteTransaction()
 
1524
        # trigger a write of the inventory store.
 
1525
        control_store.get_weave_or_empty('inventory', transaction)
 
1526
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
1527
        _revision_store.has_revision_id('A', transaction)
 
1528
        _revision_store.get_signature_file(transaction)
 
1529
        return self.open(a_bzrdir=a_bzrdir, _found=True)
 
1530
 
 
1531
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
1532
        """See RepositoryFormat.open().
 
1533
        
 
1534
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
1535
                                    repository at a slightly different url
 
1536
                                    than normal. I.e. during 'upgrade'.
 
1537
        """
 
1538
        if not _found:
 
1539
            format = RepositoryFormat.find_format(a_bzrdir)
 
1540
            assert format.__class__ ==  self.__class__
 
1541
        if _override_transport is not None:
 
1542
            repo_transport = _override_transport
 
1543
        else:
 
1544
            repo_transport = a_bzrdir.get_repository_transport(None)
 
1545
        control_files = LockableFiles(repo_transport, 'lock', LockDir)
 
1546
        text_store = self._get_text_store(repo_transport, control_files)
 
1547
        control_store = self._get_control_store(repo_transport, control_files)
 
1548
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
1549
        return KnitRepository(_format=self,
 
1550
                              a_bzrdir=a_bzrdir,
 
1551
                              control_files=control_files,
 
1552
                              _revision_store=_revision_store,
 
1553
                              control_store=control_store,
 
1554
                              text_store=text_store)
 
1555
 
 
1556
 
1944
1557
# formats which have no format string are not discoverable
1945
 
# and not independently creatable, so are not registered.  They're 
1946
 
# all in bzrlib.repofmt.weaverepo now.  When an instance of one of these is
1947
 
# needed, it's constructed directly by the BzrDir.  Non-native formats where
1948
 
# the repository is not separately opened are similar.
1949
 
 
1950
 
format_registry.register_lazy(
1951
 
    'Bazaar-NG Repository format 7',
1952
 
    'bzrlib.repofmt.weaverepo',
1953
 
    'RepositoryFormat7'
1954
 
    )
1955
 
 
1956
 
# KEEP in sync with bzrdir.format_registry default, which controls the overall
1957
 
# default control directory format
1958
 
format_registry.register_lazy(
1959
 
    'Bazaar-NG Knit Repository Format 1',
1960
 
    'bzrlib.repofmt.knitrepo',
1961
 
    'RepositoryFormatKnit1',
1962
 
    )
1963
 
format_registry.default_key = 'Bazaar-NG Knit Repository Format 1'
1964
 
 
1965
 
format_registry.register_lazy(
1966
 
    'Bazaar Knit Repository Format 3 (bzr 0.15)\n',
1967
 
    'bzrlib.repofmt.knitrepo',
1968
 
    'RepositoryFormatKnit3',
1969
 
    )
1970
 
 
1971
 
# Experimental formats. These make no guarantee about data stability.
1972
 
# There is one format for pre-subtrees, and one for post-subtrees to
1973
 
# allow ease of testing.
1974
 
format_registry.register_lazy(
1975
 
    'Bazaar Experimental no-subtrees\n',
1976
 
    'bzrlib.repofmt.pack_repo',
1977
 
    'RepositoryFormatKnitPack1',
1978
 
    )
1979
 
format_registry.register_lazy(
1980
 
    'Bazaar Experimental subtrees\n',
1981
 
    'bzrlib.repofmt.pack_repo',
1982
 
    'RepositoryFormatKnitPack3',
1983
 
    )
 
1558
# and not independently creatable, so are not registered.
 
1559
RepositoryFormat.register_format(RepositoryFormat7())
 
1560
_default_format = RepositoryFormatKnit1()
 
1561
RepositoryFormat.register_format(_default_format)
 
1562
RepositoryFormat.set_default_format(_default_format)
 
1563
_legacy_formats = [RepositoryFormat4(),
 
1564
                   RepositoryFormat5(),
 
1565
                   RepositoryFormat6()]
1984
1566
 
1985
1567
 
1986
1568
class InterRepository(InterObject):
1995
1577
    InterRepository.get(other).method_name(parameters).
1996
1578
    """
1997
1579
 
1998
 
    _optimisers = []
 
1580
    _optimisers = set()
1999
1581
    """The available optimised InterRepository types."""
2000
1582
 
2001
 
    def copy_content(self, revision_id=None):
2002
 
        raise NotImplementedError(self.copy_content)
2003
 
 
 
1583
    @needs_write_lock
 
1584
    def copy_content(self, revision_id=None, basis=None):
 
1585
        """Make a complete copy of the content in self into destination.
 
1586
        
 
1587
        This is a destructive operation! Do not use it on existing 
 
1588
        repositories.
 
1589
 
 
1590
        :param revision_id: Only copy the content needed to construct
 
1591
                            revision_id and its parents.
 
1592
        :param basis: Copy the needed data preferentially from basis.
 
1593
        """
 
1594
        try:
 
1595
            self.target.set_make_working_trees(self.source.make_working_trees())
 
1596
        except NotImplementedError:
 
1597
            pass
 
1598
        # grab the basis available data
 
1599
        if basis is not None:
 
1600
            self.target.fetch(basis, revision_id=revision_id)
 
1601
        # but don't bother fetching if we have the needed data now.
 
1602
        if (revision_id not in (None, NULL_REVISION) and 
 
1603
            self.target.has_revision(revision_id)):
 
1604
            return
 
1605
        self.target.fetch(self.source, revision_id=revision_id)
 
1606
 
 
1607
    @needs_write_lock
2004
1608
    def fetch(self, revision_id=None, pb=None):
2005
1609
        """Fetch the content required to construct revision_id.
2006
1610
 
2007
 
        The content is copied from self.source to self.target.
 
1611
        The content is copied from source to target.
2008
1612
 
2009
1613
        :param revision_id: if None all content is copied, if NULL_REVISION no
2010
1614
                            content is copied.
2014
1618
        Returns the copied revision count and the failed revisions in a tuple:
2015
1619
        (copied, failures).
2016
1620
        """
2017
 
        raise NotImplementedError(self.fetch)
2018
 
   
 
1621
        from bzrlib.fetch import GenericRepoFetcher
 
1622
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
 
1623
               self.source, self.source._format, self.target, self.target._format)
 
1624
        f = GenericRepoFetcher(to_repository=self.target,
 
1625
                               from_repository=self.source,
 
1626
                               last_revision=revision_id,
 
1627
                               pb=pb)
 
1628
        return f.count_copied, f.failed_revisions
 
1629
 
2019
1630
    @needs_read_lock
2020
1631
    def missing_revision_ids(self, revision_id=None):
2021
1632
        """Return the revision ids that source has that target does not.
2029
1640
        target_ids = set(self.target.all_revision_ids())
2030
1641
        if revision_id is not None:
2031
1642
            source_ids = self.source.get_ancestry(revision_id)
2032
 
            assert source_ids[0] is None
 
1643
            assert source_ids[0] == None
2033
1644
            source_ids.pop(0)
2034
1645
        else:
2035
1646
            source_ids = self.source.all_revision_ids()
2039
1650
        # that we've decided we need.
2040
1651
        return [rev_id for rev_id in source_ids if rev_id in result_set]
2041
1652
 
2042
 
    @staticmethod
2043
 
    def _same_model(source, target):
2044
 
        """True if source and target have the same data representation."""
2045
 
        if source.supports_rich_root() != target.supports_rich_root():
2046
 
            return False
2047
 
        if source._serializer != target._serializer:
2048
 
            return False
2049
 
        return True
2050
 
 
2051
 
 
2052
 
class InterSameDataRepository(InterRepository):
2053
 
    """Code for converting between repositories that represent the same data.
2054
 
    
2055
 
    Data format and model must match for this to work.
2056
 
    """
2057
 
 
2058
 
    @classmethod
2059
 
    def _get_repo_format_to_test(self):
2060
 
        """Repository format for testing with.
2061
 
        
2062
 
        InterSameData can pull from subtree to subtree and from non-subtree to
2063
 
        non-subtree, so we test this with the richest repository format.
2064
 
        """
2065
 
        from bzrlib.repofmt import knitrepo
2066
 
        return knitrepo.RepositoryFormatKnit3()
2067
 
 
2068
 
    @staticmethod
2069
 
    def is_compatible(source, target):
2070
 
        return InterRepository._same_model(source, target)
2071
 
 
2072
 
    @needs_write_lock
2073
 
    def copy_content(self, revision_id=None):
2074
 
        """Make a complete copy of the content in self into destination.
2075
 
 
2076
 
        This copies both the repository's revision data, and configuration information
2077
 
        such as the make_working_trees setting.
2078
 
        
2079
 
        This is a destructive operation! Do not use it on existing 
2080
 
        repositories.
2081
 
 
2082
 
        :param revision_id: Only copy the content needed to construct
2083
 
                            revision_id and its parents.
2084
 
        """
2085
 
        try:
2086
 
            self.target.set_make_working_trees(self.source.make_working_trees())
2087
 
        except NotImplementedError:
2088
 
            pass
2089
 
        # but don't bother fetching if we have the needed data now.
2090
 
        if (revision_id not in (None, _mod_revision.NULL_REVISION) and 
2091
 
            self.target.has_revision(revision_id)):
2092
 
            return
2093
 
        self.target.fetch(self.source, revision_id=revision_id)
2094
 
 
2095
 
    @needs_write_lock
2096
 
    def fetch(self, revision_id=None, pb=None):
2097
 
        """See InterRepository.fetch()."""
2098
 
        from bzrlib.fetch import GenericRepoFetcher
2099
 
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2100
 
               self.source, self.source._format, self.target,
2101
 
               self.target._format)
2102
 
        f = GenericRepoFetcher(to_repository=self.target,
2103
 
                               from_repository=self.source,
2104
 
                               last_revision=revision_id,
2105
 
                               pb=pb)
2106
 
        return f.count_copied, f.failed_revisions
2107
 
 
2108
 
 
2109
 
class InterWeaveRepo(InterSameDataRepository):
2110
 
    """Optimised code paths between Weave based repositories.
2111
 
    
2112
 
    This should be in bzrlib/repofmt/weaverepo.py but we have not yet
2113
 
    implemented lazy inter-object optimisation.
2114
 
    """
2115
 
 
2116
 
    @classmethod
2117
 
    def _get_repo_format_to_test(self):
2118
 
        from bzrlib.repofmt import weaverepo
2119
 
        return weaverepo.RepositoryFormat7()
 
1653
 
 
1654
class InterWeaveRepo(InterRepository):
 
1655
    """Optimised code paths between Weave based repositories."""
 
1656
 
 
1657
    _matching_repo_format = RepositoryFormat7()
 
1658
    """Repository format for testing with."""
2120
1659
 
2121
1660
    @staticmethod
2122
1661
    def is_compatible(source, target):
2126
1665
        could lead to confusing results, and there is no need to be 
2127
1666
        overly general.
2128
1667
        """
2129
 
        from bzrlib.repofmt.weaverepo import (
2130
 
                RepositoryFormat5,
2131
 
                RepositoryFormat6,
2132
 
                RepositoryFormat7,
2133
 
                )
2134
1668
        try:
2135
1669
            return (isinstance(source._format, (RepositoryFormat5,
2136
1670
                                                RepositoryFormat6,
2142
1676
            return False
2143
1677
    
2144
1678
    @needs_write_lock
2145
 
    def copy_content(self, revision_id=None):
 
1679
    def copy_content(self, revision_id=None, basis=None):
2146
1680
        """See InterRepository.copy_content()."""
2147
1681
        # weave specific optimised path:
2148
 
        try:
2149
 
            self.target.set_make_working_trees(self.source.make_working_trees())
2150
 
        except NotImplementedError:
2151
 
            pass
2152
 
        # FIXME do not peek!
2153
 
        if self.source.control_files._transport.listable():
2154
 
            pb = ui.ui_factory.nested_progress_bar()
 
1682
        if basis is not None:
 
1683
            # copy the basis in, then fetch remaining data.
 
1684
            basis.copy_content_into(self.target, revision_id)
 
1685
            # the basis copy_content_into could miss-set this.
2155
1686
            try:
2156
 
                self.target.weave_store.copy_all_ids(
2157
 
                    self.source.weave_store,
2158
 
                    pb=pb,
2159
 
                    from_transaction=self.source.get_transaction(),
2160
 
                    to_transaction=self.target.get_transaction())
2161
 
                pb.update('copying inventory', 0, 1)
2162
 
                self.target.control_weaves.copy_multi(
2163
 
                    self.source.control_weaves, ['inventory'],
2164
 
                    from_transaction=self.source.get_transaction(),
2165
 
                    to_transaction=self.target.get_transaction())
2166
 
                self.target._revision_store.text_store.copy_all_ids(
2167
 
                    self.source._revision_store.text_store,
2168
 
                    pb=pb)
2169
 
            finally:
2170
 
                pb.finished()
2171
 
        else:
 
1687
                self.target.set_make_working_trees(self.source.make_working_trees())
 
1688
            except NotImplementedError:
 
1689
                pass
2172
1690
            self.target.fetch(self.source, revision_id=revision_id)
 
1691
        else:
 
1692
            try:
 
1693
                self.target.set_make_working_trees(self.source.make_working_trees())
 
1694
            except NotImplementedError:
 
1695
                pass
 
1696
            # FIXME do not peek!
 
1697
            if self.source.control_files._transport.listable():
 
1698
                pb = ui.ui_factory.nested_progress_bar()
 
1699
                try:
 
1700
                    self.target.weave_store.copy_all_ids(
 
1701
                        self.source.weave_store,
 
1702
                        pb=pb,
 
1703
                        from_transaction=self.source.get_transaction(),
 
1704
                        to_transaction=self.target.get_transaction())
 
1705
                    pb.update('copying inventory', 0, 1)
 
1706
                    self.target.control_weaves.copy_multi(
 
1707
                        self.source.control_weaves, ['inventory'],
 
1708
                        from_transaction=self.source.get_transaction(),
 
1709
                        to_transaction=self.target.get_transaction())
 
1710
                    self.target._revision_store.text_store.copy_all_ids(
 
1711
                        self.source._revision_store.text_store,
 
1712
                        pb=pb)
 
1713
                finally:
 
1714
                    pb.finished()
 
1715
            else:
 
1716
                self.target.fetch(self.source, revision_id=revision_id)
2173
1717
 
2174
1718
    @needs_write_lock
2175
1719
    def fetch(self, revision_id=None, pb=None):
2199
1743
        # - RBC 20060209
2200
1744
        if revision_id is not None:
2201
1745
            source_ids = self.source.get_ancestry(revision_id)
2202
 
            assert source_ids[0] is None
 
1746
            assert source_ids[0] == None
2203
1747
            source_ids.pop(0)
2204
1748
        else:
2205
1749
            source_ids = self.source._all_possible_ids()
2225
1769
            return self.source._eliminate_revisions_not_present(required_topo_revisions)
2226
1770
 
2227
1771
 
2228
 
class InterKnitRepo(InterSameDataRepository):
 
1772
class InterKnitRepo(InterRepository):
2229
1773
    """Optimised code paths between Knit based repositories."""
2230
1774
 
2231
 
    @classmethod
2232
 
    def _get_repo_format_to_test(self):
2233
 
        from bzrlib.repofmt import knitrepo
2234
 
        return knitrepo.RepositoryFormatKnit1()
 
1775
    _matching_repo_format = RepositoryFormatKnit1()
 
1776
    """Repository format for testing with."""
2235
1777
 
2236
1778
    @staticmethod
2237
1779
    def is_compatible(source, target):
2241
1783
        could lead to confusing results, and there is no need to be 
2242
1784
        overly general.
2243
1785
        """
2244
 
        from bzrlib.repofmt.knitrepo import RepositoryFormatKnit
2245
1786
        try:
2246
 
            are_knits = (isinstance(source._format, RepositoryFormatKnit) and
2247
 
                isinstance(target._format, RepositoryFormatKnit))
 
1787
            return (isinstance(source._format, (RepositoryFormatKnit1)) and
 
1788
                    isinstance(target._format, (RepositoryFormatKnit1)))
2248
1789
        except AttributeError:
2249
1790
            return False
2250
 
        return are_knits and InterRepository._same_model(source, target)
2251
1791
 
2252
1792
    @needs_write_lock
2253
1793
    def fetch(self, revision_id=None, pb=None):
2266
1806
        """See InterRepository.missing_revision_ids()."""
2267
1807
        if revision_id is not None:
2268
1808
            source_ids = self.source.get_ancestry(revision_id)
2269
 
            assert source_ids[0] is None
 
1809
            assert source_ids[0] == None
2270
1810
            source_ids.pop(0)
2271
1811
        else:
2272
 
            source_ids = self.source.all_revision_ids()
 
1812
            source_ids = self.source._all_possible_ids()
2273
1813
        source_ids_set = set(source_ids)
2274
1814
        # source_ids is the worst possible case we may need to pull.
2275
1815
        # now we want to filter source_ids against what we actually
2276
1816
        # have in target, but don't try to check for existence where we know
2277
1817
        # we do not have a revision as that would be pointless.
2278
 
        target_ids = set(self.target.all_revision_ids())
 
1818
        target_ids = set(self.target._all_possible_ids())
2279
1819
        possibly_present_revisions = target_ids.intersection(source_ids_set)
2280
1820
        actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
2281
1821
        required_revisions = source_ids_set.difference(actually_present_revisions)
2291
1831
            # that against the revision records.
2292
1832
            return self.source._eliminate_revisions_not_present(required_topo_revisions)
2293
1833
 
2294
 
 
2295
 
class InterPackRepo(InterSameDataRepository):
2296
 
    """Optimised code paths between Pack based repositories."""
2297
 
 
2298
 
    @classmethod
2299
 
    def _get_repo_format_to_test(self):
2300
 
        from bzrlib.repofmt import pack_repo
2301
 
        return pack_repo.RepositoryFormatKnitPack1()
2302
 
 
2303
 
    @staticmethod
2304
 
    def is_compatible(source, target):
2305
 
        """Be compatible with known Pack formats.
2306
 
        
2307
 
        We don't test for the stores being of specific types because that
2308
 
        could lead to confusing results, and there is no need to be 
2309
 
        overly general.
2310
 
        """
2311
 
        from bzrlib.repofmt.pack_repo import RepositoryFormatPack
2312
 
        try:
2313
 
            are_packs = (isinstance(source._format, RepositoryFormatPack) and
2314
 
                isinstance(target._format, RepositoryFormatPack))
2315
 
        except AttributeError:
2316
 
            return False
2317
 
        return are_packs and InterRepository._same_model(source, target)
2318
 
 
2319
 
    @needs_write_lock
2320
 
    def fetch(self, revision_id=None, pb=None):
2321
 
        """See InterRepository.fetch()."""
2322
 
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2323
 
               self.source, self.source._format, self.target, self.target._format)
2324
 
        self.count_copied = 0
2325
 
        if revision_id is None:
2326
 
            # TODO:
2327
 
            # everything to do - use pack logic
2328
 
            # to fetch from all packs to one without
2329
 
            # inventory parsing etc, IFF nothing to be copied is in the target.
2330
 
            # till then:
2331
 
            revision_ids = self.source.all_revision_ids()
2332
 
            # implementing the TODO will involve:
2333
 
            # - detecting when all of a pack is selected
2334
 
            # - avoiding as much as possible pre-selection, so the
2335
 
            # more-core routines such as create_pack_from_packs can filter in
2336
 
            # a just-in-time fashion. (though having a HEADS list on a
2337
 
            # repository might make this a lot easier, because we could
2338
 
            # sensibly detect 'new revisions' without doing a full index scan.
2339
 
        elif _mod_revision.is_null(revision_id):
2340
 
            # nothing to do:
2341
 
            return
2342
 
        else:
2343
 
            try:
2344
 
                revision_ids = self.missing_revision_ids(revision_id)
2345
 
            except errors.NoSuchRevision:
2346
 
                raise errors.InstallFailed([revision_id])
2347
 
        packs = self.source._pack_collection.all_packs()
2348
 
        pack = self.target._pack_collection.create_pack_from_packs(
2349
 
            packs, '.fetch', revision_ids,
2350
 
            )
2351
 
        if pack is not None:
2352
 
            self.target._pack_collection._save_pack_names()
2353
 
            # Trigger an autopack. This may duplicate effort as we've just done
2354
 
            # a pack creation, but for now it is simpler to think about as
2355
 
            # 'upload data, then repack if needed'.
2356
 
            self.target._pack_collection.autopack()
2357
 
            return pack.get_revision_count()
2358
 
        else:
2359
 
            return 0
2360
 
 
2361
 
    @needs_read_lock
2362
 
    def missing_revision_ids(self, revision_id=None):
2363
 
        """See InterRepository.missing_revision_ids()."""
2364
 
        if revision_id is not None:
2365
 
            source_ids = self.source.get_ancestry(revision_id)
2366
 
            assert source_ids[0] is None
2367
 
            source_ids.pop(0)
2368
 
        else:
2369
 
            source_ids = self.source.all_revision_ids()
2370
 
        # source_ids is the worst possible case we may need to pull.
2371
 
        # now we want to filter source_ids against what we actually
2372
 
        # have in target, but don't try to check for existence where we know
2373
 
        # we do not have a revision as that would be pointless.
2374
 
        target_ids = set(self.target.all_revision_ids())
2375
 
        return [r for r in source_ids if (r not in target_ids)]
2376
 
 
2377
 
 
2378
 
class InterModel1and2(InterRepository):
2379
 
 
2380
 
    @classmethod
2381
 
    def _get_repo_format_to_test(self):
2382
 
        return None
2383
 
 
2384
 
    @staticmethod
2385
 
    def is_compatible(source, target):
2386
 
        if not source.supports_rich_root() and target.supports_rich_root():
2387
 
            return True
2388
 
        else:
2389
 
            return False
2390
 
 
2391
 
    @needs_write_lock
2392
 
    def fetch(self, revision_id=None, pb=None):
2393
 
        """See InterRepository.fetch()."""
2394
 
        from bzrlib.fetch import Model1toKnit2Fetcher
2395
 
        f = Model1toKnit2Fetcher(to_repository=self.target,
2396
 
                                 from_repository=self.source,
2397
 
                                 last_revision=revision_id,
2398
 
                                 pb=pb)
2399
 
        return f.count_copied, f.failed_revisions
2400
 
 
2401
 
    @needs_write_lock
2402
 
    def copy_content(self, revision_id=None):
2403
 
        """Make a complete copy of the content in self into destination.
2404
 
        
2405
 
        This is a destructive operation! Do not use it on existing 
2406
 
        repositories.
2407
 
 
2408
 
        :param revision_id: Only copy the content needed to construct
2409
 
                            revision_id and its parents.
2410
 
        """
2411
 
        try:
2412
 
            self.target.set_make_working_trees(self.source.make_working_trees())
2413
 
        except NotImplementedError:
2414
 
            pass
2415
 
        # but don't bother fetching if we have the needed data now.
2416
 
        if (revision_id not in (None, _mod_revision.NULL_REVISION) and 
2417
 
            self.target.has_revision(revision_id)):
2418
 
            return
2419
 
        self.target.fetch(self.source, revision_id=revision_id)
2420
 
 
2421
 
 
2422
 
class InterKnit1and2(InterKnitRepo):
2423
 
 
2424
 
    @classmethod
2425
 
    def _get_repo_format_to_test(self):
2426
 
        return None
2427
 
 
2428
 
    @staticmethod
2429
 
    def is_compatible(source, target):
2430
 
        """Be compatible with Knit1 source and Knit3 target"""
2431
 
        from bzrlib.repofmt.knitrepo import RepositoryFormatKnit3
2432
 
        try:
2433
 
            from bzrlib.repofmt.knitrepo import (RepositoryFormatKnit1,
2434
 
                RepositoryFormatKnit3)
2435
 
            from bzrlib.repofmt.pack_repo import (RepositoryFormatKnitPack1,
2436
 
                RepositoryFormatKnitPack3)
2437
 
            return (isinstance(source._format,
2438
 
                    (RepositoryFormatKnit1, RepositoryFormatKnitPack1)) and
2439
 
                isinstance(target._format,
2440
 
                    (RepositoryFormatKnit3, RepositoryFormatKnitPack3))
2441
 
                )
2442
 
        except AttributeError:
2443
 
            return False
2444
 
 
2445
 
    @needs_write_lock
2446
 
    def fetch(self, revision_id=None, pb=None):
2447
 
        """See InterRepository.fetch()."""
2448
 
        from bzrlib.fetch import Knit1to2Fetcher
2449
 
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2450
 
               self.source, self.source._format, self.target, 
2451
 
               self.target._format)
2452
 
        f = Knit1to2Fetcher(to_repository=self.target,
2453
 
                            from_repository=self.source,
2454
 
                            last_revision=revision_id,
2455
 
                            pb=pb)
2456
 
        return f.count_copied, f.failed_revisions
2457
 
 
2458
 
 
2459
 
class InterRemoteToOther(InterRepository):
2460
 
 
2461
 
    def __init__(self, source, target):
2462
 
        InterRepository.__init__(self, source, target)
2463
 
        self._real_inter = None
2464
 
 
2465
 
    @staticmethod
2466
 
    def is_compatible(source, target):
2467
 
        if not isinstance(source, remote.RemoteRepository):
2468
 
            return False
2469
 
        source._ensure_real()
2470
 
        real_source = source._real_repository
2471
 
        # Is source's model compatible with target's model, and are they the
2472
 
        # same format?  Currently we can only optimise fetching from an
2473
 
        # identical model & format repo.
2474
 
        assert not isinstance(real_source, remote.RemoteRepository), (
2475
 
            "We don't support remote repos backed by remote repos yet.")
2476
 
        return real_source._format == target._format
2477
 
 
2478
 
    @needs_write_lock
2479
 
    def fetch(self, revision_id=None, pb=None):
2480
 
        """See InterRepository.fetch()."""
2481
 
        from bzrlib.fetch import RemoteToOtherFetcher
2482
 
        mutter("Using fetch logic to copy between %s(remote) and %s(%s)",
2483
 
               self.source, self.target, self.target._format)
2484
 
        # TODO: jam 20070210 This should be an assert, not a translate
2485
 
        revision_id = osutils.safe_revision_id(revision_id)
2486
 
        f = RemoteToOtherFetcher(to_repository=self.target,
2487
 
                                 from_repository=self.source,
2488
 
                                 last_revision=revision_id,
2489
 
                                 pb=pb)
2490
 
        return f.count_copied, f.failed_revisions
2491
 
 
2492
 
    @classmethod
2493
 
    def _get_repo_format_to_test(self):
2494
 
        return None
2495
 
 
2496
 
 
2497
 
class InterOtherToRemote(InterRepository):
2498
 
 
2499
 
    def __init__(self, source, target):
2500
 
        InterRepository.__init__(self, source, target)
2501
 
        self._real_inter = None
2502
 
 
2503
 
    @staticmethod
2504
 
    def is_compatible(source, target):
2505
 
        if isinstance(target, remote.RemoteRepository):
2506
 
            return True
2507
 
        return False
2508
 
 
2509
 
    def _ensure_real_inter(self):
2510
 
        if self._real_inter is None:
2511
 
            self.target._ensure_real()
2512
 
            real_target = self.target._real_repository
2513
 
            self._real_inter = InterRepository.get(self.source, real_target)
2514
 
    
2515
 
    def copy_content(self, revision_id=None):
2516
 
        self._ensure_real_inter()
2517
 
        self._real_inter.copy_content(revision_id=revision_id)
2518
 
 
2519
 
    def fetch(self, revision_id=None, pb=None):
2520
 
        self._ensure_real_inter()
2521
 
        self._real_inter.fetch(revision_id=revision_id, pb=pb)
2522
 
 
2523
 
    @classmethod
2524
 
    def _get_repo_format_to_test(self):
2525
 
        return None
2526
 
 
2527
 
 
2528
 
InterRepository.register_optimiser(InterSameDataRepository)
2529
1834
InterRepository.register_optimiser(InterWeaveRepo)
2530
1835
InterRepository.register_optimiser(InterKnitRepo)
2531
 
InterRepository.register_optimiser(InterModel1and2)
2532
 
InterRepository.register_optimiser(InterKnit1and2)
2533
 
InterRepository.register_optimiser(InterPackRepo)
2534
 
InterRepository.register_optimiser(InterRemoteToOther)
2535
 
InterRepository.register_optimiser(InterOtherToRemote)
 
1836
 
 
1837
 
 
1838
class RepositoryTestProviderAdapter(object):
 
1839
    """A tool to generate a suite testing multiple repository formats at once.
 
1840
 
 
1841
    This is done by copying the test once for each transport and injecting
 
1842
    the transport_server, transport_readonly_server, and bzrdir_format and
 
1843
    repository_format classes into each copy. Each copy is also given a new id()
 
1844
    to make it easy to identify.
 
1845
    """
 
1846
 
 
1847
    def __init__(self, transport_server, transport_readonly_server, formats):
 
1848
        self._transport_server = transport_server
 
1849
        self._transport_readonly_server = transport_readonly_server
 
1850
        self._formats = formats
 
1851
    
 
1852
    def adapt(self, test):
 
1853
        result = TestSuite()
 
1854
        for repository_format, bzrdir_format in self._formats:
 
1855
            new_test = deepcopy(test)
 
1856
            new_test.transport_server = self._transport_server
 
1857
            new_test.transport_readonly_server = self._transport_readonly_server
 
1858
            new_test.bzrdir_format = bzrdir_format
 
1859
            new_test.repository_format = repository_format
 
1860
            def make_new_test_id():
 
1861
                new_id = "%s(%s)" % (new_test.id(), repository_format.__class__.__name__)
 
1862
                return lambda: new_id
 
1863
            new_test.id = make_new_test_id()
 
1864
            result.addTest(new_test)
 
1865
        return result
 
1866
 
 
1867
 
 
1868
class InterRepositoryTestProviderAdapter(object):
 
1869
    """A tool to generate a suite testing multiple inter repository formats.
 
1870
 
 
1871
    This is done by copying the test once for each interrepo provider and injecting
 
1872
    the transport_server, transport_readonly_server, repository_format and 
 
1873
    repository_to_format classes into each copy.
 
1874
    Each copy is also given a new id() to make it easy to identify.
 
1875
    """
 
1876
 
 
1877
    def __init__(self, transport_server, transport_readonly_server, formats):
 
1878
        self._transport_server = transport_server
 
1879
        self._transport_readonly_server = transport_readonly_server
 
1880
        self._formats = formats
 
1881
    
 
1882
    def adapt(self, test):
 
1883
        result = TestSuite()
 
1884
        for interrepo_class, repository_format, repository_format_to in self._formats:
 
1885
            new_test = deepcopy(test)
 
1886
            new_test.transport_server = self._transport_server
 
1887
            new_test.transport_readonly_server = self._transport_readonly_server
 
1888
            new_test.interrepo_class = interrepo_class
 
1889
            new_test.repository_format = repository_format
 
1890
            new_test.repository_format_to = repository_format_to
 
1891
            def make_new_test_id():
 
1892
                new_id = "%s(%s)" % (new_test.id(), interrepo_class.__name__)
 
1893
                return lambda: new_id
 
1894
            new_test.id = make_new_test_id()
 
1895
            result.addTest(new_test)
 
1896
        return result
 
1897
 
 
1898
    @staticmethod
 
1899
    def default_test_list():
 
1900
        """Generate the default list of interrepo permutations to test."""
 
1901
        result = []
 
1902
        # test the default InterRepository between format 6 and the current 
 
1903
        # default format.
 
1904
        # XXX: robertc 20060220 reinstate this when there are two supported
 
1905
        # formats which do not have an optimal code path between them.
 
1906
        result.append((InterRepository,
 
1907
                       RepositoryFormat6(),
 
1908
                       RepositoryFormatKnit1()))
 
1909
        for optimiser in InterRepository._optimisers:
 
1910
            result.append((optimiser,
 
1911
                           optimiser._matching_repo_format,
 
1912
                           optimiser._matching_repo_format
 
1913
                           ))
 
1914
        # if there are specific combinations we want to use, we can add them 
 
1915
        # here.
 
1916
        return result
2536
1917
 
2537
1918
 
2538
1919
class CopyConverter(object):
2564
1945
        self.step('Moving repository to repository.backup')
2565
1946
        self.repo_dir.transport.move('repository', 'repository.backup')
2566
1947
        backup_transport =  self.repo_dir.transport.clone('repository.backup')
2567
 
        repo._format.check_conversion_target(self.target_format)
2568
1948
        self.source_repo = repo._format.open(self.repo_dir,
2569
1949
            _found=True,
2570
1950
            _override_transport=backup_transport)
2587
1967
        self.pb.update(message, self.count, self.total)
2588
1968
 
2589
1969
 
 
1970
class CommitBuilder(object):
 
1971
    """Provides an interface to build up a commit.
 
1972
 
 
1973
    This allows describing a tree to be committed without needing to 
 
1974
    know the internals of the format of the repository.
 
1975
    """
 
1976
    
 
1977
    record_root_entry = False
 
1978
    def __init__(self, repository, parents, config, timestamp=None, 
 
1979
                 timezone=None, committer=None, revprops=None, 
 
1980
                 revision_id=None):
 
1981
        """Initiate a CommitBuilder.
 
1982
 
 
1983
        :param repository: Repository to commit to.
 
1984
        :param parents: Revision ids of the parents of the new revision.
 
1985
        :param config: Configuration to use.
 
1986
        :param timestamp: Optional timestamp recorded for commit.
 
1987
        :param timezone: Optional timezone for timestamp.
 
1988
        :param committer: Optional committer to set for commit.
 
1989
        :param revprops: Optional dictionary of revision properties.
 
1990
        :param revision_id: Optional revision id.
 
1991
        """
 
1992
        self._config = config
 
1993
 
 
1994
        if committer is None:
 
1995
            self._committer = self._config.username()
 
1996
        else:
 
1997
            assert isinstance(committer, basestring), type(committer)
 
1998
            self._committer = committer
 
1999
 
 
2000
        self.new_inventory = Inventory(None)
 
2001
        self._new_revision_id = revision_id
 
2002
        self.parents = parents
 
2003
        self.repository = repository
 
2004
 
 
2005
        self._revprops = {}
 
2006
        if revprops is not None:
 
2007
            self._revprops.update(revprops)
 
2008
 
 
2009
        if timestamp is None:
 
2010
            timestamp = time.time()
 
2011
        # Restrict resolution to 1ms
 
2012
        self._timestamp = round(timestamp, 3)
 
2013
 
 
2014
        if timezone is None:
 
2015
            self._timezone = local_time_offset()
 
2016
        else:
 
2017
            self._timezone = int(timezone)
 
2018
 
 
2019
        self._generate_revision_if_needed()
 
2020
 
 
2021
    def commit(self, message):
 
2022
        """Make the actual commit.
 
2023
 
 
2024
        :return: The revision id of the recorded revision.
 
2025
        """
 
2026
        rev = Revision(timestamp=self._timestamp,
 
2027
                       timezone=self._timezone,
 
2028
                       committer=self._committer,
 
2029
                       message=message,
 
2030
                       inventory_sha1=self.inv_sha1,
 
2031
                       revision_id=self._new_revision_id,
 
2032
                       properties=self._revprops)
 
2033
        rev.parent_ids = self.parents
 
2034
        self.repository.add_revision(self._new_revision_id, rev, 
 
2035
            self.new_inventory, self._config)
 
2036
        return self._new_revision_id
 
2037
 
 
2038
    def finish_inventory(self):
 
2039
        """Tell the builder that the inventory is finished."""
 
2040
        if self.new_inventory.root is None:
 
2041
            symbol_versioning.warn('Root entry should be supplied to'
 
2042
                ' record_entry_contents, as of bzr 0.10.',
 
2043
                 DeprecationWarning, stacklevel=2)
 
2044
            self.new_inventory.add(InventoryDirectory(ROOT_ID, '', None))
 
2045
        self.new_inventory.revision_id = self._new_revision_id
 
2046
        self.inv_sha1 = self.repository.add_inventory(
 
2047
            self._new_revision_id,
 
2048
            self.new_inventory,
 
2049
            self.parents
 
2050
            )
 
2051
 
 
2052
    def _gen_revision_id(self):
 
2053
        """Return new revision-id."""
 
2054
        s = '%s-%s-' % (self._config.user_email(), 
 
2055
                        compact_date(self._timestamp))
 
2056
        s += hexlify(rand_bytes(8))
 
2057
        return s
 
2058
 
 
2059
    def _generate_revision_if_needed(self):
 
2060
        """Create a revision id if None was supplied.
 
2061
        
 
2062
        If the repository can not support user-specified revision ids
 
2063
        they should override this function and raise UnsupportedOperation
 
2064
        if _new_revision_id is not None.
 
2065
 
 
2066
        :raises: UnsupportedOperation
 
2067
        """
 
2068
        if self._new_revision_id is None:
 
2069
            self._new_revision_id = self._gen_revision_id()
 
2070
 
 
2071
    def record_entry_contents(self, ie, parent_invs, path, tree):
 
2072
        """Record the content of ie from tree into the commit if needed.
 
2073
 
 
2074
        Side effect: sets ie.revision when unchanged
 
2075
 
 
2076
        :param ie: An inventory entry present in the commit.
 
2077
        :param parent_invs: The inventories of the parent revisions of the
 
2078
            commit.
 
2079
        :param path: The path the entry is at in the tree.
 
2080
        :param tree: The tree which contains this entry and should be used to 
 
2081
        obtain content.
 
2082
        """
 
2083
        if self.new_inventory.root is None and ie.parent_id is not None:
 
2084
            symbol_versioning.warn('Root entry should be supplied to'
 
2085
                ' record_entry_contents, as of bzr 0.10.',
 
2086
                 DeprecationWarning, stacklevel=2)
 
2087
            self.record_entry_contents(tree.inventory.root.copy(), parent_invs,
 
2088
                                       '', tree)
 
2089
        self.new_inventory.add(ie)
 
2090
 
 
2091
        # ie.revision is always None if the InventoryEntry is considered
 
2092
        # for committing. ie.snapshot will record the correct revision 
 
2093
        # which may be the sole parent if it is untouched.
 
2094
        if ie.revision is not None:
 
2095
            return
 
2096
 
 
2097
        # In this revision format, root entries have no knit or weave
 
2098
        if ie is self.new_inventory.root:
 
2099
            if len(parent_invs):
 
2100
                ie.revision = parent_invs[0].root.revision
 
2101
            else:
 
2102
                ie.revision = None
 
2103
            return
 
2104
        previous_entries = ie.find_previous_heads(
 
2105
            parent_invs,
 
2106
            self.repository.weave_store,
 
2107
            self.repository.get_transaction())
 
2108
        # we are creating a new revision for ie in the history store
 
2109
        # and inventory.
 
2110
        ie.snapshot(self._new_revision_id, path, previous_entries, tree, self)
 
2111
 
 
2112
    def modified_directory(self, file_id, file_parents):
 
2113
        """Record the presence of a symbolic link.
 
2114
 
 
2115
        :param file_id: The file_id of the link to record.
 
2116
        :param file_parents: The per-file parent revision ids.
 
2117
        """
 
2118
        self._add_text_to_weave(file_id, [], file_parents.keys())
 
2119
    
 
2120
    def modified_file_text(self, file_id, file_parents,
 
2121
                           get_content_byte_lines, text_sha1=None,
 
2122
                           text_size=None):
 
2123
        """Record the text of file file_id
 
2124
 
 
2125
        :param file_id: The file_id of the file to record the text of.
 
2126
        :param file_parents: The per-file parent revision ids.
 
2127
        :param get_content_byte_lines: A callable which will return the byte
 
2128
            lines for the file.
 
2129
        :param text_sha1: Optional SHA1 of the file contents.
 
2130
        :param text_size: Optional size of the file contents.
 
2131
        """
 
2132
        # mutter('storing text of file {%s} in revision {%s} into %r',
 
2133
        #        file_id, self._new_revision_id, self.repository.weave_store)
 
2134
        # special case to avoid diffing on renames or 
 
2135
        # reparenting
 
2136
        if (len(file_parents) == 1
 
2137
            and text_sha1 == file_parents.values()[0].text_sha1
 
2138
            and text_size == file_parents.values()[0].text_size):
 
2139
            previous_ie = file_parents.values()[0]
 
2140
            versionedfile = self.repository.weave_store.get_weave(file_id, 
 
2141
                self.repository.get_transaction())
 
2142
            versionedfile.clone_text(self._new_revision_id, 
 
2143
                previous_ie.revision, file_parents.keys())
 
2144
            return text_sha1, text_size
 
2145
        else:
 
2146
            new_lines = get_content_byte_lines()
 
2147
            # TODO: Rather than invoking sha_strings here, _add_text_to_weave
 
2148
            # should return the SHA1 and size
 
2149
            self._add_text_to_weave(file_id, new_lines, file_parents.keys())
 
2150
            return osutils.sha_strings(new_lines), \
 
2151
                sum(map(len, new_lines))
 
2152
 
 
2153
    def modified_link(self, file_id, file_parents, link_target):
 
2154
        """Record the presence of a symbolic link.
 
2155
 
 
2156
        :param file_id: The file_id of the link to record.
 
2157
        :param file_parents: The per-file parent revision ids.
 
2158
        :param link_target: Target location of this link.
 
2159
        """
 
2160
        self._add_text_to_weave(file_id, [], file_parents.keys())
 
2161
 
 
2162
    def _add_text_to_weave(self, file_id, new_lines, parents):
 
2163
        versionedfile = self.repository.weave_store.get_weave_or_empty(
 
2164
            file_id, self.repository.get_transaction())
 
2165
        versionedfile.add_lines(self._new_revision_id, parents, new_lines)
 
2166
        versionedfile.clear_cache()
 
2167
 
 
2168
 
 
2169
class _CommitBuilder(CommitBuilder):
 
2170
    """Temporary class so old CommitBuilders are detected properly
 
2171
    
 
2172
    Note: CommitBuilder works whether or not root entry is recorded.
 
2173
    """
 
2174
 
 
2175
    record_root_entry = True
 
2176
 
 
2177
 
2590
2178
_unescape_map = {
2591
2179
    'apos':"'",
2592
2180
    'quot':'"',
2597
2185
 
2598
2186
 
2599
2187
def _unescaper(match, _map=_unescape_map):
2600
 
    code = match.group(1)
2601
 
    try:
2602
 
        return _map[code]
2603
 
    except KeyError:
2604
 
        if not code.startswith('#'):
2605
 
            raise
2606
 
        return unichr(int(code[1:])).encode('utf8')
 
2188
    return _map[match.group(1)]
2607
2189
 
2608
2190
 
2609
2191
_unescape_re = None
2613
2195
    """Unescape predefined XML entities in a string of data."""
2614
2196
    global _unescape_re
2615
2197
    if _unescape_re is None:
2616
 
        _unescape_re = re.compile('\&([^;]*);')
 
2198
        _unescape_re = re.compile('\&([^;]*);')
2617
2199
    return _unescape_re.sub(_unescaper, data)
2618
 
 
2619
 
 
2620
 
class _RevisionTextVersionCache(object):
2621
 
    """A cache of the versionedfile versions for revision and file-id."""
2622
 
 
2623
 
    def __init__(self, repository):
2624
 
        self.repository = repository
2625
 
        self.revision_versions = {}
2626
 
        self.revision_parents = {}
2627
 
        self.repo_graph = self.repository.get_graph()
2628
 
        # XXX: RBC: I haven't tracked down what uses this, but it would be
2629
 
        # better to use the headscache directly I think.
2630
 
        self.heads = graph.HeadsCache(self.repo_graph).heads
2631
 
 
2632
 
    def add_revision_text_versions(self, tree):
2633
 
        """Cache text version data from the supplied revision tree"""
2634
 
        inv_revisions = {}
2635
 
        for path, entry in tree.iter_entries_by_dir():
2636
 
            inv_revisions[entry.file_id] = entry.revision
2637
 
        self.revision_versions[tree.get_revision_id()] = inv_revisions
2638
 
        return inv_revisions
2639
 
 
2640
 
    def get_text_version(self, file_id, revision_id):
2641
 
        """Determine the text version for a given file-id and revision-id"""
2642
 
        try:
2643
 
            inv_revisions = self.revision_versions[revision_id]
2644
 
        except KeyError:
2645
 
            try:
2646
 
                tree = self.repository.revision_tree(revision_id)
2647
 
            except errors.RevisionNotPresent:
2648
 
                self.revision_versions[revision_id] = inv_revisions = {}
2649
 
            else:
2650
 
                inv_revisions = self.add_revision_text_versions(tree)
2651
 
        return inv_revisions.get(file_id)
2652
 
 
2653
 
    def prepopulate_revs(self, revision_ids):
2654
 
        # Filter out versions that we don't have an inventory for, so that the
2655
 
        # revision_trees() call won't fail.
2656
 
        inv_weave = self.repository.get_inventory_weave()
2657
 
        revs = [r for r in revision_ids if inv_weave.has_version(r)]
2658
 
        # XXX: this loop is very similar to
2659
 
        # bzrlib.fetch.Inter1and2Helper.iter_rev_trees.
2660
 
        while revs:
2661
 
            for tree in self.repository.revision_trees(revs[:100]):
2662
 
                if tree.inventory.revision_id is None:
2663
 
                    tree.inventory.revision_id = tree.get_revision_id()
2664
 
                self.add_revision_text_versions(tree)
2665
 
            revs = revs[100:]
2666
 
 
2667
 
    def get_parents(self, revision_id):
2668
 
        try:
2669
 
            return self.revision_parents[revision_id]
2670
 
        except KeyError:
2671
 
            parents = self.repository.get_parents([revision_id])[0]
2672
 
            self.revision_parents[revision_id] = parents
2673
 
            return parents
2674
 
 
2675
 
 
2676
 
class VersionedFileChecker(object):
2677
 
 
2678
 
    def __init__(self, planned_revisions, revision_versions, repository):
2679
 
        self.planned_revisions = planned_revisions
2680
 
        self.revision_versions = revision_versions
2681
 
        self.repository = repository
2682
 
    
2683
 
    def calculate_file_version_parents(self, revision_id, file_id):
2684
 
        text_revision = self.revision_versions.get_text_version(
2685
 
            file_id, revision_id)
2686
 
        if text_revision is None:
2687
 
            return None
2688
 
        parents_of_text_revision = self.revision_versions.get_parents(
2689
 
            text_revision)
2690
 
        parents_from_inventories = []
2691
 
        for parent in parents_of_text_revision:
2692
 
            if parent == _mod_revision.NULL_REVISION:
2693
 
                continue
2694
 
            introduced_in = self.revision_versions.get_text_version(file_id,
2695
 
                    parent)
2696
 
            if introduced_in is not None:
2697
 
                parents_from_inventories.append(introduced_in)
2698
 
        heads = set(self.revision_versions.heads(parents_from_inventories))
2699
 
        new_parents = []
2700
 
        for parent in parents_from_inventories:
2701
 
            if parent in heads and parent not in new_parents:
2702
 
                new_parents.append(parent)
2703
 
        return tuple(new_parents)
2704
 
 
2705
 
    def check_file_version_parents(self, weave, file_id):
2706
 
        result = {}
2707
 
        for num, revision_id in enumerate(self.planned_revisions):
2708
 
            correct_parents = self.calculate_file_version_parents(
2709
 
                revision_id, file_id)
2710
 
            if correct_parents is None:
2711
 
                continue
2712
 
            text_revision = self.revision_versions.get_text_version(
2713
 
                file_id, revision_id)
2714
 
            knit_parents = tuple(weave.get_parents(text_revision))
2715
 
            if correct_parents != knit_parents:
2716
 
                result[revision_id] = (knit_parents, correct_parents)
2717
 
        return result