/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/repository.py

  • Committer: Martin Pool
  • Date: 2006-03-08 03:53:30 UTC
  • mto: This revision was merged to the branch mainline in revision 1602.
  • Revision ID: mbp@sourcefrog.net-20060308035330-40065f85adc9652e
Add new TestCaseWithTransport.assertIsDirectory() and tests

Show diffs side-by-side

added added

removed removed

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