/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2220.2.1 by Martin Pool
Start adding space for tags stored in the repository
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1185.65.10 by Robert Collins
Rename Controlfiles to LockableFiles.
16
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
17
from cStringIO import StringIO
18
19
from bzrlib.lazy_import import lazy_import
20
lazy_import(globals(), """
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
21
import re
22
import time
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
23
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
24
from bzrlib import (
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
25
    bzrdir,
26
    check,
2745.1.1 by Robert Collins
Add a number of -Devil checkpoints.
27
    debug,
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
28
    deprecated_graph,
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
29
    errors,
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
30
    generate_ids,
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
31
    gpg,
32
    graph,
2163.2.1 by John Arbash Meinel
Speed up the fileids_altered_by_revision_ids processing
33
    lazy_regex,
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
34
    lockable_files,
35
    lockdir,
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
36
    osutils,
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
37
    registry,
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
38
    remote,
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
39
    revision as _mod_revision,
40
    symbol_versioning,
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
41
    transactions,
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
42
    ui,
43
    )
2520.4.54 by Aaron Bentley
Hang a create_bundle method off repository
44
from bzrlib.bundle import serializer
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
45
from bzrlib.revisiontree import RevisionTree
46
from bzrlib.store.versioned import VersionedFileStore
47
from bzrlib.store.text import TextStore
48
from bzrlib.testament import Testament
2535.3.40 by Andrew Bennetts
Tidy up more XXXs.
49
from bzrlib.util import bencode
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
50
""")
51
1534.4.28 by Robert Collins
first cut at merge from integration.
52
from bzrlib.decorators import needs_read_lock, needs_write_lock
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
53
from bzrlib.inter import InterObject
1910.2.3 by Aaron Bentley
All tests pass
54
from bzrlib.inventory import Inventory, InventoryDirectory, ROOT_ID
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
55
from bzrlib.symbol_versioning import (
56
        deprecated_method,
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
57
        )
2745.1.1 by Robert Collins
Add a number of -Devil checkpoints.
58
from bzrlib.trace import mutter, mutter_callsite, note, warning
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
59
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
60
1904.2.5 by Martin Pool
Fix format warning inside test suite and add test
61
# Old formats display a warning, but only once
62
_deprecation_warning_done = False
63
64
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
65
class CommitBuilder(object):
66
    """Provides an interface to build up a commit.
67
68
    This allows describing a tree to be committed without needing to 
69
    know the internals of the format of the repository.
70
    """
71
    
72
    # all clients should supply tree roots.
73
    record_root_entry = True
2825.5.2 by Robert Collins
Review feedback, and fix pointless commits with nested trees to raise PointlessCommit appropriately.
74
    # the default CommitBuilder does not manage trees whose root is versioned.
75
    _versioned_root = False
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
76
77
    def __init__(self, repository, parents, config, timestamp=None, 
78
                 timezone=None, committer=None, revprops=None, 
79
                 revision_id=None):
80
        """Initiate a CommitBuilder.
81
82
        :param repository: Repository to commit to.
83
        :param parents: Revision ids of the parents of the new revision.
84
        :param config: Configuration to use.
85
        :param timestamp: Optional timestamp recorded for commit.
86
        :param timezone: Optional timezone for timestamp.
87
        :param committer: Optional committer to set for commit.
88
        :param revprops: Optional dictionary of revision properties.
89
        :param revision_id: Optional revision id.
90
        """
91
        self._config = config
92
93
        if committer is None:
94
            self._committer = self._config.username()
95
        else:
96
            assert isinstance(committer, basestring), type(committer)
97
            self._committer = committer
98
99
        self.new_inventory = Inventory(None)
2858.2.1 by Martin Pool
Remove most calls to safe_file_id and safe_revision_id.
100
        self._new_revision_id = revision_id
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
101
        self.parents = parents
102
        self.repository = repository
103
104
        self._revprops = {}
105
        if revprops is not None:
106
            self._revprops.update(revprops)
107
108
        if timestamp is None:
109
            timestamp = time.time()
110
        # Restrict resolution to 1ms
111
        self._timestamp = round(timestamp, 3)
112
113
        if timezone is None:
114
            self._timezone = osutils.local_time_offset()
115
        else:
116
            self._timezone = int(timezone)
117
118
        self._generate_revision_if_needed()
2776.4.20 by Robert Collins
(robertc) Reduce object creation volume during commit by reusing the repository graph object. (Robert Collins)
119
        self._repo_graph = repository.get_graph()
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
120
121
    def commit(self, message):
122
        """Make the actual commit.
123
124
        :return: The revision id of the recorded revision.
125
        """
126
        rev = _mod_revision.Revision(
127
                       timestamp=self._timestamp,
128
                       timezone=self._timezone,
129
                       committer=self._committer,
130
                       message=message,
131
                       inventory_sha1=self.inv_sha1,
132
                       revision_id=self._new_revision_id,
133
                       properties=self._revprops)
134
        rev.parent_ids = self.parents
135
        self.repository.add_revision(self._new_revision_id, rev,
136
            self.new_inventory, self._config)
137
        self.repository.commit_write_group()
138
        return self._new_revision_id
139
140
    def abort(self):
141
        """Abort the commit that is being built.
142
        """
143
        self.repository.abort_write_group()
144
145
    def revision_tree(self):
146
        """Return the tree that was just committed.
147
148
        After calling commit() this can be called to get a RevisionTree
149
        representing the newly committed tree. This is preferred to
150
        calling Repository.revision_tree() because that may require
151
        deserializing the inventory, while we already have a copy in
152
        memory.
153
        """
154
        return RevisionTree(self.repository, self.new_inventory,
155
                            self._new_revision_id)
156
157
    def finish_inventory(self):
158
        """Tell the builder that the inventory is finished."""
159
        if self.new_inventory.root is None:
2903.2.9 by Martin Pool
Review cleanups, mostly documentation
160
            raise AssertionError('Root entry should be supplied to'
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
161
                ' record_entry_contents, as of bzr 0.10.',
162
                 DeprecationWarning, stacklevel=2)
163
            self.new_inventory.add(InventoryDirectory(ROOT_ID, '', None))
164
        self.new_inventory.revision_id = self._new_revision_id
165
        self.inv_sha1 = self.repository.add_inventory(
166
            self._new_revision_id,
167
            self.new_inventory,
168
            self.parents
169
            )
170
171
    def _gen_revision_id(self):
172
        """Return new revision-id."""
173
        return generate_ids.gen_revision_id(self._config.username(),
174
                                            self._timestamp)
175
176
    def _generate_revision_if_needed(self):
177
        """Create a revision id if None was supplied.
178
        
179
        If the repository can not support user-specified revision ids
180
        they should override this function and raise CannotSetRevisionId
181
        if _new_revision_id is not None.
182
183
        :raises: CannotSetRevisionId
184
        """
185
        if self._new_revision_id is None:
186
            self._new_revision_id = self._gen_revision_id()
187
            self.random_revid = True
188
        else:
189
            self.random_revid = False
190
191
    def _check_root(self, ie, parent_invs, tree):
192
        """Helper for record_entry_contents.
193
194
        :param ie: An entry being added.
195
        :param parent_invs: The inventories of the parent revisions of the
196
            commit.
197
        :param tree: The tree that is being committed.
198
        """
2871.1.2 by Robert Collins
* ``CommitBuilder.record_entry_contents`` now requires the root entry of a
199
        # In this revision format, root entries have no knit or weave When
200
        # serializing out to disk and back in root.revision is always
201
        # _new_revision_id
202
        ie.revision = self._new_revision_id
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
203
2871.1.4 by Robert Collins
Merge bzr.dev.
204
    def _get_delta(self, ie, basis_inv, path):
205
        """Get a delta against the basis inventory for ie."""
206
        if ie.file_id not in basis_inv:
207
            # add
208
            return (None, path, ie.file_id, ie)
209
        elif ie != basis_inv[ie.file_id]:
210
            # common but altered
211
            # TODO: avoid tis id2path call.
212
            return (basis_inv.id2path(ie.file_id), path, ie.file_id, ie)
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
213
        else:
2871.1.4 by Robert Collins
Merge bzr.dev.
214
            # common, unaltered
215
            return None
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
216
2776.4.11 by Robert Collins
Merge bzr.dev.
217
    def record_entry_contents(self, ie, parent_invs, path, tree,
218
        content_summary):
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
219
        """Record the content of ie from tree into the commit if needed.
220
221
        Side effect: sets ie.revision when unchanged
222
223
        :param ie: An inventory entry present in the commit.
224
        :param parent_invs: The inventories of the parent revisions of the
225
            commit.
226
        :param path: The path the entry is at in the tree.
227
        :param tree: The tree which contains this entry and should be used to 
2776.4.11 by Robert Collins
Merge bzr.dev.
228
            obtain content.
229
        :param content_summary: Summary data from the tree about the paths
230
            content - stat, length, exec, sha/link target. This is only
231
            accessed when the entry has a revision of None - that is when it is
232
            a candidate to commit.
2871.1.3 by Robert Collins
* The CommitBuilder method ``record_entry_contents`` now returns summary
233
        :return: A tuple (change_delta, version_recorded). change_delta is 
234
            an inventory_delta change for this entry against the basis tree of
235
            the commit, or None if no change occured against the basis tree.
236
            version_recorded is True if a new version of the entry has been
237
            recorded. For instance, committing a merge where a file was only
238
            changed on the other side will return (delta, False).
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
239
        """
240
        if self.new_inventory.root is None:
2871.1.2 by Robert Collins
* ``CommitBuilder.record_entry_contents`` now requires the root entry of a
241
            if ie.parent_id is not None:
242
                raise errors.RootMissing()
2825.5.2 by Robert Collins
Review feedback, and fix pointless commits with nested trees to raise PointlessCommit appropriately.
243
            self._check_root(ie, parent_invs, tree)
2776.4.11 by Robert Collins
Merge bzr.dev.
244
        if ie.revision is None:
245
            kind = content_summary[0]
246
        else:
247
            # ie is carried over from a prior commit
248
            kind = ie.kind
249
        # XXX: repository specific check for nested tree support goes here - if
250
        # the repo doesn't want nested trees we skip it ?
251
        if (kind == 'tree-reference' and
252
            not self.repository._format.supports_tree_reference):
253
            # mismatch between commit builder logic and repository:
254
            # this needs the entry creation pushed down into the builder.
2776.4.18 by Robert Collins
Review feedback.
255
            raise NotImplementedError('Missing repository subtree support.')
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
256
        self.new_inventory.add(ie)
257
2871.1.3 by Robert Collins
* The CommitBuilder method ``record_entry_contents`` now returns summary
258
        # TODO: slow, take it out of the inner loop.
259
        try:
260
            basis_inv = parent_invs[0]
261
        except IndexError:
262
            basis_inv = Inventory(root_id=None)
263
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
264
        # ie.revision is always None if the InventoryEntry is considered
2776.4.13 by Robert Collins
Merge bzr.dev.
265
        # for committing. We may record the previous parents revision if the
266
        # content is actually unchanged against a sole head.
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
267
        if ie.revision is not None:
2903.2.5 by Martin Pool
record_entry_contents should give back deltas for changed roots; clean it up a bit
268
            if not self._versioned_root and path == '':
2871.1.3 by Robert Collins
* The CommitBuilder method ``record_entry_contents`` now returns summary
269
                # repositories that do not version the root set the root's
270
                # revision to the new commit even when no change occurs, and
271
                # this masks when a change may have occurred against the basis,
272
                # so calculate if one happened.
2903.2.5 by Martin Pool
record_entry_contents should give back deltas for changed roots; clean it up a bit
273
                if ie.file_id in basis_inv:
274
                    delta = (basis_inv.id2path(ie.file_id), path,
275
                        ie.file_id, ie)
276
                else:
2871.1.3 by Robert Collins
* The CommitBuilder method ``record_entry_contents`` now returns summary
277
                    # add
278
                    delta = (None, path, ie.file_id, ie)
2903.2.5 by Martin Pool
record_entry_contents should give back deltas for changed roots; clean it up a bit
279
                return delta, False
280
            else:
281
                # we don't need to commit this, because the caller already
282
                # determined that an existing revision of this file is
283
                # appropriate.
2903.2.9 by Martin Pool
Review cleanups, mostly documentation
284
                return None, (ie.revision == self._new_revision_id)
2776.4.11 by Robert Collins
Merge bzr.dev.
285
        # XXX: Friction: parent_candidates should return a list not a dict
286
        #      so that we don't have to walk the inventories again.
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
287
        parent_candiate_entries = ie.parent_candidates(parent_invs)
2776.4.20 by Robert Collins
(robertc) Reduce object creation volume during commit by reusing the repository graph object. (Robert Collins)
288
        head_set = self._repo_graph.heads(parent_candiate_entries.keys())
2776.4.11 by Robert Collins
Merge bzr.dev.
289
        heads = []
290
        for inv in parent_invs:
291
            if ie.file_id in inv:
292
                old_rev = inv[ie.file_id].revision
293
                if old_rev in head_set:
294
                    heads.append(inv[ie.file_id].revision)
295
                    head_set.remove(inv[ie.file_id].revision)
296
297
        store = False
298
        # now we check to see if we need to write a new record to the
299
        # file-graph.
300
        # We write a new entry unless there is one head to the ancestors, and
301
        # the kind-derived content is unchanged.
302
303
        # Cheapest check first: no ancestors, or more the one head in the
304
        # ancestors, we write a new node.
305
        if len(heads) != 1:
306
            store = True
307
        if not store:
308
            # There is a single head, look it up for comparison
309
            parent_entry = parent_candiate_entries[heads[0]]
310
            # if the non-content specific data has changed, we'll be writing a
311
            # node:
312
            if (parent_entry.parent_id != ie.parent_id or
313
                parent_entry.name != ie.name):
314
                store = True
315
        # now we need to do content specific checks:
316
        if not store:
317
            # if the kind changed the content obviously has
318
            if kind != parent_entry.kind:
319
                store = True
320
        if kind == 'file':
321
            if not store:
322
                if (# if the file length changed we have to store:
323
                    parent_entry.text_size != content_summary[1] or
324
                    # if the exec bit has changed we have to store:
325
                    parent_entry.executable != content_summary[2]):
326
                    store = True
327
                elif parent_entry.text_sha1 == content_summary[3]:
328
                    # all meta and content is unchanged (using a hash cache
329
                    # hit to check the sha)
330
                    ie.revision = parent_entry.revision
331
                    ie.text_size = parent_entry.text_size
332
                    ie.text_sha1 = parent_entry.text_sha1
333
                    ie.executable = parent_entry.executable
2871.1.4 by Robert Collins
Merge bzr.dev.
334
                    return self._get_delta(ie, basis_inv, path), False
2776.4.11 by Robert Collins
Merge bzr.dev.
335
                else:
336
                    # Either there is only a hash change(no hash cache entry,
337
                    # or same size content change), or there is no change on
338
                    # this file at all.
2776.4.19 by Robert Collins
Final review tweaks.
339
                    # Provide the parent's hash to the store layer, so that the
340
                    # content is unchanged we will not store a new node.
2776.4.11 by Robert Collins
Merge bzr.dev.
341
                    nostore_sha = parent_entry.text_sha1
342
            if store:
2776.4.18 by Robert Collins
Review feedback.
343
                # We want to record a new node regardless of the presence or
344
                # absence of a content change in the file.
2776.4.11 by Robert Collins
Merge bzr.dev.
345
                nostore_sha = None
2776.4.18 by Robert Collins
Review feedback.
346
            ie.executable = content_summary[2]
347
            lines = tree.get_file(ie.file_id, path).readlines()
2776.4.11 by Robert Collins
Merge bzr.dev.
348
            try:
349
                ie.text_sha1, ie.text_size = self._add_text_to_weave(
350
                    ie.file_id, lines, heads, nostore_sha)
351
            except errors.ExistingContent:
2776.4.18 by Robert Collins
Review feedback.
352
                # Turns out that the file content was unchanged, and we were
353
                # only going to store a new node if it was changed. Carry over
354
                # the entry.
2776.4.11 by Robert Collins
Merge bzr.dev.
355
                ie.revision = parent_entry.revision
356
                ie.text_size = parent_entry.text_size
357
                ie.text_sha1 = parent_entry.text_sha1
358
                ie.executable = parent_entry.executable
2871.1.4 by Robert Collins
Merge bzr.dev.
359
                return self._get_delta(ie, basis_inv, path), False
2776.4.11 by Robert Collins
Merge bzr.dev.
360
        elif kind == 'directory':
361
            if not store:
362
                # all data is meta here, nothing specific to directory, so
363
                # carry over:
364
                ie.revision = parent_entry.revision
2871.1.4 by Robert Collins
Merge bzr.dev.
365
                return self._get_delta(ie, basis_inv, path), False
2776.4.11 by Robert Collins
Merge bzr.dev.
366
            lines = []
367
            self._add_text_to_weave(ie.file_id, lines, heads, None)
368
        elif kind == 'symlink':
369
            current_link_target = content_summary[3]
370
            if not store:
2776.4.18 by Robert Collins
Review feedback.
371
                # symlink target is not generic metadata, check if it has
2776.4.11 by Robert Collins
Merge bzr.dev.
372
                # changed.
373
                if current_link_target != parent_entry.symlink_target:
374
                    store = True
375
            if not store:
376
                # unchanged, carry over.
