/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/repository.py

Hide the .basis_delta variable, and require callers to use .get_basis_delta()
This allows us to check that the callers were sure they would be
generating a proper delta, by using CommitBuilder.record_delete() correctly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
from bzrlib.lazy_import import lazy_import
 
18
lazy_import(globals(), """
 
19
import cStringIO
 
20
import re
 
21
import time
 
22
 
 
23
from bzrlib import (
 
24
    bzrdir,
 
25
    check,
 
26
    debug,
 
27
    errors,
 
28
    generate_ids,
 
29
    gpg,
 
30
    graph,
 
31
    lazy_regex,
 
32
    lockable_files,
 
33
    lockdir,
 
34
    lru_cache,
 
35
    osutils,
 
36
    remote,
 
37
    revision as _mod_revision,
 
38
    symbol_versioning,
 
39
    tsort,
 
40
    ui,
 
41
    versionedfile,
 
42
    )
 
43
from bzrlib.bundle import serializer
 
44
from bzrlib.revisiontree import RevisionTree
 
45
from bzrlib.store.versioned import VersionedFileStore
 
46
from bzrlib.testament import Testament
 
47
""")
 
48
 
 
49
from bzrlib import registry
 
50
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
51
from bzrlib.inter import InterObject
 
52
from bzrlib.inventory import Inventory, InventoryDirectory, ROOT_ID
 
53
from bzrlib.symbol_versioning import (
 
54
        deprecated_method,
 
55
        one_one,
 
56
        one_two,
 
57
        one_six,
 
58
        )
 
59
from bzrlib.trace import (
 
60
    log_exception_quietly, note, mutter, mutter_callsite, warning)
 
61
 
 
62
 
 
63
# Old formats display a warning, but only once
 
64
_deprecation_warning_done = False
 
65
 
 
66
 
 
67
class CommitBuilder(object):
 
68
    """Provides an interface to build up a commit.
 
69
 
 
70
    This allows describing a tree to be committed without needing to
 
71
    know the internals of the format of the repository.
 
72
    """
 
73
 
 
74
    # all clients should supply tree roots.
 
75
    record_root_entry = True
 
76
    # the default CommitBuilder does not manage trees whose root is versioned.
 
77
    _versioned_root = False
 
78
 
 
79
    def __init__(self, repository, parents, config, timestamp=None,
 
80
                 timezone=None, committer=None, revprops=None,
 
81
                 revision_id=None):
 
82
        """Initiate a CommitBuilder.
 
83
 
 
84
        :param repository: Repository to commit to.
 
85
        :param parents: Revision ids of the parents of the new revision.
 
86
        :param config: Configuration to use.
 
87
        :param timestamp: Optional timestamp recorded for commit.
 
88
        :param timezone: Optional timezone for timestamp.
 
89
        :param committer: Optional committer to set for commit.
 
90
        :param revprops: Optional dictionary of revision properties.
 
91
        :param revision_id: Optional revision id.
 
92
        """
 
93
        self._config = config
 
94
 
 
95
        if committer is None:
 
96
            self._committer = self._config.username()
 
97
        else:
 
98
            self._committer = committer
 
99
 
 
100
        self.new_inventory = Inventory(None)
 
101
        self._new_revision_id = revision_id
 
102
        self.parents = parents
 
103
        self.repository = repository
 
104
 
 
105
        self._revprops = {}
 
106
        if revprops is not None:
 
107
            self._validate_revprops(revprops)
 
108
            self._revprops.update(revprops)
 
109
 
 
110
        if timestamp is None:
 
111
            timestamp = time.time()
 
112
        # Restrict resolution to 1ms
 
113
        self._timestamp = round(timestamp, 3)
 
114
 
 
115
        if timezone is None:
 
116
            self._timezone = osutils.local_time_offset()
 
117
        else:
 
118
            self._timezone = int(timezone)
 
119
 
 
120
        self._generate_revision_if_needed()
 
121
        self.__heads = graph.HeadsCache(repository.get_graph()).heads
 
122
        self._basis_delta = []
 
123
        # API compatibility, older code that used CommitBuilder did not call
 
124
        # .record_delete(), which means the delta that is computed would not be
 
125
        # valid. Callers that will call record_delete() should call
 
126
        # .will_record_deletes() to indicate that.
 
127
        self._recording_deletes = False
 
128
 
 
129
    def _validate_unicode_text(self, text, context):
 
130
        """Verify things like commit messages don't have bogus characters."""
 
131
        if '\r' in text:
 
132
            raise ValueError('Invalid value for %s: %r' % (context, text))
 
133
 
 
134
    def _validate_revprops(self, revprops):
 
135
        for key, value in revprops.iteritems():
 
136
            # We know that the XML serializers do not round trip '\r'
 
137
            # correctly, so refuse to accept them
 
138
            if not isinstance(value, basestring):
 
139
                raise ValueError('revision property (%s) is not a valid'
 
140
                                 ' (unicode) string: %r' % (key, value))
 
141
            self._validate_unicode_text(value,
 
142
                                        'revision property (%s)' % (key,))
 
143
 
 
144
    def commit(self, message):
 
145
        """Make the actual commit.
 
146
 
 
147
        :return: The revision id of the recorded revision.
 
148
        """
 
149
        self._validate_unicode_text(message, 'commit message')
 
150
        rev = _mod_revision.Revision(
 
151
                       timestamp=self._timestamp,
 
152
                       timezone=self._timezone,
 
153
                       committer=self._committer,
 
154
                       message=message,
 
155
                       inventory_sha1=self.inv_sha1,
 
156
                       revision_id=self._new_revision_id,
 
157
                       properties=self._revprops)
 
158
        rev.parent_ids = self.parents
 
159
        self.repository.add_revision(self._new_revision_id, rev,
 
160
            self.new_inventory, self._config)
 
161
        self.repository.commit_write_group()
 
162
        return self._new_revision_id
 
163
 
 
164
    def abort(self):
 
165
        """Abort the commit that is being built.
 
166
        """
 
167
        self.repository.abort_write_group()
 
168
 
 
169
    def revision_tree(self):
 
170
        """Return the tree that was just committed.
 
171
 
 
172
        After calling commit() this can be called to get a RevisionTree
 
173
        representing the newly committed tree. This is preferred to
 
174
        calling Repository.revision_tree() because that may require
 
175
        deserializing the inventory, while we already have a copy in
 
176
        memory.
 
177
        """
 
178
        return RevisionTree(self.repository, self.new_inventory,
 
179
                            self._new_revision_id)
 
180
 
 
181
    def finish_inventory(self):
 
182
        """Tell the builder that the inventory is finished."""
 
183
        if self.new_inventory.root is None:
 
184
            raise AssertionError('Root entry should be supplied to'
 
185
                ' record_entry_contents, as of bzr 0.10.')
 
186
            self.new_inventory.add(InventoryDirectory(ROOT_ID, '', None))
 
187
        self.new_inventory.revision_id = self._new_revision_id
 
188
        self.inv_sha1 = self.repository.add_inventory(
 
189
            self._new_revision_id,
 
190
            self.new_inventory,
 
191
            self.parents
 
192
            )
 
193
 
 
194
    def _gen_revision_id(self):
 
195
        """Return new revision-id."""
 
196
        return generate_ids.gen_revision_id(self._config.username(),
 
197
                                            self._timestamp)
 
198
 
 
199
    def _generate_revision_if_needed(self):
 
200
        """Create a revision id if None was supplied.
 
201
        
 
202
        If the repository can not support user-specified revision ids
 
203
        they should override this function and raise CannotSetRevisionId
 
204
        if _new_revision_id is not None.
 
205
 
 
206
        :raises: CannotSetRevisionId
 
207
        """
 
208
        if self._new_revision_id is None:
 
209
            self._new_revision_id = self._gen_revision_id()
 
210
            self.random_revid = True
 
211
        else:
 
212
            self.random_revid = False
 
213
 
 
214
    def _heads(self, file_id, revision_ids):
 
215
        """Calculate the graph heads for revision_ids in the graph of file_id.
 
216
 
 
217
        This can use either a per-file graph or a global revision graph as we
 
218
        have an identity relationship between the two graphs.
 
219
        """
 
220
        return self.__heads(revision_ids)
 
221
 
 
222
    def _check_root(self, ie, parent_invs, tree):
 
223
        """Helper for record_entry_contents.
 
224
 
 
225
        :param ie: An entry being added.
 
226
        :param parent_invs: The inventories of the parent revisions of the
 
227
            commit.
 
228
        :param tree: The tree that is being committed.
 
229
        """
 
230
        # In this revision format, root entries have no knit or weave When
 
231
        # serializing out to disk and back in root.revision is always
 
232
        # _new_revision_id
 
233
        ie.revision = self._new_revision_id
 
234
 
 
235
    def _get_delta(self, ie, basis_inv, path):
 
236
        """Get a delta against the basis inventory for ie."""
 
237
        if ie.file_id not in basis_inv:
 
238
            # add
 
239
            result = (None, path, ie.file_id, ie)
 
240
            self._basis_delta.append(result)
 
241
            return result
 
242
        elif ie != basis_inv[ie.file_id]:
 
243
            # common but altered
 
244
            # TODO: avoid tis id2path call.
 
245
            result = (basis_inv.id2path(ie.file_id), path, ie.file_id, ie)
 
246
            self._basis_delta.append(result)
 
247
            return result
 
248
        else:
 
249
            # common, unaltered
 
250
            return None
 
251
 
 
252
    def get_basis_delta(self):
 
253
        """Return the complete inventory delta versus the basis inventory.
 
254
 
 
255
        This has been built up with the calls to record_delete and
 
256
        record_entry_contents. The client must have already called
 
257
        will_record_deletes() to indicate that they will be generating a
 
258
        complete delta.
 
259
 
 
260
        :return: An inventory delta, suitable for use with apply_delta, or
 
261
            Repository.add_inventory_by_delta, etc.
 
262
        """
 
263
        if not self._recording_deletes:
 
264
            raise AssertionError("recording deletes not activated.")
 
265
        return self._basis_delta
 
266
 
 
267
    def record_delete(self, path, file_id):
 
268
        """Record that a delete occured against a basis tree.
 
269
 
 
270
        This is an optional API - when used it adds items to the basis_delta
 
271
        being accumulated by the commit builder. It cannot be called unless the
 
272
        method will_record_deletes() has been called to inform the builder that
 
273
        a delta is being supplied.
 
274
 
 
275
        :param path: The path of the thing deleted.
 
276
        :param file_id: The file id that was deleted.
 
277
        """
 
278
        if not self._recording_deletes:
 
279
            raise AssertionError("recording deletes not activated.")
 
280
        self._basis_delta.append((path, None, file_id, None))
 
281
 
 
282
    def will_record_deletes(self):
 
283
        """Tell the commit builder that deletes are being notified.
 
284
 
 
285
        This enables the accumulation of an inventory delta; for the resulting
 
286
        commit to be valid, deletes against the basis MUST be recorded via
 
287
        builder.record_delete().
 
288
        """
 
289
        self._recording_deletes = True
 
290
 
 
291
    def record_entry_contents(self, ie, parent_invs, path, tree,
 
292
        content_summary):
 
293
        """Record the content of ie from tree into the commit if needed.
 
294
 
 
295
        Side effect: sets ie.revision when unchanged
 
296
 
 
297
        :param ie: An inventory entry present in the commit.
 
298
        :param parent_invs: The inventories of the parent revisions of the
 
299
            commit.
 
300
        :param path: The path the entry is at in the tree.
 
301
        :param tree: The tree which contains this entry and should be used to 
 
302
            obtain content.
 
303
        :param content_summary: Summary data from the tree about the paths
 
304
            content - stat, length, exec, sha/link target. This is only
 
305
            accessed when the entry has a revision of None - that is when it is
 
306
            a candidate to commit.
 
307
        :return: A tuple (change_delta, version_recorded, fs_hash).
 
308
            change_delta is an inventory_delta change for this entry against
 
309
            the basis tree of the commit, or None if no change occured against
 
310
            the basis tree.
 
311
            version_recorded is True if a new version of the entry has been
 
312
            recorded. For instance, committing a merge where a file was only
 
313
            changed on the other side will return (delta, False).
 
314
            fs_hash is either None, or the hash details for the path (currently
 
315
            a tuple of the contents sha1 and the statvalue returned by
 
316
            tree.get_file_with_stat()).
 
317
        """
 
318
        if self.new_inventory.root is None:
 
319
            if ie.parent_id is not None:
 
320
                raise errors.RootMissing()
 
321
            self._check_root(ie, parent_invs, tree)
 
322
        if ie.revision is None:
 
323
            kind = content_summary[0]
 
324
        else:
 
325
            # ie is carried over from a prior commit
 
326
            kind = ie.kind
 
327
        # XXX: repository specific check for nested tree support goes here - if
 
328
        # the repo doesn't want nested trees we skip it ?
 
329
        if (kind == 'tree-reference' and
 
330
            not self.repository._format.supports_tree_reference):
 
331
            # mismatch between commit builder logic and repository:
 
332
            # this needs the entry creation pushed down into the builder.
 
333
            raise NotImplementedError('Missing repository subtree support.')
 
334
        self.new_inventory.add(ie)
 
335
 
 
336
        # TODO: slow, take it out of the inner loop.
 
337
        try:
 
338
            basis_inv = parent_invs[0]
 
339
        except IndexError:
 
340
            basis_inv = Inventory(root_id=None)
 
341
 
 
342
        # ie.revision is always None if the InventoryEntry is considered
 
343
        # for committing. We may record the previous parents revision if the
 
344
        # content is actually unchanged against a sole head.
 
345
        if ie.revision is not None:
 
346
            if not self._versioned_root and path == '':
 
347
                # repositories that do not version the root set the root's
 
348
                # revision to the new commit even when no change occurs (more
 
349
                # specifically, they do not record a revision on the root; and
 
350
                # the rev id is assigned to the root during deserialisation -
 
351
                # this masks when a change may have occurred against the basis.
 
352
                # To match this we always issue a delta, because the revision
 
353
                # of the root will always be changing.
 
354
                if ie.file_id in basis_inv:
 
355
                    delta = (basis_inv.id2path(ie.file_id), path,
 
356
                        ie.file_id, ie)
 
357
                else:
 
358
                    # add
 
359
                    delta = (None, path, ie.file_id, ie)
 
360
                self._basis_delta.append(delta)
 
361
                return delta, False, None
 
362
            else:
 
363
                # we don't need to commit this, because the caller already
 
364
                # determined that an existing revision of this file is
 
365
                # appropriate. If its not being considered for committing then
 
366
                # it and all its parents to the root must be unaltered so
 
367
                # no-change against the basis.
 
368
                if ie.revision == self._new_revision_id:
 
369
                    raise AssertionError("Impossible situation, a skipped "
 
370
                        "inventory entry (%r) claims to be modified in this "
 
371
                        "commit (%r).", (ie, self._new_revision_id))
 
372
                return None, False, None
 
373
        # XXX: Friction: parent_candidates should return a list not a dict
 
374
        #      so that we don't have to walk the inventories again.
 
375
        parent_candiate_entries = ie.parent_candidates(parent_invs)
 
376
        head_set = self._heads(ie.file_id, parent_candiate_entries.keys())
 
377
        heads = []
 
378
        for inv in parent_invs:
 
379
            if ie.file_id in inv:
 
380
                old_rev = inv[ie.file_id].revision
 
381
                if old_rev in head_set:
 
382
                    heads.append(inv[ie.file_id].revision)
 
383
                    head_set.remove(inv[ie.file_id].revision)
 
384
 
 
385
        store = False
 
386
        # now we check to see if we need to write a new record to the
 
387
        # file-graph.
 
388
        # We write a new entry unless there is one head to the ancestors, and
 
389
        # the kind-derived content is unchanged.
 
390
 
 
391
        # Cheapest check first: no ancestors, or more the one head in the
 
392
        # ancestors, we write a new node.
 
393
        if len(heads) != 1:
 
394
            store = True
 
395
        if not store:
 
396
            # There is a single head, look it up for comparison
 
397
            parent_entry = parent_candiate_entries[heads[0]]
 
398
            # if the non-content specific data has changed, we'll be writing a
 
399
            # node:
 
400
            if (parent_entry.parent_id != ie.parent_id or
 
401
                parent_entry.name != ie.name):
 
402
                store = True
 
403
        # now we need to do content specific checks:
 
404
        if not store:
 
405
            # if the kind changed the content obviously has
 
406
            if kind != parent_entry.kind:
 
407
                store = True
 
408
        # Stat cache fingerprint feedback for the caller - None as we usually
 
409
        # don't generate one.
 
410
        fingerprint = None
 
411
        if kind == 'file':
 
412
            if content_summary[2] is None:
 
413
                raise ValueError("Files must not have executable = None")
 
414
            if not store:
 
415
                if (# if the file length changed we have to store:
 
416
                    parent_entry.text_size != content_summary[1] or
 
417
                    # if the exec bit has changed we have to store:
 
418
                    parent_entry.executable != content_summary[2]):
 
419
                    store = True
 
420
                elif parent_entry.text_sha1 == content_summary[3]:
 
421
                    # all meta and content is unchanged (using a hash cache
 
422
                    # hit to check the sha)
 
423
                    ie.revision = parent_entry.revision
 
424
                    ie.text_size = parent_entry.text_size
 
425
                    ie.text_sha1 = parent_entry.text_sha1
 
426
                    ie.executable = parent_entry.executable
 
427
                    return self._get_delta(ie, basis_inv, path), False, None
 
428
                else:
 
429
                    # Either there is only a hash change(no hash cache entry,
 
430
                    # or same size content change), or there is no change on
 
431
                    # this file at all.
 
432
                    # Provide the parent's hash to the store layer, so that the
 
433
                    # content is unchanged we will not store a new node.
 
434
                    nostore_sha = parent_entry.text_sha1
 
435
            if store:
 
436
                # We want to record a new node regardless of the presence or
 
437
                # absence of a content change in the file.
 
438
                nostore_sha = None
 
439
            ie.executable = content_summary[2]
 
440
            file_obj, stat_value = tree.get_file_with_stat(ie.file_id, path)
 
441
            try:
 
442
                lines = file_obj.readlines()
 
443
            finally:
 
444
                file_obj.close()
 
445
            try:
 
446
                ie.text_sha1, ie.text_size = self._add_text_to_weave(
 
447
                    ie.file_id, lines, heads, nostore_sha)
 
448
                # Let the caller know we generated a stat fingerprint.
 
449
                fingerprint = (ie.text_sha1, stat_value)
 
450
            except errors.ExistingContent:
 
451
                # Turns out that the file content was unchanged, and we were
 
452
                # only going to store a new node if it was changed. Carry over
 
453
                # the entry.
 
454
                ie.revision = parent_entry.revision
 
455
                ie.text_size = parent_entry.text_size
 
456
                ie.text_sha1 = parent_entry.text_sha1
 
457
                ie.executable = parent_entry.executable
 
458
                return self._get_delta(ie, basis_inv, path), False, None
 
459
        elif kind == 'directory':
 
460
            if not store:
 
461
                # all data is meta here, nothing specific to directory, so
 
462
                # carry over:
 
463
                ie.revision = parent_entry.revision
 
464
                return self._get_delta(ie, basis_inv, path), False, None
 
465
            lines = []
 
466
            self._add_text_to_weave(ie.file_id, lines, heads, None)
 
467
        elif kind == 'symlink':
 
468
            current_link_target = content_summary[3]
 
469
            if not store:
 
470
                # symlink target is not generic metadata, check if it has
 
471
                # changed.
 
472
                if current_link_target != parent_entry.symlink_target:
 
473
                    store = True
 
474
            if not store:
 
475
                # unchanged, carry over.
 
476
                ie.revision = parent_entry.revision
 
477
                ie.symlink_target = parent_entry.symlink_target
 
478
                return self._get_delta(ie, basis_inv, path), False, None
 
479
            ie.symlink_target = current_link_target
 
480
            lines = []
 
