/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/vf_repository.py

  • Committer: Jelmer Vernooij
  • Date: 2011-05-15 17:58:19 UTC
  • mto: This revision was merged to the branch mainline in revision 5887.
  • Revision ID: jelmer@samba.org-20110515175819-0wakfnxgxvzfryl1
Move interrepository implementation to vf_repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Repository formats built around versioned files."""
 
18
 
 
19
 
 
20
from bzrlib.lazy_import import lazy_import
 
21
lazy_import(globals(), """
 
22
from bzrlib import (
 
23
    check,
 
24
    debug,
 
25
    fetch as _mod_fetch,
 
26
    fifo_cache,
 
27
    gpg,
 
28
    graph,
 
29
    inventory_delta,
 
30
    lru_cache,
 
31
    osutils,
 
32
    revision as _mod_revision,
 
33
    serializer as _mod_serializer,
 
34
    static_tuple,
 
35
    symbol_versioning,
 
36
    tsort,
 
37
    ui,
 
38
    versionedfile,
 
39
    )
 
40
 
 
41
from bzrlib.recordcounter import RecordCounter
 
42
from bzrlib.revisiontree import InventoryRevisionTree
 
43
from bzrlib.testament import Testament
 
44
""")
 
45
 
 
46
from bzrlib import (
 
47
    errors,
 
48
    )
 
49
from bzrlib.decorators import (
 
50
    needs_read_lock,
 
51
    needs_write_lock,
 
52
    only_raises,
 
53
    )
 
54
from bzrlib.inventory import (
 
55
    Inventory,
 
56
    InventoryDirectory,
 
57
    ROOT_ID,
 
58
    entry_factory,
 
59
    )
 
60
 
 
61
from bzrlib.repository import (
 
62
    CommitBuilder,
 
63
    InterRepository,
 
64
    MetaDirRepository,
 
65
    MetaDirRepositoryFormat,
 
66
    Repository,
 
67
    RepositoryFormat,
 
68
    )
 
69
 
 
70
from bzrlib.trace import (
 
71
    mutter,
 
72
    )
 
73
 
 
74
 
 
75
class VersionedFileRepositoryFormat(RepositoryFormat):
 
76
    """Base class for all repository formats that are VersionedFiles-based."""
 
77
 
 
78
    supports_full_versioned_files = True
 
79
 
 
80
    # Should commit add an inventory, or an inventory delta to the repository.
 
81
    _commit_inv_deltas = True
 
82
    # What order should fetch operations request streams in?
 
83
    # The default is unordered as that is the cheapest for an origin to
 
84
    # provide.
 
85
    _fetch_order = 'unordered'
 
86
    # Does this repository format use deltas that can be fetched as-deltas ?
 
87
    # (E.g. knits, where the knit deltas can be transplanted intact.
 
88
    # We default to False, which will ensure that enough data to get
 
89
    # a full text out of any fetch stream will be grabbed.
 
90
    _fetch_uses_deltas = False
 
91
 
 
92
 
 
93
class VersionedFileCommitBuilder(CommitBuilder):
 
94
    """Commit builder implementation for versioned files based repositories.
 
95
    """
 
96
 
 
97
    # this commit builder supports the record_entry_contents interface
 
98
    supports_record_entry_contents = True
 
99
 
 
100
    # the default CommitBuilder does not manage trees whose root is versioned.
 
101
    _versioned_root = False
 
102
 
 
103
    def __init__(self, repository, parents, config, timestamp=None,
 
104
                 timezone=None, committer=None, revprops=None,
 
105
                 revision_id=None, lossy=False):
 
106
        super(VersionedFileCommitBuilder, self).__init__(repository,
 
107
            parents, config, timestamp, timezone, committer, revprops,
 
108
            revision_id, lossy)
 
109
        try:
 
110
            basis_id = self.parents[0]
 
111
        except IndexError:
 
112
            basis_id = _mod_revision.NULL_REVISION
 
113
        self.basis_delta_revision = basis_id
 
114
        self.new_inventory = Inventory(None)
 
115
        self._basis_delta = []
 
116
        self.__heads = graph.HeadsCache(repository.get_graph()).heads
 
117
        # memo'd check for no-op commits.
 
118
        self._any_changes = False
 
119
        # API compatibility, older code that used CommitBuilder did not call
 
120
        # .record_delete(), which means the delta that is computed would not be
 
121
        # valid. Callers that will call record_delete() should call
 
122
        # .will_record_deletes() to indicate that.
 
123
        self._recording_deletes = False
 
124
 
 
125
    def will_record_deletes(self):
 
126
        """Tell the commit builder that deletes are being notified.
 
127
 
 
128
        This enables the accumulation of an inventory delta; for the resulting
 
129
        commit to be valid, deletes against the basis MUST be recorded via
 
130
        builder.record_delete().
 
131
        """
 
132
        self._recording_deletes = True
 
133
 
 
134
    def any_changes(self):
 
135
        """Return True if any entries were changed.
 
136
 
 
137
        This includes merge-only changes. It is the core for the --unchanged
 
138
        detection in commit.
 
139
 
 
140
        :return: True if any changes have occured.
 
141
        """
 
142
        return self._any_changes
 
143
 
 
144
    def _ensure_fallback_inventories(self):
 
145
        """Ensure that appropriate inventories are available.
 
146
 
 
147
        This only applies to repositories that are stacked, and is about
 
148
        enusring the stacking invariants. Namely, that for any revision that is
 
149
        present, we either have all of the file content, or we have the parent
 
150
        inventory and the delta file content.
 
151
        """
 
152
        if not self.repository._fallback_repositories:
 
153
            return
 
154
        if not self.repository._format.supports_chks:
 
155
            raise errors.BzrError("Cannot commit directly to a stacked branch"
 
156
                " in pre-2a formats. See "
 
157
                "https://bugs.launchpad.net/bzr/+bug/375013 for details.")
 
158
        # This is a stacked repo, we need to make sure we have the parent
 
159
        # inventories for the parents.
 
160
        parent_keys = [(p,) for p in self.parents]
 
161
        parent_map = self.repository.inventories._index.get_parent_map(parent_keys)
 
162
        missing_parent_keys = set([pk for pk in parent_keys
 
163
                                       if pk not in parent_map])
 
164
        fallback_repos = list(reversed(self.repository._fallback_repositories))
 
165
        missing_keys = [('inventories', pk[0])
 
166
                        for pk in missing_parent_keys]
 
167
        resume_tokens = []
 
168
        while missing_keys and fallback_repos:
 
169
            fallback_repo = fallback_repos.pop()
 
170
            source = fallback_repo._get_source(self.repository._format)
 
171
            sink = self.repository._get_sink()
 
172
            stream = source.get_stream_for_missing_keys(missing_keys)
 
173
            missing_keys = sink.insert_stream_without_locking(stream,
 
174
                self.repository._format)
 
175
        if missing_keys:
 
176
            raise errors.BzrError('Unable to fill in parent inventories for a'
 
177
                                  ' stacked branch')
 
178
 
 
179
    def commit(self, message):
 
180
        """Make the actual commit.
 
181
 
 
182
        :return: The revision id of the recorded revision.
 
183
        """
 
184
        self._validate_unicode_text(message, 'commit message')
 
185
        rev = _mod_revision.Revision(
 
186
                       timestamp=self._timestamp,
 
187
                       timezone=self._timezone,
 
188
                       committer=self._committer,
 
189
                       message=message,
 
190
                       inventory_sha1=self.inv_sha1,
 
191
                       revision_id=self._new_revision_id,
 
192
                       properties=self._revprops)
 
193
        rev.parent_ids = self.parents
 
194
        self.repository.add_revision(self._new_revision_id, rev,
 
195
            self.new_inventory, self._config)
 
196
        self._ensure_fallback_inventories()
 
197
        self.repository.commit_write_group()
 
198
        return self._new_revision_id
 
199
 
 
200
    def abort(self):
 
201
        """Abort the commit that is being built.
 
202
        """
 
203
        self.repository.abort_write_group()
 
204
 
 
205
    def revision_tree(self):
 
206
        """Return the tree that was just committed.
 
207
 
 
208
        After calling commit() this can be called to get a
 
209
        RevisionTree representing the newly committed tree. This is
 
210
        preferred to calling Repository.revision_tree() because that may
 
211
        require deserializing the inventory, while we already have a copy in
 
212
        memory.
 
213
        """
 
214
        if self.new_inventory is None:
 
215
            self.new_inventory = self.repository.get_inventory(
 
216
                self._new_revision_id)
 
217
        return InventoryRevisionTree(self.repository, self.new_inventory,
 
218
            self._new_revision_id)
 
219
 
 
220
    def finish_inventory(self):
 
221
        """Tell the builder that the inventory is finished.
 
222
 
 
223
        :return: The inventory id in the repository, which can be used with
 
224
            repository.get_inventory.
 
225
        """
 
226
        if self.new_inventory is None:
 
227
            # an inventory delta was accumulated without creating a new
 
228
            # inventory.
 
229
            basis_id = self.basis_delta_revision
 
230
            # We ignore the 'inventory' returned by add_inventory_by_delta
 
231
            # because self.new_inventory is used to hint to the rest of the
 
232
            # system what code path was taken
 
233
            self.inv_sha1, _ = self.repository.add_inventory_by_delta(
 
234
                basis_id, self._basis_delta, self._new_revision_id,
 
235
                self.parents)
 
236
        else:
 
237
            if self.new_inventory.root is None:
 
238
                raise AssertionError('Root entry should be supplied to'
 
239
                    ' record_entry_contents, as of bzr 0.10.')
 
240
                self.new_inventory.add(InventoryDirectory(ROOT_ID, '', None))
 
241
            self.new_inventory.revision_id = self._new_revision_id
 
242
            self.inv_sha1 = self.repository.add_inventory(
 
243
                self._new_revision_id,
 
244
                self.new_inventory,
 
245
                self.parents
 
246
                )
 
247
        return self._new_revision_id
 
248
 
 
249
    def _check_root(self, ie, parent_invs, tree):
 
250
        """Helper for record_entry_contents.
 
251
 
 
252
        :param ie: An entry being added.
 
253
        :param parent_invs: The inventories of the parent revisions of the
 
254
            commit.
 
255
        :param tree: The tree that is being committed.
 
256
        """
 
257
        # In this revision format, root entries have no knit or weave When
 
258
        # serializing out to disk and back in root.revision is always
 
259
        # _new_revision_id
 
260
        ie.revision = self._new_revision_id
 
261
 
 
262
    def _require_root_change(self, tree):
 
263
        """Enforce an appropriate root object change.
 
264
 
 
265
        This is called once when record_iter_changes is called, if and only if
 
266
        the root was not in the delta calculated by record_iter_changes.
 
267
 
 
268
        :param tree: The tree which is being committed.
 
269
        """
 
270
        if len(self.parents) == 0:
 
271
            raise errors.RootMissing()
 
272
        entry = entry_factory['directory'](tree.path2id(''), '',
 
273
            None)
 
274
        entry.revision = self._new_revision_id
 
275
        self._basis_delta.append(('', '', entry.file_id, entry))
 
276
 
 
277
    def _get_delta(self, ie, basis_inv, path):
 
278
        """Get a delta against the basis inventory for ie."""
 
279
        if ie.file_id not in basis_inv:
 
280
            # add
 
281
            result = (None, path, ie.file_id, ie)
 
282
            self._basis_delta.append(result)
 
283
            return result
 
284
        elif ie != basis_inv[ie.file_id]:
 
285
            # common but altered
 
286
            # TODO: avoid tis id2path call.
 
287
            result = (basis_inv.id2path(ie.file_id), path, ie.file_id, ie)
 
288
            self._basis_delta.append(result)
 
289
            return result
 
290
        else:
 
291
            # common, unaltered
 
292
            return None
 
293
 
 
294
    def _heads(self, file_id, revision_ids):
 
295
        """Calculate the graph heads for revision_ids in the graph of file_id.
 
296
 
 
297
        This can use either a per-file graph or a global revision graph as we
 
298
        have an identity relationship between the two graphs.
 
299
        """
 
300
        return self.__heads(revision_ids)
 
301
 
 
302
    def get_basis_delta(self):
 
303
        """Return the complete inventory delta versus the basis inventory.
 
304
 
 
305
        This has been built up with the calls to record_delete and
 
306
        record_entry_contents. The client must have already called
 
307
        will_record_deletes() to indicate that they will be generating a
 
308
        complete delta.
 
309
 
 
310
        :return: An inventory delta, suitable for use with apply_delta, or
 
311
            Repository.add_inventory_by_delta, etc.
 
312
        """
 
313
        if not self._recording_deletes:
 
314
            raise AssertionError("recording deletes not activated.")
 
315
        return self._basis_delta
 
316
 
 
317
    def record_delete(self, path, file_id):
 
318
        """Record that a delete occured against a basis tree.
 
319
 
 
320
        This is an optional API - when used it adds items to the basis_delta
 
321
        being accumulated by the commit builder. It cannot be called unless the
 
322
        method will_record_deletes() has been called to inform the builder that
 
323
        a delta is being supplied.
 
324
 
 
325
        :param path: The path of the thing deleted.
 
326
        :param file_id: The file id that was deleted.
 
327
        """
 
328
        if not self._recording_deletes:
 
329
            raise AssertionError("recording deletes not activated.")
 
330
        delta = (path, None, file_id, None)
 
331
        self._basis_delta.append(delta)
 
332
        self._any_changes = True
 
333
        return delta
 
334
 
 
335
    def record_entry_contents(self, ie, parent_invs, path, tree,
 
336
        content_summary):
 
337
        """Record the content of ie from tree into the commit if needed.
 
338
 
 
339
        Side effect: sets ie.revision when unchanged
 
340
 
 
341
        :param ie: An inventory entry present in the commit.
 
342
        :param parent_invs: The inventories of the parent revisions of the
 
343
            commit.
 
344
        :param path: The path the entry is at in the tree.
 
345
        :param tree: The tree which contains this entry and should be used to
 
346
            obtain content.
 
347
        :param content_summary: Summary data from the tree about the paths
 
348
            content - stat, length, exec, sha/link target. This is only
 
349
            accessed when the entry has a revision of None - that is when it is
 
350
            a candidate to commit.
 
351
        :return: A tuple (change_delta, version_recorded, fs_hash).
 
352
            change_delta is an inventory_delta change for this entry against
 
353
            the basis tree of the commit, or None if no change occured against
 
354
            the basis tree.
 
355
            version_recorded is True if a new version of the entry has been
 
356
            recorded. For instance, committing a merge where a file was only
 
357
            changed on the other side will return (delta, False).
 
358
            fs_hash is either None, or the hash details for the path (currently
 
359
            a tuple of the contents sha1 and the statvalue returned by
 
360
            tree.get_file_with_stat()).
 
