/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-10-11 00:23:23 UTC
  • mfrom: (2070 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2071.
  • Revision ID: john@arbash-meinel.com-20061011002323-82ba88c293d7caff
[merge] bzr.dev 2070

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