481
            self._add_text_to_weave(ie.file_id, lines, heads, None)
 
482
        elif kind == 'tree-reference':
 
483
            if not store:
 
484
                if content_summary[3] != parent_entry.reference_revision:
 
485
                    store = True
 
486
            if not store:
 
487
                # unchanged, carry over.
 
488
                ie.reference_revision = parent_entry.reference_revision
 
489
                ie.revision = parent_entry.revision
 
490
                return self._get_delta(ie, basis_inv, path), False, None
 
491
            ie.reference_revision = content_summary[3]
 
492
            lines = []
 
493
            self._add_text_to_weave(ie.file_id, lines, heads, None)
 
494
        else:
 
495
            raise NotImplementedError('unknown kind')
 
496
        ie.revision = self._new_revision_id
 
497
        return self._get_delta(ie, basis_inv, path), True, fingerprint
 
498
 
 
499
    def _add_text_to_weave(self, file_id, new_lines, parents, nostore_sha):
 
500
        # Note: as we read the content directly from the tree, we know its not
 
501
        # been turned into unicode or badly split - but a broken tree
 
502
        # implementation could give us bad output from readlines() so this is
 
503
        # not a guarantee of safety. What would be better is always checking
 
504
        # the content during test suite execution. RBC 20070912
 
505
        parent_keys = tuple((file_id, parent) for parent in parents)
 
506
        return self.repository.texts.add_lines(
 
507
            (file_id, self._new_revision_id), parent_keys, new_lines,
 
508
            nostore_sha=nostore_sha, random_id=self.random_revid,
 
509
            check_content=False)[0:2]
 
510
 
 
511
 
 
512
class RootCommitBuilder(CommitBuilder):
 
513
    """This commitbuilder actually records the root id"""
 
514
    
 
515
    # the root entry gets versioned properly by this builder.
 
516
    _versioned_root = True
 
517
 
 
518
    def _check_root(self, ie, parent_invs, tree):
 
519
        """Helper for record_entry_contents.
 
520
 
 
521
        :param ie: An entry being added.
 
522
        :param parent_invs: The inventories of the parent revisions of the
 
523
            commit.
 
524
        :param tree: The tree that is being committed.
 
525
        """
 
526
 
 
527
 
 
528
######################################################################
 
529
# Repositories
 
530
 
 
531
class Repository(object):
 
532
    """Repository holding history for one or more branches.
 
533
 
 
534
    The repository holds and retrieves historical information including
 
535
    revisions and file history.  It's normally accessed only by the Branch,
 
536
    which views a particular line of development through that history.
 
537
 
 
538
    The Repository builds on top of some byte storage facilies (the revisions,
 
539
    signatures, inventories and texts attributes) and a Transport, which
 
540
    respectively provide byte storage and a means to access the (possibly
 
541
    remote) disk.
 
542
 
 
543
    The byte storage facilities are addressed via tuples, which we refer to
 
544
    as 'keys' throughout the code base. Revision_keys, inventory_keys and
 
545
    signature_keys are all 1-tuples: (revision_id,). text_keys are two-tuples:
 
546
    (file_id, revision_id). We use this interface because it allows low
 
547
    friction with the underlying code that implements disk indices, network
 
548
    encoding and other parts of bzrlib.
 
549
 
 
550
    :ivar revisions: A bzrlib.versionedfile.VersionedFiles instance containing
 
551
        the serialised revisions for the repository. This can be used to obtain
 
552
        revision graph information or to access raw serialised revisions.
 
553
        The result of trying to insert data into the repository via this store
 
554
        is undefined: it should be considered read-only except for implementors
 
555
        of repositories.
 
556
    :ivar signatures: A bzrlib.versionedfile.VersionedFiles instance containing
 
557
        the serialised signatures for the repository. This can be used to
 
558
        obtain access to raw serialised signatures.  The result of trying to
 
559
        insert data into the repository via this store is undefined: it should
 
560
        be considered read-only except for implementors of repositories.
 
561
    :ivar inventories: A bzrlib.versionedfile.VersionedFiles instance containing
 
562
        the serialised inventories for the repository. This can be used to
 
563
        obtain unserialised inventories.  The result of trying to insert data
 
564
        into the repository via this store is undefined: it should be
 
565
        considered read-only except for implementors of repositories.
 
566
    :ivar texts: A bzrlib.versionedfile.VersionedFiles instance containing the
 
567
        texts of files and directories for the repository. This can be used to
 
568
        obtain file texts or file graphs. Note that Repository.iter_file_bytes
 
569
        is usually a better interface for accessing file texts.
 
570
        The result of trying to insert data into the repository via this store
 
571
        is undefined: it should be considered read-only except for implementors
 
572
        of repositories.
 
573
    :ivar _transport: Transport for file access to repository, typically
 
574
        pointing to .bzr/repository.
 
575
    """
 
576
 
 
577
    # What class to use for a CommitBuilder. Often its simpler to change this
 
578
    # in a Repository class subclass rather than to override
 
579
    # get_commit_builder.
 
580
    _commit_builder_class = CommitBuilder
 
581
    # The search regex used by xml based repositories to determine what things
 
582
    # where changed in a single commit.
 
583
    _file_ids_altered_regex = lazy_regex.lazy_compile(
 
584
        r'file_id="(?P<file_id>[^"]+)"'
 
585
        r'.* revision="(?P<revision_id>[^"]+)"'
 
586
        )
 
587
 
 
588
    def abort_write_group(self, suppress_errors=False):
 
589
        """Commit the contents accrued within the current write group.
 
590
 
 
591
        :param suppress_errors: if true, abort_write_group will catch and log
 
592
            unexpected errors that happen during the abort, rather than
 
593
            allowing them to propagate.  Defaults to False.
 
594
 
 
595
        :seealso: start_write_group.
 
596
        """
 
597
        if self._write_group is not self.get_transaction():
 
598
            # has an unlock or relock occured ?
 
599
            raise errors.BzrError('mismatched lock context and write group.')
 
600
        try:
 
601
            self._abort_write_group()
 
602
        except Exception, exc:
 
603
            self._write_group = None
 
604
            if not suppress_errors:
 
605
                raise
 
606
            mutter('abort_write_group failed')
 
607
            log_exception_quietly()
 
608
            note('bzr: ERROR (ignored): %s', exc)
 
609
        self._write_group = None
 
610
 
 
611
    def _abort_write_group(self):
 
612
        """Template method for per-repository write group cleanup.
 
613
        
 
614
        This is called during abort before the write group is considered to be 
 
615
        finished and should cleanup any internal state accrued during the write
 
616
        group. There is no requirement that data handed to the repository be
 
617
        *not* made available - this is not a rollback - but neither should any
 
618
        attempt be made to ensure that data added is fully commited. Abort is
 
619
        invoked when an error has occured so futher disk or network operations
 
620
        may not be possible or may error and if possible should not be
 
621
        attempted.
 
622
        """
 
623
 
 
624
    def add_fallback_repository(self, repository):
 
625
        """Add a repository to use for looking up data not held locally.
 
626
        
 
627
        :param repository: A repository.
 
628
        """
 
629
        if not self._format.supports_external_lookups:
 
630
            raise errors.UnstackableRepositoryFormat(self._format, self.base)
 
631
        self._check_fallback_repository(repository)
 
632
        self._fallback_repositories.append(repository)
 
633
        self.texts.add_fallback_versioned_files(repository.texts)
 
634
        self.inventories.add_fallback_versioned_files(repository.inventories)
 
635
        self.revisions.add_fallback_versioned_files(repository.revisions)
 
636
        self.signatures.add_fallback_versioned_files(repository.signatures)
 
637
 
 
638
    def _check_fallback_repository(self, repository):
 
639
        """Check that this repository can fallback to repository safely.
 
640
 
 
641
        Raise an error if not.
 
642
        
 
643
        :param repository: A repository to fallback to.
 
644
        """
 
645
        return InterRepository._assert_same_model(self, repository)
 
646
 
 
647
    def add_inventory(self, revision_id, inv, parents):
 
648
        """Add the inventory inv to the repository as revision_id.
 
649
        
 
650
        :param parents: The revision ids of the parents that revision_id
 
651
                        is known to have and are in the repository already.
 
652
 
 
653
        :returns: The validator(which is a sha1 digest, though what is sha'd is
 
654
            repository format specific) of the serialized inventory.
 
655
        """
 
656
        if not self.is_in_write_group():
 
657
            raise AssertionError("%r not in write group" % (self,))
 
658
        _mod_revision.check_not_reserved_id(revision_id)
 
659
        if not (inv.revision_id is None or inv.revision_id == revision_id):
 
660
            raise AssertionError(
 
661
                "Mismatch between inventory revision"
 
662
                " id and insertion revid (%r, %r)"
 
663
                % (inv.revision_id, revision_id))
 
664
        if inv.root is None:
 
665
            raise AssertionError()
 
666
        inv_lines = self._serialise_inventory_to_lines(inv)
 
667
        return self._inventory_add_lines(revision_id, parents,
 
668
            inv_lines, check_content=False)
 
669
 
 
670
    def add_inventory_by_delta(self, basis_revision_id, delta, new_revision_id,
 
671
                               parents):
 
672
        """Add a new inventory expressed as a delta against another revision.
 
673
 
 
674
        :param basis_revision_id: The inventory id the delta was created
 
675
            against. (This does not have to be a direct parent.)
 
676
        :param delta: The inventory delta (see Inventory.apply_delta for
 
677
            details).
 
678
        :param new_revision_id: The revision id that the inventory is being
 
679
            added for.
 
680
        :param parents: The revision ids of the parents that revision_id is
 
681
            known to have and are in the repository already. These are supplied
 
682
            for repositories that depend on the inventory graph for revision
 
683
            graph access, as well as for those that pun ancestry with delta
 
684
            compression.
 
685
 
 
686
        :returns: The validator(which is a sha1 digest, though what is sha'd is
 
687
            repository format specific) of the serialized inventory.
 
688
        """
 
689
        if not self.is_in_write_group():
 
690
            raise AssertionError("%r not in write group" % (self,))
 
691
        _mod_revision.check_not_reserved_id(new_revision_id)
 
692
        basis_tree = self.revision_tree(basis_revision_id)
 
693
        basis_tree.lock_read()
 
694
        try:
 
695
            # Note that this mutates the inventory of basis_tree, which not all
 
696
            # inventory implementations may support: A better idiom would be to
 
697
            # return a new inventory, but as there is no revision tree cache in
 
698
            # repository this is safe for now - RBC 20081013
 
699
            basis_inv = basis_tree.inventory
 
700
            basis_inv.apply_delta(delta)
 
701
            basis_inv.revision_id = new_revision_id
 
702
            return self.add_inventory(new_revision_id, basis_inv, parents)
 
703
        finally:
 
704
            basis_tree.unlock()
 
705
 
 
706
    def _inventory_add_lines(self, revision_id, parents, lines,
 
707
        check_content=True):
 
708
        """Store lines in inv_vf and return the sha1 of the inventory."""
 
709
        parents = [(parent,) for parent in parents]
 
710
        return self.inventories.add_lines((revision_id,), parents, lines,
 
711
            check_content=check_content)[0]
 
712
 
 
713
    def add_revision(self, revision_id, rev, inv=None, config=None):
 
714
        """Add rev to the revision store as revision_id.
 
715
 
 
716
        :param revision_id: the revision id to use.
 
717
        :param rev: The revision object.
 
718
        :param inv: The inventory for the revision. if None, it will be looked
 
719
                    up in the inventory storer
 
720
        :param config: If None no digital signature will be created.
 
721
                       If supplied its signature_needed method will be used
 
722
                       to determine if a signature should be made.
 
723
        """
 
724
        # TODO: jam 20070210 Shouldn't we check rev.revision_id and
 
725
        #       rev.parent_ids?
 
726
        _mod_revision.check_not_reserved_id(revision_id)
 
727
        if config is not None and config.signature_needed():
 
728
            if inv is None:
 
729
                inv = self.get_inventory(revision_id)
 
730
            plaintext = Testament(rev, inv).as_short_text()
 
731
            self.store_revision_signature(
 
732
                gpg.GPGStrategy(config), plaintext, revision_id)
 
733
        # check inventory present
 
734
        if not self.inventories.get_parent_map([(revision_id,)]):
 
735
            if inv is None:
 
736
                raise errors.WeaveRevisionNotPresent(revision_id,
 
737
                                                     self.inventories)
 
738
            else:
 
739
                # yes, this is not suitable for adding with ghosts.
 
740
                rev.inventory_sha1 = self.add_inventory(revision_id, inv,
 
741
                                                        rev.parent_ids)
 
742
        else:
 
743
            key = (revision_id,)
 
744
            rev.inventory_sha1 = self.inventories.get_sha1s([key])[key]
 
745
        self._add_revision(rev)
 
746
 
 
747
    def _add_revision(self, revision):
 
748
        text = self._serializer.write_revision_to_string(revision)
 
749
        key = (revision.revision_id,)
 
750
        parents = tuple((parent,) for parent in revision.parent_ids)
 
751
        self.revisions.add_lines(key, parents, osutils.split_lines(text))
 
752
 
 
753
    def all_revision_ids(self):
 
754
        """Returns a list of all the revision ids in the repository. 
 
755
 
 
756
        This is conceptually deprecated because code should generally work on
 
757
        the graph reachable from a particular revision, and ignore any other
 
758
        revisions that might be present.  There is no direct replacement
 
759
        method.
 
760
        """
 
761
        if 'evil' in debug.debug_flags:
 
762
            mutter_callsite(2, "all_revision_ids is linear with history.")
 
763
        return self._all_revision_ids()
 
764
 
 
765
    def _all_revision_ids(self):
 
766
        """Returns a list of all the revision ids in the repository. 
 
767
 
 
768
        These are in as much topological order as the underlying store can 
 
769
        present.
 
770
        """
 
771
        raise NotImplementedError(self._all_revision_ids)
 
772
 
 
773
    def break_lock(self):
 
774
        """Break a lock if one is present from another instance.
 
775
 
 
776
        Uses the ui factory to ask for confirmation if the lock may be from
 
777
        an active process.
 
778
        """
 
779
        self.control_files.break_lock()
 
780
 
 
781
    @needs_read_lock
 
782
    def _eliminate_revisions_not_present(self, revision_ids):
 
783
        """Check every revision id in revision_ids to see if we have it.
 
784
 
 
785
        Returns a set of the present revisions.
 
786
        """
 
787
        result = []
 
788
        graph = self.get_graph()
 
789
        parent_map = graph.get_parent_map(revision_ids)
 
790
        # The old API returned a list, should this actually be a set?
 
791
        return parent_map.keys()
 
792
 
 
793
    @staticmethod
 
794
    def create(a_bzrdir):
 
795
        """Construct the current default format repository in a_bzrdir."""
 
796
        return RepositoryFormat.get_default_format().initialize(a_bzrdir)
 
797
 
 
798
    def __init__(self, _format, a_bzrdir, control_files):
 
799
        """instantiate a Repository.
 
800
 
 
801
        :param _format: The format of the repository on disk.
 
802
        :param a_bzrdir: The BzrDir of the repository.
 
803
 
 
804
        In the future we will have a single api for all stores for
 
805
        getting file texts, inventories and revisions, then
 
806
        this construct will accept instances of those things.
 
807
        """
 
808
        super(Repository, self).__init__()
 
809
        self._format = _format
 
810
        # the following are part of the public API for Repository:
 
811
        self.bzrdir = a_bzrdir
 
812
        self.control_files = control_files
 
813
        self._transport = control_files._transport
 
814
        self.base = self._transport.base
 
815
        # for tests
 
816
        self._reconcile_does_inventory_gc = True
 
817
        self._reconcile_fixes_text_parents = False
 
818
        self._reconcile_backsup_inventory = True
 
819
        # not right yet - should be more semantically clear ? 
 
820
        # 
 
821
        # TODO: make sure to construct the right store classes, etc, depending
 
822
        # on whether escaping is required.
 
823
        self._warn_if_deprecated()
 
824
        self._write_group = None
 
825
        # Additional places to query for data.
 
826
        self._fallback_repositories = []
 
827
        # What order should fetch operations request streams in?
 
828
        # The default is unordered as that is the cheapest for an origin to
 
829
        # provide.
 
830
        self._fetch_order = 'unordered'
 
831
        # Does this repository use deltas that can be fetched as-deltas ?
 
832
        # (E.g. knits, where the knit deltas can be transplanted intact.
 
833
        # We default to False, which will ensure that enough data to get
 
834
        # a full text out of any fetch stream will be grabbed.
 
835
        self._fetch_uses_deltas = False
 
836
        # Should fetch trigger a reconcile after the fetch? Only needed for
 
837
        # some repository formats that can suffer internal inconsistencies.
 
838
        self._fetch_reconcile = False
 
839
 
 
840
    def __repr__(self):
 
841
        return '%s(%r)' % (self.__class__.__name__,
 
842
                           self.base)
 
843
 
 
844
    def has_same_location(self, other):
 
845
        """Returns a boolean indicating if this repository is at the same
 
846
        location as another repository.
 
847
 
 
848
        This might return False even when two repository objects are accessing
 
849
        the same physical repository via different URLs.
 
850
        """
 
851
        if self.__class__ is not other.__class__:
 
852
            return False
 
853
        return (self._transport.base == other._transport.base)
 
854
 
 
855
    def is_in_write_group(self):
 
856
        """Return True if there is an open write group.
 
857
 
 
858
        :seealso: start_write_group.
 
859
        """
 
860
        return self._write_group is not None
 
861
 
 
862
    def is_locked(self):
 
863
        return self.control_files.is_locked()
 
864
 
 
865
    def is_write_locked(self):
 
866
        """Return True if this object is write locked."""
 
867
        return self.is_locked() and self.control_files._lock_mode == 'w'
 
868
 
 
869
    def lock_write(self, token=None):
 
870
        """Lock this repository for writing.
 
871
 
 
872
        This causes caching within the repository obejct to start accumlating
 
873
        data during reads, and allows a 'write_group' to be obtained. Write
 
874
        groups must be used for actual data insertion.
 
875
        
 
876
        :param token: if this is already locked, then lock_write will fail
 
877
            unless the token matches the existing lock.
 
878
        :returns: a token if this instance supports tokens, otherwise None.
 
879
        :raises TokenLockingNotSupported: when a token is given but this
 
880
            instance doesn't support using token locks.
 
881
        :raises MismatchedToken: if the specified token doesn't match the token
 
882
            of the existing lock.
 
883
        :seealso: start_write_group.
 
884
 
 
885
        A token should be passed in if you know that you have locked the object
 
886
        some other way, and need to synchronise this object's state with that
 
887
        fact.
 
888
 
 
889
        XXX: this docstring is duplicated in many places, e.g. lockable_files.py
 
890
        """
 
891
        result = self.control_files.lock_write(token=token)
 
892
        for repo in self._fallback_repositories:
 
893
            # Writes don't affect fallback repos
 
894
            repo.lock_read()
 
895
        self._refresh_data()
 
896
        return result
 
897
 
 
898
    def lock_read(self):
 
899
        self.control_files.lock_read()
 
900
        for repo in self._fallback_repositories:
 
901
            repo.lock_read()
 
902
        self._refresh_data()
 
903
 
 
904
    def get_physical_lock_status(self):
 
905
        return self.control_files.get_physical_lock_status()
 
906
 
 
907
    def leave_lock_in_place(self):
 
908
        """Tell this repository not to release the physical lock when this
 
909
        object is unlocked.
 
910
        
 
911
        If lock_write doesn't return a token, then this method is not supported.
 
912
        """
 
913
        self.control_files.leave_in_place()
 
914
 
 
915
    def dont_leave_lock_in_place(self):
 
916
        """Tell this repository to release the physical lock when this
 
917
        object is unlocked, even if it didn't originally acquire it.
 
918
 
 
919
        If lock_write doesn't return a token, then this method is not supported.
 
920
        """
 
921
        self.control_files.dont_leave_in_place()
 
