~bzr-pqm/bzr/bzr.dev

5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
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(), """
5852.1.8 by Jelmer Vernooij
Simplify revision limiting.
22
import itertools
23
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
24
from bzrlib import (
5850.1.3 by Jelmer Vernooij
Add VersionedFileCheck.
25
    check,
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
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,
5863.4.1 by Jelmer Vernooij
Move interrepository implementation to vf_repository.
37
    symbol_versioning,
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
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,
5815.4.2 by Jelmer Vernooij
split out versionedfile-specific stuff from commitbuilder.
58
    InventoryDirectory,
59
    ROOT_ID,
60
    entry_factory,
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
61
    )
62
63
from bzrlib.repository import (
5815.4.2 by Jelmer Vernooij
split out versionedfile-specific stuff from commitbuilder.
64
    CommitBuilder,
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
65
    InterRepository,
66
    MetaDirRepository,
5815.4.5 by Jelmer Vernooij
Use MetaDirVersionedFileRepositoryFormat (a Soyuz worthy name).
67
    MetaDirRepositoryFormat,
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
68
    Repository,
5815.4.5 by Jelmer Vernooij
Use MetaDirVersionedFileRepositoryFormat (a Soyuz worthy name).
69
    RepositoryFormat,
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
70
    )
71
72
from bzrlib.trace import (
73
    mutter,
74
    )
75
76
5815.4.5 by Jelmer Vernooij
Use MetaDirVersionedFileRepositoryFormat (a Soyuz worthy name).
77
class VersionedFileRepositoryFormat(RepositoryFormat):
5815.4.16 by Jelmer Vernooij
Move supports_versioned_files setting to VersionedFileRepositoryFormat.
78
    """Base class for all repository formats that are VersionedFiles-based."""
79
80
    supports_full_versioned_files = True
5993.3.2 by Jelmer Vernooij
Add Repository.supports_versioned_directories.
81
    supports_versioned_directories = True
5815.4.5 by Jelmer Vernooij
Use MetaDirVersionedFileRepositoryFormat (a Soyuz worthy name).
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
5815.4.2 by Jelmer Vernooij
split out versionedfile-specific stuff from commitbuilder.
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)
5847.3.2 by Jelmer Vernooij
Move root checking to VersionedFileRepository.
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
5815.4.2 by Jelmer Vernooij
split out versionedfile-specific stuff from commitbuilder.
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."""
5967.7.1 by Martin Pool
Deprecate __contains__ on Tree and Inventory
282
        if not basis_inv.has_id(ie.file_id):
5815.4.2 by Jelmer Vernooij
split out versionedfile-specific stuff from commitbuilder.
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.
5967.7.1 by Martin Pool
Deprecate __contains__ on Tree and Inventory
401
                if basis_inv.has_id(ie.file_id):
5815.4.2 by Jelmer Vernooij
split out versionedfile-specific stuff from commitbuilder.
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_candiate_entries = ie.parent_candidates(parent_invs)
423
        head_set = self._heads(ie.file_id, parent_candiate_entries.keys())
424
        heads = []
425
        for inv in parent_invs:
5967.7.1 by Martin Pool
Deprecate __contains__ on Tree and Inventory
426
            if inv.has_id(ie.file_id):
5815.4.2 by Jelmer Vernooij
split out versionedfile-specific stuff from commitbuilder.
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_candiate_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
5847.3.2 by Jelmer Vernooij
Move root checking to VersionedFileRepository.
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
5815.4.2 by Jelmer Vernooij
split out versionedfile-specific stuff from commitbuilder.
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 beefore 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
5847.3.2 by Jelmer Vernooij
Move root checking to VersionedFileRepository.
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)):
5815.4.2 by Jelmer Vernooij
split out versionedfile-specific stuff from commitbuilder.
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
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
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
5815.4.2 by Jelmer Vernooij
split out versionedfile-specific stuff from commitbuilder.
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
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
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
        if self.is_locked():
922
            # This repository will call fallback.unlock() when we transition to
923
            # the unlocked state, so we make sure to increment the lock count
924
            repository.lock_read()
925
        self._check_fallback_repository(repository)
926
        self._fallback_repositories.append(repository)
927
        self.texts.add_fallback_versioned_files(repository.texts)
928
        self.inventories.add_fallback_versioned_files(repository.inventories)
929
        self.revisions.add_fallback_versioned_files(repository.revisions)
930
        self.signatures.add_fallback_versioned_files(repository.signatures)
931
        if self.chk_bytes is not None:
932
            self.chk_bytes.add_fallback_versioned_files(repository.chk_bytes)
933
934
    @only_raises(errors.LockNotHeld, errors.LockBroken)
935
    def unlock(self):
936
        super(VersionedFileRepository, self).unlock()
937
        if self.control_files._lock_count == 0:
938
            self._inventory_entry_cache.clear()
939
940
    def add_inventory(self, revision_id, inv, parents):
941
        """Add the inventory inv to the repository as revision_id.
942
943
        :param parents: The revision ids of the parents that revision_id
944
                        is known to have and are in the repository already.
945
946
        :returns: The validator(which is a sha1 digest, though what is sha'd is
947
            repository format specific) of the serialized inventory.
948
        """
949
        if not self.is_in_write_group():
950
            raise AssertionError("%r not in write group" % (self,))
951
        _mod_revision.check_not_reserved_id(revision_id)
952
        if not (inv.revision_id is None or inv.revision_id == revision_id):
953
            raise AssertionError(
954
                "Mismatch between inventory revision"
955
                " id and insertion revid (%r, %r)"
956
                % (inv.revision_id, revision_id))
957
        if inv.root is None:
958
            raise errors.RootMissing()
959
        return self._add_inventory_checked(revision_id, inv, parents)
960
961
    def _add_inventory_checked(self, revision_id, inv, parents):
962
        """Add inv to the repository after checking the inputs.
963
964
        This function can be overridden to allow different inventory styles.
965
966
        :seealso: add_inventory, for the contract.
967
        """
968
        inv_lines = self._serializer.write_inventory_to_lines(inv)
969
        return self._inventory_add_lines(revision_id, parents,
970
            inv_lines, check_content=False)
971
972
    def add_inventory_by_delta(self, basis_revision_id, delta, new_revision_id,
973
                               parents, basis_inv=None, propagate_caches=False):
974
        """Add a new inventory expressed as a delta against another revision.
975
976
        See the inventory developers documentation for the theory behind
977
        inventory deltas.
978
979
        :param basis_revision_id: The inventory id the delta was created
980
            against. (This does not have to be a direct parent.)
981
        :param delta: The inventory delta (see Inventory.apply_delta for
982
            details).
983
        :param new_revision_id: The revision id that the inventory is being
984
            added for.
985
        :param parents: The revision ids of the parents that revision_id is
986
            known to have and are in the repository already. These are supplied
987
            for repositories that depend on the inventory graph for revision
988
            graph access, as well as for those that pun ancestry with delta
989
            compression.
990
        :param basis_inv: The basis inventory if it is already known,
991
            otherwise None.
992
        :param propagate_caches: If True, the caches for this inventory are
993
          copied to and updated for the result if possible.
994
995
        :returns: (validator, new_inv)
996
            The validator(which is a sha1 digest, though what is sha'd is
997
            repository format specific) of the serialized inventory, and the
998
            resulting inventory.
999
        """
1000
        if not self.is_in_write_group():
1001
            raise AssertionError("%r not in write group" % (self,))
1002
        _mod_revision.check_not_reserved_id(new_revision_id)
1003
        basis_tree = self.revision_tree(basis_revision_id)
1004
        basis_tree.lock_read()
1005
        try:
1006
            # Note that this mutates the inventory of basis_tree, which not all
1007
            # inventory implementations may support: A better idiom would be to
1008
            # return a new inventory, but as there is no revision tree cache in
1009
            # repository this is safe for now - RBC 20081013
1010
            if basis_inv is None:
1011
                basis_inv = basis_tree.inventory
1012
            basis_inv.apply_delta(delta)
1013
            basis_inv.revision_id = new_revision_id
1014
            return (self.add_inventory(new_revision_id, basis_inv, parents),
1015
                    basis_inv)
1016
        finally:
1017
            basis_tree.unlock()
1018
1019
    def _inventory_add_lines(self, revision_id, parents, lines,
1020
        check_content=True):
1021
        """Store lines in inv_vf and return the sha1 of the inventory."""
1022
        parents = [(parent,) for parent in parents]
1023
        result = self.inventories.add_lines((revision_id,), parents, lines,
1024
            check_content=check_content)[0]
1025
        self.inventories._access.flush()
1026
        return result
1027
1028
    def add_revision(self, revision_id, rev, inv=None, config=None):
1029
        """Add rev to the revision store as revision_id.
1030
1031
        :param revision_id: the revision id to use.
1032
        :param rev: The revision object.
1033
        :param inv: The inventory for the revision. if None, it will be looked
1034
                    up in the inventory storer
1035
        :param config: If None no digital signature will be created.
1036
                       If supplied its signature_needed method will be used
1037
                       to determine if a signature should be made.
1038
        """
1039
        # TODO: jam 20070210 Shouldn't we check rev.revision_id and
1040
        #       rev.parent_ids?
1041
        _mod_revision.check_not_reserved_id(revision_id)
1042
        if config is not None and config.signature_needed():
1043
            if inv is None:
1044
                inv = self.get_inventory(revision_id)
1045
            tree = InventoryRevisionTree(self, inv, revision_id)
1046
            testament = Testament(rev, tree)
1047
            plaintext = testament.as_short_text()
1048
            self.store_revision_signature(
1049
                gpg.GPGStrategy(config), plaintext, revision_id)
1050
        # check inventory present
1051
        if not self.inventories.get_parent_map([(revision_id,)]):
1052
            if inv is None:
1053
                raise errors.WeaveRevisionNotPresent(revision_id,
1054
                                                     self.inventories)
1055
            else:
1056
                # yes, this is not suitable for adding with ghosts.
1057
                rev.inventory_sha1 = self.add_inventory(revision_id, inv,
1058
                                                        rev.parent_ids)
1059
        else:
1060
            key = (revision_id,)
1061
            rev.inventory_sha1 = self.inventories.get_sha1s([key])[key]
1062
        self._add_revision(rev)
1063
1064
    def _add_revision(self, revision):
1065
        text = self._serializer.write_revision_to_string(revision)
1066
        key = (revision.revision_id,)
1067
        parents = tuple((parent,) for parent in revision.parent_ids)
1068
        self.revisions.add_lines(key, parents, osutils.split_lines(text))
1069
1070
    def _check_inventories(self, checker):
1071
        """Check the inventories found from the revision scan.
1072
        
1073
        This is responsible for verifying the sha1 of inventories and
1074
        creating a pending_keys set that covers data referenced by inventories.
1075
        """
1076
        bar = ui.ui_factory.nested_progress_bar()
1077
        try:
1078
            self._do_check_inventories(checker, bar)
1079
        finally:
1080
            bar.finished()
1081
1082
    def _do_check_inventories(self, checker, bar):
1083
        """Helper for _check_inventories."""
1084
        revno = 0
1085
        keys = {'chk_bytes':set(), 'inventories':set(), 'texts':set()}
1086
        kinds = ['chk_bytes', 'texts']
1087
        count = len(checker.pending_keys)
1088
        bar.update("inventories", 0, 2)
1089
        current_keys = checker.pending_keys
1090
        checker.pending_keys = {}
1091
        # Accumulate current checks.
1092
        for key in current_keys:
1093
            if key[0] != 'inventories' and key[0] not in kinds:
1094
                checker._report_items.append('unknown key type %r' % (key,))
1095
            keys[key[0]].add(key[1:])
1096
        if keys['inventories']:
1097
            # NB: output order *should* be roughly sorted - topo or
1098
            # inverse topo depending on repository - either way decent
1099
            # to just delta against. However, pre-CHK formats didn't
1100
            # try to optimise inventory layout on disk. As such the
1101
            # pre-CHK code path does not use inventory deltas.
1102
            last_object = None
1103
            for record in self.inventories.check(keys=keys['inventories']):
1104
                if record.storage_kind == 'absent':
1105
                    checker._report_items.append(
1106
                        'Missing inventory {%s}' % (record.key,))
1107
                else:
1108
                    last_object = self._check_record('inventories', record,
1109
                        checker, last_object,
1110
                        current_keys[('inventories',) + record.key])
1111
            del keys['inventories']
1112
        else:
1113
            return
1114
        bar.update("texts", 1)
1115
        while (checker.pending_keys or keys['chk_bytes']
1116
            or keys['texts']):
1117
            # Something to check.
1118
            current_keys = checker.pending_keys
1119
            checker.pending_keys = {}
1120
            # Accumulate current checks.
1121
            for key in current_keys:
1122
                if key[0] not in kinds:
1123
                    checker._report_items.append('unknown key type %r' % (key,))
1124
                keys[key[0]].add(key[1:])
1125
            # Check the outermost kind only - inventories || chk_bytes || texts
1126
            for kind in kinds:
1127
                if keys[kind]:
1128
                    last_object = None