361
        """
 
362
        if self.new_inventory.root is None:
 
363
            if ie.parent_id is not None:
 
364
                raise errors.RootMissing()
 
365
            self._check_root(ie, parent_invs, tree)
 
366
        if ie.revision is None:
 
367
            kind = content_summary[0]
 
368
        else:
 
369
            # ie is carried over from a prior commit
 
370
            kind = ie.kind
 
371
        # XXX: repository specific check for nested tree support goes here - if
 
372
        # the repo doesn't want nested trees we skip it ?
 
373
        if (kind == 'tree-reference' and
 
374
            not self.repository._format.supports_tree_reference):
 
375
            # mismatch between commit builder logic and repository:
 
376
            # this needs the entry creation pushed down into the builder.
 
377
            raise NotImplementedError('Missing repository subtree support.')
 
378
        self.new_inventory.add(ie)
 
379
 
 
380
        # TODO: slow, take it out of the inner loop.
 
381
        try:
 
382
            basis_inv = parent_invs[0]
 
383
        except IndexError:
 
384
            basis_inv = Inventory(root_id=None)
 
385
 
 
386
        # ie.revision is always None if the InventoryEntry is considered
 
387
        # for committing. We may record the previous parents revision if the
 
388
        # content is actually unchanged against a sole head.
 
389
        if ie.revision is not None:
 
390
            if not self._versioned_root and path == '':
 
391
                # repositories that do not version the root set the root's
 
392
                # revision to the new commit even when no change occurs (more
 
393
                # specifically, they do not record a revision on the root; and
 
394
                # the rev id is assigned to the root during deserialisation -
 
395
                # this masks when a change may have occurred against the basis.
 
396
                # To match this we always issue a delta, because the revision
 
397
                # of the root will always be changing.
 
398
                if ie.file_id in basis_inv:
 
399
                    delta = (basis_inv.id2path(ie.file_id), path,
 
400
                        ie.file_id, ie)
 
401
                else:
 
402
                    # add
 
403
                    delta = (None, path, ie.file_id, ie)
 
404
                self._basis_delta.append(delta)
 
405
                return delta, False, None
 
406
            else:
 
407
                # we don't need to commit this, because the caller already
 
408
                # determined that an existing revision of this file is
 
409
                # appropriate. If it's not being considered for committing then
 
410
                # it and all its parents to the root must be unaltered so
 
411
                # no-change against the basis.
 
412
                if ie.revision == self._new_revision_id:
 
413
                    raise AssertionError("Impossible situation, a skipped "
 
414
                        "inventory entry (%r) claims to be modified in this "
 
415
                        "commit (%r).", (ie, self._new_revision_id))
 
416
                return None, False, None
 
417
        # XXX: Friction: parent_candidates should return a list not a dict
 
418
        #      so that we don't have to walk the inventories again.
 
419
        parent_candiate_entries = ie.parent_candidates(parent_invs)
 
420
        head_set = self._heads(ie.file_id, parent_candiate_entries.keys())
 
421
        heads = []
 
422
        for inv in parent_invs:
 
423
            if ie.file_id in inv:
 
424
                old_rev = inv[ie.file_id].revision
 
425
                if old_rev in head_set:
 
426
                    heads.append(inv[ie.file_id].revision)
 
427
                    head_set.remove(inv[ie.file_id].revision)
 
428
 
 
429
        store = False
 
430
        # now we check to see if we need to write a new record to the
 
431
        # file-graph.
 
432
        # We write a new entry unless there is one head to the ancestors, and
 
433
        # the kind-derived content is unchanged.
 
434
 
 
435
        # Cheapest check first: no ancestors, or more the one head in the
 
436
        # ancestors, we write a new node.
 
437
        if len(heads) != 1:
 
438
            store = True
 
439
        if not store:
 
440
            # There is a single head, look it up for comparison
 
441
            parent_entry = parent_candiate_entries[heads[0]]
 
442
            # if the non-content specific data has changed, we'll be writing a
 
443
            # node:
 
444
            if (parent_entry.parent_id != ie.parent_id or
 
445
                parent_entry.name != ie.name):
 
446
                store = True
 
447
        # now we need to do content specific checks:
 
448
        if not store:
 
449
            # if the kind changed the content obviously has
 
450
            if kind != parent_entry.kind:
 
451
                store = True
 
452
        # Stat cache fingerprint feedback for the caller - None as we usually
 
453
        # don't generate one.
 
454
        fingerprint = None
 
455
        if kind == 'file':
 
456
            if content_summary[2] is None:
 
457
                raise ValueError("Files must not have executable = None")
 
458
            if not store:
 
459
                # We can't trust a check of the file length because of content
 
460
                # filtering...
 
461
                if (# if the exec bit has changed we have to store:
 
462
                    parent_entry.executable != content_summary[2]):
 
463
                    store = True
 
464
                elif parent_entry.text_sha1 == content_summary[3]:
 
465
                    # all meta and content is unchanged (using a hash cache
 
466
                    # hit to check the sha)
 
467
                    ie.revision = parent_entry.revision
 
468
                    ie.text_size = parent_entry.text_size
 
469
                    ie.text_sha1 = parent_entry.text_sha1
 
470
                    ie.executable = parent_entry.executable
 
471
                    return self._get_delta(ie, basis_inv, path), False, None
 
472
                else:
 
473
                    # Either there is only a hash change(no hash cache entry,
 
474
                    # or same size content change), or there is no change on
 
475
                    # this file at all.
 
476
                    # Provide the parent's hash to the store layer, so that the
 
477
                    # content is unchanged we will not store a new node.
 
478
                    nostore_sha = parent_entry.text_sha1
 
479
            if store:
 
480
                # We want to record a new node regardless of the presence or
 
481
                # absence of a content change in the file.
 
482
                nostore_sha = None
 
483
            ie.executable = content_summary[2]
 
484
            file_obj, stat_value = tree.get_file_with_stat(ie.file_id, path)
 
485
            try:
 
486
                text = file_obj.read()
 
487
            finally:
 
488
                file_obj.close()
 
489
            try:
 
490
                ie.text_sha1, ie.text_size = self._add_text_to_weave(
 
491
                    ie.file_id, text, heads, nostore_sha)
 
492
                # Let the caller know we generated a stat fingerprint.
 
493
                fingerprint = (ie.text_sha1, stat_value)
 
494
            except errors.ExistingContent:
 
495
                # Turns out that the file content was unchanged, and we were
 
496
                # only going to store a new node if it was changed. Carry over
 
497
                # the entry.
 
498
                ie.revision = parent_entry.revision
 
499
                ie.text_size = parent_entry.text_size
 
500
                ie.text_sha1 = parent_entry.text_sha1
 
501
                ie.executable = parent_entry.executable
 
502
                return self._get_delta(ie, basis_inv, path), False, None
 
503
        elif kind == 'directory':
 
504
            if not store:
 
505
                # all data is meta here, nothing specific to directory, so
 
506
                # carry over:
 
507
                ie.revision = parent_entry.revision
 
508
                return self._get_delta(ie, basis_inv, path), False, None
 
509
            self._add_text_to_weave(ie.file_id, '', heads, None)
 
510
        elif kind == 'symlink':
 
511
            current_link_target = content_summary[3]
 
512
            if not store:
 
513
                # symlink target is not generic metadata, check if it has
 
514
                # changed.
 
515
                if current_link_target != parent_entry.symlink_target:
 
516
                    store = True
 
517
            if not store:
 
518
                # unchanged, carry over.
 
519
                ie.revision = parent_entry.revision
 
520
                ie.symlink_target = parent_entry.symlink_target
 
521
                return self._get_delta(ie, basis_inv, path), False, None
 
522
            ie.symlink_target = current_link_target
 
523
            self._add_text_to_weave(ie.file_id, '', heads, None)
 
524
        elif kind == 'tree-reference':
 
525
            if not store:
 
526
                if content_summary[3] != parent_entry.reference_revision:
 
527
                    store = True
 
528
            if not store:
 
529
                # unchanged, carry over.
 
530
                ie.reference_revision = parent_entry.reference_revision
 
531
                ie.revision = parent_entry.revision
 
532
                return self._get_delta(ie, basis_inv, path), False, None
 
533
            ie.reference_revision = content_summary[3]
 
534
            if ie.reference_revision is None:
 
535
                raise AssertionError("invalid content_summary for nested tree: %r"
 
536
                    % (content_summary,))
 
537
            self._add_text_to_weave(ie.file_id, '', heads, None)
 
538
        else:
 
539
            raise NotImplementedError('unknown kind')
 
540
        ie.revision = self._new_revision_id
 
541
        # The initial commit adds a root directory, but this in itself is not
 
542
        # a worthwhile commit.
 
543
        if (self.basis_delta_revision != _mod_revision.NULL_REVISION or
 
544
            path != ""):
 
545
            self._any_changes = True
 
546
        return self._get_delta(ie, basis_inv, path), True, fingerprint
 
547
 
 
548
    def record_iter_changes(self, tree, basis_revision_id, iter_changes,
 
549
        _entry_factory=entry_factory):
 
550
        """Record a new tree via iter_changes.
 
551
 
 
552
        :param tree: The tree to obtain text contents from for changed objects.
 
553
        :param basis_revision_id: The revision id of the tree the iter_changes
 
554
            has been generated against. Currently assumed to be the same
 
555
            as self.parents[0] - if it is not, errors may occur.
 
556
        :param iter_changes: An iter_changes iterator with the changes to apply
 
557
            to basis_revision_id. The iterator must not include any items with
 
558
            a current kind of None - missing items must be either filtered out
 
559
            or errored-on beefore record_iter_changes sees the item.
 
560
        :param _entry_factory: Private method to bind entry_factory locally for
 
561
            performance.
 
562
        :return: A generator of (file_id, relpath, fs_hash) tuples for use with
 
563
            tree._observed_sha1.
 
564
        """
 
565
        # Create an inventory delta based on deltas between all the parents and
 
566
        # deltas between all the parent inventories. We use inventory delta's 
 
567
        # between the inventory objects because iter_changes masks
 
568
        # last-changed-field only changes.
 
569
        # Working data:
 
570
        # file_id -> change map, change is fileid, paths, changed, versioneds,
 
571
        # parents, names, kinds, executables
 
572
        merged_ids = {}
 
573
        # {file_id -> revision_id -> inventory entry, for entries in parent
 
574
        # trees that are not parents[0]
 
575
        parent_entries = {}
 
576
        ghost_basis = False
 
577
        try:
 
578
            revtrees = list(self.repository.revision_trees(self.parents))
 
579
        except errors.NoSuchRevision:
 
580
            # one or more ghosts, slow path.
 
581
            revtrees = []
 
582
            for revision_id in self.parents:
 
583
                try:
 
584
                    revtrees.append(self.repository.revision_tree(revision_id))
 
585
                except errors.NoSuchRevision:
 
586
                    if not revtrees:
 
587
                        basis_revision_id = _mod_revision.NULL_REVISION
 
588
                        ghost_basis = True
 
589
                    revtrees.append(self.repository.revision_tree(
 
590
                        _mod_revision.NULL_REVISION))
 
591
        # The basis inventory from a repository 
 
592
        if revtrees:
 
593
            basis_inv = revtrees[0].inventory
 
594
        else:
 
595
            basis_inv = self.repository.revision_tree(
 
596
                _mod_revision.NULL_REVISION).inventory
 
597
        if len(self.parents) > 0:
 
598
            if basis_revision_id != self.parents[0] and not ghost_basis:
 
599
                raise Exception(
 
600
                    "arbitrary basis parents not yet supported with merges")
 
601
            for revtree in revtrees[1:]:
 
602
                for change in revtree.inventory._make_delta(basis_inv):
 
603
                    if change[1] is None:
 
604
                        # Not present in this parent.
 
605
                        continue
 
606
                    if change[2] not in merged_ids:
 
607
                        if change[0] is not None:
 
608
                            basis_entry = basis_inv[change[2]]
 
609
                            merged_ids[change[2]] = [
 
610
                                # basis revid
 
611
                                basis_entry.revision,
 
612
                                # new tree revid
 
613
                                change[3].revision]
 
614
                            parent_entries[change[2]] = {
 
615
                                # basis parent
 
616
                                basis_entry.revision:basis_entry,
 
617
                                # this parent 
 
618
                                change[3].revision:change[3],
 
619
                                }
 
620
                        else:
 
621
                            merged_ids[change[2]] = [change[3].revision]
 
622
                            parent_entries[change[2]] = {change[3].revision:change[3]}
 
623
                    else:
 
624
                        merged_ids[change[2]].append(change[3].revision)
 
625
                        parent_entries[change[2]][change[3].revision] = change[3]
 
626
        else:
 
627
            merged_ids = {}
 
628
        # Setup the changes from the tree:
 
629
        # changes maps file_id -> (change, [parent revision_ids])
 
630
        changes= {}
 
631
        for change in iter_changes:
 
632
            # This probably looks up in basis_inv way to much.
 
633
            if change[1][0] is not None:
 
634
                head_candidate = [basis_inv[change[0]].revision]
 
635
            else:
 
636
                head_candidate = []
 
637
            changes[change[0]] = change, merged_ids.get(change[0],
 
638
                head_candidate)
 
639
        unchanged_merged = set(merged_ids) - set(changes)
 
640
        # Extend the changes dict with synthetic changes to record merges of
 
641
        # texts.
 
642
        for file_id in unchanged_merged:
 
643
            # Record a merged version of these items that did not change vs the
 
644
            # basis. This can be either identical parallel changes, or a revert
 
645
            # of a specific file after a merge. The recorded content will be
 
646
            # that of the current tree (which is the same as the basis), but
 
647
            # the per-file graph will reflect a merge.
 
648
            # NB:XXX: We are reconstructing path information we had, this
 
649
            # should be preserved instead.
 
650
            # inv delta  change: (file_id, (path_in_source, path_in_target),
 
651
            #   changed_content, versioned, parent, name, kind,
 
652
            #   executable)
 
653
            try:
 
654
                basis_entry = basis_inv[file_id]
 
655
            except errors.NoSuchId:
 
656
                # a change from basis->some_parents but file_id isn't in basis
 
657
                # so was new in the merge, which means it must have changed
 
658
                # from basis -> current, and as it hasn't the add was reverted
 
659
                # by the user. So we discard this change.
 
660
                pass
 
661
            else:
 
662
                change = (file_id,
 
663
                    (basis_inv.id2path(file_id), tree.id2path(file_id)),
 
664
                    False, (True, True),
 
665
                    (basis_entry.parent_id, basis_entry.parent_id),
 
666
                    (basis_entry.name, basis_entry.name),
 
667
                    (basis_entry.kind, basis_entry.kind),
 
668
                    (basis_entry.executable, basis_entry.executable))
 
669
                changes[file_id] = (change, merged_ids[file_id])
 
670
        # changes contains tuples with the change and a set of inventory
 
671
        # candidates for the file.
 
672
        # inv delta is:
 
673
        # old_path, new_path, file_id, new_inventory_entry
 
674
        seen_root = False # Is the root in the basis delta?
 
675
        inv_delta = self._basis_delta
 
676
        modified_rev = self._new_revision_id
 
677
        for change, head_candidates in changes.values():
 
678
            if change[3][1]: # versioned in target.
 
679
                # Several things may be happening here:
 
680
                # We may have a fork in the per-file graph
 
681
                #  - record a change with the content from tree
 
682
                # We may have a change against < all trees  
 
683
                #  - carry over the tree that hasn't changed
 
684
                # We may have a change against all trees
 
685
                #  - record the change with the content from tree
 
686
                kind = change[6][1]
 
687
                file_id = change[0]
 
688
                entry = _entry_factory[kind](file_id, change[5][1],
 
689
                    change[4][1])
 
690
                head_set = self._heads(change[0], set(head_candidates))
 
691
                heads = []
 
692
                # Preserve ordering.
 
693
                for head_candidate in head_candidates:
 
694
                    if head_candidate in head_set:
 
695
                        heads.append(head_candidate)
 
696
                        head_set.remove(head_candidate)
 
697
                carried_over = False
 
698
                if len(heads) == 1:
 
699
                    # Could be a carry-over situation:
 
700
                    parent_entry_revs = parent_entries.get(file_id, None)
 
701
                    if parent_entry_revs:
 
702
                        parent_entry = parent_entry_revs.get(heads[0], None)
 
703
                    else:
 
704
                        parent_entry = None
 
705
                    if parent_entry is None:
 
706
                        # The parent iter_changes was called against is the one
 
707
                        # that is the per-file head, so any change is relevant
 
708
                        # iter_changes is valid.
 
709
                        carry_over_possible = False
 
710
                    else:
 
711
                        # could be a carry over situation
 
712
                        # A change against the basis may just indicate a merge,
 
713
                        # we need to check the content against the source of the
 
714
                        # merge to determine if it was changed after the merge
 
715
                        # or carried over.
 
716
                        if (parent_entry.kind != entry.kind or
 
717
                            parent_entry.parent_id != entry.parent_id or
 
718
                            parent_entry.name != entry.name):
 
719
                            # Metadata common to all entries has changed
 
720
                            # against per-file parent
 
721
                            carry_over_possible = False
 
722
                        else:
 
723
                            carry_over_possible = True
 
724
                        # per-type checks for changes against the parent_entry
 
725
                        # are done below.
 
726
                else:
 
727
                    # Cannot be a carry-over situation
 
728
                    carry_over_possible = False
 
729
                # Populate the entry in the delta
 
730
                if kind == 'file':
 
731
                    # XXX: There is still a small race here: If someone reverts the content of a file
 
732
                    # after iter_changes examines and decides it has changed,
 
733
                    # we will unconditionally record a new version even if some
 
734
                    # other process reverts it while commit is running (with
 
735
                    # the revert happening after iter_changes did its
 
736
                    # examination).
 
737
                    if change[7][1]:
 
738
                        entry.executable = True
 
739
                    else:
 
740
                        entry.executable = False
 
741
                    if (carry_over_possible and
 
742
                        parent_entry.executable == entry.executable):
 
743
                            # Check the file length, content hash after reading
 
744
                            # the file.
 
745
                            nostore_sha = parent_entry.text_sha1
 
746
                    else:
 
747
                        nostore_sha = None
 
748
                    file_obj, stat_value = tree.get_file_with_stat(file_id, change[1][1])
 
749
                    try:
 
750
                        text = file_obj.read()
 
751
                    finally:
 
752
                        file_obj.close()
 
753
                    try:
 
754
                        entry.text_sha1, entry.text_size = self._add_text_to_weave(
 
755
                            file_id, text, heads, nostore_sha)
 
756
                        yield file_id, change[1][1], (entry.text_sha1, stat_value)
 
757
                    except errors.ExistingContent:
 
758
                        # No content change against a carry_over parent
 
759
                        # Perhaps this should also yield a fs hash update?
 
760
                        carried_over = True
 
761
                        entry.text_size = parent_entry.text_size
 
762
                        entry.text_sha1 = parent_entry.text_sha1
 
763
                elif kind == 'symlink':
 
764
                    # Wants a path hint?
 
765
                    entry.symlink_target = tree.get_symlink_target(file_id)
 
766
                    if (carry_over_possible and
 
767
                        parent_entry.symlink_target == entry.symlink_target):
 
768
                        carried_over = True
 
769
                    else:
 
770
                        self._add_text_to_weave(change[0], '', heads, None)
 
771
                elif kind == 'directory':
 
772
                    if carry_over_possible:
 
773
                        carried_over = True
 
774
                    else:
 
775
                        # Nothing to set on the entry.
 
776
                        # XXX: split into the Root and nonRoot versions.
 
777
                        if change[1][1] != '' or self.repository.supports_rich_root():
 
778
                            self._add_text_to_weave(change[0], '', heads, None)
 
779
                elif kind == 'tree-reference':
 
780
                    if not self.repository._format.supports_tree_reference:
 
781
                        # This isn't quite sane as an error, but we shouldn't
 
782
                        # ever see this code path in practice: tree's don't
 
783
                        # permit references when the repo doesn't support tree
 
784
                        # references.
 
785
                        raise errors.UnsupportedOperation(tree.add_reference,
 
786
                            self.repository)
 
787
                    reference_revision = tree.get_reference_revision(change[0])
 
788
                    entry.reference_revision = reference_revision
 
789
                    if (carry_over_possible and
 
790
                        parent_entry.reference_revision == reference_revision):
 
791
                        carried_over = True
 
792
                    else:
 
793
                        self._add_text_to_weave(change[0], '', heads, None)
 
794
                else:
 
795
                    raise AssertionError('unknown kind %r' % kind)
 
796
                if not carried_over:
 
797
                    entry.revision = modified_rev
 
798
                else:
 
799
                    entry.revision = parent_entry.revision
 
800
            else:
 
801
                entry = None
 
802
            new_path = change[1][1]
 
803
            inv_delta.append((change[1][0], new_path, change[0], entry))
 
804
            if new_path == '':
 
805
                seen_root = True
 
806
        self.new_inventory = None
 
807
        # The initial commit adds a root directory, but this in itself is not
 
808
        # a worthwhile commit.
 
809
        if ((len(inv_delta) > 0 and basis_revision_id != _mod_revision.NULL_REVISION) or
 
810
            (len(inv_delta) > 1 and basis_revision_id == _mod_revision.NULL_REVISION)):
 
811
            # This should perhaps be guarded by a check that the basis we
 
812
            # commit against is the basis for the commit and if not do a delta
 
813
            # against the basis.
 
814
            self._any_changes = True
 
815
        if not seen_root:
 
816
            # housekeeping root entry changes do not affect no-change commits.
 
817
            self._require_root_change(tree)
 
818
        self.basis_delta_revision = basis_revision_id
 
819
 
 
820
    def _add_text_to_weave(self, file_id, new_text, parents, nostore_sha):
 
821
        parent_keys = tuple([(file_id, parent) for parent in parents])
 
822
        return self.repository.texts._add_text(
 
823
            (file_id, self._new_revision_id), parent_keys, new_text,
 
824
            nostore_sha=nostore_sha, random_id=self.random_revid)[0:2]
 
825
 
 
826
 
 
827
class VersionedFileRootCommitBuilder(VersionedFileCommitBuilder):
 
828
    """This commitbuilder actually records the root id"""
 
829
 
 
830
    # the root entry gets versioned properly by this builder.
 
831
    _versioned_root = True
 
832
 
 
833
    def _check_root(self, ie, parent_invs, tree):
 
834
        """Helper for record_entry_contents.
 
835
 
 
836
        :param ie: An entry being added.
 
837
        :param parent_invs: The inventories of the parent revisions of the
 