922
 
 
923
    @needs_read_lock
 
924
    def gather_stats(self, revid=None, committers=None):
 
925
        """Gather statistics from a revision id.
 
926
 
 
927
        :param revid: The revision id to gather statistics from, if None, then
 
928
            no revision specific statistics are gathered.
 
929
        :param committers: Optional parameter controlling whether to grab
 
930
            a count of committers from the revision specific statistics.
 
931
        :return: A dictionary of statistics. Currently this contains:
 
932
            committers: The number of committers if requested.
 
933
            firstrev: A tuple with timestamp, timezone for the penultimate left
 
934
                most ancestor of revid, if revid is not the NULL_REVISION.
 
935
            latestrev: A tuple with timestamp, timezone for revid, if revid is
 
936
                not the NULL_REVISION.
 
937
            revisions: The total revision count in the repository.
 
938
            size: An estimate disk size of the repository in bytes.
 
939
        """
 
940
        result = {}
 
941
        if revid and committers:
 
942
            result['committers'] = 0
 
943
        if revid and revid != _mod_revision.NULL_REVISION:
 
944
            if committers:
 
945
                all_committers = set()
 
946
            revisions = self.get_ancestry(revid)
 
947
            # pop the leading None
 
948
            revisions.pop(0)
 
949
            first_revision = None
 
950
            if not committers:
 
951
                # ignore the revisions in the middle - just grab first and last
 
952
                revisions = revisions[0], revisions[-1]
 
953
            for revision in self.get_revisions(revisions):
 
954
                if not first_revision:
 
955
                    first_revision = revision
 
956
                if committers:
 
957
                    all_committers.add(revision.committer)
 
958
            last_revision = revision
 
959
            if committers:
 
960
                result['committers'] = len(all_committers)
 
961
            result['firstrev'] = (first_revision.timestamp,
 
962
                first_revision.timezone)
 
963
            result['latestrev'] = (last_revision.timestamp,
 
964
                last_revision.timezone)
 
965
 
 
966
        # now gather global repository information
 
967
        # XXX: This is available for many repos regardless of listability.
 
968
        if self.bzrdir.root_transport.listable():
 
969
            # XXX: do we want to __define len__() ?
 
970
            # Maybe the versionedfiles object should provide a different
 
971
            # method to get the number of keys.
 
972
            result['revisions'] = len(self.revisions.keys())
 
973
            # result['size'] = t
 
974
        return result
 
975
 
 
976
    def find_branches(self, using=False):
 
977
        """Find branches underneath this repository.
 
978
 
 
979
        This will include branches inside other branches.
 
980
 
 
981
        :param using: If True, list only branches using this repository.
 
982
        """
 
983
        if using and not self.is_shared():
 
984
            try:
 
985
                return [self.bzrdir.open_branch()]
 
986
            except errors.NotBranchError:
 
987
                return []
 
988
        class Evaluator(object):
 
989
 
 
990
            def __init__(self):
 
991
                self.first_call = True
 
992
 
 
993
            def __call__(self, bzrdir):
 
994
                # On the first call, the parameter is always the bzrdir
 
995
                # containing the current repo.
 
996
                if not self.first_call:
 
997
                    try:
 
998
                        repository = bzrdir.open_repository()
 
999
                    except errors.NoRepositoryPresent:
 
1000
                        pass
 
1001
                    else:
 
1002
                        return False, (None, repository)
 
1003
                self.first_call = False
 
1004
                try:
 
1005
                    value = (bzrdir.open_branch(), None)
 
1006
                except errors.NotBranchError:
 
1007
                    value = (None, None)
 
1008
                return True, value
 
1009
 
 
1010
        branches = []
 
1011
        for branch, repository in bzrdir.BzrDir.find_bzrdirs(
 
1012
                self.bzrdir.root_transport, evaluate=Evaluator()):
 
1013
            if branch is not None:
 
1014
                branches.append(branch)
 
1015
            if not using and repository is not None:
 
1016
                branches.extend(repository.find_branches())
 
1017
        return branches
 
1018
 
 
1019
    @needs_read_lock
 
1020
    def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
 
1021
        """Return the revision ids that other has that this does not.
 
1022
        
 
1023
        These are returned in topological order.
 
1024
 
 
1025
        revision_id: only return revision ids included by revision_id.
 
1026
        """
 
1027
        return InterRepository.get(other, self).search_missing_revision_ids(
 
1028
            revision_id, find_ghosts)
 
1029
 
 
1030
    @deprecated_method(one_two)
 
1031
    @needs_read_lock
 
1032
    def missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
 
1033
        """Return the revision ids that other has that this does not.
 
1034
        
 
1035
        These are returned in topological order.
 
1036
 
 
1037
        revision_id: only return revision ids included by revision_id.
 
1038
        """
 
1039
        keys =  self.search_missing_revision_ids(
 
1040
            other, revision_id, find_ghosts).get_keys()
 
1041
        other.lock_read()
 
1042
        try:
 
1043
            parents = other.get_graph().get_parent_map(keys)
 
1044
        finally:
 
1045
            other.unlock()
 
1046
        return tsort.topo_sort(parents)
 
1047
 
 
1048
    @staticmethod
 
1049
    def open(base):
 
1050
        """Open the repository rooted at base.
 
1051
 
 
1052
        For instance, if the repository is at URL/.bzr/repository,
 
1053
        Repository.open(URL) -> a Repository instance.
 
1054
        """
 
1055
        control = bzrdir.BzrDir.open(base)
 
1056
        return control.open_repository()
 
1057
 
 
1058
    def copy_content_into(self, destination, revision_id=None):
 
1059
        """Make a complete copy of the content in self into destination.
 
1060
        
 
1061
        This is a destructive operation! Do not use it on existing 
 
1062
        repositories.
 
1063
        """
 
1064
        return InterRepository.get(self, destination).copy_content(revision_id)
 
1065
 
 
1066
    def commit_write_group(self):
 
1067
        """Commit the contents accrued within the current write group.
 
1068
 
 
1069
        :seealso: start_write_group.
 
1070
        """
 
1071
        if self._write_group is not self.get_transaction():
 
1072
            # has an unlock or relock occured ?
 
1073
            raise errors.BzrError('mismatched lock context %r and '
 
1074
                'write group %r.' %
 
1075
                (self.get_transaction(), self._write_group))
 
1076
        self._commit_write_group()
 
1077
        self._write_group = None
 
1078
 
 
1079
    def _commit_write_group(self):
 
1080
        """Template method for per-repository write group cleanup.
 
1081
        
 
1082
        This is called before the write group is considered to be 
 
1083
        finished and should ensure that all data handed to the repository
 
1084
        for writing during the write group is safely committed (to the 
 
1085
        extent possible considering file system caching etc).
 
1086
        """
 
1087
 
 
1088
    def fetch(self, source, revision_id=None, pb=None, find_ghosts=False):
 
1089
        """Fetch the content required to construct revision_id from source.
 
1090
 
 
1091
        If revision_id is None all content is copied.
 
1092
        :param find_ghosts: Find and copy revisions in the source that are
 
1093
            ghosts in the target (and not reachable directly by walking out to
 
1094
            the first-present revision in target from revision_id).
 
1095
        """
 
1096
        # fast path same-url fetch operations
 
1097
        if self.has_same_location(source):
 
1098
            # check that last_revision is in 'from' and then return a
 
1099
            # no-operation.
 
1100
            if (revision_id is not None and
 
1101
                not _mod_revision.is_null(revision_id)):
 
1102
                self.get_revision(revision_id)
 
1103
            return 0, []
 
1104
        # if there is no specific appropriate InterRepository, this will get
 
1105
        # the InterRepository base class, which raises an
 
1106
        # IncompatibleRepositories when asked to fetch.
 
1107
        inter = InterRepository.get(source, self)
 
1108
        return inter.fetch(revision_id=revision_id, pb=pb,
 
1109
            find_ghosts=find_ghosts)
 
1110
 
 
1111
    def create_bundle(self, target, base, fileobj, format=None):
 
1112
        return serializer.write_bundle(self, target, base, fileobj, format)
 
1113
 
 
1114
    def get_commit_builder(self, branch, parents, config, timestamp=None,
 
1115
                           timezone=None, committer=None, revprops=None,
 
1116
                           revision_id=None):
 
1117
        """Obtain a CommitBuilder for this repository.
 
1118
        
 
1119
        :param branch: Branch to commit to.
 
1120
        :param parents: Revision ids of the parents of the new revision.
 
1121
        :param config: Configuration to use.
 
1122
        :param timestamp: Optional timestamp recorded for commit.
 
1123
        :param timezone: Optional timezone for timestamp.
 
1124
        :param committer: Optional committer to set for commit.
 
1125
        :param revprops: Optional dictionary of revision properties.
 
1126
        :param revision_id: Optional revision id.
 
1127
        """
 
1128
        result = self._commit_builder_class(self, parents, config,
 
1129
            timestamp, timezone, committer, revprops, revision_id)
 
1130
        self.start_write_group()
 
1131
        return result
 
1132
 
 
1133
    def unlock(self):
 
1134
        if (self.control_files._lock_count == 1 and
 
1135
            self.control_files._lock_mode == 'w'):
 
1136
            if self._write_group is not None:
 
1137
                self.abort_write_group()
 
1138
                self.control_files.unlock()
 
1139
                raise errors.BzrError(
 
1140
                    'Must end write groups before releasing write locks.')
 
1141
        self.control_files.unlock()
 
1142
        for repo in self._fallback_repositories:
 
1143
            repo.unlock()
 
1144
 
 
1145
    @needs_read_lock
 
1146
    def clone(self, a_bzrdir, revision_id=None):
 
1147
        """Clone this repository into a_bzrdir using the current format.
 
1148
 
 
1149
        Currently no check is made that the format of this repository and
 
1150
        the bzrdir format are compatible. FIXME RBC 20060201.
 
1151
 
 
1152
        :return: The newly created destination repository.
 
1153
        """
 
1154
        # TODO: deprecate after 0.16; cloning this with all its settings is
 
1155
        # probably not very useful -- mbp 20070423
 
1156
        dest_repo = self._create_sprouting_repo(a_bzrdir, shared=self.is_shared())
 
1157
        self.copy_content_into(dest_repo, revision_id)
 
1158
        return dest_repo
 
1159
 
 
1160
    def start_write_group(self):
 
1161
        """Start a write group in the repository.
 
1162
 
 
1163
        Write groups are used by repositories which do not have a 1:1 mapping
 
1164
        between file ids and backend store to manage the insertion of data from
 
1165
        both fetch and commit operations.
 
1166
 
 
1167
        A write lock is required around the start_write_group/commit_write_group
 
1168
        for the support of lock-requiring repository formats.
 
1169
 
 
1170
        One can only insert data into a repository inside a write group.
 
1171
 
 
1172
        :return: None.
 
1173
        """
 
1174
        if not self.is_write_locked():
 
1175
            raise errors.NotWriteLocked(self)
 
1176
        if self._write_group:
 
1177
            raise errors.BzrError('already in a write group')
 
1178
        self._start_write_group()
 
1179
        # so we can detect unlock/relock - the write group is now entered.
 
1180
        self._write_group = self.get_transaction()
 
1181
 
 
1182
    def _start_write_group(self):
 
1183
        """Template method for per-repository write group startup.
 
1184
        
 
1185
        This is called before the write group is considered to be 
 
1186
        entered.
 
1187
        """
 
1188
 
 
1189
    @needs_read_lock
 
1190
    def sprout(self, to_bzrdir, revision_id=None):
 
1191
        """Create a descendent repository for new development.
 
1192
 
 
1193
        Unlike clone, this does not copy the settings of the repository.
 
1194
        """
 
1195
        dest_repo = self._create_sprouting_repo(to_bzrdir, shared=False)
 
1196
        dest_repo.fetch(self, revision_id=revision_id)
 
1197
        return dest_repo
 
1198
 
 
1199
    def _create_sprouting_repo(self, a_bzrdir, shared):
 
1200
        if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
 
1201
            # use target default format.
 
1202
            dest_repo = a_bzrdir.create_repository()
 
1203
        else:
 
1204
            # Most control formats need the repository to be specifically
 
1205
            # created, but on some old all-in-one formats it's not needed
 
1206
            try:
 
1207
                dest_repo = self._format.initialize(a_bzrdir, shared=shared)
 
1208
            except errors.UninitializableFormat:
 
1209
                dest_repo = a_bzrdir.open_repository()
 
1210
        return dest_repo
 
1211
 
 
1212
    @needs_read_lock
 
1213
    def has_revision(self, revision_id):
 
1214
        """True if this repository has a copy of the revision."""
 
1215
        return revision_id in self.has_revisions((revision_id,))
 
1216
 
 
1217
    @needs_read_lock
 
1218
    def has_revisions(self, revision_ids):
 
1219
        """Probe to find out the presence of multiple revisions.
 
1220
 
 
1221
        :param revision_ids: An iterable of revision_ids.
 
1222
        :return: A set of the revision_ids that were present.
 
1223
        """
 
1224
        parent_map = self.revisions.get_parent_map(
 
1225
            [(rev_id,) for rev_id in revision_ids])
 
1226
        result = set()
 
1227
        if _mod_revision.NULL_REVISION in revision_ids:
 
1228
            result.add(_mod_revision.NULL_REVISION)
 
1229
        result.update([key[0] for key in parent_map])
 
1230
        return result
 
1231
 
 
1232
    @needs_read_lock
 
1233
    def get_revision(self, revision_id):
 
1234
        """Return the Revision object for a named revision."""
 
1235
        return self.get_revisions([revision_id])[0]
 
1236
 
 
1237
    @needs_read_lock
 
1238
    def get_revision_reconcile(self, revision_id):
 
1239
        """'reconcile' helper routine that allows access to a revision always.
 
1240
        
 
1241
        This variant of get_revision does not cross check the weave graph
 
1242
        against the revision one as get_revision does: but it should only
 
1243
        be used by reconcile, or reconcile-alike commands that are correcting
 
1244
        or testing the revision graph.
 
1245
        """
 
1246
        return self._get_revisions([revision_id])[0]
 
1247
 
 
1248
    @needs_read_lock
 
1249
    def get_revisions(self, revision_ids):
 
1250
        """Get many revisions at once."""
 
1251
        return self._get_revisions(revision_ids)
 
1252
 
 
1253
    @needs_read_lock
 
1254
    def _get_revisions(self, revision_ids):
 
1255
        """Core work logic to get many revisions without sanity checks."""
 
1256
        for rev_id in revision_ids:
 
1257
            if not rev_id or not isinstance(rev_id, basestring):
 
1258
                raise errors.InvalidRevisionId(revision_id=rev_id, branch=self)
 
1259
        keys = [(key,) for key in revision_ids]
 
1260
        stream = self.revisions.get_record_stream(keys, 'unordered', True)
 
1261
        revs = {}
 
1262
        for record in stream:
 
1263
            if record.storage_kind == 'absent':
 
1264
                raise errors.NoSuchRevision(self, record.key[0])
 
1265
            text = record.get_bytes_as('fulltext')
 
1266
            rev = self._serializer.read_revision_from_string(text)
 
1267
            revs[record.key[0]] = rev
 
1268
        return [revs[revid] for revid in revision_ids]
 
1269
 
 
1270
    @needs_read_lock
 
1271
    def get_revision_xml(self, revision_id):
 
1272
        # TODO: jam 20070210 This shouldn't be necessary since get_revision
 
1273
        #       would have already do it.
 
1274
        # TODO: jam 20070210 Just use _serializer.write_revision_to_string()
 
1275
        rev = self.get_revision(revision_id)
 
1276
        rev_tmp = cStringIO.StringIO()
 
1277
        # the current serializer..
 
1278
        self._serializer.write_revision(rev, rev_tmp)
 
1279
        rev_tmp.seek(0)
 
1280
        return rev_tmp.getvalue()
 
1281
 
 
1282
    def get_deltas_for_revisions(self, revisions):
 
1283
        """Produce a generator of revision deltas.
 
1284
        
 
1285
        Note that the input is a sequence of REVISIONS, not revision_ids.
 
1286
        Trees will be held in memory until the generator exits.
 
1287
        Each delta is relative to the revision's lefthand predecessor.
 
1288
        """
 
1289
        required_trees = set()
 
1290
        for revision in revisions:
 
1291
            required_trees.add(revision.revision_id)
 
1292
            required_trees.update(revision.parent_ids[:1])
 
1293
        trees = dict((t.get_revision_id(), t) for 
 
1294
                     t in self.revision_trees(required_trees))
 
1295
        for revision in revisions:
 
1296
            if not revision.parent_ids:
 
1297
                old_tree = self.revision_tree(_mod_revision.NULL_REVISION)
 
1298
            else:
 
1299
                old_tree = trees[revision.parent_ids[0]]
 
1300
            yield trees[revision.revision_id].changes_from(old_tree)
 
1301
 
 
1302
    @needs_read_lock
 
1303
    def get_revision_delta(self, revision_id):
 
1304
        """Return the delta for one revision.
 
1305
 
 
1306
        The delta is relative to the left-hand predecessor of the
 
1307
        revision.
 
1308
        """
 
1309
        r = self.get_revision(revision_id)
 
1310
        return list(self.get_deltas_for_revisions([r]))[0]
 
1311
 
 
1312
    @needs_write_lock
 
1313
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
 
1314
        signature = gpg_strategy.sign(plaintext)
 
1315
        self.add_signature_text(revision_id, signature)
 
1316
 
 
1317
    @needs_write_lock
 
1318
    def add_signature_text(self, revision_id, signature):
 
1319
        self.signatures.add_lines((revision_id,), (),
 
1320
            osutils.split_lines(signature))
 
1321
 
 
1322
    def find_text_key_references(self):
 
1323
        """Find the text key references within the repository.
 
1324
 
 
1325
        :return: a dictionary mapping (file_id, revision_id) tuples to altered file-ids to an iterable of
 
1326
        revision_ids. Each altered file-ids has the exact revision_ids that
 
1327
        altered it listed explicitly.
 
1328
        :return: A dictionary mapping text keys ((fileid, revision_id) tuples)
 
1329
            to whether they were referred to by the inventory of the
 
1330
            revision_id that they contain. The inventory texts from all present
 
1331
            revision ids are assessed to generate this report.
 
1332
        """
 
1333
        revision_keys = self.revisions.keys()
 
1334
        w = self.inventories
 
1335
        pb = ui.ui_factory.nested_progress_bar()
 
1336
        try:
 
1337
            return self._find_text_key_references_from_xml_inventory_lines(
 
1338
                w.iter_lines_added_or_present_in_keys(revision_keys, pb=pb))
 
1339
        finally:
 
1340
            pb.finished()
 
1341
 
 
1342
    def _find_text_key_references_from_xml_inventory_lines(self,
 
1343
        line_iterator):
 
1344
        """Core routine for extracting references to texts from inventories.
 
1345
 
 
1346
        This performs the translation of xml lines to revision ids.
 
1347
 
 
1348
        :param line_iterator: An iterator of lines, origin_version_id
 
1349
        :return: A dictionary mapping text keys ((fileid, revision_id) tuples)
 
1350
            to whether they were referred to by the inventory of the
 
1351
            revision_id that they contain. Note that if that revision_id was
 
1352
            not part of the line_iterator's output then False will be given -
 
1353
            even though it may actually refer to that key.
 
1354
        """
 
1355
        if not self._serializer.support_altered_by_hack:
 
1356
            raise AssertionError(
 
1357
                "_find_text_key_references_from_xml_inventory_lines only "
 
1358
                "supported for branches which store inventory as unnested xml"
 
1359
                ", not on %r" % self)
 
1360
        result = {}
 
1361
 
 
1362
        # this code needs to read every new line in every inventory for the
 
1363
        # inventories [revision_ids]. Seeing a line twice is ok. Seeing a line
 
1364
        # not present in one of those inventories is unnecessary but not 
 
1365
        # harmful because we are filtering by the revision id marker in the
 
1366
        # inventory lines : we only select file ids altered in one of those  
 
