/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
1
# Copyright (C) 2005 Canonical Ltd
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1185.65.10 by Robert Collins
Rename Controlfiles to LockableFiles.
16
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
17
from copy import deepcopy
1185.65.10 by Robert Collins
Rename Controlfiles to LockableFiles.
18
from cStringIO import StringIO
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
19
from unittest import TestSuite
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
20
import xml.sax.saxutils
21
1185.65.10 by Robert Collins
Rename Controlfiles to LockableFiles.
22
1534.4.28 by Robert Collins
first cut at merge from integration.
23
from bzrlib.decorators import needs_read_lock, needs_write_lock
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
24
import bzrlib.errors as errors
1534.4.28 by Robert Collins
first cut at merge from integration.
25
from bzrlib.errors import InvalidRevisionId
1185.66.3 by Aaron Bentley
Renamed ControlFiles to LockableFiles
26
from bzrlib.lockable_files import LockableFiles
1534.4.28 by Robert Collins
first cut at merge from integration.
27
from bzrlib.osutils import safe_unicode
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
28
from bzrlib.revision import NULL_REVISION
1185.65.14 by Robert Collins
Merge from aaron. Whee, we are synced. Yay. Begone the foul demons of merge conflicts.
29
from bzrlib.store import copy_all
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
30
from bzrlib.store.weave import WeaveStore
31
from bzrlib.store.text import TextStore
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
32
from bzrlib.symbol_versioning import *
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
33
from bzrlib.trace import mutter
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
34
from bzrlib.tree import RevisionTree
35
from bzrlib.testament import Testament
1534.4.28 by Robert Collins
first cut at merge from integration.
36
from bzrlib.tree import EmptyTree
1185.79.2 by John Arbash Meinel
Adding progress bars to copy_all and copy_multi, fixing ordering of repository.clone() to pull inventories after weaves.
37
import bzrlib.ui
1534.4.28 by Robert Collins
first cut at merge from integration.
38
import bzrlib.xml5
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
39
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
40
1185.66.5 by Aaron Bentley
Renamed RevisionStorage to Repository
41
class Repository(object):
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
42
    """Repository holding history for one or more branches.
43
44
    The repository holds and retrieves historical information including
45
    revisions and file history.  It's normally accessed only by the Branch,
46
    which views a particular line of development through that history.
47
48
    The Repository builds on top of Stores and a Transport, which respectively 
49
    describe the disk data format and the way of accessing the (possibly 
50
    remote) disk.
51
    """
1185.65.17 by Robert Collins
Merge from integration, mode-changes are broken.
52
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
53
    @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.
54
    def _all_possible_ids(self):
55
        """Return all the possible revisions that we could find."""
56
        return self.get_inventory_weave().names()
57
58
    @needs_read_lock
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
59
    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.
60
        """Returns a list of all the revision ids in the repository. 
61
62
        These are in as much topological order as the underlying store can 
63
        present: for weaves ghosts may lead to a lack of correctness until
64
        the reweave updates the parents list.
65
        """
66
        result = self._all_possible_ids()
67
        return self._eliminate_revisions_not_present(result)
68
69
    @needs_read_lock
70
    def _eliminate_revisions_not_present(self, revision_ids):
71
        """Check every revision id in revision_ids to see if we have it.
72
73
        Returns a set of the present revisions.
74
        """
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
75
        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.
76
        for id in revision_ids:
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
77
            if self.has_revision(id):
78
               result.append(id)
79
        return result
80
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
81
    @staticmethod
82
    def create(a_bzrdir):
83
        """Construct the current default format repository in a_bzrdir."""
84
        return RepositoryFormat.get_default_format().initialize(a_bzrdir)
85
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
86
    def __init__(self, _format, a_bzrdir, control_files, revision_store):
87
        """instantiate a Repository.
88
89
        :param _format: The format of the repository on disk.
90
        :param a_bzrdir: The BzrDir of the repository.
91
92
        In the future we will have a single api for all stores for
93
        getting file texts, inventories and revisions, then
94
        this construct will accept instances of those things.
95
        """
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
96
        object.__init__(self)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
97
        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
98
        # 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.
99
        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
100
        self.control_files = control_files
101
        self.revision_store = revision_store
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
102
103
    def lock_write(self):
104
        self.control_files.lock_write()
105
106
    def lock_read(self):
107
        self.control_files.lock_read()
108
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.
109
    @needs_read_lock
110
    def missing_revision_ids(self, other, revision_id=None):
111
        """Return the revision ids that other has that this does not.
112
        
113
        These are returned in topological order.
114
115
        revision_id: only return revision ids included by revision_id.
116
        """
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
117
        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.
118
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
119
    @staticmethod
120
    def open(base):
121
        """Open the repository rooted at base.
122
123
        For instance, if the repository is at URL/.bzr/repository,
124
        Repository.open(URL) -> a Repository instance.
125
        """
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.
126
        control = bzrlib.bzrdir.BzrDir.open(base)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
127
        return control.open_repository()
128
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.
129
    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.
130
        """Make a complete copy of the content in self into destination.
131
        
132
        This is a destructive operation! Do not use it on existing 
133
        repositories.
134
        """
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.
135
        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.
136
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.
137
    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.
138
        """Fetch the content required to construct revision_id from source.
139
140
        If revision_id is None all content is copied.
141
        """
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.
142
        return InterRepository.get(source, self).fetch(revision_id=revision_id,
143
                                                       pb=pb)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
144
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
145
    def unlock(self):
146
        self.control_files.unlock()
147
1185.65.27 by Robert Collins
Tweak storage towards mergability.
148
    @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.
149
    def clone(self, a_bzrdir, revision_id=None, basis=None):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
150
        """Clone this repository into a_bzrdir using the current format.
151
152
        Currently no check is made that the format of this repository and
153
        the bzrdir format are compatible. FIXME RBC 20060201.
154
        """
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.
155
        if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
156
            # use target default format.
157
            result = a_bzrdir.create_repository()
158
        # FIXME RBC 20060209 split out the repository type to avoid this check ?
