/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

Fixup pb usage to use nested_progress_bar.

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_graph(self, revision_id=None):
 
433
        """Return a dictionary containing the revision graph.
 
434
        
 
435
        :return: a dictionary of revision_id->revision_parents_list.
 
436
        """
 
437
        weave = self.get_inventory_weave()
 
438
        all_revisions = self._eliminate_revisions_not_present(weave.names())
 
439
        entire_graph = dict([(node, weave.parent_names(node)) for 
 
440
                             node in all_revisions])
 
441
        if revision_id is None:
 
442
            return entire_graph
 
443
        elif revision_id not in entire_graph:
 
444
            raise errors.NoSuchRevision(self, revision_id)
 
445
        else:
 
446
            # add what can be reached from revision_id
 
447
            result = {}
 
448
            pending = set([revision_id])
 
449
            while len(pending) > 0:
 
450
                node = pending.pop()
 
451
                result[node] = entire_graph[node]
 
452
                for revision_id in result[node]:
 
453
                    if revision_id not in result:
 
454
                        pending.add(revision_id)
 
455
            return result
 
456
 
 
457
    @needs_read_lock
 
458
    def get_revision_inventory(self, revision_id):
 
459
        """Return inventory of a past revision."""
 
460
        # TODO: Unify this with get_inventory()
 
461
        # bzr 0.0.6 and later imposes the constraint that the inventory_id
 
462
        # must be the same as its revision, so this is trivial.
 
463
        if revision_id is None:
 
464
            # This does not make sense: if there is no revision,
 
465
            # then it is the current tree inventory surely ?!
 
466
            # and thus get_root_id() is something that looks at the last
 
467
            # commit on the branch, and the get_root_id is an inventory check.
 
468
            raise NotImplementedError
 
469
            # return Inventory(self.get_root_id())
 
470
        else:
 
471
            return self.get_inventory(revision_id)
 
472
 
 
473
    @needs_read_lock
 
474
    def is_shared(self):
 
475
        """Return True if this repository is flagged as a shared repository."""
 
476
        # FIXME format 4-6 cannot be shared, this is technically faulty.
 
477
        return self.control_files._transport.has('shared-storage')
 
478
 
 
479
    @needs_read_lock
 
480
    def revision_tree(self, revision_id):
 
481
        """Return Tree for a revision on this branch.
 
482
 
 
483
        `revision_id` may be None for the null revision, in which case
 
484
        an `EmptyTree` is returned."""
 
485
        # TODO: refactor this to use an existing revision object
 
486
        # so we don't need to read it in twice.
 
487
        if revision_id is None or revision_id == NULL_REVISION:
 
488
            return EmptyTree()
 
489
        else:
 
490
            inv = self.get_revision_inventory(revision_id)
 
491
            return RevisionTree(self, inv, revision_id)
 
492
 
 
493
    @needs_read_lock
 
494
    def get_ancestry(self, revision_id):
 
495
        """Return a list of revision-ids integrated by a revision.
 
496
        
 
497
        This is topologically sorted.
 
498
        """
 
499
        if revision_id is None:
 
500
            return [None]
 
501
        if not self.has_revision(revision_id):
 
502
            raise errors.NoSuchRevision(self, revision_id)
 
503
        w = self.get_inventory_weave()
 
504
        return [None] + map(w.idx_to_name,
 
505
                            w.inclusions([w.lookup(revision_id)]))
 
506
 
 
507
    @needs_read_lock
 
508
    def print_file(self, file, revision_id):
 
509
        """Print `file` to stdout.
 
510
        
 
511
        FIXME RBC 20060125 as John Meinel points out this is a bad api
 
512
        - it writes to stdout, it assumes that that is valid etc. Fix
 
513
        by creating a new more flexible convenience function.
 
514
        """
 
515
        tree = self.revision_tree(revision_id)
 
516
        # use inventory as it was in that revision
 
517
        file_id = tree.inventory.path2id(file)
 
518
        if not file_id:
 
519
            raise BzrError("%r is not present in revision %s" % (file, revno))
 
520
            try:
 
521
                revno = self.revision_id_to_revno(revision_id)
 
522
            except errors.NoSuchRevision:
 
523
                # TODO: This should not be BzrError,
 
524
                # but NoSuchFile doesn't fit either
 
525
                raise BzrError('%r is not present in revision %s' 
 
526
                                % (file, revision_id))
 
527
            else:
 
528
                raise BzrError('%r is not present in revision %s'
 
529
                                % (file, revno))
 
530
        tree.print_file(file_id)
 
531
 
 
532
    def get_transaction(self):
 
533
        return self.control_files.get_transaction()
 
534
 
 
535
    def revision_parents(self, revid):
 
536
        return self.get_inventory_weave().parent_names(revid)
 
537
 
 
538
    @needs_write_lock
 
539
    def set_make_working_trees(self, new_value):
 
540
        """Set the policy flag for making working trees when creating branches.
 
541
 
 
542
        This only applies to branches that use this repository.
 
543
 
 
544
        The default is 'True'.
 
545
        :param new_value: True to restore the default, False to disable making
 
546
                          working trees.
 
547
        """
 
548
        # FIXME: split out into a new class/strategy ?
 
549
        if isinstance(self._format, (RepositoryFormat4,
 
550
                                     RepositoryFormat5,
 
551
                                     RepositoryFormat6)):
 
552
            raise NotImplementedError(self.set_make_working_trees)
 
553
        if new_value:
 
554
            try:
 
555
                self.control_files._transport.delete('no-working-trees')
 
556
            except errors.NoSuchFile:
 
557
                pass
 
558
        else:
 
559
            self.control_files.put_utf8('no-working-trees', '')
 
560
    
 
561
    def make_working_trees(self):
 
562
        """Returns the policy for making working trees on new branches."""
 
563
        # FIXME: split out into a new class/strategy ?
 