1367
        # revisions. We don't need to see all lines in the inventory because
 
1368
        # only those added in an inventory in rev X can contain a revision=X
 
1369
        # line.
 
1370
        unescape_revid_cache = {}
 
1371
        unescape_fileid_cache = {}
 
1372
 
 
1373
        # jam 20061218 In a big fetch, this handles hundreds of thousands
 
1374
        # of lines, so it has had a lot of inlining and optimizing done.
 
1375
        # Sorry that it is a little bit messy.
 
1376
        # Move several functions to be local variables, since this is a long
 
1377
        # running loop.
 
1378
        search = self._file_ids_altered_regex.search
 
1379
        unescape = _unescape_xml
 
1380
        setdefault = result.setdefault
 
1381
        for line, line_key in line_iterator:
 
1382
            match = search(line)
 
1383
            if match is None:
 
1384
                continue
 
1385
            # One call to match.group() returning multiple items is quite a
 
1386
            # bit faster than 2 calls to match.group() each returning 1
 
1387
            file_id, revision_id = match.group('file_id', 'revision_id')
 
1388
 
 
1389
            # Inlining the cache lookups helps a lot when you make 170,000
 
1390
            # lines and 350k ids, versus 8.4 unique ids.
 
1391
            # Using a cache helps in 2 ways:
 
1392
            #   1) Avoids unnecessary decoding calls
 
1393
            #   2) Re-uses cached strings, which helps in future set and
 
1394
            #      equality checks.
 
1395
            # (2) is enough that removing encoding entirely along with
 
1396
            # the cache (so we are using plain strings) results in no
 
1397
            # performance improvement.
 
1398
            try:
 
1399
                revision_id = unescape_revid_cache[revision_id]
 
1400
            except KeyError:
 
1401
                unescaped = unescape(revision_id)
 
1402
                unescape_revid_cache[revision_id] = unescaped
 
1403
                revision_id = unescaped
 
1404
 
 
1405
            # Note that unconditionally unescaping means that we deserialise
 
1406
            # every fileid, which for general 'pull' is not great, but we don't
 
1407
            # really want to have some many fulltexts that this matters anyway.
 
1408
            # RBC 20071114.
 
1409
            try:
 
1410
                file_id = unescape_fileid_cache[file_id]
 
1411
            except KeyError:
 
1412
                unescaped = unescape(file_id)
 
1413
                unescape_fileid_cache[file_id] = unescaped
 
1414
                file_id = unescaped
 
1415
 
 
1416
            key = (file_id, revision_id)
 
1417
            setdefault(key, False)
 
1418
            if revision_id == line_key[-1]:
 
1419
                result[key] = True
 
1420
        return result
 
1421
 
 
1422
    def _find_file_ids_from_xml_inventory_lines(self, line_iterator,
 
1423
        revision_ids):
 
1424
        """Helper routine for fileids_altered_by_revision_ids.
 
1425
 
 
1426
        This performs the translation of xml lines to revision ids.
 
1427
 
 
1428
        :param line_iterator: An iterator of lines, origin_version_id
 
1429
        :param revision_ids: The revision ids to filter for. This should be a
 
1430
            set or other type which supports efficient __contains__ lookups, as
 
1431
            the revision id from each parsed line will be looked up in the
 
1432
            revision_ids filter.
 
1433
        :return: a dictionary mapping altered file-ids to an iterable of
 
1434
        revision_ids. Each altered file-ids has the exact revision_ids that
 
1435
        altered it listed explicitly.
 
1436
        """
 
1437
        result = {}
 
1438
        setdefault = result.setdefault
 
1439
        for key in \
 
1440
            self._find_text_key_references_from_xml_inventory_lines(
 
1441
                line_iterator).iterkeys():
 
1442
            # once data is all ensured-consistent; then this is
 
1443
            # if revision_id == version_id
 
1444
            if key[-1:] in revision_ids:
 
1445
                setdefault(key[0], set()).add(key[-1])
 
1446
        return result
 
1447
 
 
1448
    def fileids_altered_by_revision_ids(self, revision_ids, _inv_weave=None):
 
1449
        """Find the file ids and versions affected by revisions.
 
1450
 
 
1451
        :param revisions: an iterable containing revision ids.
 
1452
        :param _inv_weave: The inventory weave from this repository or None.
 
1453
            If None, the inventory weave will be opened automatically.
 
1454
        :return: a dictionary mapping altered file-ids to an iterable of
 
1455
        revision_ids. Each altered file-ids has the exact revision_ids that
 
1456
        altered it listed explicitly.
 
1457
        """
 
1458
        selected_keys = set((revid,) for revid in revision_ids)
 
1459
        w = _inv_weave or self.inventories
 
1460
        pb = ui.ui_factory.nested_progress_bar()
 
1461
        try:
 
1462
            return self._find_file_ids_from_xml_inventory_lines(
 
1463
                w.iter_lines_added_or_present_in_keys(
 
1464
                    selected_keys, pb=pb),
 
1465
                selected_keys)
 
1466
        finally:
 
1467
            pb.finished()
 
1468
 
 
1469
    def iter_files_bytes(self, desired_files):
 
1470
        """Iterate through file versions.
 
1471
 
 
1472
        Files will not necessarily be returned in the order they occur in
 
1473
        desired_files.  No specific order is guaranteed.
 
1474
 
 
1475
        Yields pairs of identifier, bytes_iterator.  identifier is an opaque
 
1476
        value supplied by the caller as part of desired_files.  It should
 
1477
        uniquely identify the file version in the caller's context.  (Examples:
 
1478
        an index number or a TreeTransform trans_id.)
 
1479
 
 
1480
        bytes_iterator is an iterable of bytestrings for the file.  The
 
1481
        kind of iterable and length of the bytestrings are unspecified, but for
 
1482
        this implementation, it is a list of bytes produced by
 
1483
        VersionedFile.get_record_stream().
 
1484
 
 
1485
        :param desired_files: a list of (file_id, revision_id, identifier)
 
1486
            triples
 
1487
        """
 
1488
        transaction = self.get_transaction()
 
1489
        text_keys = {}
 
1490
        for file_id, revision_id, callable_data in desired_files:
 
1491
            text_keys[(file_id, revision_id)] = callable_data
 
1492
        for record in self.texts.get_record_stream(text_keys, 'unordered', True):
 
1493
            if record.storage_kind == 'absent':
 
1494
                raise errors.RevisionNotPresent(record.key, self)
 
1495
            yield text_keys[record.key], record.get_bytes_as('fulltext')
 
1496
 
 
1497
    def _generate_text_key_index(self, text_key_references=None,
 
1498
        ancestors=None):
 
1499
        """Generate a new text key index for the repository.
 
1500
 
 
1501
        This is an expensive function that will take considerable time to run.
 
1502
 
 
1503
        :return: A dict mapping text keys ((file_id, revision_id) tuples) to a
 
1504
            list of parents, also text keys. When a given key has no parents,
 
1505
            the parents list will be [NULL_REVISION].
 
1506
        """
 
1507
        # All revisions, to find inventory parents.
 
1508
        if ancestors is None:
 
1509
            graph = self.get_graph()
 
1510
            ancestors = graph.get_parent_map(self.all_revision_ids())
 
1511
        if text_key_references is None:
 
1512
            text_key_references = self.find_text_key_references()
 
1513
        pb = ui.ui_factory.nested_progress_bar()
 
1514
        try:
 
1515
            return self._do_generate_text_key_index(ancestors,
 
1516
                text_key_references, pb)
 
1517
        finally:
 
1518
            pb.finished()
 
1519
 
 
1520
    def _do_generate_text_key_index(self, ancestors, text_key_references, pb):
 
1521
        """Helper for _generate_text_key_index to avoid deep nesting."""
 
1522
        revision_order = tsort.topo_sort(ancestors)
 
1523
        invalid_keys = set()
 
1524
        revision_keys = {}
 
1525
        for revision_id in revision_order:
 
1526
            revision_keys[revision_id] = set()
 
1527
        text_count = len(text_key_references)
 
1528
        # a cache of the text keys to allow reuse; costs a dict of all the
 
1529
        # keys, but saves a 2-tuple for every child of a given key.
 
1530
        text_key_cache = {}
 
1531
        for text_key, valid in text_key_references.iteritems():
 
1532
            if not valid:
 
1533
                invalid_keys.add(text_key)
 
1534
            else:
 
1535
                revision_keys[text_key[1]].add(text_key)
 
1536
            text_key_cache[text_key] = text_key
 
1537
        del text_key_references
 
1538
        text_index = {}
 
1539
        text_graph = graph.Graph(graph.DictParentsProvider(text_index))
 
1540
        NULL_REVISION = _mod_revision.NULL_REVISION
 
1541
        # Set a cache with a size of 10 - this suffices for bzr.dev but may be
 
1542
        # too small for large or very branchy trees. However, for 55K path
 
1543
        # trees, it would be easy to use too much memory trivially. Ideally we
 
1544
        # could gauge this by looking at available real memory etc, but this is
 
1545
        # always a tricky proposition.
 
1546
        inventory_cache = lru_cache.LRUCache(10)
 
1547
        batch_size = 10 # should be ~150MB on a 55K path tree
 
1548
        batch_count = len(revision_order) / batch_size + 1
 
1549
        processed_texts = 0
 
1550
        pb.update("Calculating text parents.", processed_texts, text_count)
 
1551
        for offset in xrange(batch_count):
 
1552
            to_query = revision_order[offset * batch_size:(offset + 1) *
 
1553
                batch_size]
 
1554
            if not to_query:
 
1555
                break
 
1556
            for rev_tree in self.revision_trees(to_query):
 
1557
                revision_id = rev_tree.get_revision_id()
 
1558
                parent_ids = ancestors[revision_id]
 
1559
                for text_key in revision_keys[revision_id]:
 
1560
                    pb.update("Calculating text parents.", processed_texts)
 
1561
                    processed_texts += 1
 
1562
                    candidate_parents = []
 
1563
                    for parent_id in parent_ids:
 
1564
                        parent_text_key = (text_key[0], parent_id)
 
1565
                        try:
 
1566
                            check_parent = parent_text_key not in \
 
1567
                                revision_keys[parent_id]
 
1568
                        except KeyError:
 
1569
                            # the parent parent_id is a ghost:
 
1570
                            check_parent = False
 
1571
                            # truncate the derived graph against this ghost.
 
1572
                            parent_text_key = None
 
1573
                        if check_parent:
 
1574
                            # look at the parent commit details inventories to
 
1575
                            # determine possible candidates in the per file graph.
 
1576
                            # TODO: cache here.
 
1577
                            try:
 
1578
                                inv = inventory_cache[parent_id]
 
1579
                            except KeyError:
 
1580
                                inv = self.revision_tree(parent_id).inventory
 
1581
                                inventory_cache[parent_id] = inv
 
1582
                            parent_entry = inv._byid.get(text_key[0], None)
 
1583
                            if parent_entry is not None:
 
1584
                                parent_text_key = (
 
1585
                                    text_key[0], parent_entry.revision)
 
1586
                            else:
 
1587
                                parent_text_key = None
 
1588
                        if parent_text_key is not None:
 
1589
                            candidate_parents.append(
 
1590
                                text_key_cache[parent_text_key])
 
1591
                    parent_heads = text_graph.heads(candidate_parents)
 
1592
                    new_parents = list(parent_heads)
 
1593
                    new_parents.sort(key=lambda x:candidate_parents.index(x))
 
1594
                    if new_parents == []:
 
1595
                        new_parents = [NULL_REVISION]
 
1596
                    text_index[text_key] = new_parents
 
1597
 
 
1598
        for text_key in invalid_keys:
 
1599
            text_index[text_key] = [NULL_REVISION]
 
1600
        return text_index
 
1601
 
 
1602
    def item_keys_introduced_by(self, revision_ids, _files_pb=None):
 
1603
        """Get an iterable listing the keys of all the data introduced by a set
 
1604
        of revision IDs.
 
1605
 
 
1606
        The keys will be ordered so that the corresponding items can be safely
 
1607
        fetched and inserted in that order.
 
1608
 
 
1609
        :returns: An iterable producing tuples of (knit-kind, file-id,
 
1610
            versions).  knit-kind is one of 'file', 'inventory', 'signatures',
 
1611
            'revisions'.  file-id is None unless knit-kind is 'file'.
 
1612
        """
 
1613
        # XXX: it's a bit weird to control the inventory weave caching in this
 
1614
        # generator.  Ideally the caching would be done in fetch.py I think.  Or
 
1615
        # maybe this generator should explicitly have the contract that it
 
1616
        # should not be iterated until the previously yielded item has been
 
1617
        # processed?
 
1618
        inv_w = self.inventories
 
1619
 
 
1620
        # file ids that changed
 
1621
        file_ids = self.fileids_altered_by_revision_ids(revision_ids, inv_w)
 
1622
        count = 0
 
1623
        num_file_ids = len(file_ids)
 
1624
        for file_id, altered_versions in file_ids.iteritems():
 
1625
            if _files_pb is not None:
 
1626
                _files_pb.update("fetch texts", count, num_file_ids)
 
1627
            count += 1
 
1628
            yield ("file", file_id, altered_versions)
 
1629
        # We're done with the files_pb.  Note that it finished by the caller,
 
1630
        # just as it was created by the caller.
 
1631
        del _files_pb
 
1632
 
 
1633
        # inventory
 
1634
        yield ("inventory", None, revision_ids)
 
1635
 
 
1636
        # signatures
 
1637
        # XXX: Note ATM no callers actually pay attention to this return
 
1638
        #      instead they just use the list of revision ids and ignore
 
1639
        #      missing sigs. Consider removing this work entirely
 
1640
        revisions_with_signatures = set(self.signatures.get_parent_map(
 
1641
            [(r,) for r in revision_ids]))
 
1642
        revisions_with_signatures = set(
 
1643
            [r for (r,) in revisions_with_signatures])
 
1644
        revisions_with_signatures.intersection_update(revision_ids)
 
1645
        yield ("signatures", None, revisions_with_signatures)
 
1646
 
 
1647
        # revisions
 
1648
        yield ("revisions", None, revision_ids)
 
1649
 
 
1650
    @needs_read_lock
 
1651
    def get_inventory(self, revision_id):
 
1652
        """Get Inventory object by revision id."""
 
1653
        return self.iter_inventories([revision_id]).next()
 
1654
 
 
1655
    def iter_inventories(self, revision_ids):
 
1656
        """Get many inventories by revision_ids.
 
1657
 
 
1658
        This will buffer some or all of the texts used in constructing the
 
1659
        inventories in memory, but will only parse a single inventory at a
 
1660
        time.
 
1661
 
 
1662
        :return: An iterator of inventories.
 
1663
        """
 
1664
        if ((None in revision_ids)
 
1665
            or (_mod_revision.NULL_REVISION in revision_ids)):
 
1666
            raise ValueError('cannot get null revision inventory')
 
1667
        return self._iter_inventories(revision_ids)
 
1668
 
 
1669
    def _iter_inventories(self, revision_ids):
 
1670
        """single-document based inventory iteration."""
 
1671
        for text, revision_id in self._iter_inventory_xmls(revision_ids):
 
1672
            yield self.deserialise_inventory(revision_id, text)
 
1673
 
 
1674
    def _iter_inventory_xmls(self, revision_ids):
 
1675
        keys = [(revision_id,) for revision_id in revision_ids]
 
1676
        stream = self.inventories.get_record_stream(keys, 'unordered', True)
 
1677
        texts = {}
 
1678
        for record in stream:
 
1679
            if record.storage_kind != 'absent':
 
1680
                texts[record.key] = record.get_bytes_as('fulltext')
 
1681
            else:
 
1682
                raise errors.NoSuchRevision(self, record.key)
 
1683
        for key in keys:
 
1684
            yield texts[key], key[-1]
 
1685
 
 
1686
    def deserialise_inventory(self, revision_id, xml):
 
1687
        """Transform the xml into an inventory object. 
 
1688
 
 
1689
        :param revision_id: The expected revision id of the inventory.
 
1690
        :param xml: A serialised inventory.
 
1691
        """
 
1692
        result = self._serializer.read_inventory_from_string(xml, revision_id)
 
1693
        if result.revision_id != revision_id:
 
1694
            raise AssertionError('revision id mismatch %s != %s' % (
 
1695
                result.revision_id, revision_id))
 
1696
        return result
 
1697
 
 
1698
    def serialise_inventory(self, inv):
 
1699
        return self._serializer.write_inventory_to_string(inv)
 
1700
 
 
1701
    def _serialise_inventory_to_lines(self, inv):
 
1702
        return self._serializer.write_inventory_to_lines(inv)
 
1703
 
 
1704
    def get_serializer_format(self):
 
1705
        return self._serializer.format_num
 
1706
 
 
1707
    @needs_read_lock
 
1708
    def get_inventory_xml(self, revision_id):
 
1709
        """Get inventory XML as a file object."""
 
1710
        texts = self._iter_inventory_xmls([revision_id])
 
1711
        try:
 
1712
            text, revision_id = texts.next()
 
1713
        except StopIteration:
 
1714
            raise errors.HistoryMissing(self, 'inventory', revision_id)
 
1715
        return text
 
1716
 
 
1717
    @needs_read_lock
 
1718
    def get_inventory_sha1(self, revision_id):
 
1719
        """Return the sha1 hash of the inventory entry
 
1720
        """
 
1721
        return self.get_revision(revision_id).inventory_sha1
 
1722
 
 
1723
    def iter_reverse_revision_history(self, revision_id):
 
1724
        """Iterate backwards through revision ids in the lefthand history
 
1725
 
 
1726
        :param revision_id: The revision id to start with.  All its lefthand
 
1727
            ancestors will be traversed.
 
1728
        """
 
1729
        graph = self.get_graph()
 
1730
        next_id = revision_id
 
1731
        while True:
 
1732
            if next_id in (None, _mod_revision.NULL_REVISION):
 
1733
                return
 
1734
            yield next_id
 
1735
            # Note: The following line may raise KeyError in the event of
 
1736
            # truncated history. We decided not to have a try:except:raise
 
1737
            # RevisionNotPresent here until we see a use for it, because of the
 
1738
            # cost in an inner loop that is by its very nature O(history).
 
1739
            # Robert Collins 20080326
 
1740
            parents = graph.get_parent_map([next_id])[next_id]
 
1741
            if len(parents) == 0:
 
1742
                return
 
1743
            else:
 
1744
                next_id = parents[0]
 
1745
 
 
1746
    @needs_read_lock
 
1747
    def get_revision_inventory(self, revision_id):
 
1748
        """Return inventory of a past revision."""
 
1749
        # TODO: Unify this with get_inventory()
 
1750
        # bzr 0.0.6 and later imposes the constraint that the inventory_id
 
1751
        # must be the same as its revision, so this is trivial.
 
1752
        if revision_id is None:
 
1753
            # This does not make sense: if there is no revision,
 
1754
            # then it is the current tree inventory surely ?!
 
1755
            # and thus get_root_id() is something that looks at the last
 
1756
            # commit on the branch, and the get_root_id is an inventory check.
 
1757
            raise NotImplementedError
 
1758
            # return Inventory(self.get_root_id())
 
1759
        else:
 
1760
            return self.get_inventory(revision_id)
 
1761
 
 
1762
    def is_shared(self):
 
1763
        """Return True if this repository is flagged as a shared repository."""
 
1764
        raise NotImplementedError(self.is_shared)
 
1765
 
 
1766
    @needs_write_lock
 
1767
    def reconcile(self, other=None, thorough=False):
 
1768
        """Reconcile this repository."""
 
1769
        from bzrlib.reconcile import RepoReconciler
 
1770
        reconciler = RepoReconciler(self, thorough=thorough)
 
1771
        reconciler.reconcile()
 
1772
        return reconciler
 
1773
 
 
1774
    def _refresh_data(self):
 