838
            commit.
 
839
        :param tree: The tree that is being committed.
 
840
        """
 
841
 
 
842
    def _require_root_change(self, tree):
 
843
        """Enforce an appropriate root object change.
 
844
 
 
845
        This is called once when record_iter_changes is called, if and only if
 
846
        the root was not in the delta calculated by record_iter_changes.
 
847
 
 
848
        :param tree: The tree which is being committed.
 
849
        """
 
850
        # versioned roots do not change unless the tree found a change.
 
851
 
 
852
 
 
853
class VersionedFileRepository(Repository):
 
854
    """Repository holding history for one or more branches.
 
855
 
 
856
    The repository holds and retrieves historical information including
 
857
    revisions and file history.  It's normally accessed only by the Branch,
 
858
    which views a particular line of development through that history.
 
859
 
 
860
    The Repository builds on top of some byte storage facilies (the revisions,
 
861
    signatures, inventories, texts and chk_bytes attributes) and a Transport,
 
862
    which respectively provide byte storage and a means to access the (possibly
 
863
    remote) disk.
 
864
 
 
865
    The byte storage facilities are addressed via tuples, which we refer to
 
866
    as 'keys' throughout the code base. Revision_keys, inventory_keys and
 
867
    signature_keys are all 1-tuples: (revision_id,). text_keys are two-tuples:
 
868
    (file_id, revision_id). chk_bytes uses CHK keys - a 1-tuple with a single
 
869
    byte string made up of a hash identifier and a hash value.
 
870
    We use this interface because it allows low friction with the underlying
 
871
    code that implements disk indices, network encoding and other parts of
 
872
    bzrlib.
 
873
 
 
874
    :ivar revisions: A bzrlib.versionedfile.VersionedFiles instance containing
 
875
        the serialised revisions for the repository. This can be used to obtain
 
876
        revision graph information or to access raw serialised revisions.
 
877
        The result of trying to insert data into the repository via this store
 
878
        is undefined: it should be considered read-only except for implementors
 
879
        of repositories.
 
880
    :ivar signatures: A bzrlib.versionedfile.VersionedFiles instance containing
 
881
        the serialised signatures for the repository. This can be used to
 
882
        obtain access to raw serialised signatures.  The result of trying to
 
883
        insert data into the repository via this store is undefined: it should
 
884
        be considered read-only except for implementors of repositories.
 
885
    :ivar inventories: A bzrlib.versionedfile.VersionedFiles instance containing
 
886
        the serialised inventories for the repository. This can be used to
 
887
        obtain unserialised inventories.  The result of trying to insert data
 
888
        into the repository via this store is undefined: it should be
 
889
        considered read-only except for implementors of repositories.
 
890
    :ivar texts: A bzrlib.versionedfile.VersionedFiles instance containing the
 
891
        texts of files and directories for the repository. This can be used to
 
892
        obtain file texts or file graphs. Note that Repository.iter_file_bytes
 
893
        is usually a better interface for accessing file texts.
 
894
        The result of trying to insert data into the repository via this store
 
895
        is undefined: it should be considered read-only except for implementors
 
896
        of repositories.
 
897
    :ivar chk_bytes: A bzrlib.versionedfile.VersionedFiles instance containing
 
898
        any data the repository chooses to store or have indexed by its hash.
 
899
        The result of trying to insert data into the repository via this store
 
900
        is undefined: it should be considered read-only except for implementors
 
901
        of repositories.
 
902
    :ivar _transport: Transport for file access to repository, typically
 
903
        pointing to .bzr/repository.
 
904
    """
 
905
 
 
906
    # What class to use for a CommitBuilder. Often it's simpler to change this
 
907
    # in a Repository class subclass rather than to override
 
908
    # get_commit_builder.
 
909
    _commit_builder_class = VersionedFileCommitBuilder
 
910
 
 
911
    def add_fallback_repository(self, repository):
 
912
        """Add a repository to use for looking up data not held locally.
 
913
 
 
914
        :param repository: A repository.
 
915
        """
 
916
        if not self._format.supports_external_lookups:
 
917
            raise errors.UnstackableRepositoryFormat(self._format, self.base)
 
918
        if self.is_locked():
 
919
            # This repository will call fallback.unlock() when we transition to
 
920
            # the unlocked state, so we make sure to increment the lock count
 
921
            repository.lock_read()
 
922
        self._check_fallback_repository(repository)
 
923
        self._fallback_repositories.append(repository)
 
924
        self.texts.add_fallback_versioned_files(repository.texts)
 
925
        self.inventories.add_fallback_versioned_files(repository.inventories)
 
926
        self.revisions.add_fallback_versioned_files(repository.revisions)
 
927
        self.signatures.add_fallback_versioned_files(repository.signatures)
 
928
        if self.chk_bytes is not None:
 
929
            self.chk_bytes.add_fallback_versioned_files(repository.chk_bytes)
 
930
 
 
931
    @only_raises(errors.LockNotHeld, errors.LockBroken)
 
932
    def unlock(self):
 
933
        super(VersionedFileRepository, self).unlock()
 
934
        if self.control_files._lock_count == 0:
 
935
            self._inventory_entry_cache.clear()
 
936
 
 
937
    def add_inventory(self, revision_id, inv, parents):
 
938
        """Add the inventory inv to the repository as revision_id.
 
939
 
 
940
        :param parents: The revision ids of the parents that revision_id
 
941
                        is known to have and are in the repository already.
 
942
 
 
943
        :returns: The validator(which is a sha1 digest, though what is sha'd is
 
944
            repository format specific) of the serialized inventory.
 
945
        """
 
946
        if not self.is_in_write_group():
 
947
            raise AssertionError("%r not in write group" % (self,))
 
948
        _mod_revision.check_not_reserved_id(revision_id)
 
949
        if not (inv.revision_id is None or inv.revision_id == revision_id):
 
950
            raise AssertionError(
 
951
                "Mismatch between inventory revision"
 
952
                " id and insertion revid (%r, %r)"
 
953
                % (inv.revision_id, revision_id))
 
954
        if inv.root is None:
 
955
            raise errors.RootMissing()
 
956
        return self._add_inventory_checked(revision_id, inv, parents)
 
957
 
 
958
    def _add_inventory_checked(self, revision_id, inv, parents):
 
959
        """Add inv to the repository after checking the inputs.
 
960
 
 
961
        This function can be overridden to allow different inventory styles.
 
962
 
 
963
        :seealso: add_inventory, for the contract.
 
964
        """
 
965
        inv_lines = self._serializer.write_inventory_to_lines(inv)
 
966
        return self._inventory_add_lines(revision_id, parents,
 
967
            inv_lines, check_content=False)
 
968
 
 
969
    def add_inventory_by_delta(self, basis_revision_id, delta, new_revision_id,
 
970
                               parents, basis_inv=None, propagate_caches=False):
 
971
        """Add a new inventory expressed as a delta against another revision.
 
972
 
 
973
        See the inventory developers documentation for the theory behind
 
974
        inventory deltas.
 
975
 
 
976
        :param basis_revision_id: The inventory id the delta was created
 
977
            against. (This does not have to be a direct parent.)
 
978
        :param delta: The inventory delta (see Inventory.apply_delta for
 
979
            details).
 
980
        :param new_revision_id: The revision id that the inventory is being
 
981
            added for.
 
982
        :param parents: The revision ids of the parents that revision_id is
 
983
            known to have and are in the repository already. These are supplied
 
984
            for repositories that depend on the inventory graph for revision
 
985
            graph access, as well as for those that pun ancestry with delta
 
986
            compression.
 
987
        :param basis_inv: The basis inventory if it is already known,
 
988
            otherwise None.
 
989
        :param propagate_caches: If True, the caches for this inventory are
 
990
          copied to and updated for the result if possible.
 
991
 
 
992
        :returns: (validator, new_inv)
 
993
            The validator(which is a sha1 digest, though what is sha'd is
 
994
            repository format specific) of the serialized inventory, and the
 
995
            resulting inventory.
 
996
        """
 
997
        if not self.is_in_write_group():
 
998
            raise AssertionError("%r not in write group" % (self,))
 
999
        _mod_revision.check_not_reserved_id(new_revision_id)
 
1000
        basis_tree = self.revision_tree(basis_revision_id)
 
1001
        basis_tree.lock_read()
 
1002
        try:
 
1003
            # Note that this mutates the inventory of basis_tree, which not all
 
1004
            # inventory implementations may support: A better idiom would be to
 
1005
            # return a new inventory, but as there is no revision tree cache in
 
1006
            # repository this is safe for now - RBC 20081013
 
1007
            if basis_inv is None:
 
1008
                basis_inv = basis_tree.inventory
 
1009
            basis_inv.apply_delta(delta)
 
1010
            basis_inv.revision_id = new_revision_id
 
1011
            return (self.add_inventory(new_revision_id, basis_inv, parents),
 
1012
                    basis_inv)
 
1013
        finally:
 
1014
            basis_tree.unlock()
 
1015
 
 
1016
    def _inventory_add_lines(self, revision_id, parents, lines,
 
1017
        check_content=True):
 
1018
        """Store lines in inv_vf and return the sha1 of the inventory."""
 
1019
        parents = [(parent,) for parent in parents]
 
1020
        result = self.inventories.add_lines((revision_id,), parents, lines,
 
1021
            check_content=check_content)[0]
 
1022
        self.inventories._access.flush()
 
1023
        return result
 
1024
 
 
1025
    def add_revision(self, revision_id, rev, inv=None, config=None):
 
1026
        """Add rev to the revision store as revision_id.
 
1027
 
 
1028
        :param revision_id: the revision id to use.
 
1029
        :param rev: The revision object.
 
1030
        :param inv: The inventory for the revision. if None, it will be looked
 
1031
                    up in the inventory storer
 
1032
        :param config: If None no digital signature will be created.
 
1033
                       If supplied its signature_needed method will be used
 
1034
                       to determine if a signature should be made.
 
1035
        """
 
1036
        # TODO: jam 20070210 Shouldn't we check rev.revision_id and
 
1037
        #       rev.parent_ids?
 
1038
        _mod_revision.check_not_reserved_id(revision_id)
 
1039
        if config is not None and config.signature_needed():
 
1040
            if inv is None:
 
1041
                inv = self.get_inventory(revision_id)
 
1042
            tree = InventoryRevisionTree(self, inv, revision_id)
 
1043
            testament = Testament(rev, tree)
 
1044
            plaintext = testament.as_short_text()
 
1045
            self.store_revision_signature(
 
1046
                gpg.GPGStrategy(config), plaintext, revision_id)
 
1047
        # check inventory present
 
1048
        if not self.inventories.get_parent_map([(revision_id,)]):
 
1049
            if inv is None:
 
1050
                raise errors.WeaveRevisionNotPresent(revision_id,
 
1051
                                                     self.inventories)
 
1052
            else:
 
1053
                # yes, this is not suitable for adding with ghosts.
 
1054
                rev.inventory_sha1 = self.add_inventory(revision_id, inv,
 
1055
                                                        rev.parent_ids)
 
1056
        else:
 
1057
            key = (revision_id,)
 
1058
            rev.inventory_sha1 = self.inventories.get_sha1s([key])[key]
 
1059
        self._add_revision(rev)
 
1060
 
 
1061
    def _add_revision(self, revision):
 
1062
        text = self._serializer.write_revision_to_string(revision)
 
1063
        key = (revision.revision_id,)
 
1064
        parents = tuple((parent,) for parent in revision.parent_ids)
 
1065
        self.revisions.add_lines(key, parents, osutils.split_lines(text))
 
1066
 
 
1067
    def _check_inventories(self, checker):
 
1068
        """Check the inventories found from the revision scan.
 
1069
        
 
1070
        This is responsible for verifying the sha1 of inventories and
 
1071
        creating a pending_keys set that covers data referenced by inventories.
 
1072
        """
 
1073
        bar = ui.ui_factory.nested_progress_bar()
 
1074
        try:
 
1075
            self._do_check_inventories(checker, bar)
 
1076
        finally:
 
1077
            bar.finished()
 
1078
 
 
1079
    def _do_check_inventories(self, checker, bar):
 
1080
        """Helper for _check_inventories."""
 
1081
        revno = 0
 
1082
        keys = {'chk_bytes':set(), 'inventories':set(), 'texts':set()}
 
1083
        kinds = ['chk_bytes', 'texts']
 
1084
        count = len(checker.pending_keys)
 
1085
        bar.update("inventories", 0, 2)
 
1086
        current_keys = checker.pending_keys
 
1087
        checker.pending_keys = {}
 
1088
        # Accumulate current checks.
 
1089
        for key in current_keys:
 
1090
            if key[0] != 'inventories' and key[0] not in kinds:
 
1091
                checker._report_items.append('unknown key type %r' % (key,))
 
1092
            keys[key[0]].add(key[1:])
 
1093
        if keys['inventories']:
 
1094
            # NB: output order *should* be roughly sorted - topo or
 
1095
            # inverse topo depending on repository - either way decent
 
1096
            # to just delta against. However, pre-CHK formats didn't
 
1097
            # try to optimise inventory layout on disk. As such the
 
1098
            # pre-CHK code path does not use inventory deltas.
 
1099
            last_object = None
 
1100
            for record in self.inventories.check(keys=keys['inventories']):
 
1101
                if record.storage_kind == 'absent':
 
1102
                    checker._report_items.append(
 
1103
                        'Missing inventory {%s}' % (record.key,))
 
1104
                else:
 
1105
                    last_object = self._check_record('inventories', record,
 
1106
                        checker, last_object,
 
1107
                        current_keys[('inventories',) + record.key])
 
1108
            del keys['inventories']
 
1109
        else:
 
1110
            return
 
1111
        bar.update("texts", 1)
 
1112
        while (checker.pending_keys or keys['chk_bytes']
 
1113
            or keys['texts']):
 
1114
            # Something to check.
 
1115
            current_keys = checker.pending_keys
 
1116
            checker.pending_keys = {}
 
1117
            # Accumulate current checks.
 
1118
            for key in current_keys:
 
1119
                if key[0] not in kinds:
 
1120
                    checker._report_items.append('unknown key type %r' % (key,))
 
1121
                keys[key[0]].add(key[1:])
 
1122
            # Check the outermost kind only - inventories || chk_bytes || texts
 
1123
            for kind in kinds:
 
1124
                if keys[kind]:
 
1125
                    last_object = None
 
1126
                    for record in getattr(self, kind).check(keys=keys[kind]):
 
1127
                        if record.storage_kind == 'absent':
 
1128
                            checker._report_items.append(
 
1129
                                'Missing %s {%s}' % (kind, record.key,))
 
1130
                        else:
 
1131
                            last_object = self._check_record(kind, record,
 
1132
                                checker, last_object, current_keys[(kind,) + record.key])
 
1133
                    keys[kind] = set()
 
1134
                    break
 
1135
 
 
1136
    def _check_record(self, kind, record, checker, last_object, item_data):
 
1137
        """Check a single text from this repository."""
 
1138
        if kind == 'inventories':
 
1139
            rev_id = record.key[0]
 
1140
            inv = self._deserialise_inventory(rev_id,
 
1141
                record.get_bytes_as('fulltext'))
 
1142
            if last_object is not None:
 
1143
                delta = inv._make_delta(last_object)
 
1144
                for old_path, path, file_id, ie in delta:
 
1145
                    if ie is None:
 
1146
                        continue
 
1147
                    ie.check(checker, rev_id, inv)
 
1148
            else:
 
1149
                for path, ie in inv.iter_entries():
 
1150
                    ie.check(checker, rev_id, inv)
 
1151
            if self._format.fast_deltas:
 
1152
                return inv
 
1153
        elif kind == 'chk_bytes':
 
1154
            # No code written to check chk_bytes for this repo format.
 
1155
            checker._report_items.append(
 
1156
                'unsupported key type chk_bytes for %s' % (record.key,))
 
1157
        elif kind == 'texts':
 
1158
            self._check_text(record, checker, item_data)
 
1159
        else:
 
1160
            checker._report_items.append(
 
1161
                'unknown key type %s for %s' % (kind, record.key))
 
1162
 
 
1163
    def _check_text(self, record, checker, item_data):
 
1164
        """Check a single text."""
 
1165
        # Check it is extractable.
 
1166
        # TODO: check length.
 
1167
        if record.storage_kind == 'chunked':
 
1168
            chunks = record.get_bytes_as(record.storage_kind)
 
1169
            sha1 = osutils.sha_strings(chunks)
 
1170
            length = sum(map(len, chunks))
 
1171
        else:
 
1172
            content = record.get_bytes_as('fulltext')
 
1173
            sha1 = osutils.sha_string(content)
 
1174
            length = len(content)
 
1175
        if item_data and sha1 != item_data[1]:
 
1176
            checker._report_items.append(
 
1177
                'sha1 mismatch: %s has sha1 %s expected %s referenced by %s' %
 
1178
                (record.key, sha1, item_data[1], item_data[2]))
 
1179
 
 
1180
    def __init__(self, _format, a_bzrdir, control_files):
 
1181
        """Instantiate a VersionedFileRepository.
 
1182
 
 
1183
        :param _format: The format of the repository on disk.
 
1184
        :param a_bzrdir: The BzrDir of the repository.
 
1185
        :param control_files: Control files to use for locking, etc.
 
1186
        """
 
1187
        # In the future we will have a single api for all stores for
 
1188
        # getting file texts, inventories and revisions, then
 
1189
        # this construct will accept instances of those things.
 
1190
        super(VersionedFileRepository, self).__init__(_format, a_bzrdir,
 
1191
            control_files)
 
1192
        # for tests
 
1193
        self._reconcile_does_inventory_gc = True
 
1194
        self._reconcile_fixes_text_parents = False
 
1195
        self._reconcile_backsup_inventory = True
 
1196
        # An InventoryEntry cache, used during deserialization
 
1197
        self._inventory_entry_cache = fifo_cache.FIFOCache(10*1024)
 
1198
        # Is it safe to return inventory entries directly from the entry cache,
 
1199
        # rather copying them?
 
1200
        self._safe_to_return_from_cache = False
 
1201
 
 
1202
    @needs_read_lock
 
1203
    def gather_stats(self, revid=None, committers=None):
 
1204
        """See Repository.gather_stats()."""
 
1205
        result = super(VersionedFileRepository, self).gather_stats(revid, committers)
 
1206
        # now gather global repository information
 
1207
        # XXX: This is available for many repos regardless of listability.
 
1208
        if self.user_transport.listable():
 
1209
            # XXX: do we want to __define len__() ?
 
1210
            # Maybe the versionedfiles object should provide a different
 
1211
            # method to get the number of keys.
 
1212
            result['revisions'] = len(self.revisions.keys())
 
1213
            # result['size'] = t
 
1214
        return result
 
1215
 
 
1216
    def get_commit_builder(self, branch, parents, config, timestamp=None,
 
1217
                           timezone=None, committer=None, revprops=None,
 
1218
                           revision_id=None, lossy=False):
 
1219
        """Obtain a CommitBuilder for this repository.
 