564
        if isinstance(self._format, (RepositoryFormat4,
 
565
                                     RepositoryFormat5,
 
566
                                     RepositoryFormat6)):
 
567
            return True
 
568
        return not self.control_files._transport.has('no-working-trees')
 
569
 
 
570
    @needs_write_lock
 
571
    def sign_revision(self, revision_id, gpg_strategy):
 
572
        plaintext = Testament.from_revision(self, revision_id).as_short_text()
 
573
        self.store_revision_signature(gpg_strategy, plaintext, revision_id)
 
574
 
 
575
 
 
576
class AllInOneRepository(Repository):
 
577
    """Legacy support - the repository behaviour for all-in-one branches."""
 
578
 
 
579
    def __init__(self, _format, a_bzrdir, revision_store):
 
580
        # we reuse one control files instance.
 
581
        dir_mode = a_bzrdir._control_files._dir_mode
 
582
        file_mode = a_bzrdir._control_files._file_mode
 
583
 
 
584
        def get_weave(name, prefixed=False):
 
585
            if name:
 
586
                name = safe_unicode(name)
 
587
            else:
 
588
                name = ''
 
589
            relpath = a_bzrdir._control_files._escape(name)
 
590
            weave_transport = a_bzrdir._control_files._transport.clone(relpath)
 
591
            ws = WeaveStore(weave_transport, prefixed=prefixed,
 
592
                            dir_mode=dir_mode,
 
593
                            file_mode=file_mode)
 
594
            if a_bzrdir._control_files._transport.should_cache():
 
595
                ws.enable_cache = True
 
596
            return ws
 
597
 
 
598
        def get_store(name, compressed=True, prefixed=False):
 
599
            # FIXME: This approach of assuming stores are all entirely compressed
 
600
            # or entirely uncompressed is tidy, but breaks upgrade from 
 
601
            # some existing branches where there's a mixture; we probably 
 
602
            # still want the option to look for both.
 
603
            relpath = a_bzrdir._control_files._escape(name)
 
604
            store = TextStore(a_bzrdir._control_files._transport.clone(relpath),
 
605
                              prefixed=prefixed, compressed=compressed,
 
606
                              dir_mode=dir_mode,
 
607
                              file_mode=file_mode)
 
608
            #if self._transport.should_cache():
 
609
            #    cache_path = os.path.join(self.cache_root, name)
 
610
            #    os.mkdir(cache_path)
 
611
            #    store = bzrlib.store.CachedStore(store, cache_path)
 
612
            return store
 
613
 
 
614
        # not broken out yet because the controlweaves|inventory_store
 
615
        # and text_store | weave_store bits are still different.
 
616
        if isinstance(_format, RepositoryFormat4):
 
617
            self.inventory_store = get_store('inventory-store')
 
618
            self.text_store = get_store('text-store')
 
619
        elif isinstance(_format, RepositoryFormat5):
 
620
            self.control_weaves = get_weave('')
 
621
            self.weave_store = get_weave('weaves')
 
622
        elif isinstance(_format, RepositoryFormat6):
 
623
            self.control_weaves = get_weave('')
 
624
            self.weave_store = get_weave('weaves', prefixed=True)
 
625
        else:
 
626
            raise errors.BzrError('unreachable code: unexpected repository'
 
627
                                  ' format.')
 
628
        revision_store.register_suffix('sig')
 
629
        super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, revision_store)
 
630
 
 
631
 
 
632
class MetaDirRepository(Repository):
 
633
    """Repositories in the new meta-dir layout."""
 
634
 
 
635
    def __init__(self, _format, a_bzrdir, control_files, revision_store):
 
636
        super(MetaDirRepository, self).__init__(_format,
 
637
                                                a_bzrdir,
 
638
                                                control_files,
 
639
                                                revision_store)
 
640
 
 
641
        dir_mode = self.control_files._dir_mode
 
642
        file_mode = self.control_files._file_mode
 
643
 
 
644
        def get_weave(name, prefixed=False):
 
645
            if name:
 
646
                name = safe_unicode(name)
 
647
            else:
 
648
                name = ''
 
649
            relpath = self.control_files._escape(name)
 
650
            weave_transport = self.control_files._transport.clone(relpath)
 
651
            ws = WeaveStore(weave_transport, prefixed=prefixed,
 
652
                            dir_mode=dir_mode,
 
653
                            file_mode=file_mode)
 
654
            if self.control_files._transport.should_cache():
 
655
                ws.enable_cache = True
 
656
            return ws
 
657
 
 
658
        if isinstance(self._format, RepositoryFormat7):
 
659
            self.control_weaves = get_weave('')
 
660
            self.weave_store = get_weave('weaves', prefixed=True)
 
661
        elif isinstance(self._format, RepositoryFormatKnit1):
 
662
            self.control_weaves = get_weave('')
 
663
            self.weave_store = get_weave('knits', prefixed=True)
 
664
        else:
 
665
            raise errors.BzrError('unreachable code: unexpected repository'
 
666
                                  ' format.')
 
667
 
 
668
 
 
669
class RepositoryFormat(object):
 
670
    """A repository format.
 
671
 
 
672
    Formats provide three things:
 
673
     * An initialization routine to construct repository data on disk.
 
674
     * a format string which is used when the BzrDir supports versioned
 
675
       children.
 
676
     * an open routine which returns a Repository instance.
 
677
 
 
678
    Formats are placed in an dict by their format string for reference 
 
679
    during opening. These should be subclasses of RepositoryFormat
 
680
    for consistency.
 
681
 
 
682
    Once a format is deprecated, just deprecate the initialize and open
 
683
    methods on the format class. Do not deprecate the object, as the 
 
684
    object will be created every system load.
 
685
 
 
686
    Common instance attributes:
 
687
    _matchingbzrdir - the bzrdir format that the repository format was
 
688
    originally written to work with. This can be used if manually
 
689
    constructing a bzrdir and repository, or more commonly for test suite
 
690
    parameterisation.
 
691
    """
 