377
                ie.revision = parent_entry.revision
378
                ie.symlink_target = parent_entry.symlink_target
2871.1.4 by Robert Collins
Merge bzr.dev.
379
                return self._get_delta(ie, basis_inv, path), False
2776.4.11 by Robert Collins
Merge bzr.dev.
380
            ie.symlink_target = current_link_target
381
            lines = []
382
            self._add_text_to_weave(ie.file_id, lines, heads, None)
383
        elif kind == 'tree-reference':
384
            if not store:
385
                if content_summary[3] != parent_entry.reference_revision:
386
                    store = True
387
            if not store:
388
                # unchanged, carry over.
389
                ie.reference_revision = parent_entry.reference_revision
390
                ie.revision = parent_entry.revision
2871.1.4 by Robert Collins
Merge bzr.dev.
391
                return self._get_delta(ie, basis_inv, path), False
2776.4.11 by Robert Collins
Merge bzr.dev.
392
            ie.reference_revision = content_summary[3]
393
            lines = []
394
            self._add_text_to_weave(ie.file_id, lines, heads, None)
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
395
        else:
2776.4.11 by Robert Collins
Merge bzr.dev.
396
            raise NotImplementedError('unknown kind')
397
        ie.revision = self._new_revision_id
2871.1.4 by Robert Collins
Merge bzr.dev.
398
        return self._get_delta(ie, basis_inv, path), True
2776.4.11 by Robert Collins
Merge bzr.dev.
399
400
    def _add_text_to_weave(self, file_id, new_lines, parents, nostore_sha):
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
401
        versionedfile = self.repository.weave_store.get_weave_or_empty(
402
            file_id, self.repository.get_transaction())
403
        # Don't change this to add_lines - add_lines_with_ghosts is cheaper
404
        # than add_lines, and allows committing when a parent is ghosted for
405
        # some reason.
406
        # Note: as we read the content directly from the tree, we know its not
407
        # been turned into unicode or badly split - but a broken tree
408
        # implementation could give us bad output from readlines() so this is
409
        # not a guarantee of safety. What would be better is always checking
410
        # the content during test suite execution. RBC 20070912
2776.4.11 by Robert Collins
Merge bzr.dev.
411
        try:
412
            return versionedfile.add_lines_with_ghosts(
413
                self._new_revision_id, parents, new_lines,
414
                nostore_sha=nostore_sha, random_id=self.random_revid,
415
                check_content=False)[0:2]
416
        finally:
417
            versionedfile.clear_cache()
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
418
419
420
class RootCommitBuilder(CommitBuilder):
421
    """This commitbuilder actually records the root id"""
422
    
2825.5.2 by Robert Collins
Review feedback, and fix pointless commits with nested trees to raise PointlessCommit appropriately.
423
    # the root entry gets versioned properly by this builder.
2840.1.1 by Ian Clatworthy
faster pointless commit detection (Robert Collins)
424
    _versioned_root = True
2825.5.2 by Robert Collins
Review feedback, and fix pointless commits with nested trees to raise PointlessCommit appropriately.
425
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
426
    def _check_root(self, ie, parent_invs, tree):
427
        """Helper for record_entry_contents.
428
429
        :param ie: An entry being added.
430
        :param parent_invs: The inventories of the parent revisions of the
431
            commit.
432
        :param tree: The tree that is being committed.
433
        """
434
435
2220.2.3 by Martin Pool
Add tag: revision namespace.
436
######################################################################
437
# Repositories
438
1185.66.5 by Aaron Bentley
Renamed RevisionStorage to Repository
439
class Repository(object):
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
440
    """Repository holding history for one or more branches.
441
442
    The repository holds and retrieves historical information including
443
    revisions and file history.  It's normally accessed only by the Branch,
444
    which views a particular line of development through that history.
445
446
    The Repository builds on top of Stores and a Transport, which respectively 
447
    describe the disk data format and the way of accessing the (possibly 
448
    remote) disk.
449
    """
1185.65.17 by Robert Collins
Merge from integration, mode-changes are broken.
450
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
451
    # What class to use for a CommitBuilder. Often its simpler to change this
452
    # in a Repository class subclass rather than to override
453
    # get_commit_builder.
454
    _commit_builder_class = CommitBuilder
455
    # The search regex used by xml based repositories to determine what things
456
    # where changed in a single commit.
2163.2.1 by John Arbash Meinel
Speed up the fileids_altered_by_revision_ids processing
457
    _file_ids_altered_regex = lazy_regex.lazy_compile(
458
        r'file_id="(?P<file_id>[^"]+)"'
2776.4.6 by Robert Collins
Fixup various commit test failures falling out from the other commit changes.
459
        r'.* revision="(?P<revision_id>[^"]+)"'
2163.2.1 by John Arbash Meinel
Speed up the fileids_altered_by_revision_ids processing
460
        )
461
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
462
    def abort_write_group(self):
463
        """Commit the contents accrued within the current write group.
464
465
        :seealso: start_write_group.
466
        """
467
        if self._write_group is not self.get_transaction():
468
            # has an unlock or relock occured ?
469
            raise errors.BzrError('mismatched lock context and write group.')
470
        self._abort_write_group()
471
        self._write_group = None
472
473
    def _abort_write_group(self):
474
        """Template method for per-repository write group cleanup.
475
        
476
        This is called during abort before the write group is considered to be 
477
        finished and should cleanup any internal state accrued during the write
478
        group. There is no requirement that data handed to the repository be
479
        *not* made available - this is not a rollback - but neither should any
480
        attempt be made to ensure that data added is fully commited. Abort is
481
        invoked when an error has occured so futher disk or network operations
482
        may not be possible or may error and if possible should not be
483
        attempted.
484
        """
485
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
486
    @needs_write_lock
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
487
    def add_inventory(self, revision_id, inv, parents):
488
        """Add the inventory inv to the repository as revision_id.
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
489
        
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
490
        :param parents: The revision ids of the parents that revision_id
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
491
                        is known to have and are in the repository already.
492
493
        returns the sha1 of the serialized inventory.
494
        """
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
495
        _mod_revision.check_not_reserved_id(revision_id)
496
        assert inv.revision_id is None or inv.revision_id == revision_id, \
1740.2.2 by Aaron Bentley
Add test for the basis inventory automatically adding the revision id.
497
            "Mismatch between inventory revision" \
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
498
            " id and insertion revid (%r, %r)" % (inv.revision_id, revision_id)
1910.2.6 by Aaron Bentley
Update for merge review, handle deprecations
499
        assert inv.root is not None
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
500
        inv_lines = self._serialise_inventory_to_lines(inv)
501
        inv_vf = self.get_inventory_weave()
502
        return self._inventory_add_lines(inv_vf, revision_id, parents,
503
            inv_lines, check_content=False)
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
504
2805.6.7 by Robert Collins
Review feedback.
505
    def _inventory_add_lines(self, inv_vf, revision_id, parents, lines,
506
        check_content=True):
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
507
        """Store lines in inv_vf and return the sha1 of the inventory."""
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
508
        final_parents = []
509
        for parent in parents:
510
            if parent in inv_vf:
511
                final_parents.append(parent)
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
512
        return inv_vf.add_lines(revision_id, final_parents, lines,
513
            check_content=check_content)[0]
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
514
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
515
    @needs_write_lock
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
516
    def add_revision(self, revision_id, rev, inv=None, config=None):
517
        """Add rev to the revision store as revision_id.
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
518
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
519
        :param revision_id: the revision id to use.
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
520
        :param rev: The revision object.
521
        :param inv: The inventory for the revision. if None, it will be looked
522
                    up in the inventory storer
523
        :param config: If None no digital signature will be created.
524
                       If supplied its signature_needed method will be used
525
                       to determine if a signature should be made.
526
        """
2249.5.13 by John Arbash Meinel
Finish auditing Repository, and fix generate_ids to always generate utf8 ids.
527
        # TODO: jam 20070210 Shouldn't we check rev.revision_id and
528
        #       rev.parent_ids?
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
529
        _mod_revision.check_not_reserved_id(revision_id)
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
530
        if config is not None and config.signature_needed():
531
            if inv is None:
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
532
                inv = self.get_inventory(revision_id)
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
533
            plaintext = Testament(rev, inv).as_short_text()
534
            self.store_revision_signature(
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
535
                gpg.GPGStrategy(config), plaintext, revision_id)
536
        if not revision_id in self.get_inventory_weave():
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
537
            if inv is None:
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
538
                raise errors.WeaveRevisionNotPresent(revision_id,
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
539
                                                     self.get_inventory_weave())
540
            else:
541
                # yes, this is not suitable for adding with ghosts.
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
542
                self.add_inventory(revision_id, inv, rev.parent_ids)
1608.2.1 by Martin Pool
[merge] Storage filename escaping
543
        self._revision_store.add_revision(rev, self.get_transaction())
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
544
2520.4.10 by Aaron Bentley
Enable installation of revisions
545
    def _add_revision_text(self, revision_id, text):
546
        revision = self._revision_store._serializer.read_revision_from_string(
547
            text)
548
        self._revision_store._add_revision(revision, StringIO(text),
549
                                           self.get_transaction())
550
1732.2.4 by Martin Pool
Split check into Branch.check and Repository.check
551
    def all_revision_ids(self):
552
        """Returns a list of all the revision ids in the repository. 
553
554
        This is deprecated because code should generally work on the graph
555
        reachable from a particular revision, and ignore any other revisions
556
        that might be present.  There is no direct replacement method.
557
        """
2850.3.1 by Robert Collins
Move various weave specific code out of the base Repository class to weaverepo.py.
558
        if 'evil' in debug.debug_flags:
559
            mutter_callsite(2, "all_revision_ids is linear with history.")
1732.2.4 by Martin Pool
Split check into Branch.check and Repository.check
560
        return self._all_revision_ids()
561
562
    def _all_revision_ids(self):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
563
        """Returns a list of all the revision ids in the repository. 
564
565
        These are in as much topological order as the underlying store can 
2850.3.1 by Robert Collins
Move various weave specific code out of the base Repository class to weaverepo.py.
566
        present.
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
567
        """
2850.3.1 by Robert Collins
Move various weave specific code out of the base Repository class to weaverepo.py.
568
        raise NotImplementedError(self._all_revision_ids)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
569
1687.1.7 by Robert Collins
Teach Repository about break_lock.
570
    def break_lock(self):
571
        """Break a lock if one is present from another instance.
572
573
        Uses the ui factory to ask for confirmation if the lock may be from
574
        an active process.
575
        """
576
        self.control_files.break_lock()
577
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
578
    @needs_read_lock
579
    def _eliminate_revisions_not_present(self, revision_ids):
580
        """Check every revision id in revision_ids to see if we have it.
581
582
        Returns a set of the present revisions.
583
        """
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
584
        result = []
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
585
        for id in revision_ids:
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
586
            if self.has_revision(id):
587
               result.append(id)
588
        return result
589
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
590
    @staticmethod
591
    def create(a_bzrdir):
592
        """Construct the current default format repository in a_bzrdir."""
593
        return RepositoryFormat.get_default_format().initialize(a_bzrdir)
594
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
595
    def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
596
        """instantiate a Repository.
597
598
        :param _format: The format of the repository on disk.
599
        :param a_bzrdir: The BzrDir of the repository.
600
601
        In the future we will have a single api for all stores for
602
        getting file texts, inventories and revisions, then
603
        this construct will accept instances of those things.
604
        """
1608.2.1 by Martin Pool
[merge] Storage filename escaping
605
        super(Repository, self).__init__()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
606
        self._format = _format
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
607
        # the following are part of the public API for Repository:
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
608
        self.bzrdir = a_bzrdir
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
609
        self.control_files = control_files
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
610
        self._revision_store = _revision_store
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
611
        # backwards compatibility
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
612
        self.weave_store = text_store
2671.4.2 by Robert Collins
Review feedback.
613
        # for tests
614
        self._reconcile_does_inventory_gc = True
2745.6.16 by Aaron Bentley
Update from review
615
        self._reconcile_fixes_text_parents = False
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
616
        # not right yet - should be more semantically clear ? 
617
        # 
618
        self.control_store = control_store
619
        self.control_weaves = control_store
1608.2.1 by Martin Pool
[merge] Storage filename escaping
620
        # TODO: make sure to construct the right store classes, etc, depending
621
        # on whether escaping is required.
1904.2.3 by Martin Pool
Give a warning on access to old repository formats
622
        self._warn_if_deprecated()
2617.6.1 by Robert Collins
* New method on Repository - ``start_write_group``, ``end_write_group``
623
        self._write_group = None
2881.4.1 by Robert Collins
Move responsibility for detecting same-repo fetching from the
624
        self.base = control_files._transport.base
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
625
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
626
    def __repr__(self):
2881.4.1 by Robert Collins
Move responsibility for detecting same-repo fetching from the
627
        return '%s(%r)' % (self.__class__.__name__,
628
                           self.base)
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
629
2671.1.4 by Andrew Bennetts
Rename is_same_repository to has_same_location, thanks Aaron!
630
    def has_same_location(self, other):
2671.1.3 by Andrew Bennetts
Remove Repository.__eq__/__ne__ methods, replace with is_same_repository method.
631
        """Returns a boolean indicating if this repository is at the same
632
        location as another repository.
633
634
        This might return False even when two repository objects are accessing
635
        the same physical repository via different URLs.
636
        """
2671.1.1 by Andrew Bennetts
Add support for comparing Repositories with == and != operators.
637
        if self.__class__ is not other.__class__:
638
            return False
639
        return (self.control_files._transport.base ==
640
                other.control_files._transport.base)
641
2617.6.1 by Robert Collins
* New method on Repository - ``start_write_group``, ``end_write_group``
642
    def is_in_write_group(self):
643
        """Return True if there is an open write group.
644
645
        :seealso: start_write_group.
646
        """
647
        return self._write_group is not None
648
1694.2.6 by Martin Pool
[merge] bzr.dev
649
    def is_locked(self):
650
        return self.control_files.is_locked()
651
2904.1.1 by Robert Collins
* New method ``bzrlib.repository.Repository.is_write_locked`` useful for
652
    def is_write_locked(self):
653
        """Return True if this object is write locked."""
654
        return self.is_locked() and self.control_files._lock_mode == 'w'
655
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
656
    def lock_write(self, token=None):
657
        """Lock this repository for writing.
2617.6.8 by Robert Collins
Review feedback and documentation.
658
659
        This causes caching within the repository obejct to start accumlating
660
        data during reads, and allows a 'write_group' to be obtained. Write
661
        groups must be used for actual data insertion.
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
662
        
663
        :param token: if this is already locked, then lock_write will fail
664
            unless the token matches the existing lock.
665
        :returns: a token if this instance supports tokens, otherwise None.
666
        :raises TokenLockingNotSupported: when a token is given but this
667
            instance doesn't support using token locks.
668
        :raises MismatchedToken: if the specified token doesn't match the token
669
            of the existing lock.
2617.6.8 by Robert Collins
Review feedback and documentation.
670
        :seealso: start_write_group.
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
671
2018.5.145 by Andrew Bennetts
Add a brief explanation of what tokens are used for to lock_write docstrings.
672
        A token should be passed in if you know that you have locked the object
673
        some other way, and need to synchronise this object's state with that
674
        fact.
675
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
676
        XXX: this docstring is duplicated in many places, e.g. lockable_files.py
677
        """
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
678
        result = self.control_files.lock_write(token=token)
679
        self._refresh_data()
680
        return result
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
681
682
    def lock_read(self):
1553.5.55 by Martin Pool
[revert] broken changes
683
        self.control_files.lock_read()
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
684
        self._refresh_data()
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
685
1694.2.6 by Martin Pool
[merge] bzr.dev
686
    def get_physical_lock_status(self):
687
        return self.control_files.get_physical_lock_status()
1624.3.36 by Olaf Conradi
Rename is_transport_locked() to get_physical_lock_status() as the
688
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
689
    def leave_lock_in_place(self):
690
        """Tell this repository not to release the physical lock when this
691
        object is unlocked.
2018.5.76 by Andrew Bennetts
Testing that repository.{dont_,}leave_lock_in_place raises NotImplementedError if lock_write returns None.
692
        
693
        If lock_write doesn't return a token, then this method is not supported.
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
694
        """
695
        self.control_files.leave_in_place()
696
697
    def dont_leave_lock_in_place(self):
698
        """Tell this repository to release the physical lock when this
699
        object is unlocked, even if it didn't originally acquire it.
2018.5.76 by Andrew Bennetts
Testing that repository.{dont_,}leave_lock_in_place raises NotImplementedError if lock_write returns None.
700
701
        If lock_write doesn't return a token, then this method is not supported.
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
702
        """
703
        self.control_files.dont_leave_in_place()
704
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
705
    @needs_read_lock
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
706
    def gather_stats(self, revid=None, committers=None):
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
707
        """Gather statistics from a revision id.
708
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
709
        :param revid: The revision id to gather statistics from, if None, then
710
            no revision specific statistics are gathered.
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
711
        :param committers: Optional parameter controlling whether to grab
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
712
            a count of committers from the revision specific statistics.
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
713
        :return: A dictionary of statistics. Currently this contains:
714
            committers: The number of committers if requested.
715
            firstrev: A tuple with timestamp, timezone for the penultimate left
716
                most ancestor of revid, if revid is not the NULL_REVISION.
717
            latestrev: A tuple with timestamp, timezone for revid, if revid is
718
                not the NULL_REVISION.
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
719
            revisions: The total revision count in the repository.
720
            size: An estimate disk size of the repository in bytes.
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
721
        """