1220
 
 
1221
        :param branch: Branch to commit to.
 
1222
        :param parents: Revision ids of the parents of the new revision.
 
1223
        :param config: Configuration to use.
 
1224
        :param timestamp: Optional timestamp recorded for commit.
 
1225
        :param timezone: Optional timezone for timestamp.
 
1226
        :param committer: Optional committer to set for commit.
 
1227
        :param revprops: Optional dictionary of revision properties.
 
1228
        :param revision_id: Optional revision id.
 
1229
        :param lossy: Whether to discard data that can not be natively
 
1230
            represented, when pushing to a foreign VCS
 
1231
        """
 
1232
        if self._fallback_repositories and not self._format.supports_chks:
 
1233
            raise errors.BzrError("Cannot commit directly to a stacked branch"
 
1234
                " in pre-2a formats. See "
 
1235
                "https://bugs.launchpad.net/bzr/+bug/375013 for details.")
 
1236
        result = self._commit_builder_class(self, parents, config,
 
1237
            timestamp, timezone, committer, revprops, revision_id,
 
1238
            lossy)
 
1239
        self.start_write_group()
 
1240
        return result
 
1241
 
 
1242
    def get_missing_parent_inventories(self, check_for_missing_texts=True):
 
1243
        """Return the keys of missing inventory parents for revisions added in
 
1244
        this write group.
 
1245
 
 
1246
        A revision is not complete if the inventory delta for that revision
 
1247
        cannot be calculated.  Therefore if the parent inventories of a
 
1248
        revision are not present, the revision is incomplete, and e.g. cannot
 
1249
        be streamed by a smart server.  This method finds missing inventory
 
1250
        parents for revisions added in this write group.
 
1251
        """
 
1252
        if not self._format.supports_external_lookups:
 
1253
            # This is only an issue for stacked repositories
 
1254
            return set()
 
1255
        if not self.is_in_write_group():
 
1256
            raise AssertionError('not in a write group')
 
1257
 
 
1258
        # XXX: We assume that every added revision already has its
 
1259
        # corresponding inventory, so we only check for parent inventories that
 
1260
        # might be missing, rather than all inventories.
 
1261
        parents = set(self.revisions._index.get_missing_parents())
 
1262
        parents.discard(_mod_revision.NULL_REVISION)
 
1263
        unstacked_inventories = self.inventories._index
 
1264
        present_inventories = unstacked_inventories.get_parent_map(
 
1265
            key[-1:] for key in parents)
 
1266
        parents.difference_update(present_inventories)
 
1267
        if len(parents) == 0:
 
1268
            # No missing parent inventories.
 
1269
            return set()
 
1270
        if not check_for_missing_texts:
 
1271
            return set(('inventories', rev_id) for (rev_id,) in parents)
 
1272
        # Ok, now we have a list of missing inventories.  But these only matter
 
1273
        # if the inventories that reference them are missing some texts they
 
1274
        # appear to introduce.
 
1275
        # XXX: Texts referenced by all added inventories need to be present,
 
1276
        # but at the moment we're only checking for texts referenced by
 
1277
        # inventories at the graph's edge.
 
1278
        key_deps = self.revisions._index._key_dependencies
 
1279
        key_deps.satisfy_refs_for_keys(present_inventories)
 
1280
        referrers = frozenset(r[0] for r in key_deps.get_referrers())
 
1281
        file_ids = self.fileids_altered_by_revision_ids(referrers)
 
1282
        missing_texts = set()
 
1283
        for file_id, version_ids in file_ids.iteritems():
 
1284
            missing_texts.update(
 
1285
                (file_id, version_id) for version_id in version_ids)
 
1286
        present_texts = self.texts.get_parent_map(missing_texts)
 
1287
        missing_texts.difference_update(present_texts)
 
1288
        if not missing_texts:
 
1289
            # No texts are missing, so all revisions and their deltas are
 
1290
            # reconstructable.
 
1291
            return set()
 
1292
        # Alternatively the text versions could be returned as the missing
 
1293
        # keys, but this is likely to be less data.
 
1294
        missing_keys = set(('inventories', rev_id) for (rev_id,) in parents)
 
1295
        return missing_keys
 
1296
 
 
1297
    @needs_read_lock
 
1298
    def has_revisions(self, revision_ids):
 
1299
        """Probe to find out the presence of multiple revisions.
 
1300
 
 
1301
        :param revision_ids: An iterable of revision_ids.
 
1302
        :return: A set of the revision_ids that were present.
 
1303
        """
 
1304
        parent_map = self.revisions.get_parent_map(
 
1305
            [(rev_id,) for rev_id in revision_ids])
 
1306
        result = set()
 
1307
        if _mod_revision.NULL_REVISION in revision_ids:
 
1308
            result.add(_mod_revision.NULL_REVISION)
 
1309
        result.update([key[0] for key in parent_map])
 
1310
        return result
 
1311
 
 
1312
    @needs_read_lock
 
1313
    def get_revision_reconcile(self, revision_id):
 
1314
        """'reconcile' helper routine that allows access to a revision always.
 
1315
 
 
1316
        This variant of get_revision does not cross check the weave graph
 
1317
        against the revision one as get_revision does: but it should only
 
1318
        be used by reconcile, or reconcile-alike commands that are correcting
 
1319
        or testing the revision graph.
 
1320
        """
 
1321
        return self._get_revisions([revision_id])[0]
 
1322
 
 
1323
    @needs_read_lock
 
1324
    def get_revisions(self, revision_ids):
 
1325
        """Get many revisions at once.
 
1326
        
 
1327
        Repositories that need to check data on every revision read should 
 
1328
        subclass this method.
 
1329
        """
 
1330
        return self._get_revisions(revision_ids)
 
1331
 
 
1332
    @needs_read_lock
 
1333
    def _get_revisions(self, revision_ids):
 
1334
        """Core work logic to get many revisions without sanity checks."""
 
1335
        revs = {}
 
1336
        for revid, rev in self._iter_revisions(revision_ids):
 
1337
            if rev is None:
 
1338
                raise errors.NoSuchRevision(self, revid)
 
1339
            revs[revid] = rev
 
1340
        return [revs[revid] for revid in revision_ids]
 
1341
 
 
1342
    def _iter_revisions(self, revision_ids):
 
1343
        """Iterate over revision objects.
 
1344
 
 
1345
        :param revision_ids: An iterable of revisions to examine. None may be
 
1346
            passed to request all revisions known to the repository. Note that
 
1347
            not all repositories can find unreferenced revisions; for those
 
1348
            repositories only referenced ones will be returned.
 
1349
        :return: An iterator of (revid, revision) tuples. Absent revisions (
 
1350
            those asked for but not available) are returned as (revid, None).
 
1351
        """
 
1352
        if revision_ids is None:
 
1353
            revision_ids = self.all_revision_ids()
 
1354
        else:
 
1355
            for rev_id in revision_ids:
 
1356
                if not rev_id or not isinstance(rev_id, basestring):
 
1357
                    raise errors.InvalidRevisionId(revision_id=rev_id, branch=self)
 
1358
        keys = [(key,) for key in revision_ids]
 
1359
        stream = self.revisions.get_record_stream(keys, 'unordered', True)
 
1360
        for record in stream:
 
1361
            revid = record.key[0]
 
1362
            if record.storage_kind == 'absent':
 
1363
                yield (revid, None)
 
1364
            else:
 
1365
                text = record.get_bytes_as('fulltext')
 
1366
                rev = self._serializer.read_revision_from_string(text)
 
1367
                yield (revid, rev)
 
1368
 
 
1369
    @needs_write_lock
 
1370
    def add_signature_text(self, revision_id, signature):
 
1371
        """Store a signature text for a revision.
 
1372
 
 
1373
        :param revision_id: Revision id of the revision
 
1374
        :param signature: Signature text.
 
1375
        """
 
1376
        self.signatures.add_lines((revision_id,), (),
 
1377
            osutils.split_lines(signature))
 
1378
 
 
1379
    def find_text_key_references(self):
 
1380
        """Find the text key references within the repository.
 
1381
 
 
1382
        :return: A dictionary mapping text keys ((fileid, revision_id) tuples)
 
1383
            to whether they were referred to by the inventory of the
 
1384
            revision_id that they contain. The inventory texts from all present
 
1385
            revision ids are assessed to generate this report.
 
1386
        """
 
1387
        revision_keys = self.revisions.keys()
 
1388
        w = self.inventories
 
1389
        pb = ui.ui_factory.nested_progress_bar()
 
1390
        try:
 
1391
            return self._serializer._find_text_key_references(
 
1392
                w.iter_lines_added_or_present_in_keys(revision_keys, pb=pb))
 
1393
        finally:
 
1394
            pb.finished()
 
1395
 
 
1396
    def _inventory_xml_lines_for_keys(self, keys):
 
1397
        """Get a line iterator of the sort needed for findind references.
 
1398
 
 
1399
        Not relevant for non-xml inventory repositories.
 
1400
 
 
1401
        Ghosts in revision_keys are ignored.
 
1402
 
 
1403
        :param revision_keys: The revision keys for the inventories to inspect.
 
1404
        :return: An iterator over (inventory line, revid) for the fulltexts of
 
1405
            all of the xml inventories specified by revision_keys.
 
1406
        """
 
1407
        stream = self.inventories.get_record_stream(keys, 'unordered', True)
 
1408
        for record in stream:
 
1409
            if record.storage_kind != 'absent':
 
1410
                chunks = record.get_bytes_as('chunked')
 
1411
                revid = record.key[-1]
 
1412
                lines = osutils.chunks_to_lines(chunks)
 
1413
                for line in lines:
 
1414
                    yield line, revid
 
1415
 
 
1416
    def _find_file_ids_from_xml_inventory_lines(self, line_iterator,
 
1417
        revision_keys):
 
1418
        """Helper routine for fileids_altered_by_revision_ids.
 
1419
 
 
1420
        This performs the translation of xml lines to revision ids.
 
1421
 
 
1422
        :param line_iterator: An iterator of lines, origin_version_id
 
1423
        :param revision_keys: The revision ids to filter for. This should be a
 
1424
            set or other type which supports efficient __contains__ lookups, as
 
1425
            the revision key from each parsed line will be looked up in the
 
1426
            revision_keys filter.
 
1427
        :return: a dictionary mapping altered file-ids to an iterable of
 
1428
        revision_ids. Each altered file-ids has the exact revision_ids that
 
1429
        altered it listed explicitly.
 
1430
        """
 
1431
        seen = set(self._serializer._find_text_key_references(
 
1432
                line_iterator).iterkeys())
 
1433
        parent_keys = self._find_parent_keys_of_revisions(revision_keys)
 
1434
        parent_seen = set(self._serializer._find_text_key_references(
 
1435
            self._inventory_xml_lines_for_keys(parent_keys)))
 
1436
        new_keys = seen - parent_seen
 
1437
        result = {}
 
1438
        setdefault = result.setdefault
 
1439
        for key in new_keys:
 
1440
            setdefault(key[0], set()).add(key[-1])
 
1441
        return result
 
1442
 
 
1443
    def _find_parent_keys_of_revisions(self, revision_keys):
 
1444
        """Similar to _find_parent_ids_of_revisions, but used with keys.
 
1445
 
 
1446
        :param revision_keys: An iterable of revision_keys.
 
1447
        :return: The parents of all revision_keys that are not already in
 
1448
            revision_keys
 
1449
        """
 
1450
        parent_map = self.revisions.get_parent_map(revision_keys)
 
1451
        parent_keys = set()
 
1452
        map(parent_keys.update, parent_map.itervalues())
 
1453
        parent_keys.difference_update(revision_keys)
 
1454
        parent_keys.discard(_mod_revision.NULL_REVISION)
 
1455
        return parent_keys
 
1456
 
 
1457
    def fileids_altered_by_revision_ids(self, revision_ids, _inv_weave=None):
 
1458
        """Find the file ids and versions affected by revisions.
 
1459
 
 
1460
        :param revisions: an iterable containing revision ids.
 
1461
        :param _inv_weave: The inventory weave from this repository or None.
 
1462
            If None, the inventory weave will be opened automatically.
 
1463
        :return: a dictionary mapping altered file-ids to an iterable of
 
1464
        revision_ids. Each altered file-ids has the exact revision_ids that
 
1465
        altered it listed explicitly.
 
1466
        """
 
1467
        selected_keys = set((revid,) for revid in revision_ids)
 
1468
        w = _inv_weave or self.inventories
 
1469
        return self._find_file_ids_from_xml_inventory_lines(
 
1470
            w.iter_lines_added_or_present_in_keys(
 
1471
                selected_keys, pb=None),
 
1472
            selected_keys)
 
1473
 
 
1474
    def iter_files_bytes(self, desired_files):
 
1475
        """Iterate through file versions.
 
1476
 
 
1477
        Files will not necessarily be returned in the order they occur in
 
1478
        desired_files.  No specific order is guaranteed.
 
1479
 
 
1480
        Yields pairs of identifier, bytes_iterator.  identifier is an opaque
 
1481
        value supplied by the caller as part of desired_files.  It should
 
1482
        uniquely identify the file version in the caller's context.  (Examples:
 
1483
        an index number or a TreeTransform trans_id.)
 
1484
 
 
1485
        bytes_iterator is an iterable of bytestrings for the file.  The
 
1486
        kind of iterable and length of the bytestrings are unspecified, but for
 
1487
        this implementation, it is a list of bytes produced by
 
1488
        VersionedFile.get_record_stream().
 
1489
 
 
1490
        :param desired_files: a list of (file_id, revision_id, identifier)
 
1491
            triples
 
1492
        """
 
1493
        text_keys = {}
 
1494
        for file_id, revision_id, callable_data in desired_files:
 
1495
            text_keys[(file_id, revision_id)] = callable_data
 
1496
        for record in self.texts.get_record_stream(text_keys, 'unordered', True):
 
1497
            if record.storage_kind == 'absent':
 
1498
                raise errors.RevisionNotPresent(record.key, self)
 
1499
            yield text_keys[record.key], record.get_bytes_as('chunked')
 
1500
 
 
1501
    def _generate_text_key_index(self, text_key_references=None,
 
1502
        ancestors=None):
 
1503
        """Generate a new text key index for the repository.
 
1504
 
 
1505
        This is an expensive function that will take considerable time to run.
 
1506
 
 
1507
        :return: A dict mapping text keys ((file_id, revision_id) tuples) to a
 
1508
            list of parents, also text keys. When a given key has no parents,
 
1509
            the parents list will be [NULL_REVISION].
 
1510
        """
 
1511
        # All revisions, to find inventory parents.
 
1512
        if ancestors is None:
 
1513
            graph = self.get_graph()
 
1514
            ancestors = graph.get_parent_map(self.all_revision_ids())
 
1515
        if text_key_references is None:
 
1516
            text_key_references = self.find_text_key_references()
 
1517
        pb = ui.ui_factory.nested_progress_bar()
 
1518
        try:
 
1519
            return self._do_generate_text_key_index(ancestors,
 
1520
                text_key_references, pb)
 
1521
        finally:
 
1522
            pb.finished()
 
1523
 
 
1524
    def _do_generate_text_key_index(self, ancestors, text_key_references, pb):
 
1525
        """Helper for _generate_text_key_index to avoid deep nesting."""
 
1526
        revision_order = tsort.topo_sort(ancestors)
 
1527
        invalid_keys = set()
 
1528
        revision_keys = {}
 
1529
        for revision_id in revision_order:
 
1530
            revision_keys[revision_id] = set()
 
1531
        text_count = len(text_key_references)
 
1532
        # a cache of the text keys to allow reuse; costs a dict of all the
 
1533
        # keys, but saves a 2-tuple for every child of a given key.
 
1534
        text_key_cache = {}
 
1535
        for text_key, valid in text_key_references.iteritems():
 
1536
            if not valid:
 
1537
                invalid_keys.add(text_key)
 
1538
            else:
 
1539
                revision_keys[text_key[1]].add(text_key)
 
1540
            text_key_cache[text_key] = text_key
 
1541
        del text_key_references
 
1542
        text_index = {}
 
1543
        text_graph = graph.Graph(graph.DictParentsProvider(text_index))
 
1544
        NULL_REVISION = _mod_revision.NULL_REVISION
 
1545
        # Set a cache with a size of 10 - this suffices for bzr.dev but may be
 
1546
        # too small for large or very branchy trees. However, for 55K path
 
1547
        # trees, it would be easy to use too much memory trivially. Ideally we
 
1548
        # could gauge this by looking at available real memory etc, but this is
 
1549
        # always a tricky proposition.
 
1550
        inventory_cache = lru_cache.LRUCache(10)
 
1551
        batch_size = 10 # should be ~150MB on a 55K path tree
 
1552
        batch_count = len(revision_order) / batch_size + 1
 
1553
        processed_texts = 0
 
1554
        pb.update("Calculating text parents", processed_texts, text_count)
 
1555
        for offset in xrange(batch_count):
 
1556
            to_query = revision_order[offset * batch_size:(offset + 1) *
 
1557
                batch_size]
 
1558
            if not to_query:
 
1559
                break
 
1560
            for revision_id in to_query:
 
1561
                parent_ids = ancestors[revision_id]
 
1562
                for text_key in revision_keys[revision_id]:
 
1563
                    pb.update("Calculating text parents", processed_texts)
 
1564
                    processed_texts += 1
 
1565
                    candidate_parents = []
 
1566
                    for parent_id in parent_ids:
 
1567
                        parent_text_key = (text_key[0], parent_id)
 
1568
                        try:
 
1569
                            check_parent = parent_text_key not in \
 
1570
                                revision_keys[parent_id]
 
1571
                        except KeyError:
 
1572
                            # the parent parent_id is a ghost:
 
1573
                            check_parent = False
 
1574
                            # truncate the derived graph against this ghost.
 
1575
                            parent_text_key = None
 
1576
                        if check_parent:
 
1577
                            # look at the parent commit details inventories to
 
1578
                            # determine possible candidates in the per file graph.
 
1579
                            # TODO: cache here.
 
1580
                            try:
 
1581
                                inv = inventory_cache[parent_id]
 
1582
                            except KeyError:
 
1583
                                inv = self.revision_tree(parent_id).inventory
 
1584
                                inventory_cache[parent_id] = inv
 
1585
                            try:
 
1586
                                parent_entry = inv[text_key[0]]
 
1587
                            except (KeyError, errors.NoSuchId):
 
1588
                                parent_entry = None
 
1589
                            if parent_entry is not None:
 
1590
                                parent_text_key = (
 
1591
                                    text_key[0], parent_entry.revision)
 
1592
                            else:
 
1593
                                parent_text_key = None
 
1594
                        if parent_text_key is not None:
 
1595
                            candidate_parents.append(
 
1596
                                text_key_cache[parent_text_key])
 
1597
                    parent_heads = text_graph.heads(candidate_parents)
 
1598
                    new_parents = list(parent_heads)
 
1599
                    new_parents.sort(key=lambda x:candidate_parents.index(x))
 
1600
                    if new_parents == []:
 
1601
                        new_parents = [NULL_REVISION]
 
1602
                    text_index[text_key] = new_parents
 
1603
 
 
1604
        for text_key in invalid_keys:
 
1605
            text_index[text_key] = [NULL_REVISION]
 
1606
        return text_index
 
1607
 
 
1608
    def item_keys_introduced_by(self, revision_ids, _files_pb=None):
 
1609
        """Get an iterable listing the keys of all the data introduced by a set
 