1129
                    for record in getattr(self, kind).check(keys=keys[kind]):
1130
                        if record.storage_kind == 'absent':
1131
                            checker._report_items.append(
1132
                                'Missing %s {%s}' % (kind, record.key,))
1133
                        else:
1134
                            last_object = self._check_record(kind, record,
1135
                                checker, last_object, current_keys[(kind,) + record.key])
1136
                    keys[kind] = set()
1137
                    break
1138
1139
    def _check_record(self, kind, record, checker, last_object, item_data):
1140
        """Check a single text from this repository."""
1141
        if kind == 'inventories':
1142
            rev_id = record.key[0]
1143
            inv = self._deserialise_inventory(rev_id,
1144
                record.get_bytes_as('fulltext'))
1145
            if last_object is not None:
1146
                delta = inv._make_delta(last_object)
1147
                for old_path, path, file_id, ie in delta:
1148
                    if ie is None:
1149
                        continue
1150
                    ie.check(checker, rev_id, inv)
1151
            else:
1152
                for path, ie in inv.iter_entries():
1153
                    ie.check(checker, rev_id, inv)
1154
            if self._format.fast_deltas:
1155
                return inv
1156
        elif kind == 'chk_bytes':
1157
            # No code written to check chk_bytes for this repo format.
1158
            checker._report_items.append(
1159
                'unsupported key type chk_bytes for %s' % (record.key,))
1160
        elif kind == 'texts':
1161
            self._check_text(record, checker, item_data)
1162
        else:
1163
            checker._report_items.append(
1164
                'unknown key type %s for %s' % (kind, record.key))
1165
1166
    def _check_text(self, record, checker, item_data):
1167
        """Check a single text."""
1168
        # Check it is extractable.
1169
        # TODO: check length.
1170
        if record.storage_kind == 'chunked':
1171
            chunks = record.get_bytes_as(record.storage_kind)
1172
            sha1 = osutils.sha_strings(chunks)
1173
            length = sum(map(len, chunks))
1174
        else:
1175
            content = record.get_bytes_as('fulltext')
1176
            sha1 = osutils.sha_string(content)
1177
            length = len(content)
1178
        if item_data and sha1 != item_data[1]:
1179
            checker._report_items.append(
1180
                'sha1 mismatch: %s has sha1 %s expected %s referenced by %s' %
1181
                (record.key, sha1, item_data[1], item_data[2]))
1182
6039.2.1 by Jelmer Vernooij
Move Repository._eliminate_revisions_not_present to VFRepository.
1183
    @needs_read_lock
1184
    def _eliminate_revisions_not_present(self, revision_ids):
1185
        """Check every revision id in revision_ids to see if we have it.
1186
1187
        Returns a set of the present revisions.
1188
        """
1189
        result = []
1190
        graph = self.get_graph()
1191
        parent_map = graph.get_parent_map(revision_ids)
1192
        # The old API returned a list, should this actually be a set?
1193
        return parent_map.keys()
1194
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
1195
    def __init__(self, _format, a_bzrdir, control_files):
1196
        """Instantiate a VersionedFileRepository.
1197
1198
        :param _format: The format of the repository on disk.
1199
        :param a_bzrdir: The BzrDir of the repository.
1200
        :param control_files: Control files to use for locking, etc.
1201
        """
1202
        # In the future we will have a single api for all stores for
1203
        # getting file texts, inventories and revisions, then
1204
        # this construct will accept instances of those things.
1205
        super(VersionedFileRepository, self).__init__(_format, a_bzrdir,
1206
            control_files)
1207
        # for tests
1208
        self._reconcile_does_inventory_gc = True
1209
        self._reconcile_fixes_text_parents = False
1210
        self._reconcile_backsup_inventory = True
1211
        # An InventoryEntry cache, used during deserialization
1212
        self._inventory_entry_cache = fifo_cache.FIFOCache(10*1024)
1213
        # Is it safe to return inventory entries directly from the entry cache,
1214
        # rather copying them?
1215
        self._safe_to_return_from_cache = False
1216
5815.4.21 by Jelmer Vernooij
Fix lock.
1217
    @needs_read_lock
1218
    def gather_stats(self, revid=None, committers=None):
5815.4.18 by Jelmer Vernooij
Move vf-specific gather_stats bits to vf_repository.
1219
        """See Repository.gather_stats()."""
5815.4.21 by Jelmer Vernooij
Fix lock.
1220
        result = super(VersionedFileRepository, self).gather_stats(revid, committers)
5815.4.18 by Jelmer Vernooij
Move vf-specific gather_stats bits to vf_repository.
1221
        # now gather global repository information
1222
        # XXX: This is available for many repos regardless of listability.
1223
        if self.user_transport.listable():
1224
            # XXX: do we want to __define len__() ?
1225
            # Maybe the versionedfiles object should provide a different
1226
            # method to get the number of keys.
1227
            result['revisions'] = len(self.revisions.keys())
1228
            # result['size'] = t
1229
        return result
1230
5815.4.2 by Jelmer Vernooij
split out versionedfile-specific stuff from commitbuilder.
1231
    def get_commit_builder(self, branch, parents, config, timestamp=None,
1232
                           timezone=None, committer=None, revprops=None,
1233
                           revision_id=None, lossy=False):
1234
        """Obtain a CommitBuilder for this repository.
1235
1236
        :param branch: Branch to commit to.
1237
        :param parents: Revision ids of the parents of the new revision.
1238
        :param config: Configuration to use.
1239
        :param timestamp: Optional timestamp recorded for commit.
1240
        :param timezone: Optional timezone for timestamp.
1241
        :param committer: Optional committer to set for commit.
1242
        :param revprops: Optional dictionary of revision properties.
1243
        :param revision_id: Optional revision id.
1244
        :param lossy: Whether to discard data that can not be natively
1245
            represented, when pushing to a foreign VCS
1246
        """
1247
        if self._fallback_repositories and not self._format.supports_chks:
1248
            raise errors.BzrError("Cannot commit directly to a stacked branch"
1249
                " in pre-2a formats. See "
1250
                "https://bugs.launchpad.net/bzr/+bug/375013 for details.")
1251
        result = self._commit_builder_class(self, parents, config,
1252
            timestamp, timezone, committer, revprops, revision_id,
1253
            lossy)
1254
        self.start_write_group()
1255
        return result
1256
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
1257
    def get_missing_parent_inventories(self, check_for_missing_texts=True):
1258
        """Return the keys of missing inventory parents for revisions added in
1259
        this write group.
1260
1261
        A revision is not complete if the inventory delta for that revision
1262
        cannot be calculated.  Therefore if the parent inventories of a
1263
        revision are not present, the revision is incomplete, and e.g. cannot
1264
        be streamed by a smart server.  This method finds missing inventory
1265
        parents for revisions added in this write group.
1266
        """
1267
        if not self._format.supports_external_lookups:
1268
            # This is only an issue for stacked repositories
1269
            return set()
1270
        if not self.is_in_write_group():
1271
            raise AssertionError('not in a write group')
1272
1273
        # XXX: We assume that every added revision already has its
1274
        # corresponding inventory, so we only check for parent inventories that
1275
        # might be missing, rather than all inventories.
1276
        parents = set(self.revisions._index.get_missing_parents())
1277
        parents.discard(_mod_revision.NULL_REVISION)
1278
        unstacked_inventories = self.inventories._index
1279
        present_inventories = unstacked_inventories.get_parent_map(
1280
            key[-1:] for key in parents)
1281
        parents.difference_update(present_inventories)
1282
        if len(parents) == 0:
1283
            # No missing parent inventories.
1284
            return set()
1285
        if not check_for_missing_texts:
1286
            return set(('inventories', rev_id) for (rev_id,) in parents)
1287
        # Ok, now we have a list of missing inventories.  But these only matter
1288
        # if the inventories that reference them are missing some texts they
1289
        # appear to introduce.
1290
        # XXX: Texts referenced by all added inventories need to be present,
1291
        # but at the moment we're only checking for texts referenced by
1292
        # inventories at the graph's edge.
1293
        key_deps = self.revisions._index._key_dependencies
1294
        key_deps.satisfy_refs_for_keys(present_inventories)
1295
        referrers = frozenset(r[0] for r in key_deps.get_referrers())
1296
        file_ids = self.fileids_altered_by_revision_ids(referrers)
1297
        missing_texts = set()
1298
        for file_id, version_ids in file_ids.iteritems():
1299
            missing_texts.update(
1300
                (file_id, version_id) for version_id in version_ids)
1301
        present_texts = self.texts.get_parent_map(missing_texts)
1302
        missing_texts.difference_update(present_texts)
1303
        if not missing_texts:
1304
            # No texts are missing, so all revisions and their deltas are
1305
            # reconstructable.
1306
            return set()
1307
        # Alternatively the text versions could be returned as the missing
1308
        # keys, but this is likely to be less data.
1309
        missing_keys = set(('inventories', rev_id) for (rev_id,) in parents)
1310
        return missing_keys
1311
1312
    @needs_read_lock
1313
    def has_revisions(self, revision_ids):
1314
        """Probe to find out the presence of multiple revisions.
1315
1316
        :param revision_ids: An iterable of revision_ids.
1317
        :return: A set of the revision_ids that were present.
1318
        """
1319
        parent_map = self.revisions.get_parent_map(
1320
            [(rev_id,) for rev_id in revision_ids])
1321
        result = set()
1322
        if _mod_revision.NULL_REVISION in revision_ids:
1323
            result.add(_mod_revision.NULL_REVISION)
1324
        result.update([key[0] for key in parent_map])
1325
        return result
1326
1327
    @needs_read_lock
1328
    def get_revision_reconcile(self, revision_id):
1329
        """'reconcile' helper routine that allows access to a revision always.
1330
1331
        This variant of get_revision does not cross check the weave graph
1332
        against the revision one as get_revision does: but it should only
1333
        be used by reconcile, or reconcile-alike commands that are correcting
1334
        or testing the revision graph.
1335
        """
1336
        return self._get_revisions([revision_id])[0]
1337
1338
    @needs_read_lock
1339
    def get_revisions(self, revision_ids):
1340
        """Get many revisions at once.
1341
        
1342
        Repositories that need to check data on every revision read should 
1343
        subclass this method.
1344
        """
1345
        return self._get_revisions(revision_ids)
1346
1347
    @needs_read_lock
1348
    def _get_revisions(self, revision_ids):
1349
        """Core work logic to get many revisions without sanity checks."""
1350
        revs = {}
1351
        for revid, rev in self._iter_revisions(revision_ids):
1352
            if rev is None:
1353
                raise errors.NoSuchRevision(self, revid)
1354
            revs[revid] = rev
1355
        return [revs[revid] for revid in revision_ids]
1356
1357
    def _iter_revisions(self, revision_ids):
1358
        """Iterate over revision objects.
1359
1360
        :param revision_ids: An iterable of revisions to examine. None may be
1361
            passed to request all revisions known to the repository. Note that
1362
            not all repositories can find unreferenced revisions; for those
1363
            repositories only referenced ones will be returned.
1364
        :return: An iterator of (revid, revision) tuples. Absent revisions (
1365
            those asked for but not available) are returned as (revid, None).
1366
        """
1367
        if revision_ids is None:
1368
            revision_ids = self.all_revision_ids()
1369
        else:
1370
            for rev_id in revision_ids:
1371
                if not rev_id or not isinstance(rev_id, basestring):
1372
                    raise errors.InvalidRevisionId(revision_id=rev_id, branch=self)
1373
        keys = [(key,) for key in revision_ids]
1374
        stream = self.revisions.get_record_stream(keys, 'unordered', True)
1375
        for record in stream:
1376
            revid = record.key[0]
1377
            if record.storage_kind == 'absent':
1378
                yield (revid, None)
1379
            else:
1380
                text = record.get_bytes_as('fulltext')
1381
                rev = self._serializer.read_revision_from_string(text)
1382
                yield (revid, rev)
1383
1384
    @needs_write_lock
1385
    def add_signature_text(self, revision_id, signature):
1386
        """Store a signature text for a revision.
1387
1388
        :param revision_id: Revision id of the revision
1389
        :param signature: Signature text.
1390
        """
1391
        self.signatures.add_lines((revision_id,), (),
1392
            osutils.split_lines(signature))
1393
1394
    def find_text_key_references(self):
1395
        """Find the text key references within the repository.
1396
1397
        :return: A dictionary mapping text keys ((fileid, revision_id) tuples)
1398
            to whether they were referred to by the inventory of the
1399
            revision_id that they contain. The inventory texts from all present
1400
            revision ids are assessed to generate this report.
1401
        """
1402
        revision_keys = self.revisions.keys()
1403
        w = self.inventories
1404
        pb = ui.ui_factory.nested_progress_bar()
1405
        try:
1406
            return self._serializer._find_text_key_references(
1407
                w.iter_lines_added_or_present_in_keys(revision_keys, pb=pb))
1408
        finally:
1409
            pb.finished()
1410
1411
    def _inventory_xml_lines_for_keys(self, keys):