692
 
 
693
    _default_format = None
 
694
    """The default format used for new repositories."""
 
695
 
 
696
    _formats = {}
 
697
    """The known formats."""
 
698
 
 
699
    @classmethod
 
700
    def find_format(klass, a_bzrdir):
 
701
        """Return the format for the repository object in a_bzrdir."""
 
702
        try:
 
703
            transport = a_bzrdir.get_repository_transport(None)
 
704
            format_string = transport.get("format").read()
 
705
            return klass._formats[format_string]
 
706
        except errors.NoSuchFile:
 
707
            raise errors.NoRepositoryPresent(a_bzrdir)
 
708
        except KeyError:
 
709
            raise errors.UnknownFormatError(format_string)
 
710
 
 
711
    @classmethod
 
712
    def get_default_format(klass):
 
713
        """Return the current default format."""
 
714
        return klass._default_format
 
715
 
 
716
    def get_format_string(self):
 
717
        """Return the ASCII format string that identifies this format.
 
718
        
 
719
        Note that in pre format ?? repositories the format string is 
 
720
        not permitted nor written to disk.
 
721
        """
 
722
        raise NotImplementedError(self.get_format_string)
 
723
 
 
724
    def _get_revision_store(self, repo_transport, control_files):
 
725
        """Return the revision store object for this a_bzrdir."""
 
726
        raise NotImplementedError(self._get_revision_store)
 
727
 
 
728
    def _get_rev_store(self,
 
729
                   transport,
 
730
                   control_files,
 
731
                   name,
 
732
                   compressed=True,
 
733
                   prefixed=False):
 
734
        """Common logic for getting a revision store for a repository.
 
735
        
 
736
        see self._get_revision_store for the method to 
 
737
        get the store for a repository.
 
738
        """
 
739
        if name:
 
740
            name = safe_unicode(name)
 
741
        else:
 
742
            name = ''
 
743
        dir_mode = control_files._dir_mode
 
744
        file_mode = control_files._file_mode
 
745
        revision_store =TextStore(transport.clone(name),
 
746
                                  prefixed=prefixed,
 
747
                                  compressed=compressed,
 
748
                                  dir_mode=dir_mode,
 
749
                                  file_mode=file_mode)
 
750
        revision_store.register_suffix('sig')
 
751
        return revision_store
 
752
 
 
753
    def initialize(self, a_bzrdir, shared=False):
 
754
        """Initialize a repository of this format in a_bzrdir.
 
755
 
 
756
        :param a_bzrdir: The bzrdir to put the new repository in it.
 
757
        :param shared: The repository should be initialized as a sharable one.
 
758
 
 
759
        This may raise UninitializableFormat if shared repository are not
 
760
        compatible the a_bzrdir.
 
761
        """
 
762
 
 
763
    def is_supported(self):
 
764
        """Is this format supported?
 
765
 
 
766
        Supported formats must be initializable and openable.
 
767
        Unsupported formats may not support initialization or committing or 
 
768
        some other features depending on the reason for not being supported.
 
769
        """
 
770
        return True
 
771
 
 
772
    def open(self, a_bzrdir, _found=False):
 
773
        """Return an instance of this format for the bzrdir a_bzrdir.
 
774
        
 
775
        _found is a private parameter, do not use it.
 
776
        """
 
777
        raise NotImplementedError(self.open)
 
778
 
 
779
    @classmethod
 
780
    def register_format(klass, format):
 
781
        klass._formats[format.get_format_string()] = format
 
782
 
 
783
    @classmethod
 
784
    def set_default_format(klass, format):
 
785
        klass._default_format = format
 
786
 
 
787
    @classmethod
 
788
    def unregister_format(klass, format):
 
789
        assert klass._formats[format.get_format_string()] is format
 
790
        del klass._formats[format.get_format_string()]
 
791
 
 
792
 
 
793
class PreSplitOutRepositoryFormat(RepositoryFormat):
 
794
    """Base class for the pre split out repository formats."""
 
795
 
 
796
    def initialize(self, a_bzrdir, shared=False, _internal=False):
 
797
        """Create a weave repository.
 
798
        
 
799
        TODO: when creating split out bzr branch formats, move this to a common
 
800
        base for Format5, Format6. or something like that.
 
801
        """
 
802
        from bzrlib.weavefile import write_weave_v5
 
803
        from bzrlib.weave import Weave
 
804
 
 
805
        if shared:
 
806
            raise errors.IncompatibleFormat(self, a_bzrdir._format)
 
807
 
 
808
        if not _internal:
 
809
            # always initialized when the bzrdir is.
 
810
            return self.open(a_bzrdir, _found=True)
 
811
        
 
812
        # Create an empty weave
 
813
        sio = StringIO()
 
814
        bzrlib.weavefile.write_weave_v5(Weave(), sio)
 
815
        empty_weave = sio.getvalue()
 
816
 
 
817
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
818
        dirs = ['revision-store', 'weaves']
 
819
        files = [('inventory.weave', StringIO(empty_weave)),
 
820
                 ]
 
821
        
 
822
        # FIXME: RBC 20060125 dont peek under the covers
 
823
        # NB: no need to escape relative paths that are url safe.
 
824
        control_files = LockableFiles(a_bzrdir.transport, 'branch-lock',
 
825
                                      TransportLock)
 
826
        control_files.create_lock()
 
827
        control_files.lock_write()
 
828
        control_files._transport.mkdir_multi(dirs,
 
829
                mode=control_files._dir_mode)
 
830
        try:
 
831
            for file, content in files:
 
832
                control_files.put(file, content)
 
833
        finally:
 
834
            control_files.unlock()
 
835
        return self.open(a_bzrdir, _found=True)
 