1610
        of revision IDs.
 
1611
 
 
1612
        The keys will be ordered so that the corresponding items can be safely
 
1613
        fetched and inserted in that order.
 
1614
 
 
1615
        :returns: An iterable producing tuples of (knit-kind, file-id,
 
1616
            versions).  knit-kind is one of 'file', 'inventory', 'signatures',
 
1617
            'revisions'.  file-id is None unless knit-kind is 'file'.
 
1618
        """
 
1619
        for result in self._find_file_keys_to_fetch(revision_ids, _files_pb):
 
1620
            yield result
 
1621
        del _files_pb
 
1622
        for result in self._find_non_file_keys_to_fetch(revision_ids):
 
1623
            yield result
 
1624
 
 
1625
    def _find_file_keys_to_fetch(self, revision_ids, pb):
 
1626
        # XXX: it's a bit weird to control the inventory weave caching in this
 
1627
        # generator.  Ideally the caching would be done in fetch.py I think.  Or
 
1628
        # maybe this generator should explicitly have the contract that it
 
1629
        # should not be iterated until the previously yielded item has been
 
1630
        # processed?
 
1631
        inv_w = self.inventories
 
1632
 
 
1633
        # file ids that changed
 
1634
        file_ids = self.fileids_altered_by_revision_ids(revision_ids, inv_w)
 
1635
        count = 0
 
1636
        num_file_ids = len(file_ids)
 
1637
        for file_id, altered_versions in file_ids.iteritems():
 
1638
            if pb is not None:
 
1639
                pb.update("Fetch texts", count, num_file_ids)
 
1640
            count += 1
 
1641
            yield ("file", file_id, altered_versions)
 
1642
 
 
1643
    def _find_non_file_keys_to_fetch(self, revision_ids):
 
1644
        # inventory
 
1645
        yield ("inventory", None, revision_ids)
 
1646
 
 
1647
        # signatures
 
1648
        # XXX: Note ATM no callers actually pay attention to this return
 
1649
        #      instead they just use the list of revision ids and ignore
 
1650
        #      missing sigs. Consider removing this work entirely
 
1651
        revisions_with_signatures = set(self.signatures.get_parent_map(
 
1652
            [(r,) for r in revision_ids]))
 
1653
        revisions_with_signatures = set(
 
1654
            [r for (r,) in revisions_with_signatures])
 
1655
        revisions_with_signatures.intersection_update(revision_ids)
 
1656
        yield ("signatures", None, revisions_with_signatures)
 
1657
 
 
1658
        # revisions
 
1659
        yield ("revisions", None, revision_ids)
 
1660
 
 
1661
    @needs_read_lock
 
1662
    def get_inventory(self, revision_id):
 
1663
        """Get Inventory object by revision id."""
 
1664
        return self.iter_inventories([revision_id]).next()
 
1665
 
 
1666
    def iter_inventories(self, revision_ids, ordering=None):
 
1667
        """Get many inventories by revision_ids.
 
1668
 
 
1669
        This will buffer some or all of the texts used in constructing the
 
1670
        inventories in memory, but will only parse a single inventory at a
 
1671
        time.
 
1672
 
 
1673
        :param revision_ids: The expected revision ids of the inventories.
 
1674
        :param ordering: optional ordering, e.g. 'topological'.  If not
 
1675
            specified, the order of revision_ids will be preserved (by
 
1676
            buffering if necessary).
 
1677
        :return: An iterator of inventories.
 
1678
        """
 
1679
        if ((None in revision_ids)
 
1680
            or (_mod_revision.NULL_REVISION in revision_ids)):
 
1681
            raise ValueError('cannot get null revision inventory')
 
1682
        return self._iter_inventories(revision_ids, ordering)
 
1683
 
 
1684
    def _iter_inventories(self, revision_ids, ordering):
 
1685
        """single-document based inventory iteration."""
 
1686
        inv_xmls = self._iter_inventory_xmls(revision_ids, ordering)
 
1687
        for text, revision_id in inv_xmls:
 
1688
            yield self._deserialise_inventory(revision_id, text)
 
1689
 
 
1690
    def _iter_inventory_xmls(self, revision_ids, ordering):
 
1691
        if ordering is None:
 
1692
            order_as_requested = True
 
1693
            ordering = 'unordered'
 
1694
        else:
 
1695
            order_as_requested = False
 
1696
        keys = [(revision_id,) for revision_id in revision_ids]
 
1697
        if not keys:
 
1698
            return
 
1699
        if order_as_requested:
 
1700
            key_iter = iter(keys)
 
1701
            next_key = key_iter.next()
 
1702
        stream = self.inventories.get_record_stream(keys, ordering, True)
 
1703
        text_chunks = {}
 
1704
        for record in stream:
 
1705
            if record.storage_kind != 'absent':
 
1706
                chunks = record.get_bytes_as('chunked')
 
1707
                if order_as_requested:
 
1708
                    text_chunks[record.key] = chunks
 
1709
                else:
 
1710
                    yield ''.join(chunks), record.key[-1]
 
1711
            else:
 
1712
                raise errors.NoSuchRevision(self, record.key)
 
1713
            if order_as_requested:
 
1714
                # Yield as many results as we can while preserving order.
 
1715
                while next_key in text_chunks:
 
1716
                    chunks = text_chunks.pop(next_key)
 
1717
                    yield ''.join(chunks), next_key[-1]
 
1718
                    try:
 
1719
                        next_key = key_iter.next()
 
1720
                    except StopIteration:
 
1721
                        # We still want to fully consume the get_record_stream,
 
1722
                        # just in case it is not actually finished at this point
 
1723
                        next_key = None
 
1724
                        break
 
1725
 
 
1726
    def _deserialise_inventory(self, revision_id, xml):
 
1727
        """Transform the xml into an inventory object.
 
1728
 
 
1729
        :param revision_id: The expected revision id of the inventory.
 
1730
        :param xml: A serialised inventory.
 
1731
        """
 
1732
        result = self._serializer.read_inventory_from_string(xml, revision_id,
 
1733
                    entry_cache=self._inventory_entry_cache,
 
1734
                    return_from_cache=self._safe_to_return_from_cache)
 
1735
        if result.revision_id != revision_id:
 
1736
            raise AssertionError('revision id mismatch %s != %s' % (
 
1737
                result.revision_id, revision_id))
 
1738
        return result
 
1739
 
 
1740
    def get_serializer_format(self):
 
1741
        return self._serializer.format_num
 
1742
 
 
1743
    @needs_read_lock
 
1744
    def _get_inventory_xml(self, revision_id):
 
1745
        """Get serialized inventory as a string."""
 
1746
        texts = self._iter_inventory_xmls([revision_id], 'unordered')
 
1747
        try:
 
1748
            text, revision_id = texts.next()
 
1749
        except StopIteration:
 
1750
            raise errors.HistoryMissing(self, 'inventory', revision_id)
 
1751
        return text
 
1752
 
 
1753
    @needs_read_lock
 
1754
    def revision_tree(self, revision_id):
 
1755
        """Return Tree for a revision on this branch.
 
1756
 
 
1757
        `revision_id` may be NULL_REVISION for the empty tree revision.
 
1758
        """
 
1759
        revision_id = _mod_revision.ensure_null(revision_id)
 
1760
        # TODO: refactor this to use an existing revision object
 
1761
        # so we don't need to read it in twice.
 
1762
        if revision_id == _mod_revision.NULL_REVISION:
 
1763
            return InventoryRevisionTree(self,
 
1764
                Inventory(root_id=None), _mod_revision.NULL_REVISION)
 
1765
        else:
 
1766
            inv = self.get_inventory(revision_id)
 
1767
            return InventoryRevisionTree(self, inv, revision_id)
 
1768
 
 
1769
    def revision_trees(self, revision_ids):
 
1770
        """Return Trees for revisions in this repository.
 
1771
 
 
1772
        :param revision_ids: a sequence of revision-ids;
 
1773
          a revision-id may not be None or 'null:'
 
1774
        """
 
1775
        inventories = self.iter_inventories(revision_ids)
 
1776
        for inv in inventories:
 
1777
            yield InventoryRevisionTree(self, inv, inv.revision_id)
 
1778
 
 
1779
    def _filtered_revision_trees(self, revision_ids, file_ids):
 
1780
        """Return Tree for a revision on this branch with only some files.
 
1781
 
 
1782
        :param revision_ids: a sequence of revision-ids;
 
1783
          a revision-id may not be None or 'null:'
 
1784
        :param file_ids: if not None, the result is filtered
 
1785
          so that only those file-ids, their parents and their
 
1786
          children are included.
 
1787
        """
 
1788
        inventories = self.iter_inventories(revision_ids)
 
1789
        for inv in inventories:
 
1790
            # Should we introduce a FilteredRevisionTree class rather
 
1791
            # than pre-filter the inventory here?
 
1792
            filtered_inv = inv.filter(file_ids)
 
1793
            yield InventoryRevisionTree(self, filtered_inv, filtered_inv.revision_id)
 
1794
 
 
1795
    def get_parent_map(self, revision_ids):
 
1796
        """See graph.StackedParentsProvider.get_parent_map"""
 
1797
        # revisions index works in keys; this just works in revisions
 
1798
        # therefore wrap and unwrap
 
1799
        query_keys = []
 
1800
        result = {}
 
1801
        for revision_id in revision_ids:
 
1802
            if revision_id == _mod_revision.NULL_REVISION:
 
1803
                result[revision_id] = ()
 
1804
            elif revision_id is None:
 
1805
                raise ValueError('get_parent_map(None) is not valid')
 
1806
            else:
 
1807
                query_keys.append((revision_id ,))
 
1808
        for ((revision_id,), parent_keys) in \
 
1809
                self.revisions.get_parent_map(query_keys).iteritems():
 
1810
            if parent_keys:
 
1811
                result[revision_id] = tuple([parent_revid
 
1812
                    for (parent_revid,) in parent_keys])
 
1813
            else:
 
1814
                result[revision_id] = (_mod_revision.NULL_REVISION,)
 
1815
        return result
 
1816
 
 
1817
    @needs_read_lock
 
1818
    def get_known_graph_ancestry(self, revision_ids):
 
1819
        """Return the known graph for a set of revision ids and their ancestors.
 
1820
        """
 
1821
        st = static_tuple.StaticTuple
 
1822
        revision_keys = [st(r_id).intern() for r_id in revision_ids]
 
1823
        known_graph = self.revisions.get_known_graph_ancestry(revision_keys)
 
1824
        return graph.GraphThunkIdsToKeys(known_graph)
 
1825
 
 
1826
    @needs_read_lock
 
1827
    def get_file_graph(self):
 
1828
        """Return the graph walker for text revisions."""
 
1829
        return graph.Graph(self.texts)
 
1830
 
 
1831
    def _get_versioned_file_checker(self, text_key_references=None,
 
1832
        ancestors=None):
 
1833
        """Return an object suitable for checking versioned files.
 
1834
        
 
1835
        :param text_key_references: if non-None, an already built
 
1836
            dictionary mapping text keys ((fileid, revision_id) tuples)
 
1837
            to whether they were referred to by the inventory of the
 
1838
            revision_id that they contain. If None, this will be
 
1839
            calculated.
 
1840
        :param ancestors: Optional result from
 
1841
            self.get_graph().get_parent_map(self.all_revision_ids()) if already
 
1842
            available.
 
1843
        """
 
1844
        return _VersionedFileChecker(self,
 
1845
            text_key_references=text_key_references, ancestors=ancestors)
 
1846
 
 
1847
    @needs_read_lock
 
1848
    def has_signature_for_revision_id(self, revision_id):
 
1849
        """Query for a revision signature for revision_id in the repository."""
 
1850
        if not self.has_revision(revision_id):
 
1851
            raise errors.NoSuchRevision(self, revision_id)
 
1852
        sig_present = (1 == len(
 
1853
            self.signatures.get_parent_map([(revision_id,)])))
 
1854
        return sig_present
 
1855
 
 
1856
    @needs_read_lock
 
1857
    def get_signature_text(self, revision_id):
 
1858
        """Return the text for a signature."""
 
1859
        stream = self.signatures.get_record_stream([(revision_id,)],
 
1860
            'unordered', True)
 
1861
        record = stream.next()
 
1862
        if record.storage_kind == 'absent':
 
1863
            raise errors.NoSuchRevision(self, revision_id)
 
1864
        return record.get_bytes_as('fulltext')
 
1865
 
 
1866
    @needs_read_lock
 
1867
    def _check(self, revision_ids, callback_refs, check_repo):
 
1868
        result = check.VersionedFileCheck(self, check_repo=check_repo)
 
1869
        result.check(callback_refs)
 
1870
        return result
 
1871
 
 
1872
    def _find_inconsistent_revision_parents(self, revisions_iterator=None):
 
1873
        """Find revisions with different parent lists in the revision object
 
1874
        and in the index graph.
 
1875
 
 
1876
        :param revisions_iterator: None, or an iterator of (revid,
 
1877
            Revision-or-None). This iterator controls the revisions checked.
 
1878
        :returns: an iterator yielding tuples of (revison-id, parents-in-index,
 
1879
            parents-in-revision).
 
1880
        """
 
1881
        if not self.is_locked():
 
1882
            raise AssertionError()
 
1883
        vf = self.revisions
 
1884
        if revisions_iterator is None:
 
1885
            revisions_iterator = self._iter_revisions(None)
 
1886
        for revid, revision in revisions_iterator:
 
1887
            if revision is None:
 
1888
                pass
 
1889
            parent_map = vf.get_parent_map([(revid,)])
 
1890
            parents_according_to_index = tuple(parent[-1] for parent in
 
1891
                parent_map[(revid,)])
 
1892
            parents_according_to_revision = tuple(revision.parent_ids)
 
1893
            if parents_according_to_index != parents_according_to_revision:
 
1894
                yield (revid, parents_according_to_index,
 
1895
                    parents_according_to_revision)
 
1896
 
 
1897
    def _check_for_inconsistent_revision_parents(self):
 
1898
        inconsistencies = list(self._find_inconsistent_revision_parents())
 
1899
        if inconsistencies:
 
1900
            raise errors.BzrCheckError(
 
1901
                "Revision knit has inconsistent parents.")
 
1902
 
 
1903
    def _get_sink(self):
 
1904
        """Return a sink for streaming into this repository."""
 
1905
        return StreamSink(self)
 
1906
 
 
1907
    def _get_source(self, to_format):
 
1908
        """Return a source for streaming from this repository."""
 
1909
        return StreamSource(self, to_format)
 
1910
 
 
1911
 
 
1912
class MetaDirVersionedFileRepository(MetaDirRepository,
 
1913
                                     VersionedFileRepository):
 
1914
    """Repositories in a meta-dir, that work via versioned file objects."""
 
1915
 
 
1916
    def __init__(self, _format, a_bzrdir, control_files):
 
1917
        super(MetaDirVersionedFileRepository, self).__init__(_format, a_bzrdir,
 
1918
            control_files)
 
1919
 
 
1920
 
 
1921
class MetaDirVersionedFileRepositoryFormat(MetaDirRepositoryFormat,
 
1922
        VersionedFileRepositoryFormat):
 
1923
    """Base class for repository formats using versioned files in metadirs."""
 
1924
 
 
1925
 
 
1926
class StreamSink(object):
 
1927
    """An object that can insert a stream into a repository.
 
1928
 
 
1929
    This interface handles the complexity of reserialising inventories and
 
1930
    revisions from different formats, and allows unidirectional insertion into
 
1931
    stacked repositories without looking for the missing basis parents
 
1932
    beforehand.
 
1933
    """
 
1934
 
 
1935
    def __init__(self, target_repo):
 
1936
        self.target_repo = target_repo
 
1937
 
 
1938
    def insert_stream(self, stream, src_format, resume_tokens):
 
1939
        """Insert a stream's content into the target repository.
 
1940
 
 
1941
        :param src_format: a bzr repository format.
 
1942
 
 
1943
        :return: a list of resume tokens and an  iterable of keys additional
 
1944
            items required before the insertion can be completed.
 
1945
        """
 
1946
        self.target_repo.lock_write()
 
1947
        try:
 
1948
            if resume_tokens:
 
1949
                self.target_repo.resume_write_group(resume_tokens)
 
1950
                is_resume = True
 
1951
            else:
 
1952
                self.target_repo.start_write_group()
 
1953
                is_resume = False
 
1954
            try:
 
1955
                # locked_insert_stream performs a commit|suspend.
 
1956
                missing_keys = self.insert_stream_without_locking(stream,
 
1957
                                    src_format, is_resume)
 
1958
                if missing_keys:
 
1959
                    # suspend the write group and tell the caller what we is
 
1960
                    # missing. We know we can suspend or else we would not have
 
1961
                    # entered this code path. (All repositories that can handle
 
1962
                    # missing keys can handle suspending a write group).
 
1963
                    write_group_tokens = self.target_repo.suspend_write_group()
 
1964
                    return write_group_tokens, missing_keys
 
1965
                hint = self.target_repo.commit_write_group()
 
1966
                to_serializer = self.target_repo._format._serializer
 
1967
                src_serializer = src_format._serializer
 
1968
                if (to_serializer != src_serializer and
 
1969
                    self.target_repo._format.pack_compresses):
 
1970
                    self.target_repo.pack(hint=hint)
 
1971
                return [], set()
 
1972
            except:
 
1973
                self.target_repo.abort_write_group(suppress_errors=True)
 
1974
                raise
 
1975
        finally:
 
1976
            self.target_repo.unlock()
 
1977
 
 
1978
    def insert_stream_without_locking(self, stream, src_format,
 
1979
                                      is_resume=False):
 
1980
        """Insert a stream's content into the target repository.
 