722
        result = {}
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
723
        if revid and committers:
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
724
            result['committers'] = 0
2258.1.2 by Robert Collins
New version of gather_stats which gathers aggregate data too.
725
        if revid and revid != _mod_revision.NULL_REVISION:
726
            if committers:
727
                all_committers = set()
728
            revisions = self.get_ancestry(revid)
729
            # pop the leading None
730
            revisions.pop(0)
731
            first_revision = None
732
            if not committers:
733
                # ignore the revisions in the middle - just grab first and last
734
                revisions = revisions[0], revisions[-1]
735
            for revision in self.get_revisions(revisions):
736
                if not first_revision:
737
                    first_revision = revision
738
                if committers:
739
                    all_committers.add(revision.committer)
740
            last_revision = revision
741
            if committers:
742
                result['committers'] = len(all_committers)
743
            result['firstrev'] = (first_revision.timestamp,
744
                first_revision.timezone)
745
            result['latestrev'] = (last_revision.timestamp,
746
                last_revision.timezone)
747
748
        # now gather global repository information
749
        if self.bzrdir.root_transport.listable():
750
            c, t = self._revision_store.total_size(self.get_transaction())
751
            result['revisions'] = c
752
            result['size'] = t
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
753
        return result
754
2535.3.12 by Andrew Bennetts
Add a first cut of a get_data_stream method to Repository.
755
    def get_data_stream(self, revision_ids):
756
        raise NotImplementedError(self.get_data_stream)
757
2535.3.17 by Andrew Bennetts
[broken] Closer to a working Repository.fetch_revisions smart request.
758
    def insert_data_stream(self, stream):
2535.3.50 by Andrew Bennetts
Use tuple names in data streams rather than concatenated strings.
759
        for item_key, bytes in stream:
760
            if item_key[0] == 'file':
761
                (file_id,) = item_key[1:]
2535.3.27 by Andrew Bennetts
Merge from bzr.dev.
762
                knit = self.weave_store.get_weave_or_empty(
2535.3.17 by Andrew Bennetts
[broken] Closer to a working Repository.fetch_revisions smart request.
763
                    file_id, self.get_transaction())
2535.3.50 by Andrew Bennetts
Use tuple names in data streams rather than concatenated strings.
764
            elif item_key == ('inventory',):
2535.3.17 by Andrew Bennetts
[broken] Closer to a working Repository.fetch_revisions smart request.
765
                knit = self.get_inventory_weave()
2535.3.50 by Andrew Bennetts
Use tuple names in data streams rather than concatenated strings.
766
            elif item_key == ('revisions',):
2535.3.28 by Andrew Bennetts
Use self._revision_store.get_revision_file rather that self.control_store.get_weave('revisions', ...)
767
                knit = self._revision_store.get_revision_file(
768
                    self.get_transaction())
2535.3.50 by Andrew Bennetts
Use tuple names in data streams rather than concatenated strings.
769
            elif item_key == ('signatures',):
2535.3.25 by Andrew Bennetts
Fetch signatures too.
770
                knit = self._revision_store.get_signature_file(
771
                    self.get_transaction())
2535.3.17 by Andrew Bennetts
[broken] Closer to a working Repository.fetch_revisions smart request.
772
            else:
2535.3.40 by Andrew Bennetts
Tidy up more XXXs.
773
                raise RepositoryDataStreamError(
2535.3.50 by Andrew Bennetts
Use tuple names in data streams rather than concatenated strings.
774
                    "Unrecognised data stream key '%s'" % (item_key,))
2535.3.17 by Andrew Bennetts
[broken] Closer to a working Repository.fetch_revisions smart request.
775
            decoded_list = bencode.bdecode(bytes)
776
            format = decoded_list.pop(0)
777
            data_list = []
778
            knit_bytes = ''
779
            for version, options, parents, some_bytes in decoded_list:
780
                data_list.append((version, options, len(some_bytes), parents))
781
                knit_bytes += some_bytes
2535.3.40 by Andrew Bennetts
Tidy up more XXXs.
782
            knit.insert_data_stream(
783
                (format, data_list, StringIO(knit_bytes).read))
2535.3.17 by Andrew Bennetts
[broken] Closer to a working Repository.fetch_revisions smart request.
784
2258.1.1 by Robert Collins
Move info branch statistics gathering into the repository to allow smart server optimisation (Robert Collins).
785
    @needs_read_lock
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
786
    def missing_revision_ids(self, other, revision_id=None):
787
        """Return the revision ids that other has that this does not.
788
        
789
        These are returned in topological order.
790
791
        revision_id: only return revision ids included by revision_id.
792
        """
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
793
        return InterRepository.get(other, self).missing_revision_ids(revision_id)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
794
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
795
    @staticmethod
796
    def open(base):
797
        """Open the repository rooted at base.
798
799
        For instance, if the repository is at URL/.bzr/repository,
800
        Repository.open(URL) -> a Repository instance.
801
        """
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
802
        control = bzrdir.BzrDir.open(base)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
803
        return control.open_repository()
804
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
805
    def copy_content_into(self, destination, revision_id=None):
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
806
        """Make a complete copy of the content in self into destination.
807
        
808
        This is a destructive operation! Do not use it on existing 
809
        repositories.
810
        """
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
811
        return InterRepository.get(self, destination).copy_content(revision_id)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
812
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
813
    def commit_write_group(self):
814
        """Commit the contents accrued within the current write group.
2617.6.1 by Robert Collins
* New method on Repository - ``start_write_group``, ``end_write_group``
815
816
        :seealso: start_write_group.
817
        """
818
        if self._write_group is not self.get_transaction():
819
            # has an unlock or relock occured ?
2881.4.1 by Robert Collins
Move responsibility for detecting same-repo fetching from the
820
            raise errors.BzrError('mismatched lock context %r and '
821
                'write group %r.' %
822
                (self.get_transaction(), self._write_group))
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
823
        self._commit_write_group()
2617.6.1 by Robert Collins
* New method on Repository - ``start_write_group``, ``end_write_group``
824
        self._write_group = None
825
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
826
    def _commit_write_group(self):
827
        """Template method for per-repository write group cleanup.
828
        
829
        This is called before the write group is considered to be 
830
        finished and should ensure that all data handed to the repository
831
        for writing during the write group is safely committed (to the 
832
        extent possible considering file system caching etc).
833
        """
834
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
835
    def fetch(self, source, revision_id=None, pb=None):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
836
        """Fetch the content required to construct revision_id from source.
837
838
        If revision_id is None all content is copied.
839
        """
2881.4.1 by Robert Collins
Move responsibility for detecting same-repo fetching from the
840
        # fast path same-url fetch operations
841
        if self.has_same_location(source):
842
            # check that last_revision is in 'from' and then return a
843
            # no-operation.
844
            if (revision_id is not None and
845
                not _mod_revision.is_null(revision_id)):
846
                self.get_revision(revision_id)
847
            return 0, []
2323.8.3 by Aaron Bentley
Reduce scope of try/except, update NEWS
848
        inter = InterRepository.get(source, self)
2323.8.2 by Aaron Bentley
Give a nicer error on fetch when repos are in incompatible formats
849
        try:
2323.8.3 by Aaron Bentley
Reduce scope of try/except, update NEWS
850
            return inter.fetch(revision_id=revision_id, pb=pb)
2323.8.2 by Aaron Bentley
Give a nicer error on fetch when repos are in incompatible formats
851
        except NotImplementedError:
852
            raise errors.IncompatibleRepositories(source, self)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
853
2520.4.54 by Aaron Bentley
Hang a create_bundle method off repository
854
    def create_bundle(self, target, base, fileobj, format=None):
855
        return serializer.write_bundle(self, target, base, fileobj, format)
856
2803.2.1 by Robert Collins
* CommitBuilder now advertises itself as requiring the root entry to be
857
    def get_commit_builder(self, branch, parents, config, timestamp=None,
858
                           timezone=None, committer=None, revprops=None,
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
859
                           revision_id=None):
860
        """Obtain a CommitBuilder for this repository.
861
        
862
        :param branch: Branch to commit to.
863
        :param parents: Revision ids of the parents of the new revision.
864
        :param config: Configuration to use.
865
        :param timestamp: Optional timestamp recorded for commit.
866
        :param timezone: Optional timezone for timestamp.
867
        :param committer: Optional committer to set for commit.
868
        :param revprops: Optional dictionary of revision properties.
869
        :param revision_id: Optional revision id.
870
        """
2818.3.2 by Robert Collins
Review feedback.
871
        result = self._commit_builder_class(self, parents, config,
2818.3.1 by Robert Collins
Change CommitBuilder factory delegation to allow simple declaration.
872
            timestamp, timezone, committer, revprops, revision_id)
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
873
        self.start_write_group()
874
        return result
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
875
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
876
    def unlock(self):
2617.6.1 by Robert Collins
* New method on Repository - ``start_write_group``, ``end_write_group``
877
        if (self.control_files._lock_count == 1 and
878
            self.control_files._lock_mode == 'w'):
879
            if self._write_group is not None:
880
                raise errors.BzrError(
881
                    'Must end write groups before releasing write locks.')
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
882
        self.control_files.unlock()
883
1185.65.27 by Robert Collins
Tweak storage towards mergability.
884
    @needs_read_lock
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
885
    def clone(self, a_bzrdir, revision_id=None):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
886
        """Clone this repository into a_bzrdir using the current format.
887
888
        Currently no check is made that the format of this repository and
889
        the bzrdir format are compatible. FIXME RBC 20060201.
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
890
891
        :return: The newly created destination repository.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
892
        """
2440.1.1 by Martin Pool
Add new Repository.sprout,
893
        # TODO: deprecate after 0.16; cloning this with all its settings is
894
        # probably not very useful -- mbp 20070423
895
        dest_repo = self._create_sprouting_repo(a_bzrdir, shared=self.is_shared())
896
        self.copy_content_into(dest_repo, revision_id)
897
        return dest_repo
898
2617.6.1 by Robert Collins
* New method on Repository - ``start_write_group``, ``end_write_group``
899
    def start_write_group(self):
900
        """Start a write group in the repository.
901
902
        Write groups are used by repositories which do not have a 1:1 mapping
903
        between file ids and backend store to manage the insertion of data from
904
        both fetch and commit operations.
905
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
906
        A write lock is required around the start_write_group/commit_write_group
2617.6.1 by Robert Collins
* New method on Repository - ``start_write_group``, ``end_write_group``
907
        for the support of lock-requiring repository formats.
2617.6.8 by Robert Collins
Review feedback and documentation.
908
909
        One can only insert data into a repository inside a write group.
910
2617.6.6 by Robert Collins
Some review feedback.
911
        :return: None.
2617.6.1 by Robert Collins
* New method on Repository - ``start_write_group``, ``end_write_group``
912
        """
2904.1.1 by Robert Collins
* New method ``bzrlib.repository.Repository.is_write_locked`` useful for
913
        if not self.is_write_locked():
2617.6.1 by Robert Collins
* New method on Repository - ``start_write_group``, ``end_write_group``
914
            raise errors.NotWriteLocked(self)
915
        if self._write_group:
916
            raise errors.BzrError('already in a write group')
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
917
        self._start_write_group()
918
        # so we can detect unlock/relock - the write group is now entered.
2617.6.1 by Robert Collins
* New method on Repository - ``start_write_group``, ``end_write_group``
919
        self._write_group = self.get_transaction()
920
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
921
    def _start_write_group(self):
922
        """Template method for per-repository write group startup.
923
        
924
        This is called before the write group is considered to be 
925
        entered.
926
        """
927
2440.1.1 by Martin Pool
Add new Repository.sprout,
928
    @needs_read_lock
929
    def sprout(self, to_bzrdir, revision_id=None):
930
        """Create a descendent repository for new development.
931
932
        Unlike clone, this does not copy the settings of the repository.
933
        """
934
        dest_repo = self._create_sprouting_repo(to_bzrdir, shared=False)
935
        dest_repo.fetch(self, revision_id=revision_id)
936
        return dest_repo
937
938
    def _create_sprouting_repo(self, a_bzrdir, shared):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
939
        if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
940
            # use target default format.
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
941
            dest_repo = a_bzrdir.create_repository()
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
942
        else:
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
943
            # Most control formats need the repository to be specifically
944
            # created, but on some old all-in-one formats it's not needed
945
            try:
2440.1.1 by Martin Pool
Add new Repository.sprout,
946
                dest_repo = self._format.initialize(a_bzrdir, shared=shared)
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
947
            except errors.UninitializableFormat:
948
                dest_repo = a_bzrdir.open_repository()
949
        return dest_repo
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
950
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
951
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
952
    def has_revision(self, revision_id):
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
953
        """True if this repository has a copy of the revision."""
2745.1.1 by Robert Collins
Add a number of -Devil checkpoints.
954
        if 'evil' in debug.debug_flags:
2881.4.1 by Robert Collins
Move responsibility for detecting same-repo fetching from the
955
            mutter_callsite(3, "has_revision is a LBYL symptom.")
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
956
        return self._revision_store.has_revision_id(revision_id,
957
                                                    self.get_transaction())
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
958
1185.65.27 by Robert Collins
Tweak storage towards mergability.
959
    @needs_read_lock
2850.3.1 by Robert Collins
Move various weave specific code out of the base Repository class to weaverepo.py.
960
    def get_revision(self, revision_id):
961
        """Return the Revision object for a named revision."""
962
        return self.get_revisions([revision_id])[0]
963
964
    @needs_read_lock
1570.1.13 by Robert Collins
Check for incorrect revision parentage in the weave during revision access.
965
    def get_revision_reconcile(self, revision_id):
966
        """'reconcile' helper routine that allows access to a revision always.
967
        
968
        This variant of get_revision does not cross check the weave graph
969
        against the revision one as get_revision does: but it should only
970
        be used by reconcile, or reconcile-alike commands that are correcting
971
        or testing the revision graph.
972
        """
2850.3.1 by Robert Collins
Move various weave specific code out of the base Repository class to weaverepo.py.
973
        return self._get_revisions([revision_id])[0]
2249.5.13 by John Arbash Meinel
Finish auditing Repository, and fix generate_ids to always generate utf8 ids.
974
1756.1.2 by Aaron Bentley
Show logs using get_revisions
975
    @needs_read_lock
976
    def get_revisions(self, revision_ids):
2850.3.1 by Robert Collins
Move various weave specific code out of the base Repository class to weaverepo.py.
977
        """Get many revisions at once."""
978
        return self._get_revisions(revision_ids)
979
980
    @needs_read_lock
981
    def _get_revisions(self, revision_ids):
982
        """Core work logic to get many revisions without sanity checks."""
983
        for rev_id in revision_ids:
984
            if not rev_id or not isinstance(rev_id, basestring):
985
                raise errors.InvalidRevisionId(revision_id=rev_id, branch=self)
2249.5.13 by John Arbash Meinel
Finish auditing Repository, and fix generate_ids to always generate utf8 ids.
986
        revs = self._revision_store.get_revisions(revision_ids,
1756.1.2 by Aaron Bentley
Show logs using get_revisions
987
                                                  self.get_transaction())
2249.5.13 by John Arbash Meinel
Finish auditing Repository, and fix generate_ids to always generate utf8 ids.
988
        for rev in revs:
989
            assert not isinstance(rev.revision_id, unicode)
990
            for parent_id in rev.parent_ids:
991
                assert not isinstance(parent_id, unicode)
992
        return revs
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
993
1185.65.27 by Robert Collins
Tweak storage towards mergability.
994
    @needs_read_lock
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
995
    def get_revision_xml(self, revision_id):
2249.5.13 by John Arbash Meinel
Finish auditing Repository, and fix generate_ids to always generate utf8 ids.
996
        # TODO: jam 20070210 This shouldn't be necessary since get_revision
997
        #       would have already do it.
998
        # TODO: jam 20070210 Just use _serializer.write_revision_to_string()
999
        rev = self.get_revision(revision_id)
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1000
        rev_tmp = StringIO()
1001
        # the current serializer..
1002
        self._revision_store._serializer.write_revision(rev, rev_tmp)
1003
        rev_tmp.seek(0)
1004
        return rev_tmp.getvalue()
1005
1006
    @needs_read_lock
1756.3.22 by Aaron Bentley
Tweaks from review
1007
    def get_deltas_for_revisions(self, revisions):
1756.3.19 by Aaron Bentley
Documentation and cleanups
1008
        """Produce a generator of revision deltas.
1009
        
1010
        Note that the input is a sequence of REVISIONS, not revision_ids.
1011
        Trees will be held in memory until the generator exits.
1012
        Each delta is relative to the revision's lefthand predecessor.
1013
        """
1756.3.3 by Aaron Bentley
More refactoring, introduce revision_trees.
1014
        required_trees = set()
1015
        for revision in revisions:
1016
            required_trees.add(revision.revision_id)
1017
            required_trees.update(revision.parent_ids[:1])
1018
        trees = dict((t.get_revision_id(), t) for 
1019
                     t in self.revision_trees(required_trees))
1020
        for revision in revisions:
1021
            if not revision.parent_ids:
1852.5.1 by Robert Collins
Deprecate EmptyTree in favour of using Repository.revision_tree.
1022
                old_tree = self.revision_tree(None)