1775
        """Helper called from lock_* to ensure coherency with disk.
 
1776
 
 
1777
        The default implementation does nothing; it is however possible
 
1778
        for repositories to maintain loaded indices across multiple locks
 
1779
        by checking inside their implementation of this method to see
 
1780
        whether their indices are still valid. This depends of course on
 
1781
        the disk format being validatable in this manner.
 
1782
        """
 
1783
 
 
1784
    @needs_read_lock
 
1785
    def revision_tree(self, revision_id):
 
1786
        """Return Tree for a revision on this branch.
 
1787
 
 
1788
        `revision_id` may be NULL_REVISION for the empty tree revision.
 
1789
        """
 
1790
        revision_id = _mod_revision.ensure_null(revision_id)
 
1791
        # TODO: refactor this to use an existing revision object
 
1792
        # so we don't need to read it in twice.
 
1793
        if revision_id == _mod_revision.NULL_REVISION:
 
1794
            return RevisionTree(self, Inventory(root_id=None), 
 
1795
                                _mod_revision.NULL_REVISION)
 
1796
        else:
 
1797
            inv = self.get_revision_inventory(revision_id)
 
1798
            return RevisionTree(self, inv, revision_id)
 
1799
 
 
1800
    def revision_trees(self, revision_ids):
 
1801
        """Return Tree for a revision on this branch.
 
1802
 
 
1803
        `revision_id` may not be None or 'null:'"""
 
1804
        inventories = self.iter_inventories(revision_ids)
 
1805
        for inv in inventories:
 
1806
            yield RevisionTree(self, inv, inv.revision_id)
 
1807
 
 
1808
    @needs_read_lock
 
1809
    def get_ancestry(self, revision_id, topo_sorted=True):
 
1810
        """Return a list of revision-ids integrated by a revision.
 
1811
 
 
1812
        The first element of the list is always None, indicating the origin 
 
1813
        revision.  This might change when we have history horizons, or 
 
1814
        perhaps we should have a new API.
 
1815
        
 
1816
        This is topologically sorted.
 
1817
        """
 
1818
        if _mod_revision.is_null(revision_id):
 
1819
            return [None]
 
1820
        if not self.has_revision(revision_id):
 
1821
            raise errors.NoSuchRevision(self, revision_id)
 
1822
        graph = self.get_graph()
 
1823
        keys = set()
 
1824
        search = graph._make_breadth_first_searcher([revision_id])
 
1825
        while True:
 
1826
            try:
 
1827
                found, ghosts = search.next_with_ghosts()
 
1828
            except StopIteration:
 
1829
                break
 
1830
            keys.update(found)
 
1831
        if _mod_revision.NULL_REVISION in keys:
 
1832
            keys.remove(_mod_revision.NULL_REVISION)
 
1833
        if topo_sorted:
 
1834
            parent_map = graph.get_parent_map(keys)
 
1835
            keys = tsort.topo_sort(parent_map)
 
1836
        return [None] + list(keys)
 
1837
 
 
1838
    def pack(self):
 
1839
        """Compress the data within the repository.
 
1840
 
 
1841
        This operation only makes sense for some repository types. For other
 
1842
        types it should be a no-op that just returns.
 
1843
 
 
1844
        This stub method does not require a lock, but subclasses should use
 
1845
        @needs_write_lock as this is a long running call its reasonable to 
 
1846
        implicitly lock for the user.
 
1847
        """
 
1848
 
 
1849
    @needs_read_lock
 
1850
    @deprecated_method(one_six)
 
1851
    def print_file(self, file, revision_id):
 
1852
        """Print `file` to stdout.
 
1853
        
 
1854
        FIXME RBC 20060125 as John Meinel points out this is a bad api
 
1855
        - it writes to stdout, it assumes that that is valid etc. Fix
 
1856
        by creating a new more flexible convenience function.
 
1857
        """
 
1858
        tree = self.revision_tree(revision_id)
 
1859
        # use inventory as it was in that revision
 
1860
        file_id = tree.inventory.path2id(file)
 
1861
        if not file_id:
 
1862
            # TODO: jam 20060427 Write a test for this code path
 
1863
            #       it had a bug in it, and was raising the wrong
 
1864
            #       exception.
 
1865
            raise errors.BzrError("%r is not present in revision %s" % (file, revision_id))
 
1866
        tree.print_file(file_id)
 
1867
 
 
1868
    def get_transaction(self):
 
1869
        return self.control_files.get_transaction()
 
1870
 
 
1871
    @deprecated_method(one_one)
 
1872
    def get_parents(self, revision_ids):
 
1873
        """See StackedParentsProvider.get_parents"""
 
1874
        parent_map = self.get_parent_map(revision_ids)
 
1875
        return [parent_map.get(r, None) for r in revision_ids]
 
1876
 
 
1877
    def get_parent_map(self, revision_ids):
 
1878
        """See graph._StackedParentsProvider.get_parent_map"""
 
1879
        # revisions index works in keys; this just works in revisions
 
1880
        # therefore wrap and unwrap
 
1881
        query_keys = []
 
1882
        result = {}
 
1883
        for revision_id in revision_ids:
 
1884
            if revision_id == _mod_revision.NULL_REVISION:
 
1885
                result[revision_id] = ()
 
1886
            elif revision_id is None:
 
1887
                raise ValueError('get_parent_map(None) is not valid')
 
1888
            else:
 
1889
                query_keys.append((revision_id ,))
 
1890
        for ((revision_id,), parent_keys) in \
 
1891
                self.revisions.get_parent_map(query_keys).iteritems():
 
1892
            if parent_keys:
 
1893
                result[revision_id] = tuple(parent_revid
 
1894
                    for (parent_revid,) in parent_keys)
 
1895
            else:
 
1896
                result[revision_id] = (_mod_revision.NULL_REVISION,)
 
1897
        return result
 
1898
 
 
1899
    def _make_parents_provider(self):
 
1900
        return self
 
1901
 
 
1902
    def get_graph(self, other_repository=None):
 
1903
        """Return the graph walker for this repository format"""
 
1904
        parents_provider = self._make_parents_provider()
 
1905
        if (other_repository is not None and
 
1906
            not self.has_same_location(other_repository)):
 
1907
            parents_provider = graph._StackedParentsProvider(
 
1908
                [parents_provider, other_repository._make_parents_provider()])
 
1909
        return graph.Graph(parents_provider)
 
1910
 
 
1911
    def _get_versioned_file_checker(self):
 
1912
        """Return an object suitable for checking versioned files."""
 
1913
        return _VersionedFileChecker(self)
 
1914
 
 
1915
    def revision_ids_to_search_result(self, result_set):
 
1916
        """Convert a set of revision ids to a graph SearchResult."""
 
1917
        result_parents = set()
 
1918
        for parents in self.get_graph().get_parent_map(
 
1919
            result_set).itervalues():
 
1920
            result_parents.update(parents)
 
1921
        included_keys = result_set.intersection(result_parents)
 
1922
        start_keys = result_set.difference(included_keys)
 
1923
        exclude_keys = result_parents.difference(result_set)
 
1924
        result = graph.SearchResult(start_keys, exclude_keys,
 
1925
            len(result_set), result_set)
 
1926
        return result
 
1927
 
 
1928
    @needs_write_lock
 
1929
    def set_make_working_trees(self, new_value):
 
1930
        """Set the policy flag for making working trees when creating branches.
 
1931
 
 
1932
        This only applies to branches that use this repository.
 
1933
 
 
1934
        The default is 'True'.
 
1935
        :param new_value: True to restore the default, False to disable making
 
1936
                          working trees.
 
1937
        """
 
1938
        raise NotImplementedError(self.set_make_working_trees)
 
1939
    
 
1940
    def make_working_trees(self):
 
1941
        """Returns the policy for making working trees on new branches."""
 
1942
        raise NotImplementedError(self.make_working_trees)
 
1943
 
 
1944
    @needs_write_lock
 
1945
    def sign_revision(self, revision_id, gpg_strategy):
 
1946
        plaintext = Testament.from_revision(self, revision_id).as_short_text()
 
1947
        self.store_revision_signature(gpg_strategy, plaintext, revision_id)
 
1948
 
 
1949
    @needs_read_lock
 
1950
    def has_signature_for_revision_id(self, revision_id):
 
1951
        """Query for a revision signature for revision_id in the repository."""
 
1952
        if not self.has_revision(revision_id):
 
1953
            raise errors.NoSuchRevision(self, revision_id)
 
1954
        sig_present = (1 == len(
 
1955
            self.signatures.get_parent_map([(revision_id,)])))
 
1956
        return sig_present
 
1957
 
 
1958
    @needs_read_lock
 
1959
    def get_signature_text(self, revision_id):
 
1960
        """Return the text for a signature."""
 
1961
        stream = self.signatures.get_record_stream([(revision_id,)],
 
1962
            'unordered', True)
 
1963
        record = stream.next()
 
1964
        if record.storage_kind == 'absent':
 
1965
            raise errors.NoSuchRevision(self, revision_id)
 
1966
        return record.get_bytes_as('fulltext')
 
1967
 
 
1968
    @needs_read_lock
 
1969
    def check(self, revision_ids=None):
 
1970
        """Check consistency of all history of given revision_ids.
 
1971
 
 
1972
        Different repository implementations should override _check().
 
1973
 
 
1974
        :param revision_ids: A non-empty list of revision_ids whose ancestry
 
1975
             will be checked.  Typically the last revision_id of a branch.
 
1976
        """
 
1977
        return self._check(revision_ids)
 
1978
 
 
1979
    def _check(self, revision_ids):
 
1980
        result = check.Check(self)
 
1981
        result.check()
 
1982
        return result
 
1983
 
 
1984
    def _warn_if_deprecated(self):
 
1985
        global _deprecation_warning_done
 
1986
        if _deprecation_warning_done:
 
1987
            return
 
1988
        _deprecation_warning_done = True
 
1989
        warning("Format %s for %s is deprecated - please use 'bzr upgrade' to get better performance"
 
1990
                % (self._format, self.bzrdir.transport.base))
 
1991
 
 
1992
    def supports_rich_root(self):
 
1993
        return self._format.rich_root_data
 
1994
 
 
1995
    def _check_ascii_revisionid(self, revision_id, method):
 
1996
        """Private helper for ascii-only repositories."""
 
1997
        # weave repositories refuse to store revisionids that are non-ascii.
 
1998
        if revision_id is not None:
 
1999
            # weaves require ascii revision ids.
 
2000
            if isinstance(revision_id, unicode):
 
2001
                try:
 
2002
                    revision_id.encode('ascii')
 
2003
                except UnicodeEncodeError:
 
2004
                    raise errors.NonAsciiRevisionId(method, self)
 
2005
            else:
 
2006
                try:
 
2007
                    revision_id.decode('ascii')
 
2008
                except UnicodeDecodeError:
 
2009
                    raise errors.NonAsciiRevisionId(method, self)
 
2010
    
 
2011
    def revision_graph_can_have_wrong_parents(self):
 
2012
        """Is it possible for this repository to have a revision graph with
 
2013
        incorrect parents?
 
2014
 
 
2015
        If True, then this repository must also implement
 
2016
        _find_inconsistent_revision_parents so that check and reconcile can
 
2017
        check for inconsistencies before proceeding with other checks that may
 
2018
        depend on the revision index being consistent.
 
2019
        """
 
2020
        raise NotImplementedError(self.revision_graph_can_have_wrong_parents)
 
2021
 
 
2022
 
 
2023
# remove these delegates a while after bzr 0.15
 
2024
def __make_delegated(name, from_module):
 
2025
    def _deprecated_repository_forwarder():
 
2026
        symbol_versioning.warn('%s moved to %s in bzr 0.15'
 
2027
            % (name, from_module),
 
2028
            DeprecationWarning,
 
2029
            stacklevel=2)
 
2030
        m = __import__(from_module, globals(), locals(), [name])
 
2031
        try:
 
2032
            return getattr(m, name)
 
2033
        except AttributeError:
 
2034
            raise AttributeError('module %s has no name %s'
 
2035
                    % (m, name))
 
2036
    globals()[name] = _deprecated_repository_forwarder
 
2037
 
 
2038
for _name in [
 
2039
        'AllInOneRepository',
 
2040
        'WeaveMetaDirRepository',
 
2041
        'PreSplitOutRepositoryFormat',
 
2042
        'RepositoryFormat4',
 
2043
        'RepositoryFormat5',
 
2044
        'RepositoryFormat6',
 
2045
        'RepositoryFormat7',
 
2046
        ]:
 
2047
    __make_delegated(_name, 'bzrlib.repofmt.weaverepo')
 
2048
 
 
2049
for _name in [
 
2050
        'KnitRepository',
 
2051
        'RepositoryFormatKnit',
 
2052
        'RepositoryFormatKnit1',
 
2053
        ]:
 
2054
    __make_delegated(_name, 'bzrlib.repofmt.knitrepo')
 
2055
 
 
2056
 
 
2057
def install_revision(repository, rev, revision_tree):
 
2058
    """Install all revision data into a repository."""
 
2059
    install_revisions(repository, [(rev, revision_tree, None)])
 
2060
 
 
2061
 
 
2062
def install_revisions(repository, iterable, num_revisions=None, pb=None):
 
2063
    """Install all revision data into a repository.
 
2064
 
 
2065
    Accepts an iterable of revision, tree, signature tuples.  The signature
 
2066
    may be None.
 
2067
    """
 
2068
    repository.start_write_group()
 
2069
    try:
 
2070
        for n, (revision, revision_tree, signature) in enumerate(iterable):
 
2071
            _install_revision(repository, revision, revision_tree, signature)
 
2072
            if pb is not None:
 
2073
                pb.update('Transferring revisions', n + 1, num_revisions)
 
2074
    except:
 
2075
        repository.abort_write_group()
 
2076
        raise
 
2077
    else:
 
2078
        repository.commit_write_group()
 
2079
 
 
2080
 
 
2081
def _install_revision(repository, rev, revision_tree, signature):
 
2082
    """Install all revision data into a repository."""
 
2083
    present_parents = []
 
2084
    parent_trees = {}
 
2085
    for p_id in rev.parent_ids:
 
2086
        if repository.has_revision(p_id):
 
2087
            present_parents.append(p_id)
 
2088
            parent_trees[p_id] = repository.revision_tree(p_id)
 
2089
        else:
 
2090
            parent_trees[p_id] = repository.revision_tree(
 
2091
                                     _mod_revision.NULL_REVISION)
 
2092
 
 
2093
    inv = revision_tree.inventory
 
2094
    entries = inv.iter_entries()
 
2095
    # backwards compatibility hack: skip the root id.
 
2096
    if not repository.supports_rich_root():
 
2097
        path, root = entries.next()
 
2098
        if root.revision != rev.revision_id:
 
2099
            raise errors.IncompatibleRevision(repr(repository))
 
2100
    text_keys = {}
 
2101
    for path, ie in entries:
 
2102
        text_keys[(ie.file_id, ie.revision)] = ie
 
2103
    text_parent_map = repository.texts.get_parent_map(text_keys)
 
2104
    missing_texts = set(text_keys) - set(text_parent_map)
 
2105
    # Add the texts that are not already present
 
2106
    for text_key in missing_texts:
 
2107
        ie = text_keys[text_key]
 
2108
        text_parents = []
 
2109
        # FIXME: TODO: The following loop overlaps/duplicates that done by
 
2110
        # commit to determine parents. There is a latent/real bug here where
 
2111
        # the parents inserted are not those commit would do - in particular
 
2112
        # they are not filtered by heads(). RBC, AB
 
2113
        for revision, tree in parent_trees.iteritems():
 
2114
            if ie.file_id not in tree:
 
2115
                continue
 
2116
            parent_id = tree.inventory[ie.file_id].revision
 
2117
            if parent_id in text_parents:
 
2118
                continue
 
2119
            text_parents.append((ie.file_id, parent_id))
 
2120
        lines = revision_tree.get_file(ie.file_id).readlines()
 
2121
        repository.texts.add_lines(text_key, text_parents, lines)
 
2122
    try:
 
2123
        # install the inventory
 
2124
        repository.add_inventory(rev.revision_id, inv, present_parents)
 
2125
    except errors.RevisionAlreadyPresent:
 
2126
        pass
 
2127
    if signature is not None:
 
2128
        repository.add_signature_text(rev.revision_id, signature)
 
2129
    repository.add_revision(rev.revision_id, rev, inv)
 
2130
 
 
2131
 
 
2132
class MetaDirRepository(Repository):
 
2133
    """Repositories in the new meta-dir layout.
 
2134
    
 
2135
    :ivar _transport: Transport for access to repository control files,
 
2136
        typically pointing to .bzr/repository.
 
2137
    """
 
2138
 
 
2139
    def __init__(self, _format, a_bzrdir, control_files):
 
2140
        super(MetaDirRepository, self).__init__(_format, a_bzrdir, control_files)
 
2141
        self._transport = control_files._transport
 
2142
 
 
2143
    def is_shared(self):
 
2144
        """Return True if this repository is flagged as a shared repository."""
 
2145
        return self._transport.has('shared-storage')
 
2146
 
 
2147
    @needs_write_lock
 
2148
    def set_make_working_trees(self, new_value):
 
2149
        """Set the policy flag for making working trees when creating branches.
 
2150
 
 
2151
        This only applies to branches that use this repository.
 
2152
 
 
2153
        The default is 'True'.
 
2154
        :param new_value: True to restore the default, False to disable making
 
2155
                          working trees.
 
2156
        """
 
2157
        if new_value:
 
2158
            try:
 
2159
                self._transport.delete('no-working-trees')
 
2160
            except errors.NoSuchFile:
 
2161
                pass
 
2162
        else:
 
2163
            self._transport.put_bytes('no-working-trees', '',
 
2164
                mode=self.bzrdir._get_file_mode())
 
2165
    
 
2166
    def make_working_trees(self):
 
2167
        """Returns the policy for making working trees on new branches."""
 
2168
        return not self._transport.has('no-working-trees')
 
2169
 
 
2170
 
 
2171
class MetaDirVersionedFileRepository(MetaDirRepository):
 
2172
    """Repositories in a meta-dir, that work via versioned file objects."""
 
2173
 
 
2174
    def __init__(self, _format, a_bzrdir, control_files):
 
2175
        super(MetaDirVersionedFileRepository, self).__init__(_format, a_bzrdir,
 
2176
            control_files)
 
2177
 
 
2178
 
 
2179
class RepositoryFormatRegistry(registry.Registry):
 
2180
    """Registry of RepositoryFormats."""
 
2181
 
 
2182
    def get(self, format_string):
 
2183
        r = registry.Registry.get(self, format_string)
 
2184
        if callable(r):
 
2185
            r = r()
 
2186
        return r
 
2187
    
 
2188
 
 
2189
format_registry = RepositoryFormatRegistry()
 
2190
"""Registry of formats, indexed by their identifying format string.
 
2191
 
 
2192
This can contain either format instances themselves, or classes/factories that
 
2193
can be called to obtain one.
 
2194
"""
 
2195
 
 
2196
 
 
2197
#####################################################################
 
2198
# Repository Formats
 
2199
 
 
2200
class RepositoryFormat(object):
 
2201
    """A repository format.
 
2202
 
 
2203
    Formats provide three things:
 
2204
     * An initialization routine to construct repository data on disk.
 
2205
     * a format string which is used when the BzrDir supports versioned
 
2206
       children.
 
2207
     * an open routine which returns a Repository instance.
 
2208
 
 
2209
    There is one and only one Format subclass for each on-disk format. But
 
2210
    there can be one Repository subclass that is used for several different
 
2211
    formats. The _format attribute on a Repository instance can be used to
 
2212
    determine the disk format.
 
2213
 
 
2214
    Formats are placed in an dict by their format string for reference 
 
2215
    during opening. These should be subclasses of RepositoryFormat
 
2216
    for consistency.
 
2217
 
 
2218
    Once a format is deprecated, just deprecate the initialize and open
 
2219
    methods on the format class. Do not deprecate the object, as the 
 
2220
    object will be created every system load.
 
2221
 
 
2222
    Common instance attributes:
 
2223
    _matchingbzrdir - the bzrdir format that the repository format was
 
2224
    originally written to work with. This can be used if manually
 
2225
    constructing a bzrdir and repository, or more commonly for test suite
 
2226
    parameterization.
 
2227
    """
 