1412
        """Get a line iterator of the sort needed for findind references.
1413
1414
        Not relevant for non-xml inventory repositories.
1415
1416
        Ghosts in revision_keys are ignored.
1417
1418
        :param revision_keys: The revision keys for the inventories to inspect.
1419
        :return: An iterator over (inventory line, revid) for the fulltexts of
1420
            all of the xml inventories specified by revision_keys.
1421
        """
1422
        stream = self.inventories.get_record_stream(keys, 'unordered', True)
1423
        for record in stream:
1424
            if record.storage_kind != 'absent':
1425
                chunks = record.get_bytes_as('chunked')
1426
                revid = record.key[-1]
1427
                lines = osutils.chunks_to_lines(chunks)
1428
                for line in lines:
1429
                    yield line, revid
1430
1431
    def _find_file_ids_from_xml_inventory_lines(self, line_iterator,
1432
        revision_keys):
1433
        """Helper routine for fileids_altered_by_revision_ids.
1434
1435
        This performs the translation of xml lines to revision ids.
1436
1437
        :param line_iterator: An iterator of lines, origin_version_id
1438
        :param revision_keys: The revision ids to filter for. This should be a
1439
            set or other type which supports efficient __contains__ lookups, as
1440
            the revision key from each parsed line will be looked up in the
1441
            revision_keys filter.
1442
        :return: a dictionary mapping altered file-ids to an iterable of
5891.1.3 by Andrew Bennetts
Move docstring formatting fixes.
1443
            revision_ids. Each altered file-ids has the exact revision_ids that
1444
            altered it listed explicitly.
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
1445
        """
1446
        seen = set(self._serializer._find_text_key_references(
1447
                line_iterator).iterkeys())
1448
        parent_keys = self._find_parent_keys_of_revisions(revision_keys)
1449
        parent_seen = set(self._serializer._find_text_key_references(
1450
            self._inventory_xml_lines_for_keys(parent_keys)))
1451
        new_keys = seen - parent_seen
1452
        result = {}
1453
        setdefault = result.setdefault
1454
        for key in new_keys:
1455
            setdefault(key[0], set()).add(key[-1])
1456
        return result
1457
1458
    def _find_parent_keys_of_revisions(self, revision_keys):
1459
        """Similar to _find_parent_ids_of_revisions, but used with keys.
1460
1461
        :param revision_keys: An iterable of revision_keys.
1462
        :return: The parents of all revision_keys that are not already in
1463
            revision_keys
1464
        """
1465
        parent_map = self.revisions.get_parent_map(revision_keys)
1466
        parent_keys = set()
1467
        map(parent_keys.update, parent_map.itervalues())
1468
        parent_keys.difference_update(revision_keys)
1469
        parent_keys.discard(_mod_revision.NULL_REVISION)
1470
        return parent_keys
1471
1472
    def fileids_altered_by_revision_ids(self, revision_ids, _inv_weave=None):
1473
        """Find the file ids and versions affected by revisions.
1474
1475
        :param revisions: an iterable containing revision ids.
1476
        :param _inv_weave: The inventory weave from this repository or None.
1477
            If None, the inventory weave will be opened automatically.
1478
        :return: a dictionary mapping altered file-ids to an iterable of
5891.1.3 by Andrew Bennetts
Move docstring formatting fixes.
1479
            revision_ids. Each altered file-ids has the exact revision_ids that
1480
            altered it listed explicitly.
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
1481
        """
1482
        selected_keys = set((revid,) for revid in revision_ids)
1483
        w = _inv_weave or self.inventories
1484
        return self._find_file_ids_from_xml_inventory_lines(
1485
            w.iter_lines_added_or_present_in_keys(
1486
                selected_keys, pb=None),
1487
            selected_keys)
1488
1489
    def iter_files_bytes(self, desired_files):
1490
        """Iterate through file versions.
1491
1492
        Files will not necessarily be returned in the order they occur in
1493
        desired_files.  No specific order is guaranteed.
1494
1495
        Yields pairs of identifier, bytes_iterator.  identifier is an opaque
1496
        value supplied by the caller as part of desired_files.  It should
1497
        uniquely identify the file version in the caller's context.  (Examples:
1498
        an index number or a TreeTransform trans_id.)
1499
1500
        bytes_iterator is an iterable of bytestrings for the file.  The
1501
        kind of iterable and length of the bytestrings are unspecified, but for
1502
        this implementation, it is a list of bytes produced by
1503
        VersionedFile.get_record_stream().
1504
1505
        :param desired_files: a list of (file_id, revision_id, identifier)
1506
            triples
1507
        """
1508
        text_keys = {}
1509
        for file_id, revision_id, callable_data in desired_files:
1510
            text_keys[(file_id, revision_id)] = callable_data
1511
        for record in self.texts.get_record_stream(text_keys, 'unordered', True):
1512
            if record.storage_kind == 'absent':
1513
                raise errors.RevisionNotPresent(record.key, self)
1514
            yield text_keys[record.key], record.get_bytes_as('chunked')
1515
1516
    def _generate_text_key_index(self, text_key_references=None,
1517
        ancestors=None):
1518
        """Generate a new text key index for the repository.
1519
1520
        This is an expensive function that will take considerable time to run.
1521
1522
        :return: A dict mapping text keys ((file_id, revision_id) tuples) to a
1523
            list of parents, also text keys. When a given key has no parents,
1524
            the parents list will be [NULL_REVISION].
1525
        """
1526
        # All revisions, to find inventory parents.
1527
        if ancestors is None:
1528
            graph = self.get_graph()
1529
            ancestors = graph.get_parent_map(self.all_revision_ids())
1530
        if text_key_references is None:
1531
            text_key_references = self.find_text_key_references()
1532
        pb = ui.ui_factory.nested_progress_bar()
1533
        try:
1534
            return self._do_generate_text_key_index(ancestors,
1535
                text_key_references, pb)
1536
        finally:
1537
            pb.finished()
1538
1539
    def _do_generate_text_key_index(self, ancestors, text_key_references, pb):
1540
        """Helper for _generate_text_key_index to avoid deep nesting."""
1541
        revision_order = tsort.topo_sort(ancestors)
1542
        invalid_keys = set()
1543
        revision_keys = {}
1544
        for revision_id in revision_order:
1545
            revision_keys[revision_id] = set()
1546
        text_count = len(text_key_references)
1547
        # a cache of the text keys to allow reuse; costs a dict of all the
1548
        # keys, but saves a 2-tuple for every child of a given key.
1549
        text_key_cache = {}
1550
        for text_key, valid in text_key_references.iteritems():
1551
            if not valid:
1552
                invalid_keys.add(text_key)
1553
            else:
1554
                revision_keys[text_key[1]].add(text_key)
1555
            text_key_cache[text_key] = text_key
1556
        del text_key_references
1557
        text_index = {}
1558
        text_graph = graph.Graph(graph.DictParentsProvider(text_index))
1559
        NULL_REVISION = _mod_revision.NULL_REVISION
1560
        # Set a cache with a size of 10 - this suffices for bzr.dev but may be
1561
        # too small for large or very branchy trees. However, for 55K path
1562
        # trees, it would be easy to use too much memory trivially. Ideally we
1563
        # could gauge this by looking at available real memory etc, but this is
1564
        # always a tricky proposition.
1565
        inventory_cache = lru_cache.LRUCache(10)
1566
        batch_size = 10 # should be ~150MB on a 55K path tree
1567
        batch_count = len(revision_order) / batch_size + 1
1568
        processed_texts = 0
1569
        pb.update("Calculating text parents", processed_texts, text_count)
1570
        for offset in xrange(batch_count):
1571
            to_query = revision_order[offset * batch_size:(offset + 1) *
1572
                batch_size]
1573
            if not to_query:
1574
                break
1575
            for revision_id in to_query:
1576
                parent_ids = ancestors[revision_id]
1577
                for text_key in revision_keys[revision_id]:
1578
                    pb.update("Calculating text parents", processed_texts)
1579
                    processed_texts += 1
1580
                    candidate_parents = []
1581
                    for parent_id in parent_ids:
1582
                        parent_text_key = (text_key[0], parent_id)
1583
                        try:
1584
                            check_parent = parent_text_key not in \
1585
                                revision_keys[parent_id]
1586
                        except KeyError:
1587
                            # the parent parent_id is a ghost:
1588
                            check_parent = False
1589
                            # truncate the derived graph against this ghost.
1590
                            parent_text_key = None
1591
                        if check_parent:
1592
                            # look at the parent commit details inventories to
1593
                            # determine possible candidates in the per file graph.
1594
                            # TODO: cache here.
1595
                            try:
1596
                                inv = inventory_cache[parent_id]
1597
                            except KeyError:
1598
                                inv = self.revision_tree(parent_id).inventory
1599
                                inventory_cache[parent_id] = inv
1600
                            try:
1601
                                parent_entry = inv[text_key[0]]
1602
                            except (KeyError, errors.NoSuchId):
1603
                                parent_entry = None
1604
                            if parent_entry is not None:
1605
                                parent_text_key = (
1606
                                    text_key[0], parent_entry.revision)
1607
                            else:
1608
                                parent_text_key = None
1609
                        if parent_text_key is not None:
1610
                            candidate_parents.append(
1611
                                text_key_cache[parent_text_key])
1612
                    parent_heads = text_graph.heads(candidate_parents)
1613
                    new_parents = list(parent_heads)
1614
                    new_parents.sort(key=lambda x:candidate_parents.index(x))
1615
                    if new_parents == []:
1616
                        new_parents = [NULL_REVISION]
1617
                    text_index[text_key] = new_parents
1618
1619
        for text_key in invalid_keys:
1620
            text_index[text_key] = [NULL_REVISION]
1621
        return text_index
1622
1623
    def item_keys_introduced_by(self, revision_ids, _files_pb=None):
1624
        """Get an iterable listing the keys of all the data introduced by a set
1625
        of revision IDs.
1626
1627
        The keys will be ordered so that the corresponding items can be safely
1628
        fetched and inserted in that order.
1629
1630
        :returns: An iterable producing tuples of (knit-kind, file-id,
1631
            versions).  knit-kind is one of 'file', 'inventory', 'signatures',
1632
            'revisions'.  file-id is None unless knit-kind is 'file'.
1633
        """
1634
        for result in self._find_file_keys_to_fetch(revision_ids, _files_pb):
1635
            yield result
1636
        del _files_pb
1637
        for result in self._find_non_file_keys_to_fetch(revision_ids):
1638
            yield result
1639
1640
    def _find_file_keys_to_fetch(self, revision_ids, pb):
1641
        # XXX: it's a bit weird to control the inventory weave caching in this
1642
        # generator.  Ideally the caching would be done in fetch.py I think.  Or
1643
        # maybe this generator should explicitly have the contract that it
1644
        # should not be iterated until the previously yielded item has been
1645
        # processed?
1646
        inv_w = self.inventories
1647
1648
        # file ids that changed
1649
        file_ids = self.fileids_altered_by_revision_ids(revision_ids, inv_w)
1650
        count = 0
1651
        num_file_ids = len(file_ids)
1652
        for file_id, altered_versions in file_ids.iteritems():
1653
            if pb is not None:
1654
                pb.update("Fetch texts", count, num_file_ids)
1655
            count += 1
1656
            yield ("file", file_id, altered_versions)
1657
1658
    def _find_non_file_keys_to_fetch(self, revision_ids):
1659
        # inventory
1660
        yield ("inventory", None, revision_ids)
1661
1662
        # signatures
1663
        # XXX: Note ATM no callers actually pay attention to this return
1664
        #      instead they just use the list of revision ids and ignore
1665
        #      missing sigs. Consider removing this work entirely
1666
        revisions_with_signatures = set(self.signatures.get_parent_map(
1667
            [(r,) for r in revision_ids]))
1668
        revisions_with_signatures = set(
1669
            [r for (r,) in revisions_with_signatures])
1670
        revisions_with_signatures.intersection_update(revision_ids)
1671
        yield ("signatures", None, revisions_with_signatures)
1672
1673
        # revisions
1674
        yield ("revisions", None, revision_ids)
1675
1676
    @needs_read_lock
1677
    def get_inventory(self, revision_id):
1678
        """Get Inventory object by revision id."""
1679
        return self.iter_inventories([revision_id]).next()
1680
1681
    def iter_inventories(self, revision_ids, ordering=None):
1682
        """Get many inventories by revision_ids.
1683
1684
        This will buffer some or all of the texts used in constructing the
1685
        inventories in memory, but will only parse a single inventory at a
1686
        time.
1687
1688
        :param revision_ids: The expected revision ids of the inventories.
1689
        :param ordering: optional ordering, e.g. 'topological'.  If not
1690
            specified, the order of revision_ids will be preserved (by
1691
            buffering if necessary).
1692
        :return: An iterator of inventories.
1693
        """
1694
        if ((None in revision_ids)
1695
            or (_mod_revision.NULL_REVISION in revision_ids)):
1696
            raise ValueError('cannot get null revision inventory')
1697
        return self._iter_inventories(revision_ids, ordering)
1698
1699
    def _iter_inventories(self, revision_ids, ordering):
1700
        """single-document based inventory iteration."""