836
 
 
837
    def open(self, a_bzrdir, _found=False):
 
838
        """See RepositoryFormat.open()."""
 
839
        if not _found:
 
840
            # we are being called directly and must probe.
 
841
            raise NotImplementedError
 
842
 
 
843
        repo_transport = a_bzrdir.get_repository_transport(None)
 
844
        control_files = a_bzrdir._control_files
 
845
        revision_store = self._get_revision_store(repo_transport, control_files)
 
846
        return AllInOneRepository(_format=self,
 
847
                                  a_bzrdir=a_bzrdir,
 
848
                                  revision_store=revision_store)
 
849
 
 
850
 
 
851
class RepositoryFormat4(PreSplitOutRepositoryFormat):
 
852
    """Bzr repository format 4.
 
853
 
 
854
    This repository format has:
 
855
     - flat stores
 
856
     - TextStores for texts, inventories,revisions.
 
857
 
 
858
    This format is deprecated: it indexes texts using a text id which is
 
859
    removed in format 5; initializationa and write support for this format
 
860
    has been removed.
 
861
    """
 
862
 
 
863
    def __init__(self):
 
864
        super(RepositoryFormat4, self).__init__()
 
865
        self._matchingbzrdir = bzrlib.bzrdir.BzrDirFormat4()
 
866
 
 
867
    def initialize(self, url, shared=False, _internal=False):
 
868
        """Format 4 branches cannot be created."""
 
869
        raise errors.UninitializableFormat(self)
 
870
 
 
871
    def is_supported(self):
 
872
        """Format 4 is not supported.
 
873
 
 
874
        It is not supported because the model changed from 4 to 5 and the
 
875
        conversion logic is expensive - so doing it on the fly was not 
 
876
        feasible.
 
877
        """
 
878
        return False
 
879
 
 
880
    def _get_revision_store(self, repo_transport, control_files):
 
881
        """See RepositoryFormat._get_revision_store()."""
 
882
        return self._get_rev_store(repo_transport,
 
883
                                   control_files,
 
884
                                   'revision-store')
 
885
 
 
886
 
 
887
class RepositoryFormat5(PreSplitOutRepositoryFormat):
 
888
    """Bzr control format 5.
 
889
 
 
890
    This repository format has:
 
891
     - weaves for file texts and inventory
 
892
     - flat stores
 
893
     - TextStores for revisions and signatures.
 
894
    """
 
895
 
 
896
    def __init__(self):
 
897
        super(RepositoryFormat5, self).__init__()
 
898
        self._matchingbzrdir = bzrlib.bzrdir.BzrDirFormat5()
 
899
 
 
900
    def _get_revision_store(self, repo_transport, control_files):
 
901
        """See RepositoryFormat._get_revision_store()."""
 
902
        """Return the revision store object for this a_bzrdir."""
 
903
        return self._get_rev_store(repo_transport,
 
904
                                   control_files,
 
905
                                   'revision-store',
 
906
                                   compressed=False)
 
907
 
 
908
 
 
909
class RepositoryFormat6(PreSplitOutRepositoryFormat):
 
910
    """Bzr control format 6.
 
911
 
 
912
    This repository format has:
 
913
     - weaves for file texts and inventory
 
914
     - hash subdirectory based stores.
 
915
     - TextStores for revisions and signatures.
 
916
    """
 
917
 
 
918
    def __init__(self):
 
919
        super(RepositoryFormat6, self).__init__()
 
920
        self._matchingbzrdir = bzrlib.bzrdir.BzrDirFormat6()
 
921
 
 
922
    def _get_revision_store(self, repo_transport, control_files):
 
923
        """See RepositoryFormat._get_revision_store()."""
 
924
        return self._get_rev_store(repo_transport,
 
925
                                   control_files,
 
926
                                   'revision-store',
 
927
                                   compressed=False,
 
928
                                   prefixed=True)
 
929
 
 
930
 
 
931
class MetaDirRepositoryFormat(RepositoryFormat):
 
932
    """Common base class for the new repositories using the metadir layour."""
 
933
 
 
934
    def __init__(self):
 
935
        super(MetaDirRepositoryFormat, self).__init__()
 
936
        self._matchingbzrdir = bzrlib.bzrdir.BzrDirMetaFormat1()
 
937
 
 
938
    def _create_control_files(self, a_bzrdir):
 
939
        """Create the required files and the initial control_files object."""
 
940
        # FIXME: RBC 20060125 dont peek under the covers
 
941
        # NB: no need to escape relative paths that are url safe.
 
942
        repository_transport = a_bzrdir.get_repository_transport(self)
 
943
        control_files = LockableFiles(repository_transport, 'lock', LockDir)
 
944
        control_files.create_lock()
 
945
        return control_files
 
946
 
 
947
    def _get_revision_store(self, repo_transport, control_files):
 
948
        """See RepositoryFormat._get_revision_store()."""
 
949
        return self._get_rev_store(repo_transport,
 
950
                                   control_files,
 
951
                                   'revision-store',
 
952
                                   compressed=False,
 
953
                                   prefixed=True,
 
954
                                   )
 
955
 
 
956
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
957
        """See RepositoryFormat.open().
 
958
        
 
959
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
960
                                    repository at a slightly different url
 
961
                                    than normal. I.e. during 'upgrade'.
 
962
        """
 
963
        if not _found:
 
964
            format = RepositoryFormat.find_format(a_bzrdir)
 
965
            assert format.__class__ ==  self.__class__
 
966
        if _override_transport is not None:
 
967
            repo_transport = _override_transport
 
968
        else:
 
969
            repo_transport = a_bzrdir.get_repository_transport(None)
 
970
        control_files = LockableFiles(repo_transport, 'lock', LockDir)
 
971
        revision_store = self._get_revision_store(repo_transport, control_files)
 
