/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
1
# Copyright (C) 2005, 2006 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
from binascii import hexlify
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
22
from copy import deepcopy
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
23
import re
24
import time
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
25
import unittest
26
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
27
from bzrlib import (
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
28
    bzrdir,
29
    check,
30
    delta,
31
    errors,
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
32
    generate_ids,
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
33
    gpg,
34
    graph,
1996.3.5 by John Arbash Meinel
Cleanup, deprecated, and get the tests passing again.
35
    knit,
2163.2.1 by John Arbash Meinel
Speed up the fileids_altered_by_revision_ids processing
36
    lazy_regex,
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
37
    lockable_files,
38
    lockdir,
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
39
    osutils,
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
40
    revision as _mod_revision,
41
    symbol_versioning,
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
42
    transactions,
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
43
    ui,
44
    weave,
45
    weavefile,
46
    xml5,
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
47
    xml6,
48
    )
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
49
from bzrlib.osutils import (
50
    rand_bytes,
51
    compact_date, 
52
    local_time_offset,
53
    )
54
from bzrlib.revisiontree import RevisionTree
55
from bzrlib.store.versioned import VersionedFileStore
56
from bzrlib.store.text import TextStore
57
from bzrlib.testament import Testament
58
""")
59
1534.4.28 by Robert Collins
first cut at merge from integration.
60
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.
61
from bzrlib.inter import InterObject
1910.2.3 by Aaron Bentley
All tests pass
62
from bzrlib.inventory import Inventory, InventoryDirectory, ROOT_ID
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
63
from bzrlib.symbol_versioning import (
64
        deprecated_method,
65
        zero_nine,
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
66
        )
1904.2.3 by Martin Pool
Give a warning on access to old repository formats
67
from bzrlib.trace import mutter, note, warning
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
68
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
69
1904.2.5 by Martin Pool
Fix format warning inside test suite and add test
70
# Old formats display a warning, but only once
71
_deprecation_warning_done = False
72
73
1185.66.5 by Aaron Bentley
Renamed RevisionStorage to Repository
74
class Repository(object):
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
75
    """Repository holding history for one or more branches.
76
77
    The repository holds and retrieves historical information including
78
    revisions and file history.  It's normally accessed only by the Branch,
79
    which views a particular line of development through that history.
80
81
    The Repository builds on top of Stores and a Transport, which respectively 
82
    describe the disk data format and the way of accessing the (possibly 
83
    remote) disk.
84
    """
1185.65.17 by Robert Collins
Merge from integration, mode-changes are broken.
85
2163.2.1 by John Arbash Meinel
Speed up the fileids_altered_by_revision_ids processing
86
    _file_ids_altered_regex = lazy_regex.lazy_compile(
87
        r'file_id="(?P<file_id>[^"]+)"'
88
        r'.*revision="(?P<revision_id>[^"]+)"'
89
        )
90
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
91
    @needs_write_lock
92
    def add_inventory(self, revid, inv, parents):
93
        """Add the inventory inv to the repository as revid.
94
        
95
        :param parents: The revision ids of the parents that revid
96
                        is known to have and are in the repository already.
97
98
        returns the sha1 of the serialized inventory.
99
        """
2229.2.1 by Aaron Bentley
Reject reserved ids in versiondfile, tree, branch and repository
100
        if _mod_revision.reserved_id(revid):
101
            raise errors.ReservedId(revid)
1740.2.2 by Aaron Bentley
Add test for the basis inventory automatically adding the revision id.
102
        assert inv.revision_id is None or inv.revision_id == revid, \
103
            "Mismatch between inventory revision" \
104
            " id and insertion revid (%r, %r)" % (inv.revision_id, revid)
1910.2.6 by Aaron Bentley
Update for merge review, handle deprecations
105
        assert inv.root is not None
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
106
        inv_text = self.serialise_inventory(inv)
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
107
        inv_sha1 = osutils.sha_string(inv_text)
1563.2.25 by Robert Collins
Merge in upstream.
108
        inv_vf = self.control_weaves.get_weave('inventory',
109
                                               self.get_transaction())
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
110
        self._inventory_add_lines(inv_vf, revid, parents, osutils.split_lines(inv_text))
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
111
        return inv_sha1
112
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
113
    def _inventory_add_lines(self, inv_vf, revid, parents, lines):
114
        final_parents = []
115
        for parent in parents:
116
            if parent in inv_vf:
117
                final_parents.append(parent)
118
119
        inv_vf.add_lines(revid, final_parents, lines)
120
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
121
    @needs_write_lock
122
    def add_revision(self, rev_id, rev, inv=None, config=None):
123
        """Add rev to the revision store as rev_id.
124
125
        :param rev_id: the revision id to use.
126
        :param rev: The revision object.
127
        :param inv: The inventory for the revision. if None, it will be looked
128
                    up in the inventory storer
129
        :param config: If None no digital signature will be created.
130
                       If supplied its signature_needed method will be used
131
                       to determine if a signature should be made.
132
        """
2229.2.1 by Aaron Bentley
Reject reserved ids in versiondfile, tree, branch and repository
133
        if _mod_revision.reserved_id(rev_id):
134
            raise errors.ReservedId(rev_id)
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
135
        if config is not None and config.signature_needed():
136
            if inv is None:
137
                inv = self.get_inventory(rev_id)
138
            plaintext = Testament(rev, inv).as_short_text()
139
            self.store_revision_signature(
140
                gpg.GPGStrategy(config), plaintext, rev_id)
141
        if not rev_id in self.get_inventory_weave():
142
            if inv is None:
143
                raise errors.WeaveRevisionNotPresent(rev_id,
144
                                                     self.get_inventory_weave())
145
            else:
146
                # yes, this is not suitable for adding with ghosts.
147
                self.add_inventory(rev_id, inv, rev.parent_ids)
1608.2.1 by Martin Pool
[merge] Storage filename escaping
148
        self._revision_store.add_revision(rev, self.get_transaction())
1570.1.2 by Robert Collins
Import bzrtools' 'fix' command as 'bzr reconcile.'
149
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
150
    @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.
151
    def _all_possible_ids(self):
152
        """Return all the possible revisions that we could find."""
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
153
        return self.get_inventory_weave().versions()
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.
154
1732.2.4 by Martin Pool
Split check into Branch.check and Repository.check
155
    def all_revision_ids(self):
156
        """Returns a list of all the revision ids in the repository. 
157
158
        This is deprecated because code should generally work on the graph
159
        reachable from a particular revision, and ignore any other revisions
160
        that might be present.  There is no direct replacement method.
161
        """
162
        return self._all_revision_ids()
163
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.
164
    @needs_read_lock
1732.2.4 by Martin Pool
Split check into Branch.check and Repository.check
165
    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.
166
        """Returns a list of all the revision ids in the repository. 
167
168
        These are in as much topological order as the underlying store can 
169
        present: for weaves ghosts may lead to a lack of correctness until
170
        the reweave updates the parents list.
171
        """
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
172
        if self._revision_store.text_store.listable():
173
            return self._revision_store.all_revision_ids(self.get_transaction())
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.
174
        result = self._all_possible_ids()
175
        return self._eliminate_revisions_not_present(result)
176
1687.1.7 by Robert Collins
Teach Repository about break_lock.
177
    def break_lock(self):
178
        """Break a lock if one is present from another instance.
179
180
        Uses the ui factory to ask for confirmation if the lock may be from
181
        an active process.
182
        """
183
        self.control_files.break_lock()
184
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.
185
    @needs_read_lock
186
    def _eliminate_revisions_not_present(self, revision_ids):
187
        """Check every revision id in revision_ids to see if we have it.
188
189
        Returns a set of the present revisions.
190
        """
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
191
        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.
192
        for id in revision_ids:
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
193
            if self.has_revision(id):
194
               result.append(id)
195
        return result
196
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
197
    @staticmethod
198
    def create(a_bzrdir):
199
        """Construct the current default format repository in a_bzrdir."""
200
        return RepositoryFormat.get_default_format().initialize(a_bzrdir)
201
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
202
    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
203
        """instantiate a Repository.
204
205
        :param _format: The format of the repository on disk.
206
        :param a_bzrdir: The BzrDir of the repository.
207
208
        In the future we will have a single api for all stores for
209
        getting file texts, inventories and revisions, then
210
        this construct will accept instances of those things.
211
        """
1608.2.1 by Martin Pool
[merge] Storage filename escaping
212
        super(Repository, self).__init__()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
213
        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
214
        # 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.
215
        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
216
        self.control_files = control_files
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
217
        self._revision_store = _revision_store
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
218
        self.text_store = text_store
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
219
        # backwards compatibility
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
220
        self.weave_store = text_store
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
221
        # not right yet - should be more semantically clear ? 
222
        # 
223
        self.control_store = control_store
224
        self.control_weaves = control_store
1608.2.1 by Martin Pool
[merge] Storage filename escaping
225
        # TODO: make sure to construct the right store classes, etc, depending
226
        # on whether escaping is required.
1904.2.3 by Martin Pool
Give a warning on access to old repository formats
227
        self._warn_if_deprecated()
1910.2.48 by Aaron Bentley
Update from review comments
228
        self._serializer = xml5.serializer_v5
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
229
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
230
    def __repr__(self):
231
        return '%s(%r)' % (self.__class__.__name__, 
232
                           self.bzrdir.transport.base)
233
1694.2.6 by Martin Pool
[merge] bzr.dev
234
    def is_locked(self):
235
        return self.control_files.is_locked()
236
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
237
    def lock_write(self):
238
        self.control_files.lock_write()
239
240
    def lock_read(self):
1553.5.55 by Martin Pool
[revert] broken changes
241
        self.control_files.lock_read()
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
242
1694.2.6 by Martin Pool
[merge] bzr.dev
243
    def get_physical_lock_status(self):
244
        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
245
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.
246
    @needs_read_lock
247
    def missing_revision_ids(self, other, revision_id=None):
248
        """Return the revision ids that other has that this does not.
249
        
250
        These are returned in topological order.
251
252
        revision_id: only return revision ids included by revision_id.
253
        """
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
254
        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.
255
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
256
    @staticmethod
257
    def open(base):
258
        """Open the repository rooted at base.
259
260
        For instance, if the repository is at URL/.bzr/repository,
261
        Repository.open(URL) -> a Repository instance.
262
        """
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
263
        control = bzrdir.BzrDir.open(base)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
264
        return control.open_repository()
265
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.
266
    def copy_content_into(self, destination, revision_id=None, basis=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.
267
        """Make a complete copy of the content in self into destination.
268
        
269
        This is a destructive operation! Do not use it on existing 
270
        repositories.
271
        """
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.
272
        return InterRepository.get(self, destination).copy_content(revision_id, basis)
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.
273
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.
274
    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.
275
        """Fetch the content required to construct revision_id from source.
276
277
        If revision_id is None all content is copied.
278
        """
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.
279
        return InterRepository.get(source, self).fetch(revision_id=revision_id,
280
                                                       pb=pb)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
281
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
282
    def get_commit_builder(self, branch, parents, config, timestamp=None, 
283
                           timezone=None, committer=None, revprops=None, 
284
                           revision_id=None):
285
        """Obtain a CommitBuilder for this repository.
286
        
287
        :param branch: Branch to commit to.
288
        :param parents: Revision ids of the parents of the new revision.
289
        :param config: Configuration to use.
290
        :param timestamp: Optional timestamp recorded for commit.
291
        :param timezone: Optional timezone for timestamp.
292
        :param committer: Optional committer to set for commit.
293
        :param revprops: Optional dictionary of revision properties.
294
        :param revision_id: Optional revision id.
295
        """
1910.2.6 by Aaron Bentley
Update for merge review, handle deprecations
296
        return _CommitBuilder(self, parents, config, timestamp, timezone,
297
                              committer, revprops, revision_id)
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
298
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
299
    def unlock(self):
300
        self.control_files.unlock()
301
1185.65.27 by Robert Collins
Tweak storage towards mergability.
302
    @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.
303
    def clone(self, a_bzrdir, revision_id=None, basis=None):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
304
        """Clone this repository into a_bzrdir using the current format.
305
306
        Currently no check is made that the format of this repository and
307
        the bzrdir format are compatible. FIXME RBC 20060201.
308
        """
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.
309
        if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
310
            # use target default format.
311
            result = a_bzrdir.create_repository()
312
        # FIXME RBC 20060209 split out the repository type to avoid this check ?
313
        elif isinstance(a_bzrdir._format,
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
314
                      (bzrdir.BzrDirFormat4,
315
                       bzrdir.BzrDirFormat5,
316
                       bzrdir.BzrDirFormat6)):
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.
317
            result = a_bzrdir.open_repository()
318
        else:
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
319
            result = self._format.initialize(a_bzrdir, shared=self.is_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.
320
        self.copy_content_into(result, revision_id, basis)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
321
        return result
322
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
323
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
324
    def has_revision(self, revision_id):
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
325
        """True if this repository has a copy of the revision."""
326
        return self._revision_store.has_revision_id(revision_id,
327
                                                    self.get_transaction())
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
328
1185.65.27 by Robert Collins
Tweak storage towards mergability.
329
    @needs_read_lock
1570.1.13 by Robert Collins
Check for incorrect revision parentage in the weave during revision access.
330
    def get_revision_reconcile(self, revision_id):
331
        """'reconcile' helper routine that allows access to a revision always.
332
        
333
        This variant of get_revision does not cross check the weave graph
334
        against the revision one as get_revision does: but it should only
335
        be used by reconcile, or reconcile-alike commands that are correcting
336
        or testing the revision graph.
337
        """
1563.2.25 by Robert Collins
Merge in upstream.
338
        if not revision_id or not isinstance(revision_id, basestring):
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
339
            raise errors.InvalidRevisionId(revision_id=revision_id,
340
                                           branch=self)
1756.1.2 by Aaron Bentley
Show logs using get_revisions
341
        return self._revision_store.get_revisions([revision_id],
342
                                                  self.get_transaction())[0]
343
    @needs_read_lock
344
    def get_revisions(self, revision_ids):
345
        return self._revision_store.get_revisions(revision_ids,
346
                                                  self.get_transaction())
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
347
1185.65.27 by Robert Collins
Tweak storage towards mergability.
348
    @needs_read_lock
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
349
    def get_revision_xml(self, revision_id):
350
        rev = self.get_revision(revision_id) 
351
        rev_tmp = StringIO()
352
        # the current serializer..
353
        self._revision_store._serializer.write_revision(rev, rev_tmp)
354
        rev_tmp.seek(0)
355
        return rev_tmp.getvalue()
356
357
    @needs_read_lock
1570.1.13 by Robert Collins
Check for incorrect revision parentage in the weave during revision access.
358
    def get_revision(self, revision_id):
359
        """Return the Revision object for a named revision"""
360
        r = self.get_revision_reconcile(revision_id)
361
        # weave corruption can lead to absent revision markers that should be
362
        # present.
363
        # the following test is reasonably cheap (it needs a single weave read)
364
        # and the weave is cached in read transactions. In write transactions
365
        # it is not cached but typically we only read a small number of
366
        # revisions. For knits when they are introduced we will probably want
367
        # to ensure that caching write transactions are in use.
368
        inv = self.get_inventory_weave()
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
369
        self._check_revision_parents(r, inv)
370
        return r
371
1756.3.19 by Aaron Bentley
Documentation and cleanups
372
    @needs_read_lock
1756.3.22 by Aaron Bentley
Tweaks from review
373
    def get_deltas_for_revisions(self, revisions):
1756.3.19 by Aaron Bentley
Documentation and cleanups
374
        """Produce a generator of revision deltas.