1701
        inv_xmls = self._iter_inventory_xmls(revision_ids, ordering)
1702
        for text, revision_id in inv_xmls:
1703
            yield self._deserialise_inventory(revision_id, text)
1704
1705
    def _iter_inventory_xmls(self, revision_ids, ordering):
1706
        if ordering is None:
1707
            order_as_requested = True
1708
            ordering = 'unordered'
1709
        else:
1710
            order_as_requested = False
1711
        keys = [(revision_id,) for revision_id in revision_ids]
1712
        if not keys:
1713
            return
1714
        if order_as_requested:
1715
            key_iter = iter(keys)
1716
            next_key = key_iter.next()
1717
        stream = self.inventories.get_record_stream(keys, ordering, True)
1718
        text_chunks = {}
1719
        for record in stream:
1720
            if record.storage_kind != 'absent':
1721
                chunks = record.get_bytes_as('chunked')
1722
                if order_as_requested:
1723
                    text_chunks[record.key] = chunks
1724
                else:
1725
                    yield ''.join(chunks), record.key[-1]
1726
            else:
1727
                raise errors.NoSuchRevision(self, record.key)
1728
            if order_as_requested:
1729
                # Yield as many results as we can while preserving order.
1730
                while next_key in text_chunks:
1731
                    chunks = text_chunks.pop(next_key)
1732
                    yield ''.join(chunks), next_key[-1]
1733
                    try:
1734
                        next_key = key_iter.next()
1735
                    except StopIteration:
1736
                        # We still want to fully consume the get_record_stream,
1737
                        # just in case it is not actually finished at this point
1738
                        next_key = None
1739
                        break
1740
1741
    def _deserialise_inventory(self, revision_id, xml):
1742
        """Transform the xml into an inventory object.
1743
1744
        :param revision_id: The expected revision id of the inventory.
1745
        :param xml: A serialised inventory.
1746
        """
1747
        result = self._serializer.read_inventory_from_string(xml, revision_id,
1748
                    entry_cache=self._inventory_entry_cache,
1749
                    return_from_cache=self._safe_to_return_from_cache)
1750
        if result.revision_id != revision_id:
1751
            raise AssertionError('revision id mismatch %s != %s' % (
1752
                result.revision_id, revision_id))
1753
        return result
1754
1755
    def get_serializer_format(self):
1756
        return self._serializer.format_num
1757
1758
    @needs_read_lock
1759
    def _get_inventory_xml(self, revision_id):
1760
        """Get serialized inventory as a string."""
1761
        texts = self._iter_inventory_xmls([revision_id], 'unordered')
1762
        try:
1763
            text, revision_id = texts.next()
1764
        except StopIteration:
1765
            raise errors.HistoryMissing(self, 'inventory', revision_id)
1766
        return text
1767
1768
    @needs_read_lock
1769
    def revision_tree(self, revision_id):
1770
        """Return Tree for a revision on this branch.
1771
1772
        `revision_id` may be NULL_REVISION for the empty tree revision.
1773
        """
1774
        revision_id = _mod_revision.ensure_null(revision_id)
1775
        # TODO: refactor this to use an existing revision object
1776
        # so we don't need to read it in twice.
1777
        if revision_id == _mod_revision.NULL_REVISION:
1778
            return InventoryRevisionTree(self,
1779
                Inventory(root_id=None), _mod_revision.NULL_REVISION)
1780
        else:
1781
            inv = self.get_inventory(revision_id)
1782
            return InventoryRevisionTree(self, inv, revision_id)
1783
1784
    def revision_trees(self, revision_ids):
1785
        """Return Trees for revisions in this repository.
1786
1787
        :param revision_ids: a sequence of revision-ids;
1788
          a revision-id may not be None or 'null:'
1789
        """
1790
        inventories = self.iter_inventories(revision_ids)
1791
        for inv in inventories:
1792
            yield InventoryRevisionTree(self, inv, inv.revision_id)
1793
1794
    def _filtered_revision_trees(self, revision_ids, file_ids):
1795
        """Return Tree for a revision on this branch with only some files.
1796
1797
        :param revision_ids: a sequence of revision-ids;
1798
          a revision-id may not be None or 'null:'
1799
        :param file_ids: if not None, the result is filtered
1800
          so that only those file-ids, their parents and their
1801
          children are included.
1802
        """
1803
        inventories = self.iter_inventories(revision_ids)
1804
        for inv in inventories:
1805
            # Should we introduce a FilteredRevisionTree class rather
1806
            # than pre-filter the inventory here?
1807
            filtered_inv = inv.filter(file_ids)
1808
            yield InventoryRevisionTree(self, filtered_inv, filtered_inv.revision_id)
1809
1810
    def get_parent_map(self, revision_ids):
1811
        """See graph.StackedParentsProvider.get_parent_map"""
1812
        # revisions index works in keys; this just works in revisions
1813
        # therefore wrap and unwrap
1814
        query_keys = []
1815
        result = {}
1816
        for revision_id in revision_ids:
1817
            if revision_id == _mod_revision.NULL_REVISION:
1818
                result[revision_id] = ()
1819
            elif revision_id is None:
1820
                raise ValueError('get_parent_map(None) is not valid')
1821
            else:
1822
                query_keys.append((revision_id ,))
1823
        for ((revision_id,), parent_keys) in \
1824
                self.revisions.get_parent_map(query_keys).iteritems():
1825
            if parent_keys:
1826
                result[revision_id] = tuple([parent_revid
1827
                    for (parent_revid,) in parent_keys])
1828
            else:
1829
                result[revision_id] = (_mod_revision.NULL_REVISION,)
1830
        return result
1831
1832
    @needs_read_lock
1833
    def get_known_graph_ancestry(self, revision_ids):
1834
        """Return the known graph for a set of revision ids and their ancestors.
1835
        """
1836
        st = static_tuple.StaticTuple
1837
        revision_keys = [st(r_id).intern() for r_id in revision_ids]
1838
        known_graph = self.revisions.get_known_graph_ancestry(revision_keys)
1839
        return graph.GraphThunkIdsToKeys(known_graph)
1840
5815.5.5 by Jelmer Vernooij
Add more tests.
1841
    @needs_read_lock
5815.5.8 by Jelmer Vernooij
Use traditional (fileid, revision) entries in file graph.
1842
    def get_file_graph(self):
1843
        """Return the graph walker for text revisions."""
1844
        return graph.Graph(self.texts)
5815.5.5 by Jelmer Vernooij
Add more tests.
1845
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
1846
    def _get_versioned_file_checker(self, text_key_references=None,
1847
        ancestors=None):
1848
        """Return an object suitable for checking versioned files.
1849
        
1850
        :param text_key_references: if non-None, an already built
1851
            dictionary mapping text keys ((fileid, revision_id) tuples)
1852
            to whether they were referred to by the inventory of the
1853
            revision_id that they contain. If None, this will be
1854
            calculated.
1855
        :param ancestors: Optional result from
1856
            self.get_graph().get_parent_map(self.all_revision_ids()) if already
1857
            available.
1858
        """
1859
        return _VersionedFileChecker(self,
1860
            text_key_references=text_key_references, ancestors=ancestors)
1861
1862
    @needs_read_lock
1863
    def has_signature_for_revision_id(self, revision_id):
1864
        """Query for a revision signature for revision_id in the repository."""
1865
        if not self.has_revision(revision_id):
1866
            raise errors.NoSuchRevision(self, revision_id)
1867
        sig_present = (1 == len(
1868
            self.signatures.get_parent_map([(revision_id,)])))
1869
        return sig_present
1870
1871
    @needs_read_lock
1872
    def get_signature_text(self, revision_id):
1873
        """Return the text for a signature."""
1874
        stream = self.signatures.get_record_stream([(revision_id,)],
1875
            'unordered', True)
1876
        record = stream.next()
1877
        if record.storage_kind == 'absent':
1878
            raise errors.NoSuchRevision(self, revision_id)
1879
        return record.get_bytes_as('fulltext')
1880
5850.1.6 by Jelmer Vernooij
Reintroduce double indirection, raise NotImplementedError from _check rather than
1881
    @needs_read_lock
1882
    def _check(self, revision_ids, callback_refs, check_repo):
5850.1.3 by Jelmer Vernooij
Add VersionedFileCheck.
1883
        result = check.VersionedFileCheck(self, check_repo=check_repo)
1884
        result.check(callback_refs)
1885
        return result
1886
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
1887
    def _find_inconsistent_revision_parents(self, revisions_iterator=None):
1888
        """Find revisions with different parent lists in the revision object
1889
        and in the index graph.
1890
1891
        :param revisions_iterator: None, or an iterator of (revid,
1892
            Revision-or-None). This iterator controls the revisions checked.
1893
        :returns: an iterator yielding tuples of (revison-id, parents-in-index,
1894
            parents-in-revision).
1895
        """
1896
        if not self.is_locked():
1897
            raise AssertionError()
1898
        vf = self.revisions
1899
        if revisions_iterator is None:
1900
            revisions_iterator = self._iter_revisions(None)
1901
        for revid, revision in revisions_iterator:
1902
            if revision is None:
1903
                pass
1904
            parent_map = vf.get_parent_map([(revid,)])
1905
            parents_according_to_index = tuple(parent[-1] for parent in
1906
                parent_map[(revid,)])
1907
            parents_according_to_revision = tuple(revision.parent_ids)
1908
            if parents_according_to_index != parents_according_to_revision:
1909
                yield (revid, parents_according_to_index,
1910
                    parents_according_to_revision)
1911
1912
    def _check_for_inconsistent_revision_parents(self):
1913
        inconsistencies = list(self._find_inconsistent_revision_parents())
1914
        if inconsistencies:
1915
            raise errors.BzrCheckError(
1916
                "Revision knit has inconsistent parents.")
1917
1918
    def _get_sink(self):
1919
        """Return a sink for streaming into this repository."""
1920
        return StreamSink(self)
1921
1922
    def _get_source(self, to_format):
1923
        """Return a source for streaming from this repository."""
1924
        return StreamSource(self, to_format)
1925
1926
1927
class MetaDirVersionedFileRepository(MetaDirRepository,
1928
                                     VersionedFileRepository):
1929
    """Repositories in a meta-dir, that work via versioned file objects."""
1930
1931
    def __init__(self, _format, a_bzrdir, control_files):
1932
        super(MetaDirVersionedFileRepository, self).__init__(_format, a_bzrdir,
1933
            control_files)
1934
1935
5815.4.5 by Jelmer Vernooij
Use MetaDirVersionedFileRepositoryFormat (a Soyuz worthy name).
1936
class MetaDirVersionedFileRepositoryFormat(MetaDirRepositoryFormat,
1937
        VersionedFileRepositoryFormat):
1938
    """Base class for repository formats using versioned files in metadirs."""
1939
1940
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
1941
class StreamSink(object):
1942
    """An object that can insert a stream into a repository.
1943
1944
    This interface handles the complexity of reserialising inventories and
1945
    revisions from different formats, and allows unidirectional insertion into
1946
    stacked repositories without looking for the missing basis parents
1947
    beforehand.
1948
    """
1949
1950
    def __init__(self, target_repo):
1951
        self.target_repo = target_repo
1952
1953
    def insert_stream(self, stream, src_format, resume_tokens):
1954
        """Insert a stream's content into the target repository.
1955
1956
        :param src_format: a bzr repository format.
1957
1958
        :return: a list of resume tokens and an  iterable of keys additional
1959
            items required before the insertion can be completed.
1960
        """
1961
        self.target_repo.lock_write()
1962
        try:
1963
            if resume_tokens:
1964
                self.target_repo.resume_write_group(resume_tokens)
1965
                is_resume = True
1966
            else:
1967
                self.target_repo.start_write_group()
1968
                is_resume = False
1969
            try:
1970
                # locked_insert_stream performs a commit|suspend.
1971
                missing_keys = self.insert_stream_without_locking(stream,
1972
                                    src_format, is_resume)
1973
                if missing_keys:
1974
                    # suspend the write group and tell the caller what we is
1975
                    # missing. We know we can suspend or else we would not have
1976
                    # entered this code path. (All repositories that can handle
1977
                    # missing keys can handle suspending a write group).
1978
                    write_group_tokens = self.target_repo.suspend_write_group()
1979
                    return write_group_tokens, missing_keys
1980
                hint = self.target_repo.commit_write_group()
1981
                to_serializer = self.target_repo._format._serializer
1982
                src_serializer = src_format._serializer
1983
                if (to_serializer != src_serializer and
1984
                    self.target_repo._format.pack_compresses):
1985
                    self.target_repo.pack(hint=hint)
1986
                return [], set()
1987
            except:
1988
                self.target_repo.abort_write_group(suppress_errors=True)
1989
                raise
1990
        finally:
1991
            self.target_repo.unlock()
1992
1993
    def insert_stream_without_locking(self, stream, src_format,
1994
                                      is_resume=False):