972
        return MetaDirRepository(_format=self,
 
973
                                 a_bzrdir=a_bzrdir,
 
974
                                 control_files=control_files,
 
975
                                 revision_store=revision_store)
 
976
 
 
977
    def _upload_blank_content(self, a_bzrdir, dirs, files, utf8_files, shared):
 
978
        """Upload the initial blank content."""
 
979
        control_files = self._create_control_files(a_bzrdir)
 
980
        control_files.lock_write()
 
981
        try:
 
982
            control_files._transport.mkdir_multi(dirs,
 
983
                    mode=control_files._dir_mode)
 
984
            for file, content in files:
 
985
                control_files.put(file, content)
 
986
            for file, content in utf8_files:
 
987
                control_files.put_utf8(file, content)
 
988
            if shared == True:
 
989
                control_files.put_utf8('shared-storage', '')
 
990
        finally:
 
991
            control_files.unlock()
 
992
 
 
993
 
 
994
class RepositoryFormat7(MetaDirRepositoryFormat):
 
995
    """Bzr repository 7.
 
996
 
 
997
    This repository format has:
 
998
     - weaves for file texts and inventory
 
999
     - hash subdirectory based stores.
 
1000
     - TextStores for revisions and signatures.
 
1001
     - a format marker of its own
 
1002
     - an optional 'shared-storage' flag
 
1003
     - an optional 'no-working-trees' flag
 
1004
    """
 
1005
 
 
1006
    def get_format_string(self):
 
1007
        """See RepositoryFormat.get_format_string()."""
 
1008
        return "Bazaar-NG Repository format 7"
 
1009
 
 
1010
    def initialize(self, a_bzrdir, shared=False):
 
1011
        """Create a weave repository.
 
1012
 
 
1013
        :param shared: If true the repository will be initialized as a shared
 
1014
                       repository.
 
1015
        """
 
1016
        from bzrlib.weavefile import write_weave_v5
 
1017
        from bzrlib.weave import Weave
 
1018
 
 
1019
        # Create an empty weave
 
1020
        sio = StringIO()
 
1021
        bzrlib.weavefile.write_weave_v5(Weave(), sio)
 
1022
        empty_weave = sio.getvalue()
 
1023
 
 
1024
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
1025
        dirs = ['revision-store', 'weaves']
 
1026
        files = [('inventory.weave', StringIO(empty_weave)), 
 
1027
                 ]
 
1028
        utf8_files = [('format', self.get_format_string())]
 
1029
 
 
1030
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
 
1031
        return self.open(a_bzrdir=a_bzrdir, _found=True)
 
1032
 
 
1033
 
 
1034
class RepositoryFormatKnit1(MetaDirRepositoryFormat):
 
1035
    """Bzr repository knit format 1.
 
1036
 
 
1037
    This repository format has:
 
1038
     - knits for file texts and inventory
 
1039
     - hash subdirectory based stores.
 
1040
     - knits for revisions and signatures
 
1041
     - TextStores for revisions and signatures.
 
1042
     - a format marker of its own
 
1043
     - an optional 'shared-storage' flag
 
1044
     - an optional 'no-working-trees' flag
 
1045
     - a LockDir lock
 
1046
    """
 
1047
 
 
1048
    def get_format_string(self):
 
1049
        """See RepositoryFormat.get_format_string()."""
 
1050
        return "Bazaar-NG Knit Repository Format 1"
 
1051
 
 
1052
    def initialize(self, a_bzrdir, shared=False):
 
1053
        """Create a knit format 1 repository.
 
1054
 
 
1055
        :param shared: If true the repository will be initialized as a shared
 
1056
                       repository.
 
1057
        XXX NOTE that this current uses a Weave for testing and will become 
 
1058
            A Knit in due course.
 
1059
        """
 
1060
        from bzrlib.weavefile import write_weave_v5
 
1061
        from bzrlib.weave import Weave
 
1062
 
 
1063
        # Create an empty weave
 
1064
        sio = StringIO()
 
1065
        bzrlib.weavefile.write_weave_v5(Weave(), sio)
 
1066
        empty_weave = sio.getvalue()
 
1067
 
 
1068
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
1069
        dirs = ['revision-store', 'knits']
 
1070
        files = [('inventory.weave', StringIO(empty_weave)), 
 
1071
                 ]
 
1072
        utf8_files = [('format', self.get_format_string())]
 
1073
        
 
1074
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
 
1075
        return self.open(a_bzrdir=a_bzrdir, _found=True)
 
1076
 
 
1077
 
 
1078
# formats which have no format string are not discoverable
 
1079
# and not independently creatable, so are not registered.
 
1080
_default_format = RepositoryFormat7()
 
1081
RepositoryFormat.register_format(_default_format)
 
1082
RepositoryFormat.register_format(RepositoryFormatKnit1())
 
1083
RepositoryFormat.set_default_format(_default_format)
 
1084
_legacy_formats = [RepositoryFormat4(),
 
1085
                   RepositoryFormat5(),
 
1086
                   RepositoryFormat6()]
 
1087
 
 
1088
 
 
1089
class InterRepository(object):
 
1090
    """This class represents operations taking place between two repositories.
 
1091
 
 
1092
    Its instances have methods like copy_content and fetch, and contain
 
1093
    references to the source and target repositories these operations can be 
 
1094
    carried out on.
 
1095
 
 
1096
    Often we will provide convenience methods on 'repository' which carry out
 
1097
    operations with another repository - they will always forward to
 
1098
    InterRepository.get(other).method_name(parameters).
 
1099
    """
 
1100
    # XXX: FIXME: FUTURE: robertc
 
1101
    # testing of these probably requires a factory in optimiser type, and 
 
1102
    # then a test adapter to test each type thoroughly.
 
1103
    #
 
1104
 
 
1105
    _optimisers = set()
 
1106
    """The available optimised InterRepository types."""
 
1107
 
 
1108
    def __init__(self, source, target):
 