1981
 
 
1982
        This assumes that you already have a locked repository and an active
 
1983
        write group.
 
1984
 
 
1985
        :param src_format: a bzr repository format.
 
1986
        :param is_resume: Passed down to get_missing_parent_inventories to
 
1987
            indicate if we should be checking for missing texts at the same
 
1988
            time.
 
1989
 
 
1990
        :return: A set of keys that are missing.
 
1991
        """
 
1992
        if not self.target_repo.is_write_locked():
 
1993
            raise errors.ObjectNotLocked(self)
 
1994
        if not self.target_repo.is_in_write_group():
 
1995
            raise errors.BzrError('you must already be in a write group')
 
1996
        to_serializer = self.target_repo._format._serializer
 
1997
        src_serializer = src_format._serializer
 
1998
        new_pack = None
 
1999
        if to_serializer == src_serializer:
 
2000
            # If serializers match and the target is a pack repository, set the
 
2001
            # write cache size on the new pack.  This avoids poor performance
 
2002
            # on transports where append is unbuffered (such as
 
2003
            # RemoteTransport).  This is safe to do because nothing should read
 
2004
            # back from the target repository while a stream with matching
 
2005
            # serialization is being inserted.
 
2006
            # The exception is that a delta record from the source that should
 
2007
            # be a fulltext may need to be expanded by the target (see
 
2008
            # test_fetch_revisions_with_deltas_into_pack); but we take care to
 
2009
            # explicitly flush any buffered writes first in that rare case.
 
2010
            try:
 
2011
                new_pack = self.target_repo._pack_collection._new_pack
 
2012
            except AttributeError:
 
2013
                # Not a pack repository
 
2014
                pass
 
2015
            else:
 
2016
                new_pack.set_write_cache_size(1024*1024)
 
2017
        for substream_type, substream in stream:
 
2018
            if 'stream' in debug.debug_flags:
 
2019
                mutter('inserting substream: %s', substream_type)
 
2020
            if substream_type == 'texts':
 
2021
                self.target_repo.texts.insert_record_stream(substream)
 
2022
            elif substream_type == 'inventories':
 
2023
                if src_serializer == to_serializer:
 
2024
                    self.target_repo.inventories.insert_record_stream(
 
2025
                        substream)
 
2026
                else:
 
2027
                    self._extract_and_insert_inventories(
 
2028
                        substream, src_serializer)
 
2029
            elif substream_type == 'inventory-deltas':
 
2030
                self._extract_and_insert_inventory_deltas(
 
2031
                    substream, src_serializer)
 
2032
            elif substream_type == 'chk_bytes':
 
2033
                # XXX: This doesn't support conversions, as it assumes the
 
2034
                #      conversion was done in the fetch code.
 
2035
                self.target_repo.chk_bytes.insert_record_stream(substream)
 
2036
            elif substream_type == 'revisions':
 
2037
                # This may fallback to extract-and-insert more often than
 
2038
                # required if the serializers are different only in terms of
 
2039
                # the inventory.
 
2040
                if src_serializer == to_serializer:
 
2041
                    self.target_repo.revisions.insert_record_stream(substream)
 
2042
                else:
 
2043
                    self._extract_and_insert_revisions(substream,
 
2044
                        src_serializer)
 
2045
            elif substream_type == 'signatures':
 
2046
                self.target_repo.signatures.insert_record_stream(substream)
 
2047
            else:
 
2048
                raise AssertionError('kaboom! %s' % (substream_type,))
 
2049
        # Done inserting data, and the missing_keys calculations will try to
 
2050
        # read back from the inserted data, so flush the writes to the new pack
 
2051
        # (if this is pack format).
 
2052
        if new_pack is not None:
 
2053
            new_pack._write_data('', flush=True)
 
2054
        # Find all the new revisions (including ones from resume_tokens)
 
2055
        missing_keys = self.target_repo.get_missing_parent_inventories(
 
2056
            check_for_missing_texts=is_resume)
 
2057
        try:
 
2058
            for prefix, versioned_file in (
 
2059
                ('texts', self.target_repo.texts),
 
2060
                ('inventories', self.target_repo.inventories),
 
2061
                ('revisions', self.target_repo.revisions),
 
2062
                ('signatures', self.target_repo.signatures),
 
2063
                ('chk_bytes', self.target_repo.chk_bytes),
 
2064
                ):
 
2065
                if versioned_file is None:
 
2066
                    continue
 
2067
                # TODO: key is often going to be a StaticTuple object
 
2068
                #       I don't believe we can define a method by which
 
2069
                #       (prefix,) + StaticTuple will work, though we could
 
2070
                #       define a StaticTuple.sq_concat that would allow you to
 
2071
                #       pass in either a tuple or a StaticTuple as the second
 
2072
                #       object, so instead we could have:
 
2073
                #       StaticTuple(prefix) + key here...
 
2074
                missing_keys.update((prefix,) + key for key in
 
2075
                    versioned_file.get_missing_compression_parent_keys())
 
2076
        except NotImplementedError:
 
2077
            # cannot even attempt suspending, and missing would have failed
 
2078
            # during stream insertion.
 
2079
            missing_keys = set()
 
2080
        return missing_keys
 
2081
 
 
2082
    def _extract_and_insert_inventory_deltas(self, substream, serializer):
 
2083
        target_rich_root = self.target_repo._format.rich_root_data
 
2084
        target_tree_refs = self.target_repo._format.supports_tree_reference
 
2085
        for record in substream:
 
2086
            # Insert the delta directly
 
2087
            inventory_delta_bytes = record.get_bytes_as('fulltext')
 
2088
            deserialiser = inventory_delta.InventoryDeltaDeserializer()
 
2089
            try:
 
2090
                parse_result = deserialiser.parse_text_bytes(
 
2091
                    inventory_delta_bytes)
 
2092
            except inventory_delta.IncompatibleInventoryDelta, err:
 
2093
                mutter("Incompatible delta: %s", err.msg)
 
2094
                raise errors.IncompatibleRevision(self.target_repo._format)
 
2095
            basis_id, new_id, rich_root, tree_refs, inv_delta = parse_result
 
2096
            revision_id = new_id
 
2097
            parents = [key[0] for key in record.parents]
 
2098
            self.target_repo.add_inventory_by_delta(
 
2099
                basis_id, inv_delta, revision_id, parents)
 
2100
 
 
2101
    def _extract_and_insert_inventories(self, substream, serializer,
 
2102
            parse_delta=None):
 
2103
        """Generate a new inventory versionedfile in target, converting data.
 
2104
 
 
2105
        The inventory is retrieved from the source, (deserializing it), and
 
2106
        stored in the target (reserializing it in a different format).
 
2107
        """
 
2108
        target_rich_root = self.target_repo._format.rich_root_data
 
2109
        target_tree_refs = self.target_repo._format.supports_tree_reference
 
2110
        for record in substream:
 
2111
            # It's not a delta, so it must be a fulltext in the source
 
2112
            # serializer's format.
 
2113
            bytes = record.get_bytes_as('fulltext')
 
2114
            revision_id = record.key[0]
 
2115
            inv = serializer.read_inventory_from_string(bytes, revision_id)
 
2116
            parents = [key[0] for key in record.parents]
 
2117
            self.target_repo.add_inventory(revision_id, inv, parents)
 
2118
            # No need to keep holding this full inv in memory when the rest of
 
2119
            # the substream is likely to be all deltas.
 
2120
            del inv
 
2121
 
 
2122
    def _extract_and_insert_revisions(self, substream, serializer):
 
2123
        for record in substream:
 
2124
            bytes = record.get_bytes_as('fulltext')
 
2125
            revision_id = record.key[0]
 
2126
            rev = serializer.read_revision_from_string(bytes)
 
2127
            if rev.revision_id != revision_id:
 
2128
                raise AssertionError('wtf: %s != %s' % (rev, revision_id))
 
2129
            self.target_repo.add_revision(revision_id, rev)
 
2130
 
 
2131
    def finished(self):
 
2132
        if self.target_repo._format._fetch_reconcile:
 
2133
            self.target_repo.reconcile()
 
2134
 
 
2135
 
 
2136
class StreamSource(object):
 
2137
    """A source of a stream for fetching between repositories."""
 
2138
 
 
2139
    def __init__(self, from_repository, to_format):
 
2140
        """Create a StreamSource streaming from from_repository."""
 
2141
        self.from_repository = from_repository
 
2142
        self.to_format = to_format
 
2143
        self._record_counter = RecordCounter()
 
2144
 
 
2145
    def delta_on_metadata(self):
 
2146
        """Return True if delta's are permitted on metadata streams.
 
2147
 
 
2148
        That is on revisions and signatures.
 
2149
        """
 
2150
        src_serializer = self.from_repository._format._serializer
 
2151
        target_serializer = self.to_format._serializer
 
2152
        return (self.to_format._fetch_uses_deltas and
 
2153
            src_serializer == target_serializer)
 
2154
 
 
2155
    def _fetch_revision_texts(self, revs):
 
2156
        # fetch signatures first and then the revision texts
 
2157
        # may need to be a InterRevisionStore call here.
 
2158
        from_sf = self.from_repository.signatures
 
2159
        # A missing signature is just skipped.
 
2160
        keys = [(rev_id,) for rev_id in revs]
 
2161
        signatures = versionedfile.filter_absent(from_sf.get_record_stream(
 
2162
            keys,
 
2163
            self.to_format._fetch_order,
 
2164
            not self.to_format._fetch_uses_deltas))
 
2165
        # If a revision has a delta, this is actually expanded inside the
 
2166
        # insert_record_stream code now, which is an alternate fix for
 
2167
        # bug #261339
 
2168
        from_rf = self.from_repository.revisions
 
2169
        revisions = from_rf.get_record_stream(
 
2170
            keys,
 
2171
            self.to_format._fetch_order,
 
2172
            not self.delta_on_metadata())
 
2173
        return [('signatures', signatures), ('revisions', revisions)]
 
2174
 
 
2175
    def _generate_root_texts(self, revs):
 
2176
        """This will be called by get_stream between fetching weave texts and
 
2177
        fetching the inventory weave.
 
2178
        """
 
2179
        if self._rich_root_upgrade():
 
2180
            return _mod_fetch.Inter1and2Helper(
 
2181
                self.from_repository).generate_root_texts(revs)
 
2182
        else:
 
2183
            return []
 
2184
 
 
2185
    def get_stream(self, search):
 
2186
        phase = 'file'
 
2187
        revs = search.get_keys()
 
2188
        graph = self.from_repository.get_graph()
 
2189
        revs = tsort.topo_sort(graph.get_parent_map(revs))
 
2190
        data_to_fetch = self.from_repository.item_keys_introduced_by(revs)
 
2191
        text_keys = []
 
2192
        for knit_kind, file_id, revisions in data_to_fetch:
 
2193
            if knit_kind != phase:
 
2194
                phase = knit_kind
 
2195
                # Make a new progress bar for this phase
 
2196
            if knit_kind == "file":
 
2197
                # Accumulate file texts
 
2198
                text_keys.extend([(file_id, revision) for revision in
 
2199
                    revisions])
 
2200
            elif knit_kind == "inventory":
 
2201
                # Now copy the file texts.
 
2202
                from_texts = self.from_repository.texts
 
2203
                yield ('texts', from_texts.get_record_stream(
 
2204
                    text_keys, self.to_format._fetch_order,
 
2205
                    not self.to_format._fetch_uses_deltas))
 
2206
                # Cause an error if a text occurs after we have done the
 
2207
                # copy.
 
2208
                text_keys = None
 
2209
                # Before we process the inventory we generate the root
 
2210
                # texts (if necessary) so that the inventories references
 
2211
                # will be valid.
 
2212
                for _ in self._generate_root_texts(revs):
 
2213
                    yield _
 
2214
                # we fetch only the referenced inventories because we do not
 
2215
                # know for unselected inventories whether all their required
 
2216
                # texts are present in the other repository - it could be
 
2217
                # corrupt.
 
2218
                for info in self._get_inventory_stream(revs):
 
2219
                    yield info
 
2220
            elif knit_kind == "signatures":
 
2221
                # Nothing to do here; this will be taken care of when
 
2222
                # _fetch_revision_texts happens.
 
2223
                pass
 
2224
            elif knit_kind == "revisions":
 
2225
                for record in self._fetch_revision_texts(revs):
 
2226
                    yield record
 
2227
            else:
 
2228
                raise AssertionError("Unknown knit kind %r" % knit_kind)
 
2229
 
 
2230
    def get_stream_for_missing_keys(self, missing_keys):
 
2231
        # missing keys can only occur when we are byte copying and not
 
2232
        # translating (because translation means we don't send
 
2233
        # unreconstructable deltas ever).
 
2234
        keys = {}
 
2235
        keys['texts'] = set()
 
2236
        keys['revisions'] = set()
 
2237
        keys['inventories'] = set()
 
2238
        keys['chk_bytes'] = set()
 
2239
        keys['signatures'] = set()
 
2240
        for key in missing_keys:
 
2241
            keys[key[0]].add(key[1:])
 
2242
        if len(keys['revisions']):
 
2243
            # If we allowed copying revisions at this point, we could end up
 
2244
            # copying a revision without copying its required texts: a
 
2245
            # violation of the requirements for repository integrity.
 
2246
            raise AssertionError(
 
2247
                'cannot copy revisions to fill in missing deltas %s' % (
 
2248
                    keys['revisions'],))
 
2249
        for substream_kind, keys in keys.iteritems():
 
2250
            vf = getattr(self.from_repository, substream_kind)
 
2251
            if vf is None and keys:
 
2252
                    raise AssertionError(
 
2253
                        "cannot fill in keys for a versioned file we don't"
 
2254
                        " have: %s needs %s" % (substream_kind, keys))
 
2255
            if not keys:
 
2256
                # No need to stream something we don't have
 
2257
                continue
 
2258
            if substream_kind == 'inventories':
 
2259
                # Some missing keys are genuinely ghosts, filter those out.
 
2260
                present = self.from_repository.inventories.get_parent_map(keys)
 
2261
                revs = [key[0] for key in present]
 
2262
                # Get the inventory stream more-or-less as we do for the
 
2263
                # original stream; there's no reason to assume that records
 
2264
                # direct from the source will be suitable for the sink.  (Think
 
2265
                # e.g. 2a -> 1.9-rich-root).
 
2266
                for info in self._get_inventory_stream(revs, missing=True):
 
2267
                    yield info
 
2268
                continue
 
2269
 
 
2270
            # Ask for full texts always so that we don't need more round trips
 
2271
            # after this stream.
 
2272
            # Some of the missing keys are genuinely ghosts, so filter absent
 
2273
            # records. The Sink is responsible for doing another check to
 
2274
            # ensure that ghosts don't introduce missing data for future
 
2275
            # fetches.
 
2276
            stream = versionedfile.filter_absent(vf.get_record_stream(keys,
 
2277
                self.to_format._fetch_order, True))
 
2278
            yield substream_kind, stream
 
2279
 
 
2280
    def inventory_fetch_order(self):
 
2281
        if self._rich_root_upgrade():
 
2282
            return 'topological'
 
2283
        else:
 
2284
            return self.to_format._fetch_order
 
2285
 
 
2286
    def _rich_root_upgrade(self):
 
2287
        return (not self.from_repository._format.rich_root_data and
 
2288
            self.to_format.rich_root_data)
 
2289
 
 
2290
    def _get_inventory_stream(self, revision_ids, missing=False):
 
2291
        from_format = self.from_repository._format
 
2292
        if (from_format.supports_chks and self.to_format.supports_chks and
 
2293
            from_format.network_name() == self.to_format.network_name()):
 
2294
            raise AssertionError(
 
2295
                "this case should be handled by GroupCHKStreamSource")
 
2296
        elif 'forceinvdeltas' in debug.debug_flags:
 
2297
            return self._get_convertable_inventory_stream(revision_ids,
 
2298
                    delta_versus_null=missing)
 
2299
        elif from_format.network_name() == self.to_format.network_name():
 
2300
            # Same format.
 
2301
            return self._get_simple_inventory_stream(revision_ids,
 
2302
                    missing=missing)
 
2303
        elif (not from_format.supports_chks and not self.to_format.supports_chks
 
2304
                and from_format._serializer == self.to_format._serializer):
 
2305
            # Essentially the same format.
 
2306
            return self._get_simple_inventory_stream(revision_ids,
 
2307
                    missing=missing)
 
2308
        else:
 
2309
            # Any time we switch serializations, we want to use an
 
2310
            # inventory-delta based approach.
 
2311
            return self._get_convertable_inventory_stream(revision_ids,
 
2312
                    delta_versus_null=missing)
 
2313
 
 
2314
    def _get_simple_inventory_stream(self, revision_ids, missing=False):
 
2315
        # NB: This currently reopens the inventory weave in source;
 
2316
        # using a single stream interface instead would avoid this.
 
2317
        from_weave = self.from_repository.inventories
 
2318
        if missing:
 
2319
            delta_closure = True
 
2320
        else:
 
2321
            delta_closure = not self.delta_on_metadata()
 
2322
        yield ('inventories', from_weave.get_record_stream(
 
2323
            [(rev_id,) for rev_id in revision_ids],
 
2324
            self.inventory_fetch_order(), delta_closure))
 
2325
 
 
2326
    def _get_convertable_inventory_stream(self, revision_ids,
 
2327
                                          delta_versus_null=False):
 
2328
        # The two formats are sufficiently different that there is no fast
 
2329
        # path, so we need to send just inventorydeltas, which any
 
2330
        # sufficiently modern client can insert into any repository.
 
2331
        # The StreamSink code expects to be able to
 
2332
        # convert on the target, so we need to put bytes-on-the-wire that can
 
2333
        # be converted.  That means inventory deltas (if the remote is <1.19,
 
2334
        # RemoteStreamSink will fallback to VFS to insert the deltas).
 
2335
        yield ('inventory-deltas',
 
2336
           self._stream_invs_as_deltas(revision_ids,
 
2337
                                       delta_versus_null=delta_versus_null))
 
2338
 
 
2339
    def _stream_invs_as_deltas(self, revision_ids, delta_versus_null=False):
 
2340
        """Return a stream of inventory-deltas for the given rev ids.
 
2341
 
 
2342
        :param revision_ids: The list of inventories to transmit
 
2343
        :param delta_versus_null: Don't try to find a minimal delta for this
 
2344
            entry, instead compute the delta versus the NULL_REVISION. This
 
2345
            effectively streams a complete inventory. Used for stuff like
 
2346
            filling in missing parents, etc.
 