2228
 
 
2229
    # Set to True or False in derived classes. True indicates that the format
 
2230
    # supports ghosts gracefully.
 
2231
    supports_ghosts = None
 
2232
    # Can this repository be given external locations to lookup additional
 
2233
    # data. Set to True or False in derived classes.
 
2234
    supports_external_lookups = None
 
2235
 
 
2236
    def __str__(self):
 
2237
        return "<%s>" % self.__class__.__name__
 
2238
 
 
2239
    def __eq__(self, other):
 
2240
        # format objects are generally stateless
 
2241
        return isinstance(other, self.__class__)
 
2242
 
 
2243
    def __ne__(self, other):
 
2244
        return not self == other
 
2245
 
 
2246
    @classmethod
 
2247
    def find_format(klass, a_bzrdir):
 
2248
        """Return the format for the repository object in a_bzrdir.
 
2249
        
 
2250
        This is used by bzr native formats that have a "format" file in
 
2251
        the repository.  Other methods may be used by different types of 
 
2252
        control directory.
 
2253
        """
 
2254
        try:
 
2255
            transport = a_bzrdir.get_repository_transport(None)
 
2256
            format_string = transport.get("format").read()
 
2257
            return format_registry.get(format_string)
 
2258
        except errors.NoSuchFile:
 
2259
            raise errors.NoRepositoryPresent(a_bzrdir)
 
2260
        except KeyError:
 
2261
            raise errors.UnknownFormatError(format=format_string,
 
2262
                                            kind='repository')
 
2263
 
 
2264
    @classmethod
 
2265
    def register_format(klass, format):
 
2266
        format_registry.register(format.get_format_string(), format)
 
2267
 
 
2268
    @classmethod
 
2269
    def unregister_format(klass, format):
 
2270
        format_registry.remove(format.get_format_string())
 
2271
    
 
2272
    @classmethod
 
2273
    def get_default_format(klass):
 
2274
        """Return the current default format."""
 
2275
        from bzrlib import bzrdir
 
2276
        return bzrdir.format_registry.make_bzrdir('default').repository_format
 
2277
 
 
2278
    def get_format_string(self):
 
2279
        """Return the ASCII format string that identifies this format.
 
2280
        
 
2281
        Note that in pre format ?? repositories the format string is 
 
2282
        not permitted nor written to disk.
 
2283
        """
 
2284
        raise NotImplementedError(self.get_format_string)
 
2285
 
 
2286
    def get_format_description(self):
 
2287
        """Return the short description for this format."""
 
2288
        raise NotImplementedError(self.get_format_description)
 
2289
 
 
2290
    # TODO: this shouldn't be in the base class, it's specific to things that
 
2291
    # use weaves or knits -- mbp 20070207
 
2292
    def _get_versioned_file_store(self,
 
2293
                                  name,
 
2294
                                  transport,
 
2295
                                  control_files,
 
2296
                                  prefixed=True,
 
2297
                                  versionedfile_class=None,
 
2298
                                  versionedfile_kwargs={},
 
2299
                                  escaped=False):
 
2300
        if versionedfile_class is None:
 
2301
            versionedfile_class = self._versionedfile_class
 
2302
        weave_transport = control_files._transport.clone(name)
 
2303
        dir_mode = control_files._dir_mode
 
2304
        file_mode = control_files._file_mode
 
2305
        return VersionedFileStore(weave_transport, prefixed=prefixed,
 
2306
                                  dir_mode=dir_mode,
 
2307
                                  file_mode=file_mode,
 
2308
                                  versionedfile_class=versionedfile_class,
 
2309
                                  versionedfile_kwargs=versionedfile_kwargs,
 
2310
                                  escaped=escaped)
 
2311
 
 
2312
    def initialize(self, a_bzrdir, shared=False):
 
2313
        """Initialize a repository of this format in a_bzrdir.
 
2314
 
 
2315
        :param a_bzrdir: The bzrdir to put the new repository in it.
 
2316
        :param shared: The repository should be initialized as a sharable one.
 
2317
        :returns: The new repository object.
 
2318
        
 
2319
        This may raise UninitializableFormat if shared repository are not
 
2320
        compatible the a_bzrdir.
 
2321
        """
 
2322
        raise NotImplementedError(self.initialize)
 
2323
 
 
2324
    def is_supported(self):
 
2325
        """Is this format supported?
 
2326
 
 
2327
        Supported formats must be initializable and openable.
 
2328
        Unsupported formats may not support initialization or committing or 
 
2329
        some other features depending on the reason for not being supported.
 
2330
        """
 
2331
        return True
 
2332
 
 
2333
    def check_conversion_target(self, target_format):
 
2334
        raise NotImplementedError(self.check_conversion_target)
 
2335
 
 
2336
    def open(self, a_bzrdir, _found=False):
 
2337
        """Return an instance of this format for the bzrdir a_bzrdir.
 
2338
        
 
2339
        _found is a private parameter, do not use it.
 
2340
        """
 
2341
        raise NotImplementedError(self.open)
 
2342
 
 
2343
 
 
2344
class MetaDirRepositoryFormat(RepositoryFormat):
 
2345
    """Common base class for the new repositories using the metadir layout."""
 
2346
 
 
2347
    rich_root_data = False
 
2348
    supports_tree_reference = False
 
2349
    supports_external_lookups = False
 
2350
 
 
2351
    @property
 
2352
    def _matchingbzrdir(self):
 
2353
        matching = bzrdir.BzrDirMetaFormat1()
 
2354
        matching.repository_format = self
 
2355
        return matching
 
2356
 
 
2357
    def __init__(self):
 
2358
        super(MetaDirRepositoryFormat, self).__init__()
 
2359
 
 
2360
    def _create_control_files(self, a_bzrdir):
 
2361
        """Create the required files and the initial control_files object."""
 
2362
        # FIXME: RBC 20060125 don't peek under the covers
 
2363
        # NB: no need to escape relative paths that are url safe.
 
2364
        repository_transport = a_bzrdir.get_repository_transport(self)
 
2365
        control_files = lockable_files.LockableFiles(repository_transport,
 
2366
                                'lock', lockdir.LockDir)
 
2367
        control_files.create_lock()
 
2368
        return control_files
 
2369
 
 
2370
    def _upload_blank_content(self, a_bzrdir, dirs, files, utf8_files, shared):
 
2371
        """Upload the initial blank content."""
 
2372
        control_files = self._create_control_files(a_bzrdir)
 
2373
        control_files.lock_write()
 
2374
        transport = control_files._transport
 
2375
        if shared == True:
 
2376
            utf8_files += [('shared-storage', '')]
 
2377
        try:
 
2378
            transport.mkdir_multi(dirs, mode=a_bzrdir._get_dir_mode())
 
2379
            for (filename, content_stream) in files:
 
2380
                transport.put_file(filename, content_stream,
 
2381
                    mode=a_bzrdir._get_file_mode())
 
2382
            for (filename, content_bytes) in utf8_files:
 
2383
                transport.put_bytes_non_atomic(filename, content_bytes,
 
2384
                    mode=a_bzrdir._get_file_mode())
 
2385
        finally:
 
2386
            control_files.unlock()
 
2387
 
 
2388
 
 
2389
# formats which have no format string are not discoverable
 
2390
# and not independently creatable, so are not registered.  They're 
 
2391
# all in bzrlib.repofmt.weaverepo now.  When an instance of one of these is
 
2392
# needed, it's constructed directly by the BzrDir.  Non-native formats where
 
2393
# the repository is not separately opened are similar.
 
2394
 
 
2395
format_registry.register_lazy(
 
2396
    'Bazaar-NG Repository format 7',
 
2397
    'bzrlib.repofmt.weaverepo',
 
2398
    'RepositoryFormat7'
 
2399
    )
 
2400
 
 
2401
format_registry.register_lazy(
 
2402
    'Bazaar-NG Knit Repository Format 1',
 
2403
    'bzrlib.repofmt.knitrepo',
 
2404
    'RepositoryFormatKnit1',
 
2405
    )
 
2406
 
 
2407
format_registry.register_lazy(
 
2408
    'Bazaar Knit Repository Format 3 (bzr 0.15)\n',
 
2409
    'bzrlib.repofmt.knitrepo',
 
2410
    'RepositoryFormatKnit3',
 
2411
    )
 
2412
 
 
2413
format_registry.register_lazy(
 
2414
    'Bazaar Knit Repository Format 4 (bzr 1.0)\n',
 
2415
    'bzrlib.repofmt.knitrepo',
 
2416
    'RepositoryFormatKnit4',
 
2417
    )
 
2418
 
 
2419
# Pack-based formats. There is one format for pre-subtrees, and one for
 
2420
# post-subtrees to allow ease of testing.
 
2421
# NOTE: These are experimental in 0.92. Stable in 1.0 and above
 
2422
format_registry.register_lazy(
 
2423
    'Bazaar pack repository format 1 (needs bzr 0.92)\n',
 
2424
    'bzrlib.repofmt.pack_repo',
 
2425
    'RepositoryFormatKnitPack1',
 
2426
    )
 
2427
format_registry.register_lazy(
 
2428
    'Bazaar pack repository format 1 with subtree support (needs bzr 0.92)\n',
 
2429
    'bzrlib.repofmt.pack_repo',
 
2430
    'RepositoryFormatKnitPack3',
 
2431
    )
 
2432
format_registry.register_lazy(
 
2433
    'Bazaar pack repository format 1 with rich root (needs bzr 1.0)\n',
 
2434
    'bzrlib.repofmt.pack_repo',
 
2435
    'RepositoryFormatKnitPack4',
 
2436
    )
 
2437
format_registry.register_lazy(
 
2438
    'Bazaar RepositoryFormatKnitPack5 (bzr 1.6)\n',
 
2439
    'bzrlib.repofmt.pack_repo',
 
2440
    'RepositoryFormatKnitPack5',
 
2441
    )
 
2442
format_registry.register_lazy(
 
2443
    'Bazaar RepositoryFormatKnitPack5RichRoot (bzr 1.6.1)\n',
 
2444
    'bzrlib.repofmt.pack_repo',
 
2445
    'RepositoryFormatKnitPack5RichRoot',
 
2446
    )
 
2447
format_registry.register_lazy(
 
2448
    'Bazaar RepositoryFormatKnitPack5RichRoot (bzr 1.6)\n',
 
2449
    'bzrlib.repofmt.pack_repo',
 
2450
    'RepositoryFormatKnitPack5RichRootBroken',
 
2451
    )
 
2452
format_registry.register_lazy(
 
2453
    'Bazaar RepositoryFormatKnitPack6 (bzr 1.9)\n',
 
2454
    'bzrlib.repofmt.pack_repo',
 
2455
    'RepositoryFormatKnitPack6',
 
2456
    )
 
2457
format_registry.register_lazy(
 
2458
    'Bazaar RepositoryFormatKnitPack6RichRoot (bzr 1.9)\n',
 
2459
    'bzrlib.repofmt.pack_repo',
 
2460
    'RepositoryFormatKnitPack6RichRoot',
 
2461
    )
 
2462
 
 
2463
# Development formats. 
 
2464
# 1.7->1.8 go below here
 
2465
format_registry.register_lazy(
 
2466
    "Bazaar development format 2 (needs bzr.dev from before 1.8)\n",
 
2467
    'bzrlib.repofmt.pack_repo',
 
2468
    'RepositoryFormatPackDevelopment2',
 
2469
    )
 
2470
format_registry.register_lazy(
 
2471
    ("Bazaar development format 2 with subtree support "
 
2472
        "(needs bzr.dev from before 1.8)\n"),
 
2473
    'bzrlib.repofmt.pack_repo',
 
2474
    'RepositoryFormatPackDevelopment2Subtree',
 
2475
    )
 
2476
 
 
2477
 
 
2478
class InterRepository(InterObject):
 
2479
    """This class represents operations taking place between two repositories.
 
2480
 
 
2481
    Its instances have methods like copy_content and fetch, and contain
 
2482
    references to the source and target repositories these operations can be 
 
2483
    carried out on.
 
2484
 
 
2485
    Often we will provide convenience methods on 'repository' which carry out
 
2486
    operations with another repository - they will always forward to
 
2487
    InterRepository.get(other).method_name(parameters).
 
2488
    """
 
2489
 
 
2490
    _walk_to_common_revisions_batch_size = 1
 
2491
    _optimisers = []
 
2492
    """The available optimised InterRepository types."""
 
2493
 
 
2494
    def __init__(self, source, target):
 
2495
        InterObject.__init__(self, source, target)
 
2496
        # These two attributes may be overridden by e.g. InterOtherToRemote to
 
2497
        # provide a faster implementation.
 
2498
        self.target_get_graph = self.target.get_graph
 
2499
        self.target_get_parent_map = self.target.get_parent_map
 
2500
 
 
2501
    def copy_content(self, revision_id=None):
 
2502
        raise NotImplementedError(self.copy_content)
 
2503
 
 
2504
    def fetch(self, revision_id=None, pb=None, find_ghosts=False):
 
2505
        """Fetch the content required to construct revision_id.
 
2506
 
 
2507
        The content is copied from self.source to self.target.
 
2508
 
 
2509
        :param revision_id: if None all content is copied, if NULL_REVISION no
 
2510
                            content is copied.
 
2511
        :param pb: optional progress bar to use for progress reports. If not
 
2512
                   provided a default one will be created.
 
2513
 
 
2514
        :returns: (copied_revision_count, failures).
 
2515
        """
 
2516
        # Normally we should find a specific InterRepository subclass to do
 
2517
        # the fetch; if nothing else then at least InterSameDataRepository.
 
2518
        # If none of them is suitable it looks like fetching is not possible;
 
2519
        # we try to give a good message why.  _assert_same_model will probably
 
2520
        # give a helpful message; otherwise a generic one.
 
2521
        self._assert_same_model(self.source, self.target)
 
2522
        raise errors.IncompatibleRepositories(self.source, self.target,
 
2523
            "no suitableInterRepository found")
 
2524
 
 
2525
    def _walk_to_common_revisions(self, revision_ids):
 
2526
        """Walk out from revision_ids in source to revisions target has.
 
2527
 
 
2528
        :param revision_ids: The start point for the search.
 
2529
        :return: A set of revision ids.
 
2530
        """
 
2531
        target_graph = self.target_get_graph()
 
2532
        revision_ids = frozenset(revision_ids)
 
2533
        # Fast path for the case where all the revisions are already in the
 
2534
        # target repo.
 
2535
        # (Although this does incur an extra round trip for the
 
2536
        # fairly common case where the target doesn't already have the revision
 
2537
        # we're pushing.)
 
2538
        if set(target_graph.get_parent_map(revision_ids)) == revision_ids:
 
2539
            return graph.SearchResult(revision_ids, set(), 0, set())
 
2540
        missing_revs = set()
 
2541
        source_graph = self.source.get_graph()
 
2542
        # ensure we don't pay silly lookup costs.
 
2543
        searcher = source_graph._make_breadth_first_searcher(revision_ids)
 
2544
        null_set = frozenset([_mod_revision.NULL_REVISION])
 
2545
        searcher_exhausted = False
 
2546
        while True:
 
2547
            next_revs = set()
 
2548
            ghosts = set()
 
2549
            # Iterate the searcher until we have enough next_revs
 
2550
            while len(next_revs) < self._walk_to_common_revisions_batch_size:
 
2551
                try:
 
2552
                    next_revs_part, ghosts_part = searcher.next_with_ghosts()
 
2553
                    next_revs.update(next_revs_part)
 
2554
                    ghosts.update(ghosts_part)
 
2555
                except StopIteration:
 
2556
                    searcher_exhausted = True
 
2557
                    break
 
2558
            # If there are ghosts in the source graph, and the caller asked for
 
2559
            # them, make sure that they are present in the target.
 
2560
            # We don't care about other ghosts as we can't fetch them and
 
2561
            # haven't been asked to.
 
2562
            ghosts_to_check = set(revision_ids.intersection(ghosts))
 
2563
            revs_to_get = set(next_revs).union(ghosts_to_check)
 
2564
            if revs_to_get:
 
2565
                have_revs = set(target_graph.get_parent_map(revs_to_get))
 
2566
                # we always have NULL_REVISION present.
 
2567
                have_revs = have_revs.union(null_set)
 
2568
                # Check if the target is missing any ghosts we need.
 
2569
                ghosts_to_check.difference_update(have_revs)
 
2570
                if ghosts_to_check:
 
2571
                    # One of the caller's revision_ids is a ghost in both the
 
2572
                    # source and the target.
 
2573
                    raise errors.NoSuchRevision(
 
2574
                        self.source, ghosts_to_check.pop())
 
2575
                missing_revs.update(next_revs - have_revs)
 
2576
                # Because we may have walked past the original stop point, make
 
2577
                # sure everything is stopped
 
2578
                stop_revs = searcher.find_seen_ancestors(have_revs)
 
2579
                searcher.stop_searching_any(stop_revs)
 
2580
            if searcher_exhausted:
 
2581
                break
 
2582
        return searcher.get_result()
 
2583
 
 
2584
    @deprecated_method(one_two)
 
2585
    @needs_read_lock
 
2586
    def missing_revision_ids(self, revision_id=None, find_ghosts=True):
 
2587
        """Return the revision ids that source has that target does not.
 
2588
        
 
2589
        These are returned in topological order.
 
2590
 
 
2591
        :param revision_id: only return revision ids included by this
 
2592
                            revision_id.
 
2593
        :param find_ghosts: If True find missing revisions in deep history
 
2594
            rather than just finding the surface difference.
 
2595
        """
 
2596
        return list(self.search_missing_revision_ids(
 
2597
            revision_id, find_ghosts).get_keys())
 
2598
 
 
2599
    @needs_read_lock
 
2600
    def search_missing_revision_ids(self, revision_id=None, find_ghosts=True):
 
2601
        """Return the revision ids that source has that target does not.
 
2602
        
 
2603
        :param revision_id: only return revision ids included by this
 
2604
                            revision_id.
 
2605
        :param find_ghosts: If True find missing revisions in deep history
 
2606
            rather than just finding the surface difference.
 
2607
        :return: A bzrlib.graph.SearchResult.
 
2608
        """
 
2609
        # stop searching at found target revisions.
 
2610
        if not find_ghosts and revision_id is not None:
 
2611
            return self._walk_to_common_revisions([revision_id])
 
2612
        # generic, possibly worst case, slow code path.
 
2613
        target_ids = set(self.target.all_revision_ids())
 
2614
        if revision_id is not None:
 
2615
            source_ids = self.source.get_ancestry(revision_id)
 
2616
            if source_ids[0] is not None:
 
2617
                raise AssertionError()
 
2618
            source_ids.pop(0)
 
2619
        else:
 
2620
            source_ids = self.source.all_revision_ids()
 
2621
        result_set = set(source_ids).difference(target_ids)
 
2622
        return self.source.revision_ids_to_search_result(result_set)
 
2623
 
 
2624
    @staticmethod
 
2625
    def _same_model(source, target):
 
2626
        """True if source and target have the same data representation.
 
2627
        
 
2628
        Note: this is always called on the base class; overriding it in a
 
2629
        subclass will have no effect.
 
2630
        """
 
2631
        try:
 
2632
            InterRepository._assert_same_model(source, target)
 
2633
            return True
 
2634
        except errors.IncompatibleRepositories, e:
 
2635
            return False
 
2636
 
 
2637
    @staticmethod
 
2638
    def _assert_same_model(source, target):
 
2639
        """Raise an exception if two repositories do not use the same model.
 
2640
        """
 
2641
        if source.supports_rich_root() != target.supports_rich_root():
 
2642
            raise errors.IncompatibleRepositories(source, target,
 
2643
                "different rich-root support")
 
2644
        if source._serializer != target._serializer:
 
