~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/repository.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-11 23:23:53 UTC
  • Revision ID: mbp@sourcefrog.net-20050311232353-f5e33da490872c6a
Add .bzrignore file

Show diffs side-by-side

added added

removed removed

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