1995
        """Insert a stream's content into the target repository.
1996
1997
        This assumes that you already have a locked repository and an active
1998
        write group.
1999
2000
        :param src_format: a bzr repository format.
2001
        :param is_resume: Passed down to get_missing_parent_inventories to
2002
            indicate if we should be checking for missing texts at the same
2003
            time.
2004
2005
        :return: A set of keys that are missing.
2006
        """
2007
        if not self.target_repo.is_write_locked():
2008
            raise errors.ObjectNotLocked(self)
2009
        if not self.target_repo.is_in_write_group():
2010
            raise errors.BzrError('you must already be in a write group')
2011
        to_serializer = self.target_repo._format._serializer
2012
        src_serializer = src_format._serializer
2013
        new_pack = None
2014
        if to_serializer == src_serializer:
2015
            # If serializers match and the target is a pack repository, set the
2016
            # write cache size on the new pack.  This avoids poor performance
2017
            # on transports where append is unbuffered (such as
2018
            # RemoteTransport).  This is safe to do because nothing should read
2019
            # back from the target repository while a stream with matching
2020
            # serialization is being inserted.
2021
            # The exception is that a delta record from the source that should
2022
            # be a fulltext may need to be expanded by the target (see
2023
            # test_fetch_revisions_with_deltas_into_pack); but we take care to
2024
            # explicitly flush any buffered writes first in that rare case.
2025
            try:
2026
                new_pack = self.target_repo._pack_collection._new_pack
2027
            except AttributeError:
2028
                # Not a pack repository
2029
                pass
2030
            else:
2031
                new_pack.set_write_cache_size(1024*1024)
2032
        for substream_type, substream in stream:
2033
            if 'stream' in debug.debug_flags:
2034
                mutter('inserting substream: %s', substream_type)
2035
            if substream_type == 'texts':
2036
                self.target_repo.texts.insert_record_stream(substream)
2037
            elif substream_type == 'inventories':
2038
                if src_serializer == to_serializer:
2039
                    self.target_repo.inventories.insert_record_stream(
2040
                        substream)
2041
                else:
2042
                    self._extract_and_insert_inventories(
2043
                        substream, src_serializer)
2044
            elif substream_type == 'inventory-deltas':
2045
                self._extract_and_insert_inventory_deltas(
2046
                    substream, src_serializer)
2047
            elif substream_type == 'chk_bytes':
2048
                # XXX: This doesn't support conversions, as it assumes the
2049
                #      conversion was done in the fetch code.
2050
                self.target_repo.chk_bytes.insert_record_stream(substream)
2051
            elif substream_type == 'revisions':
2052
                # This may fallback to extract-and-insert more often than
2053
                # required if the serializers are different only in terms of
2054
                # the inventory.
2055
                if src_serializer == to_serializer:
2056
                    self.target_repo.revisions.insert_record_stream(substream)
2057
                else:
2058
                    self._extract_and_insert_revisions(substream,
2059
                        src_serializer)
2060
            elif substream_type == 'signatures':
2061
                self.target_repo.signatures.insert_record_stream(substream)
2062
            else:
2063
                raise AssertionError('kaboom! %s' % (substream_type,))
2064
        # Done inserting data, and the missing_keys calculations will try to
2065
        # read back from the inserted data, so flush the writes to the new pack
2066
        # (if this is pack format).
2067
        if new_pack is not None:
2068
            new_pack._write_data('', flush=True)
2069
        # Find all the new revisions (including ones from resume_tokens)
2070
        missing_keys = self.target_repo.get_missing_parent_inventories(
2071
            check_for_missing_texts=is_resume)
2072
        try:
2073
            for prefix, versioned_file in (
2074
                ('texts', self.target_repo.texts),
2075
                ('inventories', self.target_repo.inventories),
2076
                ('revisions', self.target_repo.revisions),
2077
                ('signatures', self.target_repo.signatures),
2078
                ('chk_bytes', self.target_repo.chk_bytes),
2079
                ):
2080
                if versioned_file is None:
2081
                    continue
2082
                # TODO: key is often going to be a StaticTuple object
2083
                #       I don't believe we can define a method by which
2084
                #       (prefix,) + StaticTuple will work, though we could
2085
                #       define a StaticTuple.sq_concat that would allow you to
2086
                #       pass in either a tuple or a StaticTuple as the second
2087
                #       object, so instead we could have:
2088
                #       StaticTuple(prefix) + key here...
2089
                missing_keys.update((prefix,) + key for key in
2090
                    versioned_file.get_missing_compression_parent_keys())
2091
        except NotImplementedError:
2092
            # cannot even attempt suspending, and missing would have failed
2093
            # during stream insertion.
2094
            missing_keys = set()
2095
        return missing_keys
2096
2097
    def _extract_and_insert_inventory_deltas(self, substream, serializer):
2098
        target_rich_root = self.target_repo._format.rich_root_data
2099
        target_tree_refs = self.target_repo._format.supports_tree_reference
2100
        for record in substream:
2101
            # Insert the delta directly
2102
            inventory_delta_bytes = record.get_bytes_as('fulltext')
2103
            deserialiser = inventory_delta.InventoryDeltaDeserializer()
2104
            try:
2105
                parse_result = deserialiser.parse_text_bytes(
2106
                    inventory_delta_bytes)
2107
            except inventory_delta.IncompatibleInventoryDelta, err:
2108
                mutter("Incompatible delta: %s", err.msg)
2109
                raise errors.IncompatibleRevision(self.target_repo._format)
2110
            basis_id, new_id, rich_root, tree_refs, inv_delta = parse_result
2111
            revision_id = new_id
2112
            parents = [key[0] for key in record.parents]
2113
            self.target_repo.add_inventory_by_delta(
2114
                basis_id, inv_delta, revision_id, parents)
2115
2116
    def _extract_and_insert_inventories(self, substream, serializer,
2117
            parse_delta=None):
2118
        """Generate a new inventory versionedfile in target, converting data.
2119
2120
        The inventory is retrieved from the source, (deserializing it), and
2121
        stored in the target (reserializing it in a different format).
2122
        """
2123
        target_rich_root = self.target_repo._format.rich_root_data
2124
        target_tree_refs = self.target_repo._format.supports_tree_reference
2125
        for record in substream:
2126
            # It's not a delta, so it must be a fulltext in the source
2127
            # serializer's format.
2128
            bytes = record.get_bytes_as('fulltext')
2129
            revision_id = record.key[0]
2130
            inv = serializer.read_inventory_from_string(bytes, revision_id)
2131
            parents = [key[0] for key in record.parents]
2132
            self.target_repo.add_inventory(revision_id, inv, parents)
2133
            # No need to keep holding this full inv in memory when the rest of
2134
            # the substream is likely to be all deltas.
2135
            del inv
2136
2137
    def _extract_and_insert_revisions(self, substream, serializer):
2138
        for record in substream:
2139
            bytes = record.get_bytes_as('fulltext')
2140
            revision_id = record.key[0]
2141
            rev = serializer.read_revision_from_string(bytes)
2142
            if rev.revision_id != revision_id:
2143
                raise AssertionError('wtf: %s != %s' % (rev, revision_id))
2144
            self.target_repo.add_revision(revision_id, rev)
2145
2146
    def finished(self):
2147
        if self.target_repo._format._fetch_reconcile:
2148
            self.target_repo.reconcile()
2149
2150
2151
class StreamSource(object):
2152
    """A source of a stream for fetching between repositories."""
2153
2154
    def __init__(self, from_repository, to_format):
2155
        """Create a StreamSource streaming from from_repository."""
2156
        self.from_repository = from_repository
2157
        self.to_format = to_format
2158
        self._record_counter = RecordCounter()
2159
2160
    def delta_on_metadata(self):
2161
        """Return True if delta's are permitted on metadata streams.
2162
2163
        That is on revisions and signatures.
2164
        """
2165
        src_serializer = self.from_repository._format._serializer
2166
        target_serializer = self.to_format._serializer
2167
        return (self.to_format._fetch_uses_deltas and
2168
            src_serializer == target_serializer)
2169
2170
    def _fetch_revision_texts(self, revs):
2171
        # fetch signatures first and then the revision texts
2172
        # may need to be a InterRevisionStore call here.
2173
        from_sf = self.from_repository.signatures
2174
        # A missing signature is just skipped.
2175
        keys = [(rev_id,) for rev_id in revs]
2176
        signatures = versionedfile.filter_absent(from_sf.get_record_stream(
2177
            keys,
2178
            self.to_format._fetch_order,
2179
            not self.to_format._fetch_uses_deltas))
2180
        # If a revision has a delta, this is actually expanded inside the
2181
        # insert_record_stream code now, which is an alternate fix for
2182
        # bug #261339
2183
        from_rf = self.from_repository.revisions
2184
        revisions = from_rf.get_record_stream(
2185
            keys,
2186
            self.to_format._fetch_order,
2187
            not self.delta_on_metadata())
2188
        return [('signatures', signatures), ('revisions', revisions)]
2189
2190
    def _generate_root_texts(self, revs):
2191
        """This will be called by get_stream between fetching weave texts and
2192
        fetching the inventory weave.
2193
        """
2194
        if self._rich_root_upgrade():
2195
            return _mod_fetch.Inter1and2Helper(
2196
                self.from_repository).generate_root_texts(revs)
2197
        else:
2198
            return []
2199
2200
    def get_stream(self, search):
2201
        phase = 'file'
2202
        revs = search.get_keys()
2203
        graph = self.from_repository.get_graph()
2204
        revs = tsort.topo_sort(graph.get_parent_map(revs))
2205
        data_to_fetch = self.from_repository.item_keys_introduced_by(revs)
2206
        text_keys = []
2207
        for knit_kind, file_id, revisions in data_to_fetch:
2208
            if knit_kind != phase:
2209
                phase = knit_kind
2210
                # Make a new progress bar for this phase
2211
            if knit_kind == "file":
2212
                # Accumulate file texts
2213
                text_keys.extend([(file_id, revision) for revision in
2214
                    revisions])
2215
            elif knit_kind == "inventory":
2216
                # Now copy the file texts.
2217
                from_texts = self.from_repository.texts
2218
                yield ('texts', from_texts.get_record_stream(
2219
                    text_keys, self.to_format._fetch_order,
2220
                    not self.to_format._fetch_uses_deltas))
2221
                # Cause an error if a text occurs after we have done the
2222
                # copy.
2223
                text_keys = None
2224
                # Before we process the inventory we generate the root
2225
                # texts (if necessary) so that the inventories references
2226
                # will be valid.
2227
                for _ in self._generate_root_texts(revs):
2228
                    yield _
2229
                # we fetch only the referenced inventories because we do not
2230
                # know for unselected inventories whether all their required
2231
                # texts are present in the other repository - it could be
2232
                # corrupt.
2233
                for info in self._get_inventory_stream(revs):
2234
                    yield info
2235
            elif knit_kind == "signatures":
2236
                # Nothing to do here; this will be taken care of when
2237
                # _fetch_revision_texts happens.
2238
                pass
2239
            elif knit_kind == "revisions":
2240
                for record in self._fetch_revision_texts(revs):
2241
                    yield record
2242
            else:
2243
                raise AssertionError("Unknown knit kind %r" % knit_kind)
2244
2245
    def get_stream_for_missing_keys(self, missing_keys):
2246
        # missing keys can only occur when we are byte copying and not
2247
        # translating (because translation means we don't send
2248
        # unreconstructable deltas ever).
2249
        keys = {}
2250
        keys['texts'] = set()
2251
        keys['revisions'] = set()
2252
        keys['inventories'] = set()
2253
        keys['chk_bytes'] = set()
2254
        keys['signatures'] = set()
2255
        for key in missing_keys:
2256
            keys[key[0]].add(key[1:])
2257
        if len(keys['revisions']):
2258
            # If we allowed copying revisions at this point, we could end up
2259
            # copying a revision without copying its required texts: a
2260
            # violation of the requirements for repository integrity.
2261
            raise AssertionError(
2262
                'cannot copy revisions to fill in missing deltas %s' % (
2263
                    keys['revisions'],))
2264
        for substream_kind, keys in keys.iteritems():
2265
            vf = getattr(self.from_repository, substream_kind)
2266
            if vf is None and keys:
2267
                    raise AssertionError(
2268
                        "cannot fill in keys for a versioned file we don't"
2269
                        " have: %s needs %s" % (substream_kind, keys))
2270
            if not keys:
2271
                # No need to stream something we don't have
2272
                continue
2273
            if substream_kind == 'inventories':
2274
                # Some missing keys are genuinely ghosts, filter those out.
2275
                present = self.from_repository.inventories.get_parent_map(keys)
2276
                revs = [key[0] for key in present]
2277
                # Get the inventory stream more-or-less as we do for the
2278
                # original stream; there's no reason to assume that records
2279
                # direct from the source will be suitable for the sink.  (Think
2280
                # e.g. 2a -> 1.9-rich-root).
2281
                for info in self._get_inventory_stream(revs, missing=True):
2282
                    yield info
2283
                continue
2284
2285
            # Ask for full texts always so that we don't need more round trips
2286
            # after this stream.
2287
            # Some of the missing keys are genuinely ghosts, so filter absent
2288
            # records. The Sink is responsible for doing another check to
2289
            # ensure that ghosts don't introduce missing data for future