1756.3.3 by Aaron Bentley
More refactoring, introduce revision_trees.
1023
            else:
1024
                old_tree = trees[revision.parent_ids[0]]
1852.10.3 by Robert Collins
Remove all uses of compare_trees and replace with Tree.changes_from throughout bzrlib.
1025
            yield trees[revision.revision_id].changes_from(old_tree)
1756.3.3 by Aaron Bentley
More refactoring, introduce revision_trees.
1026
1756.3.19 by Aaron Bentley
Documentation and cleanups
1027
    @needs_read_lock
1744.2.2 by Johan Rydberg
Add get_revision_delta to Repository; and make Branch.get_revision_delta use it.
1028
    def get_revision_delta(self, revision_id):
1029
        """Return the delta for one revision.
1030
1031
        The delta is relative to the left-hand predecessor of the
1032
        revision.
1033
        """
1756.3.3 by Aaron Bentley
More refactoring, introduce revision_trees.
1034
        r = self.get_revision(revision_id)
1756.3.22 by Aaron Bentley
Tweaks from review
1035
        return list(self.get_deltas_for_revisions([r]))[0]
1744.2.2 by Johan Rydberg
Add get_revision_delta to Repository; and make Branch.get_revision_delta use it.
1036
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1037
    @needs_write_lock
1038
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1039
        signature = gpg_strategy.sign(plaintext)
1040
        self._revision_store.add_revision_signature_text(revision_id,
1041
                                                         signature,
1042
                                                         self.get_transaction())
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1043
1694.2.6 by Martin Pool
[merge] bzr.dev
1044
    def fileids_altered_by_revision_ids(self, revision_ids):
1045
        """Find the file ids and versions affected by revisions.
1046
1047
        :param revisions: an iterable containing revision ids.
1048
        :return: a dictionary mapping altered file-ids to an iterable of
1049
        revision_ids. Each altered file-ids has the exact revision_ids that
1050
        altered it listed explicitly.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1051
        """
1910.2.48 by Aaron Bentley
Update from review comments
1052
        assert self._serializer.support_altered_by_hack, \
1732.2.1 by Martin Pool
Remove obsolete fileid_involved from KnitRepository, fix error message.
1053
            ("fileids_altered_by_revision_ids only supported for branches " 
1054
             "which store inventory as unnested xml, not on %r" % self)
2858.2.1 by Martin Pool
Remove most calls to safe_file_id and safe_revision_id.
1055
        selected_revision_ids = set(revision_ids)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1056
        w = self.get_inventory_weave()
1694.2.6 by Martin Pool
[merge] bzr.dev
1057
        result = {}
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
1058
1694.2.6 by Martin Pool
[merge] bzr.dev
1059
        # this code needs to read every new line in every inventory for the
1060
        # inventories [revision_ids]. Seeing a line twice is ok. Seeing a line
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1061
        # not present in one of those inventories is unnecessary but not 
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
1062
        # harmful because we are filtering by the revision id marker in the
1694.2.6 by Martin Pool
[merge] bzr.dev
1063
        # inventory lines : we only select file ids altered in one of those  
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
1064
        # revisions. We don't need to see all lines in the inventory because
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
1065
        # only those added in an inventory in rev X can contain a revision=X
1066
        # line.
2163.2.3 by John Arbash Meinel
Change to local variables to save another 300ms
1067
        unescape_revid_cache = {}
1068
        unescape_fileid_cache = {}
1069
2163.2.5 by John Arbash Meinel
Inline the cache lookup, and explain why
1070
        # jam 20061218 In a big fetch, this handles hundreds of thousands
1071
        # of lines, so it has had a lot of inlining and optimizing done.
1072
        # Sorry that it is a little bit messy.
2163.2.3 by John Arbash Meinel
Change to local variables to save another 300ms
1073
        # Move several functions to be local variables, since this is a long
1074
        # running loop.
1075
        search = self._file_ids_altered_regex.search
2163.2.5 by John Arbash Meinel
Inline the cache lookup, and explain why
1076
        unescape = _unescape_xml
2163.2.3 by John Arbash Meinel
Change to local variables to save another 300ms
1077
        setdefault = result.setdefault
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
1078
        pb = ui.ui_factory.nested_progress_bar()
1079
        try:
1080
            for line in w.iter_lines_added_or_present_in_versions(
2163.2.3 by John Arbash Meinel
Change to local variables to save another 300ms
1081
                                        selected_revision_ids, pb=pb):
1082
                match = search(line)
2163.2.1 by John Arbash Meinel
Speed up the fileids_altered_by_revision_ids processing
1083
                if match is None:
1084
                    continue
2163.2.5 by John Arbash Meinel
Inline the cache lookup, and explain why
1085
                # One call to match.group() returning multiple items is quite a
1086
                # bit faster than 2 calls to match.group() each returning 1
2163.2.1 by John Arbash Meinel
Speed up the fileids_altered_by_revision_ids processing
1087
                file_id, revision_id = match.group('file_id', 'revision_id')
2163.2.5 by John Arbash Meinel
Inline the cache lookup, and explain why
1088
1089
                # Inlining the cache lookups helps a lot when you make 170,000
1090
                # lines and 350k ids, versus 8.4 unique ids.
1091
                # Using a cache helps in 2 ways:
1092
                #   1) Avoids unnecessary decoding calls
1093
                #   2) Re-uses cached strings, which helps in future set and
1094
                #      equality checks.
1095
                # (2) is enough that removing encoding entirely along with
1096
                # the cache (so we are using plain strings) results in no
1097
                # performance improvement.
1098
                try:
1099
                    revision_id = unescape_revid_cache[revision_id]
1100
                except KeyError:
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
1101
                    unescaped = unescape(revision_id)
2163.2.5 by John Arbash Meinel
Inline the cache lookup, and explain why
1102
                    unescape_revid_cache[revision_id] = unescaped
1103
                    revision_id = unescaped
1104
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
1105
                if revision_id in selected_revision_ids:
2163.2.5 by John Arbash Meinel
Inline the cache lookup, and explain why
1106
                    try:
1107
                        file_id = unescape_fileid_cache[file_id]
1108
                    except KeyError:
1109
                        unescaped = unescape(file_id)
1110
                        unescape_fileid_cache[file_id] = unescaped
1111
                        file_id = unescaped
2163.2.3 by John Arbash Meinel
Change to local variables to save another 300ms
1112
                    setdefault(file_id, set()).add(revision_id)
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
1113
        finally:
1114
            pb.finished()
1694.2.6 by Martin Pool
[merge] bzr.dev
1115
        return result
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1116
2708.1.7 by Aaron Bentley
Rename extract_files_bytes to iter_files_bytes
1117
    def iter_files_bytes(self, desired_files):
2708.1.9 by Aaron Bentley
Clean-up docs and imports
1118
        """Iterate through file versions.
1119
2708.1.10 by Aaron Bentley
Update docstrings
1120
        Files will not necessarily be returned in the order they occur in
1121
        desired_files.  No specific order is guaranteed.
1122
2708.1.9 by Aaron Bentley
Clean-up docs and imports
1123
        Yields pairs of identifier, bytes_iterator.  identifier is an opaque
2708.1.10 by Aaron Bentley
Update docstrings
1124
        value supplied by the caller as part of desired_files.  It should
1125
        uniquely identify the file version in the caller's context.  (Examples:
1126
        an index number or a TreeTransform trans_id.)
1127
1128
        bytes_iterator is an iterable of bytestrings for the file.  The
1129
        kind of iterable and length of the bytestrings are unspecified, but for
1130
        this implementation, it is a list of lines produced by
1131
        VersionedFile.get_lines().
1132
2708.1.9 by Aaron Bentley
Clean-up docs and imports
1133
        :param desired_files: a list of (file_id, revision_id, identifier)
2708.1.10 by Aaron Bentley
Update docstrings
1134
            triples
2708.1.9 by Aaron Bentley
Clean-up docs and imports
1135
        """
2708.1.3 by Aaron Bentley
Implement extract_files_bytes on Repository
1136
        transaction = self.get_transaction()
1137
        for file_id, revision_id, callable_data in desired_files:
2708.1.11 by Aaron Bentley
Test and tweak error handling
1138
            try:
1139
                weave = self.weave_store.get_weave(file_id, transaction)
1140
            except errors.NoSuchFile:
1141
                raise errors.NoSuchIdInRepository(self, file_id)
2708.1.6 by Aaron Bentley
Turn extract_files_bytes into an iterator
1142
            yield callable_data, weave.get_lines(revision_id)
2708.1.3 by Aaron Bentley
Implement extract_files_bytes on Repository
1143
2668.2.8 by Andrew Bennetts
Rename get_data_to_fetch_for_revision_ids as item_keys_introduced_by.
1144
    def item_keys_introduced_by(self, revision_ids, _files_pb=None):
1145
        """Get an iterable listing the keys of all the data introduced by a set
1146
        of revision IDs.
1147
1148
        The keys will be ordered so that the corresponding items can be safely
1149
        fetched and inserted in that order.
1150
1151
        :returns: An iterable producing tuples of (knit-kind, file-id,
1152
            versions).  knit-kind is one of 'file', 'inventory', 'signatures',
1153
            'revisions'.  file-id is None unless knit-kind is 'file'.
2535.3.6 by Andrew Bennetts
Move some "what repo data to fetch logic" from RepoFetcher to Repository.
1154
        """
1155
        # XXX: it's a bit weird to control the inventory weave caching in this
2535.3.7 by Andrew Bennetts
Remove now unused _fetch_weave_texts, make progress reporting closer to how it was before I refactored __fetch.
1156
        # generator.  Ideally the caching would be done in fetch.py I think.  Or
1157
        # maybe this generator should explicitly have the contract that it
1158
        # should not be iterated until the previously yielded item has been
1159
        # processed?
2535.3.47 by Andrew Bennetts
Massively improve item_keys_introduced_by performance by calling self.lock_read().
1160
        self.lock_read()
2535.3.6 by Andrew Bennetts
Move some "what repo data to fetch logic" from RepoFetcher to Repository.
1161
        inv_w = self.get_inventory_weave()
1162
        inv_w.enable_cache()
1163
1164
        # file ids that changed
1165
        file_ids = self.fileids_altered_by_revision_ids(revision_ids)
2535.3.8 by Andrew Bennetts
Unbreak progress reporting.
1166
        count = 0
1167
        num_file_ids = len(file_ids)
2535.3.6 by Andrew Bennetts
Move some "what repo data to fetch logic" from RepoFetcher to Repository.
1168
        for file_id, altered_versions in file_ids.iteritems():
2668.2.8 by Andrew Bennetts
Rename get_data_to_fetch_for_revision_ids as item_keys_introduced_by.
1169
            if _files_pb is not None:
1170
                _files_pb.update("fetch texts", count, num_file_ids)
2535.3.8 by Andrew Bennetts
Unbreak progress reporting.
1171
            count += 1
2535.3.6 by Andrew Bennetts
Move some "what repo data to fetch logic" from RepoFetcher to Repository.
1172
            yield ("file", file_id, altered_versions)
2535.3.9 by Andrew Bennetts
More comments.
1173
        # We're done with the files_pb.  Note that it finished by the caller,
1174
        # just as it was created by the caller.
2668.2.8 by Andrew Bennetts
Rename get_data_to_fetch_for_revision_ids as item_keys_introduced_by.
1175
        del _files_pb
2535.3.6 by Andrew Bennetts
Move some "what repo data to fetch logic" from RepoFetcher to Repository.
1176
1177
        # inventory
1178
        yield ("inventory", None, revision_ids)
1179
        inv_w.clear_cache()
1180
1181
        # signatures
2535.3.25 by Andrew Bennetts
Fetch signatures too.
1182
        revisions_with_signatures = set()
1183
        for rev_id in revision_ids:
1184
            try:
1185
                self.get_signature_text(rev_id)
1186
            except errors.NoSuchRevision:
1187
                # not signed.
1188
                pass
1189
            else:
1190
                revisions_with_signatures.add(rev_id)
2535.3.47 by Andrew Bennetts
Massively improve item_keys_introduced_by performance by calling self.lock_read().
1191
        self.unlock()
2535.3.25 by Andrew Bennetts
Fetch signatures too.
1192
        yield ("signatures", None, revisions_with_signatures)
2535.3.6 by Andrew Bennetts
Move some "what repo data to fetch logic" from RepoFetcher to Repository.
1193
1194
        # revisions
1195
        yield ("revisions", None, revision_ids)
1196
1185.65.27 by Robert Collins
Tweak storage towards mergability.
1197
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1198
    def get_inventory_weave(self):
1199
        return self.control_weaves.get_weave('inventory',
1200
            self.get_transaction())
1201
1185.65.27 by Robert Collins
Tweak storage towards mergability.
1202
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1203
    def get_inventory(self, revision_id):
1204
        """Get Inventory object by hash."""
1740.2.3 by Aaron Bentley
Only reserialize the working tree basis inventory when needed.
1205
        return self.deserialise_inventory(
1206
            revision_id, self.get_inventory_xml(revision_id))
1207
1208
    def deserialise_inventory(self, revision_id, xml):
1209
        """Transform the xml into an inventory object. 
1210
1211
        :param revision_id: The expected revision id of the inventory.
1212
        :param xml: A serialised inventory.
1213
        """
2889.1.3 by Robert Collins
Merge bzr.dev.
1214
        return self._serializer.read_inventory_from_string(xml, revision_id)
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1215
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
1216
    def serialise_inventory(self, inv):
1910.2.48 by Aaron Bentley
Update from review comments
1217
        return self._serializer.write_inventory_to_string(inv)
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
1218
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
1219
    def _serialise_inventory_to_lines(self, inv):
1220
        return self._serializer.write_inventory_to_lines(inv)
1221
2520.4.113 by Aaron Bentley
Avoid peeking at Repository._serializer
1222
    def get_serializer_format(self):
1223
        return self._serializer.format_num
1224
1185.65.27 by Robert Collins
Tweak storage towards mergability.
1225
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1226
    def get_inventory_xml(self, revision_id):
1227
        """Get inventory XML as a file object."""
1228
        try:
2249.5.13 by John Arbash Meinel
Finish auditing Repository, and fix generate_ids to always generate utf8 ids.
1229
            assert isinstance(revision_id, str), type(revision_id)
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1230
            iw = self.get_inventory_weave()
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
1231
            return iw.get_text(revision_id)
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1232
        except IndexError:
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
1233
            raise errors.HistoryMissing(self, 'inventory', revision_id)
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1234
1185.65.27 by Robert Collins
Tweak storage towards mergability.
1235
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1236
    def get_inventory_sha1(self, revision_id):
1237
        """Return the sha1 hash of the inventory entry
1238
        """
1239
        return self.get_revision(revision_id).inventory_sha1
1240
1185.65.27 by Robert Collins
Tweak storage towards mergability.
1241
    @needs_read_lock
2850.3.2 by Robert Collins
Review feedback.
1242
    def get_revision_graph(self, revision_id=None):
1243
        """Return a dictionary containing the revision graph.
1244
1245
        NB: This method should not be used as it accesses the entire graph all
1246
        at once, which is much more data than most operations should require.
1247
1248
        :param revision_id: The revision_id to get a graph from. If None, then
1249
        the entire revision graph is returned. This is a deprecated mode of
1250
        operation and will be removed in the future.
1251
        :return: a dictionary of revision_id->revision_parents_list.
1252
        """
1253
        raise NotImplementedError(self.get_revision_graph)
1254
1255
    @needs_read_lock