1109
        """Construct a default InterRepository instance. Please use 'get'.
 
1110
        
 
1111
        Only subclasses of InterRepository should call 
 
1112
        InterRepository.__init__ - clients should call InterRepository.get
 
1113
        instead which will create an optimised InterRepository if possible.
 
1114
        """
 
1115
        self.source = source
 
1116
        self.target = target
 
1117
 
 
1118
    @needs_write_lock
 
1119
    def copy_content(self, revision_id=None, basis=None):
 
1120
        """Make a complete copy of the content in self into destination.
 
1121
        
 
1122
        This is a destructive operation! Do not use it on existing 
 
1123
        repositories.
 
1124
 
 
1125
        :param revision_id: Only copy the content needed to construct
 
1126
                            revision_id and its parents.
 
1127
        :param basis: Copy the needed data preferentially from basis.
 
1128
        """
 
1129
        try:
 
1130
            self.target.set_make_working_trees(self.source.make_working_trees())
 
1131
        except NotImplementedError:
 
1132
            pass
 
1133
        # grab the basis available data
 
1134
        if basis is not None:
 
1135
            self.target.fetch(basis, revision_id=revision_id)
 
1136
        # but dont both fetching if we have the needed data now.
 
1137
        if (revision_id not in (None, NULL_REVISION) and 
 
1138
            self.target.has_revision(revision_id)):
 
1139
            return
 
1140
        self.target.fetch(self.source, revision_id=revision_id)
 
1141
 
 
1142
    def _double_lock(self, lock_source, lock_target):
 
1143
        """Take out too locks, rolling back the first if the second throws."""
 
1144
        lock_source()
 
1145
        try:
 
1146
            lock_target()
 
1147
        except Exception:
 
1148
            # we want to ensure that we don't leave source locked by mistake.
 
1149
            # and any error on target should not confuse source.
 
1150
            self.source.unlock()
 
1151
            raise
 
1152
 
 
1153
    @needs_write_lock
 
1154
    def fetch(self, revision_id=None, pb=None):
 
1155
        """Fetch the content required to construct revision_id.
 
1156
 
 
1157
        The content is copied from source to target.
 
1158
 
 
1159
        :param revision_id: if None all content is copied, if NULL_REVISION no
 
1160
                            content is copied.
 
1161
        :param pb: optional progress bar to use for progress reports. If not
 
1162
                   provided a default one will be created.
 
1163
 
 
1164
        Returns the copied revision count and the failed revisions in a tuple:
 
1165
        (copied, failures).
 
1166
        """
 
1167
        from bzrlib.fetch import RepoFetcher
 
1168
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
 
1169
               self.source, self.source._format, self.target, self.target._format)
 
1170
        f = RepoFetcher(to_repository=self.target,
 
1171
                        from_repository=self.source,
 
1172
                        last_revision=revision_id,
 
1173
                        pb=pb)
 
1174
        return f.count_copied, f.failed_revisions
 
1175
 
 
1176
    @classmethod
 
1177
    def get(klass, repository_source, repository_target):
 
1178
        """Retrieve a InterRepository worker object for these repositories.
 
1179
 
 
1180
        :param repository_source: the repository to be the 'source' member of
 
1181
                                  the InterRepository instance.
 
1182
        :param repository_target: the repository to be the 'target' member of
 
1183
                                the InterRepository instance.
 
1184
        If an optimised InterRepository worker exists it will be used otherwise
 
1185
        a default InterRepository instance will be created.
 
1186
        """
 
1187
        for provider in klass._optimisers:
 
1188
            if provider.is_compatible(repository_source, repository_target):
 
1189
                return provider(repository_source, repository_target)
 
1190
        return InterRepository(repository_source, repository_target)
 
1191
 
 
1192
    def lock_read(self):
 
1193
        """Take out a logical read lock.
 
1194
 
 
1195
        This will lock the source branch and the target branch. The source gets
 
1196
        a read lock and the target a read lock.
 
1197
        """
 
1198
        self._double_lock(self.source.lock_read, self.target.lock_read)
 
1199
 
 
1200
    def lock_write(self):
 
1201
        """Take out a logical write lock.
 
1202
 
 
1203
        This will lock the source branch and the target branch. The source gets
 
1204
        a read lock and the target a write lock.
 
1205
        """
 
1206
        self._double_lock(self.source.lock_read, self.target.lock_write)
 
1207
 
 
1208
    @needs_read_lock
 
1209
    def missing_revision_ids(self, revision_id=None):
 
1210
        """Return the revision ids that source has that target does not.
 
1211
        
 
1212
        These are returned in topological order.
 
1213
 
 
1214
        :param revision_id: only return revision ids included by this
 
1215
                            revision_id.
 
1216
        """
 
1217
        # generic, possibly worst case, slow code path.
 
1218
        target_ids = set(self.target.all_revision_ids())
 
1219
        if revision_id is not None:
 
1220
            source_ids = self.source.get_ancestry(revision_id)
 
1221
            assert source_ids.pop(0) == None
 
1222
        else:
 
1223
            source_ids = self.source.all_revision_ids()
 
1224
        result_set = set(source_ids).difference(target_ids)
 
1225
        # this may look like a no-op: its not. It preserves the ordering
 
1226
        # other_ids had while only returning the members from other_ids
 
1227
        # that we've decided we need.
 
1228
        return [rev_id for rev_id in source_ids if rev_id in result_set]
 
1229
 
 
1230
    @classmethod
 
1231
    def register_optimiser(klass, optimiser):
 
1232
        """Register an InterRepository optimiser."""
 
1233
        klass._optimisers.add(optimiser)
 
1234
 
 
1235
    def unlock(self):
 
1236
        """Release the locks on source and target."""
 
1237
        try:
 
1238
            self.target.unlock()
 
1239
        finally:
 
1240
            self.source.unlock()
 
1241
 
 
1242
    @classmethod
 