159
        elif isinstance(a_bzrdir._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.
160
                      (bzrlib.bzrdir.BzrDirFormat4,
161
                       bzrlib.bzrdir.BzrDirFormat5,
162
                       bzrlib.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.
163
            result = a_bzrdir.open_repository()
164
        else:
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
165
            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.
166
        self.copy_content_into(result, revision_id, basis)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
167
        return result
168
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
169
    def has_revision(self, revision_id):
170
        """True if this branch has a copy of the revision.
171
172
        This does not necessarily imply the revision is merge
173
        or on the mainline."""
174
        return (revision_id is None
175
                or self.revision_store.has_id(revision_id))
176
177
    @needs_read_lock
178
    def get_revision_xml_file(self, revision_id):
179
        """Return XML file object for revision object."""
180
        if not revision_id or not isinstance(revision_id, basestring):
181
            raise InvalidRevisionId(revision_id=revision_id, branch=self)
182
        try:
183
            return self.revision_store.get(revision_id)
184
        except (IndexError, KeyError):
185
            raise bzrlib.errors.NoSuchRevision(self, revision_id)
186
1185.65.27 by Robert Collins
Tweak storage towards mergability.
187
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
188
    def get_revision_xml(self, revision_id):
189
        return self.get_revision_xml_file(revision_id).read()
190
1185.65.27 by Robert Collins
Tweak storage towards mergability.
191
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
192
    def get_revision(self, revision_id):
193
        """Return the Revision object for a named revision"""
194
        xml_file = self.get_revision_xml_file(revision_id)
195
196
        try:
197
            r = bzrlib.xml5.serializer_v5.read_revision(xml_file)
198
        except SyntaxError, e:
199
            raise bzrlib.errors.BzrError('failed to unpack revision_xml',
200
                                         [revision_id,
201
                                          str(e)])
202
            
203
        assert r.revision_id == revision_id
204
        return r
205
1185.65.27 by Robert Collins
Tweak storage towards mergability.
206
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
207
    def get_revision_sha1(self, revision_id):
208
        """Hash the stored value of a revision, and return it."""
209
        # In the future, revision entries will be signed. At that
210
        # point, it is probably best *not* to include the signature
211
        # in the revision hash. Because that lets you re-sign
212
        # the revision, (add signatures/remove signatures) and still
213
        # have all hash pointers stay consistent.
214
        # But for now, just hash the contents.
215
        return bzrlib.osutils.sha_file(self.get_revision_xml_file(revision_id))
216
217
    @needs_write_lock
218
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
219
        self.revision_store.add(StringIO(gpg_strategy.sign(plaintext)), 
220
                                revision_id, "sig")
221
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
222
    def fileid_involved_between_revs(self, from_revid, to_revid):
223
        """Find file_id(s) which are involved in the changes between revisions.
224
225
        This determines the set of revisions which are involved, and then
226
        finds all file ids affected by those revisions.
227
        """
228
        # TODO: jam 20060119 This code assumes that w.inclusions will
229
        #       always be correct. But because of the presence of ghosts
230
        #       it is possible to be wrong.
231
        #       One specific example from Robert Collins:
232
        #       Two branches, with revisions ABC, and AD
233
        #       C is a ghost merge of D.
234
        #       Inclusions doesn't recognize D as an ancestor.
235
        #       If D is ever merged in the future, the weave
236
        #       won't be fixed, because AD never saw revision C
237
        #       to cause a conflict which would force a reweave.
238
        w = self.get_inventory_weave()
239
        from_set = set(w.inclusions([w.lookup(from_revid)]))
240
        to_set = set(w.inclusions([w.lookup(to_revid)]))
241
        included = to_set.difference(from_set)
242
        changed = map(w.idx_to_name, included)
243
        return self._fileid_involved_by_set(changed)
244
245
    def fileid_involved(self, last_revid=None):
246
        """Find all file_ids modified in the ancestry of last_revid.
247
248
        :param last_revid: If None, last_revision() will be used.
249
        """
250
        w = self.get_inventory_weave()
251
        if not last_revid:
252
            changed = set(w._names)
253
        else:
254
            included = w.inclusions([w.lookup(last_revid)])
255
            changed = map(w.idx_to_name, included)
256
        return self._fileid_involved_by_set(changed)
257
258
    def fileid_involved_by_set(self, changes):
259
        """Find all file_ids modified by the set of revisions passed in.
260
261
        :param changes: A set() of revision ids
262
        """
263
        # TODO: jam 20060119 This line does *nothing*, remove it.
264
        #       or better yet, change _fileid_involved_by_set so
265
        #       that it takes the inventory weave, rather than
266
        #       pulling it out by itself.
267
        return self._fileid_involved_by_set(changes)
268
269
    def _fileid_involved_by_set(self, changes):
270
        """Find the set of file-ids affected by the set of revisions.
271
272
        :param changes: A set() of revision ids.
273
        :return: A set() of file ids.
274
        
275
        This peaks at the Weave, interpreting each line, looking to
276
        see if it mentions one of the revisions. And if so, includes
277
        the file id mentioned.
278
        This expects both the Weave format, and the serialization
279
        to have a single line per file/directory, and to have
280
        fileid="" and revision="" on that line.
281
        """
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
282
        assert isinstance(self._format, (RepositoryFormat5,
283
                                         RepositoryFormat6,
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
284
                                         RepositoryFormat7,
285
                                         RepositoryFormatKnit1)), \
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
286
            "fileid_involved only supported for branches which store inventory as unnested xml"
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
287
288
        w = self.get_inventory_weave()
289
        file_ids = set()
290
        for line in w._weave:
291
292
            # it is ugly, but it is due to the weave structure
293
            if not isinstance(line, basestring): continue
294
295
            start = line.find('file_id="')+9
296
            if start < 9: continue
297
            end = line.find('"', start)
298
            assert end>= 0
299
            file_id = xml.sax.saxutils.unescape(line[start:end])
300
301
            # check if file_id is already present
302
            if file_id in file_ids: continue
303
304
            start = line.find('revision="')+10
305
            if start < 10: continue
306
            end = line.find('"', start)
307
            assert end>= 0
308
            revision_id = xml.sax.saxutils.unescape(line[start:end])
309
310
            if revision_id in changes:
311
                file_ids.add(file_id)
312
        return file_ids
313
1185.65.27 by Robert Collins
Tweak storage towards mergability.
314
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
315
    def get_inventory_weave(self):
316
        return self.control_weaves.get_weave('inventory',
317
            self.get_transaction())
318
1185.65.27 by Robert Collins
Tweak storage towards mergability.
319
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
320
    def get_inventory(self, revision_id):
321
        """Get Inventory object by hash."""
322
        xml = self.get_inventory_xml(revision_id)
323
        return bzrlib.xml5.serializer_v5.read_inventory_from_string(xml)
324
1185.65.27 by Robert Collins
Tweak storage towards mergability.
325
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
326
    def get_inventory_xml(self, revision_id):
327
        """Get inventory XML as a file object."""
328
        try:
329
            assert isinstance(revision_id, basestring), type(revision_id)
330
            iw = self.get_inventory_weave()
331
            return iw.get_text(iw.lookup(revision_id))
332
        except IndexError:
333
            raise bzrlib.errors.HistoryMissing(self, 'inventory', revision_id)
334
1185.65.27 by Robert Collins
Tweak storage towards mergability.
335
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
336
    def get_inventory_sha1(self, revision_id):
337
        """Return the sha1 hash of the inventory entry
338
        """
339
        return self.get_revision(revision_id).inventory_sha1
340
1185.65.27 by Robert Collins
Tweak storage towards mergability.
341
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
342
    def get_revision_inventory(self, revision_id):
343
        """Return inventory of a past revision."""
344
        # TODO: Unify this with get_inventory()
345
        # bzr 0.0.6 and later imposes the constraint that the inventory_id
346
        # must be the same as its revision, so this is trivial.
1534.4.28 by Robert Collins
first cut at merge from integration.
347
        if revision_id is None:
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
348
            # This does not make sense: if there is no revision,
349
            # then it is the current tree inventory surely ?!
350
            # and thus get_root_id() is something that looks at the last
351
            # commit on the branch, and the get_root_id is an inventory check.
352
            raise NotImplementedError
353
            # return Inventory(self.get_root_id())
354
        else:
355
            return self.get_inventory(revision_id)
356
1185.65.27 by Robert Collins
Tweak storage towards mergability.
357
    @needs_read_lock
1534.6.3 by Robert Collins
find_repository sufficiently robust.
358
    def is_shared(self):
359
        """Return True if this repository is flagged as a shared repository."""
360
        # FIXME format 4-6 cannot be shared, this is technically faulty.
361
        return self.control_files._transport.has('shared-storage')
362
363
    @needs_read_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
364
    def revision_tree(self, revision_id):
365
        """Return Tree for a revision on this branch.
366
367
        `revision_id` may be None for the null revision, in which case
368
        an `EmptyTree` is returned."""
369
        # TODO: refactor this to use an existing revision object
370
        # so we don't need to read it in twice.
1534.4.28 by Robert Collins
first cut at merge from integration.
371
        if revision_id is None or revision_id == NULL_REVISION:
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
372
            return EmptyTree()
373
        else:
374
            inv = self.get_revision_inventory(revision_id)
1185.65.17 by Robert Collins
Merge from integration, mode-changes are broken.
375
            return RevisionTree(self, inv, revision_id)
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
376
1185.65.27 by Robert Collins
Tweak storage towards mergability.
377
    @needs_read_lock
1185.66.2 by Aaron Bentley
Moved get_ancestry to RevisionStorage
378
    def get_ancestry(self, revision_id):
379
        """Return a list of revision-ids integrated by a revision.
380
        
381
        This is topologically sorted.
382
        """
383
        if revision_id is None:
384
            return [None]
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
385
        if not self.has_revision(revision_id):
386
            raise errors.NoSuchRevision(self, revision_id)
1185.66.2 by Aaron Bentley
Moved get_ancestry to RevisionStorage
387
        w = self.get_inventory_weave()
388
        return [None] + map(w.idx_to_name,
389
                            w.inclusions([w.lookup(revision_id)]))
390
1185.65.4 by Aaron Bentley
Fixed cat command
391
    @needs_read_lock
392
    def print_file(self, file, revision_id):
1185.65.29 by Robert Collins
Implement final review suggestions.
393
        """Print `file` to stdout.
394
        
395
        FIXME RBC 20060125 as John Meinel points out this is a bad api
396
        - it writes to stdout, it assumes that that is valid etc. Fix
397
        by creating a new more flexible convenience function.
398
        """
1185.65.4 by Aaron Bentley
Fixed cat command
399
        tree = self.revision_tree(revision_id)
400
        # use inventory as it was in that revision
401
        file_id = tree.inventory.path2id(file)
402
        if not file_id:
403
            raise BzrError("%r is not present in revision %s" % (file, revno))
1185.65.15 by Robert Collins
Merge from integration.
404
            try:
405
                revno = self.revision_id_to_revno(revision_id)
406
            except errors.NoSuchRevision:
407
                # TODO: This should not be BzrError,
408
                # but NoSuchFile doesn't fit either
409
                raise BzrError('%r is not present in revision %s' 
410
                                % (file, revision_id))
411
            else:
412
                raise BzrError('%r is not present in revision %s'
413
                                % (file, revno))
1185.65.4 by Aaron Bentley
Fixed cat command
414
        tree.print_file(file_id)
415
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
416
    def get_transaction(self):
417
        return self.control_files.get_transaction()
418
1185.65.27 by Robert Collins
Tweak storage towards mergability.
419
    @needs_write_lock
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
420
    def set_make_working_trees(self, new_value):
421
        """Set the policy flag for making working trees when creating branches.
422
423
        This only applies to branches that use this repository.
424
425
        The default is 'True'.
426
        :param new_value: True to restore the default, False to disable making
427
                          working trees.
428
        """
429
        # FIXME: split out into a new class/strategy ?
430
        if isinstance(self._format, (RepositoryFormat4,
431
                                     RepositoryFormat5,
432
                                     RepositoryFormat6)):
433
            raise NotImplementedError(self.set_make_working_trees)
434
        if new_value:
435
            try:
436
                self.control_files._transport.delete('no-working-trees')
437
            except errors.NoSuchFile:
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.
438
                pass
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
439
        else:
440
            self.control_files.put_utf8('no-working-trees', '')
441
    
442
    def make_working_trees(self):
443
        """Returns the policy for making working trees on new branches."""
444
        # FIXME: split out into a new class/strategy ?
445
        if isinstance(self._format, (RepositoryFormat4,
446
                                     RepositoryFormat5,
447
                                     RepositoryFormat6)):
448
            return True
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.
449
        return not self.control_files._transport.has('no-working-trees')
1534.6.5 by Robert Collins
Cloning of repos preserves shared and make-working-tree attributes.
450
451
    @needs_write_lock
1185.65.1 by Aaron Bentley
Refactored out ControlFiles and RevisionStore from _Branch
452
    def sign_revision(self, revision_id, gpg_strategy):
453
        plaintext = Testament.from_revision(self, revision_id).as_short_text()
454
        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.
455
456
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
457
class AllInOneRepository(Repository):
458
    """Legacy support - the repository behaviour for all-in-one branches."""
459
460
    def __init__(self, _format, a_bzrdir, revision_store):
461
        # we reuse one control files instance.
462
        dir_mode = a_bzrdir._control_files._dir_mode
463
        file_mode = a_bzrdir._control_files._file_mode
464
465
        def get_weave(name, prefixed=False):
466
            if name:
467
                name = safe_unicode(name)
468
            else:
469
                name = ''
470
            relpath = a_bzrdir._control_files._escape(name)
471
            weave_transport = a_bzrdir._control_files._transport.clone(relpath)
472
            ws = WeaveStore(weave_transport, prefixed=prefixed,
473
                            dir_mode=dir_mode,
474
                            file_mode=file_mode)
475
            if a_bzrdir._control_files._transport.should_cache():
476
                ws.enable_cache = True
477
            return ws
478
479
        def get_store(name, compressed=True, prefixed=False):
480
            # FIXME: This approach of assuming stores are all entirely compressed
481
            # or entirely uncompressed is tidy, but breaks upgrade from 
482
            # some existing branches where there's a mixture; we probably 
483
            # still want the option to look for both.
484
            relpath = a_bzrdir._control_files._escape(name)
485
            store = TextStore(a_bzrdir._control_files._transport.clone(relpath),
486
                              prefixed=prefixed, compressed=compressed,
487
                              dir_mode=dir_mode,
488
                              file_mode=file_mode)
489
            #if self._transport.should_cache():
490
            #    cache_path = os.path.join(self.cache_root, name)
491
            #    os.mkdir(cache_path)
492
            #    store = bzrlib.store.CachedStore(store, cache_path)
493
            return store
494
495
        # not broken out yet because the controlweaves|inventory_store
496
        # and text_store | weave_store bits are still different.
497
        if isinstance(_format, RepositoryFormat4):
498
            self.inventory_store = get_store('inventory-store')
499
            self.text_store = get_store('text-store')
500
        elif isinstance(_format, RepositoryFormat5):
501
            self.control_weaves = get_weave('')
502
            self.weave_store = get_weave('weaves')
503
        elif isinstance(_format, RepositoryFormat6):
504
            self.control_weaves = get_weave('')
505
            self.weave_store = get_weave('weaves', prefixed=True)
506
        else:
507
            raise errors.BzrError('unreachable code: unexpected repository'
508
                                  ' format.')
509
        revision_store.register_suffix('sig')
510
        super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, revision_store)
511
512
513
class MetaDirRepository(Repository):
514
    """Repositories in the new meta-dir layout."""
515
516
    def __init__(self, _format, a_bzrdir, control_files, revision_store):
517
        super(MetaDirRepository, self).__init__(_format,
518
                                                a_bzrdir,
519
                                                control_files,
520
                                                revision_store)
521
522
        dir_mode = self.control_files._dir_mode
523
        file_mode = self.control_files._file_mode
524
525
        def get_weave(name, prefixed=False):
526
            if name:
527
                name = safe_unicode(name)
528
            else:
529
                name = ''
530
            relpath = self.control_files._escape(name)
531
            weave_transport = self.control_files._transport.clone(relpath)
532
            ws = WeaveStore(weave_transport, prefixed=prefixed,
533
                            dir_mode=dir_mode,
534
                            file_mode=file_mode)
535
            if self.control_files._transport.should_cache():
536
                ws.enable_cache = True
537
            return ws
538
539
        if isinstance(self._format, RepositoryFormat7):
540
            self.control_weaves = get_weave('')
541
            self.weave_store = get_weave('weaves', prefixed=True)
542
        elif isinstance(self._format, RepositoryFormatKnit1):
543
            self.control_weaves = get_weave('')
544
            self.weave_store = get_weave('knits', prefixed=True)
545
        else:
546
            raise errors.BzrError('unreachable code: unexpected repository'
547
                                  ' format.')
548
549
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
550
class RepositoryFormat(object):
551
    """A repository format.
552
553
    Formats provide three things:
554
     * An initialization routine to construct repository data on disk.
555
     * a format string which is used when the BzrDir supports versioned
556
       children.
557
     * an open routine which returns a Repository instance.
558
559
    Formats are placed in an dict by their format string for reference 
560
    during opening. These should be subclasses of RepositoryFormat
561
    for consistency.
562
563
    Once a format is deprecated, just deprecate the initialize and open
564
    methods on the format class. Do not deprecate the object, as the 
565
    object will be created every system load.
566
567
    Common instance attributes:
568
    _matchingbzrdir - the bzrdir format that the repository format was
569
    originally written to work with. This can be used if manually
570
    constructing a bzrdir and repository, or more commonly for test suite
571
    parameterisation.
572
    """
573
574
    _default_format = None
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
575
    """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.
576
577
    _formats = {}
578
    """The known formats."""
579
580
    @classmethod
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
581
    def find_format(klass, a_bzrdir):
582
        """Return the format for the repository object in a_bzrdir."""
583
        try:
584
            transport = a_bzrdir.get_repository_transport(None)
585
            format_string = transport.get("format").read()
586
            return klass._formats[format_string]
587
        except errors.NoSuchFile:
588
            raise errors.NoRepositoryPresent(a_bzrdir)
589
        except KeyError:
590
            raise errors.UnknownFormatError(format_string)
591
592
    @classmethod
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
593
    def get_default_format(klass):
594
        """Return the current default format."""
595
        return klass._default_format
596
597
    def get_format_string(self):
598
        """Return the ASCII format string that identifies this format.
599
        
600
        Note that in pre format ?? repositories the format string is 
601
        not permitted nor written to disk.
602
        """
603
        raise NotImplementedError(self.get_format_string)
604
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
605
    def _get_revision_store(self, repo_transport, control_files):
606
        """Return the revision store object for this a_bzrdir."""
1556.1.5 by Robert Collins
Review feedback.
607
        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
608
609
    def _get_rev_store(self,
610
                   transport,
611
                   control_files,
612
                   name,
613
                   compressed=True,
614
                   prefixed=False):
615
        """Common logic for getting a revision store for a repository.
616
        
617
        see self._get_revision_store for the method to 
618
        get the store for a repository.
619
        """
620
        if name:
621
            name = safe_unicode(name)
622
        else:
623
            name = ''
624
        dir_mode = control_files._dir_mode
625
        file_mode = control_files._file_mode
626
        revision_store =TextStore(transport.clone(name),
627
                                  prefixed=prefixed,
628
                                  compressed=compressed,
629
                                  dir_mode=dir_mode,
630
                                  file_mode=file_mode)
631
        revision_store.register_suffix('sig')
632
        return revision_store
633
1534.6.1 by Robert Collins
allow API creation of shared repositories
634
    def initialize(self, a_bzrdir, shared=False):
635
        """Initialize a repository of this format in a_bzrdir.
636
637
        :param a_bzrdir: The bzrdir to put the new repository in it.
638
        :param shared: The repository should be initialized as a sharable one.
639
640
        This may raise UninitializableFormat if shared repository are not
641
        compatible the a_bzrdir.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
642
        """
643
644
    def is_supported(self):
645
        """Is this format supported?
646
647
        Supported formats must be initializable and openable.
648
        Unsupported formats may not support initialization or committing or 
649
        some other features depending on the reason for not being supported.
650
        """
651
        return True
652
653
    def open(self, a_bzrdir, _found=False):
654
        """Return an instance of this format for the bzrdir a_bzrdir.
655
        
656
        _found is a private parameter, do not use it.
657
        """
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
658
        raise NotImplementedError(self.open)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
659
660
    @classmethod
661
    def register_format(klass, format):
662
        klass._formats[format.get_format_string()] = format
663
664
    @classmethod
665
    def set_default_format(klass, format):
666
        klass._default_format = format
667
668
    @classmethod
669
    def unregister_format(klass, format):
670
        assert klass._formats[format.get_format_string()] is format
671
        del klass._formats[format.get_format_string()]
672
673
1534.6.1 by Robert Collins
allow API creation of shared repositories
674
class PreSplitOutRepositoryFormat(RepositoryFormat):
675
    """Base class for the pre split out repository formats."""
676
677
    def initialize(self, a_bzrdir, shared=False, _internal=False):
678
        """Create a weave repository.
679
        
680
        TODO: when creating split out bzr branch formats, move this to a common
681
        base for Format5, Format6. or something like that.
682
        """
683
        from bzrlib.weavefile import write_weave_v5
684
        from bzrlib.weave import Weave
685
686
        if shared:
687
            raise errors.IncompatibleFormat(self, a_bzrdir._format)
688
689
        if not _internal:
690
            # 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
691
            return self.open(a_bzrdir, _found=True)
1534.6.1 by Robert Collins
allow API creation of shared repositories
692
        
693
        # Create an empty weave
694
        sio = StringIO()
695
        bzrlib.weavefile.write_weave_v5(Weave(), sio)
696
        empty_weave = sio.getvalue()
697
698
        mutter('creating repository in %s.', a_bzrdir.transport.base)
699
        dirs = ['revision-store', 'weaves']
700
        lock_file = 'branch-lock'
701
        files = [('inventory.weave', StringIO(empty_weave)), 
702
                 ]
703
        
704
        # FIXME: RBC 20060125 dont peek under the covers
705
        # NB: no need to escape relative paths that are url safe.
706
        control_files = LockableFiles(a_bzrdir.transport, 'branch-lock')
707
        control_files.lock_write()
708
        control_files._transport.mkdir_multi(dirs,
709
                mode=control_files._dir_mode)
710
        try:
711
            for file, content in files:
712
                control_files.put(file, content)
713
        finally:
714
            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
715
        return self.open(a_bzrdir, _found=True)
716
717
    def open(self, a_bzrdir, _found=False):
718
        """See RepositoryFormat.open()."""
719
        if not _found:
720
            # we are being called directly and must probe.
721
            raise NotImplementedError
722
723
        repo_transport = a_bzrdir.get_repository_transport(None)
724
        control_files = a_bzrdir._control_files
725
        revision_store = self._get_revision_store(repo_transport, control_files)
726
        return AllInOneRepository(_format=self,
727
                                  a_bzrdir=a_bzrdir,
728
                                  revision_store=revision_store)
1534.6.1 by Robert Collins
allow API creation of shared repositories
729
730
731
class RepositoryFormat4(PreSplitOutRepositoryFormat):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
732
    """Bzr repository format 4.
733
734
    This repository format has:
735
     - flat stores
736
     - TextStores for texts, inventories,revisions.
737
738
    This format is deprecated: it indexes texts using a text id which is
739
    removed in format 5; initializationa and write support for this format
740
    has been removed.
741
    """
742
743
    def __init__(self):
744
        super(RepositoryFormat4, self).__init__()
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.
745
        self._matchingbzrdir = bzrlib.bzrdir.BzrDirFormat4()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
746
1534.6.1 by Robert Collins
allow API creation of shared repositories
747
    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.
748
        """Format 4 branches cannot be created."""
749
        raise errors.UninitializableFormat(self)
750
751
    def is_supported(self):
752
        """Format 4 is not supported.
753
754
        It is not supported because the model changed from 4 to 5 and the
755
        conversion logic is expensive - so doing it on the fly was not 
756
        feasible.
757
        """
758
        return False
759
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
760
    def _get_revision_store(self, repo_transport, control_files):
761
        """See RepositoryFormat._get_revision_store()."""
762
        return self._get_rev_store(repo_transport,
763
                                   control_files,
764
                                   'revision-store')
765
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
766
1534.6.1 by Robert Collins
allow API creation of shared repositories
767
class RepositoryFormat5(PreSplitOutRepositoryFormat):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
768
    """Bzr control format 5.
769
770
    This repository format has:
771
     - weaves for file texts and inventory
772
     - flat stores
773
     - TextStores for revisions and signatures.
774
    """
775
776
    def __init__(self):
777
        super(RepositoryFormat5, self).__init__()
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.
778
        self._matchingbzrdir = bzrlib.bzrdir.BzrDirFormat5()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
779
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
780
    def _get_revision_store(self, repo_transport, control_files):
781
        """See RepositoryFormat._get_revision_store()."""
782
        """Return the revision store object for this a_bzrdir."""
783
        return self._get_rev_store(repo_transport,
784
                                   control_files,
785
                                   'revision-store',
786
                                   compressed=False)
787
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
788
1534.6.1 by Robert Collins
allow API creation of shared repositories
789
class RepositoryFormat6(PreSplitOutRepositoryFormat):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
790
    """Bzr control format 6.
791
792
    This repository format has:
793
     - weaves for file texts and inventory
794
     - hash subdirectory based stores.
795
     - TextStores for revisions and signatures.
796
    """
797
798
    def __init__(self):
799
        super(RepositoryFormat6, self).__init__()
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.
800
        self._matchingbzrdir = bzrlib.bzrdir.BzrDirFormat6()
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
801
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
802
    def _get_revision_store(self, repo_transport, control_files):
803
        """See RepositoryFormat._get_revision_store()."""
804
        return self._get_rev_store(repo_transport,
805
                                   control_files,
806
                                   'revision-store',
807
                                   compressed=False,
808
                                   prefixed=True)
809
810
811
class MetaDirRepositoryFormat(RepositoryFormat):
812
    """Common base class for the new repositories using the metadir layour."""
813
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.
814
    def __init__(self):
815
        super(MetaDirRepositoryFormat, self).__init__()
816
        self._matchingbzrdir = bzrlib.bzrdir.BzrDirMetaFormat1()
817
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
818
    def _create_control_files(self, a_bzrdir):
819
        """Create the required files and the initial control_files object."""
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
820
        # FIXME: RBC 20060125 dont peek under the covers
821
        # NB: no need to escape relative paths that are url safe.
822
        lock_file = 'lock'
823
        repository_transport = a_bzrdir.get_repository_transport(self)
824
        repository_transport.put(lock_file, StringIO()) # TODO get the file mode from the bzrdir lock files., mode=file_mode)
825
        control_files = LockableFiles(repository_transport, 'lock')
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
826
        return control_files
827
828
    def _get_revision_store(self, repo_transport, control_files):
829
        """See RepositoryFormat._get_revision_store()."""
830
        return self._get_rev_store(repo_transport,
831
                                   control_files,
832
                                   'revision-store',
833
                                   compressed=False,
834
                                   prefixed=True,
835
                                   )
836
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.
837
    def open(self, a_bzrdir, _found=False, _override_transport=None):
838
        """See RepositoryFormat.open().
839
        
840
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
841
                                    repository at a slightly different url
842
                                    than normal. I.e. during 'upgrade'.
843
        """
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
844
        if not _found:
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.
845
            format = RepositoryFormat.find_format(a_bzrdir)
846
            assert format.__class__ ==  self.__class__
847
        if _override_transport is not None:
848
            repo_transport = _override_transport
849
        else:
850
            repo_transport = a_bzrdir.get_repository_transport(None)
851
        control_files = LockableFiles(repo_transport, 'lock')
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
852
        revision_store = self._get_revision_store(repo_transport, control_files)
853
        return MetaDirRepository(_format=self,
854
                                 a_bzrdir=a_bzrdir,
855
                                 control_files=control_files,
856
                                 revision_store=revision_store)
857
858
    def _upload_blank_content(self, a_bzrdir, dirs, files, utf8_files, shared):
859
        """Upload the initial blank content."""
860
        control_files = self._create_control_files(a_bzrdir)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
861
        control_files.lock_write()
862
        control_files._transport.mkdir_multi(dirs,
863
                mode=control_files._dir_mode)
864
        try:
865
            for file, content in files:
866
                control_files.put(file, content)
867
            for file, content in utf8_files:
868
                control_files.put_utf8(file, content)
1534.6.1 by Robert Collins
allow API creation of shared repositories
869
            if shared == True:
870
                control_files.put_utf8('shared-storage', '')
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
871
        finally:
872
            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
873
874
875
class RepositoryFormat7(MetaDirRepositoryFormat):
876
    """Bzr repository 7.
877
878
    This repository format has:
879
     - weaves for file texts and inventory
880
     - hash subdirectory based stores.
881
     - TextStores for revisions and signatures.
882
     - a format marker of its own
883
     - an optional 'shared-storage' flag
884
     - an optional 'no-working-trees' flag
885
    """
886
887
    def get_format_string(self):
888
        """See RepositoryFormat.get_format_string()."""
889
        return "Bazaar-NG Repository format 7"
890
891
    def initialize(self, a_bzrdir, shared=False):
892
        """Create a weave repository.
893
894
        :param shared: If true the repository will be initialized as a shared
895
                       repository.
896
        """
897
        from bzrlib.weavefile import write_weave_v5
898
        from bzrlib.weave import Weave
899
900
        # Create an empty weave
901
        sio = StringIO()
902
        bzrlib.weavefile.write_weave_v5(Weave(), sio)
903
        empty_weave = sio.getvalue()
904
905
        mutter('creating repository in %s.', a_bzrdir.transport.base)
906
        dirs = ['revision-store', 'weaves']
907
        files = [('inventory.weave', StringIO(empty_weave)), 
908
                 ]
909
        utf8_files = [('format', self.get_format_string())]
910
 
911
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
912
        return self.open(a_bzrdir=a_bzrdir, _found=True)
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
913
914
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
915
class RepositoryFormatKnit1(MetaDirRepositoryFormat):
916
    """Bzr repository knit format 1.
917
918
    This repository format has:
919
     - knits for file texts and inventory
920
     - hash subdirectory based stores.
921
     - knits for revisions and signatures
922
     - TextStores for revisions and signatures.
923
     - a format marker of its own
924
     - an optional 'shared-storage' flag
925
     - an optional 'no-working-trees' flag
926
    """
927
928
    def get_format_string(self):
929
        """See RepositoryFormat.get_format_string()."""
930
        return "Bazaar-NG Knit Repository Format 1"
931
932
    def initialize(self, a_bzrdir, shared=False):
933
        """Create a knit format 1 repository.
934
935
        :param shared: If true the repository will be initialized as a shared
936
                       repository.
1556.1.5 by Robert Collins
Review feedback.
937
        XXX NOTE that this current uses a Weave for testing and will become 
938
            A Knit in due course.
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
939
        """
940
        from bzrlib.weavefile import write_weave_v5
941
        from bzrlib.weave import Weave
942
943
        # Create an empty weave
944
        sio = StringIO()
945
        bzrlib.weavefile.write_weave_v5(Weave(), sio)
946
        empty_weave = sio.getvalue()
947
948
        mutter('creating repository in %s.', a_bzrdir.transport.base)
949
        dirs = ['revision-store', 'knits']
950
        files = [('inventory.weave', StringIO(empty_weave)), 
951
                 ]
952
        utf8_files = [('format', self.get_format_string())]
953
        
954
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
955
        return self.open(a_bzrdir=a_bzrdir, _found=True)
956
957
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
958
# formats which have no format string are not discoverable
959
# and not independently creatable, so are not registered.
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.
960
_default_format = RepositoryFormat7()
961
RepositoryFormat.register_format(_default_format)
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
962
RepositoryFormat.register_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.
963
RepositoryFormat.set_default_format(_default_format)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
964
_legacy_formats = [RepositoryFormat4(),
965
                   RepositoryFormat5(),
966
                   RepositoryFormat6()]
967
968
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
969
class InterRepository(object):
970
    """This class represents operations taking place between two repositories.
971
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.
972
    Its instances have methods like copy_content and fetch, and contain
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
973
    references to the source and target repositories these operations can be 
974
    carried out on.
975
976
    Often we will provide convenience methods on 'repository' which carry out
977
    operations with another repository - they will always forward to
978
    InterRepository.get(other).method_name(parameters).
979
    """
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
980
    # XXX: FIXME: FUTURE: robertc
981
    # testing of these probably requires a factory in optimiser type, and 
982
    # then a test adapter to test each type thoroughly.
983
    #
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
984
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
985
    _optimisers = set()
986
    """The available optimised InterRepository types."""
987
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
988
    def __init__(self, source, target):
989
        """Construct a default InterRepository instance. Please use 'get'.
990
        
991
        Only subclasses of InterRepository should call 
992
        InterRepository.__init__ - clients should call InterRepository.get
993
        instead which will create an optimised InterRepository if possible.
994
        """
995
        self.source = source
996
        self.target = target
997
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.
998
    @needs_write_lock
999
    def copy_content(self, revision_id=None, basis=None):
1000
        """Make a complete copy of the content in self into destination.
1001
        
1002
        This is a destructive operation! Do not use it on existing 
1003
        repositories.
1004
1005
        :param revision_id: Only copy the content needed to construct
1006
                            revision_id and its parents.
1007
        :param basis: Copy the needed data preferentially from basis.
1008
        """
1009
        try:
1010
            self.target.set_make_working_trees(self.source.make_working_trees())
1011
        except NotImplementedError:
1012
            pass
1013
        # grab the basis available data
1014
        if basis is not None:
1015
            self.target.fetch(basis, revision_id=revision_id)
1016
        # but dont both fetching if we have the needed data now.
1017
        if (revision_id not in (None, NULL_REVISION) and 
1018
            self.target.has_revision(revision_id)):
1019
            return
1020
        self.target.fetch(self.source, revision_id=revision_id)
1021
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
1022
    def _double_lock(self, lock_source, lock_target):
1023
        """Take out too locks, rolling back the first if the second throws."""
1024
        lock_source()
1025
        try:
1026
            lock_target()
1027
        except Exception:
1028
            # we want to ensure that we don't leave source locked by mistake.
1029
            # and any error on target should not confuse source.
1030
            self.source.unlock()
1031
            raise
1032
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.
1033
    @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.
1034
    def fetch(self, revision_id=None, pb=None):
1035
        """Fetch the content required to construct revision_id.
1036
1037
        The content is copied from source to target.
1038
1039
        :param revision_id: if None all content is copied, if NULL_REVISION no
1040
                            content is copied.
1041
        :param pb: optional progress bar to use for progress reports. If not
1042
                   provided a default one will be created.
1043
1044
        Returns the copied revision count and the failed revisions in a tuple:
1045
        (copied, failures).
1046
        """
1047
        from bzrlib.fetch import RepoFetcher
1048
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
1049
               self.source, self.source._format, self.target, self.target._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.
1050
        f = RepoFetcher(to_repository=self.target,
1051
                        from_repository=self.source,
1052
                        last_revision=revision_id,
1053
                        pb=pb)
1054
        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.
1055
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
1056
    @classmethod
1057
    def get(klass, repository_source, repository_target):
1058
        """Retrieve a InterRepository worker object for these repositories.
1059
1060
        :param repository_source: the repository to be the 'source' member of
1061
                                  the InterRepository instance.
1062
        :param repository_target: the repository to be the 'target' member of
1063
                                the InterRepository instance.
1064
        If an optimised InterRepository worker exists it will be used otherwise
1065
        a default InterRepository instance will be created.
1066
        """
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
1067
        for provider in klass._optimisers:
1068
            if provider.is_compatible(repository_source, repository_target):
1069
                return provider(repository_source, repository_target)
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
1070
        return InterRepository(repository_source, repository_target)
1071
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
1072
    def lock_read(self):
1073
        """Take out a logical read lock.
1074
1075
        This will lock the source branch and the target branch. The source gets
1076
        a read lock and the target a read lock.
1077
        """
1078
        self._double_lock(self.source.lock_read, self.target.lock_read)
1079
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.
1080
    def lock_write(self):
1081
        """Take out a logical write lock.
1082
1083
        This will lock the source branch and the target branch. The source gets
1084
        a read lock and the target a write lock.
1085
        """
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
1086
        self._double_lock(self.source.lock_read, self.target.lock_write)
1087
1088
    @needs_read_lock
1089
    def missing_revision_ids(self, revision_id=None):
1090
        """Return the revision ids that source has that target does not.
1091
        
1092
        These are returned in topological order.
1093
1094
        :param revision_id: only return revision ids included by this
1095
                            revision_id.
1096
        """
1097
        # generic, possibly worst case, slow code path.
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1098
        target_ids = set(self.target.all_revision_ids())
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
1099
        if revision_id is not None:
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1100
            source_ids = self.source.get_ancestry(revision_id)
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
1101
            assert source_ids.pop(0) == None
1102
        else:
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1103
            source_ids = self.source.all_revision_ids()
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
1104
        result_set = set(source_ids).difference(target_ids)
1105
        # this may look like a no-op: its not. It preserves the ordering
1106
        # other_ids had while only returning the members from other_ids
1107
        # that we've decided we need.
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1108
        return [rev_id for rev_id in source_ids if rev_id in result_set]
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.
1109
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
1110
    @classmethod
1111
    def register_optimiser(klass, optimiser):
1112
        """Register an InterRepository optimiser."""
1113
        klass._optimisers.add(optimiser)
1114
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.
1115
    def unlock(self):
1116
        """Release the locks on source and target."""
1117
        try:
1118
            self.target.unlock()
1119
        finally:
1120
            self.source.unlock()
1121
1534.1.28 by Robert Collins
Allow for optimised InterRepository selection.
1122
    @classmethod
1123
    def unregister_optimiser(klass, optimiser):
1124
        """Unregister an InterRepository optimiser."""
1125
        klass._optimisers.remove(optimiser)
1126
1534.1.27 by Robert Collins
Start InterRepository with InterRepository.get.
1127
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.
1128
class InterWeaveRepo(InterRepository):
1129
    """Optimised code paths between Weave based repositories."""
1130
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.
1131
    _matching_repo_format = _default_format
1132
    """Repository format for testing with."""
1133
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.
1134
    @staticmethod
1135
    def is_compatible(source, target):
1136
        """Be compatible with known Weave formats.
1137
        
1138
        We dont test for the stores being of specific types becase that
1139
        could lead to confusing results, and there is no need to be 
1140
        overly general.
1141
        """
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.
1142
        try:
1143
            return (isinstance(source._format, (RepositoryFormat5,
1144
                                                RepositoryFormat6,
1145
                                                RepositoryFormat7)) and
1146
                    isinstance(target._format, (RepositoryFormat5,
1147
                                                RepositoryFormat6,
1148
                                                RepositoryFormat7)))
1149
        except AttributeError:
1150
            return False
1151
    
1152
    @needs_write_lock
1153
    def copy_content(self, revision_id=None, basis=None):
1154
        """See InterRepository.copy_content()."""
1155
        # weave specific optimised path:
1156
        if basis is not None:
1157
            # copy the basis in, then fetch remaining data.
1158
            basis.copy_content_into(self.target, revision_id)
1159
            # the basis copy_content_into could misset this.
1160
            try:
1161
                self.target.set_make_working_trees(self.source.make_working_trees())
1162
            except NotImplementedError:
1163
                pass
1164
            self.target.fetch(self.source, revision_id=revision_id)
1165
        else:
1166
            try:
1167
                self.target.set_make_working_trees(self.source.make_working_trees())
1168
            except NotImplementedError:
1169
                pass
1170
            # FIXME do not peek!
1171
            if self.source.control_files._transport.listable():
1172
                pb = bzrlib.ui.ui_factory.progress_bar()
1173
                copy_all(self.source.weave_store,
1174
                    self.target.weave_store, pb=pb)
1175
                pb.update('copying inventory', 0, 1)
1176
                self.target.control_weaves.copy_multi(
1177
                    self.source.control_weaves, ['inventory'])
1178
                copy_all(self.source.revision_store,
1179
                    self.target.revision_store, pb=pb)
1180
            else:
1181
                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.
1182
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.
1183
    @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.
1184
    def fetch(self, revision_id=None, pb=None):
1185
        """See InterRepository.fetch()."""
1186
        from bzrlib.fetch import RepoFetcher
1187
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
1188
               self.source, self.source._format, self.target, self.target._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.
1189
        f = RepoFetcher(to_repository=self.target,
1190
                        from_repository=self.source,
1191
                        last_revision=revision_id,
1192
                        pb=pb)
1193
        return f.count_copied, f.failed_revisions
1194
1534.1.34 by Robert Collins
Move missing_revision_ids from Repository to InterRepository, and eliminate the now unused Repository._compatible_formats method.
1195
    @needs_read_lock
1196
    def missing_revision_ids(self, revision_id=None):
1197
        """See InterRepository.missing_revision_ids()."""
1198
        # we want all revisions to satisfy revision_id in source.
1199
        # but we dont want to stat every file here and there.
1200
        # we want then, all revisions other needs to satisfy revision_id 
1201
        # checked, but not those that we have locally.
1202
        # so the first thing is to get a subset of the revisions to 
1203
        # satisfy revision_id in source, and then eliminate those that
1204
        # we do already have. 
1205
        # this is slow on high latency connection to self, but as as this
1206
        # disk format scales terribly for push anyway due to rewriting 
1207
        # inventory.weave, this is considered acceptable.
1208
        # - RBC 20060209
1209
        if revision_id is not None:
1210
            source_ids = self.source.get_ancestry(revision_id)
1211
            assert source_ids.pop(0) == None
1212
        else:
1213
            source_ids = self.source._all_possible_ids()
1214
        source_ids_set = set(source_ids)
1215
        # source_ids is the worst possible case we may need to pull.
1216
        # now we want to filter source_ids against what we actually
1217
        # have in target, but dont try to check for existence where we know
1218
        # we do not have a revision as that would be pointless.
1219
        target_ids = set(self.target._all_possible_ids())
1220
        possibly_present_revisions = target_ids.intersection(source_ids_set)
1221
        actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
1222
        required_revisions = source_ids_set.difference(actually_present_revisions)
1223
        required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
1224
        if revision_id is not None:
1225
            # we used get_ancestry to determine source_ids then we are assured all
1226
            # revisions referenced are present as they are installed in topological order.
1227
            # and the tip revision was validated by get_ancestry.
1228
            return required_topo_revisions
1229
        else:
1230
            # if we just grabbed the possibly available ids, then 
1231
            # we only have an estimate of whats available and need to validate
1232
            # that against the revision records.
1233
            return self.source._eliminate_revisions_not_present(required_topo_revisions)
1234
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.
1235
1236
InterRepository.register_optimiser(InterWeaveRepo)
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.
1237
1238
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1239
class RepositoryTestProviderAdapter(object):
1240
    """A tool to generate a suite testing multiple repository formats at once.
1241
1242
    This is done by copying the test once for each transport and injecting
1243
    the transport_server, transport_readonly_server, and bzrdir_format and
1244
    repository_format classes into each copy. Each copy is also given a new id()
1245
    to make it easy to identify.
1246
    """
1247
1248
    def __init__(self, transport_server, transport_readonly_server, formats):
1249
        self._transport_server = transport_server
1250
        self._transport_readonly_server = transport_readonly_server
1251
        self._formats = formats
1252
    
1253
    def adapt(self, test):
1254
        result = TestSuite()
1255
        for repository_format, bzrdir_format in self._formats:
1256
            new_test = deepcopy(test)
1257
            new_test.transport_server = self._transport_server
1258
            new_test.transport_readonly_server = self._transport_readonly_server
1259
            new_test.bzrdir_format = bzrdir_format
1260
            new_test.repository_format = repository_format
1261
            def make_new_test_id():
1262
                new_id = "%s(%s)" % (new_test.id(), repository_format.__class__.__name__)
1263
                return lambda: new_id
1264
            new_test.id = make_new_test_id()
1265
            result.addTest(new_test)
1266
        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.
1267
1268
1269
class InterRepositoryTestProviderAdapter(object):
1270
    """A tool to generate a suite testing multiple inter repository formats.
1271
1272
    This is done by copying the test once for each interrepo provider and injecting
1273
    the transport_server, transport_readonly_server, repository_format and 
1274
    repository_to_format classes into each copy.
1275
    Each copy is also given a new id() to make it easy to identify.
1276
    """
1277
1278
    def __init__(self, transport_server, transport_readonly_server, formats):
1279
        self._transport_server = transport_server
1280
        self._transport_readonly_server = transport_readonly_server
1281
        self._formats = formats
1282
    
1283
    def adapt(self, test):
1284
        result = TestSuite()
1285
        for interrepo_class, repository_format, repository_format_to in self._formats:
1286
            new_test = deepcopy(test)
1287
            new_test.transport_server = self._transport_server
1288
            new_test.transport_readonly_server = self._transport_readonly_server
1289
            new_test.interrepo_class = interrepo_class
1290
            new_test.repository_format = repository_format
1291
            new_test.repository_format_to = repository_format_to
1292
            def make_new_test_id():
1293
                new_id = "%s(%s)" % (new_test.id(), interrepo_class.__name__)
1294
                return lambda: new_id
1295
            new_test.id = make_new_test_id()
1296
            result.addTest(new_test)
1297
        return result
1298
1299
    @staticmethod
1300
    def default_test_list():
1301
        """Generate the default list of interrepo permutations to test."""
1302
        result = []
1303
        # test the default InterRepository between format 6 and the current 
1304
        # 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.
1305
        # XXX: robertc 20060220 reinstate this when there are two supported
1306
        # formats which do not have an optimal code path between them.
1556.1.3 by Robert Collins
Rearrangment of Repository logic to be less type code driven, and bugfix InterRepository.missing_revision_ids
1307
        result.append((InterRepository,
1308
                       RepositoryFormat6(),
1309
                       RepositoryFormatKnit1()))
1534.1.29 by Robert Collins
Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.
1310
        for optimiser in InterRepository._optimisers:
1311
            result.append((optimiser,
1312
                           optimiser._matching_repo_format,
1313
                           optimiser._matching_repo_format
1314
                           ))
1315
        # if there are specific combinations we want to use, we can add them 
1316
        # here.
1317
        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.
1318
1319
1320
class CopyConverter(object):
1321
    """A repository conversion tool which just performs a copy of the content.
1322
    
1323
    This is slow but quite reliable.
1324
    """
1325
1326
    def __init__(self, target_format):
1327
        """Create a CopyConverter.
1328
1329
        :param target_format: The format the resulting repository should be.
1330
        """
1331
        self.target_format = target_format
1332
        
1333
    def convert(self, repo, pb):
1334
        """Perform the conversion of to_convert, giving feedback via pb.
1335
1336
        :param to_convert: The disk object to convert.
1337
        :param pb: a progress bar to use for progress information.
1338
        """
1339
        self.pb = pb
1340
        self.count = 0
1341
        self.total = 3
1342
        # this is only useful with metadir layouts - separated repo content.
1343
        # trigger an assertion if not such
1344
        repo._format.get_format_string()
1345
        self.repo_dir = repo.bzrdir
1346
        self.step('Moving repository to repository.backup')
1347
        self.repo_dir.transport.move('repository', 'repository.backup')
1348
        backup_transport =  self.repo_dir.transport.clone('repository.backup')
1349
        self.source_repo = repo._format.open(self.repo_dir,
1350
            _found=True,
1351
            _override_transport=backup_transport)
1352
        self.step('Creating new repository')
1353
        converted = self.target_format.initialize(self.repo_dir,
1354
                                                  self.source_repo.is_shared())
1355
        converted.lock_write()
1356
        try:
1357
            self.step('Copying content into repository.')
1358
            self.source_repo.copy_content_into(converted)
1359
        finally:
1360
            converted.unlock()
1361
        self.step('Deleting old repository content.')
1362
        self.repo_dir.transport.delete_tree('repository.backup')
1363
        self.pb.note('repository converted')
1364
1365
    def step(self, message):
1366
        """Update the pb by a step."""
1367
        self.count +=1
1368
        self.pb.update(message, self.count, self.total)