375
        
376
        Note that the input is a sequence of REVISIONS, not revision_ids.
377
        Trees will be held in memory until the generator exits.
378
        Each delta is relative to the revision's lefthand predecessor.
379
        """
1756.3.3 by Aaron Bentley
More refactoring, introduce revision_trees.
380
        required_trees = set()
381
        for revision in revisions:
382
            required_trees.add(revision.revision_id)
383
            required_trees.update(revision.parent_ids[:1])
384
        trees = dict((t.get_revision_id(), t) for 
385
                     t in self.revision_trees(required_trees))
386
        for revision in revisions:
387
            if not revision.parent_ids:
1852.5.1 by Robert Collins
Deprecate EmptyTree in favour of using Repository.revision_tree.
388
                old_tree = self.revision_tree(None)
1756.3.3 by Aaron Bentley
More refactoring, introduce revision_trees.
389
            else:
390
                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.
391
            yield trees[revision.revision_id].changes_from(old_tree)
1756.3.3 by Aaron Bentley
More refactoring, introduce revision_trees.
392
1756.3.19 by Aaron Bentley
Documentation and cleanups
393
    @needs_read_lock
1744.2.2 by Johan Rydberg
Add get_revision_delta to Repository; and make Branch.get_revision_delta use it.
394
    def get_revision_delta(self, revision_id):
395
        """Return the delta for one revision.
396
397
        The delta is relative to the left-hand predecessor of the
398
        revision.
399
        """
1756.3.3 by Aaron Bentley
More refactoring, introduce revision_trees.
400
        r = self.get_revision(revision_id)
1756.3.22 by Aaron Bentley
Tweaks from review
401
        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.
402
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
403
    def _check_revision_parents(self, revision, inventory):
404
        """Private to Repository and Fetch.
405
        
406
        This checks the parentage of revision in an inventory weave for 
407
        consistency and is only applicable to inventory-weave-for-ancestry
408
        using repository formats & fetchers.
409
        """
1563.2.25 by Robert Collins
Merge in upstream.
410
        weave_parents = inventory.get_parents(revision.revision_id)
411
        weave_names = inventory.versions()
1570.1.14 by Robert Collins
Enforce repository consistency during 'fetch' operations.
412
        for parent_id in revision.parent_ids:
1570.1.13 by Robert Collins
Check for incorrect revision parentage in the weave during revision access.
413
            if parent_id in weave_names:
414
                # this parent must not be a ghost.
415
                if not parent_id in weave_parents:
416
                    # but it is a ghost
417
                    raise errors.CorruptRepository(self)
418
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
419
    @needs_write_lock
420
    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.
421
        signature = gpg_strategy.sign(plaintext)
422
        self._revision_store.add_revision_signature_text(revision_id,
423
                                                         signature,
424
                                                         self.get_transaction())
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
425
1694.2.6 by Martin Pool
[merge] bzr.dev
426
    def fileids_altered_by_revision_ids(self, revision_ids):
427
        """Find the file ids and versions affected by revisions.
428
429
        :param revisions: an iterable containing revision ids.
430
        :return: a dictionary mapping altered file-ids to an iterable of
431
        revision_ids. Each altered file-ids has the exact revision_ids that
432
        altered it listed explicitly.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
433
        """
1910.2.48 by Aaron Bentley
Update from review comments
434
        assert self._serializer.support_altered_by_hack, \
1732.2.1 by Martin Pool
Remove obsolete fileid_involved from KnitRepository, fix error message.
435
            ("fileids_altered_by_revision_ids only supported for branches " 
436
             "which store inventory as unnested xml, not on %r" % self)
1694.2.6 by Martin Pool
[merge] bzr.dev
437
        selected_revision_ids = set(revision_ids)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
438
        w = self.get_inventory_weave()
1694.2.6 by Martin Pool
[merge] bzr.dev
439
        result = {}
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
440
1694.2.6 by Martin Pool
[merge] bzr.dev
441
        # this code needs to read every new line in every inventory for the
442
        # inventories [revision_ids]. Seeing a line twice is ok. Seeing a line
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
443
        # 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.
444
        # harmful because we are filtering by the revision id marker in the
1694.2.6 by Martin Pool
[merge] bzr.dev
445
        # 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.
446
        # 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.
447
        # only those added in an inventory in rev X can contain a revision=X
448
        # line.
2163.2.3 by John Arbash Meinel
Change to local variables to save another 300ms
449
        unescape_revid_cache = {}
450
        unescape_fileid_cache = {}
451
2163.2.5 by John Arbash Meinel
Inline the cache lookup, and explain why
452
        # jam 20061218 In a big fetch, this handles hundreds of thousands
453
        # of lines, so it has had a lot of inlining and optimizing done.
454
        # Sorry that it is a little bit messy.
2163.2.3 by John Arbash Meinel
Change to local variables to save another 300ms
455
        # Move several functions to be local variables, since this is a long
456
        # running loop.
457
        search = self._file_ids_altered_regex.search
2163.2.5 by John Arbash Meinel
Inline the cache lookup, and explain why
458
        unescape = _unescape_xml
2163.2.3 by John Arbash Meinel
Change to local variables to save another 300ms
459
        setdefault = result.setdefault
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
460
        pb = ui.ui_factory.nested_progress_bar()
461
        try:
462
            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
463
                                        selected_revision_ids, pb=pb):
464
                match = search(line)
2163.2.1 by John Arbash Meinel
Speed up the fileids_altered_by_revision_ids processing
465
                if match is None:
466
                    continue
2163.2.5 by John Arbash Meinel
Inline the cache lookup, and explain why
467
                # One call to match.group() returning multiple items is quite a
468
                # 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
469
                file_id, revision_id = match.group('file_id', 'revision_id')
2163.2.5 by John Arbash Meinel
Inline the cache lookup, and explain why
470
471
                # Inlining the cache lookups helps a lot when you make 170,000
472
                # lines and 350k ids, versus 8.4 unique ids.
473
                # Using a cache helps in 2 ways:
474
                #   1) Avoids unnecessary decoding calls
475
                #   2) Re-uses cached strings, which helps in future set and
476
                #      equality checks.
477
                # (2) is enough that removing encoding entirely along with
478
                # the cache (so we are using plain strings) results in no
479
                # performance improvement.
480
                try:
481
                    revision_id = unescape_revid_cache[revision_id]
482
                except KeyError:
483
                    unescaped = unescape(revision_id)
484
                    unescape_revid_cache[revision_id] = unescaped
485
                    revision_id = unescaped
486
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
487
                if revision_id in selected_revision_ids:
2163.2.5 by John Arbash Meinel
Inline the cache lookup, and explain why
488
                    try:
489
                        file_id = unescape_fileid_cache[file_id]
490
                    except KeyError:
491
                        unescaped = unescape(file_id)
492
                        unescape_fileid_cache[file_id] = unescaped
493
                        file_id = unescaped
2163.2.3 by John Arbash Meinel
Change to local variables to save another 300ms
494
                    setdefault(file_id, set()).add(revision_id)
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
495
        finally:
496
            pb.finished()
1694.2.6 by Martin Pool
[merge] bzr.dev
497
        return result
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
498
1185.65.27 by Robert Collins
Tweak storage towards mergability.
499
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
500
    def get_inventory_weave(self):
501
        return self.control_weaves.get_weave('inventory',
502
            self.get_transaction())
503
1185.65.27 by Robert Collins
Tweak storage towards mergability.
504
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
505
    def get_inventory(self, revision_id):
506
        """Get Inventory object by hash."""
1740.2.3 by Aaron Bentley
Only reserialize the working tree basis inventory when needed.
507
        return self.deserialise_inventory(
508
            revision_id, self.get_inventory_xml(revision_id))
509
510
    def deserialise_inventory(self, revision_id, xml):
511
        """Transform the xml into an inventory object. 
512
513
        :param revision_id: The expected revision id of the inventory.
514
        :param xml: A serialised inventory.
515
        """
1910.2.48 by Aaron Bentley
Update from review comments
516
        result = self._serializer.read_inventory_from_string(xml)
1910.2.1 by Aaron Bentley
Ensure root entry always has a revision
517
        result.root.revision = revision_id
518
        return result
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
519
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
520
    def serialise_inventory(self, inv):
1910.2.48 by Aaron Bentley
Update from review comments
521
        return self._serializer.write_inventory_to_string(inv)
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
522
1185.65.27 by Robert Collins
Tweak storage towards mergability.
523
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
524
    def get_inventory_xml(self, revision_id):
525
        """Get inventory XML as a file object."""
526
        try:
527
            assert isinstance(revision_id, basestring), type(revision_id)
528
            iw = self.get_inventory_weave()
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
529
            return iw.get_text(revision_id)
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
530
        except IndexError:
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
531
            raise errors.HistoryMissing(self, 'inventory', revision_id)
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
532
1185.65.27 by Robert Collins
Tweak storage towards mergability.
533
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
534
    def get_inventory_sha1(self, revision_id):
535
        """Return the sha1 hash of the inventory entry
536
        """
537
        return self.get_revision(revision_id).inventory_sha1
538
1185.65.27 by Robert Collins
Tweak storage towards mergability.
539
    @needs_read_lock
1590.1.1 by Robert Collins
Improve common_ancestor performance.
540
    def get_revision_graph(self, revision_id=None):
541
        """Return a dictionary containing the revision graph.
542
        
1836.3.1 by Robert Collins
(robertc) Teach repository.get_revision_graph, and revision.common_ancestor, about NULL_REVISION.
543
        :param revision_id: The revision_id to get a graph from. If None, then
544
        the entire revision graph is returned. This is a deprecated mode of
545
        operation and will be removed in the future.
1590.1.1 by Robert Collins
Improve common_ancestor performance.
546
        :return: a dictionary of revision_id->revision_parents_list.
547
        """
1836.3.1 by Robert Collins
(robertc) Teach repository.get_revision_graph, and revision.common_ancestor, about NULL_REVISION.
548
        # special case NULL_REVISION
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
549
        if revision_id == _mod_revision.NULL_REVISION:
1836.3.1 by Robert Collins
(robertc) Teach repository.get_revision_graph, and revision.common_ancestor, about NULL_REVISION.
550
            return {}
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
551
        a_weave = self.get_inventory_weave()
552
        all_revisions = self._eliminate_revisions_not_present(
553
                                a_weave.versions())
554
        entire_graph = dict([(node, a_weave.get_parents(node)) for 
1590.1.1 by Robert Collins
Improve common_ancestor performance.
555
                             node in all_revisions])
556
        if revision_id is None:
557
            return entire_graph
558
        elif revision_id not in entire_graph:
559
            raise errors.NoSuchRevision(self, revision_id)
560
        else:
561
            # add what can be reached from revision_id
562
            result = {}
563
            pending = set([revision_id])
564
            while len(pending) > 0:
565
                node = pending.pop()
566
                result[node] = entire_graph[node]
567
                for revision_id in result[node]:
568
                    if revision_id not in result:
569
                        pending.add(revision_id)
570
            return result
571
572
    @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.
573
    def get_revision_graph_with_ghosts(self, revision_ids=None):
574
        """Return a graph of the revisions with ghosts marked as applicable.
575
576
        :param revision_ids: an iterable of revisions to graph or None for all.
577
        :return: a Graph object with the graph reachable from revision_ids.
578
        """
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
579
        result = 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.
580
        if not revision_ids:
1773.4.2 by Martin Pool
Cleanup of imports; undeprecate all_revision_ids()
581
            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.
582
            required = set([])
583
        else:
584
            pending = set(revision_ids)
1836.3.1 by Robert Collins
(robertc) Teach repository.get_revision_graph, and revision.common_ancestor, about NULL_REVISION.
585
            # special case NULL_REVISION
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
586
            if _mod_revision.NULL_REVISION in pending:
587
                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.
588
            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.
589
        done = set([])
590
        while len(pending):
591
            revision_id = pending.pop()
592
            try:
593
                rev = self.get_revision(revision_id)
594
            except errors.NoSuchRevision:
595
                if revision_id in required:
596
                    raise
597
                # a ghost
598
                result.add_ghost(revision_id)
599
                continue
600
            for parent_id in rev.parent_ids:
601
                # is this queued or done ?
602
                if (parent_id not in pending and
603
                    parent_id not in done):
604
                    # no, queue it.
605
                    pending.add(parent_id)
606
            result.add_node(revision_id, rev.parent_ids)
1594.2.15 by Robert Collins
Unfuck performance.
607
            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.
608
        return result
609
610
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
611
    def get_revision_inventory(self, revision_id):
612
        """Return inventory of a past revision."""
613
        # TODO: Unify this with get_inventory()
614
        # bzr 0.0.6 and later imposes the constraint that the inventory_id
615
        # must be the same as its revision, so this is trivial.
1534.4.28 by Robert Collins
first cut at merge from integration.
616
        if revision_id is None:
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
617
            # This does not make sense: if there is no revision,
618
            # then it is the current tree inventory surely ?!
619
            # and thus get_root_id() is something that looks at the last
620
            # commit on the branch, and the get_root_id is an inventory check.
621
            raise NotImplementedError
622
            # return Inventory(self.get_root_id())
623
        else:
624
            return self.get_inventory(revision_id)
625
1185.65.27 by Robert Collins
Tweak storage towards mergability.
626
    @needs_read_lock
1534.6.3 by Robert Collins
find_repository sufficiently robust.
627
    def is_shared(self):
628
        """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.
629
        raise NotImplementedError(self.is_shared)
1534.6.3 by Robert Collins
find_repository sufficiently robust.
630
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
631
    @needs_write_lock
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
632
    def reconcile(self, other=None, thorough=False):
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
633
        """Reconcile this repository."""
634
        from bzrlib.reconcile import RepoReconciler
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
635
        reconciler = RepoReconciler(self, thorough=thorough)
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
636
        reconciler.reconcile()
637
        return reconciler
638
    
1534.6.3 by Robert Collins
find_repository sufficiently robust.
639
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
640
    def revision_tree(self, revision_id):
641
        """Return Tree for a revision on this branch.
642
1852.5.1 by Robert Collins
Deprecate EmptyTree in favour of using Repository.revision_tree.
643
        `revision_id` may be None for the empty tree revision.
644
        """
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
645
        # TODO: refactor this to use an existing revision object
646
        # so we don't need to read it in twice.
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
647
        if revision_id is None or revision_id == _mod_revision.NULL_REVISION:
1731.1.61 by Aaron Bentley
Merge bzr.dev
648
            return RevisionTree(self, Inventory(root_id=None), 
649
                                _mod_revision.NULL_REVISION)
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
650
        else:
651
            inv = self.get_revision_inventory(revision_id)
1185.65.17 by Robert Collins
Merge from integration, mode-changes are broken.
652
            return RevisionTree(self, inv, revision_id)
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
653
1185.65.27 by Robert Collins
Tweak storage towards mergability.
654
    @needs_read_lock
1756.3.3 by Aaron Bentley
More refactoring, introduce revision_trees.
655
    def revision_trees(self, revision_ids):
656
        """Return Tree for a revision on this branch.
657
1756.3.19 by Aaron Bentley
Documentation and cleanups
658
        `revision_id` may not be None or 'null:'"""
