/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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-03-23 01:17:27 UTC
  • mfrom: (4165.3.2 ec2-ubuntu)
  • Revision ID: pqm@pqm.ubuntu.com-20090323011727-b4nl10tcxfo4jiwb
(robertc) Various tweaks to ec2 stuff,
        and make it less windows only. (Robert Collins)

Show diffs side-by-side

added added

removed removed

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