2347
        """
 
2348
        from_repo = self.from_repository
 
2349
        revision_keys = [(rev_id,) for rev_id in revision_ids]
 
2350
        parent_map = from_repo.inventories.get_parent_map(revision_keys)
 
2351
        # XXX: possibly repos could implement a more efficient iter_inv_deltas
 
2352
        # method...
 
2353
        inventories = self.from_repository.iter_inventories(
 
2354
            revision_ids, 'topological')
 
2355
        format = from_repo._format
 
2356
        invs_sent_so_far = set([_mod_revision.NULL_REVISION])
 
2357
        inventory_cache = lru_cache.LRUCache(50)
 
2358
        null_inventory = from_repo.revision_tree(
 
2359
            _mod_revision.NULL_REVISION).inventory
 
2360
        # XXX: ideally the rich-root/tree-refs flags would be per-revision, not
 
2361
        # per-repo (e.g.  streaming a non-rich-root revision out of a rich-root
 
2362
        # repo back into a non-rich-root repo ought to be allowed)
 
2363
        serializer = inventory_delta.InventoryDeltaSerializer(
 
2364
            versioned_root=format.rich_root_data,
 
2365
            tree_references=format.supports_tree_reference)
 
2366
        for inv in inventories:
 
2367
            key = (inv.revision_id,)
 
2368
            parent_keys = parent_map.get(key, ())
 
2369
            delta = None
 
2370
            if not delta_versus_null and parent_keys:
 
2371
                # The caller did not ask for complete inventories and we have
 
2372
                # some parents that we can delta against.  Make a delta against
 
2373
                # each parent so that we can find the smallest.
 
2374
                parent_ids = [parent_key[0] for parent_key in parent_keys]
 
2375
                for parent_id in parent_ids:
 
2376
                    if parent_id not in invs_sent_so_far:
 
2377
                        # We don't know that the remote side has this basis, so
 
2378
                        # we can't use it.
 
2379
                        continue
 
2380
                    if parent_id == _mod_revision.NULL_REVISION:
 
2381
                        parent_inv = null_inventory
 
2382
                    else:
 
2383
                        parent_inv = inventory_cache.get(parent_id, None)
 
2384
                        if parent_inv is None:
 
2385
                            parent_inv = from_repo.get_inventory(parent_id)
 
2386
                    candidate_delta = inv._make_delta(parent_inv)
 
2387
                    if (delta is None or
 
2388
                        len(delta) > len(candidate_delta)):
 
2389
                        delta = candidate_delta
 
2390
                        basis_id = parent_id
 
2391
            if delta is None:
 
2392
                # Either none of the parents ended up being suitable, or we
 
2393
                # were asked to delta against NULL
 
2394
                basis_id = _mod_revision.NULL_REVISION
 
2395
                delta = inv._make_delta(null_inventory)
 
2396
            invs_sent_so_far.add(inv.revision_id)
 
2397
            inventory_cache[inv.revision_id] = inv
 
2398
            delta_serialized = ''.join(
 
2399
                serializer.delta_to_lines(basis_id, key[-1], delta))
 
2400
            yield versionedfile.FulltextContentFactory(
 
2401
                key, parent_keys, None, delta_serialized)
 
2402
 
 
2403
 
 
2404
class _VersionedFileChecker(object):
 
2405
 
 
2406
    def __init__(self, repository, text_key_references=None, ancestors=None):
 
2407
        self.repository = repository
 
2408
        self.text_index = self.repository._generate_text_key_index(
 
2409
            text_key_references=text_key_references, ancestors=ancestors)
 
2410
 
 
2411
    def calculate_file_version_parents(self, text_key):
 
2412
        """Calculate the correct parents for a file version according to
 
2413
        the inventories.
 
2414
        """
 
2415
        parent_keys = self.text_index[text_key]
 
2416
        if parent_keys == [_mod_revision.NULL_REVISION]:
 
2417
            return ()
 
2418
        return tuple(parent_keys)
 
2419
 
 
2420
    def check_file_version_parents(self, texts, progress_bar=None):
 
2421
        """Check the parents stored in a versioned file are correct.
 
2422
 
 
2423
        It also detects file versions that are not referenced by their
 
2424
        corresponding revision's inventory.
 
2425
 
 
2426
        :returns: A tuple of (wrong_parents, dangling_file_versions).
 
2427
            wrong_parents is a dict mapping {revision_id: (stored_parents,
 
2428
            correct_parents)} for each revision_id where the stored parents
 
2429
            are not correct.  dangling_file_versions is a set of (file_id,
 
2430
            revision_id) tuples for versions that are present in this versioned
 
2431
            file, but not used by the corresponding inventory.
 
2432
        """
 
2433
        local_progress = None
 
2434
        if progress_bar is None:
 
2435
            local_progress = ui.ui_factory.nested_progress_bar()
 
2436
            progress_bar = local_progress
 
2437
        try:
 
2438
            return self._check_file_version_parents(texts, progress_bar)
 
2439
        finally:
 
2440
            if local_progress:
 
2441
                local_progress.finished()
 
2442
 
 
2443
    def _check_file_version_parents(self, texts, progress_bar):
 
2444
        """See check_file_version_parents."""
 
2445
        wrong_parents = {}
 
2446
        self.file_ids = set([file_id for file_id, _ in
 
2447
            self.text_index.iterkeys()])
 
2448
        # text keys is now grouped by file_id
 
2449
        n_versions = len(self.text_index)
 
2450
        progress_bar.update('loading text store', 0, n_versions)
 
2451
        parent_map = self.repository.texts.get_parent_map(self.text_index)
 
2452
        # On unlistable transports this could well be empty/error...
 
2453
        text_keys = self.repository.texts.keys()
 
2454
        unused_keys = frozenset(text_keys) - set(self.text_index)
 
2455
        for num, key in enumerate(self.text_index.iterkeys()):
 
2456
            progress_bar.update('checking text graph', num, n_versions)
 
2457
            correct_parents = self.calculate_file_version_parents(key)
 
2458
            try:
 
2459
                knit_parents = parent_map[key]
 
2460
            except errors.RevisionNotPresent:
 
2461
                # Missing text!
 
2462
                knit_parents = None
 
2463
            if correct_parents != knit_parents:
 
2464
                wrong_parents[key] = (knit_parents, correct_parents)
 
2465
        return wrong_parents, unused_keys
 
2466
 
 
2467
 
 
2468
class InterVersionedFileRepository(InterRepository):
 
2469
 
 
2470
    _walk_to_common_revisions_batch_size = 50
 
2471
 
 
2472
    @needs_write_lock
 
2473
    def fetch(self, revision_id=None, find_ghosts=False,
 
2474
            fetch_spec=None):
 
2475
        """Fetch the content required to construct revision_id.
 
2476
 
 
2477
        The content is copied from self.source to self.target.
 
2478
 
 
2479
        :param revision_id: if None all content is copied, if NULL_REVISION no
 
2480
                            content is copied.
 
2481
        :return: None.
 
2482
        """
 
2483
        ui.ui_factory.warn_experimental_format_fetch(self)
 
2484
        from bzrlib.fetch import RepoFetcher
 
2485
        # See <https://launchpad.net/bugs/456077> asking for a warning here
 
2486
        if self.source._format.network_name() != self.target._format.network_name():
 
2487
            ui.ui_factory.show_user_warning('cross_format_fetch',
 
2488
                from_format=self.source._format,
 
2489
                to_format=self.target._format)
 
2490
        f = RepoFetcher(to_repository=self.target,
 
2491
                               from_repository=self.source,
 
2492
                               last_revision=revision_id,
 
2493
                               fetch_spec=fetch_spec,
 
2494
                               find_ghosts=find_ghosts)
 
2495
 
 
2496
    def _walk_to_common_revisions(self, revision_ids, if_present_ids=None):
 
2497
        """Walk out from revision_ids in source to revisions target has.
 
2498
 
 
2499
        :param revision_ids: The start point for the search.
 
2500
        :return: A set of revision ids.
 
2501
        """
 
2502
        target_graph = self.target.get_graph()
 
2503
        revision_ids = frozenset(revision_ids)
 
2504
        if if_present_ids:
 
2505
            all_wanted_revs = revision_ids.union(if_present_ids)
 
2506
        else:
 
2507
            all_wanted_revs = revision_ids
 
2508
        missing_revs = set()
 
2509
        source_graph = self.source.get_graph()
 
2510
        # ensure we don't pay silly lookup costs.
 
2511
        searcher = source_graph._make_breadth_first_searcher(all_wanted_revs)
 
2512
        null_set = frozenset([_mod_revision.NULL_REVISION])
 
2513
        searcher_exhausted = False
 
2514
        while True:
 
2515
            next_revs = set()
 
2516
            ghosts = set()
 
2517
            # Iterate the searcher until we have enough next_revs
 
2518
            while len(next_revs) < self._walk_to_common_revisions_batch_size:
 
2519
                try:
 
2520
                    next_revs_part, ghosts_part = searcher.next_with_ghosts()
 
2521
                    next_revs.update(next_revs_part)
 
2522
                    ghosts.update(ghosts_part)
 
2523
                except StopIteration:
 
2524
                    searcher_exhausted = True
 
2525
                    break
 
2526
            # If there are ghosts in the source graph, and the caller asked for
 
2527
            # them, make sure that they are present in the target.
 
2528
            # We don't care about other ghosts as we can't fetch them and
 
2529
            # haven't been asked to.
 
2530
            ghosts_to_check = set(revision_ids.intersection(ghosts))
 
2531
            revs_to_get = set(next_revs).union(ghosts_to_check)
 
2532
            if revs_to_get:
 
2533
                have_revs = set(target_graph.get_parent_map(revs_to_get))
 
2534
                # we always have NULL_REVISION present.
 
2535
                have_revs = have_revs.union(null_set)
 
2536
                # Check if the target is missing any ghosts we need.
 
2537
                ghosts_to_check.difference_update(have_revs)
 
2538
                if ghosts_to_check:
 
2539
                    # One of the caller's revision_ids is a ghost in both the
 
2540
                    # source and the target.
 
2541
                    raise errors.NoSuchRevision(
 
2542
                        self.source, ghosts_to_check.pop())
 
2543
                missing_revs.update(next_revs - have_revs)
 
2544
                # Because we may have walked past the original stop point, make
 
2545
                # sure everything is stopped
 
2546
                stop_revs = searcher.find_seen_ancestors(have_revs)
 
2547
                searcher.stop_searching_any(stop_revs)
 
2548
            if searcher_exhausted:
 
2549
                break
 
2550
        return searcher.get_result()
 
2551
 
 
2552
    @needs_read_lock
 
2553
    def search_missing_revision_ids(self,
 
2554
            revision_id=symbol_versioning.DEPRECATED_PARAMETER,
 
2555
            find_ghosts=True, revision_ids=None, if_present_ids=None):
 
2556
        """Return the revision ids that source has that target does not.
 
2557
 
 
2558
        :param revision_id: only return revision ids included by this
 
2559
            revision_id.
 
2560
        :param revision_ids: return revision ids included by these
 
2561
            revision_ids.  NoSuchRevision will be raised if any of these
 
2562
            revisions are not present.
 
2563
        :param if_present_ids: like revision_ids, but will not cause
 
2564
            NoSuchRevision if any of these are absent, instead they will simply
 
2565
            not be in the result.  This is useful for e.g. finding revisions
 
2566
            to fetch for tags, which may reference absent revisions.
 
2567
        :param find_ghosts: If True find missing revisions in deep history
 
2568
            rather than just finding the surface difference.
 
2569
        :return: A bzrlib.graph.SearchResult.
 
2570
        """
 
2571
        if symbol_versioning.deprecated_passed(revision_id):
 
2572
            symbol_versioning.warn(
 
2573
                'search_missing_revision_ids(revision_id=...) was '
 
2574
                'deprecated in 2.4.  Use revision_ids=[...] instead.',
 
2575
                DeprecationWarning, stacklevel=2)
 
2576
            if revision_ids is not None:
 
2577
                raise AssertionError(
 
2578
                    'revision_ids is mutually exclusive with revision_id')
 
2579
            if revision_id is not None:
 
2580
                revision_ids = [revision_id]
 
2581
        del revision_id
 
2582
        # stop searching at found target revisions.
 
2583
        if not find_ghosts and (revision_ids is not None or if_present_ids is
 
2584
                not None):
 
2585
            return self._walk_to_common_revisions(revision_ids,
 
2586
                    if_present_ids=if_present_ids)
 
2587
        # generic, possibly worst case, slow code path.
 
2588
        target_ids = set(self.target.all_revision_ids())
 
2589
        source_ids = self._present_source_revisions_for(
 
2590
            revision_ids, if_present_ids)
 
2591
        result_set = set(source_ids).difference(target_ids)
 
2592
        return self.source.revision_ids_to_search_result(result_set)
 
2593
 
 
2594
    def _present_source_revisions_for(self, revision_ids, if_present_ids=None):
 
2595
        """Returns set of all revisions in ancestry of revision_ids present in
 
2596
        the source repo.
 
2597
 
 
2598
        :param revision_ids: if None, all revisions in source are returned.
 
2599
        :param if_present_ids: like revision_ids, but if any/all of these are
 
2600
            absent no error is raised.
 
2601
        """
 
2602
        if revision_ids is not None or if_present_ids is not None:
 
2603
            # First, ensure all specified revisions exist.  Callers expect
 
2604
            # NoSuchRevision when they pass absent revision_ids here.
 
2605
            if revision_ids is None:
 
2606
                revision_ids = set()
 
2607
            if if_present_ids is None:
 
2608
                if_present_ids = set()
 
2609
            revision_ids = set(revision_ids)
 
2610
            if_present_ids = set(if_present_ids)
 
2611
            all_wanted_ids = revision_ids.union(if_present_ids)
 
2612
            graph = self.source.get_graph()
 
2613
            present_revs = set(graph.get_parent_map(all_wanted_ids))
 
2614
            missing = revision_ids.difference(present_revs)
 
2615
            if missing:
 
2616
                raise errors.NoSuchRevision(self.source, missing.pop())
 
2617
            found_ids = all_wanted_ids.intersection(present_revs)
 
2618
            source_ids = [rev_id for (rev_id, parents) in
 
2619
                          graph.iter_ancestry(found_ids)
 
2620
                          if rev_id != _mod_revision.NULL_REVISION
 
2621
                          and parents is not None]
 
2622
        else:
 
2623
            source_ids = self.source.all_revision_ids()
 
2624
        return set(source_ids)
 
2625
 
 
2626
    @classmethod
 
2627
    def _get_repo_format_to_test(self):
 
2628
        return None
 
2629
 
 
2630
    @classmethod
 
2631
    def is_compatible(cls, source, target):
 
2632
        # The default implementation is compatible with everything
 
2633
        return (source._format.supports_full_versioned_files and
 
2634
                target._format.supports_full_versioned_files)
 
2635
 
 
2636
 
 
2637
class InterDifferingSerializer(InterVersionedFileRepository):
 
2638
 
 
2639
    @classmethod
 
2640
    def _get_repo_format_to_test(self):
 
2641
        return None
 
2642
 
 
2643
    @staticmethod
 
2644
    def is_compatible(source, target):
 
2645
        if not source._format.supports_full_versioned_files:
 
2646
            return False
 
2647
        if not target._format.supports_full_versioned_files:
 
2648
            return False
 
2649
        # This is redundant with format.check_conversion_target(), however that
 
2650
        # raises an exception, and we just want to say "False" as in we won't
 
2651
        # support converting between these formats.
 
2652
        if 'IDS_never' in debug.debug_flags:
 
2653
            return False
 
2654
        if source.supports_rich_root() and not target.supports_rich_root():
 
2655
            return False
 
2656
        if (source._format.supports_tree_reference
 
2657
            and not target._format.supports_tree_reference):
 
2658
            return False
 
2659
        if target._fallback_repositories and target._format.supports_chks:
 
2660
            # IDS doesn't know how to copy CHKs for the parent inventories it
 
2661
            # adds to stacked repos.
 
2662
            return False
 
2663
        if 'IDS_always' in debug.debug_flags:
 
2664
            return True
 
2665
        # Only use this code path for local source and target.  IDS does far
 
2666
        # too much IO (both bandwidth and roundtrips) over a network.
 
2667
        if not source.bzrdir.transport.base.startswith('file:///'):
 
2668
            return False
 
2669
        if not target.bzrdir.transport.base.startswith('file:///'):
 
2670
            return False
 
2671
        return True
 
2672
 
 
2673
    def _get_trees(self, revision_ids, cache):
 
2674
        possible_trees = []
 
2675
        for rev_id in revision_ids:
 
2676
            if rev_id in cache:
 
2677
                possible_trees.append((rev_id, cache[rev_id]))
 
2678
            else:
 
2679
                # Not cached, but inventory might be present anyway.
 
2680
                try:
 
2681
                    tree = self.source.revision_tree(rev_id)
 
2682
                except errors.NoSuchRevision:
 
2683
                    # Nope, parent is ghost.
 
2684
                    pass
 
2685
                else:
 
2686
                    cache[rev_id] = tree
 
2687
                    possible_trees.append((rev_id, tree))
 
2688
        return possible_trees
 
2689
 
 
2690
    def _get_delta_for_revision(self, tree, parent_ids, possible_trees):
 
2691
        """Get the best delta and base for this revision.
 
2692
 
 
2693
        :return: (basis_id, delta)
 
2694
        """
 
2695
        deltas = []
 
2696
        # Generate deltas against each tree, to find the shortest.
 
2697
        texts_possibly_new_in_tree = set()
 
2698
        for basis_id, basis_tree in possible_trees:
 
2699
            delta = tree.inventory._make_delta(basis_tree.inventory)
 
2700
            for old_path, new_path, file_id, new_entry in delta:
 
2701
                if new_path is None:
 
2702
                    # This file_id isn't present in the new rev, so we don't
 
2703
                    # care about it.
 
2704
                    continue
 
2705
                if not new_path:
 
2706
                    # Rich roots are handled elsewhere...
 
2707
                    continue
 
2708
                kind = new_entry.kind
 
2709
                if kind != 'directory' and kind != 'file':
 
2710
                    # No text record associated with this inventory entry.
 
2711
                    continue
 
2712
                # This is a directory or file that has changed somehow.
 
2713
                texts_possibly_new_in_tree.add((file_id, new_entry.revision))
 
2714
            deltas.append((len(delta), basis_id, delta))
 
2715
        deltas.sort()
 
2716
        return deltas[0][1:]
 
2717
 
 
2718
    def _fetch_parent_invs_for_stacking(self, parent_map, cache):
 
2719
        """Find all parent revisions that are absent, but for which the
 