1756.3.3 by Aaron Bentley
More refactoring, introduce revision_trees.
659
        assert None not in revision_ids
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
660
        assert _mod_revision.NULL_REVISION not in revision_ids
1756.3.5 by Aaron Bentley
Switch to get_texts, optimize get_texts
661
        texts = self.get_inventory_weave().get_texts(revision_ids)
1756.3.3 by Aaron Bentley
More refactoring, introduce revision_trees.
662
        for text, revision_id in zip(texts, revision_ids):
663
            inv = self.deserialise_inventory(revision_id, text)
664
            yield RevisionTree(self, inv, revision_id)
665
666
    @needs_read_lock
1185.66.2 by Aaron Bentley
Moved get_ancestry to RevisionStorage
667
    def get_ancestry(self, revision_id):
668
        """Return a list of revision-ids integrated by a revision.
1732.2.4 by Martin Pool
Split check into Branch.check and Repository.check
669
670
        The first element of the list is always None, indicating the origin 
671
        revision.  This might change when we have history horizons, or 
672
        perhaps we should have a new API.
1185.66.2 by Aaron Bentley
Moved get_ancestry to RevisionStorage
673
        
674
        This is topologically sorted.
675
        """
676
        if revision_id is None:
677
            return [None]
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
678
        if not self.has_revision(revision_id):
679
            raise errors.NoSuchRevision(self, revision_id)
1185.66.2 by Aaron Bentley
Moved get_ancestry to RevisionStorage
680
        w = self.get_inventory_weave()
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
681
        candidates = w.get_ancestry(revision_id)
682
        return [None] + candidates # self._eliminate_revisions_not_present(candidates)
1185.66.2 by Aaron Bentley
Moved get_ancestry to RevisionStorage
683
1185.65.4 by Aaron Bentley
Fixed cat command
684
    @needs_read_lock
685
    def print_file(self, file, revision_id):
1185.65.29 by Robert Collins
Implement final review suggestions.
686
        """Print `file` to stdout.
687
        
688
        FIXME RBC 20060125 as John Meinel points out this is a bad api
689
        - it writes to stdout, it assumes that that is valid etc. Fix
690
        by creating a new more flexible convenience function.
691
        """
1185.65.4 by Aaron Bentley
Fixed cat command
692
        tree = self.revision_tree(revision_id)
693
        # use inventory as it was in that revision
694
        file_id = tree.inventory.path2id(file)
695
        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
696
            # TODO: jam 20060427 Write a test for this code path
697
            #       it had a bug in it, and was raising the wrong
698
            #       exception.
699
            raise errors.BzrError("%r is not present in revision %s" % (file, revision_id))
1185.65.4 by Aaron Bentley
Fixed cat command
700
        tree.print_file(file_id)
701
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
702
    def get_transaction(self):
703
        return self.control_files.get_transaction()
704
1590.1.1 by Robert Collins
Improve common_ancestor performance.
705
    def revision_parents(self, revid):
706
        return self.get_inventory_weave().parent_names(revid)
707
1185.65.27 by Robert Collins
Tweak storage towards mergability.
708
    @needs_write_lock
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
709
    def set_make_working_trees(self, new_value):
710
        """Set the policy flag for making working trees when creating branches.
711
712
        This only applies to branches that use this repository.
713
714
        The default is 'True'.
715
        :param new_value: True to restore the default, False to disable making
716
                          working trees.
717
        """
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
718
        raise NotImplementedError(self.set_make_working_trees)
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
719
    
720
    def make_working_trees(self):
721
        """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.
722
        raise NotImplementedError(self.make_working_trees)
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
723
724
    @needs_write_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
725
    def sign_revision(self, revision_id, gpg_strategy):
726
        plaintext = Testament.from_revision(self, revision_id).as_short_text()
727
        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.
728
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
729
    @needs_read_lock
730
    def has_signature_for_revision_id(self, revision_id):
731
        """Query for a revision signature for revision_id in the repository."""
732
        return self._revision_store.has_signature(revision_id,
733
                                                  self.get_transaction())
734
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
735
    @needs_read_lock
736
    def get_signature_text(self, revision_id):
737
        """Return the text for a signature."""
738
        return self._revision_store.get_signature_text(revision_id,
739
                                                       self.get_transaction())
740
1732.2.4 by Martin Pool
Split check into Branch.check and Repository.check
741
    @needs_read_lock
742
    def check(self, revision_ids):
743
        """Check consistency of all history of given revision_ids.
744
745
        Different repository implementations should override _check().
746
747
        :param revision_ids: A non-empty list of revision_ids whose ancestry
748
             will be checked.  Typically the last revision_id of a branch.
749
        """
750
        if not revision_ids:
751
            raise ValueError("revision_ids must be non-empty in %s.check" 
752
                    % (self,))
753
        return self._check(revision_ids)
754
755
    def _check(self, revision_ids):
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
756
        result = check.Check(self)
1732.2.4 by Martin Pool
Split check into Branch.check and Repository.check
757
        result.check()
758
        return result
759
1904.2.3 by Martin Pool
Give a warning on access to old repository formats
760
    def _warn_if_deprecated(self):
1904.2.5 by Martin Pool
Fix format warning inside test suite and add test
761
        global _deprecation_warning_done
762
        if _deprecation_warning_done:
763
            return
764
        _deprecation_warning_done = True
1904.2.3 by Martin Pool
Give a warning on access to old repository formats
765
        warning("Format %s for %s is deprecated - please use 'bzr upgrade' to get better performance"
766
                % (self._format, self.bzrdir.transport.base))
767
1910.2.63 by Aaron Bentley
Add supports_rich_root member to repository
768
    def supports_rich_root(self):
769
        return self._format.rich_root_data
770
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.
771
    def _check_ascii_revisionid(self, revision_id, method):
772
        """Private helper for ascii-only repositories."""
773
        # weave repositories refuse to store revisionids that are non-ascii.
774
        if revision_id is not None:
775
            # weaves require ascii revision ids.
776
            if isinstance(revision_id, unicode):
777
                try:
778
                    revision_id.encode('ascii')
779
                except UnicodeEncodeError:
780
                    raise errors.NonAsciiRevisionId(method, self)
781
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
782
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
783
class AllInOneRepository(Repository):
784
    """Legacy support - the repository behaviour for all-in-one branches."""
785
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
786
    def __init__(self, _format, a_bzrdir, _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
787
        # we reuse one control files instance.
788
        dir_mode = a_bzrdir._control_files._dir_mode
789
        file_mode = a_bzrdir._control_files._file_mode
790
791
        def get_store(name, compressed=True, prefixed=False):
792
            # FIXME: This approach of assuming stores are all entirely compressed
793
            # or entirely uncompressed is tidy, but breaks upgrade from 
794
            # some existing branches where there's a mixture; we probably 
795
            # still want the option to look for both.
796
            relpath = a_bzrdir._control_files._escape(name)
797
            store = TextStore(a_bzrdir._control_files._transport.clone(relpath),
798
                              prefixed=prefixed, compressed=compressed,
799
                              dir_mode=dir_mode,
800
                              file_mode=file_mode)
801
            #if self._transport.should_cache():
802
            #    cache_path = os.path.join(self.cache_root, name)
803
            #    os.mkdir(cache_path)
804
            #    store = bzrlib.store.CachedStore(store, cache_path)
805
            return store
806
807
        # not broken out yet because the controlweaves|inventory_store
808
        # and text_store | weave_store bits are still different.
809
        if isinstance(_format, RepositoryFormat4):
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
810
            # cannot remove these - there is still no consistent api 
811
            # which allows access to this old info.
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
812
            self.inventory_store = get_store('inventory-store')
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
813
            text_store = get_store('text-store')
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
814
        super(AllInOneRepository, self).__init__(_format, a_bzrdir, 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
815
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.
816
    def get_commit_builder(self, branch, parents, config, timestamp=None,
817
                           timezone=None, committer=None, revprops=None,
818
                           revision_id=None):
819
        self._check_ascii_revisionid(revision_id, self.get_commit_builder)
820
        return Repository.get_commit_builder(self, branch, parents, config,
821
            timestamp, timezone, committer, revprops, revision_id)
822
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
823
    @needs_read_lock
824
    def is_shared(self):
825
        """AllInOne repositories cannot be shared."""
826
        return False
827
828
    @needs_write_lock
829
    def set_make_working_trees(self, new_value):
830
        """Set the policy flag for making working trees when creating branches.
831
832
        This only applies to branches that use this repository.
833
834
        The default is 'True'.
835
        :param new_value: True to restore the default, False to disable making
836
                          working trees.
837
        """
838
        raise NotImplementedError(self.set_make_working_trees)
839
    
840
    def make_working_trees(self):
841
        """Returns the policy for making working trees on new branches."""
842
        return True
843
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
844
1185.82.84 by Aaron Bentley
Moved stuff around
845
def install_revision(repository, rev, revision_tree):
846
    """Install all revision data into a repository."""
847
    present_parents = []
848
    parent_trees = {}
849
    for p_id in rev.parent_ids:
850
        if repository.has_revision(p_id):
851
            present_parents.append(p_id)
852
            parent_trees[p_id] = repository.revision_tree(p_id)
853
        else:
1852.5.1 by Robert Collins
Deprecate EmptyTree in favour of using Repository.revision_tree.
854
            parent_trees[p_id] = repository.revision_tree(None)
1185.82.84 by Aaron Bentley
Moved stuff around
855
856
    inv = revision_tree.inventory
1910.2.51 by Aaron Bentley
Bundles now corrupt repositories
857
    entries = inv.iter_entries()
1852.6.3 by Robert Collins
Make iter(Tree) consistent for all tree types.
858
    # backwards compatability hack: skip the root id.
1910.2.63 by Aaron Bentley
Add supports_rich_root member to repository
859
    if not repository.supports_rich_root():
1910.2.60 by Aaron Bentley
Ensure that new-model revisions aren't installed into old-model repos
860
        path, root = entries.next()
861
        if root.revision != rev.revision_id:
1910.2.63 by Aaron Bentley
Add supports_rich_root member to repository
862
            raise errors.IncompatibleRevision(repr(repository))
1185.82.84 by Aaron Bentley
Moved stuff around
863
    # Add the texts that are not already present
1852.6.3 by Robert Collins
Make iter(Tree) consistent for all tree types.
864
    for path, ie in entries:
1185.82.84 by Aaron Bentley
Moved stuff around
865
        w = repository.weave_store.get_weave_or_empty(ie.file_id,
866
                repository.get_transaction())
867
        if ie.revision not in w:
868
            text_parents = []
1740.2.2 by Aaron Bentley
Add test for the basis inventory automatically adding the revision id.
869
            # FIXME: TODO: The following loop *may* be overlapping/duplicate
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
870
            # 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.
871
            # is a latent bug here where the parents may have ancestors of each
872
            # other. RBC, AB
1185.82.84 by Aaron Bentley
Moved stuff around
873
            for revision, tree in parent_trees.iteritems():
874
                if ie.file_id not in tree:
875
                    continue
876
                parent_id = tree.inventory[ie.file_id].revision
877
                if parent_id in text_parents:
878
                    continue
879
                text_parents.append(parent_id)
880
                    
881
            vfile = repository.weave_store.get_weave_or_empty(ie.file_id, 
882
                repository.get_transaction())
883
            lines = revision_tree.get_file(ie.file_id).readlines()
884
            vfile.add_lines(rev.revision_id, text_parents, lines)
885
    try:
886
        # install the inventory
887
        repository.add_inventory(rev.revision_id, inv, present_parents)
888
    except errors.RevisionAlreadyPresent:
889
        pass
890
    repository.add_revision(rev.revision_id, rev, inv)
891
892
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
893
class MetaDirRepository(Repository):
894
    """Repositories in the new meta-dir layout."""
895
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
896
    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
897
        super(MetaDirRepository, self).__init__(_format,
898
                                                a_bzrdir,
899
                                                control_files,
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
900
                                                _revision_store,
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
901
                                                control_store,
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
902
                                                text_store)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
903
        dir_mode = self.control_files._dir_mode
904
        file_mode = self.control_files._file_mode
905
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
906
    @needs_read_lock
907
    def is_shared(self):
908
        """Return True if this repository is flagged as a shared repository."""
909
        return self.control_files._transport.has('shared-storage')
910
911
    @needs_write_lock
912
    def set_make_working_trees(self, new_value):
913
        """Set the policy flag for making working trees when creating branches.
914
915
        This only applies to branches that use this repository.
916
917
        The default is 'True'.
918
        :param new_value: True to restore the default, False to disable making
919
                          working trees.
920
        """
921
        if new_value:
922
            try:
923
                self.control_files._transport.delete('no-working-trees')
924
            except errors.NoSuchFile:
925
                pass
926
        else:
927
            self.control_files.put_utf8('no-working-trees', '')
928
    
929
    def make_working_trees(self):
930
        """Returns the policy for making working trees on new branches."""
931
        return not self.control_files._transport.has('no-working-trees')
932
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
933
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.
934
class WeaveMetaDirRepository(MetaDirRepository):
935
    """A subclass of MetaDirRepository to set weave specific policy."""
936
937
    def get_commit_builder(self, branch, parents, config, timestamp=None,
938
                           timezone=None, committer=None, revprops=None,
939
                           revision_id=None):
940
        self._check_ascii_revisionid(revision_id, self.get_commit_builder)
941
        return MetaDirRepository.get_commit_builder(self, branch, parents,
942
            config, timestamp, timezone, committer, revprops, revision_id)
943
944
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
945
class KnitRepository(MetaDirRepository):
946
    """Knit format repository."""
947
1904.2.3 by Martin Pool
Give a warning on access to old repository formats
948
    def _warn_if_deprecated(self):
949
        # This class isn't deprecated
950
        pass
951
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
952
    def _inventory_add_lines(self, inv_vf, revid, parents, lines):
953
        inv_vf.add_lines_with_ghosts(revid, parents, lines)
954
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
955
    @needs_read_lock
1732.2.4 by Martin Pool
Split check into Branch.check and Repository.check
956
    def _all_revision_ids(self):
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
957
        """See Repository.all_revision_ids()."""
1732.2.4 by Martin Pool
Split check into Branch.check and Repository.check
958
        # Knits get the revision graph from the index of the revision knit, so
959
        # it's always possible even if they're on an unlistable transport.
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
960
        return self._revision_store.all_revision_ids(self.get_transaction())
961
1732.2.6 by Martin Pool
Restore removed fileid_involved* methods
962
    def fileid_involved_between_revs(self, from_revid, to_revid):
963
        """Find file_id(s) which are involved in the changes between revisions.
964
965
        This determines the set of revisions which are involved, and then
966
        finds all file ids affected by those revisions.
967
        """
968
        vf = self._get_revision_vf()
969
        from_set = set(vf.get_ancestry(from_revid))
970
        to_set = set(vf.get_ancestry(to_revid))
971
        changed = to_set.difference(from_set)
972
        return self._fileid_involved_by_set(changed)
973
974
    def fileid_involved(self, last_revid=None):
975
        """Find all file_ids modified in the ancestry of last_revid.
976
977
        :param last_revid: If None, last_revision() will be used.