2645
            raise errors.IncompatibleRepositories(source, target,
 
2646
                "different serializers")
 
2647
 
 
2648
 
 
2649
class InterSameDataRepository(InterRepository):
 
2650
    """Code for converting between repositories that represent the same data.
 
2651
    
 
2652
    Data format and model must match for this to work.
 
2653
    """
 
2654
 
 
2655
    @classmethod
 
2656
    def _get_repo_format_to_test(self):
 
2657
        """Repository format for testing with.
 
2658
        
 
2659
        InterSameData can pull from subtree to subtree and from non-subtree to
 
2660
        non-subtree, so we test this with the richest repository format.
 
2661
        """
 
2662
        from bzrlib.repofmt import knitrepo
 
2663
        return knitrepo.RepositoryFormatKnit3()
 
2664
 
 
2665
    @staticmethod
 
2666
    def is_compatible(source, target):
 
2667
        return InterRepository._same_model(source, target)
 
2668
 
 
2669
    @needs_write_lock
 
2670
    def copy_content(self, revision_id=None):
 
2671
        """Make a complete copy of the content in self into destination.
 
2672
 
 
2673
        This copies both the repository's revision data, and configuration information
 
2674
        such as the make_working_trees setting.
 
2675
        
 
2676
        This is a destructive operation! Do not use it on existing 
 
2677
        repositories.
 
2678
 
 
2679
        :param revision_id: Only copy the content needed to construct
 
2680
                            revision_id and its parents.
 
2681
        """
 
2682
        try:
 
2683
            self.target.set_make_working_trees(self.source.make_working_trees())
 
2684
        except NotImplementedError:
 
2685
            pass
 
2686
        # but don't bother fetching if we have the needed data now.
 
2687
        if (revision_id not in (None, _mod_revision.NULL_REVISION) and 
 
2688
            self.target.has_revision(revision_id)):
 
2689
            return
 
2690
        self.target.fetch(self.source, revision_id=revision_id)
 
2691
 
 
2692
    @needs_write_lock
 
2693
    def fetch(self, revision_id=None, pb=None, find_ghosts=False):
 
2694
        """See InterRepository.fetch()."""
 
2695
        from bzrlib.fetch import RepoFetcher
 
2696
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
 
2697
               self.source, self.source._format, self.target,
 
2698
               self.target._format)
 
2699
        f = RepoFetcher(to_repository=self.target,
 
2700
                               from_repository=self.source,
 
2701
                               last_revision=revision_id,
 
2702
                               pb=pb, find_ghosts=find_ghosts)
 
2703
        return f.count_copied, f.failed_revisions
 
2704
 
 
2705
 
 
2706
class InterWeaveRepo(InterSameDataRepository):
 
2707
    """Optimised code paths between Weave based repositories.
 
2708
    
 
2709
    This should be in bzrlib/repofmt/weaverepo.py but we have not yet
 
2710
    implemented lazy inter-object optimisation.
 
2711
    """
 
2712
 
 
2713
    @classmethod
 
2714
    def _get_repo_format_to_test(self):
 
2715
        from bzrlib.repofmt import weaverepo
 
2716
        return weaverepo.RepositoryFormat7()
 
2717
 
 
2718
    @staticmethod
 
2719
    def is_compatible(source, target):
 
2720
        """Be compatible with known Weave formats.
 
2721
        
 
2722
        We don't test for the stores being of specific types because that
 
2723
        could lead to confusing results, and there is no need to be 
 
2724
        overly general.
 
2725
        """
 
2726
        from bzrlib.repofmt.weaverepo import (
 
2727
                RepositoryFormat5,
 
2728
                RepositoryFormat6,
 
2729
                RepositoryFormat7,
 
2730
                )
 
2731
        try:
 
2732
            return (isinstance(source._format, (RepositoryFormat5,
 
2733
                                                RepositoryFormat6,
 
2734
                                                RepositoryFormat7)) and
 
2735
                    isinstance(target._format, (RepositoryFormat5,
 
2736
                                                RepositoryFormat6,
 
2737
                                                RepositoryFormat7)))
 
2738
        except AttributeError:
 
2739
            return False
 
2740
    
 
2741
    @needs_write_lock
 
2742
    def copy_content(self, revision_id=None):
 
2743
        """See InterRepository.copy_content()."""
 
2744
        # weave specific optimised path:
 
2745
        try:
 
2746
            self.target.set_make_working_trees(self.source.make_working_trees())
 
2747
        except (errors.RepositoryUpgradeRequired, NotImplemented):
 
2748
            pass
 
2749
        # FIXME do not peek!
 
2750
        if self.source._transport.listable():
 
2751
            pb = ui.ui_factory.nested_progress_bar()
 
2752
            try:
 
2753
                self.target.texts.insert_record_stream(
 
2754
                    self.source.texts.get_record_stream(
 
2755
                        self.source.texts.keys(), 'topological', False))
 
2756
                pb.update('copying inventory', 0, 1)
 
2757
                self.target.inventories.insert_record_stream(
 
2758
                    self.source.inventories.get_record_stream(
 
2759
                        self.source.inventories.keys(), 'topological', False))
 
2760
                self.target.signatures.insert_record_stream(
 
2761
                    self.source.signatures.get_record_stream(
 
2762
                        self.source.signatures.keys(),
 
2763
                        'unordered', True))
 
2764
                self.target.revisions.insert_record_stream(
 
2765
                    self.source.revisions.get_record_stream(
 
2766
                        self.source.revisions.keys(),
 
2767
                        'topological', True))
 
2768
            finally:
 
2769
                pb.finished()
 
2770
        else:
 
2771
            self.target.fetch(self.source, revision_id=revision_id)
 
2772
 
 
2773
    @needs_write_lock
 
2774
    def fetch(self, revision_id=None, pb=None, find_ghosts=False):
 
2775
        """See InterRepository.fetch()."""
 
2776
        from bzrlib.fetch import RepoFetcher
 
2777
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
 
2778
               self.source, self.source._format, self.target, self.target._format)
 
2779
        f = RepoFetcher(to_repository=self.target,
 
2780
                               from_repository=self.source,
 
2781
                               last_revision=revision_id,
 
2782
                               pb=pb, find_ghosts=find_ghosts)
 
2783
        return f.count_copied, f.failed_revisions
 
2784
 
 
2785
    @needs_read_lock
 
2786
    def search_missing_revision_ids(self, revision_id=None, find_ghosts=True):
 
2787
        """See InterRepository.missing_revision_ids()."""
 
2788
        # we want all revisions to satisfy revision_id in source.
 
2789
        # but we don't want to stat every file here and there.
 
2790
        # we want then, all revisions other needs to satisfy revision_id 
 
2791
        # checked, but not those that we have locally.
 
2792
        # so the first thing is to get a subset of the revisions to 
 
2793
        # satisfy revision_id in source, and then eliminate those that
 
2794
        # we do already have. 
 
2795
        # this is slow on high latency connection to self, but as as this
 
2796
        # disk format scales terribly for push anyway due to rewriting 
 
2797
        # inventory.weave, this is considered acceptable.
 
2798
        # - RBC 20060209
 
2799
        if revision_id is not None:
 
2800
            source_ids = self.source.get_ancestry(revision_id)
 
2801
            if source_ids[0] is not None:
 
2802
                raise AssertionError()
 
2803
            source_ids.pop(0)
 
2804
        else:
 
2805
            source_ids = self.source._all_possible_ids()
 
2806
        source_ids_set = set(source_ids)
 
2807
        # source_ids is the worst possible case we may need to pull.
 
2808
        # now we want to filter source_ids against what we actually
 
2809
        # have in target, but don't try to check for existence where we know
 
2810
        # we do not have a revision as that would be pointless.
 
2811
        target_ids = set(self.target._all_possible_ids())
 
2812
        possibly_present_revisions = target_ids.intersection(source_ids_set)
 
2813
        actually_present_revisions = set(
 
2814
            self.target._eliminate_revisions_not_present(possibly_present_revisions))
 
2815
        required_revisions = source_ids_set.difference(actually_present_revisions)
 
2816
        if revision_id is not None:
 
2817
            # we used get_ancestry to determine source_ids then we are assured all
 
2818
            # revisions referenced are present as they are installed in topological order.
 
2819
            # and the tip revision was validated by get_ancestry.
 
2820
            result_set = required_revisions
 
2821
        else:
 
2822
            # if we just grabbed the possibly available ids, then 
 
2823
            # we only have an estimate of whats available and need to validate
 
2824
            # that against the revision records.
 
2825
            result_set = set(
 
2826
                self.source._eliminate_revisions_not_present(required_revisions))
 
2827
        return self.source.revision_ids_to_search_result(result_set)
 
2828
 
 
2829
 
 
2830
class InterKnitRepo(InterSameDataRepository):
 
2831
    """Optimised code paths between Knit based repositories."""
 
2832
 
 
2833
    @classmethod
 
2834
    def _get_repo_format_to_test(self):
 
2835
        from bzrlib.repofmt import knitrepo
 
2836
        return knitrepo.RepositoryFormatKnit1()
 
2837
 
 
2838
    @staticmethod
 
2839
    def is_compatible(source, target):
 
2840
        """Be compatible with known Knit formats.
 
2841
        
 
2842
        We don't test for the stores being of specific types because that
 
2843
        could lead to confusing results, and there is no need to be 
 
2844
        overly general.
 
2845
        """
 
2846
        from bzrlib.repofmt.knitrepo import RepositoryFormatKnit
 
2847
        try:
 
2848
            are_knits = (isinstance(source._format, RepositoryFormatKnit) and
 
2849
                isinstance(target._format, RepositoryFormatKnit))
 
2850
        except AttributeError:
 
2851
            return False
 
2852
        return are_knits and InterRepository._same_model(source, target)
 
2853
 
 
2854
    @needs_write_lock
 
2855
    def fetch(self, revision_id=None, pb=None, find_ghosts=False):
 
2856
        """See InterRepository.fetch()."""
 
2857
        from bzrlib.fetch import RepoFetcher
 
2858
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
 
2859
               self.source, self.source._format, self.target, self.target._format)
 
2860
        f = RepoFetcher(to_repository=self.target,
 
2861
                            from_repository=self.source,
 
2862
                            last_revision=revision_id,
 
2863
                            pb=pb, find_ghosts=find_ghosts)
 
2864
        return f.count_copied, f.failed_revisions
 
2865
 
 
2866
    @needs_read_lock
 
2867
    def search_missing_revision_ids(self, revision_id=None, find_ghosts=True):
 
2868
        """See InterRepository.missing_revision_ids()."""
 
2869
        if revision_id is not None:
 
2870
            source_ids = self.source.get_ancestry(revision_id)
 
2871
            if source_ids[0] is not None:
 
2872
                raise AssertionError()
 
2873
            source_ids.pop(0)
 
2874
        else:
 
2875
            source_ids = self.source.all_revision_ids()
 
2876
        source_ids_set = set(source_ids)
 
2877
        # source_ids is the worst possible case we may need to pull.
 
2878
        # now we want to filter source_ids against what we actually
 
2879
        # have in target, but don't try to check for existence where we know
 
2880
        # we do not have a revision as that would be pointless.
 
2881
        target_ids = set(self.target.all_revision_ids())
 
2882
        possibly_present_revisions = target_ids.intersection(source_ids_set)
 
2883
        actually_present_revisions = set(
 
2884
            self.target._eliminate_revisions_not_present(possibly_present_revisions))
 
2885
        required_revisions = source_ids_set.difference(actually_present_revisions)
 
2886
        if revision_id is not None:
 
2887
            # we used get_ancestry to determine source_ids then we are assured all
 
2888
            # revisions referenced are present as they are installed in topological order.
 
2889
            # and the tip revision was validated by get_ancestry.
 
2890
            result_set = required_revisions
 
2891
        else:
 
2892
            # if we just grabbed the possibly available ids, then 
 
2893
            # we only have an estimate of whats available and need to validate
 
2894
            # that against the revision records.
 
2895
            result_set = set(
 
2896
                self.source._eliminate_revisions_not_present(required_revisions))
 
2897
        return self.source.revision_ids_to_search_result(result_set)
 
2898
 
 
2899
 
 
2900
class InterPackRepo(InterSameDataRepository):
 
2901
    """Optimised code paths between Pack based repositories."""
 
2902
 
 
2903
    @classmethod
 
2904
    def _get_repo_format_to_test(self):
 
2905
        from bzrlib.repofmt import pack_repo
 
2906
        return pack_repo.RepositoryFormatKnitPack1()
 
2907
 
 
2908
    @staticmethod
 
2909
    def is_compatible(source, target):
 
2910
        """Be compatible with known Pack formats.
 
2911
        
 
2912
        We don't test for the stores being of specific types because that
 
2913
        could lead to confusing results, and there is no need to be 
 
2914
        overly general.
 
2915
        """
 
2916
        from bzrlib.repofmt.pack_repo import RepositoryFormatPack
 
2917
        try:
 
2918
            are_packs = (isinstance(source._format, RepositoryFormatPack) and
 
2919
                isinstance(target._format, RepositoryFormatPack))
 
2920
        except AttributeError:
 
2921
            return False
 
2922
        return are_packs and InterRepository._same_model(source, target)
 
2923
 
 
2924
    @needs_write_lock
 
2925
    def fetch(self, revision_id=None, pb=None, find_ghosts=False):
 
2926
        """See InterRepository.fetch()."""
 
2927
        if (len(self.source._fallback_repositories) > 0 or
 
2928
            len(self.target._fallback_repositories) > 0):
 
2929
            # The pack layer is not aware of fallback repositories, so when
 
2930
            # fetching from a stacked repository or into a stacked repository
 
2931
            # we use the generic fetch logic which uses the VersionedFiles
 
2932
            # attributes on repository.
 
2933
            from bzrlib.fetch import RepoFetcher
 
2934
            # Make sure the generic fetcher sets the write cache size on the
 
2935
            # new pack (just like Packer.pack does) to avoid doing many tiny
 
2936
            # writes (which can be slow over a network connection).
 
2937
            # XXX: ideally the transport layer would do this automatically.
 
2938
            pack_coll = self._get_target_pack_collection()
 
2939
            set_cache_size = (
 
2940
                lambda: pack_coll._new_pack.set_write_cache_size(1024*1024))
 
2941
            fetcher = RepoFetcher(self.target, self.source, revision_id,
 
2942
                                  pb, find_ghosts, set_cache_size)
 
2943
            return fetcher.count_copied, fetcher.failed_revisions
 
2944
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
 
2945
               self.source, self.source._format, self.target, self.target._format)
 
2946
        self.count_copied = 0
 
2947
        if revision_id is None:
 
2948
            # TODO:
 
2949
            # everything to do - use pack logic
 
2950
            # to fetch from all packs to one without
 
2951
            # inventory parsing etc, IFF nothing to be copied is in the target.
 
2952
            # till then:
 
2953
            source_revision_ids = frozenset(self.source.all_revision_ids())
 
2954
            revision_ids = source_revision_ids - \
 
2955
                frozenset(self.target_get_parent_map(source_revision_ids))
 
2956
            revision_keys = [(revid,) for revid in revision_ids]
 
2957
            target_pack_collection = self._get_target_pack_collection()
 
2958
            index = target_pack_collection.revision_index.combined_index
 
2959
            present_revision_ids = set(item[1][0] for item in
 
2960
                index.iter_entries(revision_keys))
 
2961
            revision_ids = set(revision_ids) - present_revision_ids
 
2962
            # implementing the TODO will involve:
 
2963
            # - detecting when all of a pack is selected
 
2964
            # - avoiding as much as possible pre-selection, so the
 
2965
            # more-core routines such as create_pack_from_packs can filter in
 
2966
            # a just-in-time fashion. (though having a HEADS list on a
 
2967
            # repository might make this a lot easier, because we could
 
2968
            # sensibly detect 'new revisions' without doing a full index scan.
 
2969
        elif _mod_revision.is_null(revision_id):
 
2970
            # nothing to do:
 
2971
            return (0, [])
 
2972
        else:
 
2973
            try:
 
2974
                revision_ids = self.search_missing_revision_ids(revision_id,
 
2975
                    find_ghosts=find_ghosts).get_keys()
 
2976
            except errors.NoSuchRevision:
 
2977
                raise errors.InstallFailed([revision_id])
 
2978
            if len(revision_ids) == 0:
 
2979
                return (0, [])
 
2980
        return self._pack(self.source, self.target, revision_ids)
 
2981
 
 
2982
    def _pack(self, source, target, revision_ids):
 
2983
        from bzrlib.repofmt.pack_repo import Packer
 
2984
        target_pack_collection = self._get_target_pack_collection()
 
2985
        packs = source._pack_collection.all_packs()
 
2986
        pack = Packer(target_pack_collection, packs, '.fetch',
 
2987
            revision_ids).pack()
 
2988
        if pack is not None:
 
2989
            target_pack_collection._save_pack_names()
 
2990
            copied_revs = pack.get_revision_count()
 
2991
            # Trigger an autopack. This may duplicate effort as we've just done
 
2992
            # a pack creation, but for now it is simpler to think about as
 
2993
            # 'upload data, then repack if needed'.
 
2994
            self._autopack()
 
2995
            return (copied_revs, [])
 
2996
        else:
 
2997
            return (0, [])
 
2998
 
 
2999
    def _autopack(self):
 
3000
        self.target._pack_collection.autopack()
 
3001
        
 
3002
    def _get_target_pack_collection(self):
 
3003
        return self.target._pack_collection
 
3004
 
 
3005
    @needs_read_lock
 
3006
    def search_missing_revision_ids(self, revision_id=None, find_ghosts=True):
 
3007
        """See InterRepository.missing_revision_ids().
 
3008
        
 
3009
        :param find_ghosts: Find ghosts throughout the ancestry of
 
3010
            revision_id.
 
3011
        """
 
3012
        if not find_ghosts and revision_id is not None:
 
3013
            return self._walk_to_common_revisions([revision_id])
 
3014
        elif revision_id is not None:
 
3015
            # Find ghosts: search for revisions pointing from one repository to
 
3016
            # the other, and vice versa, anywhere in the history of revision_id.
 
3017
            graph = self.target_get_graph(other_repository=self.source)
 
3018
            searcher = graph._make_breadth_first_searcher([revision_id])
 
3019
            found_ids = set()
 
3020
            while True:
 
3021
                try:
 
3022
                    next_revs, ghosts = searcher.next_with_ghosts()
 
3023
                except StopIteration:
 
3024
                    break
 
3025
                if revision_id in ghosts:
 
3026
                    raise errors.NoSuchRevision(self.source, revision_id)
 
3027
                found_ids.update(next_revs)
 
3028
                found_ids.update(ghosts)
 
3029
            found_ids = frozenset(found_ids)
 
3030
            # Double query here: should be able to avoid this by changing the
 
3031
            # graph api further.
 
3032
            result_set = found_ids - frozenset(
 
3033
                self.target_get_parent_map(found_ids))
 
3034
        else:
 
3035
            source_ids = self.source.all_revision_ids()
 
3036
            # source_ids is the worst possible case we may need to pull.
 
3037
            # now we want to filter source_ids against what we actually
 
3038
            # have in target, but don't try to check for existence where we know
 
3039
            # we do not have a revision as that would be pointless.
 
3040
            target_ids = set(self.target.all_revision_ids())
 
3041
            result_set = set(source_ids).difference(target_ids)
 
3042
        return self.source.revision_ids_to_search_result(result_set)
 
3043
 
 
3044
 
 
3045
class InterModel1and2(InterRepository):
 
3046
 
 
3047
    @classmethod
 
3048
    def _get_repo_format_to_test(self):
 
3049
        return None
 
3050
 
 
3051
    @staticmethod
 
3052
    def is_compatible(source, target):
 
3053
        if not source.supports_rich_root() and target.supports_rich_root():
 
3054
            return True
 
3055
        else:
 
3056
            return False
 
3057
 
 
3058
    @needs_write_lock
 