2290
            # fetches.
2291
            stream = versionedfile.filter_absent(vf.get_record_stream(keys,
2292
                self.to_format._fetch_order, True))
2293
            yield substream_kind, stream
2294
2295
    def inventory_fetch_order(self):
2296
        if self._rich_root_upgrade():
2297
            return 'topological'
2298
        else:
2299
            return self.to_format._fetch_order
2300
2301
    def _rich_root_upgrade(self):
2302
        return (not self.from_repository._format.rich_root_data and
2303
            self.to_format.rich_root_data)
2304
2305
    def _get_inventory_stream(self, revision_ids, missing=False):
2306
        from_format = self.from_repository._format
2307
        if (from_format.supports_chks and self.to_format.supports_chks and
2308
            from_format.network_name() == self.to_format.network_name()):
2309
            raise AssertionError(
2310
                "this case should be handled by GroupCHKStreamSource")
2311
        elif 'forceinvdeltas' in debug.debug_flags:
2312
            return self._get_convertable_inventory_stream(revision_ids,
2313
                    delta_versus_null=missing)
2314
        elif from_format.network_name() == self.to_format.network_name():
2315
            # Same format.
2316
            return self._get_simple_inventory_stream(revision_ids,
2317
                    missing=missing)
2318
        elif (not from_format.supports_chks and not self.to_format.supports_chks
2319
                and from_format._serializer == self.to_format._serializer):
2320
            # Essentially the same format.
2321
            return self._get_simple_inventory_stream(revision_ids,
2322
                    missing=missing)
2323
        else:
2324
            # Any time we switch serializations, we want to use an
2325
            # inventory-delta based approach.
2326
            return self._get_convertable_inventory_stream(revision_ids,
2327
                    delta_versus_null=missing)
2328
2329
    def _get_simple_inventory_stream(self, revision_ids, missing=False):
2330
        # NB: This currently reopens the inventory weave in source;
2331
        # using a single stream interface instead would avoid this.
2332
        from_weave = self.from_repository.inventories
2333
        if missing:
2334
            delta_closure = True
2335
        else:
2336
            delta_closure = not self.delta_on_metadata()
2337
        yield ('inventories', from_weave.get_record_stream(
2338
            [(rev_id,) for rev_id in revision_ids],
2339
            self.inventory_fetch_order(), delta_closure))
2340
2341
    def _get_convertable_inventory_stream(self, revision_ids,
2342
                                          delta_versus_null=False):
2343
        # The two formats are sufficiently different that there is no fast
2344
        # path, so we need to send just inventorydeltas, which any
2345
        # sufficiently modern client can insert into any repository.
2346
        # The StreamSink code expects to be able to
2347
        # convert on the target, so we need to put bytes-on-the-wire that can
2348
        # be converted.  That means inventory deltas (if the remote is <1.19,
2349
        # RemoteStreamSink will fallback to VFS to insert the deltas).
2350
        yield ('inventory-deltas',
2351
           self._stream_invs_as_deltas(revision_ids,
2352
                                       delta_versus_null=delta_versus_null))
2353
2354
    def _stream_invs_as_deltas(self, revision_ids, delta_versus_null=False):
2355
        """Return a stream of inventory-deltas for the given rev ids.
2356
2357
        :param revision_ids: The list of inventories to transmit
2358
        :param delta_versus_null: Don't try to find a minimal delta for this
2359
            entry, instead compute the delta versus the NULL_REVISION. This
2360
            effectively streams a complete inventory. Used for stuff like
2361
            filling in missing parents, etc.
2362
        """
2363
        from_repo = self.from_repository
2364
        revision_keys = [(rev_id,) for rev_id in revision_ids]
2365
        parent_map = from_repo.inventories.get_parent_map(revision_keys)
2366
        # XXX: possibly repos could implement a more efficient iter_inv_deltas
2367
        # method...
2368
        inventories = self.from_repository.iter_inventories(
2369
            revision_ids, 'topological')
2370
        format = from_repo._format
2371
        invs_sent_so_far = set([_mod_revision.NULL_REVISION])
2372
        inventory_cache = lru_cache.LRUCache(50)
2373
        null_inventory = from_repo.revision_tree(
2374
            _mod_revision.NULL_REVISION).inventory
2375
        # XXX: ideally the rich-root/tree-refs flags would be per-revision, not
2376
        # per-repo (e.g.  streaming a non-rich-root revision out of a rich-root
2377
        # repo back into a non-rich-root repo ought to be allowed)
2378
        serializer = inventory_delta.InventoryDeltaSerializer(
2379
            versioned_root=format.rich_root_data,
2380
            tree_references=format.supports_tree_reference)
2381
        for inv in inventories:
2382
            key = (inv.revision_id,)
2383
            parent_keys = parent_map.get(key, ())
2384
            delta = None
2385
            if not delta_versus_null and parent_keys:
2386
                # The caller did not ask for complete inventories and we have
2387
                # some parents that we can delta against.  Make a delta against
2388
                # each parent so that we can find the smallest.
2389
                parent_ids = [parent_key[0] for parent_key in parent_keys]
2390
                for parent_id in parent_ids:
2391
                    if parent_id not in invs_sent_so_far:
2392
                        # We don't know that the remote side has this basis, so
2393
                        # we can't use it.
2394
                        continue
2395
                    if parent_id == _mod_revision.NULL_REVISION:
2396
                        parent_inv = null_inventory
2397
                    else:
2398
                        parent_inv = inventory_cache.get(parent_id, None)
2399
                        if parent_inv is None:
2400
                            parent_inv = from_repo.get_inventory(parent_id)
2401
                    candidate_delta = inv._make_delta(parent_inv)
2402
                    if (delta is None or
2403
                        len(delta) > len(candidate_delta)):
2404
                        delta = candidate_delta
2405
                        basis_id = parent_id
2406
            if delta is None:
2407
                # Either none of the parents ended up being suitable, or we
2408
                # were asked to delta against NULL
2409
                basis_id = _mod_revision.NULL_REVISION
2410
                delta = inv._make_delta(null_inventory)
2411
            invs_sent_so_far.add(inv.revision_id)
2412
            inventory_cache[inv.revision_id] = inv
2413
            delta_serialized = ''.join(
2414
                serializer.delta_to_lines(basis_id, key[-1], delta))
2415
            yield versionedfile.FulltextContentFactory(
2416
                key, parent_keys, None, delta_serialized)
2417
2418
2419
class _VersionedFileChecker(object):
2420
2421
    def __init__(self, repository, text_key_references=None, ancestors=None):
2422
        self.repository = repository
2423
        self.text_index = self.repository._generate_text_key_index(
2424
            text_key_references=text_key_references, ancestors=ancestors)
2425
2426
    def calculate_file_version_parents(self, text_key):
2427
        """Calculate the correct parents for a file version according to
2428
        the inventories.
2429
        """
2430
        parent_keys = self.text_index[text_key]
2431
        if parent_keys == [_mod_revision.NULL_REVISION]:
2432
            return ()
2433
        return tuple(parent_keys)
2434
2435
    def check_file_version_parents(self, texts, progress_bar=None):
2436
        """Check the parents stored in a versioned file are correct.
2437
2438
        It also detects file versions that are not referenced by their
2439
        corresponding revision's inventory.
2440
2441
        :returns: A tuple of (wrong_parents, dangling_file_versions).
2442
            wrong_parents is a dict mapping {revision_id: (stored_parents,
2443
            correct_parents)} for each revision_id where the stored parents
2444
            are not correct.  dangling_file_versions is a set of (file_id,
2445
            revision_id) tuples for versions that are present in this versioned
2446
            file, but not used by the corresponding inventory.
2447
        """
2448
        local_progress = None
2449
        if progress_bar is None:
2450
            local_progress = ui.ui_factory.nested_progress_bar()
2451
            progress_bar = local_progress
2452
        try:
2453
            return self._check_file_version_parents(texts, progress_bar)
2454
        finally:
2455
            if local_progress:
2456
                local_progress.finished()
2457
2458
    def _check_file_version_parents(self, texts, progress_bar):
2459
        """See check_file_version_parents."""
2460
        wrong_parents = {}
2461
        self.file_ids = set([file_id for file_id, _ in
2462
            self.text_index.iterkeys()])
2463
        # text keys is now grouped by file_id
2464
        n_versions = len(self.text_index)
2465
        progress_bar.update('loading text store', 0, n_versions)
2466
        parent_map = self.repository.texts.get_parent_map(self.text_index)
2467
        # On unlistable transports this could well be empty/error...
2468
        text_keys = self.repository.texts.keys()
2469
        unused_keys = frozenset(text_keys) - set(self.text_index)
2470
        for num, key in enumerate(self.text_index.iterkeys()):
2471
            progress_bar.update('checking text graph', num, n_versions)
2472
            correct_parents = self.calculate_file_version_parents(key)
2473
            try:
2474
                knit_parents = parent_map[key]
2475
            except errors.RevisionNotPresent:
2476
                # Missing text!
2477
                knit_parents = None
2478
            if correct_parents != knit_parents:
2479
                wrong_parents[key] = (knit_parents, correct_parents)
2480
        return wrong_parents, unused_keys
2481
2482
5863.4.1 by Jelmer Vernooij
Move interrepository implementation to vf_repository.
2483
class InterVersionedFileRepository(InterRepository):
2484
2485
    _walk_to_common_revisions_batch_size = 50
2486
2487
    @needs_write_lock
2488
    def fetch(self, revision_id=None, find_ghosts=False,
2489
            fetch_spec=None):
2490
        """Fetch the content required to construct revision_id.
2491
2492
        The content is copied from self.source to self.target.
2493
2494
        :param revision_id: if None all content is copied, if NULL_REVISION no
2495
                            content is copied.
2496
        :return: None.
2497
        """
6047.1.1 by Jelmer Vernooij
Use show_user_warning rather than custom warning methods on UIFactory.
2498
        if self.target._format.experimental:
2499
            ui.ui_factory.show_user_warning('experimental_format_fetch',
2500
                from_format=self.source._format,
2501
                to_format=self.target._format)
5863.4.1 by Jelmer Vernooij
Move interrepository implementation to vf_repository.
2502
        from bzrlib.fetch import RepoFetcher
2503
        # See <https://launchpad.net/bugs/456077> asking for a warning here
2504
        if self.source._format.network_name() != self.target._format.network_name():
2505
            ui.ui_factory.show_user_warning('cross_format_fetch',
2506
                from_format=self.source._format,
2507
                to_format=self.target._format)
2508
        f = RepoFetcher(to_repository=self.target,
2509
                               from_repository=self.source,
2510
                               last_revision=revision_id,
2511
                               fetch_spec=fetch_spec,
2512
                               find_ghosts=find_ghosts)
2513
2514
    def _walk_to_common_revisions(self, revision_ids, if_present_ids=None):
2515
        """Walk out from revision_ids in source to revisions target has.
2516
2517
        :param revision_ids: The start point for the search.
2518
        :return: A set of revision ids.
2519
        """
2520
        target_graph = self.target.get_graph()
2521
        revision_ids = frozenset(revision_ids)
2522
        if if_present_ids:
2523
            all_wanted_revs = revision_ids.union(if_present_ids)
2524
        else:
2525
            all_wanted_revs = revision_ids
2526
        missing_revs = set()
2527
        source_graph = self.source.get_graph()
2528
        # ensure we don't pay silly lookup costs.
2529
        searcher = source_graph._make_breadth_first_searcher(all_wanted_revs)
2530
        null_set = frozenset([_mod_revision.NULL_REVISION])
2531
        searcher_exhausted = False
2532
        while True:
2533
            next_revs = set()
2534
            ghosts = set()
2535
            # Iterate the searcher until we have enough next_revs
2536
            while len(next_revs) < self._walk_to_common_revisions_batch_size:
2537
                try:
2538
                    next_revs_part, ghosts_part = searcher.next_with_ghosts()
2539
                    next_revs.update(next_revs_part)
2540
                    ghosts.update(ghosts_part)
2541
                except StopIteration:
2542
                    searcher_exhausted = True
2543
                    break
2544
            # If there are ghosts in the source graph, and the caller asked for
2545
            # them, make sure that they are present in the target.
2546
            # We don't care about other ghosts as we can't fetch them and
2547
            # haven't been asked to.
2548
            ghosts_to_check = set(revision_ids.intersection(ghosts))
2549
            revs_to_get = set(next_revs).union(ghosts_to_check)
2550
            if revs_to_get:
2551
                have_revs = set(target_graph.get_parent_map(revs_to_get))
2552
                # we always have NULL_REVISION present.
2553
                have_revs = have_revs.union(null_set)
2554
                # Check if the target is missing any ghosts we need.
2555
                ghosts_to_check.difference_update(have_revs)
2556
                if ghosts_to_check:
2557
                    # One of the caller's revision_ids is a ghost in both the
2558
                    # source and the target.
2559
                    raise errors.NoSuchRevision(
2560
                        self.source, ghosts_to_check.pop())
2561
                missing_revs.update(next_revs - have_revs)
2562
                # Because we may have walked past the original stop point, make