978
        """
979
        if not last_revid:
980
            changed = set(self.all_revision_ids())
981
        else:
982
            changed = set(self.get_ancestry(last_revid))
983
        if None in changed:
984
            changed.remove(None)
985
        return self._fileid_involved_by_set(changed)
986
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
987
    @needs_read_lock
988
    def get_ancestry(self, revision_id):
989
        """Return a list of revision-ids integrated by a revision.
990
        
991
        This is topologically sorted.
992
        """
993
        if revision_id is None:
994
            return [None]
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
995
        vf = self._get_revision_vf()
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
996
        try:
997
            return [None] + vf.get_ancestry(revision_id)
998
        except errors.RevisionNotPresent:
999
            raise errors.NoSuchRevision(self, revision_id)
1000
1001
    @needs_read_lock
1594.2.10 by Robert Collins
Teach knit fetching and branching to only duplicate relevant data avoiding unnecessary reconciles.
1002
    def get_revision(self, revision_id):
1003
        """Return the Revision object for a named revision"""
1004
        return self.get_revision_reconcile(revision_id)
1005
1006
    @needs_read_lock
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
1007
    def get_revision_graph(self, revision_id=None):
1008
        """Return a dictionary containing the revision graph.
1836.3.1 by Robert Collins
(robertc) Teach repository.get_revision_graph, and revision.common_ancestor, about NULL_REVISION.
1009
1010
        :param revision_id: The revision_id to get a graph from. If None, then
1011
        the entire revision graph is returned. This is a deprecated mode of
1012
        operation and will be removed in the future.
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
1013
        :return: a dictionary of revision_id->revision_parents_list.
1014
        """
1836.3.1 by Robert Collins
(robertc) Teach repository.get_revision_graph, and revision.common_ancestor, about NULL_REVISION.
1015
        # special case NULL_REVISION
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1016
        if revision_id == _mod_revision.NULL_REVISION:
1836.3.1 by Robert Collins
(robertc) Teach repository.get_revision_graph, and revision.common_ancestor, about NULL_REVISION.
1017
            return {}
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1018
        a_weave = self._get_revision_vf()
1019
        entire_graph = a_weave.get_graph()
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
1020
        if revision_id is None:
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1021
            return a_weave.get_graph()
1022
        elif revision_id not in a_weave:
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
1023
            raise errors.NoSuchRevision(self, revision_id)
1024
        else:
1025
            # add what can be reached from revision_id
1026
            result = {}
1027
            pending = set([revision_id])
1028
            while len(pending) > 0:
1029
                node = pending.pop()
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1030
                result[node] = a_weave.get_parents(node)
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
1031
                for revision_id in result[node]:
1032
                    if revision_id not in result:
1033
                        pending.add(revision_id)
1034
            return result
1035
1036
    @needs_read_lock
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
1037
    def get_revision_graph_with_ghosts(self, revision_ids=None):
1038
        """Return a graph of the revisions with ghosts marked as applicable.
1039
1040
        :param revision_ids: an iterable of revisions to graph or None for all.
1041
        :return: a Graph object with the graph reachable from revision_ids.
1042
        """
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1043
        result = graph.Graph()
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
1044
        vf = self._get_revision_vf()
1628.1.7 by Robert Collins
Tune get_revision_graph_with_ghosts for Knit repositories.
1045
        versions = set(vf.versions())
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
1046
        if not revision_ids:
1773.4.2 by Martin Pool
Cleanup of imports; undeprecate all_revision_ids()
1047
            pending = set(self.all_revision_ids())
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
1048
            required = set([])
1049
        else:
1050
            pending = set(revision_ids)
1836.3.1 by Robert Collins
(robertc) Teach repository.get_revision_graph, and revision.common_ancestor, about NULL_REVISION.
1051
            # special case NULL_REVISION
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1052
            if _mod_revision.NULL_REVISION in pending:
1053
                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.
1054
            required = set(pending)
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
1055
        done = set([])
1056
        while len(pending):
1057
            revision_id = pending.pop()
1058
            if not revision_id in versions:
1059
                if revision_id in required:
1060
                    raise errors.NoSuchRevision(self, revision_id)
1061
                # a ghost
1062
                result.add_ghost(revision_id)
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
1063
                # mark it as done so we don't try for it again.
1628.1.7 by Robert Collins
Tune get_revision_graph_with_ghosts for Knit repositories.
1064
                done.add(revision_id)
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
1065
                continue
1066
            parent_ids = vf.get_parents_with_ghosts(revision_id)
1067
            for parent_id in parent_ids:
1068
                # is this queued or done ?
1069
                if (parent_id not in pending and
1070
                    parent_id not in done):
1071
                    # no, queue it.
1072
                    pending.add(parent_id)
1073
            result.add_node(revision_id, parent_ids)
1628.1.7 by Robert Collins
Tune get_revision_graph_with_ghosts for Knit repositories.
1074
            done.add(revision_id)
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
1075
        return result
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
1076
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
1077
    def _get_revision_vf(self):
1607.1.2 by Robert Collins
Merge in knit-using-revision-versioned-file-graph tuning work.
1078
        """:return: a versioned file containing the revisions."""
1596.2.12 by Robert Collins
Merge and make Knit Repository use the revision store for all possible queries.
1079
        vf = self._revision_store.get_revision_file(self.get_transaction())
1080
        return vf
1081
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
1082
    @needs_write_lock
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
1083
    def reconcile(self, other=None, thorough=False):
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
1084
        """Reconcile this repository."""
1085
        from bzrlib.reconcile import KnitReconciler
1692.1.1 by Robert Collins
* Repository.reconcile now takes a thorough keyword parameter to allow
1086
        reconciler = KnitReconciler(self, thorough=thorough)
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
1087
        reconciler.reconcile()
1088
        return reconciler
1089
    
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
1090
    def revision_parents(self, revision_id):
1091
        return self._get_revision_vf().get_parents(revision_id)
1092
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1093
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
1094
class KnitRepository2(KnitRepository):
1095
    """"""
1910.2.48 by Aaron Bentley
Update from review comments
1096
    def __init__(self, _format, a_bzrdir, control_files, _revision_store,
1097
                 control_store, text_store):
1098
        KnitRepository.__init__(self, _format, a_bzrdir, control_files,
1099
                              _revision_store, control_store, text_store)
1100
        self._serializer = xml6.serializer_v6
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
1101
1102
    def deserialise_inventory(self, revision_id, xml):
1103
        """Transform the xml into an inventory object. 
1104
1105
        :param revision_id: The expected revision id of the inventory.
1106
        :param xml: A serialised inventory.
1107
        """
1910.2.48 by Aaron Bentley
Update from review comments
1108
        result = self._serializer.read_inventory_from_string(xml)
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
1109
        assert result.root.revision is not None
1110
        return result
1111
1112
    def serialise_inventory(self, inv):
1113
        """Transform the inventory object into XML text.
1114
1115
        :param revision_id: The expected revision id of the inventory.
1116
        :param xml: A serialised inventory.
1117
        """
1910.2.23 by Aaron Bentley
Fix up test cases that manually construct inventories
1118
        assert inv.revision_id is not None
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
1119
        assert inv.root.revision is not None
1910.2.48 by Aaron Bentley
Update from review comments
1120
        return KnitRepository.serialise_inventory(self, inv)
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
1121
1122
    def get_commit_builder(self, branch, parents, config, timestamp=None, 
1123
                           timezone=None, committer=None, revprops=None, 
1124
                           revision_id=None):
1125
        """Obtain a CommitBuilder for this repository.
1126
        
1127
        :param branch: Branch to commit to.
1128
        :param parents: Revision ids of the parents of the new revision.
1129
        :param config: Configuration to use.
1130
        :param timestamp: Optional timestamp recorded for commit.
1131
        :param timezone: Optional timezone for timestamp.
1132
        :param committer: Optional committer to set for commit.
1133
        :param revprops: Optional dictionary of revision properties.
1134
        :param revision_id: Optional revision id.
1135
        """
1136
        return RootCommitBuilder(self, parents, config, timestamp, timezone,
1137
                                 committer, revprops, revision_id)
1138
1910.2.46 by Aaron Bentley
Whitespace fix
1139
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1140
class RepositoryFormat(object):
1141
    """A repository format.
1142
1143
    Formats provide three things:
1144
     * An initialization routine to construct repository data on disk.
1145
     * a format string which is used when the BzrDir supports versioned
1146
       children.
1147
     * an open routine which returns a Repository instance.
1148
1149
    Formats are placed in an dict by their format string for reference 
1150
    during opening. These should be subclasses of RepositoryFormat
1151
    for consistency.
1152
1153
    Once a format is deprecated, just deprecate the initialize and open
1154
    methods on the format class. Do not deprecate the object, as the 
1155
    object will be created every system load.
1156
1157
    Common instance attributes:
1158
    _matchingbzrdir - the bzrdir format that the repository format was
1159
    originally written to work with. This can be used if manually
1160
    constructing a bzrdir and repository, or more commonly for test suite
1161
    parameterisation.
1162
    """
1163
1164
    _default_format = None
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1165
    """The default format used for new repositories."""
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1166
1167
    _formats = {}
1168
    """The known formats."""
1169
1904.2.3 by Martin Pool
Give a warning on access to old repository formats
1170
    def __str__(self):
1171
        return "<%s>" % self.__class__.__name__
1172
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1173
    @classmethod
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1174
    def find_format(klass, a_bzrdir):
1175
        """Return the format for the repository object in a_bzrdir."""
1176
        try:
1177
            transport = a_bzrdir.get_repository_transport(None)
1178
            format_string = transport.get("format").read()
1179
            return klass._formats[format_string]
1180
        except errors.NoSuchFile:
1181
            raise errors.NoRepositoryPresent(a_bzrdir)
1182
        except KeyError:
1740.5.6 by Martin Pool
Clean up many exception classes.
1183
            raise errors.UnknownFormatError(format=format_string)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1184
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
1185
    def _get_control_store(self, repo_transport, control_files):
1186
        """Return the control store for this repository."""
1187
        raise NotImplementedError(self._get_control_store)
1188
    
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1189
    @classmethod
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1190
    def get_default_format(klass):
1191
        """Return the current default format."""
1192
        return klass._default_format
1193
1194
    def get_format_string(self):
1195
        """Return the ASCII format string that identifies this format.
1196
        
1197
        Note that in pre format ?? repositories the format string is 
1198
        not permitted nor written to disk.
1199
        """
1200
        raise NotImplementedError(self.get_format_string)
1201
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1202
    def get_format_description(self):
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1203
        """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
1204
        raise NotImplementedError(self.get_format_description)
1205
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1206
    def _get_revision_store(self, repo_transport, control_files):
1207
        """Return the revision store object for this a_bzrdir."""
1556.1.5 by Robert Collins
Review feedback.
1208
        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
1209
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1210
    def _get_text_rev_store(self,
1211
                            transport,
1212
                            control_files,
1213
                            name,
1214
                            compressed=True,
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1215
                            prefixed=False,
1216
                            serializer=None):
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1217
        """Common logic for getting a revision store for a repository.
1218
        
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1219
        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
1220
        get the store for a repository.
1221
        """
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1222
        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
1223
        dir_mode = control_files._dir_mode
1224
        file_mode = control_files._file_mode
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1225
        text_store =TextStore(transport.clone(name),
1226
                              prefixed=prefixed,
1227
                              compressed=compressed,
1228
                              dir_mode=dir_mode,
1229
                              file_mode=file_mode)
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1230
        _revision_store = TextRevisionStore(text_store, serializer)
1231
        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
1232
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1233
    def _get_versioned_file_store(self,
1234
                                  name,
1235
                                  transport,
1236
                                  control_files,
1237
                                  prefixed=True,
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1238
                                  versionedfile_class=weave.WeaveFile,
1946.2.5 by John Arbash Meinel
Make knit stores delay creation, but not control stores
1239
                                  versionedfile_kwargs={},
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
1240
                                  escaped=False):
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1241
        weave_transport = control_files._transport.clone(name)
1242
        dir_mode = control_files._dir_mode
1243
        file_mode = control_files._file_mode
1244
        return VersionedFileStore(weave_transport, prefixed=prefixed,
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
1245
                                  dir_mode=dir_mode,
1246
                                  file_mode=file_mode,
1247
                                  versionedfile_class=versionedfile_class,
1946.2.5 by John Arbash Meinel
Make knit stores delay creation, but not control stores
1248
                                  versionedfile_kwargs=versionedfile_kwargs,
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
1249
                                  escaped=escaped)
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1250
1534.6.1 by Robert Collins
allow API creation of shared repositories
1251
    def initialize(self, a_bzrdir, shared=False):
1252
        """Initialize a repository of this format in a_bzrdir.
1253
1254
        :param a_bzrdir: The bzrdir to put the new repository in it.
1255
        :param shared: The repository should be initialized as a sharable one.
1256
1257
        This may raise UninitializableFormat if shared repository are not
1258
        compatible the a_bzrdir.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1259
        """
1260
1261
    def is_supported(self):
1262
        """Is this format supported?
1263
1264
        Supported formats must be initializable and openable.
1265
        Unsupported formats may not support initialization or committing or 
1266
        some other features depending on the reason for not being supported.
1267
        """
1268
        return True
1269
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1270
    def check_conversion_target(self, target_format):
1271
        raise NotImplementedError(self.check_conversion_target)
1272
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1273
    def open(self, a_bzrdir, _found=False):
1274
        """Return an instance of this format for the bzrdir a_bzrdir.
1275
        
1276
        _found is a private parameter, do not use it.
1277
        """
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1278
        raise NotImplementedError(self.open)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1279
1280
    @classmethod
1281
    def register_format(klass, format):
1282
        klass._formats[format.get_format_string()] = format
1283
1284
    @classmethod
2204.4.11 by Aaron Bentley
deprecate Repository.set_default_format, update upgrade tests
1285
    @deprecated_method(symbol_versioning.zero_fourteen)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1286
    def set_default_format(klass, format):
2204.4.11 by Aaron Bentley
deprecate Repository.set_default_format, update upgrade tests
1287
        klass._set_default_format(format)
1288
1289
    @classmethod
1290
    def _set_default_format(klass, format):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1291
        klass._default_format = format
1292
1293
    @classmethod
1294
    def unregister_format(klass, format):
1295
        assert klass._formats[format.get_format_string()] is format
1296
        del klass._formats[format.get_format_string()]
1297
1298
1534.6.1 by Robert Collins
allow API creation of shared repositories
1299
class PreSplitOutRepositoryFormat(RepositoryFormat):
1300
    """Base class for the pre split out repository formats."""
1301
1910.2.14 by Aaron Bentley
Fail when trying to use interrepository on Knit2 and Knit1
1302
    rich_root_data = False
1303
1534.6.1 by Robert Collins
allow API creation of shared repositories
1304
    def initialize(self, a_bzrdir, shared=False, _internal=False):
1305
        """Create a weave repository.
1306
        
1307
        TODO: when creating split out bzr branch formats, move this to a common
1308
        base for Format5, Format6. or something like that.
1309
        """
1310
        if shared:
1311
            raise errors.IncompatibleFormat(self, a_bzrdir._format)
1312
1313
        if not _internal:
1314
            # always initialized when the bzrdir is.
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1315
            return self.open(a_bzrdir, _found=True)
1534.6.1 by Robert Collins
allow API creation of shared repositories
1316
        
1317
        # Create an empty weave
1318
        sio = StringIO()
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1319
        weavefile.write_weave_v5(weave.Weave(), sio)
