/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: John Arbash Meinel
  • Date: 2006-12-07 20:25:00 UTC
  • mto: This revision was merged to the branch mainline in revision 2171.
  • Revision ID: john@arbash-meinel.com-20061207202500-cdhbw4uw9p60w21o
The logic for when to display 'C' for conflicts was inverted.

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