/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: Jonathan Riddell
  • Date: 2011-09-12 16:31:46 UTC
  • mto: This revision was merged to the branch mainline in revision 6132.
  • Revision ID: jriddell@canonical.com-20110912163146-s1cukb9b6498fmz1
do not use i18n in tests

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