3059
    def fetch(self, revision_id=None, pb=None, find_ghosts=False):
 
3060
        """See InterRepository.fetch()."""
 
3061
        from bzrlib.fetch import Model1toKnit2Fetcher
 
3062
        f = Model1toKnit2Fetcher(to_repository=self.target,
 
3063
                                 from_repository=self.source,
 
3064
                                 last_revision=revision_id,
 
3065
                                 pb=pb, find_ghosts=find_ghosts)
 
3066
        return f.count_copied, f.failed_revisions
 
3067
 
 
3068
    @needs_write_lock
 
3069
    def copy_content(self, revision_id=None):
 
3070
        """Make a complete copy of the content in self into destination.
 
3071
        
 
3072
        This is a destructive operation! Do not use it on existing 
 
3073
        repositories.
 
3074
 
 
3075
        :param revision_id: Only copy the content needed to construct
 
3076
                            revision_id and its parents.
 
3077
        """
 
3078
        try:
 
3079
            self.target.set_make_working_trees(self.source.make_working_trees())
 
3080
        except NotImplementedError:
 
3081
            pass
 
3082
        # but don't bother fetching if we have the needed data now.
 
3083
        if (revision_id not in (None, _mod_revision.NULL_REVISION) and 
 
3084
            self.target.has_revision(revision_id)):
 
3085
            return
 
3086
        self.target.fetch(self.source, revision_id=revision_id)
 
3087
 
 
3088
 
 
3089
class InterKnit1and2(InterKnitRepo):
 
3090
 
 
3091
    @classmethod
 
3092
    def _get_repo_format_to_test(self):
 
3093
        return None
 
3094
 
 
3095
    @staticmethod
 
3096
    def is_compatible(source, target):
 
3097
        """Be compatible with Knit1 source and Knit3 target"""
 
3098
        try:
 
3099
            from bzrlib.repofmt.knitrepo import (
 
3100
                RepositoryFormatKnit1,
 
3101
                RepositoryFormatKnit3,
 
3102
                )
 
3103
            from bzrlib.repofmt.pack_repo import (
 
3104
                RepositoryFormatKnitPack1,
 
3105
                RepositoryFormatKnitPack3,
 
3106
                RepositoryFormatKnitPack4,
 
3107
                RepositoryFormatKnitPack5,
 
3108
                RepositoryFormatKnitPack5RichRoot,
 
3109
                RepositoryFormatKnitPack6,
 
3110
                RepositoryFormatKnitPack6RichRoot,
 
3111
                RepositoryFormatPackDevelopment2,
 
3112
                RepositoryFormatPackDevelopment2Subtree,
 
3113
                )
 
3114
            norichroot = (
 
3115
                RepositoryFormatKnit1,            # no rr, no subtree
 
3116
                RepositoryFormatKnitPack1,        # no rr, no subtree
 
3117
                RepositoryFormatPackDevelopment2, # no rr, no subtree
 
3118
                RepositoryFormatKnitPack5,        # no rr, no subtree
 
3119
                RepositoryFormatKnitPack6,        # no rr, no subtree
 
3120
                )
 
3121
            richroot = (
 
3122
                RepositoryFormatKnit3,            # rr, subtree
 
3123
                RepositoryFormatKnitPack3,        # rr, subtree
 
3124
                RepositoryFormatKnitPack4,        # rr, no subtree
 
3125
                RepositoryFormatKnitPack5RichRoot,# rr, no subtree
 
3126
                RepositoryFormatKnitPack6RichRoot,# rr, no subtree
 
3127
                RepositoryFormatPackDevelopment2Subtree, # rr, subtree
 
3128
                )
 
3129
            for format in norichroot:
 
3130
                if format.rich_root_data:
 
3131
                    raise AssertionError('Format %s is a rich-root format'
 
3132
                        ' but is included in the non-rich-root list'
 
3133
                        % (format,))
 
3134
            for format in richroot:
 
3135
                if not format.rich_root_data:
 
3136
                    raise AssertionError('Format %s is not a rich-root format'
 
3137
                        ' but is included in the rich-root list'
 
3138
                        % (format,))
 
3139
            # TODO: One alternative is to just check format.rich_root_data,
 
3140
            #       instead of keeping membership lists. However, the formats
 
3141
            #       *also* have to use the same 'Knit' style of storage
 
3142
            #       (line-deltas, fulltexts, etc.)
 
3143
            return (isinstance(source._format, norichroot) and
 
3144
                    isinstance(target._format, richroot))
 
3145
        except AttributeError:
 
3146
            return False
 
3147
 
 
3148
    @needs_write_lock
 
3149
    def fetch(self, revision_id=None, pb=None, find_ghosts=False):
 
3150
        """See InterRepository.fetch()."""
 
3151
        from bzrlib.fetch import Knit1to2Fetcher
 
3152
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
 
3153
               self.source, self.source._format, self.target, 
 
3154
               self.target._format)
 
3155
        f = Knit1to2Fetcher(to_repository=self.target,
 
3156
                            from_repository=self.source,
 
3157
                            last_revision=revision_id,
 
3158
                            pb=pb, find_ghosts=find_ghosts)
 
3159
        return f.count_copied, f.failed_revisions
 
3160
 
 
3161
 
 
3162
class InterDifferingSerializer(InterKnitRepo):
 
3163
 
 
3164
    @classmethod
 
3165
    def _get_repo_format_to_test(self):
 
3166
        return None
 
3167
 
 
3168
    @staticmethod
 
3169
    def is_compatible(source, target):
 
3170
        """Be compatible with Knit2 source and Knit3 target"""
 
3171
        if source.supports_rich_root() != target.supports_rich_root():
 
3172
            return False
 
3173
        # Ideally, we'd support fetching if the source had no tree references
 
3174
        # even if it supported them...
 
3175
        if (getattr(source, '_format.supports_tree_reference', False) and
 
3176
            not getattr(target, '_format.supports_tree_reference', False)):
 
3177
            return False
 
3178
        return True
 
3179
 
 
3180
    @needs_write_lock
 
3181
    def fetch(self, revision_id=None, pb=None, find_ghosts=False):
 
3182
        """See InterRepository.fetch()."""
 
3183
        revision_ids = self.target.search_missing_revision_ids(self.source,
 
3184
            revision_id, find_ghosts=find_ghosts).get_keys()
 
3185
        revision_ids = tsort.topo_sort(
 
3186
            self.source.get_graph().get_parent_map(revision_ids))
 
3187
        def revisions_iterator():
 
3188
            rev_ids = list(revision_ids)
 
3189
            for offset in xrange(0, len(rev_ids), 100):
 
3190
                current_revids = rev_ids[offset:offset+100]
 
3191
                revisions = self.source.get_revisions(current_revids)
 
3192
                trees = self.source.revision_trees(current_revids)
 
3193
                keys = [(r,) for r in current_revids]
 
3194
                sig_stream = self.source.signatures.get_record_stream(
 
3195
                    keys, 'unordered', True)
 
3196
                sigs = {}
 
3197
                for record in versionedfile.filter_absent(sig_stream):
 
3198
                    sigs[record.key[0]] = record.get_bytes_as('fulltext')
 
3199
                for rev, tree in zip(revisions, trees):
 
3200
                    yield rev, tree, sigs.get(rev.revision_id, None)
 
3201
        if pb is None:
 
3202
            my_pb = ui.ui_factory.nested_progress_bar()
 
3203
            pb = my_pb
 
3204
        else:
 
3205
            my_pb = None
 
3206
        try:
 
3207
            install_revisions(self.target, revisions_iterator(),
 
3208
                              len(revision_ids), pb)
 
3209
        finally:
 
3210
            if my_pb is not None:
 
3211
                my_pb.finished()
 
3212
        return len(revision_ids), 0
 
3213
 
 
3214
 
 
3215
class InterOtherToRemote(InterRepository):
 
3216
    """An InterRepository that simply delegates to the 'real' InterRepository
 
3217
    calculated for (source, target._real_repository).
 
3218
    """
 
3219
 
 
3220
    _walk_to_common_revisions_batch_size = 50
 
3221
 
 
3222
    def __init__(self, source, target):
 
3223
        InterRepository.__init__(self, source, target)
 
3224
        self._real_inter = None
 
3225
 
 
3226
    @staticmethod
 
3227
    def is_compatible(source, target):
 
3228
        if isinstance(target, remote.RemoteRepository):
 
3229
            return True
 
3230
        return False
 
3231
 
 
3232
    def _ensure_real_inter(self):
 
3233
        if self._real_inter is None:
 
3234
            self.target._ensure_real()
 
3235
            real_target = self.target._real_repository
 
3236
            self._real_inter = InterRepository.get(self.source, real_target)
 
3237
            # Make _real_inter use the RemoteRepository for get_parent_map
 
3238
            self._real_inter.target_get_graph = self.target.get_graph
 
3239
            self._real_inter.target_get_parent_map = self.target.get_parent_map
 
3240
    
 
3241
    def copy_content(self, revision_id=None):
 
3242
        self._ensure_real_inter()
 
3243
        self._real_inter.copy_content(revision_id=revision_id)
 
3244
 
 
3245
    def fetch(self, revision_id=None, pb=None, find_ghosts=False):
 
3246
        self._ensure_real_inter()
 
3247
        return self._real_inter.fetch(revision_id=revision_id, pb=pb,
 
3248
            find_ghosts=find_ghosts)
 
3249
 
 
3250
    @classmethod
 
3251
    def _get_repo_format_to_test(self):
 
3252
        return None
 
3253
 
 
3254
 
 
3255
class InterRemoteToOther(InterRepository):
 
3256
 
 
3257
    def __init__(self, source, target):
 
3258
        InterRepository.__init__(self, source, target)
 
3259
        self._real_inter = None
 
3260
 
 
3261
    @staticmethod
 
3262
    def is_compatible(source, target):
 
3263
        if not isinstance(source, remote.RemoteRepository):
 
3264
            return False
 
3265
        # Is source's model compatible with target's model?
 
3266
        source._ensure_real()
 
3267
        real_source = source._real_repository
 
3268
        if isinstance(real_source, remote.RemoteRepository):
 
3269
            raise NotImplementedError(
 
3270
                "We don't support remote repos backed by remote repos yet.")
 
3271
        return InterRepository._same_model(real_source, target)
 
3272
 
 
3273
    def _ensure_real_inter(self):
 
3274
        if self._real_inter is None:
 
3275
            self.source._ensure_real()
 
3276
            real_source = self.source._real_repository
 
3277
            self._real_inter = InterRepository.get(real_source, self.target)
 
3278
    
 
3279
    def fetch(self, revision_id=None, pb=None, find_ghosts=False):
 
3280
        self._ensure_real_inter()
 
3281
        return self._real_inter.fetch(revision_id=revision_id, pb=pb,
 
3282
            find_ghosts=find_ghosts)
 
3283
 
 
3284
    def copy_content(self, revision_id=None):
 
3285
        self._ensure_real_inter()
 
3286
        self._real_inter.copy_content(revision_id=revision_id)
 
3287
 
 
3288
    @classmethod
 
3289
    def _get_repo_format_to_test(self):
 
3290
        return None
 
3291
 
 
3292
 
 
3293
 
 
3294
class InterPackToRemotePack(InterPackRepo):
 
3295
    """A specialisation of InterPackRepo for a target that is a
 
3296
    RemoteRepository.
 
3297
 
 
3298
    This will use the get_parent_map RPC rather than plain readvs, and also
 
3299
    uses an RPC for autopacking.
 
3300
    """
 
3301
 
 
3302
    _walk_to_common_revisions_batch_size = 50
 
3303
 
 
3304
    @staticmethod
 
3305
    def is_compatible(source, target):
 
3306
        from bzrlib.repofmt.pack_repo import RepositoryFormatPack
 
3307
        if isinstance(source._format, RepositoryFormatPack):
 
3308
            if isinstance(target, remote.RemoteRepository):
 
3309
                target._ensure_real()
 
3310
                if isinstance(target._real_repository._format,
 
3311
                              RepositoryFormatPack):
 
3312
                    if InterRepository._same_model(source, target):
 
3313
                        return True
 
3314
        return False
 
3315
    
 
3316
    def _autopack(self):
 
3317
        self.target.autopack()
 
3318
        
 
3319
    def _get_target_pack_collection(self):
 
3320
        return self.target._real_repository._pack_collection
 
3321
 
 
3322
    @classmethod
 
3323
    def _get_repo_format_to_test(self):
 
3324
        return None
 
3325
 
 
3326
 
 
3327
InterRepository.register_optimiser(InterDifferingSerializer)
 
3328
InterRepository.register_optimiser(InterSameDataRepository)
 
3329
InterRepository.register_optimiser(InterWeaveRepo)
 
3330
InterRepository.register_optimiser(InterKnitRepo)
 
3331
InterRepository.register_optimiser(InterModel1and2)
 
3332
InterRepository.register_optimiser(InterKnit1and2)
 
3333
InterRepository.register_optimiser(InterPackRepo)
 
3334
InterRepository.register_optimiser(InterOtherToRemote)
 
3335
InterRepository.register_optimiser(InterRemoteToOther)
 
3336
InterRepository.register_optimiser(InterPackToRemotePack)
 
3337
 
 
3338
 
 
3339
class CopyConverter(object):
 
3340
    """A repository conversion tool which just performs a copy of the content.
 
3341
    
 
3342
    This is slow but quite reliable.
 
3343
    """
 
3344
 
 
3345
    def __init__(self, target_format):
 
3346
        """Create a CopyConverter.
 
3347
 
 
3348
        :param target_format: The format the resulting repository should be.
 
3349
        """
 
3350
        self.target_format = target_format
 
3351
        
 
3352
    def convert(self, repo, pb):
 
3353
        """Perform the conversion of to_convert, giving feedback via pb.
 
3354
 
 
3355
        :param to_convert: The disk object to convert.
 
3356
        :param pb: a progress bar to use for progress information.
 
3357
        """
 
3358
        self.pb = pb
 
3359
        self.count = 0
 
3360
        self.total = 4
 
3361
        # this is only useful with metadir layouts - separated repo content.
 
3362
        # trigger an assertion if not such
 
3363
        repo._format.get_format_string()
 
3364
        self.repo_dir = repo.bzrdir
 
3365
        self.step('Moving repository to repository.backup')
 
3366
        self.repo_dir.transport.move('repository', 'repository.backup')
 
3367
        backup_transport =  self.repo_dir.transport.clone('repository.backup')
 
3368
        repo._format.check_conversion_target(self.target_format)
 
3369
        self.source_repo = repo._format.open(self.repo_dir,
 
3370
            _found=True,
 
3371
            _override_transport=backup_transport)
 
3372
        self.step('Creating new repository')
 
3373
        converted = self.target_format.initialize(self.repo_dir,
 
3374
                                                  self.source_repo.is_shared())
 
3375
        converted.lock_write()
 
3376
        try:
 
3377
            self.step('Copying content into repository.')
 
3378
            self.source_repo.copy_content_into(converted)
 
3379
        finally:
 
3380
            converted.unlock()
 
3381
        self.step('Deleting old repository content.')
 
3382
        self.repo_dir.transport.delete_tree('repository.backup')
 
3383
        self.pb.note('repository converted')
 
3384
 
 
3385
    def step(self, message):
 
3386
        """Update the pb by a step."""
 
3387
        self.count +=1
 
3388
        self.pb.update(message, self.count, self.total)
 
3389
 
 
3390
 
 
3391
_unescape_map = {
 
3392
    'apos':"'",
 
3393
    'quot':'"',
 
3394
    'amp':'&',
 
3395
    'lt':'<',
 
3396
    'gt':'>'
 
3397
}
 
3398
 
 
3399
 
 
3400
def _unescaper(match, _map=_unescape_map):
 
3401
    code = match.group(1)
 
3402
    try:
 
3403
        return _map[code]
 
3404
    except KeyError:
 
3405
        if not code.startswith('#'):
 
3406
            raise
 
3407
        return unichr(int(code[1:])).encode('utf8')
 
3408
 
 
3409
 
 
3410
_unescape_re = None
 
3411
 
 
3412
 
 
3413
def _unescape_xml(data):
 
3414
    """Unescape predefined XML entities in a string of data."""
 
3415
    global _unescape_re
 
3416
    if _unescape_re is None:
 
3417
        _unescape_re = re.compile('\&([^;]*);')
 
3418
    return _unescape_re.sub(_unescaper, data)
 
3419
 
 
3420
 
 
3421
class _VersionedFileChecker(object):
 
3422
 
 
3423
    def __init__(self, repository):
 
3424
        self.repository = repository
 
3425
        self.text_index = self.repository._generate_text_key_index()
 
3426
    
 
3427
    def calculate_file_version_parents(self, text_key):
 
3428
        """Calculate the correct parents for a file version according to
 
3429
        the inventories.
 
3430
        """
 
3431
        parent_keys = self.text_index[text_key]
 
3432
        if parent_keys == [_mod_revision.NULL_REVISION]:
 
3433
            return ()
 
3434
        return tuple(parent_keys)
 
3435
 
 
3436
    def check_file_version_parents(self, texts, progress_bar=None):
 
3437
        """Check the parents stored in a versioned file are correct.
 
3438
 
 
3439
        It also detects file versions that are not referenced by their
 
3440
        corresponding revision's inventory.
 
3441
 
 
3442
        :returns: A tuple of (wrong_parents, dangling_file_versions).
 
3443
            wrong_parents is a dict mapping {revision_id: (stored_parents,
 
3444
            correct_parents)} for each revision_id where the stored parents
 
3445
            are not correct.  dangling_file_versions is a set of (file_id,
 
3446
            revision_id) tuples for versions that are present in this versioned
 
3447
            file, but not used by the corresponding inventory.
 
3448
        """
 
3449
        wrong_parents = {}
 
3450
        self.file_ids = set([file_id for file_id, _ in
 
3451
            self.text_index.iterkeys()])
 
3452
        # text keys is now grouped by file_id
 
3453
        n_weaves = len(self.file_ids)
 
3454
        files_in_revisions = {}
 
3455
        revisions_of_files = {}
 
3456
        n_versions = len(self.text_index)
 
3457
        progress_bar.update('loading text store', 0, n_versions)
 
3458
        parent_map = self.repository.texts.get_parent_map(self.text_index)
 
3459
        # On unlistable transports this could well be empty/error...
 
3460
        text_keys = self.repository.texts.keys()
 
3461
        unused_keys = frozenset(text_keys) - set(self.text_index)
 
3462
        for num, key in enumerate(self.text_index.iterkeys()):
 
3463
            if progress_bar is not None:
 
3464
                progress_bar.update('checking text graph', num, n_versions)
 
3465
            correct_parents = self.calculate_file_version_parents(key)
 
3466
            try:
 
3467
                knit_parents = parent_map[key]
 
3468
            except errors.RevisionNotPresent:
 
3469
                # Missing text!
 
3470
                knit_parents = None
 
3471
            if correct_parents != knit_parents:
 
3472
                wrong_parents[key] = (knit_parents, correct_parents)
 
3473
        return wrong_parents, unused_keys
 
3474
 
 
3475
 
 
3476
def _old_get_graph(repository, revision_id):
 
3477
    """DO NOT USE. That is all. I'm serious."""
 
3478
    graph = repository.get_graph()
 
3479
    revision_graph = dict(((key, value) for key, value in
 
3480
        graph.iter_ancestry([revision_id]) if value is not None))
 
3481
    return _strip_NULL_ghosts(revision_graph)
 
3482
 
 
3483
 
 
3484
def _strip_NULL_ghosts(revision_graph):
 
3485
    """Also don't use this. more compatibility code for unmigrated clients."""
 
3486
    # Filter ghosts, and null:
 
3487
    if _mod_revision.NULL_REVISION in revision_graph:
 
3488
        del revision_graph[_mod_revision.NULL_REVISION]
 
3489
    for key, parents in revision_graph.items():
 
3490
        revision_graph[key] = tuple(parent for parent in parents if parent
 
3491
            in revision_graph)
 
3492
    return revision_graph