/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/repository.py

  • Committer: John Arbash Meinel
  • Date: 2009-04-01 15:49:55 UTC
  • mto: (3735.2.184 brisbane-core)
  • mto: This revision was merged to the branch mainline in revision 4280.
  • Revision ID: john@arbash-meinel.com-20090401154955-vukyb3s3igmrnu95
Change the tests so we now just use a direct test that _get_source is
returning the optimized GroupCHKStreamSource.

Show diffs side-by-side

added added

removed removed

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