1534.6.1 by Robert Collins
allow API creation of shared repositories
1320
        empty_weave = sio.getvalue()
1321
1322
        mutter('creating repository in %s.', a_bzrdir.transport.base)
1323
        dirs = ['revision-store', 'weaves']
1553.5.56 by Martin Pool
Format 7 repo now uses LockDir!
1324
        files = [('inventory.weave', StringIO(empty_weave)),
1534.6.1 by Robert Collins
allow API creation of shared repositories
1325
                 ]
1326
        
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
1327
        # FIXME: RBC 20060125 don't peek under the covers
1534.6.1 by Robert Collins
allow API creation of shared repositories
1328
        # NB: no need to escape relative paths that are url safe.
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1329
        control_files = lockable_files.LockableFiles(a_bzrdir.transport,
1330
                                'branch-lock', lockable_files.TransportLock)
1553.5.63 by Martin Pool
Lock type is now mandatory for LockableFiles constructor
1331
        control_files.create_lock()
1534.6.1 by Robert Collins
allow API creation of shared repositories
1332
        control_files.lock_write()
1333
        control_files._transport.mkdir_multi(dirs,
1334
                mode=control_files._dir_mode)
1335
        try:
1336
            for file, content in files:
1337
                control_files.put(file, content)
1338
        finally:
1339
            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
1340
        return self.open(a_bzrdir, _found=True)
1341
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
1342
    def _get_control_store(self, repo_transport, control_files):
1343
        """Return the control store for this repository."""
1344
        return self._get_versioned_file_store('',
1345
                                              repo_transport,
1346
                                              control_files,
1347
                                              prefixed=False)
1348
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1349
    def _get_text_store(self, transport, control_files):
1350
        """Get a store for file texts for this format."""
1351
        raise NotImplementedError(self._get_text_store)
1352
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1353
    def open(self, a_bzrdir, _found=False):
1354
        """See RepositoryFormat.open()."""
1355
        if not _found:
1356
            # we are being called directly and must probe.
1357
            raise NotImplementedError
1358
1359
        repo_transport = a_bzrdir.get_repository_transport(None)
1360
        control_files = a_bzrdir._control_files
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
1361
        text_store = self._get_text_store(repo_transport, control_files)
1362
        control_store = self._get_control_store(repo_transport, control_files)
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1363
        _revision_store = self._get_revision_store(repo_transport, control_files)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1364
        return AllInOneRepository(_format=self,
1365
                                  a_bzrdir=a_bzrdir,
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1366
                                  _revision_store=_revision_store,
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
1367
                                  control_store=control_store,
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1368
                                  text_store=text_store)
1534.6.1 by Robert Collins
allow API creation of shared repositories
1369
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1370
    def check_conversion_target(self, target_format):
1371
        pass
1372
1534.6.1 by Robert Collins
allow API creation of shared repositories
1373
1374
class RepositoryFormat4(PreSplitOutRepositoryFormat):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1375
    """Bzr repository format 4.
1376
1377
    This repository format has:
1378
     - flat stores
1379
     - TextStores for texts, inventories,revisions.
1380
1381
    This format is deprecated: it indexes texts using a text id which is
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1382
    removed in format 5; initialization and write support for this format
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1383
    has been removed.
1384
    """
1385
1386
    def __init__(self):
1387
        super(RepositoryFormat4, self).__init__()
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
1388
        self._matchingbzrdir = bzrdir.BzrDirFormat4()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1389
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1390
    def get_format_description(self):
1391
        """See RepositoryFormat.get_format_description()."""
1392
        return "Repository format 4"
1393
1534.6.1 by Robert Collins
allow API creation of shared repositories
1394
    def initialize(self, url, shared=False, _internal=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1395
        """Format 4 branches cannot be created."""
1396
        raise errors.UninitializableFormat(self)
1397
1398
    def is_supported(self):
1399
        """Format 4 is not supported.
1400
1401
        It is not supported because the model changed from 4 to 5 and the
1402
        conversion logic is expensive - so doing it on the fly was not 
1403
        feasible.
1404
        """
1405
        return False
1406
1563.2.23 by Robert Collins
Add add_revision and get_revision methods to RevisionStore
1407
    def _get_control_store(self, repo_transport, control_files):
1408
        """Format 4 repositories have no formal control store at this point.
1409
        
1410
        This will cause any control-file-needing apis to fail - this is desired.
1411
        """
1412
        return None
1413
    
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1414
    def _get_revision_store(self, repo_transport, control_files):
1415
        """See RepositoryFormat._get_revision_store()."""
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1416
        from bzrlib.xml4 import serializer_v4
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1417
        return self._get_text_rev_store(repo_transport,
1418
                                        control_files,
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1419
                                        'revision-store',
1420
                                        serializer=serializer_v4)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1421
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1422
    def _get_text_store(self, transport, control_files):
1423
        """See RepositoryFormat._get_text_store()."""
1424
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1425
1534.6.1 by Robert Collins
allow API creation of shared repositories
1426
class RepositoryFormat5(PreSplitOutRepositoryFormat):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1427
    """Bzr control format 5.
1428
1429
    This repository format has:
1430
     - weaves for file texts and inventory
1431
     - flat stores
1432
     - TextStores for revisions and signatures.
1433
    """
1434
1435
    def __init__(self):
1436
        super(RepositoryFormat5, self).__init__()
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
1437
        self._matchingbzrdir = bzrdir.BzrDirFormat5()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1438
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1439
    def get_format_description(self):
1440
        """See RepositoryFormat.get_format_description()."""
1441
        return "Weave repository format 5"
1442
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1443
    def _get_revision_store(self, repo_transport, control_files):
1444
        """See RepositoryFormat._get_revision_store()."""
1445
        """Return the revision store object for this a_bzrdir."""
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1446
        return self._get_text_rev_store(repo_transport,
1447
                                        control_files,
1448
                                        'revision-store',
1449
                                        compressed=False)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1450
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1451
    def _get_text_store(self, transport, control_files):
1452
        """See RepositoryFormat._get_text_store()."""
1453
        return self._get_versioned_file_store('weaves', transport, control_files, prefixed=False)
1454
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1455
1534.6.1 by Robert Collins
allow API creation of shared repositories
1456
class RepositoryFormat6(PreSplitOutRepositoryFormat):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1457
    """Bzr control format 6.
1458
1459
    This repository format has:
1460
     - weaves for file texts and inventory
1461
     - hash subdirectory based stores.
1462
     - TextStores for revisions and signatures.
1463
    """
1464
1465
    def __init__(self):
1466
        super(RepositoryFormat6, self).__init__()
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
1467
        self._matchingbzrdir = bzrdir.BzrDirFormat6()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1468
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1469
    def get_format_description(self):
1470
        """See RepositoryFormat.get_format_description()."""
1471
        return "Weave repository format 6"
1472
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1473
    def _get_revision_store(self, repo_transport, control_files):
1474
        """See RepositoryFormat._get_revision_store()."""
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1475
        return self._get_text_rev_store(repo_transport,
1476
                                        control_files,
1477
                                        'revision-store',
1478
                                        compressed=False,
1479
                                        prefixed=True)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1480
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1481
    def _get_text_store(self, transport, control_files):
1482
        """See RepositoryFormat._get_text_store()."""
1483
        return self._get_versioned_file_store('weaves', transport, control_files)
1484
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1485
1486
class MetaDirRepositoryFormat(RepositoryFormat):
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1487
    """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
1488
1910.2.14 by Aaron Bentley
Fail when trying to use interrepository on Knit2 and Knit1
1489
    rich_root_data = False
1490
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.
1491
    def __init__(self):
1492
        super(MetaDirRepositoryFormat, self).__init__()
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
1493
        self._matchingbzrdir = bzrdir.BzrDirMetaFormat1()
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.
1494
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1495
    def _create_control_files(self, a_bzrdir):
1496
        """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.
1497
        # FIXME: RBC 20060125 don't peek under the covers
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1498
        # NB: no need to escape relative paths that are url safe.
1499
        repository_transport = a_bzrdir.get_repository_transport(self)
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1500
        control_files = lockable_files.LockableFiles(repository_transport,
1501
                                'lock', lockdir.LockDir)
1553.5.61 by Martin Pool
Locks protecting LockableFiles must now be explicitly created before use.
1502
        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
1503
        return control_files
1504
1505
    def _upload_blank_content(self, a_bzrdir, dirs, files, utf8_files, shared):
1506
        """Upload the initial blank content."""
1507
        control_files = self._create_control_files(a_bzrdir)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1508
        control_files.lock_write()
1509
        try:
1553.5.49 by Martin Pool
Use LockDirs for repo format 7
1510
            control_files._transport.mkdir_multi(dirs,
1511
                    mode=control_files._dir_mode)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1512
            for file, content in files:
1513
                control_files.put(file, content)
1514
            for file, content in utf8_files:
1515
                control_files.put_utf8(file, content)
1534.6.1 by Robert Collins
allow API creation of shared repositories
1516
            if shared == True:
1517
                control_files.put_utf8('shared-storage', '')
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1518
        finally:
1519
            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
1520
1521
1522
class RepositoryFormat7(MetaDirRepositoryFormat):
1523
    """Bzr repository 7.
1524
1525
    This repository format has:
1526
     - weaves for file texts and inventory
1527
     - hash subdirectory based stores.
1528
     - TextStores for revisions and signatures.
1529
     - a format marker of its own
1530
     - an optional 'shared-storage' flag
1531
     - an optional 'no-working-trees' flag
1532
    """
1533
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
1534
    def _get_control_store(self, repo_transport, control_files):
1535
        """Return the control store for this repository."""
1536
        return self._get_versioned_file_store('',
1537
                                              repo_transport,
1538
                                              control_files,
1539
                                              prefixed=False)
1540
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1541
    def get_format_string(self):
1542
        """See RepositoryFormat.get_format_string()."""
1543
        return "Bazaar-NG Repository format 7"
1544
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1545
    def get_format_description(self):
1546
        """See RepositoryFormat.get_format_description()."""
1547
        return "Weave repository format 7"
1548
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1549
    def check_conversion_target(self, target_format):
1550
        pass
1551
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1552
    def _get_revision_store(self, repo_transport, control_files):
1553
        """See RepositoryFormat._get_revision_store()."""
1554
        return self._get_text_rev_store(repo_transport,
1555
                                        control_files,
1556
                                        'revision-store',
1557
                                        compressed=False,
1558
                                        prefixed=True,
1559
                                        )
1560
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1561
    def _get_text_store(self, transport, control_files):
1562
        """See RepositoryFormat._get_text_store()."""
1563
        return self._get_versioned_file_store('weaves',
1564
                                              transport,
1565
                                              control_files)
1566
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1567
    def initialize(self, a_bzrdir, shared=False):
1568
        """Create a weave repository.
1569
1570
        :param shared: If true the repository will be initialized as a shared
1571
                       repository.
1572
        """
1573
        # Create an empty weave
1574
        sio = StringIO()
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1575
        weavefile.write_weave_v5(weave.Weave(), sio)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1576
        empty_weave = sio.getvalue()
1577
1578
        mutter('creating repository in %s.', a_bzrdir.transport.base)
1579
        dirs = ['revision-store', 'weaves']
1580
        files = [('inventory.weave', StringIO(empty_weave)), 
1581
                 ]
1582
        utf8_files = [('format', self.get_format_string())]
1583
 
1584
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
1585
        return self.open(a_bzrdir=a_bzrdir, _found=True)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1586
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1587
    def open(self, a_bzrdir, _found=False, _override_transport=None):
1588
        """See RepositoryFormat.open().
1589
        
1590
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
1591
                                    repository at a slightly different url
1592
                                    than normal. I.e. during 'upgrade'.
1593
        """
1594
        if not _found:
1595
            format = RepositoryFormat.find_format(a_bzrdir)
1596
            assert format.__class__ ==  self.__class__
1597
        if _override_transport is not None:
1598
            repo_transport = _override_transport
1599
        else:
1600
            repo_transport = a_bzrdir.get_repository_transport(None)
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1601
        control_files = lockable_files.LockableFiles(repo_transport,
1602
                                'lock', lockdir.LockDir)
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1603
        text_store = self._get_text_store(repo_transport, control_files)
1604
        control_store = self._get_control_store(repo_transport, control_files)
1605
        _revision_store = self._get_revision_store(repo_transport, control_files)
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.
1606
        return WeaveMetaDirRepository(_format=self,
1607
            a_bzrdir=a_bzrdir,
1608
            control_files=control_files,
1609
            _revision_store=_revision_store,
1610
            control_store=control_store,
1611
            text_store=text_store)
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
1612
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1613
1910.2.11 by Aaron Bentley
Start work on Knit format 2
1614
class RepositoryFormatKnit(MetaDirRepositoryFormat):
1615
    """Bzr repository knit format (generalized). 
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1616
1617
    This repository format has:
1618
     - knits for file texts and inventory
1619
     - hash subdirectory based stores.
1620
     - knits for revisions and signatures
1621
     - TextStores for revisions and signatures.
1622
     - a format marker of its own
1623
     - an optional 'shared-storage' flag
1624
     - an optional 'no-working-trees' flag
1553.5.62 by Martin Pool
Add tests that MetaDir repositories use LockDirs
1625
     - a LockDir lock
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1626
    """
1627
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
1628
    def _get_control_store(self, repo_transport, control_files):
1629
        """Return the control store for this repository."""
1628.1.5 by Robert Collins
Make inventory knits not annotated, only delta compressed.
1630
        return VersionedFileStore(
1631
            repo_transport,
1632
            prefixed=False,
1633
            file_mode=control_files._file_mode,
1996.3.5 by John Arbash Meinel
Cleanup, deprecated, and get the tests passing again.
1634
            versionedfile_class=knit.KnitVersionedFile,
1635
            versionedfile_kwargs={'factory':knit.KnitPlainFactory()},
1628.1.5 by Robert Collins
Make inventory knits not annotated, only delta compressed.
1636
            )
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
1637
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1638
    def _get_revision_store(self, repo_transport, control_files):
1639
        """See RepositoryFormat._get_revision_store()."""
1640
        from bzrlib.store.revision.knit import KnitRevisionStore
1641
        versioned_file_store = VersionedFileStore(
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
1642
            repo_transport,
1651.1.1 by Martin Pool
[merge][wip] Storage escaping
1643
            file_mode=control_files._file_mode,
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1644
            prefixed=False,
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
1645
            precious=True,
1996.3.5 by John Arbash Meinel
Cleanup, deprecated, and get the tests passing again.
1646
            versionedfile_class=knit.KnitVersionedFile,
1647
            versionedfile_kwargs={'delta':False,
1648
                                  'factory':knit.KnitPlainFactory(),
1649
                                 },
1608.2.12 by Martin Pool
Store-escaping must quote uppercase characters too, so that they're safely
1650
            escaped=True,
1651.1.1 by Martin Pool
[merge][wip] Storage escaping
1651
            )
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1652
        return KnitRevisionStore(versioned_file_store)
1653
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1654
    def _get_text_store(self, transport, control_files):
1655
        """See RepositoryFormat._get_text_store()."""
1656
        return self._get_versioned_file_store('knits',
1996.3.5 by John Arbash Meinel
Cleanup, deprecated, and get the tests passing again.
1657
                                  transport,
1658
                                  control_files,
1659
                                  versionedfile_class=knit.KnitVersionedFile,
1660
                                  versionedfile_kwargs={
1661
                                      'create_parent_dir':True,
1662
                                      'delay_create':True,
1663
                                      'dir_mode':control_files._dir_mode,
1664
                                  },
1665
                                  escaped=True)
1563.2.17 by Robert Collins
Change knits repositories to use a knit versioned file store for file texts.
1666
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1667
    def initialize(self, a_bzrdir, shared=False):
1668
        """Create a knit format 1 repository.
1669
1658.1.7 by Martin Pool
(RepositoryFormatKnit1.initialize) remove dead code that constructs weaves
1670
        :param a_bzrdir: bzrdir to contain the new repository; must already
1671
            be initialized.
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1672
        :param shared: If true the repository will be initialized as a shared
1673
                       repository.
1674
        """
1675
        mutter('creating repository in %s.', a_bzrdir.transport.base)
1707.3.29 by John Arbash Meinel
reverting 1734
1676
        dirs = ['revision-store', 'knits']
1658.1.7 by Martin Pool
(RepositoryFormatKnit1.initialize) remove dead code that constructs weaves
1677
        files = []
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1678
        utf8_files = [('format', self.get_format_string())]
1679
        
1680
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
1563.2.25 by Robert Collins
Merge in upstream.
1681
        repo_transport = a_bzrdir.get_repository_transport(None)
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1682
        control_files = lockable_files.LockableFiles(repo_transport,
1683
                                'lock', lockdir.LockDir)
1563.2.25 by Robert Collins
Merge in upstream.
1684
        control_store = self._get_control_store(repo_transport, control_files)
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
1685
        transaction = transactions.WriteTransaction()
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
1686
        # trigger a write of the inventory store.
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
1687
        control_store.get_weave_or_empty('inventory', transaction)
1688
        _revision_store = self._get_revision_store(repo_transport, control_files)
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.
1689
        # the revision id here is irrelevant: it will not be stored, and cannot
1690
        # already exist.
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
1691
        _revision_store.has_revision_id('A', transaction)
1692
        _revision_store.get_signature_file(transaction)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1693
        return self.open(a_bzrdir=a_bzrdir, _found=True)
1694
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1695
    def open(self, a_bzrdir, _found=False, _override_transport=None):
1696
        """See RepositoryFormat.open().
1697
        
1698
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
1699
                                    repository at a slightly different url
1700
                                    than normal. I.e. during 'upgrade'.
1701
        """
1702
        if not _found:
1703
            format = RepositoryFormat.find_format(a_bzrdir)
1704
            assert format.__class__ ==  self.__class__
1705
        if _override_transport is not None:
1706
            repo_transport = _override_transport
1707
        else:
1708
            repo_transport = a_bzrdir.get_repository_transport(None)
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
1709
        control_files = lockable_files.LockableFiles(repo_transport,
1710
                                'lock', lockdir.LockDir)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1711
        text_store = self._get_text_store(repo_transport, control_files)
1712
        control_store = self._get_control_store(repo_transport, control_files)
1713
        _revision_store = self._get_revision_store(repo_transport, control_files)
1714
        return KnitRepository(_format=self,
1715
                              a_bzrdir=a_bzrdir,
1716
                              control_files=control_files,
1717
                              _revision_store=_revision_store,
1718
                              control_store=control_store,
1719
                              text_store=text_store)
1720
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1721
1910.2.11 by Aaron Bentley
Start work on Knit format 2
1722
class RepositoryFormatKnit1(RepositoryFormatKnit):
1723
    """Bzr repository knit format 1.
1724
1725
    This repository format has:
1726
     - knits for file texts and inventory
1727
     - hash subdirectory based stores.
1728
     - knits for revisions and signatures
1729
     - TextStores for revisions and signatures.
1730
     - a format marker of its own
1731
     - an optional 'shared-storage' flag
1732
     - an optional 'no-working-trees' flag
1733
     - a LockDir lock
1734
1735
    This format was introduced in bzr 0.8.
1736
    """
1737
    def get_format_string(self):
1738
        """See RepositoryFormat.get_format_string()."""
1739
        return "Bazaar-NG Knit Repository Format 1"
1740
1741
    def get_format_description(self):
1742
        """See RepositoryFormat.get_format_description()."""
1743
        return "Knit repository format 1"
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1744
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1745
    def check_conversion_target(self, target_format):
1746
        pass
1747
1748
1749
class RepositoryFormatKnit2(RepositoryFormatKnit):
1750
    """Bzr repository knit format 2.
1751
1752
    THIS FORMAT IS EXPERIMENTAL
1753
    This repository format has:
1754
     - knits for file texts and inventory
1755
     - hash subdirectory based stores.
1756
     - knits for revisions and signatures
1757
     - TextStores for revisions and signatures.
1758
     - a format marker of its own
1759
     - an optional 'shared-storage' flag
1760
     - an optional 'no-working-trees' flag
1761
     - a LockDir lock
1762
     - Support for recording full info about the tree root
1763
1764
    """
1910.2.14 by Aaron Bentley
Fail when trying to use interrepository on Knit2 and Knit1
1765
    
1766
    rich_root_data = True
1767
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1768
    def get_format_string(self):
1769
        """See RepositoryFormat.get_format_string()."""
1910.2.48 by Aaron Bentley
Update from review comments
1770
        return "Bazaar Knit Repository Format 2\n"
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1771
1772
    def get_format_description(self):
1773
        """See RepositoryFormat.get_format_description()."""
1774
        return "Knit repository format 2"
1775
1776
    def check_conversion_target(self, target_format):
1910.2.14 by Aaron Bentley
Fail when trying to use interrepository on Knit2 and Knit1
1777
        if not target_format.rich_root_data:
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1778
            raise errors.BadConversionTarget(
1779
                'Does not support rich root data.', target_format)
1780
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
1781
    def open(self, a_bzrdir, _found=False, _override_transport=None):
1782
        """See RepositoryFormat.open().
1783
        
1784
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
1785
                                    repository at a slightly different url
1786
                                    than normal. I.e. during 'upgrade'.
1787
        """
1788
        if not _found:
1789
            format = RepositoryFormat.find_format(a_bzrdir)
1790
            assert format.__class__ ==  self.__class__
1791
        if _override_transport is not None:
1792
            repo_transport = _override_transport
1793
        else:
1794
            repo_transport = a_bzrdir.get_repository_transport(None)
1996.3.20 by John Arbash Meinel
[merge] bzr.dev 2063
1795
        control_files = lockable_files.LockableFiles(repo_transport, 'lock',
1796
                                                     lockdir.LockDir)
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
1797
        text_store = self._get_text_store(repo_transport, control_files)
1798
        control_store = self._get_control_store(repo_transport, control_files)
1799
        _revision_store = self._get_revision_store(repo_transport, control_files)
1800
        return KnitRepository2(_format=self,
1801
                               a_bzrdir=a_bzrdir,
1802
                               control_files=control_files,
1803
                               _revision_store=_revision_store,
1804
                               control_store=control_store,
1805
                               text_store=text_store)
1806
1807
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1808
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1809
# formats which have no format string are not discoverable
1810
# and not independently creatable, so are not registered.
1666.1.6 by Robert Collins
Make knit the default format.
1811
RepositoryFormat.register_format(RepositoryFormat7())
2204.4.6 by Aaron Bentley
Fix default to work with RepositoryFormat.set_default_format
1812
# KEEP in sync with bzrdir.format_registry default
1910.2.42 by Aaron Bentley
Restore RepositoryFormatKnit1 as the default
1813
_default_format = RepositoryFormatKnit1()
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.
1814
RepositoryFormat.register_format(_default_format)
1910.2.42 by Aaron Bentley
Restore RepositoryFormatKnit1 as the default
1815
RepositoryFormat.register_format(RepositoryFormatKnit2())
2204.4.11 by Aaron Bentley
deprecate Repository.set_default_format, update upgrade tests
1816
RepositoryFormat._set_default_format(_default_format)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1817
_legacy_formats = [RepositoryFormat4(),
1818
                   RepositoryFormat5(),
1819
                   RepositoryFormat6()]
1820
1821
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.
1822
class InterRepository(InterObject):
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
1823
    """This class represents operations taking place between two repositories.
1824
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.
1825
    Its instances have methods like copy_content and fetch, and contain
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
1826
    references to the source and target repositories these operations can be 
1827
    carried out on.
1828
1829
    Often we will provide convenience methods on 'repository' which carry out
1830
    operations with another repository - they will always forward to
1831
    InterRepository.get(other).method_name(parameters).
1832
    """
1833
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
1834
    _optimisers = []
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
1835
    """The available optimised InterRepository types."""
1836
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
1837
    def copy_content(self, revision_id=None, basis=None):
1838
        raise NotImplementedError(self.copy_content)
1839
1840
    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.
1841
        """Fetch the content required to construct revision_id.
1842
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
1843
        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.
1844
1845
        :param revision_id: if None all content is copied, if NULL_REVISION no
1846
                            content is copied.
1847
        :param pb: optional progress bar to use for progress reports. If not
1848
                   provided a default one will be created.
1849
1850
        Returns the copied revision count and the failed revisions in a tuple:
1851
        (copied, failures).
1852
        """
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
1853
        raise NotImplementedError(self.fetch)
1854
   
1855
    @needs_read_lock
1856
    def missing_revision_ids(self, revision_id=None):
1857
        """Return the revision ids that source has that target does not.
1858
        
1859
        These are returned in topological order.
1860
1861
        :param revision_id: only return revision ids included by this
1862
                            revision_id.
1863
        """
1864
        # generic, possibly worst case, slow code path.
1865
        target_ids = set(self.target.all_revision_ids())
1866
        if revision_id is not None:
1867
            source_ids = self.source.get_ancestry(revision_id)
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
1868
            assert source_ids[0] is None
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
1869
            source_ids.pop(0)
1870
        else:
1871
            source_ids = self.source.all_revision_ids()
1872
        result_set = set(source_ids).difference(target_ids)
1873
        # this may look like a no-op: its not. It preserves the ordering
1874
        # other_ids had while only returning the members from other_ids
1875
        # that we've decided we need.
1876
        return [rev_id for rev_id in source_ids if rev_id in result_set]
1877
1878
1879
class InterSameDataRepository(InterRepository):
1880
    """Code for converting between repositories that represent the same data.
1881
    
1882
    Data format and model must match for this to work.
1883
    """
1884
1885
    _matching_repo_format = RepositoryFormat4()
1886
    """Repository format for testing with."""
1887
1910.2.14 by Aaron Bentley
Fail when trying to use interrepository on Knit2 and Knit1
1888
    @staticmethod
1889
    def is_compatible(source, target):
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
1890
        if not isinstance(source, Repository):
1891
            return False
1892
        if not isinstance(target, Repository):
1893
            return False
1910.2.14 by Aaron Bentley
Fail when trying to use interrepository on Knit2 and Knit1
1894
        if source._format.rich_root_data == target._format.rich_root_data:
1895
            return True
1896
        else:
1897
            return False
1898
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.
1899
    @needs_write_lock
1900
    def copy_content(self, revision_id=None, basis=None):
1901
        """Make a complete copy of the content in self into destination.
1902
        
1903
        This is a destructive operation! Do not use it on existing 
1904
        repositories.
1905
1906
        :param revision_id: Only copy the content needed to construct
1907
                            revision_id and its parents.
1908
        :param basis: Copy the needed data preferentially from basis.
1909
        """
1910
        try:
1911
            self.target.set_make_working_trees(self.source.make_working_trees())
1912
        except NotImplementedError:
1913
            pass
1914
        # grab the basis available data
1915
        if basis is not None:
1916
            self.target.fetch(basis, revision_id=revision_id)
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
1917
        # but don't bother fetching if we have the needed data now.
1996.3.20 by John Arbash Meinel
[merge] bzr.dev 2063
1918
        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.
1919
            self.target.has_revision(revision_id)):