1594.2.3 by Robert Collins
bugfix revision.MultipleRevisionSources.get_revision_graph to integrate ghosts between sources. [slow on weaves, fast on knits.
1256
    def get_revision_graph_with_ghosts(self, revision_ids=None):
1257
        """Return a graph of the revisions with ghosts marked as applicable.
1258
1259
        :param revision_ids: an iterable of revisions to graph or None for all.
1260
        :return: a Graph object with the graph reachable from revision_ids.
1261
        """
2745.1.1 by Robert Collins
Add a number of -Devil checkpoints.
1262
        if 'evil' in debug.debug_flags:
2881.4.1 by Robert Collins
Move responsibility for detecting same-repo fetching from the
1263
            mutter_callsite(3,
2745.1.1 by Robert Collins
Add a number of -Devil checkpoints.
1264
                "get_revision_graph_with_ghosts scales with size of history.")
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
1265
        result = deprecated_graph.Graph()
1594.2.3 by Robert Collins
bugfix revision.MultipleRevisionSources.get_revision_graph to integrate ghosts between sources. [slow on weaves, fast on knits.
1266
        if not revision_ids:
1773.4.2 by Martin Pool
Cleanup of imports; undeprecate all_revision_ids()
1267
            pending = set(self.all_revision_ids())
1594.2.3 by Robert Collins
bugfix revision.MultipleRevisionSources.get_revision_graph to integrate ghosts between sources. [slow on weaves, fast on knits.
1268
            required = set([])
1269
        else:
2858.2.1 by Martin Pool
Remove most calls to safe_file_id and safe_revision_id.
1270
            pending = set(revision_ids)
1836.3.1 by Robert Collins
(robertc) Teach repository.get_revision_graph, and revision.common_ancestor, about NULL_REVISION.
1271
            # special case NULL_REVISION
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1272
            if _mod_revision.NULL_REVISION in pending:
1273
                pending.remove(_mod_revision.NULL_REVISION)
1836.3.1 by Robert Collins
(robertc) Teach repository.get_revision_graph, and revision.common_ancestor, about NULL_REVISION.
1274
            required = set(pending)
1594.2.3 by Robert Collins
bugfix revision.MultipleRevisionSources.get_revision_graph to integrate ghosts between sources. [slow on weaves, fast on knits.
1275
        done = set([])
1276
        while len(pending):
1277
            revision_id = pending.pop()
1278
            try:
1279
                rev = self.get_revision(revision_id)
1280
            except errors.NoSuchRevision:
1281
                if revision_id in required:
1282
                    raise
1283
                # a ghost
1284
                result.add_ghost(revision_id)
1285
                continue
1286
            for parent_id in rev.parent_ids:
1287
                # is this queued or done ?
1288
                if (parent_id not in pending and
1289
                    parent_id not in done):
1290
                    # no, queue it.
1291
                    pending.add(parent_id)
1292
            result.add_node(revision_id, rev.parent_ids)
1594.2.15 by Robert Collins
Unfuck performance.
1293
            done.add(revision_id)
1594.2.3 by Robert Collins
bugfix revision.MultipleRevisionSources.get_revision_graph to integrate ghosts between sources. [slow on weaves, fast on knits.
1294
        return result
1295
2230.3.54 by Aaron Bentley
Move reverse history iteration to repository
1296
    def _get_history_vf(self):
1297
        """Get a versionedfile whose history graph reflects all revisions.
1298
1299
        For weave repositories, this is the inventory weave.
1300
        """
1301
        return self.get_inventory_weave()
1302
1303
    def iter_reverse_revision_history(self, revision_id):
1304
        """Iterate backwards through revision ids in the lefthand history
1305
1306
        :param revision_id: The revision id to start with.  All its lefthand
1307
            ancestors will be traversed.
1308
        """
1309
        if revision_id in (None, _mod_revision.NULL_REVISION):
1310
            return
1311
        next_id = revision_id
1312
        versionedfile = self._get_history_vf()
1313
        while True:
1314
            yield next_id
1315
            parents = versionedfile.get_parents(next_id)
1316
            if len(parents) == 0:
1317
                return
1318
            else:
1319
                next_id = parents[0]
1320
1594.2.3 by Robert Collins
bugfix revision.MultipleRevisionSources.get_revision_graph to integrate ghosts between sources. [slow on weaves, fast on knits.
1321
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1322
    def get_revision_inventory(self, revision_id):
1323
        """Return inventory of a past revision."""
1324
        # TODO: Unify this with get_inventory()
1325
        # bzr 0.0.6 and later imposes the constraint that the inventory_id
1326
        # must be the same as its revision, so this is trivial.
1534.4.28 by Robert Collins
first cut at merge from integration.
1327
        if revision_id is None:
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1328
            # This does not make sense: if there is no revision,
1329
            # then it is the current tree inventory surely ?!
1330
            # and thus get_root_id() is something that looks at the last
1331
            # commit on the branch, and the get_root_id is an inventory check.
1332
            raise NotImplementedError
1333
            # return Inventory(self.get_root_id())
1334
        else:
1335
            return self.get_inventory(revision_id)
1336
1185.65.27 by Robert Collins
Tweak storage towards mergability.
1337
    @needs_read_lock
1534.6.3 by Robert Collins
find_repository sufficiently robust.
1338
    def is_shared(self):
1339
        """Return True if this repository is flagged as a shared repository."""
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
1340
        raise NotImplementedError(self.is_shared)
1534.6.3 by Robert Collins
find_repository sufficiently robust.
1341
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
1342
    @needs_write_lock
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
1343
    def reconcile(self, other=None, thorough=False):
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
1344
        """Reconcile this repository."""
1345
        from bzrlib.reconcile import RepoReconciler
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
1346
        reconciler = RepoReconciler(self, thorough=thorough)
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
1347
        reconciler.reconcile()
1348
        return reconciler
2440.1.1 by Martin Pool
Add new Repository.sprout,
1349
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
1350
    def _refresh_data(self):
1351
        """Helper called from lock_* to ensure coherency with disk.
1352
1353
        The default implementation does nothing; it is however possible
1354
        for repositories to maintain loaded indices across multiple locks
1355
        by checking inside their implementation of this method to see
1356
        whether their indices are still valid. This depends of course on
1357
        the disk format being validatable in this manner.
1358
        """
1359
1534.6.3 by Robert Collins
find_repository sufficiently robust.
1360
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1361
    def revision_tree(self, revision_id):
1362
        """Return Tree for a revision on this branch.
1363
1852.5.1 by Robert Collins
Deprecate EmptyTree in favour of using Repository.revision_tree.
1364
        `revision_id` may be None for the empty tree revision.
1365
        """
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1366
        # TODO: refactor this to use an existing revision object
1367
        # so we don't need to read it in twice.
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1368
        if revision_id is None or revision_id == _mod_revision.NULL_REVISION:
1731.1.61 by Aaron Bentley
Merge bzr.dev
1369
            return RevisionTree(self, Inventory(root_id=None), 
1370
                                _mod_revision.NULL_REVISION)
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1371
        else:
1372
            inv = self.get_revision_inventory(revision_id)
1185.65.17 by Robert Collins
Merge from integration, mode-changes are broken.
1373
            return RevisionTree(self, inv, revision_id)
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1374
1185.65.27 by Robert Collins
Tweak storage towards mergability.
1375
    @needs_read_lock
1756.3.3 by Aaron Bentley
More refactoring, introduce revision_trees.
1376
    def revision_trees(self, revision_ids):
1377
        """Return Tree for a revision on this branch.
1378
1756.3.19 by Aaron Bentley
Documentation and cleanups
1379
        `revision_id` may not be None or 'null:'"""
1756.3.3 by Aaron Bentley
More refactoring, introduce revision_trees.
1380
        assert None not in revision_ids
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1381
        assert _mod_revision.NULL_REVISION not in revision_ids
1756.3.5 by Aaron Bentley
Switch to get_texts, optimize get_texts
1382
        texts = self.get_inventory_weave().get_texts(revision_ids)
1756.3.3 by Aaron Bentley
More refactoring, introduce revision_trees.
1383
        for text, revision_id in zip(texts, revision_ids):
1384
            inv = self.deserialise_inventory(revision_id, text)
1385
            yield RevisionTree(self, inv, revision_id)
1386
1387
    @needs_read_lock
2530.1.1 by Aaron Bentley
Make topological sorting optional for get_ancestry
1388
    def get_ancestry(self, revision_id, topo_sorted=True):
1185.66.2 by Aaron Bentley
Moved get_ancestry to RevisionStorage
1389
        """Return a list of revision-ids integrated by a revision.
1732.2.4 by Martin Pool
Split check into Branch.check and Repository.check
1390
1391
        The first element of the list is always None, indicating the origin 
1392
        revision.  This might change when we have history horizons, or 
1393
        perhaps we should have a new API.
1185.66.2 by Aaron Bentley
Moved get_ancestry to RevisionStorage
1394
        
1395
        This is topologically sorted.
1396
        """
2598.5.1 by Aaron Bentley
Start eliminating the use of None to indicate null revision
1397
        if _mod_revision.is_null(revision_id):
1185.66.2 by Aaron Bentley
Moved get_ancestry to RevisionStorage
1398
            return [None]
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1399
        if not self.has_revision(revision_id):
1400
            raise errors.NoSuchRevision(self, revision_id)
1185.66.2 by Aaron Bentley
Moved get_ancestry to RevisionStorage
1401
        w = self.get_inventory_weave()
2490.2.33 by Aaron Bentley
Disable topological sorting of get_ancestry where sensible
1402
        candidates = w.get_ancestry(revision_id, topo_sorted)
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
1403
        return [None] + candidates # self._eliminate_revisions_not_present(candidates)
1185.66.2 by Aaron Bentley
Moved get_ancestry to RevisionStorage
1404
2604.2.1 by Robert Collins
(robertc) Introduce a pack command.
1405
    def pack(self):
1406
        """Compress the data within the repository.
1407
1408
        This operation only makes sense for some repository types. For other
1409
        types it should be a no-op that just returns.
1410
1411
        This stub method does not require a lock, but subclasses should use
1412
        @needs_write_lock as this is a long running call its reasonable to 
1413
        implicitly lock for the user.
1414
        """
1415
1185.65.4 by Aaron Bentley
Fixed cat command
1416
    @needs_read_lock
1417
    def print_file(self, file, revision_id):
1185.65.29 by Robert Collins
Implement final review suggestions.
1418
        """Print `file` to stdout.
1419
        
1420
        FIXME RBC 20060125 as John Meinel points out this is a bad api
1421
        - it writes to stdout, it assumes that that is valid etc. Fix
1422
        by creating a new more flexible convenience function.
1423
        """
1185.65.4 by Aaron Bentley
Fixed cat command
1424
        tree = self.revision_tree(revision_id)
1425
        # use inventory as it was in that revision
1426
        file_id = tree.inventory.path2id(file)
1427
        if not file_id:
1685.1.26 by John Arbash Meinel
Repository had a bug with what exception was raised when a file was missing
1428
            # TODO: jam 20060427 Write a test for this code path
1429
            #       it had a bug in it, and was raising the wrong
1430
            #       exception.
1431
            raise errors.BzrError("%r is not present in revision %s" % (file, revision_id))
1185.65.4 by Aaron Bentley
Fixed cat command
1432
        tree.print_file(file_id)
1433
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1434
    def get_transaction(self):
1435
        return self.control_files.get_transaction()
1436
2249.5.13 by John Arbash Meinel
Finish auditing Repository, and fix generate_ids to always generate utf8 ids.
1437
    def revision_parents(self, revision_id):
1438
        return self.get_inventory_weave().parent_names(revision_id)
1590.1.1 by Robert Collins
Improve common_ancestor performance.
1439
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
1440
    def get_parents(self, revision_ids):
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
1441
        """See StackedParentsProvider.get_parents"""
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
1442
        parents_list = []
1443
        for revision_id in revision_ids:
1444
            if revision_id == _mod_revision.NULL_REVISION:
1445
                parents = []
1446
            else:
1447
                try:
1448
                    parents = self.get_revision(revision_id).parent_ids
1449
                except errors.NoSuchRevision:
1450
                    parents = None
1451
                else:
1452
                    if len(parents) == 0:
1453
                        parents = [_mod_revision.NULL_REVISION]
1454
            parents_list.append(parents)
1455
        return parents_list
1456
1457
    def _make_parents_provider(self):
1458
        return self
1459
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
1460
    def get_graph(self, other_repository=None):
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
1461
        """Return the graph walker for this repository format"""
1462
        parents_provider = self._make_parents_provider()
2490.2.14 by Aaron Bentley
Avoid StackedParentsProvider when underlying repos match
1463
        if (other_repository is not None and
1464
            other_repository.bzrdir.transport.base !=
1465
            self.bzrdir.transport.base):
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
1466
            parents_provider = graph._StackedParentsProvider(
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
1467
                [parents_provider, other_repository._make_parents_provider()])
2490.2.22 by Aaron Bentley
Rename GraphWalker -> Graph, _AncestryWalker -> _BreadthFirstSearcher
1468
        return graph.Graph(parents_provider)
2490.2.13 by Aaron Bentley
Update distinct -> lowest, refactor, add ParentsProvider concept
1469
2745.6.47 by Andrew Bennetts
Move check_parents out of VersionedFile.
1470
    def get_versioned_file_checker(self, revisions, revision_versions_cache):
2745.6.57 by Andrew Bennetts
Some improvements suggested by Martin's review.
1471
        return VersionedFileChecker(revisions, revision_versions_cache, self)
2745.6.47 by Andrew Bennetts
Move check_parents out of VersionedFile.
1472
1185.65.27 by Robert Collins
Tweak storage towards mergability.
1473
    @needs_write_lock
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
1474
    def set_make_working_trees(self, new_value):
1475
        """Set the policy flag for making working trees when creating branches.
1476
1477
        This only applies to branches that use this repository.
1478
1479
        The default is 'True'.
1480
        :param new_value: True to restore the default, False to disable making
1481
                          working trees.
1482
        """
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
1483
        raise NotImplementedError(self.set_make_working_trees)
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
1484
    
1485
    def make_working_trees(self):
1486
        """Returns the policy for making working trees on new branches."""
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
1487
        raise NotImplementedError(self.make_working_trees)
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
1488
1489
    @needs_write_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1490
    def sign_revision(self, revision_id, gpg_strategy):
1491
        plaintext = Testament.from_revision(self, revision_id).as_short_text()
1492
        self.store_revision_signature(gpg_strategy, plaintext, revision_id)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1493
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1494
    @needs_read_lock
1495
    def has_signature_for_revision_id(self, revision_id):
1496
        """Query for a revision signature for revision_id in the repository."""
1497
        return self._revision_store.has_signature(revision_id,
1498
                                                  self.get_transaction())
1499
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1500
    @needs_read_lock
1501
    def get_signature_text(self, revision_id):
1502
        """Return the text for a signature."""
1503
        return self._revision_store.get_signature_text(revision_id,
1504
                                                       self.get_transaction())
1505
1732.2.4 by Martin Pool
Split check into Branch.check and Repository.check
1506
    @needs_read_lock
2745.6.36 by Andrew Bennetts
Deprecate revision_ids arg to Repository.check and other tweaks.
1507
    def check(self, revision_ids=None):
1732.2.4 by Martin Pool
Split check into Branch.check and Repository.check
1508
        """Check consistency of all history of given revision_ids.
1509
1510
        Different repository implementations should override _check().
1511
1512
        :param revision_ids: A non-empty list of revision_ids whose ancestry
1513
             will be checked.  Typically the last revision_id of a branch.
1514
        """
1515
        return self._check(revision_ids)
1516
1517
    def _check(self, revision_ids):
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
1518
        result = check.Check(self)
1732.2.4 by Martin Pool
Split check into Branch.check and Repository.check
1519
        result.check()
1520
        return result
1521
1904.2.3 by Martin Pool
Give a warning on access to old repository formats
1522
    def _warn_if_deprecated(self):
1904.2.5 by Martin Pool
Fix format warning inside test suite and add test
1523
        global _deprecation_warning_done
1524
        if _deprecation_warning_done:
1525
            return
1526
        _deprecation_warning_done = True
1904.2.3 by Martin Pool
Give a warning on access to old repository formats
1527
        warning("Format %s for %s is deprecated - please use 'bzr upgrade' to get better performance"
1528
                % (self._format, self.bzrdir.transport.base))
1529
1910.2.63 by Aaron Bentley
Add supports_rich_root member to repository
1530
    def supports_rich_root(self):
1531
        return self._format.rich_root_data
1532
2150.2.2 by Robert Collins
Change the commit builder selected-revision-id test to use a unicode revision id where possible, leading to stricter testing of the hypothetical unicode revision id support in bzr.
1533
    def _check_ascii_revisionid(self, revision_id, method):
1534
        """Private helper for ascii-only repositories."""
1535
        # weave repositories refuse to store revisionids that are non-ascii.
1536
        if revision_id is not None:
1537
            # weaves require ascii revision ids.
1538
            if isinstance(revision_id, unicode):
1539
                try:
1540
                    revision_id.encode('ascii')
1541
                except UnicodeEncodeError:
1542
                    raise errors.NonAsciiRevisionId(method, self)
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
1543
            else:
1544
                try:
1545
                    revision_id.decode('ascii')
1546
                except UnicodeDecodeError:
1547
                    raise errors.NonAsciiRevisionId(method, self)
2819.2.4 by Andrew Bennetts
Add a 'revision_graph_can_have_wrong_parents' method to repository.
1548
    
1549
    def revision_graph_can_have_wrong_parents(self):
1550
        """Is it possible for this repository to have a revision graph with
1551
        incorrect parents?
2150.2.2 by Robert Collins
Change the commit builder selected-revision-id test to use a unicode revision id where possible, leading to stricter testing of the hypothetical unicode revision id support in bzr.
1552
2819.2.4 by Andrew Bennetts
Add a 'revision_graph_can_have_wrong_parents' method to repository.
1553
        If True, then this repository must also implement
1554
        _find_inconsistent_revision_parents so that check and reconcile can
1555
        check for inconsistencies before proceeding with other checks that may
1556
        depend on the revision index being consistent.
1557
        """
1558
        raise NotImplementedError(self.revision_graph_can_have_wrong_parents)
1559
        
2241.1.18 by mbp at sourcefrog
Restore use of deprecating delegator for old formats in bzrlib.repository.
1560
# remove these delegates a while after bzr 0.15
1561
def __make_delegated(name, from_module):
1562
    def _deprecated_repository_forwarder():
1563
        symbol_versioning.warn('%s moved to %s in bzr 0.15'
1564
            % (name, from_module),
2241.1.20 by mbp at sourcefrog
update tests for new locations of weave repos
1565
            DeprecationWarning,
1566
            stacklevel=2)
2241.1.18 by mbp at sourcefrog
Restore use of deprecating delegator for old formats in bzrlib.repository.
1567
        m = __import__(from_module, globals(), locals(), [name])
1568
        try:
1569
            return getattr(m, name)
1570
        except AttributeError:
1571
            raise AttributeError('module %s has no name %s'
1572
                    % (m, name))
1573
    globals()[name] = _deprecated_repository_forwarder
1574
1575
for _name in [
1576
        'AllInOneRepository',
1577
        'WeaveMetaDirRepository',
1578
        'PreSplitOutRepositoryFormat',
1579
        'RepositoryFormat4',
1580
        'RepositoryFormat5',
1581
        'RepositoryFormat6',
1582
        'RepositoryFormat7',
1583
        ]:
1584
    __make_delegated(_name, 'bzrlib.repofmt.weaverepo')
1585
1586
for _name in [
1587
        'KnitRepository',
1588
        'RepositoryFormatKnit',
1589
        'RepositoryFormatKnit1',
1590
        ]:
1591
    __make_delegated(_name, 'bzrlib.repofmt.knitrepo')
1592
1593
1185.82.84 by Aaron Bentley
Moved stuff around
1594
def install_revision(repository, rev, revision_tree):
1595
    """Install all revision data into a repository."""
1596
    present_parents = []
1597
    parent_trees = {}
1598
    for p_id in rev.parent_ids:
1599
        if repository.has_revision(p_id):
1600
            present_parents.append(p_id)
1601
            parent_trees[p_id] = repository.revision_tree(p_id)
1602
        else:
1852.5.1 by Robert Collins
Deprecate EmptyTree in favour of using Repository.revision_tree.
1603
            parent_trees[p_id] = repository.revision_tree(None)
1185.82.84 by Aaron Bentley
Moved stuff around
1604
1605
    inv = revision_tree.inventory
1910.2.51 by Aaron Bentley
Bundles now corrupt repositories
1606
    entries = inv.iter_entries()
2617.6.6 by Robert Collins
Some review feedback.
1607
    # backwards compatibility hack: skip the root id.
1910.2.63 by Aaron Bentley
Add supports_rich_root member to repository
1608
    if not repository.supports_rich_root():
1910.2.60 by Aaron Bentley
Ensure that new-model revisions aren't installed into old-model repos
1609
        path, root = entries.next()
1610
        if root.revision != rev.revision_id:
1910.2.63 by Aaron Bentley
Add supports_rich_root member to repository
1611
            raise errors.IncompatibleRevision(repr(repository))
1185.82.84 by Aaron Bentley
Moved stuff around
1612
    # Add the texts that are not already present
1852.6.3 by Robert Collins
Make iter(Tree) consistent for all tree types.
1613
    for path, ie in entries:
1185.82.84 by Aaron Bentley
Moved stuff around
1614
        w = repository.weave_store.get_weave_or_empty(ie.file_id,
1615
                repository.get_transaction())
1616
        if ie.revision not in w:
1617
            text_parents = []
1740.2.2 by Aaron Bentley
Add test for the basis inventory automatically adding the revision id.
1618
            # FIXME: TODO: The following loop *may* be overlapping/duplicate
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1619
            # with InventoryEntry.find_previous_heads(). if it is, then there
1740.2.2 by Aaron Bentley
Add test for the basis inventory automatically adding the revision id.
1620
            # is a latent bug here where the parents may have ancestors of each
1621
            # other. RBC, AB
1185.82.84 by Aaron Bentley
Moved stuff around
1622
            for revision, tree in parent_trees.iteritems():
1623
                if ie.file_id not in tree:
1624
                    continue
1625
                parent_id = tree.inventory[ie.file_id].revision
1626
                if parent_id in text_parents:
1627
                    continue
1628
                text_parents.append(parent_id)
1629
                    
1630
            vfile = repository.weave_store.get_weave_or_empty(ie.file_id, 
1631
                repository.get_transaction())
1632
            lines = revision_tree.get_file(ie.file_id).readlines()
1633
            vfile.add_lines(rev.revision_id, text_parents, lines)
1634
    try:
1635
        # install the inventory
1636
        repository.add_inventory(rev.revision_id, inv, present_parents)
1637
    except errors.RevisionAlreadyPresent:
1638
        pass
1639
    repository.add_revision(rev.revision_id, rev, inv)
1640
1641
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1642
class MetaDirRepository(Repository):
1643
    """Repositories in the new meta-dir layout."""
1644
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1645
    def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1646
        super(MetaDirRepository, self).__init__(_format,
1647
                                                a_bzrdir,
1648
                                                control_files,
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1649
                                                _revision_store,
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
1650
                                                control_store,
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1651
                                                text_store)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1652
        dir_mode = self.control_files._dir_mode
1653
        file_mode = self.control_files._file_mode
1654
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
1655
    @needs_read_lock
1656
    def is_shared(self):
1657
        """Return True if this repository is flagged as a shared repository."""
1658
        return self.control_files._transport.has('shared-storage')
1659
1660
    @needs_write_lock
1661
    def set_make_working_trees(self, new_value):
1662
        """Set the policy flag for making working trees when creating branches.
1663
1664
        This only applies to branches that use this repository.
1665
1666
        The default is 'True'.
1667
        :param new_value: True to restore the default, False to disable making
1668
                          working trees.
1669
        """
1670
        if new_value:
1671
            try:
1672
                self.control_files._transport.delete('no-working-trees')
1673
            except errors.NoSuchFile:
1674
                pass
1675
        else:
1676
            self.control_files.put_utf8('no-working-trees', '')
1677
    
1678
    def make_working_trees(self):
1679
        """Returns the policy for making working trees on new branches."""
1680
        return not self.control_files._transport.has('no-working-trees')
1681
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1682
2241.1.2 by Martin Pool
change to using external Repository format registry
1683
class RepositoryFormatRegistry(registry.Registry):
2889.1.1 by Robert Collins
* The class ``bzrlib.repofmt.knitrepo.KnitRepository3`` has been folded into
1684
    """Registry of RepositoryFormats."""
2241.1.11 by Martin Pool
Get rid of RepositoryFormat*_instance objects. Instead the format
1685
1686
    def get(self, format_string):
1687
        r = registry.Registry.get(self, format_string)
1688
        if callable(r):
1689
            r = r()
1690
        return r
2241.1.2 by Martin Pool
change to using external Repository format registry
1691
    
1692
1693
format_registry = RepositoryFormatRegistry()
2241.1.11 by Martin Pool
Get rid of RepositoryFormat*_instance objects. Instead the format
1694
"""Registry of formats, indexed by their identifying format string.
1695
1696
This can contain either format instances themselves, or classes/factories that
1697
can be called to obtain one.
1698
"""
2241.1.2 by Martin Pool
change to using external Repository format registry
1699
2220.2.3 by Martin Pool
Add tag: revision namespace.
1700
1701
#####################################################################
1702
# Repository Formats
1910.2.46 by Aaron Bentley
Whitespace fix
1703
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1704
class RepositoryFormat(object):
1705
    """A repository format.
1706
1707
    Formats provide three things:
1708
     * An initialization routine to construct repository data on disk.
1709
     * a format string which is used when the BzrDir supports versioned
1710
       children.
1711
     * an open routine which returns a Repository instance.
1712
2889.1.2 by Robert Collins
Review feedback.
1713
    There is one and only one Format subclass for each on-disk format. But
1714
    there can be one Repository subclass that is used for several different
1715
    formats. The _format attribute on a Repository instance can be used to
1716
    determine the disk format.
2889.1.1 by Robert Collins
* The class ``bzrlib.repofmt.knitrepo.KnitRepository3`` has been folded into
1717
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1718
    Formats are placed in an dict by their format string for reference 
1719
    during opening. These should be subclasses of RepositoryFormat
1720
    for consistency.
1721
1722
    Once a format is deprecated, just deprecate the initialize and open
1723
    methods on the format class. Do not deprecate the object, as the 
1724
    object will be created every system load.
1725
1726
    Common instance attributes:
1727
    _matchingbzrdir - the bzrdir format that the repository format was
1728
    originally written to work with. This can be used if manually
1729
    constructing a bzrdir and repository, or more commonly for test suite
1730
    parameterisation.
1731
    """
1732
1904.2.3 by Martin Pool
Give a warning on access to old repository formats
1733
    def __str__(self):
1734
        return "<%s>" % self.__class__.__name__
1735
2241.1.11 by Martin Pool
Get rid of RepositoryFormat*_instance objects. Instead the format
1736
    def __eq__(self, other):
1737
        # format objects are generally stateless
1738
        return isinstance(other, self.__class__)
1739
2100.3.35 by Aaron Bentley
equality operations on bzrdir
1740
    def __ne__(self, other):
2100.3.31 by Aaron Bentley
Merged bzr.dev (17 tests failing)
1741
        return not self == other
1742
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1743
    @classmethod
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1744
    def find_format(klass, a_bzrdir):
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
1745
        """Return the format for the repository object in a_bzrdir.
1746
        
1747
        This is used by bzr native formats that have a "format" file in
1748
        the repository.  Other methods may be used by different types of 
1749
        control directory.
1750
        """
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1751
        try:
1752
            transport = a_bzrdir.get_repository_transport(None)
1753
            format_string = transport.get("format").read()
2241.1.2 by Martin Pool
change to using external Repository format registry
1754
            return format_registry.get(format_string)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1755
        except errors.NoSuchFile:
1756
            raise errors.NoRepositoryPresent(a_bzrdir)
1757
        except KeyError:
1740.5.6 by Martin Pool
Clean up many exception classes.
1758
            raise errors.UnknownFormatError(format=format_string)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1759
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
1760
    @classmethod
2241.1.2 by Martin Pool
change to using external Repository format registry
1761
    def register_format(klass, format):
1762
        format_registry.register(format.get_format_string(), format)
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
1763
1764
    @classmethod
1765
    def unregister_format(klass, format):
2241.1.2 by Martin Pool
change to using external Repository format registry
1766
        format_registry.remove(format.get_format_string())
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
1767
    
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1768
    @classmethod
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1769
    def get_default_format(klass):
1770
        """Return the current default format."""
2204.5.3 by Aaron Bentley
zap old repository default handling
1771
        from bzrlib import bzrdir
1772
        return bzrdir.format_registry.make_bzrdir('default').repository_format
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
1773
1774
    def _get_control_store(self, repo_transport, control_files):
1775
        """Return the control store for this repository."""
1776
        raise NotImplementedError(self._get_control_store)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1777
1778
    def get_format_string(self):
1779
        """Return the ASCII format string that identifies this format.
1780
        
1781
        Note that in pre format ?? repositories the format string is 
1782
        not permitted nor written to disk.
1783
        """
1784
        raise NotImplementedError(self.get_format_string)
1785
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1786
    def get_format_description(self):
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1787
        """Return the short description for this format."""
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1788
        raise NotImplementedError(self.get_format_description)
1789
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1790
    def _get_revision_store(self, repo_transport, control_files):
1791
        """Return the revision store object for this a_bzrdir."""
1556.1.5 by Robert Collins
Review feedback.
1792
        raise NotImplementedError(self._get_revision_store)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1793
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1794
    def _get_text_rev_store(self,
1795
                            transport,
1796
                            control_files,
1797
                            name,
1798
                            compressed=True,
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1799
                            prefixed=False,
1800
                            serializer=None):
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1801
        """Common logic for getting a revision store for a repository.
1802
        
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1803
        see self._get_revision_store for the subclass-overridable method to 
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1804
        get the store for a repository.
1805
        """
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1806
        from bzrlib.store.revision.text import TextRevisionStore
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1807
        dir_mode = control_files._dir_mode
1808
        file_mode = control_files._file_mode
2220.2.2 by Martin Pool
Add tag command and basic implementation
1809
        text_store = TextStore(transport.clone(name),
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1810
                              prefixed=prefixed,
1811
                              compressed=compressed,
1812
                              dir_mode=dir_mode,
1813
                              file_mode=file_mode)
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1814
        _revision_store = TextRevisionStore(text_store, serializer)
1815
        return _revision_store
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1816
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
1817
    # TODO: this shouldn't be in the base class, it's specific to things that
1818
    # use weaves or knits -- mbp 20070207
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1819
    def _get_versioned_file_store(self,
1820
                                  name,
1821
                                  transport,
1822
                                  control_files,
1823
                                  prefixed=True,
2241.1.10 by Martin Pool
Remove more references to weaves from the repository.py file
1824
                                  versionedfile_class=None,
1946.2.5 by John Arbash Meinel
Make knit stores delay creation, but not control stores
1825
                                  versionedfile_kwargs={},
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
1826
                                  escaped=False):
2241.1.10 by Martin Pool
Remove more references to weaves from the repository.py file
1827
        if versionedfile_class is None:
1828
            versionedfile_class = self._versionedfile_class
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1829
        weave_transport = control_files._transport.clone(name)
1830
        dir_mode = control_files._dir_mode
1831
        file_mode = control_files._file_mode
1832
        return VersionedFileStore(weave_transport, prefixed=prefixed,
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
1833
                                  dir_mode=dir_mode,
1834
                                  file_mode=file_mode,
1835
                                  versionedfile_class=versionedfile_class,
1946.2.5 by John Arbash Meinel
Make knit stores delay creation, but not control stores
1836
                                  versionedfile_kwargs=versionedfile_kwargs,
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
1837
                                  escaped=escaped)
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1838
1534.6.1 by Robert Collins
allow API creation of shared repositories
1839
    def initialize(self, a_bzrdir, shared=False):
1840
        """Initialize a repository of this format in a_bzrdir.
1841
1842
        :param a_bzrdir: The bzrdir to put the new repository in it.
1843
        :param shared: The repository should be initialized as a sharable one.
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
1844
        :returns: The new repository object.
1845
        
1534.6.1 by Robert Collins
allow API creation of shared repositories
1846
        This may raise UninitializableFormat if shared repository are not
1847
        compatible the a_bzrdir.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1848
        """
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
1849
        raise NotImplementedError(self.initialize)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1850
1851
    def is_supported(self):
1852
        """Is this format supported?
1853
1854
        Supported formats must be initializable and openable.
1855
        Unsupported formats may not support initialization or committing or 
1856
        some other features depending on the reason for not being supported.
1857
        """
1858
        return True
1859
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1860
    def check_conversion_target(self, target_format):
1861
        raise NotImplementedError(self.check_conversion_target)
1862
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1863
    def open(self, a_bzrdir, _found=False):
1864
        """Return an instance of this format for the bzrdir a_bzrdir.
1865
        
1866
        _found is a private parameter, do not use it.
1867
        """
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1868
        raise NotImplementedError(self.open)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1869
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1870
1871
class MetaDirRepositoryFormat(RepositoryFormat):
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1872
    """Common base class for the new repositories using the metadir layout."""
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1873
1910.2.14 by Aaron Bentley
Fail when trying to use interrepository on Knit2 and Knit1
1874
    rich_root_data = False
2323.5.17 by Martin Pool
Add supports_tree_reference to all repo formats (robert)
1875
    supports_tree_reference = False
2241.1.11 by Martin Pool
Get rid of RepositoryFormat*_instance objects. Instead the format
1876
    _matchingbzrdir = bzrdir.BzrDirMetaFormat1()
1910.2.14 by Aaron Bentley
Fail when trying to use interrepository on Knit2 and Knit1
1877
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1878
    def __init__(self):
1879
        super(MetaDirRepositoryFormat, self).__init__()
1880
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1881
    def _create_control_files(self, a_bzrdir):
1882
        """Create the required files and the initial control_files object."""
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
1883
        # FIXME: RBC 20060125 don't peek under the covers
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1884
        # NB: no need to escape relative paths that are url safe.
1885
        repository_transport = a_bzrdir.get_repository_transport(self)
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1886
        control_files = lockable_files.LockableFiles(repository_transport,
1887
                                'lock', lockdir.LockDir)
1553.5.61 by Martin Pool
Locks protecting LockableFiles must now be explicitly created before use.
1888
        control_files.create_lock()
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1889
        return control_files
1890
1891
    def _upload_blank_content(self, a_bzrdir, dirs, files, utf8_files, shared):
1892
        """Upload the initial blank content."""
1893
        control_files = self._create_control_files(a_bzrdir)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1894
        control_files.lock_write()
1895
        try:
1553.5.49 by Martin Pool
Use LockDirs for repo format 7
1896
            control_files._transport.mkdir_multi(dirs,
1897
                    mode=control_files._dir_mode)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1898
            for file, content in files:
1899
                control_files.put(file, content)
1900
            for file, content in utf8_files:
1901
                control_files.put_utf8(file, content)
1534.6.1 by Robert Collins
allow API creation of shared repositories
1902
            if shared == True:
1903
                control_files.put_utf8('shared-storage', '')
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1904
        finally:
1905
            control_files.unlock()
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1906
1907
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1908
# formats which have no format string are not discoverable
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1909
# and not independently creatable, so are not registered.  They're 
2241.1.11 by Martin Pool
Get rid of RepositoryFormat*_instance objects. Instead the format
1910
# all in bzrlib.repofmt.weaverepo now.  When an instance of one of these is
1911
# needed, it's constructed directly by the BzrDir.  Non-native formats where
1912
# the repository is not separately opened are similar.
1913
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1914
format_registry.register_lazy(
1915
    'Bazaar-NG Repository format 7',
1916
    'bzrlib.repofmt.weaverepo',
2241.1.11 by Martin Pool
Get rid of RepositoryFormat*_instance objects. Instead the format
1917
    'RepositoryFormat7'
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1918
    )
2881.4.1 by Robert Collins
Move responsibility for detecting same-repo fetching from the
1919
2241.1.1 by Martin Pool
Change RepositoryFormat to use a Registry rather than ad-hoc dictionary
1920
# KEEP in sync with bzrdir.format_registry default, which controls the overall
1921
# default control directory format
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
1922
format_registry.register_lazy(
1923
    'Bazaar-NG Knit Repository Format 1',
1924
    'bzrlib.repofmt.knitrepo',
2241.1.11 by Martin Pool
Get rid of RepositoryFormat*_instance objects. Instead the format
1925
    'RepositoryFormatKnit1',
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
1926
    )
1927
format_registry.default_key = 'Bazaar-NG Knit Repository Format 1'
1928
2241.1.5 by Martin Pool
Move KnitFormat2 into repofmt
1929
format_registry.register_lazy(
2255.2.230 by Robert Collins
Update tree format signatures to mention introducing bzr version.
1930
    'Bazaar Knit Repository Format 3 (bzr 0.15)\n',
2100.3.31 by Aaron Bentley
Merged bzr.dev (17 tests failing)
1931
    'bzrlib.repofmt.knitrepo',
1932
    'RepositoryFormatKnit3',
1933
    )
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1934
1935
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
1936
class InterRepository(InterObject):
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
1937
    """This class represents operations taking place between two repositories.
1938
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
1939
    Its instances have methods like copy_content and fetch, and contain
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
1940
    references to the source and target repositories these operations can be 
1941
    carried out on.
1942
1943
    Often we will provide convenience methods on 'repository' which carry out
1944
    operations with another repository - they will always forward to
1945
    InterRepository.get(other).method_name(parameters).
1946
    """
1947
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
1948
    _optimisers = []
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
1949
    """The available optimised InterRepository types."""
1950
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
1951
    def copy_content(self, revision_id=None):
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
1952
        raise NotImplementedError(self.copy_content)
1953
1954
    def fetch(self, revision_id=None, pb=None):
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
1955
        """Fetch the content required to construct revision_id.
1956
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
1957
        The content is copied from self.source to self.target.
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
1958
1959
        :param revision_id: if None all content is copied, if NULL_REVISION no
1960
                            content is copied.
1961
        :param pb: optional progress bar to use for progress reports. If not
1962
                   provided a default one will be created.
1963
1964
        Returns the copied revision count and the failed revisions in a tuple:
1965
        (copied, failures).
1966
        """
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
1967
        raise NotImplementedError(self.fetch)
1968
   
1969
    @needs_read_lock
1970
    def missing_revision_ids(self, revision_id=None):
1971
        """Return the revision ids that source has that target does not.
1972
        
1973
        These are returned in topological order.
1974
1975
        :param revision_id: only return revision ids included by this
1976
                            revision_id.
1977
        """
1978
        # generic, possibly worst case, slow code path.
1979
        target_ids = set(self.target.all_revision_ids())
1980
        if revision_id is not None:
1981
            source_ids = self.source.get_ancestry(revision_id)
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
1982
            assert source_ids[0] is None
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
1983
            source_ids.pop(0)
1984
        else:
1985
            source_ids = self.source.all_revision_ids()
1986
        result_set = set(source_ids).difference(target_ids)
1987
        # this may look like a no-op: its not. It preserves the ordering
1988
        # other_ids had while only returning the members from other_ids
1989
        # that we've decided we need.
1990
        return [rev_id for rev_id in source_ids if rev_id in result_set]
1991
2881.4.1 by Robert Collins
Move responsibility for detecting same-repo fetching from the
1992
    @staticmethod
1993
    def _same_model(source, target):
1994
        """True if source and target have the same data representation."""
1995
        if source.supports_rich_root() != target.supports_rich_root():
1996
            return False
1997
        if source._serializer != target._serializer:
1998
            return False
1999
        return True
2000
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
2001
2002
class InterSameDataRepository(InterRepository):
2003
    """Code for converting between repositories that represent the same data.
2004
    
2005
    Data format and model must match for this to work.
2006
    """
2007
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
2008
    @classmethod
2241.1.7 by Martin Pool
rename method
2009
    def _get_repo_format_to_test(self):
2814.1.1 by Robert Collins
* Pushing, pulling and branching branches with subtree references was not
2010
        """Repository format for testing with.
2011
        
2012
        InterSameData can pull from subtree to subtree and from non-subtree to
2013
        non-subtree, so we test this with the richest repository format.
2014
        """
2015
        from bzrlib.repofmt import knitrepo
2016
        return knitrepo.RepositoryFormatKnit3()
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
2017
1910.2.14 by Aaron Bentley
Fail when trying to use interrepository on Knit2 and Knit1
2018
    @staticmethod
2019
    def is_compatible(source, target):
2881.4.1 by Robert Collins
Move responsibility for detecting same-repo fetching from the
2020
        return InterRepository._same_model(source, target)
1910.2.14 by Aaron Bentley
Fail when trying to use interrepository on Knit2 and Knit1
2021
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
2022
    @needs_write_lock
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
2023
    def copy_content(self, revision_id=None):
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
2024
        """Make a complete copy of the content in self into destination.
2440.1.1 by Martin Pool
Add new Repository.sprout,
2025
2026
        This copies both the repository's revision data, and configuration information
2027
        such as the make_working_trees setting.
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
2028
        
2029
        This is a destructive operation! Do not use it on existing 
2030
        repositories.
2031
2032
        :param revision_id: Only copy the content needed to construct
2033
                            revision_id and its parents.
2034
        """
2035
        try:
2036
            self.target.set_make_working_trees(self.source.make_working_trees())
2037
        except NotImplementedError:
2038
            pass
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
2039
        # but don't bother fetching if we have the needed data now.
1996.3.20 by John Arbash Meinel
[merge] bzr.dev 2063
2040
        if (revision_id not in (None, _mod_revision.NULL_REVISION) and 
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
2041
            self.target.has_revision(revision_id)):
2042
            return
2043
        self.target.fetch(self.source, revision_id=revision_id)
2044
2045
    @needs_write_lock
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
2046
    def fetch(self, revision_id=None, pb=None):
1910.7.20 by Andrew Bennetts
Merge from bzr.dev
2047
        """See InterRepository.fetch()."""
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2048
        from bzrlib.fetch import GenericRepoFetcher
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
2049
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2858.2.1 by Martin Pool
Remove most calls to safe_file_id and safe_revision_id.
2050
               self.source, self.source._format, self.target,
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
2051
               self.target._format)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2052
        f = GenericRepoFetcher(to_repository=self.target,
2053
                               from_repository=self.source,
2054
                               last_revision=revision_id,
2055
                               pb=pb)
1534.1.33 by Robert Collins
Move copy_content_into into InterRepository and InterWeaveRepo, and disable the default codepath test as we have optimised paths for all current combinations.
2056
        return f.count_copied, f.failed_revisions
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
2057
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
2058
2241.1.12 by Martin Pool
Restore InterWeaveRepo
2059
class InterWeaveRepo(InterSameDataRepository):
2850.3.1 by Robert Collins
Move various weave specific code out of the base Repository class to weaverepo.py.
2060
    """Optimised code paths between Weave based repositories.
2061
    
2062
    This should be in bzrlib/repofmt/weaverepo.py but we have not yet
2063
    implemented lazy inter-object optimisation.
2064
    """
2241.1.12 by Martin Pool
Restore InterWeaveRepo
2065
2241.1.13 by Martin Pool
Re-register InterWeaveRepo, fix test integration, add test for it
2066
    @classmethod
2241.1.12 by Martin Pool
Restore InterWeaveRepo
2067
    def _get_repo_format_to_test(self):
2068
        from bzrlib.repofmt import weaverepo
2069
        return weaverepo.RepositoryFormat7()
2070
2071
    @staticmethod
2072
    def is_compatible(source, target):
2073
        """Be compatible with known Weave formats.
2074
        
2075
        We don't test for the stores being of specific types because that
2076
        could lead to confusing results, and there is no need to be 
2077
        overly general.
2078
        """
2079
        from bzrlib.repofmt.weaverepo import (
2080
                RepositoryFormat5,
2081
                RepositoryFormat6,
2082
                RepositoryFormat7,
2083
                )
2084
        try:
2085
            return (isinstance(source._format, (RepositoryFormat5,
2086
                                                RepositoryFormat6,
2087
                                                RepositoryFormat7)) and
2088
                    isinstance(target._format, (RepositoryFormat5,
2089
                                                RepositoryFormat6,
2090
                                                RepositoryFormat7)))
2091
        except AttributeError:
2092
            return False
2093
    
2094
    @needs_write_lock
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
2095
    def copy_content(self, revision_id=None):
2241.1.12 by Martin Pool
Restore InterWeaveRepo
2096
        """See InterRepository.copy_content()."""
2097
        # weave specific optimised path:
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
2098
        try:
2099
            self.target.set_make_working_trees(self.source.make_working_trees())
2100
        except NotImplementedError:
2101
            pass
2102
        # FIXME do not peek!
2103
        if self.source.control_files._transport.listable():
2104
            pb = ui.ui_factory.nested_progress_bar()
2241.1.12 by Martin Pool
Restore InterWeaveRepo
2105
            try:
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
2106
                self.target.weave_store.copy_all_ids(
2107
                    self.source.weave_store,
2108
                    pb=pb,
2109
                    from_transaction=self.source.get_transaction(),
2110
                    to_transaction=self.target.get_transaction())
2111
                pb.update('copying inventory', 0, 1)
2112
                self.target.control_weaves.copy_multi(
2113
                    self.source.control_weaves, ['inventory'],
2114
                    from_transaction=self.source.get_transaction(),
2115
                    to_transaction=self.target.get_transaction())
2116
                self.target._revision_store.text_store.copy_all_ids(
2117
                    self.source._revision_store.text_store,
2118
                    pb=pb)
2119
            finally:
2120
                pb.finished()
2121
        else:
2241.1.12 by Martin Pool
Restore InterWeaveRepo
2122
            self.target.fetch(self.source, revision_id=revision_id)
2123
2124
    @needs_write_lock
2125
    def fetch(self, revision_id=None, pb=None):
2126
        """See InterRepository.fetch()."""
2127
        from bzrlib.fetch import GenericRepoFetcher
2128
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2129
               self.source, self.source._format, self.target, self.target._format)
2130
        f = GenericRepoFetcher(to_repository=self.target,
2131
                               from_repository=self.source,
2132
                               last_revision=revision_id,
2133
                               pb=pb)
2134
        return f.count_copied, f.failed_revisions
2135
2136
    @needs_read_lock
2137
    def missing_revision_ids(self, revision_id=None):
2138
        """See InterRepository.missing_revision_ids()."""
2139
        # we want all revisions to satisfy revision_id in source.
2140
        # but we don't want to stat every file here and there.
2141
        # we want then, all revisions other needs to satisfy revision_id 
2142
        # checked, but not those that we have locally.
2143
        # so the first thing is to get a subset of the revisions to 
2144
        # satisfy revision_id in source, and then eliminate those that
2145
        # we do already have. 
2146
        # this is slow on high latency connection to self, but as as this
2147
        # disk format scales terribly for push anyway due to rewriting 
2148
        # inventory.weave, this is considered acceptable.
2149
        # - RBC 20060209
2150
        if revision_id is not None:
2151
            source_ids = self.source.get_ancestry(revision_id)
2152
            assert source_ids[0] is None
2153
            source_ids.pop(0)
2154
        else:
2155
            source_ids = self.source._all_possible_ids()
2156
        source_ids_set = set(source_ids)
2157
        # source_ids is the worst possible case we may need to pull.
2158
        # now we want to filter source_ids against what we actually
2159
        # have in target, but don't try to check for existence where we know
2160
        # we do not have a revision as that would be pointless.
2161
        target_ids = set(self.target._all_possible_ids())
2162
        possibly_present_revisions = target_ids.intersection(source_ids_set)
2163
        actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
2164
        required_revisions = source_ids_set.difference(actually_present_revisions)
2165
        required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
2166
        if revision_id is not None:
2167
            # we used get_ancestry to determine source_ids then we are assured all
2168
            # revisions referenced are present as they are installed in topological order.
2169
            # and the tip revision was validated by get_ancestry.
2170
            return required_topo_revisions
2171
        else:
2172
            # if we just grabbed the possibly available ids, then 
2173
            # we only have an estimate of whats available and need to validate
2174
            # that against the revision records.
2175
            return self.source._eliminate_revisions_not_present(required_topo_revisions)
2176
2177
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
2178
class InterKnitRepo(InterSameDataRepository):
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2179
    """Optimised code paths between Knit based repositories."""
2180
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
2181
    @classmethod
2241.1.7 by Martin Pool
rename method
2182
    def _get_repo_format_to_test(self):
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
2183
        from bzrlib.repofmt import knitrepo
2184
        return knitrepo.RepositoryFormatKnit1()
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2185
2186
    @staticmethod
2187
    def is_compatible(source, target):
2188
        """Be compatible with known Knit formats.
2189
        
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
2190
        We don't test for the stores being of specific types because that
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2191
        could lead to confusing results, and there is no need to be 
2192
        overly general.
2193
        """
2881.4.1 by Robert Collins
Move responsibility for detecting same-repo fetching from the
2194
        from bzrlib.repofmt.knitrepo import RepositoryFormatKnit
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2195
        try:
2881.4.1 by Robert Collins
Move responsibility for detecting same-repo fetching from the
2196
            are_knits = (isinstance(source._format, RepositoryFormatKnit) and
2197
                isinstance(target._format, RepositoryFormatKnit))
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2198
        except AttributeError:
2199
            return False
2881.4.1 by Robert Collins
Move responsibility for detecting same-repo fetching from the
2200
        return are_knits and InterRepository._same_model(source, target)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2201
2202
    @needs_write_lock
2203
    def fetch(self, revision_id=None, pb=None):
2204
        """See InterRepository.fetch()."""
2205
        from bzrlib.fetch import KnitRepoFetcher
2206
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2207
               self.source, self.source._format, self.target, self.target._format)
2208
        f = KnitRepoFetcher(to_repository=self.target,
2209
                            from_repository=self.source,
2210
                            last_revision=revision_id,
2211
                            pb=pb)
2212
        return f.count_copied, f.failed_revisions
2213
2214
    @needs_read_lock
2215
    def missing_revision_ids(self, revision_id=None):
2216
        """See InterRepository.missing_revision_ids()."""
2217
        if revision_id is not None:
2218
            source_ids = self.source.get_ancestry(revision_id)
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
2219
            assert source_ids[0] is None
1668.1.14 by Martin Pool
merge olaf - InvalidRevisionId fixes
2220
            source_ids.pop(0)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2221
        else:
2850.3.1 by Robert Collins
Move various weave specific code out of the base Repository class to weaverepo.py.
2222
            source_ids = self.source.all_revision_ids()
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2223
        source_ids_set = set(source_ids)
2224
        # source_ids is the worst possible case we may need to pull.
2225
        # now we want to filter source_ids against what we actually
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
2226
        # have in target, but don't try to check for existence where we know
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2227
        # we do not have a revision as that would be pointless.
2850.3.1 by Robert Collins
Move various weave specific code out of the base Repository class to weaverepo.py.
2228
        target_ids = set(self.target.all_revision_ids())
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2229
        possibly_present_revisions = target_ids.intersection(source_ids_set)
2230
        actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
2231
        required_revisions = source_ids_set.difference(actually_present_revisions)
2232
        required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
2233
        if revision_id is not None:
2234
            # we used get_ancestry to determine source_ids then we are assured all
2235
            # revisions referenced are present as they are installed in topological order.
2236
            # and the tip revision was validated by get_ancestry.
2237
            return required_topo_revisions
2238
        else:
2239
            # if we just grabbed the possibly available ids, then 
2240
            # we only have an estimate of whats available and need to validate
2241
            # that against the revision records.
2242
            return self.source._eliminate_revisions_not_present(required_topo_revisions)
2243
1910.2.17 by Aaron Bentley
Get fetching from 1 to 2 under test
2244
1910.2.24 by Aaron Bentley
Got intra-repository fetch working between model1 and 2 for all types
2245
class InterModel1and2(InterRepository):
2246
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
2247
    @classmethod
2241.1.7 by Martin Pool
rename method
2248
    def _get_repo_format_to_test(self):
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
2249
        return None
1910.2.24 by Aaron Bentley
Got intra-repository fetch working between model1 and 2 for all types
2250
2251
    @staticmethod
2252
    def is_compatible(source, target):
2305.2.1 by Andrew Bennetts
Use repo.supports_rich_root() everywhere rather than
2253
        if not source.supports_rich_root() and target.supports_rich_root():
1910.2.24 by Aaron Bentley
Got intra-repository fetch working between model1 and 2 for all types
2254
            return True
2255
        else:
2256
            return False
2257
2258
    @needs_write_lock
2259
    def fetch(self, revision_id=None, pb=None):
2260
        """See InterRepository.fetch()."""
2261
        from bzrlib.fetch import Model1toKnit2Fetcher
2262
        f = Model1toKnit2Fetcher(to_repository=self.target,
2263
                                 from_repository=self.source,
2264
                                 last_revision=revision_id,
2265
                                 pb=pb)
2266
        return f.count_copied, f.failed_revisions
2267
1910.2.26 by Aaron Bentley
Fix up some test cases
2268
    @needs_write_lock
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
2269
    def copy_content(self, revision_id=None):
1910.2.26 by Aaron Bentley
Fix up some test cases
2270
        """Make a complete copy of the content in self into destination.
2271
        
2272
        This is a destructive operation! Do not use it on existing 
2273
        repositories.
2274
2275
        :param revision_id: Only copy the content needed to construct
2276
                            revision_id and its parents.
2277
        """
2278
        try:
2279
            self.target.set_make_working_trees(self.source.make_working_trees())
2280
        except NotImplementedError:
2281
            pass
2282
        # but don't bother fetching if we have the needed data now.
1996.3.20 by John Arbash Meinel
[merge] bzr.dev 2063
2283
        if (revision_id not in (None, _mod_revision.NULL_REVISION) and 
1910.2.26 by Aaron Bentley
Fix up some test cases
2284
            self.target.has_revision(revision_id)):
2285
            return
2286
        self.target.fetch(self.source, revision_id=revision_id)
2287
1910.2.24 by Aaron Bentley
Got intra-repository fetch working between model1 and 2 for all types
2288
1910.2.17 by Aaron Bentley
Get fetching from 1 to 2 under test
2289
class InterKnit1and2(InterKnitRepo):
2290
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
2291
    @classmethod
2241.1.7 by Martin Pool
rename method
2292
    def _get_repo_format_to_test(self):
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
2293
        return None
1910.2.24 by Aaron Bentley
Got intra-repository fetch working between model1 and 2 for all types
2294
1910.2.17 by Aaron Bentley
Get fetching from 1 to 2 under test
2295
    @staticmethod
2296
    def is_compatible(source, target):
2255.2.211 by Robert Collins
Remove knit2 repository format- it has never been supported.
2297
        """Be compatible with Knit1 source and Knit3 target"""
2298
        from bzrlib.repofmt.knitrepo import RepositoryFormatKnit3
1910.2.17 by Aaron Bentley
Get fetching from 1 to 2 under test
2299
        try:
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
2300
            from bzrlib.repofmt.knitrepo import RepositoryFormatKnit1, \
2255.2.211 by Robert Collins
Remove knit2 repository format- it has never been supported.
2301
                    RepositoryFormatKnit3
1910.2.17 by Aaron Bentley
Get fetching from 1 to 2 under test
2302
            return (isinstance(source._format, (RepositoryFormatKnit1)) and
2255.2.211 by Robert Collins
Remove knit2 repository format- it has never been supported.
2303
                    isinstance(target._format, (RepositoryFormatKnit3)))
1910.2.17 by Aaron Bentley
Get fetching from 1 to 2 under test
2304
        except AttributeError:
2305
            return False
2306
2307
    @needs_write_lock
2308
    def fetch(self, revision_id=None, pb=None):
2309
        """See InterRepository.fetch()."""
2310
        from bzrlib.fetch import Knit1to2Fetcher
2311
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2312
               self.source, self.source._format, self.target, 
2313
               self.target._format)
2314
        f = Knit1to2Fetcher(to_repository=self.target,
2315
                            from_repository=self.source,
2316
                            last_revision=revision_id,
2317
                            pb=pb)
2318
        return f.count_copied, f.failed_revisions
2319
2320
2535.3.12 by Andrew Bennetts
Add a first cut of a get_data_stream method to Repository.
2321
class InterRemoteToOther(InterRepository):
2322
2323
    def __init__(self, source, target):
2324
        InterRepository.__init__(self, source, target)
2325
        self._real_inter = None
2326
2327
    @staticmethod
2328
    def is_compatible(source, target):
2535.3.41 by Andrew Bennetts
Add tests for InterRemoteToOther.is_compatible.
2329
        if not isinstance(source, remote.RemoteRepository):
2330
            return False
2331
        source._ensure_real()
2332
        real_source = source._real_repository
2535.3.21 by Andrew Bennetts
All tests passing.
2333
        # Is source's model compatible with target's model, and are they the
2334
        # same format?  Currently we can only optimise fetching from an
2335
        # identical model & format repo.
2535.3.41 by Andrew Bennetts
Add tests for InterRemoteToOther.is_compatible.
2336
        assert not isinstance(real_source, remote.RemoteRepository), (
2337
            "We don't support remote repos backed by remote repos yet.")
2338
        return real_source._format == target._format
2339
2340
    @needs_write_lock
2341
    def fetch(self, revision_id=None, pb=None):
2342
        """See InterRepository.fetch()."""
2343
        from bzrlib.fetch import RemoteToOtherFetcher
2344
        mutter("Using fetch logic to copy between %s(remote) and %s(%s)",
2345
               self.source, self.target, self.target._format)
2346
        # TODO: jam 20070210 This should be an assert, not a translate
2347
        revision_id = osutils.safe_revision_id(revision_id)
2348
        f = RemoteToOtherFetcher(to_repository=self.target,
2349
                                 from_repository=self.source,
2350
                                 last_revision=revision_id,
2351
                                 pb=pb)
2352
        return f.count_copied, f.failed_revisions
2535.3.12 by Andrew Bennetts
Add a first cut of a get_data_stream method to Repository.
2353
2354
    @classmethod
2355
    def _get_repo_format_to_test(self):
2356
        return None
2357
2358
2359
class InterOtherToRemote(InterRepository):
2360
2361
    def __init__(self, source, target):
2362
        InterRepository.__init__(self, source, target)
2363
        self._real_inter = None
2364
2365
    @staticmethod
2366
    def is_compatible(source, target):
2367
        if isinstance(target, remote.RemoteRepository):
2368
            return True
2369
        return False
2370
2371
    def _ensure_real_inter(self):
2372
        if self._real_inter is None:
2373
            self.target._ensure_real()
2374
            real_target = self.target._real_repository
2375
            self._real_inter = InterRepository.get(self.source, real_target)
2376
    
2377
    def copy_content(self, revision_id=None):
2378
        self._ensure_real_inter()
2379
        self._real_inter.copy_content(revision_id=revision_id)
2380
2381
    def fetch(self, revision_id=None, pb=None):
2382
        self._ensure_real_inter()
2383
        self._real_inter.fetch(revision_id=revision_id, pb=pb)
2384
2385
    @classmethod
2386
    def _get_repo_format_to_test(self):
2387
        return None
2388
2389
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
2390
InterRepository.register_optimiser(InterSameDataRepository)
2241.1.13 by Martin Pool
Re-register InterWeaveRepo, fix test integration, add test for it
2391
InterRepository.register_optimiser(InterWeaveRepo)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2392
InterRepository.register_optimiser(InterKnitRepo)
1910.2.24 by Aaron Bentley
Got intra-repository fetch working between model1 and 2 for all types
2393
InterRepository.register_optimiser(InterModel1and2)
1910.2.17 by Aaron Bentley
Get fetching from 1 to 2 under test
2394
InterRepository.register_optimiser(InterKnit1and2)
2535.3.12 by Andrew Bennetts
Add a first cut of a get_data_stream method to Repository.
2395
InterRepository.register_optimiser(InterRemoteToOther)
2396
InterRepository.register_optimiser(InterOtherToRemote)
1534.1.31 by Robert Collins
Deprecated fetch.fetch and fetch.greedy_fetch for branch.fetch, and move the Repository.fetch internals to InterRepo and InterWeaveRepo.
2397
2398
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
2399
class CopyConverter(object):
2400
    """A repository conversion tool which just performs a copy of the content.
2401
    
2402
    This is slow but quite reliable.
2403
    """
2404
2405
    def __init__(self, target_format):
2406
        """Create a CopyConverter.
2407
2408
        :param target_format: The format the resulting repository should be.
2409
        """
2410
        self.target_format = target_format
2411
        
2412
    def convert(self, repo, pb):
2413
        """Perform the conversion of to_convert, giving feedback via pb.
2414
2415
        :param to_convert: The disk object to convert.
2416
        :param pb: a progress bar to use for progress information.
2417
        """
2418
        self.pb = pb
2419
        self.count = 0
1596.2.22 by Robert Collins
Fetch changes to use new pb.
2420
        self.total = 4
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
2421
        # this is only useful with metadir layouts - separated repo content.
2422
        # trigger an assertion if not such
2423
        repo._format.get_format_string()
2424
        self.repo_dir = repo.bzrdir
2425
        self.step('Moving repository to repository.backup')
2426
        self.repo_dir.transport.move('repository', 'repository.backup')
2427
        backup_transport =  self.repo_dir.transport.clone('repository.backup')
1910.2.12 by Aaron Bentley
Implement knit repo format 2
2428
        repo._format.check_conversion_target(self.target_format)
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
2429
        self.source_repo = repo._format.open(self.repo_dir,
2430
            _found=True,
2431
            _override_transport=backup_transport)
2432
        self.step('Creating new repository')
2433
        converted = self.target_format.initialize(self.repo_dir,
2434
                                                  self.source_repo.is_shared())
2435
        converted.lock_write()
2436
        try:
2437
            self.step('Copying content into repository.')
2438
            self.source_repo.copy_content_into(converted)
2439
        finally:
2440
            converted.unlock()
2441
        self.step('Deleting old repository content.')
2442
        self.repo_dir.transport.delete_tree('repository.backup')
2443
        self.pb.note('repository converted')
2444
2445
    def step(self, message):
2446
        """Update the pb by a step."""
2447
        self.count +=1
2448
        self.pb.update(message, self.count, self.total)
1596.1.1 by Martin Pool
Use simple xml unescaping rather than importing xml.sax
2449
2450
1843.2.4 by Aaron Bentley
Switch to John Meinel's _unescape_xml implementation
2451
_unescape_map = {
2452
    'apos':"'",
2453
    'quot':'"',
2454
    'amp':'&',
2455
    'lt':'<',
2456
    'gt':'>'
2457
}
2458
2459
2460
def _unescaper(match, _map=_unescape_map):
2294.1.2 by John Arbash Meinel
Track down and add tests that all tree.commit() can handle
2461
    code = match.group(1)
2462
    try:
2463
        return _map[code]
2464
    except KeyError:
2465
        if not code.startswith('#'):
2466
            raise
2294.1.10 by John Arbash Meinel
Switch all apis over to utf8 file ids. All tests pass
2467
        return unichr(int(code[1:])).encode('utf8')
1843.2.4 by Aaron Bentley
Switch to John Meinel's _unescape_xml implementation
2468
2469
2470
_unescape_re = None
2471
2472
1596.1.1 by Martin Pool
Use simple xml unescaping rather than importing xml.sax
2473
def _unescape_xml(data):
1843.2.4 by Aaron Bentley
Switch to John Meinel's _unescape_xml implementation
2474
    """Unescape predefined XML entities in a string of data."""
2475
    global _unescape_re
2476
    if _unescape_re is None:
2120.2.1 by John Arbash Meinel
Remove tabs from source files, and add a test to keep it that way.
2477
        _unescape_re = re.compile('\&([^;]*);')
1843.2.4 by Aaron Bentley
Switch to John Meinel's _unescape_xml implementation
2478
    return _unescape_re.sub(_unescaper, data)
2745.6.3 by Aaron Bentley
Implement versionedfile checking for bzr check
2479
2480
2745.6.16 by Aaron Bentley
Update from review
2481
class _RevisionTextVersionCache(object):
2745.6.57 by Andrew Bennetts
Some improvements suggested by Martin's review.
2482
    """A cache of the versionedfile versions for revision and file-id."""
2745.6.16 by Aaron Bentley
Update from review
2483
2484
    def __init__(self, repository):
2485
        self.repository = repository
2486
        self.revision_versions = {}
2906.1.1 by Andrew Bennetts
Speed up reconcile by not repeatedly fetching the full inventories, by cache heads and parents queries, and by fetching revision trees in batches.
2487
        self.revision_parents = {}
2488
        self.repo_graph = self.repository.get_graph()
2489
        self.rev_heads = {}
2745.6.16 by Aaron Bentley
Update from review
2490
2745.6.53 by Andrew Bennetts
Some more changes suggested by review.
2491
    def add_revision_text_versions(self, tree):
2745.6.16 by Aaron Bentley
Update from review
2492
        """Cache text version data from the supplied revision tree"""
2493
        inv_revisions = {}
2494
        for path, entry in tree.iter_entries_by_dir():
2495
            inv_revisions[entry.file_id] = entry.revision
2496
        self.revision_versions[tree.get_revision_id()] = inv_revisions
2497
        return inv_revisions
2498
2499
    def get_text_version(self, file_id, revision_id):
2500
        """Determine the text version for a given file-id and revision-id"""
2501
        try:
2502
            inv_revisions = self.revision_versions[revision_id]
2503
        except KeyError:
2906.1.1 by Andrew Bennetts
Speed up reconcile by not repeatedly fetching the full inventories, by cache heads and parents queries, and by fetching revision trees in batches.
2504
            try:
2505
                tree = self.repository.revision_tree(revision_id)
2506
            except errors.RevisionNotPresent:
2507
                self.revision_versions[revision_id] = inv_revisions = {}
2508
            else:
2509
                inv_revisions = self.add_revision_text_versions(tree)
2745.6.16 by Aaron Bentley
Update from review
2510
        return inv_revisions.get(file_id)
2511
2906.1.1 by Andrew Bennetts
Speed up reconcile by not repeatedly fetching the full inventories, by cache heads and parents queries, and by fetching revision trees in batches.
2512
    def prepopulate_revs(self, revision_ids):
2513
        # Filter out versions that we don't have an inventory for, so that the
2514
        # revision_trees() call won't fail.
2515
        inv_weave = self.repository.get_inventory_weave()
2516
        revs = [r for r in revision_ids if inv_weave.has_version(r)]
2517
        # XXX: this loop is very similar to
2518
        # bzrlib.fetch.Inter1and2Helper.iter_rev_trees.
2519
        while revs:
2520
            for tree in self.repository.revision_trees(revs[:100]):
2521
                if tree.inventory.revision_id is None:
2522
                    tree.inventory.revision_id = tree.get_revision_id()
2523
                self.add_revision_text_versions(tree)
2524
            revs = revs[100:]
2525
2526
    def get_parents(self, revision_id):
2527
        try:
2528
            return self.revision_parents[revision_id]
2529
        except KeyError:
2530
            parents = self.repository.get_parents([revision_id])[0]
2531
            self.revision_parents[revision_id] = parents
2532
            return parents
2533
2534
    def heads(self, revision_ids):
2535
        revision_ids = tuple(revision_ids)
2536
        try:
2537
            return self.rev_heads[revision_ids]
2538
        except KeyError:
2539
            heads = self.repo_graph.heads(revision_ids)
2540
            self.rev_heads[revision_ids] = heads
2541
            return heads
2745.6.16 by Aaron Bentley
Update from review
2542
2745.6.57 by Andrew Bennetts
Some improvements suggested by Martin's review.
2543
class VersionedFileChecker(object):
2745.6.47 by Andrew Bennetts
Move check_parents out of VersionedFile.
2544
2545
    def __init__(self, planned_revisions, revision_versions, repository):
2546
        self.planned_revisions = planned_revisions
2547
        self.revision_versions = revision_versions
2548
        self.repository = repository
2745.6.49 by Andrew Bennetts
Get rid of bzrlib.repository._RevisionParentsProvider.
2549
    
2550
    def calculate_file_version_parents(self, revision_id, file_id):
2745.6.47 by Andrew Bennetts
Move check_parents out of VersionedFile.
2551
        text_revision = self.revision_versions.get_text_version(
2552
            file_id, revision_id)
2553
        if text_revision is None:
2554
            return None
2906.1.1 by Andrew Bennetts
Speed up reconcile by not repeatedly fetching the full inventories, by cache heads and parents queries, and by fetching revision trees in batches.
2555
        parents_of_text_revision = self.revision_versions.get_parents(
2556
            text_revision)
2745.6.47 by Andrew Bennetts
Move check_parents out of VersionedFile.
2557
        parents_from_inventories = []
2558
        for parent in parents_of_text_revision:
2559
            if parent == _mod_revision.NULL_REVISION:
2560
                continue
2906.1.1 by Andrew Bennetts
Speed up reconcile by not repeatedly fetching the full inventories, by cache heads and parents queries, and by fetching revision trees in batches.
2561
            introduced_in = self.revision_versions.get_text_version(file_id,
2562
                    parent)
2563
            if introduced_in is not None:
2564
                parents_from_inventories.append(introduced_in)
2565
        heads = set(self.revision_versions.heads(parents_from_inventories))
2745.6.47 by Andrew Bennetts
Move check_parents out of VersionedFile.
2566
        new_parents = []
2567
        for parent in parents_from_inventories:
2568
            if parent in heads and parent not in new_parents:
2569
                new_parents.append(parent)
2570
        return new_parents
2571
2745.6.49 by Andrew Bennetts
Get rid of bzrlib.repository._RevisionParentsProvider.
2572
    def check_file_version_parents(self, weave, file_id):
2745.6.47 by Andrew Bennetts
Move check_parents out of VersionedFile.
2573
        result = {}
2574
        for num, revision_id in enumerate(self.planned_revisions):
2575
            correct_parents = self.calculate_file_version_parents(
2745.6.49 by Andrew Bennetts
Get rid of bzrlib.repository._RevisionParentsProvider.
2576
                revision_id, file_id)
2745.6.47 by Andrew Bennetts
Move check_parents out of VersionedFile.
2577
            if correct_parents is None:
2578
                continue
2579
            text_revision = self.revision_versions.get_text_version(
2580
                file_id, revision_id)
2581
            knit_parents = weave.get_parents(text_revision)
2582
            if correct_parents != knit_parents:
2583
                result[revision_id] = (knit_parents, correct_parents)
2584
        return result