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