1920
            return
1921
        self.target.fetch(self.source, revision_id=revision_id)
1922
1923
    @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.
1924
    def fetch(self, revision_id=None, pb=None):
1910.7.20 by Andrew Bennetts
Merge from bzr.dev
1925
        """See InterRepository.fetch()."""
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1926
        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.
1927
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
1928
               self.source, self.source._format, self.target, 
1929
               self.target._format)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
1930
        f = GenericRepoFetcher(to_repository=self.target,
1931
                               from_repository=self.source,
1932
                               last_revision=revision_id,
1933
                               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.
1934
        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.
1935
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
1936
1937
class InterWeaveRepo(InterSameDataRepository):
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.
1938
    """Optimised code paths between Weave based repositories."""
1939
1666.1.6 by Robert Collins
Make knit the default format.
1940
    _matching_repo_format = RepositoryFormat7()
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.
1941
    """Repository format for testing with."""
1942
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.
1943
    @staticmethod
1944
    def is_compatible(source, target):
1945
        """Be compatible with known Weave formats.
1946
        
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
1947
        We don't test for the stores being of specific types because that
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.
1948
        could lead to confusing results, and there is no need to be 
1949
        overly general.
1950
        """
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.
1951
        try:
1952
            return (isinstance(source._format, (RepositoryFormat5,
1953
                                                RepositoryFormat6,
1954
                                                RepositoryFormat7)) and
1955
                    isinstance(target._format, (RepositoryFormat5,
1956
                                                RepositoryFormat6,
1957
                                                RepositoryFormat7)))
1958
        except AttributeError:
1959
            return False
1960
    
1961
    @needs_write_lock
1962
    def copy_content(self, revision_id=None, basis=None):
1963
        """See InterRepository.copy_content()."""
1964
        # weave specific optimised path:
1965
        if basis is not None:
1966
            # copy the basis in, then fetch remaining data.
1967
            basis.copy_content_into(self.target, revision_id)
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
1968
            # the basis copy_content_into could miss-set this.
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.
1969
            try:
1970
                self.target.set_make_working_trees(self.source.make_working_trees())
1971
            except NotImplementedError:
1972
                pass
1973
            self.target.fetch(self.source, revision_id=revision_id)
1974
        else:
1975
            try:
1976
                self.target.set_make_working_trees(self.source.make_working_trees())
1977
            except NotImplementedError:
1978
                pass
1979
            # FIXME do not peek!
1980
            if self.source.control_files._transport.listable():
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
1981
                pb = ui.ui_factory.nested_progress_bar()
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
1982
                try:
1563.2.37 by Robert Collins
Merge in nested progress bars
1983
                    self.target.weave_store.copy_all_ids(
1984
                        self.source.weave_store,
1985
                        pb=pb,
1986
                        from_transaction=self.source.get_transaction(),
1987
                        to_transaction=self.target.get_transaction())
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
1988
                    pb.update('copying inventory', 0, 1)
1989
                    self.target.control_weaves.copy_multi(
1563.2.37 by Robert Collins
Merge in nested progress bars
1990
                        self.source.control_weaves, ['inventory'],
1991
                        from_transaction=self.source.get_transaction(),
1992
                        to_transaction=self.target.get_transaction())
1993
                    self.target._revision_store.text_store.copy_all_ids(
1994
                        self.source._revision_store.text_store,
1995
                        pb=pb)
1594.1.3 by Robert Collins
Fixup pb usage to use nested_progress_bar.
1996
                finally:
1997
                    pb.finished()
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.
1998
            else:
1999
                self.target.fetch(self.source, revision_id=revision_id)
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.
2000
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.
2001
    @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.
2002
    def fetch(self, revision_id=None, pb=None):
2003
        """See InterRepository.fetch()."""
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2004
        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.
2005
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2006
               self.source, self.source._format, self.target, self.target._format)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2007
        f = GenericRepoFetcher(to_repository=self.target,
2008
                               from_repository=self.source,
2009
                               last_revision=revision_id,
2010
                               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.
2011
        return f.count_copied, f.failed_revisions
2012
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
2013
    @needs_read_lock
2014
    def missing_revision_ids(self, revision_id=None):
2015
        """See InterRepository.missing_revision_ids()."""
2016
        # we want all revisions to satisfy revision_id in source.
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
2017
        # but we don't want to stat every file here and there.
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
2018
        # we want then, all revisions other needs to satisfy revision_id 
2019
        # checked, but not those that we have locally.
2020
        # so the first thing is to get a subset of the revisions to 
2021
        # satisfy revision_id in source, and then eliminate those that
2022
        # we do already have. 
2023
        # this is slow on high latency connection to self, but as as this
2024
        # disk format scales terribly for push anyway due to rewriting 
2025
        # inventory.weave, this is considered acceptable.
2026
        # - RBC 20060209
2027
        if revision_id is not None:
2028
            source_ids = self.source.get_ancestry(revision_id)
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
2029
            assert source_ids[0] is None
1668.1.14 by Martin Pool
merge olaf - InvalidRevisionId fixes
2030
            source_ids.pop(0)
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
2031
        else:
2032
            source_ids = self.source._all_possible_ids()
2033
        source_ids_set = set(source_ids)
2034
        # source_ids is the worst possible case we may need to pull.
2035
        # 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.
2036
        # have in target, but don't try to check for existence where we know
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
2037
        # we do not have a revision as that would be pointless.
2038
        target_ids = set(self.target._all_possible_ids())
2039
        possibly_present_revisions = target_ids.intersection(source_ids_set)
2040
        actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
2041
        required_revisions = source_ids_set.difference(actually_present_revisions)
2042
        required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
2043
        if revision_id is not None:
2044
            # we used get_ancestry to determine source_ids then we are assured all
2045
            # revisions referenced are present as they are installed in topological order.
2046
            # and the tip revision was validated by get_ancestry.
2047
            return required_topo_revisions
2048
        else:
2049
            # if we just grabbed the possibly available ids, then 
2050
            # we only have an estimate of whats available and need to validate
2051
            # that against the revision records.
2052
            return self.source._eliminate_revisions_not_present(required_topo_revisions)
2053
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.
2054
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
2055
class InterKnitRepo(InterSameDataRepository):
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2056
    """Optimised code paths between Knit based repositories."""
2057
2058
    _matching_repo_format = RepositoryFormatKnit1()
2059
    """Repository format for testing with."""
2060
2061
    @staticmethod
2062
    def is_compatible(source, target):
2063
        """Be compatible with known Knit formats.
2064
        
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
2065
        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.
2066
        could lead to confusing results, and there is no need to be 
2067
        overly general.
2068
        """
2069
        try:
2070
            return (isinstance(source._format, (RepositoryFormatKnit1)) and
2071
                    isinstance(target._format, (RepositoryFormatKnit1)))
2072
        except AttributeError:
2073
            return False
2074
2075
    @needs_write_lock
2076
    def fetch(self, revision_id=None, pb=None):
2077
        """See InterRepository.fetch()."""
2078
        from bzrlib.fetch import KnitRepoFetcher
2079
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2080
               self.source, self.source._format, self.target, self.target._format)