2563
                # sure everything is stopped
2564
                stop_revs = searcher.find_seen_ancestors(have_revs)
2565
                searcher.stop_searching_any(stop_revs)
2566
            if searcher_exhausted:
2567
                break
2568
        return searcher.get_result()
2569
2570
    @needs_read_lock
2571
    def search_missing_revision_ids(self,
2572
            revision_id=symbol_versioning.DEPRECATED_PARAMETER,
5852.1.7 by Jelmer Vernooij
merge bzr.dev.
2573
            find_ghosts=True, revision_ids=None, if_present_ids=None,
2574
            limit=None):
5863.4.1 by Jelmer Vernooij
Move interrepository implementation to vf_repository.
2575
        """Return the revision ids that source has that target does not.
2576
2577
        :param revision_id: only return revision ids included by this
2578
            revision_id.
2579
        :param revision_ids: return revision ids included by these
2580
            revision_ids.  NoSuchRevision will be raised if any of these
2581
            revisions are not present.
2582
        :param if_present_ids: like revision_ids, but will not cause
2583
            NoSuchRevision if any of these are absent, instead they will simply
2584
            not be in the result.  This is useful for e.g. finding revisions
2585
            to fetch for tags, which may reference absent revisions.
2586
        :param find_ghosts: If True find missing revisions in deep history
2587
            rather than just finding the surface difference.
2588
        :return: A bzrlib.graph.SearchResult.
2589
        """
2590
        if symbol_versioning.deprecated_passed(revision_id):
2591
            symbol_versioning.warn(
2592
                'search_missing_revision_ids(revision_id=...) was '
2593
                'deprecated in 2.4.  Use revision_ids=[...] instead.',
2594
                DeprecationWarning, stacklevel=2)
2595
            if revision_ids is not None:
2596
                raise AssertionError(
2597
                    'revision_ids is mutually exclusive with revision_id')
2598
            if revision_id is not None:
2599
                revision_ids = [revision_id]
2600
        del revision_id
2601
        # stop searching at found target revisions.
2602
        if not find_ghosts and (revision_ids is not None or if_present_ids is
2603
                not None):
5852.1.7 by Jelmer Vernooij
merge bzr.dev.
2604
            result = self._walk_to_common_revisions(revision_ids,
5863.4.1 by Jelmer Vernooij
Move interrepository implementation to vf_repository.
2605
                    if_present_ids=if_present_ids)
5852.1.8 by Jelmer Vernooij
Simplify revision limiting.
2606
            if limit is None:
2607
                return result
5852.1.7 by Jelmer Vernooij
merge bzr.dev.
2608
            result_set = result.get_keys()
2609
        else:
2610
            # generic, possibly worst case, slow code path.
2611
            target_ids = set(self.target.all_revision_ids())
2612
            source_ids = self._present_source_revisions_for(
2613
                revision_ids, if_present_ids)
2614
            result_set = set(source_ids).difference(target_ids)
2615
        if limit is not None:
5852.1.8 by Jelmer Vernooij
Simplify revision limiting.
2616
            topo_ordered = self.source.get_graph().iter_topo_order(result_set)
2617
            result_set = set(itertools.islice(topo_ordered, limit))
5863.4.1 by Jelmer Vernooij
Move interrepository implementation to vf_repository.
2618
        return self.source.revision_ids_to_search_result(result_set)
2619
2620
    def _present_source_revisions_for(self, revision_ids, if_present_ids=None):
2621
        """Returns set of all revisions in ancestry of revision_ids present in
2622
        the source repo.
2623
2624
        :param revision_ids: if None, all revisions in source are returned.
2625
        :param if_present_ids: like revision_ids, but if any/all of these are
2626
            absent no error is raised.
2627
        """
2628
        if revision_ids is not None or if_present_ids is not None:
2629
            # First, ensure all specified revisions exist.  Callers expect
2630
            # NoSuchRevision when they pass absent revision_ids here.
2631
            if revision_ids is None:
2632
                revision_ids = set()
2633
            if if_present_ids is None:
2634
                if_present_ids = set()
2635
            revision_ids = set(revision_ids)
2636
            if_present_ids = set(if_present_ids)
2637
            all_wanted_ids = revision_ids.union(if_present_ids)
2638
            graph = self.source.get_graph()
2639
            present_revs = set(graph.get_parent_map(all_wanted_ids))
2640
            missing = revision_ids.difference(present_revs)
2641
            if missing:
2642
                raise errors.NoSuchRevision(self.source, missing.pop())
2643
            found_ids = all_wanted_ids.intersection(present_revs)
2644
            source_ids = [rev_id for (rev_id, parents) in
2645
                          graph.iter_ancestry(found_ids)
2646
                          if rev_id != _mod_revision.NULL_REVISION
2647
                          and parents is not None]
2648
        else:
2649
            source_ids = self.source.all_revision_ids()
2650
        return set(source_ids)
2651
2652
    @classmethod
2653
    def _get_repo_format_to_test(self):
2654
        return None
2655
2656
    @classmethod
2657
    def is_compatible(cls, source, target):
2658
        # The default implementation is compatible with everything
2659
        return (source._format.supports_full_versioned_files and
2660
                target._format.supports_full_versioned_files)
2661
2662
2663
class InterDifferingSerializer(InterVersionedFileRepository):
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
2664
2665
    @classmethod
2666
    def _get_repo_format_to_test(self):
2667
        return None
2668
2669
    @staticmethod
2670
    def is_compatible(source, target):
5815.4.19 by Jelmer Vernooij
Fix test failures.
2671
        if not source._format.supports_full_versioned_files:
2672
            return False
2673
        if not target._format.supports_full_versioned_files:
2674
            return False
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
2675
        # This is redundant with format.check_conversion_target(), however that
2676
        # raises an exception, and we just want to say "False" as in we won't
2677
        # support converting between these formats.
2678
        if 'IDS_never' in debug.debug_flags:
2679
            return False
2680
        if source.supports_rich_root() and not target.supports_rich_root():
2681
            return False
2682
        if (source._format.supports_tree_reference
2683
            and not target._format.supports_tree_reference):
2684
            return False
2685
        if target._fallback_repositories and target._format.supports_chks:
2686
            # IDS doesn't know how to copy CHKs for the parent inventories it
2687
            # adds to stacked repos.
2688
            return False
2689
        if 'IDS_always' in debug.debug_flags:
2690
            return True
2691
        # Only use this code path for local source and target.  IDS does far
2692
        # too much IO (both bandwidth and roundtrips) over a network.
2693
        if not source.bzrdir.transport.base.startswith('file:///'):
2694
            return False
2695
        if not target.bzrdir.transport.base.startswith('file:///'):
2696
            return False
2697
        return True
2698
2699
    def _get_trees(self, revision_ids, cache):
2700
        possible_trees = []
2701
        for rev_id in revision_ids:
2702
            if rev_id in cache:
2703
                possible_trees.append((rev_id, cache[rev_id]))
2704
            else:
2705
                # Not cached, but inventory might be present anyway.
2706
                try:
2707
                    tree = self.source.revision_tree(rev_id)
2708
                except errors.NoSuchRevision:
2709
                    # Nope, parent is ghost.
2710
                    pass
2711
                else:
2712
                    cache[rev_id] = tree
2713
                    possible_trees.append((rev_id, tree))
2714
        return possible_trees
2715
2716
    def _get_delta_for_revision(self, tree, parent_ids, possible_trees):
2717
        """Get the best delta and base for this revision.
2718
2719
        :return: (basis_id, delta)
2720
        """
2721
        deltas = []
2722
        # Generate deltas against each tree, to find the shortest.
2723
        texts_possibly_new_in_tree = set()
2724
        for basis_id, basis_tree in possible_trees:
2725
            delta = tree.inventory._make_delta(basis_tree.inventory)
2726
            for old_path, new_path, file_id, new_entry in delta:
2727
                if new_path is None:
2728
                    # This file_id isn't present in the new rev, so we don't
2729
                    # care about it.
2730
                    continue
2731
                if not new_path:
2732
                    # Rich roots are handled elsewhere...
2733
                    continue
2734
                kind = new_entry.kind
2735
                if kind != 'directory' and kind != 'file':
2736
                    # No text record associated with this inventory entry.
2737
                    continue
2738
                # This is a directory or file that has changed somehow.
2739
                texts_possibly_new_in_tree.add((file_id, new_entry.revision))
2740
            deltas.append((len(delta), basis_id, delta))
2741
        deltas.sort()
2742
        return deltas[0][1:]
2743
2744
    def _fetch_parent_invs_for_stacking(self, parent_map, cache):
2745
        """Find all parent revisions that are absent, but for which the
2746
        inventory is present, and copy those inventories.
2747
2748
        This is necessary to preserve correctness when the source is stacked
2749
        without fallbacks configured.  (Note that in cases like upgrade the
2750
        source may be not have _fallback_repositories even though it is
2751
        stacked.)
2752
        """
2753
        parent_revs = set()
2754
        for parents in parent_map.values():
2755
            parent_revs.update(parents)
2756
        present_parents = self.source.get_parent_map(parent_revs)
2757
        absent_parents = set(parent_revs).difference(present_parents)
2758
        parent_invs_keys_for_stacking = self.source.inventories.get_parent_map(
2759
            (rev_id,) for rev_id in absent_parents)
2760
        parent_inv_ids = [key[-1] for key in parent_invs_keys_for_stacking]
2761
        for parent_tree in self.source.revision_trees(parent_inv_ids):
2762
            current_revision_id = parent_tree.get_revision_id()
2763
            parents_parents_keys = parent_invs_keys_for_stacking[
2764
                (current_revision_id,)]
2765
            parents_parents = [key[-1] for key in parents_parents_keys]
2766
            basis_id = _mod_revision.NULL_REVISION
2767
            basis_tree = self.source.revision_tree(basis_id)
2768
            delta = parent_tree.inventory._make_delta(basis_tree.inventory)
2769
            self.target.add_inventory_by_delta(
2770
                basis_id, delta, current_revision_id, parents_parents)
2771
            cache[current_revision_id] = parent_tree
2772
2773
    def _fetch_batch(self, revision_ids, basis_id, cache):
2774
        """Fetch across a few revisions.
2775
2776
        :param revision_ids: The revisions to copy
2777
        :param basis_id: The revision_id of a tree that must be in cache, used
2778
            as a basis for delta when no other base is available
2779
        :param cache: A cache of RevisionTrees that we can use.
2780
        :return: The revision_id of the last converted tree. The RevisionTree
2781
            for it will be in cache
2782
        """
2783
        # Walk though all revisions; get inventory deltas, copy referenced
2784
        # texts that delta references, insert the delta, revision and
2785
        # signature.
2786
        root_keys_to_create = set()
2787
        text_keys = set()
2788
        pending_deltas = []
2789
        pending_revisions = []
2790
        parent_map = self.source.get_parent_map(revision_ids)
2791
        self._fetch_parent_invs_for_stacking(parent_map, cache)
2792
        self.source._safe_to_return_from_cache = True
2793
        for tree in self.source.revision_trees(revision_ids):
2794
            # Find a inventory delta for this revision.
2795
            # Find text entries that need to be copied, too.
2796
            current_revision_id = tree.get_revision_id()
2797
            parent_ids = parent_map.get(current_revision_id, ())
2798
            parent_trees = self._get_trees(parent_ids, cache)
2799
            possible_trees = list(parent_trees)
2800
            if len(possible_trees) == 0:
2801
                # There either aren't any parents, or the parents are ghosts,
2802
                # so just use the last converted tree.
2803
                possible_trees.append((basis_id, cache[basis_id]))
2804
            basis_id, delta = self._get_delta_for_revision(tree, parent_ids,
2805
                                                           possible_trees)
2806
            revision = self.source.get_revision(current_revision_id)
2807
            pending_deltas.append((basis_id, delta,
2808
                current_revision_id, revision.parent_ids))
2809
            if self._converting_to_rich_root:
2810
                self._revision_id_to_root_id[current_revision_id] = \
2811
                    tree.get_root_id()
2812
            # Determine which texts are in present in this revision but not in
2813
            # any of the available parents.
2814
            texts_possibly_new_in_tree = set()
2815
            for old_path, new_path, file_id, entry in delta:
2816
                if new_path is None:
2817
                    # This file_id isn't present in the new rev
2818
                    continue
2819
                if not new_path:
2820
                    # This is the root
2821
                    if not self.target.supports_rich_root():
2822
                        # The target doesn't support rich root, so we don't
2823
                        # copy
2824
                        continue
2825
                    if self._converting_to_rich_root:
2826
                        # This can't be copied normally, we have to insert
2827
                        # it specially
2828
                        root_keys_to_create.add((file_id, entry.revision))
2829
                        continue
2830
                kind = entry.kind
2831
                texts_possibly_new_in_tree.add((file_id, entry.revision))
2832
            for basis_id, basis_tree in possible_trees:
2833
                basis_inv = basis_tree.inventory
2834
                for file_key in list(texts_possibly_new_in_tree):
2835
                    file_id, file_revision = file_key
2836
                    try:
2837
                        entry = basis_inv[file_id]
2838
                    except errors.NoSuchId:
2839
                        continue
2840
                    if entry.revision == file_revision:
2841
                        texts_possibly_new_in_tree.remove(file_key)
2842
            text_keys.update(texts_possibly_new_in_tree)
2843
            pending_revisions.append(revision)
2844
            cache[current_revision_id] = tree
2845
            basis_id = current_revision_id
2846
        self.source._safe_to_return_from_cache = False
2847
        # Copy file texts
2848
        from_texts = self.source.texts
2849
        to_texts = self.target.texts
2850
        if root_keys_to_create:
2851
            root_stream = _mod_fetch._new_root_data_stream(
2852
                root_keys_to_create, self._revision_id_to_root_id, parent_map,
2853
                self.source)
2854
            to_texts.insert_record_stream(root_stream)
2855
        to_texts.insert_record_stream(from_texts.get_record_stream(
2856
            text_keys, self.target._format._fetch_order,
2857
            not self.target._format._fetch_uses_deltas))
2858
        # insert inventory deltas
2859
        for delta in pending_deltas:
2860
            self.target.add_inventory_by_delta(*delta)
2861
        if self.target._fallback_repositories:
2862
            # Make sure this stacked repository has all the parent inventories
2863
            # for the new revisions that we are about to insert.  We do this
2864
            # before adding the revisions so that no revision is added until
2865
            # all the inventories it may depend on are added.
2866
            # Note that this is overzealous, as we may have fetched these in an
2867
            # earlier batch.
2868
            parent_ids = set()
2869
            revision_ids = set()
2870
            for revision in pending_revisions:
2871
                revision_ids.add(revision.revision_id)
2872
                parent_ids.update(revision.parent_ids)
2873
            parent_ids.difference_update(revision_ids)
2874
            parent_ids.discard(_mod_revision.NULL_REVISION)
2875
            parent_map = self.source.get_parent_map(parent_ids)
2876
            # we iterate over parent_map and not parent_ids because we don't
2877
            # want to try copying any revision which is a ghost
2878
            for parent_tree in self.source.revision_trees(parent_map):
2879
                current_revision_id = parent_tree.get_revision_id()
2880
                parents_parents = parent_map[current_revision_id]
2881
                possible_trees = self._get_trees(parents_parents, cache)
2882
                if len(possible_trees) == 0:
2883
                    # There either aren't any parents, or the parents are
2884
                    # ghosts, so just use the last converted tree.
2885
                    possible_trees.append((basis_id, cache[basis_id]))
2886
                basis_id, delta = self._get_delta_for_revision(parent_tree,
2887
                    parents_parents, possible_trees)
2888
                self.target.add_inventory_by_delta(
2889
                    basis_id, delta, current_revision_id, parents_parents)
2890
        # insert signatures and revisions
2891
        for revision in pending_revisions:
2892
            try:
2893
                signature = self.source.get_signature_text(
2894
                    revision.revision_id)
2895
                self.target.add_signature_text(revision.revision_id,
2896
                    signature)
2897
            except errors.NoSuchRevision:
2898
                pass
2899
            self.target.add_revision(revision.revision_id, revision)
2900
        return basis_id
2901
2902
    def _fetch_all_revisions(self, revision_ids, pb):
2903
        """Fetch everything for the list of revisions.
2904
2905
        :param revision_ids: The list of revisions to fetch. Must be in
2906
            topological order.
2907
        :param pb: A ProgressTask
2908
        :return: None
2909
        """
2910
        basis_id, basis_tree = self._get_basis(revision_ids[0])
2911
        batch_size = 100
2912
        cache = lru_cache.LRUCache(100)
2913
        cache[basis_id] = basis_tree
2914
        del basis_tree # We don't want to hang on to it here
2915
        hints = []
2916
        a_graph = None
2917
2918
        for offset in range(0, len(revision_ids), batch_size):
2919
            self.target.start_write_group()
2920
            try:
2921
                pb.update('Transferring revisions', offset,
2922
                          len(revision_ids))
2923
                batch = revision_ids[offset:offset+batch_size]
2924
                basis_id = self._fetch_batch(batch, basis_id, cache)
2925
            except:
2926
                self.source._safe_to_return_from_cache = False
2927
                self.target.abort_write_group()
2928
                raise
2929
            else:
2930
                hint = self.target.commit_write_group()
2931
                if hint:
2932
                    hints.extend(hint)
2933
        if hints and self.target._format.pack_compresses:
2934
            self.target.pack(hint=hints)
2935
        pb.update('Transferring revisions', len(revision_ids),
2936
                  len(revision_ids))
2937
2938
    @needs_write_lock
2939
    def fetch(self, revision_id=None, find_ghosts=False,
2940
            fetch_spec=None):
2941
        """See InterRepository.fetch()."""
2942
        if fetch_spec is not None:
2943
            revision_ids = fetch_spec.get_keys()
2944
        else:
2945
            revision_ids = None
6047.1.1 by Jelmer Vernooij
Use show_user_warning rather than custom warning methods on UIFactory.
2946
        if self.source._format.experimental:
2947
            ui.ui_factory.show_user_warning('experimental_format_fetch',
2948
                from_format=self.source._format,
2949
                to_format=self.target._format)
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
2950
        if (not self.source.supports_rich_root()
2951
            and self.target.supports_rich_root()):
2952
            self._converting_to_rich_root = True
2953
            self._revision_id_to_root_id = {}
2954
        else:
2955
            self._converting_to_rich_root = False
2956
        # See <https://launchpad.net/bugs/456077> asking for a warning here
2957
        if self.source._format.network_name() != self.target._format.network_name():
2958
            ui.ui_factory.show_user_warning('cross_format_fetch',
2959
                from_format=self.source._format,
2960
                to_format=self.target._format)
2961
        if revision_ids is None:
2962
            if revision_id:
2963
                search_revision_ids = [revision_id]
2964
            else:
2965
                search_revision_ids = None
2966
            revision_ids = self.target.search_missing_revision_ids(self.source,
2967
                revision_ids=search_revision_ids,
2968
                find_ghosts=find_ghosts).get_keys()
2969
        if not revision_ids:
2970
            return 0, 0
2971
        revision_ids = tsort.topo_sort(
2972
            self.source.get_graph().get_parent_map(revision_ids))
2973
        if not revision_ids:
2974
            return 0, 0
2975
        # Walk though all revisions; get inventory deltas, copy referenced
2976
        # texts that delta references, insert the delta, revision and
2977
        # signature.
2978
        pb = ui.ui_factory.nested_progress_bar()
2979
        try:
2980
            self._fetch_all_revisions(revision_ids, pb)
2981
        finally:
2982
            pb.finished()
2983
        return len(revision_ids), 0
2984
2985
    def _get_basis(self, first_revision_id):
2986
        """Get a revision and tree which exists in the target.
2987
2988
        This assumes that first_revision_id is selected for transmission
2989
        because all other ancestors are already present. If we can't find an
2990
        ancestor we fall back to NULL_REVISION since we know that is safe.
2991
2992
        :return: (basis_id, basis_tree)
2993
        """
2994
        first_rev = self.source.get_revision(first_revision_id)
2995
        try:
2996
            basis_id = first_rev.parent_ids[0]
2997
            # only valid as a basis if the target has it
2998
            self.target.get_revision(basis_id)
2999
            # Try to get a basis tree - if it's a ghost it will hit the
3000
            # NoSuchRevision case.
3001
            basis_tree = self.source.revision_tree(basis_id)
3002
        except (IndexError, errors.NoSuchRevision):
3003
            basis_id = _mod_revision.NULL_REVISION
3004
            basis_tree = self.source.revision_tree(basis_id)
3005
        return basis_id, basis_tree
3006
3007
5863.4.1 by Jelmer Vernooij
Move interrepository implementation to vf_repository.
3008
class InterSameDataRepository(InterVersionedFileRepository):
5815.4.19 by Jelmer Vernooij
Fix test failures.
3009
    """Code for converting between repositories that represent the same data.
3010
3011
    Data format and model must match for this to work.
3012
    """
3013
3014
    @classmethod
3015
    def _get_repo_format_to_test(self):
3016
        """Repository format for testing with.
3017
3018
        InterSameData can pull from subtree to subtree and from non-subtree to
3019
        non-subtree, so we test this with the richest repository format.
3020
        """
3021
        from bzrlib.repofmt import knitrepo
3022
        return knitrepo.RepositoryFormatKnit3()
3023
3024
    @staticmethod
3025
    def is_compatible(source, target):
3026
        return (
3027
            InterRepository._same_model(source, target) and
3028
            source._format.supports_full_versioned_files and
3029
            target._format.supports_full_versioned_files)
3030
3031
5863.4.1 by Jelmer Vernooij
Move interrepository implementation to vf_repository.
3032
InterRepository.register_optimiser(InterVersionedFileRepository)
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
3033
InterRepository.register_optimiser(InterDifferingSerializer)
5815.4.19 by Jelmer Vernooij
Fix test failures.
3034
InterRepository.register_optimiser(InterSameDataRepository)
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
3035
3036
3037
def install_revisions(repository, iterable, num_revisions=None, pb=None):
3038
    """Install all revision data into a repository.
3039
3040
    Accepts an iterable of revision, tree, signature tuples.  The signature
3041
    may be None.
3042
    """
3043
    repository.start_write_group()
3044
    try:
3045
        inventory_cache = lru_cache.LRUCache(10)
3046
        for n, (revision, revision_tree, signature) in enumerate(iterable):
3047
            _install_revision(repository, revision, revision_tree, signature,
3048
                inventory_cache)
3049
            if pb is not None:
3050
                pb.update('Transferring revisions', n + 1, num_revisions)
3051
    except:
3052
        repository.abort_write_group()
3053
        raise
3054
    else:
3055
        repository.commit_write_group()
3056
3057
3058
def _install_revision(repository, rev, revision_tree, signature,
3059
    inventory_cache):
3060
    """Install all revision data into a repository."""
3061
    present_parents = []
3062
    parent_trees = {}
3063
    for p_id in rev.parent_ids:
3064
        if repository.has_revision(p_id):
3065
            present_parents.append(p_id)
3066
            parent_trees[p_id] = repository.revision_tree(p_id)
3067
        else:
3068
            parent_trees[p_id] = repository.revision_tree(
3069
                                     _mod_revision.NULL_REVISION)
3070
3071
    inv = revision_tree.inventory
3072
    entries = inv.iter_entries()
3073
    # backwards compatibility hack: skip the root id.
3074
    if not repository.supports_rich_root():
3075
        path, root = entries.next()
3076
        if root.revision != rev.revision_id:
3077
            raise errors.IncompatibleRevision(repr(repository))
3078
    text_keys = {}
3079
    for path, ie in entries:
3080
        text_keys[(ie.file_id, ie.revision)] = ie
3081
    text_parent_map = repository.texts.get_parent_map(text_keys)
3082
    missing_texts = set(text_keys) - set(text_parent_map)
3083
    # Add the texts that are not already present
3084
    for text_key in missing_texts:
3085
        ie = text_keys[text_key]
3086
        text_parents = []
3087
        # FIXME: TODO: The following loop overlaps/duplicates that done by
3088
        # commit to determine parents. There is a latent/real bug here where
3089
        # the parents inserted are not those commit would do - in particular
3090
        # they are not filtered by heads(). RBC, AB
3091
        for revision, tree in parent_trees.iteritems():
5967.7.1 by Martin Pool
Deprecate __contains__ on Tree and Inventory
3092
            if not tree.has_id(ie.file_id):
5815.4.1 by Jelmer Vernooij
Split versionedfile-specific stuff out into VersionedFileRepository.
3093
                continue
3094
            parent_id = tree.get_file_revision(ie.file_id)
3095
            if parent_id in text_parents:
3096
                continue
3097
            text_parents.append((ie.file_id, parent_id))
3098
        lines = revision_tree.get_file(ie.file_id).readlines()
3099
        repository.texts.add_lines(text_key, text_parents, lines)
3100
    try:
3101
        # install the inventory
3102
        if repository._format._commit_inv_deltas and len(rev.parent_ids):
3103
            # Cache this inventory
3104
            inventory_cache[rev.revision_id] = inv
3105
            try:
3106
                basis_inv = inventory_cache[rev.parent_ids[0]]
3107
            except KeyError:
3108
                repository.add_inventory(rev.revision_id, inv, present_parents)
3109
            else:
3110
                delta = inv._make_delta(basis_inv)
3111
                repository.add_inventory_by_delta(rev.parent_ids[0], delta,
3112
                    rev.revision_id, present_parents)
3113
        else:
3114
            repository.add_inventory(rev.revision_id, inv, present_parents)
3115
    except errors.RevisionAlreadyPresent:
3116
        pass
3117
    if signature is not None:
3118
        repository.add_signature_text(rev.revision_id, signature)
3119
    repository.add_revision(rev.revision_id, rev, inv)
3120
3121
3122
def install_revision(repository, rev, revision_tree):
3123
    """Install all revision data into a repository."""
3124
    install_revisions(repository, [(rev, revision_tree, None)])