1243
    def unregister_optimiser(klass, optimiser):
 
1244
        """Unregister an InterRepository optimiser."""
 
1245
        klass._optimisers.remove(optimiser)
 
1246
 
 
1247
 
 
1248
class InterWeaveRepo(InterRepository):
 
1249
    """Optimised code paths between Weave based repositories."""
 
1250
 
 
1251
    _matching_repo_format = _default_format
 
1252
    """Repository format for testing with."""
 
1253
 
 
1254
    @staticmethod
 
1255
    def is_compatible(source, target):
 
1256
        """Be compatible with known Weave formats.
 
1257
        
 
1258
        We dont test for the stores being of specific types becase that
 
1259
        could lead to confusing results, and there is no need to be 
 
1260
        overly general.
 
1261
        """
 
1262
        try:
 
1263
            return (isinstance(source._format, (RepositoryFormat5,
 
1264
                                                RepositoryFormat6,
 
1265
                                                RepositoryFormat7)) and
 
1266
                    isinstance(target._format, (RepositoryFormat5,
 
1267
                                                RepositoryFormat6,
 
1268
                                                RepositoryFormat7)))
 
1269
        except AttributeError:
 
1270
            return False
 
1271
    
 
1272
    @needs_write_lock
 
1273
    def copy_content(self, revision_id=None, basis=None):
 
1274
        """See InterRepository.copy_content()."""
 
1275
        # weave specific optimised path:
 
1276
        if basis is not None:
 
1277
            # copy the basis in, then fetch remaining data.
 
1278
            basis.copy_content_into(self.target, revision_id)
 
1279
            # the basis copy_content_into could misset this.
 
1280
            try:
 
1281
                self.target.set_make_working_trees(self.source.make_working_trees())
 
1282
            except NotImplementedError:
 
1283
                pass
 
1284
            self.target.fetch(self.source, revision_id=revision_id)
 
1285
        else:
 
1286
            try:
 
1287
                self.target.set_make_working_trees(self.source.make_working_trees())
 
1288
            except NotImplementedError:
 
1289
                pass
 
1290
            # FIXME do not peek!
 
1291
            if self.source.control_files._transport.listable():
 
1292
                pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1293
                try:
 
1294
                    copy_all(self.source.weave_store,
 
1295
                        self.target.weave_store, pb=pb)
 
1296
                    pb.update('copying inventory', 0, 1)
 
1297
                    self.target.control_weaves.copy_multi(
 
1298
                        self.source.control_weaves, ['inventory'])
 
1299
                    copy_all(self.source.revision_store,
 
1300
                        self.target.revision_store, pb=pb)
 
1301
                finally:
 
1302
                    pb.finished()
 
1303
            else:
 
1304
                self.target.fetch(self.source, revision_id=revision_id)
 
1305
 
 
1306
    @needs_write_lock
 
1307
    def fetch(self, revision_id=None, pb=None):
 
1308
        """See InterRepository.fetch()."""
 
1309
        from bzrlib.fetch import RepoFetcher
 
1310
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
 
1311
               self.source, self.source._format, self.target, self.target._format)
 
1312
        f = RepoFetcher(to_repository=self.target,
 
1313
                        from_repository=self.source,
 
1314
                        last_revision=revision_id,
 
1315
                        pb=pb)
 
1316
        return f.count_copied, f.failed_revisions
 
1317
 
 
1318
    @needs_read_lock
 
1319
    def missing_revision_ids(self, revision_id=None):
 
1320
        """See InterRepository.missing_revision_ids()."""
 
1321
        # we want all revisions to satisfy revision_id in source.
 
1322
        # but we dont want to stat every file here and there.
 
1323
        # we want then, all revisions other needs to satisfy revision_id 
 
1324
        # checked, but not those that we have locally.
 
1325
        # so the first thing is to get a subset of the revisions to 
 
1326
        # satisfy revision_id in source, and then eliminate those that
 
1327
        # we do already have. 
 
1328
        # this is slow on high latency connection to self, but as as this
 
1329
        # disk format scales terribly for push anyway due to rewriting 
 
1330
        # inventory.weave, this is considered acceptable.
 
1331
        # - RBC 20060209
 
1332
        if revision_id is not None:
 
1333
            source_ids = self.source.get_ancestry(revision_id)
 
1334
            assert source_ids.pop(0) == None
 
1335
        else:
 
1336
            source_ids = self.source._all_possible_ids()
 
1337
        source_ids_set = set(source_ids)
 
1338
        # source_ids is the worst possible case we may need to pull.
 
1339
        # now we want to filter source_ids against what we actually
 
1340
        # have in target, but dont try to check for existence where we know
 
1341
        # we do not have a revision as that would be pointless.
 
1342
        target_ids = set(self.target._all_possible_ids())
 
1343
        possibly_present_revisions = target_ids.intersection(source_ids_set)
 
1344
        actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
 
1345
        required_revisions = source_ids_set.difference(actually_present_revisions)
 
1346
        required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
 
1347
        if revision_id is not None:
 
1348
            # we used get_ancestry to determine source_ids then we are assured all
 
1349
            # revisions referenced are present as they are installed in topological order.
 
1350
            # and the tip revision was validated by get_ancestry.
 
1351
            return required_topo_revisions
 
1352
        else:
 
1353
            # if we just grabbed the possibly available ids, then 
 
1354
            # we only have an estimate of whats available and need to validate
 
1355
            # that against the revision records.
 
1356
            return self.source._eliminate_revisions_not_present(required_topo_revisions)
 
1357
 
 
1358
 
 
1359
InterRepository.register_optimiser(InterWeaveRepo)
 
1360
 
 
1361
 
 
1362
class RepositoryTestProviderAdapter(object):
 