2720
        inventory is present, and copy those inventories.
 
2721
 
 
2722
        This is necessary to preserve correctness when the source is stacked
 
2723
        without fallbacks configured.  (Note that in cases like upgrade the
 
2724
        source may be not have _fallback_repositories even though it is
 
2725
        stacked.)
 
2726
        """
 
2727
        parent_revs = set()
 
2728
        for parents in parent_map.values():
 
2729
            parent_revs.update(parents)
 
2730
        present_parents = self.source.get_parent_map(parent_revs)
 
2731
        absent_parents = set(parent_revs).difference(present_parents)
 
2732
        parent_invs_keys_for_stacking = self.source.inventories.get_parent_map(
 
2733
            (rev_id,) for rev_id in absent_parents)
 
2734
        parent_inv_ids = [key[-1] for key in parent_invs_keys_for_stacking]
 
2735
        for parent_tree in self.source.revision_trees(parent_inv_ids):
 
2736
            current_revision_id = parent_tree.get_revision_id()
 
2737
            parents_parents_keys = parent_invs_keys_for_stacking[
 
2738
                (current_revision_id,)]
 
2739
            parents_parents = [key[-1] for key in parents_parents_keys]
 
2740
            basis_id = _mod_revision.NULL_REVISION
 
2741
            basis_tree = self.source.revision_tree(basis_id)
 
2742
            delta = parent_tree.inventory._make_delta(basis_tree.inventory)
 
2743
            self.target.add_inventory_by_delta(
 
2744
                basis_id, delta, current_revision_id, parents_parents)
 
2745
            cache[current_revision_id] = parent_tree
 
2746
 
 
2747
    def _fetch_batch(self, revision_ids, basis_id, cache):
 
2748
        """Fetch across a few revisions.
 
2749
 
 
2750
        :param revision_ids: The revisions to copy
 
2751
        :param basis_id: The revision_id of a tree that must be in cache, used
 
2752
            as a basis for delta when no other base is available
 
2753
        :param cache: A cache of RevisionTrees that we can use.
 
2754
        :return: The revision_id of the last converted tree. The RevisionTree
 
2755
            for it will be in cache
 
2756
        """
 
2757
        # Walk though all revisions; get inventory deltas, copy referenced
 
2758
        # texts that delta references, insert the delta, revision and
 
2759
        # signature.
 
2760
        root_keys_to_create = set()
 
2761
        text_keys = set()
 
2762
        pending_deltas = []
 
2763
        pending_revisions = []
 
2764
        parent_map = self.source.get_parent_map(revision_ids)
 
2765
        self._fetch_parent_invs_for_stacking(parent_map, cache)
 
2766
        self.source._safe_to_return_from_cache = True
 
2767
        for tree in self.source.revision_trees(revision_ids):
 
2768
            # Find a inventory delta for this revision.
 
2769
            # Find text entries that need to be copied, too.
 
2770
            current_revision_id = tree.get_revision_id()
 
2771
            parent_ids = parent_map.get(current_revision_id, ())
 
2772
            parent_trees = self._get_trees(parent_ids, cache)
 
2773
            possible_trees = list(parent_trees)
 
2774
            if len(possible_trees) == 0:
 
2775
                # There either aren't any parents, or the parents are ghosts,
 
2776
                # so just use the last converted tree.
 
2777
                possible_trees.append((basis_id, cache[basis_id]))
 
2778
            basis_id, delta = self._get_delta_for_revision(tree, parent_ids,
 
2779
                                                           possible_trees)
 
2780
            revision = self.source.get_revision(current_revision_id)
 
2781
            pending_deltas.append((basis_id, delta,
 
2782
                current_revision_id, revision.parent_ids))
 
2783
            if self._converting_to_rich_root:
 
2784
                self._revision_id_to_root_id[current_revision_id] = \
 
2785
                    tree.get_root_id()
 
2786
            # Determine which texts are in present in this revision but not in
 
2787
            # any of the available parents.
 
2788
            texts_possibly_new_in_tree = set()
 
2789
            for old_path, new_path, file_id, entry in delta:
 
2790
                if new_path is None:
 
2791
                    # This file_id isn't present in the new rev
 
2792
                    continue
 
2793
                if not new_path:
 
2794
                    # This is the root
 
2795
                    if not self.target.supports_rich_root():
 
2796
                        # The target doesn't support rich root, so we don't
 
2797
                        # copy
 
2798
                        continue
 
2799
                    if self._converting_to_rich_root:
 
2800
                        # This can't be copied normally, we have to insert
 
2801
                        # it specially
 
2802
                        root_keys_to_create.add((file_id, entry.revision))
 
2803
                        continue
 
2804
                kind = entry.kind
 
2805
                texts_possibly_new_in_tree.add((file_id, entry.revision))
 
2806
            for basis_id, basis_tree in possible_trees:
 
2807
                basis_inv = basis_tree.inventory
 
2808
                for file_key in list(texts_possibly_new_in_tree):
 
2809
                    file_id, file_revision = file_key
 
2810
                    try:
 
2811
                        entry = basis_inv[file_id]
 
2812
                    except errors.NoSuchId:
 
2813
                        continue
 
2814
                    if entry.revision == file_revision:
 
2815
                        texts_possibly_new_in_tree.remove(file_key)
 
2816
            text_keys.update(texts_possibly_new_in_tree)
 
2817
            pending_revisions.append(revision)
 
2818
            cache[current_revision_id] = tree
 
2819
            basis_id = current_revision_id
 
2820
        self.source._safe_to_return_from_cache = False
 
2821
        # Copy file texts
 
2822
        from_texts = self.source.texts
 
2823
        to_texts = self.target.texts
 
2824
        if root_keys_to_create:
 
2825
            root_stream = _mod_fetch._new_root_data_stream(
 
2826
                root_keys_to_create, self._revision_id_to_root_id, parent_map,
 
2827
                self.source)
 
2828
            to_texts.insert_record_stream(root_stream)
 
2829
        to_texts.insert_record_stream(from_texts.get_record_stream(
 
2830
            text_keys, self.target._format._fetch_order,
 
2831
            not self.target._format._fetch_uses_deltas))
 
2832
        # insert inventory deltas
 
2833
        for delta in pending_deltas:
 
2834
            self.target.add_inventory_by_delta(*delta)
 
2835
        if self.target._fallback_repositories:
 
2836
            # Make sure this stacked repository has all the parent inventories
 
2837
            # for the new revisions that we are about to insert.  We do this
 
2838
            # before adding the revisions so that no revision is added until
 
2839
            # all the inventories it may depend on are added.
 
2840
            # Note that this is overzealous, as we may have fetched these in an
 
2841
            # earlier batch.
 
2842
            parent_ids = set()
 
2843
            revision_ids = set()
 
2844
            for revision in pending_revisions:
 
2845
                revision_ids.add(revision.revision_id)
 
2846
                parent_ids.update(revision.parent_ids)
 
2847
            parent_ids.difference_update(revision_ids)
 
2848
            parent_ids.discard(_mod_revision.NULL_REVISION)
 
2849
            parent_map = self.source.get_parent_map(parent_ids)
 
2850
            # we iterate over parent_map and not parent_ids because we don't
 
2851
            # want to try copying any revision which is a ghost
 
2852
            for parent_tree in self.source.revision_trees(parent_map):
 
2853
                current_revision_id = parent_tree.get_revision_id()
 
2854
                parents_parents = parent_map[current_revision_id]
 
2855
                possible_trees = self._get_trees(parents_parents, cache)
 
2856
                if len(possible_trees) == 0:
 
2857
                    # There either aren't any parents, or the parents are
 
2858
                    # ghosts, so just use the last converted tree.
 
2859
                    possible_trees.append((basis_id, cache[basis_id]))
 
2860
                basis_id, delta = self._get_delta_for_revision(parent_tree,
 
2861
                    parents_parents, possible_trees)
 
2862
                self.target.add_inventory_by_delta(
 
2863
                    basis_id, delta, current_revision_id, parents_parents)
 
2864
        # insert signatures and revisions
 
2865
        for revision in pending_revisions:
 
2866
            try:
 
2867
                signature = self.source.get_signature_text(
 
2868
                    revision.revision_id)
 
2869
                self.target.add_signature_text(revision.revision_id,
 
2870
                    signature)
 
2871
            except errors.NoSuchRevision:
 
2872
                pass
 
2873
            self.target.add_revision(revision.revision_id, revision)
 
2874
        return basis_id
 
2875
 
 
2876
    def _fetch_all_revisions(self, revision_ids, pb):
 
2877
        """Fetch everything for the list of revisions.
 
2878
 
 
2879
        :param revision_ids: The list of revisions to fetch. Must be in
 
2880
            topological order.
 
2881
        :param pb: A ProgressTask
 
2882
        :return: None
 
2883
        """
 
2884
        basis_id, basis_tree = self._get_basis(revision_ids[0])
 
2885
        batch_size = 100
 
2886
        cache = lru_cache.LRUCache(100)
 
2887
        cache[basis_id] = basis_tree
 
2888
        del basis_tree # We don't want to hang on to it here
 
2889
        hints = []
 
2890
        a_graph = None
 
2891
 
 
2892
        for offset in range(0, len(revision_ids), batch_size):
 
2893
            self.target.start_write_group()
 
2894
            try:
 
2895
                pb.update('Transferring revisions', offset,
 
2896
                          len(revision_ids))
 
2897
                batch = revision_ids[offset:offset+batch_size]
 
2898
                basis_id = self._fetch_batch(batch, basis_id, cache)
 
2899
            except:
 
2900
                self.source._safe_to_return_from_cache = False
 
2901
                self.target.abort_write_group()
 
2902
                raise
 
2903
            else:
 
2904
                hint = self.target.commit_write_group()
 
2905
                if hint:
 
2906
                    hints.extend(hint)
 
2907
        if hints and self.target._format.pack_compresses:
 
2908
            self.target.pack(hint=hints)
 
2909
        pb.update('Transferring revisions', len(revision_ids),
 
2910
                  len(revision_ids))
 
2911
 
 
2912
    @needs_write_lock
 
2913
    def fetch(self, revision_id=None, find_ghosts=False,
 
2914
            fetch_spec=None):
 
2915
        """See InterRepository.fetch()."""
 
2916
        if fetch_spec is not None:
 
2917
            revision_ids = fetch_spec.get_keys()
 
2918
        else:
 
2919
            revision_ids = None
 
2920
        ui.ui_factory.warn_experimental_format_fetch(self)
 
2921
        if (not self.source.supports_rich_root()
 
2922
            and self.target.supports_rich_root()):
 
2923
            self._converting_to_rich_root = True
 
2924
            self._revision_id_to_root_id = {}
 
2925
        else:
 
2926
            self._converting_to_rich_root = False
 
2927
        # See <https://launchpad.net/bugs/456077> asking for a warning here
 
2928
        if self.source._format.network_name() != self.target._format.network_name():
 
2929
            ui.ui_factory.show_user_warning('cross_format_fetch',
 
2930
                from_format=self.source._format,
 
2931
                to_format=self.target._format)
 
2932
        if revision_ids is None:
 
2933
            if revision_id:
 
2934
                search_revision_ids = [revision_id]
 
2935
            else:
 
2936
                search_revision_ids = None
 
2937
            revision_ids = self.target.search_missing_revision_ids(self.source,
 
2938
                revision_ids=search_revision_ids,
 
2939
                find_ghosts=find_ghosts).get_keys()
 
2940
        if not revision_ids:
 
2941
            return 0, 0
 
2942
        revision_ids = tsort.topo_sort(
 
2943
            self.source.get_graph().get_parent_map(revision_ids))
 
2944
        if not revision_ids:
 
2945
            return 0, 0
 
2946
        # Walk though all revisions; get inventory deltas, copy referenced
 
2947
        # texts that delta references, insert the delta, revision and
 
2948
        # signature.
 
2949
        pb = ui.ui_factory.nested_progress_bar()
 
2950
        try:
 
2951
            self._fetch_all_revisions(revision_ids, pb)
 
2952
        finally:
 
2953
            pb.finished()
 
2954
        return len(revision_ids), 0
 
2955
 
 
2956
    def _get_basis(self, first_revision_id):
 
2957
        """Get a revision and tree which exists in the target.
 
2958
 
 
2959
        This assumes that first_revision_id is selected for transmission
 
2960
        because all other ancestors are already present. If we can't find an
 
2961
        ancestor we fall back to NULL_REVISION since we know that is safe.
 
2962
 
 
2963
        :return: (basis_id, basis_tree)
 
2964
        """
 
2965
        first_rev = self.source.get_revision(first_revision_id)
 
2966
        try:
 
2967
            basis_id = first_rev.parent_ids[0]
 
2968
            # only valid as a basis if the target has it
 
2969
            self.target.get_revision(basis_id)
 
2970
            # Try to get a basis tree - if it's a ghost it will hit the
 
2971
            # NoSuchRevision case.
 
2972
            basis_tree = self.source.revision_tree(basis_id)
 
2973
        except (IndexError, errors.NoSuchRevision):
 
2974
            basis_id = _mod_revision.NULL_REVISION
 
2975
            basis_tree = self.source.revision_tree(basis_id)
 
2976
        return basis_id, basis_tree
 
2977
 
 
2978
 
 
2979
class InterSameDataRepository(InterVersionedFileRepository):
 
2980
    """Code for converting between repositories that represent the same data.
 
2981
 
 
2982
    Data format and model must match for this to work.
 
2983
    """
 
2984
 
 
2985
    @classmethod
 
2986
    def _get_repo_format_to_test(self):
 
2987
        """Repository format for testing with.
 
2988
 
 
2989
        InterSameData can pull from subtree to subtree and from non-subtree to
 
2990
        non-subtree, so we test this with the richest repository format.
 
2991
        """
 
2992
        from bzrlib.repofmt import knitrepo
 
2993
        return knitrepo.RepositoryFormatKnit3()
 
2994
 
 
2995
    @staticmethod
 
2996
    def is_compatible(source, target):
 
2997
        return (
 
2998
            InterRepository._same_model(source, target) and
 
2999
            source._format.supports_full_versioned_files and
 
3000
            target._format.supports_full_versioned_files)
 
3001
 
 
3002
 
 
3003
InterRepository.register_optimiser(InterVersionedFileRepository)
 
3004
InterRepository.register_optimiser(InterDifferingSerializer)
 
3005
InterRepository.register_optimiser(InterSameDataRepository)
 
3006
 
 
3007
 
 
3008
def install_revisions(repository, iterable, num_revisions=None, pb=None):
 
3009
    """Install all revision data into a repository.
 
3010
 
 
3011
    Accepts an iterable of revision, tree, signature tuples.  The signature
 
3012
    may be None.
 
3013
    """
 
3014
    repository.start_write_group()
 
3015
    try:
 
3016
        inventory_cache = lru_cache.LRUCache(10)
 
3017
        for n, (revision, revision_tree, signature) in enumerate(iterable):
 
3018
            _install_revision(repository, revision, revision_tree, signature,
 
3019
                inventory_cache)
 
3020
            if pb is not None:
 
3021
                pb.update('Transferring revisions', n + 1, num_revisions)
 
3022
    except:
 
3023
        repository.abort_write_group()
 
3024
        raise
 
3025
    else:
 
3026
        repository.commit_write_group()
 
3027
 
 
3028
 
 
3029
def _install_revision(repository, rev, revision_tree, signature,
 
3030
    inventory_cache):
 
3031
    """Install all revision data into a repository."""
 
3032
    present_parents = []
 
3033
    parent_trees = {}
 
3034
    for p_id in rev.parent_ids:
 
3035
        if repository.has_revision(p_id):
 
3036
            present_parents.append(p_id)
 
3037
            parent_trees[p_id] = repository.revision_tree(p_id)
 
3038
        else:
 
3039
            parent_trees[p_id] = repository.revision_tree(
 
3040
                                     _mod_revision.NULL_REVISION)
 
3041
 
 
3042
    inv = revision_tree.inventory
 
3043
    entries = inv.iter_entries()
 
3044
    # backwards compatibility hack: skip the root id.
 
3045
    if not repository.supports_rich_root():
 
3046
        path, root = entries.next()
 
3047
        if root.revision != rev.revision_id:
 
3048
            raise errors.IncompatibleRevision(repr(repository))
 
3049
    text_keys = {}
 
3050
    for path, ie in entries:
 
3051
        text_keys[(ie.file_id, ie.revision)] = ie
 
3052
    text_parent_map = repository.texts.get_parent_map(text_keys)
 
3053
    missing_texts = set(text_keys) - set(text_parent_map)
 
3054
    # Add the texts that are not already present
 
3055
    for text_key in missing_texts:
 
3056
        ie = text_keys[text_key]
 
3057
        text_parents = []
 
3058
        # FIXME: TODO: The following loop overlaps/duplicates that done by
 
3059
        # commit to determine parents. There is a latent/real bug here where
 
3060
        # the parents inserted are not those commit would do - in particular
 
3061
        # they are not filtered by heads(). RBC, AB
 
3062
        for revision, tree in parent_trees.iteritems():
 
3063
            if ie.file_id not in tree:
 
3064
                continue
 
3065
            parent_id = tree.get_file_revision(ie.file_id)
 
3066
            if parent_id in text_parents:
 
3067
                continue
 
3068
            text_parents.append((ie.file_id, parent_id))
 
3069
        lines = revision_tree.get_file(ie.file_id).readlines()
 
3070
        repository.texts.add_lines(text_key, text_parents, lines)
 
3071
    try:
 
3072
        # install the inventory
 
3073
        if repository._format._commit_inv_deltas and len(rev.parent_ids):
 
3074
            # Cache this inventory
 
3075
            inventory_cache[rev.revision_id] = inv
 
3076
            try:
 
3077
                basis_inv = inventory_cache[rev.parent_ids[0]]
 
3078
            except KeyError:
 
3079
                repository.add_inventory(rev.revision_id, inv, present_parents)
 
3080
            else:
 
3081
                delta = inv._make_delta(basis_inv)
 
3082
                repository.add_inventory_by_delta(rev.parent_ids[0], delta,
 
3083
                    rev.revision_id, present_parents)
 
3084
        else:
 
3085
            repository.add_inventory(rev.revision_id, inv, present_parents)
 
3086
    except errors.RevisionAlreadyPresent:
 
3087
        pass
 
3088
    if signature is not None:
 
3089
        repository.add_signature_text(rev.revision_id, signature)
 
3090
    repository.add_revision(rev.revision_id, rev, inv)
 
3091
 
 
3092
 
 
3093
def install_revision(repository, rev, revision_tree):
 
3094
    """Install all revision data into a repository."""
 
3095
    install_revisions(repository, [(rev, revision_tree, None)])