2081
        f = KnitRepoFetcher(to_repository=self.target,
2082
                            from_repository=self.source,
2083
                            last_revision=revision_id,
2084
                            pb=pb)
2085
        return f.count_copied, f.failed_revisions
2086
2087
    @needs_read_lock
2088
    def missing_revision_ids(self, revision_id=None):
2089
        """See InterRepository.missing_revision_ids()."""
2090
        if revision_id is not None:
2091
            source_ids = self.source.get_ancestry(revision_id)
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
2092
            assert source_ids[0] is None
1668.1.14 by Martin Pool
merge olaf - InvalidRevisionId fixes
2093
            source_ids.pop(0)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2094
        else:
2095
            source_ids = self.source._all_possible_ids()
2096
        source_ids_set = set(source_ids)
2097
        # source_ids is the worst possible case we may need to pull.
2098
        # 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.
2099
        # 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.
2100
        # we do not have a revision as that would be pointless.
2101
        target_ids = set(self.target._all_possible_ids())
2102
        possibly_present_revisions = target_ids.intersection(source_ids_set)
2103
        actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
2104
        required_revisions = source_ids_set.difference(actually_present_revisions)
2105
        required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
2106
        if revision_id is not None:
2107
            # we used get_ancestry to determine source_ids then we are assured all
2108
            # revisions referenced are present as they are installed in topological order.
2109
            # and the tip revision was validated by get_ancestry.
2110
            return required_topo_revisions
2111
        else:
2112
            # if we just grabbed the possibly available ids, then 
2113
            # we only have an estimate of whats available and need to validate
2114
            # that against the revision records.
2115
            return self.source._eliminate_revisions_not_present(required_topo_revisions)
2116
1910.2.17 by Aaron Bentley
Get fetching from 1 to 2 under test
2117
1910.2.24 by Aaron Bentley
Got intra-repository fetch working between model1 and 2 for all types
2118
class InterModel1and2(InterRepository):
2119
2120
    _matching_repo_format = None
2121
2122
    @staticmethod
2123
    def is_compatible(source, target):
2124
        if not isinstance(source, Repository):
2125
            return False
2126
        if not isinstance(target, Repository):
2127
            return False
2128
        if not source._format.rich_root_data and target._format.rich_root_data:
2129
            return True
2130
        else:
2131
            return False
2132
2133
    @needs_write_lock
2134
    def fetch(self, revision_id=None, pb=None):
2135
        """See InterRepository.fetch()."""
2136
        from bzrlib.fetch import Model1toKnit2Fetcher
2137
        f = Model1toKnit2Fetcher(to_repository=self.target,
2138
                                 from_repository=self.source,
2139
                                 last_revision=revision_id,
2140
                                 pb=pb)
2141
        return f.count_copied, f.failed_revisions
2142
1910.2.26 by Aaron Bentley
Fix up some test cases
2143
    @needs_write_lock
2144
    def copy_content(self, revision_id=None, basis=None):
2145
        """Make a complete copy of the content in self into destination.
2146
        
2147
        This is a destructive operation! Do not use it on existing 
2148
        repositories.
2149
2150
        :param revision_id: Only copy the content needed to construct
2151
                            revision_id and its parents.
2152
        :param basis: Copy the needed data preferentially from basis.
2153
        """
2154
        try:
2155
            self.target.set_make_working_trees(self.source.make_working_trees())
2156
        except NotImplementedError:
2157
            pass
2158
        # grab the basis available data
2159
        if basis is not None:
2160
            self.target.fetch(basis, revision_id=revision_id)
2161
        # but don't bother fetching if we have the needed data now.
1996.3.20 by John Arbash Meinel
[merge] bzr.dev 2063
2162
        if (revision_id not in (None, _mod_revision.NULL_REVISION) and 
1910.2.26 by Aaron Bentley
Fix up some test cases
2163
            self.target.has_revision(revision_id)):
2164
            return
2165
        self.target.fetch(self.source, revision_id=revision_id)
2166
1910.2.24 by Aaron Bentley
Got intra-repository fetch working between model1 and 2 for all types
2167
1910.2.17 by Aaron Bentley
Get fetching from 1 to 2 under test
2168
class InterKnit1and2(InterKnitRepo):
2169
1910.2.24 by Aaron Bentley
Got intra-repository fetch working between model1 and 2 for all types
2170
    _matching_repo_format = None
2171
1910.2.17 by Aaron Bentley
Get fetching from 1 to 2 under test
2172
    @staticmethod
2173
    def is_compatible(source, target):
2174
        """Be compatible with Knit1 source and Knit2 target"""
2175
        try:
2176
            return (isinstance(source._format, (RepositoryFormatKnit1)) and
2177
                    isinstance(target._format, (RepositoryFormatKnit2)))
2178
        except AttributeError:
2179
            return False
2180
2181
    @needs_write_lock
2182
    def fetch(self, revision_id=None, pb=None):
2183
        """See InterRepository.fetch()."""
2184
        from bzrlib.fetch import Knit1to2Fetcher
2185
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
2186
               self.source, self.source._format, self.target, 
2187
               self.target._format)
2188
        f = Knit1to2Fetcher(to_repository=self.target,
2189
                            from_repository=self.source,
2190
                            last_revision=revision_id,
2191
                            pb=pb)
2192
        return f.count_copied, f.failed_revisions
2193
2194
1910.2.15 by Aaron Bentley
Back out inter.get changes, make optimizers an ordered list
2195
InterRepository.register_optimiser(InterSameDataRepository)
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.
2196
InterRepository.register_optimiser(InterWeaveRepo)
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
2197
InterRepository.register_optimiser(InterKnitRepo)
1910.2.24 by Aaron Bentley
Got intra-repository fetch working between model1 and 2 for all types
2198
InterRepository.register_optimiser(InterModel1and2)
1910.2.17 by Aaron Bentley
Get fetching from 1 to 2 under test
2199
InterRepository.register_optimiser(InterKnit1and2)
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.
2200
2201
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
2202
class RepositoryTestProviderAdapter(object):
2203
    """A tool to generate a suite testing multiple repository formats at once.
2204
2205
    This is done by copying the test once for each transport and injecting
2206
    the transport_server, transport_readonly_server, and bzrdir_format and
2207
    repository_format classes into each copy. Each copy is also given a new id()
2208
    to make it easy to identify.
2209
    """
2210
2211
    def __init__(self, transport_server, transport_readonly_server, formats):
2212
        self._transport_server = transport_server
2213
        self._transport_readonly_server = transport_readonly_server
2214
        self._formats = formats
2215
    
2216
    def adapt(self, test):
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
2217
        result = unittest.TestSuite()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
2218
        for repository_format, bzrdir_format in self._formats:
2219
            new_test = deepcopy(test)
2220
            new_test.transport_server = self._transport_server
2221
            new_test.transport_readonly_server = self._transport_readonly_server
2222
            new_test.bzrdir_format = bzrdir_format
2223
            new_test.repository_format = repository_format
2224
            def make_new_test_id():
2225
                new_id = "%s(%s)" % (new_test.id(), repository_format.__class__.__name__)
2226
                return lambda: new_id
2227
            new_test.id = make_new_test_id()
2228
            result.addTest(new_test)
2229
        return result
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
2230
2231
2232
class InterRepositoryTestProviderAdapter(object):
2233
    """A tool to generate a suite testing multiple inter repository formats.
2234
2235
    This is done by copying the test once for each interrepo provider and injecting
2236
    the transport_server, transport_readonly_server, repository_format and 
2237
    repository_to_format classes into each copy.
2238
    Each copy is also given a new id() to make it easy to identify.
2239
    """
2240
2241
    def __init__(self, transport_server, transport_readonly_server, formats):
2242
        self._transport_server = transport_server
2243
        self._transport_readonly_server = transport_readonly_server
2244
        self._formats = formats
2245
    
2246
    def adapt(self, test):
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
2247
        result = unittest.TestSuite()
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
2248
        for interrepo_class, repository_format, repository_format_to in self._formats:
2249
            new_test = deepcopy(test)
2250
            new_test.transport_server = self._transport_server
2251
            new_test.transport_readonly_server = self._transport_readonly_server
2252
            new_test.interrepo_class = interrepo_class
2253
            new_test.repository_format = repository_format
2254
            new_test.repository_format_to = repository_format_to
2255
            def make_new_test_id():
2256
                new_id = "%s(%s)" % (new_test.id(), interrepo_class.__name__)
2257
                return lambda: new_id
2258
            new_test.id = make_new_test_id()
2259
            result.addTest(new_test)
2260
        return result
2261
2262
    @staticmethod
2263
    def default_test_list():
2264
        """Generate the default list of interrepo permutations to test."""
2265
        result = []
2266
        # test the default InterRepository between format 6 and the current 
2267
        # default format.
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.
2268
        # XXX: robertc 20060220 reinstate this when there are two supported
2269
        # formats which do not have an optimal code path between them.
1910.2.24 by Aaron Bentley
Got intra-repository fetch working between model1 and 2 for all types
2270
        #result.append((InterRepository,
2271
        #               RepositoryFormat6(),
2272
        #               RepositoryFormatKnit1()))
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
2273
        for optimiser in InterRepository._optimisers:
1910.2.24 by Aaron Bentley
Got intra-repository fetch working between model1 and 2 for all types
2274
            if optimiser._matching_repo_format is not None:
2275
                result.append((optimiser,
2276
                               optimiser._matching_repo_format,
2277
                               optimiser._matching_repo_format
2278
                               ))
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
2279
        # if there are specific combinations we want to use, we can add them 
2280
        # here.
1910.2.24 by Aaron Bentley
Got intra-repository fetch working between model1 and 2 for all types
2281
        result.append((InterModel1and2, RepositoryFormat5(),
2282
                       RepositoryFormatKnit2()))
2283
        result.append((InterKnit1and2, RepositoryFormatKnit1(),
2284
                       RepositoryFormatKnit2()))
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
2285
        return result
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.
2286
2287
2288
class CopyConverter(object):
2289
    """A repository conversion tool which just performs a copy of the content.
2290
    
2291
    This is slow but quite reliable.
2292
    """
2293
2294
    def __init__(self, target_format):
2295
        """Create a CopyConverter.
2296
2297
        :param target_format: The format the resulting repository should be.
2298
        """
2299
        self.target_format = target_format
2300
        
2301
    def convert(self, repo, pb):
2302
        """Perform the conversion of to_convert, giving feedback via pb.
2303
2304
        :param to_convert: The disk object to convert.
2305
        :param pb: a progress bar to use for progress information.
2306
        """
2307
        self.pb = pb
2308
        self.count = 0
1596.2.22 by Robert Collins
Fetch changes to use new pb.
2309
        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.
2310
        # this is only useful with metadir layouts - separated repo content.
2311
        # trigger an assertion if not such
2312
        repo._format.get_format_string()
2313
        self.repo_dir = repo.bzrdir
2314
        self.step('Moving repository to repository.backup')
2315
        self.repo_dir.transport.move('repository', 'repository.backup')
2316
        backup_transport =  self.repo_dir.transport.clone('repository.backup')
1910.2.12 by Aaron Bentley
Implement knit repo format 2
2317
        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.
2318
        self.source_repo = repo._format.open(self.repo_dir,
2319
            _found=True,
2320
            _override_transport=backup_transport)
2321
        self.step('Creating new repository')
2322
        converted = self.target_format.initialize(self.repo_dir,
2323
                                                  self.source_repo.is_shared())
2324
        converted.lock_write()
2325
        try:
2326
            self.step('Copying content into repository.')
2327
            self.source_repo.copy_content_into(converted)
2328
        finally:
2329
            converted.unlock()
2330
        self.step('Deleting old repository content.')
2331
        self.repo_dir.transport.delete_tree('repository.backup')
2332
        self.pb.note('repository converted')
2333
2334
    def step(self, message):
2335
        """Update the pb by a step."""
2336
        self.count +=1
2337
        self.pb.update(message, self.count, self.total)