1363
    """A tool to generate a suite testing multiple repository formats at once.
 
1364
 
 
1365
    This is done by copying the test once for each transport and injecting
 
1366
    the transport_server, transport_readonly_server, and bzrdir_format and
 
1367
    repository_format classes into each copy. Each copy is also given a new id()
 
1368
    to make it easy to identify.
 
1369
    """
 
1370
 
 
1371
    def __init__(self, transport_server, transport_readonly_server, formats):
 
1372
        self._transport_server = transport_server
 
1373
        self._transport_readonly_server = transport_readonly_server
 
1374
        self._formats = formats
 
1375
    
 
1376
    def adapt(self, test):
 
1377
        result = TestSuite()
 
1378
        for repository_format, bzrdir_format in self._formats:
 
1379
            new_test = deepcopy(test)
 
1380
            new_test.transport_server = self._transport_server
 
1381
            new_test.transport_readonly_server = self._transport_readonly_server
 
1382
            new_test.bzrdir_format = bzrdir_format
 
1383
            new_test.repository_format = repository_format
 
1384
            def make_new_test_id():
 
1385
                new_id = "%s(%s)" % (new_test.id(), repository_format.__class__.__name__)
 
1386
                return lambda: new_id
 
1387
            new_test.id = make_new_test_id()
 
1388
            result.addTest(new_test)
 
1389
        return result
 
1390
 
 
1391
 
 
1392
class InterRepositoryTestProviderAdapter(object):
 
1393
    """A tool to generate a suite testing multiple inter repository formats.
 
1394
 
 
1395
    This is done by copying the test once for each interrepo provider and injecting
 
1396
    the transport_server, transport_readonly_server, repository_format and 
 
1397
    repository_to_format classes into each copy.
 
1398
    Each copy is also given a new id() to make it easy to identify.
 
1399
    """
 
1400
 
 
1401
    def __init__(self, transport_server, transport_readonly_server, formats):
 
1402
        self._transport_server = transport_server
 
1403
        self._transport_readonly_server = transport_readonly_server
 
1404
        self._formats = formats
 
1405
    
 
1406
    def adapt(self, test):
 
1407
        result = TestSuite()
 
1408
        for interrepo_class, repository_format, repository_format_to in self._formats:
 
1409
            new_test = deepcopy(test)
 
1410
            new_test.transport_server = self._transport_server
 
1411
            new_test.transport_readonly_server = self._transport_readonly_server
 
1412
            new_test.interrepo_class = interrepo_class
 
1413
            new_test.repository_format = repository_format
 
1414
            new_test.repository_format_to = repository_format_to
 
1415
            def make_new_test_id():
 
1416
                new_id = "%s(%s)" % (new_test.id(), interrepo_class.__name__)
 
1417
                return lambda: new_id
 
1418
            new_test.id = make_new_test_id()
 
1419
            result.addTest(new_test)
 
1420
        return result
 
1421
 
 
1422
    @staticmethod
 
1423
    def default_test_list():
 
1424
        """Generate the default list of interrepo permutations to test."""
 
1425
        result = []
 
1426
        # test the default InterRepository between format 6 and the current 
 
1427
        # default format.
 
1428
        # XXX: robertc 20060220 reinstate this when there are two supported
 
1429
        # formats which do not have an optimal code path between them.
 
1430
        result.append((InterRepository,
 
1431
                       RepositoryFormat6(),
 
1432
                       RepositoryFormatKnit1()))
 
1433
        for optimiser in InterRepository._optimisers:
 
1434
            result.append((optimiser,
 
1435
                           optimiser._matching_repo_format,
 
1436
                           optimiser._matching_repo_format
 
1437
                           ))
 
1438
        # if there are specific combinations we want to use, we can add them 
 
1439
        # here.
 
1440
        return result
 
1441
 
 
1442
 
 
1443
class CopyConverter(object):
 
1444
    """A repository conversion tool which just performs a copy of the content.
 
1445
    
 
1446
    This is slow but quite reliable.
 
1447
    """
 
1448
 
 
1449
    def __init__(self, target_format):
 
1450
        """Create a CopyConverter.
 
1451
 
 
1452
        :param target_format: The format the resulting repository should be.
 
1453
        """
 
1454
        self.target_format = target_format
 
1455
        
 
1456
    def convert(self, repo, pb):
 
1457
        """Perform the conversion of to_convert, giving feedback via pb.
 
1458
 
 
1459
        :param to_convert: The disk object to convert.
 
1460
        :param pb: a progress bar to use for progress information.
 
1461
        """
 
1462
        self.pb = pb
 
1463
        self.count = 0
 
1464
        self.total = 3
 
1465
        # this is only useful with metadir layouts - separated repo content.
 
1466
        # trigger an assertion if not such
 
1467
        repo._format.get_format_string()
 
1468
        self.repo_dir = repo.bzrdir
 
1469
        self.step('Moving repository to repository.backup')
 
1470
        self.repo_dir.transport.move('repository', 'repository.backup')
 
1471
        backup_transport =  self.repo_dir.transport.clone('repository.backup')
 
1472
        self.source_repo = repo._format.open(self.repo_dir,
 
1473
            _found=True,
 
1474
            _override_transport=backup_transport)
 
1475
        self.step('Creating new repository')
 
1476
        converted = self.target_format.initialize(self.repo_dir,
 
1477
                                                  self.source_repo.is_shared())
 
1478
        converted.lock_write()
 
1479
        try:
 
1480
            self.step('Copying content into repository.')
 
1481
            self.source_repo.copy_content_into(converted)
 
1482
        finally:
 
1483
            converted.unlock()
 
1484
        self.step('Deleting old repository content.')
 
1485
        self.repo_dir.transport.delete_tree('repository.backup')
 
1486
        self.pb.note('repository converted')
 
1487
 
 
1488
    def step(self, message):
 
1489
        """Update the pb by a step."""
 
1490
        self.count +=1
 
1491
        self.pb.update(message, self.count, self.total)