1596.1.1 by Martin Pool
Use simple xml unescaping rather than importing xml.sax
2338
2339
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
2340
class CommitBuilder(object):
2341
    """Provides an interface to build up a commit.
2342
2343
    This allows describing a tree to be committed without needing to 
2344
    know the internals of the format of the repository.
2345
    """
1910.2.4 by Aaron Bentley
Support old CommitBuilders
2346
    
2347
    record_root_entry = False
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
2348
    def __init__(self, repository, parents, config, timestamp=None, 
2349
                 timezone=None, committer=None, revprops=None, 
2350
                 revision_id=None):
2351
        """Initiate a CommitBuilder.
2352
2353
        :param repository: Repository to commit to.
2354
        :param parents: Revision ids of the parents of the new revision.
2355
        :param config: Configuration to use.
2356
        :param timestamp: Optional timestamp recorded for commit.
2357
        :param timezone: Optional timezone for timestamp.
2358
        :param committer: Optional committer to set for commit.
2359
        :param revprops: Optional dictionary of revision properties.
2360
        :param revision_id: Optional revision id.
2361
        """
2362
        self._config = config
2363
2364
        if committer is None:
2365
            self._committer = self._config.username()
2366
        else:
2367
            assert isinstance(committer, basestring), type(committer)
2368
            self._committer = committer
2369
1731.1.33 by Aaron Bentley
Revert no-special-root changes
2370
        self.new_inventory = Inventory(None)
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
2371
        self._new_revision_id = revision_id
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
2372
        self.parents = parents
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
2373
        self.repository = repository
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
2374
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
2375
        self._revprops = {}
2376
        if revprops is not None:
2377
            self._revprops.update(revprops)
2378
2379
        if timestamp is None:
1864.2.1 by John Arbash Meinel
Commit timestamp restricted to 1ms precision.
2380
            timestamp = time.time()
2381
        # Restrict resolution to 1ms
2382
        self._timestamp = round(timestamp, 3)
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
2383
2384
        if timezone is None:
2385
            self._timezone = local_time_offset()
2386
        else:
2387
            self._timezone = int(timezone)
2388
2389
        self._generate_revision_if_needed()
2390
1740.3.9 by Jelmer Vernooij
Make the commit message the first argument of CommitBuilder.commit().
2391
    def commit(self, message):
1740.3.8 by Jelmer Vernooij
Move make_revision() to commit builder.
2392
        """Make the actual commit.
2393
2394
        :return: The revision id of the recorded revision.
2395
        """
1996.3.4 by John Arbash Meinel
lazy_import bzrlib/repository.py
2396
        rev = _mod_revision.Revision(
2397
                       timestamp=self._timestamp,
1740.3.8 by Jelmer Vernooij
Move make_revision() to commit builder.
2398
                       timezone=self._timezone,
2399
                       committer=self._committer,
1740.3.9 by Jelmer Vernooij
Make the commit message the first argument of CommitBuilder.commit().
2400
                       message=message,
1740.3.8 by Jelmer Vernooij
Move make_revision() to commit builder.
2401
                       inventory_sha1=self.inv_sha1,
2402
                       revision_id=self._new_revision_id,
2403
                       properties=self._revprops)
2404
        rev.parent_ids = self.parents
2405
        self.repository.add_revision(self._new_revision_id, rev, 
2406
            self.new_inventory, self._config)
2407
        return self._new_revision_id
2408
2041.1.5 by John Arbash Meinel
CommitBuilder.get_tree => CommitBuilder.revision_tree
2409
    def revision_tree(self):
2041.1.1 by John Arbash Meinel
Add a 'get_tree()' call that returns a RevisionTree for the newly committed tree
2410
        """Return the tree that was just committed.
2411
2412
        After calling commit() this can be called to get a RevisionTree
2413
        representing the newly committed tree. This is preferred to
2414
        calling Repository.revision_tree() because that may require
2415
        deserializing the inventory, while we already have a copy in
2416
        memory.
2417
        """
2418
        return RevisionTree(self.repository, self.new_inventory,
2419
                            self._new_revision_id)
2420
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
2421
    def finish_inventory(self):
1740.3.9 by Jelmer Vernooij
Make the commit message the first argument of CommitBuilder.commit().
2422
        """Tell the builder that the inventory is finished."""
1910.2.3 by Aaron Bentley
All tests pass
2423
        if self.new_inventory.root is None:
1910.2.9 by Aaron Bentley
Inroduce assertDeprecated, and use it to test old commitbuilder API
2424
            symbol_versioning.warn('Root entry should be supplied to'
2425
                ' record_entry_contents, as of bzr 0.10.',
1910.2.3 by Aaron Bentley
All tests pass
2426
                 DeprecationWarning, stacklevel=2)
2427
            self.new_inventory.add(InventoryDirectory(ROOT_ID, '', None))
1757.1.2 by Robert Collins
Bugfix CommitBuilders recording of the inventory revision id.
2428
        self.new_inventory.revision_id = self._new_revision_id
1740.3.8 by Jelmer Vernooij
Move make_revision() to commit builder.
2429
        self.inv_sha1 = self.repository.add_inventory(
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
2430
            self._new_revision_id,
2431
            self.new_inventory,
2432
            self.parents
2433
            )
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
2434
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
2435
    def _gen_revision_id(self):
2436
        """Return new revision-id."""
2116.4.1 by John Arbash Meinel
Update file and revision id generators.
2437
        return generate_ids.gen_revision_id(self._config.username(),
2438
                                            self._timestamp)
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
2439
2440
    def _generate_revision_if_needed(self):
2441
        """Create a revision id if None was supplied.
2442
        
2443
        If the repository can not support user-specified revision ids
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.
2444
        they should override this function and raise CannotSetRevisionId
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
2445
        if _new_revision_id is not None.
2446
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.
2447
        :raises: CannotSetRevisionId
1740.3.3 by Jelmer Vernooij
Move storing directories and links to commit builder.
2448
        """
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
2449
        if self._new_revision_id is None:
2450
            self._new_revision_id = self._gen_revision_id()
1740.3.3 by Jelmer Vernooij
Move storing directories and links to commit builder.
2451
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
2452
    def record_entry_contents(self, ie, parent_invs, path, tree):
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
2453
        """Record the content of ie from tree into the commit if needed.
2454
1910.2.3 by Aaron Bentley
All tests pass
2455
        Side effect: sets ie.revision when unchanged
2456
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
2457
        :param ie: An inventory entry present in the commit.
2458
        :param parent_invs: The inventories of the parent revisions of the
2459
            commit.
2460
        :param path: The path the entry is at in the tree.
2461
        :param tree: The tree which contains this entry and should be used to 
2462
        obtain content.
2463
        """
1910.2.8 by Aaron Bentley
Fix commit_builder when root not passed to record_entry_contents
2464
        if self.new_inventory.root is None and ie.parent_id is not None:
1910.2.9 by Aaron Bentley
Inroduce assertDeprecated, and use it to test old commitbuilder API
2465
            symbol_versioning.warn('Root entry should be supplied to'
2466
                ' record_entry_contents, as of bzr 0.10.',
1910.2.8 by Aaron Bentley
Fix commit_builder when root not passed to record_entry_contents
2467
                 DeprecationWarning, stacklevel=2)
2468
            self.record_entry_contents(tree.inventory.root.copy(), parent_invs,
2469
                                       '', tree)
1907.1.1 by Aaron Bentley
Unshelved all changes except those related to removing RootEntry
2470
        self.new_inventory.add(ie)
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
2471
1740.3.4 by Jelmer Vernooij
Move inventory to commit builder.
2472
        # ie.revision is always None if the InventoryEntry is considered
2473
        # for committing. ie.snapshot will record the correct revision 
2474
        # which may be the sole parent if it is untouched.
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
2475
        if ie.revision is not None:
2476
            return
1910.2.3 by Aaron Bentley
All tests pass
2477
2478
        # In this revision format, root entries have no knit or weave
2479
        if ie is self.new_inventory.root:
2044.1.1 by Robert Collins
(Robert Collins) Forward merge from 0.11rc2 NEWS and performance-regression fix.
2480
            # When serializing out to disk and back in
2481
            # root.revision is always _new_revision_id
2482
            ie.revision = self._new_revision_id
1910.2.3 by Aaron Bentley
All tests pass
2483
            return
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
2484
        previous_entries = ie.find_previous_heads(
2485
            parent_invs,
2486
            self.repository.weave_store,
2487
            self.repository.get_transaction())
1740.3.6 by Jelmer Vernooij
Move inventory writing to the commit builder.
2488
        # we are creating a new revision for ie in the history store
2489
        # and inventory.
1740.3.7 by Jelmer Vernooij
Move committer, log, revprops, timestamp and timezone to CommitBuilder.
2490
        ie.snapshot(self._new_revision_id, path, previous_entries, tree, self)
1740.3.3 by Jelmer Vernooij
Move storing directories and links to commit builder.
2491
2492
    def modified_directory(self, file_id, file_parents):
2493
        """Record the presence of a symbolic link.
2494
2495
        :param file_id: The file_id of the link to record.
2496
        :param file_parents: The per-file parent revision ids.
2497
        """
2498
        self._add_text_to_weave(file_id, [], file_parents.keys())
1740.3.2 by Jelmer Vernooij
Move storing file texts to commit builder.
2499
    
1740.3.3 by Jelmer Vernooij
Move storing directories and links to commit builder.
2500
    def modified_file_text(self, file_id, file_parents,
2501
                           get_content_byte_lines, text_sha1=None,
2502
                           text_size=None):
1740.3.2 by Jelmer Vernooij
Move storing file texts to commit builder.
2503
        """Record the text of file file_id
2504
2505
        :param file_id: The file_id of the file to record the text of.
2506
        :param file_parents: The per-file parent revision ids.
2507
        :param get_content_byte_lines: A callable which will return the byte
2508
            lines for the file.
2509
        :param text_sha1: Optional SHA1 of the file contents.
2510
        :param text_size: Optional size of the file contents.
2511
        """
1711.2.101 by John Arbash Meinel
Clean up some unnecessary mutter() calls
2512
        # mutter('storing text of file {%s} in revision {%s} into %r',
2513
        #        file_id, self._new_revision_id, self.repository.weave_store)
1740.3.2 by Jelmer Vernooij
Move storing file texts to commit builder.
2514
        # special case to avoid diffing on renames or 
2515
        # reparenting
2516
        if (len(file_parents) == 1
2517
            and text_sha1 == file_parents.values()[0].text_sha1
2518
            and text_size == file_parents.values()[0].text_size):
2519
            previous_ie = file_parents.values()[0]
2520
            versionedfile = self.repository.weave_store.get_weave(file_id, 
2521
                self.repository.get_transaction())
1740.3.3 by Jelmer Vernooij
Move storing directories and links to commit builder.
2522
            versionedfile.clone_text(self._new_revision_id, 
1740.3.2 by Jelmer Vernooij
Move storing file texts to commit builder.
2523
                previous_ie.revision, file_parents.keys())
2524
            return text_sha1, text_size
2525
        else:
2526
            new_lines = get_content_byte_lines()
2527
            # TODO: Rather than invoking sha_strings here, _add_text_to_weave
2528
            # should return the SHA1 and size
2529
            self._add_text_to_weave(file_id, new_lines, file_parents.keys())
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
2530
            return osutils.sha_strings(new_lines), \
1740.3.2 by Jelmer Vernooij
Move storing file texts to commit builder.
2531
                sum(map(len, new_lines))
2532
1740.3.3 by Jelmer Vernooij
Move storing directories and links to commit builder.
2533
    def modified_link(self, file_id, file_parents, link_target):
2534
        """Record the presence of a symbolic link.
2535
2536
        :param file_id: The file_id of the link to record.
2537
        :param file_parents: The per-file parent revision ids.
2538
        :param link_target: Target location of this link.
2539
        """
2540
        self._add_text_to_weave(file_id, [], file_parents.keys())
2541
1740.3.2 by Jelmer Vernooij
Move storing file texts to commit builder.
2542
    def _add_text_to_weave(self, file_id, new_lines, parents):
2543
        versionedfile = self.repository.weave_store.get_weave_or_empty(
2544
            file_id, self.repository.get_transaction())
1740.3.3 by Jelmer Vernooij
Move storing directories and links to commit builder.
2545
        versionedfile.add_lines(self._new_revision_id, parents, new_lines)
1740.3.2 by Jelmer Vernooij
Move storing file texts to commit builder.
2546
        versionedfile.clear_cache()
1740.3.1 by Jelmer Vernooij
Introduce and use CommitBuilder objects.
2547
2548
1910.2.6 by Aaron Bentley
Update for merge review, handle deprecations
2549
class _CommitBuilder(CommitBuilder):
1910.2.4 by Aaron Bentley
Support old CommitBuilders
2550
    """Temporary class so old CommitBuilders are detected properly
2551
    
2552
    Note: CommitBuilder works whether or not root entry is recorded.
2553
    """
2554
2555
    record_root_entry = True
2556
2557
1910.2.22 by Aaron Bentley
Make commits preserve root entry data
2558
class RootCommitBuilder(CommitBuilder):
2559
    """This commitbuilder actually records the root id"""
2560
    
2561
    record_root_entry = True
2562
2563
    def record_entry_contents(self, ie, parent_invs, path, tree):
2564
        """Record the content of ie from tree into the commit if needed.
2565
2566
        Side effect: sets ie.revision when unchanged
2567
2568
        :param ie: An inventory entry present in the commit.
2569
        :param parent_invs: The inventories of the parent revisions of the
2570
            commit.
2571
        :param path: The path the entry is at in the tree.
2572
        :param tree: The tree which contains this entry and should be used to 
2573
        obtain content.
2574
        """
2575
        assert self.new_inventory.root is not None or ie.parent_id is None
2576
        self.new_inventory.add(ie)
2577
2578
        # ie.revision is always None if the InventoryEntry is considered
2579
        # for committing. ie.snapshot will record the correct revision 
2580
        # which may be the sole parent if it is untouched.
2581
        if ie.revision is not None:
2582
            return
2583
2584
        previous_entries = ie.find_previous_heads(
2585
            parent_invs,
2586
            self.repository.weave_store,
2587
            self.repository.get_transaction())
2588
        # we are creating a new revision for ie in the history store
2589
        # and inventory.
2590
        ie.snapshot(self._new_revision_id, path, previous_entries, tree, self)
2591
2592
1843.2.4 by Aaron Bentley
Switch to John Meinel's _unescape_xml implementation
2593
_unescape_map = {
2594
    'apos':"'",
2595
    'quot':'"',
2596
    'amp':'&',
2597
    'lt':'<',
2598
    'gt':'>'
2599
}
2600
2601
2602
def _unescaper(match, _map=_unescape_map):
2603
    return _map[match.group(1)]
2604
2605
2606
_unescape_re = None
2607
2608
1596.1.1 by Martin Pool
Use simple xml unescaping rather than importing xml.sax
2609
def _unescape_xml(data):
1843.2.4 by Aaron Bentley
Switch to John Meinel's _unescape_xml implementation
2610
    """Unescape predefined XML entities in a string of data."""
2611
    global _unescape_re
2612
    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.
2613
        _unescape_re = re.compile('\&([^;]*);')
1843.2.4 by Aaron Bentley
Switch to John Meinel's _unescape_xml implementation
2614
    return _unescape_re.sub(_unescaper, data)