1
# Copyright (C) 2005, 2006 Canonical Ltd
 
 
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.
 
 
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.
 
 
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
 
 
17
from binascii import hexlify
 
 
18
from copy import deepcopy
 
 
19
from cStringIO import StringIO
 
 
22
from unittest import TestSuite
 
 
36
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
 
37
from bzrlib.errors import InvalidRevisionId
 
 
38
from bzrlib.graph import Graph
 
 
39
from bzrlib.inter import InterObject
 
 
40
from bzrlib.inventory import Inventory, InventoryDirectory, ROOT_ID
 
 
41
from bzrlib.knit import KnitVersionedFile, KnitPlainFactory
 
 
42
from bzrlib.lockable_files import LockableFiles, TransportLock
 
 
43
from bzrlib.lockdir import LockDir
 
 
44
from bzrlib.osutils import (safe_unicode, rand_bytes, compact_date, 
 
 
46
from bzrlib.revision import NULL_REVISION, Revision
 
 
47
from bzrlib.revisiontree import RevisionTree
 
 
48
from bzrlib.store.versioned import VersionedFileStore, WeaveStore
 
 
49
from bzrlib.store.text import TextStore
 
 
50
from bzrlib import symbol_versioning
 
 
51
from bzrlib.symbol_versioning import (deprecated_method,
 
 
54
from bzrlib.testament import Testament
 
 
55
from bzrlib.trace import mutter, note, warning
 
 
56
from bzrlib.tsort import topo_sort
 
 
57
from bzrlib.weave import WeaveFile
 
 
60
# Old formats display a warning, but only once
 
 
61
_deprecation_warning_done = False
 
 
64
class Repository(object):
 
 
65
    """Repository holding history for one or more branches.
 
 
67
    The repository holds and retrieves historical information including
 
 
68
    revisions and file history.  It's normally accessed only by the Branch,
 
 
69
    which views a particular line of development through that history.
 
 
71
    The Repository builds on top of Stores and a Transport, which respectively 
 
 
72
    describe the disk data format and the way of accessing the (possibly 
 
 
77
    def add_inventory(self, revid, inv, parents):
 
 
78
        """Add the inventory inv to the repository as revid.
 
 
80
        :param parents: The revision ids of the parents that revid
 
 
81
                        is known to have and are in the repository already.
 
 
83
        returns the sha1 of the serialized inventory.
 
 
85
        assert inv.revision_id is None or inv.revision_id == revid, \
 
 
86
            "Mismatch between inventory revision" \
 
 
87
            " id and insertion revid (%r, %r)" % (inv.revision_id, revid)
 
 
88
        assert inv.root is not None
 
 
89
        inv_text = self.serialise_inventory(inv)
 
 
90
        inv_sha1 = osutils.sha_string(inv_text)
 
 
91
        inv_vf = self.control_weaves.get_weave('inventory',
 
 
92
                                               self.get_transaction())
 
 
93
        self._inventory_add_lines(inv_vf, revid, parents, osutils.split_lines(inv_text))
 
 
96
    def _inventory_add_lines(self, inv_vf, revid, parents, lines):
 
 
98
        for parent in parents:
 
 
100
                final_parents.append(parent)
 
 
102
        inv_vf.add_lines(revid, final_parents, lines)
 
 
105
    def add_revision(self, rev_id, rev, inv=None, config=None):
 
 
106
        """Add rev to the revision store as rev_id.
 
 
108
        :param rev_id: the revision id to use.
 
 
109
        :param rev: The revision object.
 
 
110
        :param inv: The inventory for the revision. if None, it will be looked
 
 
111
                    up in the inventory storer
 
 
112
        :param config: If None no digital signature will be created.
 
 
113
                       If supplied its signature_needed method will be used
 
 
114
                       to determine if a signature should be made.
 
 
116
        if config is not None and config.signature_needed():
 
 
118
                inv = self.get_inventory(rev_id)
 
 
119
            plaintext = Testament(rev, inv).as_short_text()
 
 
120
            self.store_revision_signature(
 
 
121
                gpg.GPGStrategy(config), plaintext, rev_id)
 
 
122
        if not rev_id in self.get_inventory_weave():
 
 
124
                raise errors.WeaveRevisionNotPresent(rev_id,
 
 
125
                                                     self.get_inventory_weave())
 
 
127
                # yes, this is not suitable for adding with ghosts.
 
 
128
                self.add_inventory(rev_id, inv, rev.parent_ids)
 
 
129
        self._revision_store.add_revision(rev, self.get_transaction())
 
 
132
    def _all_possible_ids(self):
 
 
133
        """Return all the possible revisions that we could find."""
 
 
134
        return self.get_inventory_weave().versions()
 
 
136
    def all_revision_ids(self):
 
 
137
        """Returns a list of all the revision ids in the repository. 
 
 
139
        This is deprecated because code should generally work on the graph
 
 
140
        reachable from a particular revision, and ignore any other revisions
 
 
141
        that might be present.  There is no direct replacement method.
 
 
143
        return self._all_revision_ids()
 
 
146
    def _all_revision_ids(self):
 
 
147
        """Returns a list of all the revision ids in the repository. 
 
 
149
        These are in as much topological order as the underlying store can 
 
 
150
        present: for weaves ghosts may lead to a lack of correctness until
 
 
151
        the reweave updates the parents list.
 
 
153
        if self._revision_store.text_store.listable():
 
 
154
            return self._revision_store.all_revision_ids(self.get_transaction())
 
 
155
        result = self._all_possible_ids()
 
 
156
        return self._eliminate_revisions_not_present(result)
 
 
158
    def break_lock(self):
 
 
159
        """Break a lock if one is present from another instance.
 
 
161
        Uses the ui factory to ask for confirmation if the lock may be from
 
 
164
        self.control_files.break_lock()
 
 
167
    def _eliminate_revisions_not_present(self, revision_ids):
 
 
168
        """Check every revision id in revision_ids to see if we have it.
 
 
170
        Returns a set of the present revisions.
 
 
173
        for id in revision_ids:
 
 
174
            if self.has_revision(id):
 
 
179
    def create(a_bzrdir):
 
 
180
        """Construct the current default format repository in a_bzrdir."""
 
 
181
        return RepositoryFormat.get_default_format().initialize(a_bzrdir)
 
 
183
    def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
 
 
184
        """instantiate a Repository.
 
 
186
        :param _format: The format of the repository on disk.
 
 
187
        :param a_bzrdir: The BzrDir of the repository.
 
 
189
        In the future we will have a single api for all stores for
 
 
190
        getting file texts, inventories and revisions, then
 
 
191
        this construct will accept instances of those things.
 
 
193
        super(Repository, self).__init__()
 
 
194
        self._format = _format
 
 
195
        # the following are part of the public API for Repository:
 
 
196
        self.bzrdir = a_bzrdir
 
 
197
        self.control_files = control_files
 
 
198
        self._revision_store = _revision_store
 
 
199
        self.text_store = text_store
 
 
200
        # backwards compatibility
 
 
201
        self.weave_store = text_store
 
 
202
        # not right yet - should be more semantically clear ? 
 
 
204
        self.control_store = control_store
 
 
205
        self.control_weaves = control_store
 
 
206
        # TODO: make sure to construct the right store classes, etc, depending
 
 
207
        # on whether escaping is required.
 
 
208
        self._warn_if_deprecated()
 
 
209
        self._serializer = xml5.serializer_v5
 
 
212
        return '%s(%r)' % (self.__class__.__name__, 
 
 
213
                           self.bzrdir.transport.base)
 
 
216
        return self.control_files.is_locked()
 
 
218
    def lock_write(self):
 
 
219
        self.control_files.lock_write()
 
 
222
        self.control_files.lock_read()
 
 
224
    def get_physical_lock_status(self):
 
 
225
        return self.control_files.get_physical_lock_status()
 
 
228
    def missing_revision_ids(self, other, revision_id=None):
 
 
229
        """Return the revision ids that other has that this does not.
 
 
231
        These are returned in topological order.
 
 
233
        revision_id: only return revision ids included by revision_id.
 
 
235
        return InterRepository.get(other, self).missing_revision_ids(revision_id)
 
 
239
        """Open the repository rooted at base.
 
 
241
        For instance, if the repository is at URL/.bzr/repository,
 
 
242
        Repository.open(URL) -> a Repository instance.
 
 
244
        control = bzrdir.BzrDir.open(base)
 
 
245
        return control.open_repository()
 
 
247
    def copy_content_into(self, destination, revision_id=None, basis=None):
 
 
248
        """Make a complete copy of the content in self into destination.
 
 
250
        This is a destructive operation! Do not use it on existing 
 
 
253
        return InterRepository.get(self, destination).copy_content(revision_id, basis)
 
 
255
    def fetch(self, source, revision_id=None, pb=None):
 
 
256
        """Fetch the content required to construct revision_id from source.
 
 
258
        If revision_id is None all content is copied.
 
 
260
        return InterRepository.get(source, self).fetch(revision_id=revision_id,
 
 
263
    def get_commit_builder(self, branch, parents, config, timestamp=None, 
 
 
264
                           timezone=None, committer=None, revprops=None, 
 
 
266
        """Obtain a CommitBuilder for this repository.
 
 
268
        :param branch: Branch to commit to.
 
 
269
        :param parents: Revision ids of the parents of the new revision.
 
 
270
        :param config: Configuration to use.
 
 
271
        :param timestamp: Optional timestamp recorded for commit.
 
 
272
        :param timezone: Optional timezone for timestamp.
 
 
273
        :param committer: Optional committer to set for commit.
 
 
274
        :param revprops: Optional dictionary of revision properties.
 
 
275
        :param revision_id: Optional revision id.
 
 
277
        return _CommitBuilder(self, parents, config, timestamp, timezone,
 
 
278
                              committer, revprops, revision_id)
 
 
281
        self.control_files.unlock()
 
 
284
    def clone(self, a_bzrdir, revision_id=None, basis=None):
 
 
285
        """Clone this repository into a_bzrdir using the current format.
 
 
287
        Currently no check is made that the format of this repository and
 
 
288
        the bzrdir format are compatible. FIXME RBC 20060201.
 
 
290
        if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
 
 
291
            # use target default format.
 
 
292
            result = a_bzrdir.create_repository()
 
 
293
        # FIXME RBC 20060209 split out the repository type to avoid this check ?
 
 
294
        elif isinstance(a_bzrdir._format,
 
 
295
                      (bzrdir.BzrDirFormat4,
 
 
296
                       bzrdir.BzrDirFormat5,
 
 
297
                       bzrdir.BzrDirFormat6)):
 
 
298
            result = a_bzrdir.open_repository()
 
 
300
            result = self._format.initialize(a_bzrdir, shared=self.is_shared())
 
 
301
        self.copy_content_into(result, revision_id, basis)
 
 
305
    def has_revision(self, revision_id):
 
 
306
        """True if this repository has a copy of the revision."""
 
 
307
        return self._revision_store.has_revision_id(revision_id,
 
 
308
                                                    self.get_transaction())
 
 
311
    def get_revision_reconcile(self, revision_id):
 
 
312
        """'reconcile' helper routine that allows access to a revision always.
 
 
314
        This variant of get_revision does not cross check the weave graph
 
 
315
        against the revision one as get_revision does: but it should only
 
 
316
        be used by reconcile, or reconcile-alike commands that are correcting
 
 
317
        or testing the revision graph.
 
 
319
        if not revision_id or not isinstance(revision_id, basestring):
 
 
320
            raise InvalidRevisionId(revision_id=revision_id, branch=self)
 
 
321
        return self._revision_store.get_revisions([revision_id],
 
 
322
                                                  self.get_transaction())[0]
 
 
324
    def get_revisions(self, revision_ids):
 
 
325
        return self._revision_store.get_revisions(revision_ids,
 
 
326
                                                  self.get_transaction())
 
 
329
    def get_revision_xml(self, revision_id):
 
 
330
        rev = self.get_revision(revision_id) 
 
 
332
        # the current serializer..
 
 
333
        self._revision_store._serializer.write_revision(rev, rev_tmp)
 
 
335
        return rev_tmp.getvalue()
 
 
338
    def get_revision(self, revision_id):
 
 
339
        """Return the Revision object for a named revision"""
 
 
340
        r = self.get_revision_reconcile(revision_id)
 
 
341
        # weave corruption can lead to absent revision markers that should be
 
 
343
        # the following test is reasonably cheap (it needs a single weave read)
 
 
344
        # and the weave is cached in read transactions. In write transactions
 
 
345
        # it is not cached but typically we only read a small number of
 
 
346
        # revisions. For knits when they are introduced we will probably want
 
 
347
        # to ensure that caching write transactions are in use.
 
 
348
        inv = self.get_inventory_weave()
 
 
349
        self._check_revision_parents(r, inv)
 
 
353
    def get_deltas_for_revisions(self, revisions):
 
 
354
        """Produce a generator of revision deltas.
 
 
356
        Note that the input is a sequence of REVISIONS, not revision_ids.
 
 
357
        Trees will be held in memory until the generator exits.
 
 
358
        Each delta is relative to the revision's lefthand predecessor.
 
 
360
        required_trees = set()
 
 
361
        for revision in revisions:
 
 
362
            required_trees.add(revision.revision_id)
 
 
363
            required_trees.update(revision.parent_ids[:1])
 
 
364
        trees = dict((t.get_revision_id(), t) for 
 
 
365
                     t in self.revision_trees(required_trees))
 
 
366
        for revision in revisions:
 
 
367
            if not revision.parent_ids:
 
 
368
                old_tree = self.revision_tree(None)
 
 
370
                old_tree = trees[revision.parent_ids[0]]
 
 
371
            yield trees[revision.revision_id].changes_from(old_tree)
 
 
374
    def get_revision_delta(self, revision_id):
 
 
375
        """Return the delta for one revision.
 
 
377
        The delta is relative to the left-hand predecessor of the
 
 
380
        r = self.get_revision(revision_id)
 
 
381
        return list(self.get_deltas_for_revisions([r]))[0]
 
 
383
    def _check_revision_parents(self, revision, inventory):
 
 
384
        """Private to Repository and Fetch.
 
 
386
        This checks the parentage of revision in an inventory weave for 
 
 
387
        consistency and is only applicable to inventory-weave-for-ancestry
 
 
388
        using repository formats & fetchers.
 
 
390
        weave_parents = inventory.get_parents(revision.revision_id)
 
 
391
        weave_names = inventory.versions()
 
 
392
        for parent_id in revision.parent_ids:
 
 
393
            if parent_id in weave_names:
 
 
394
                # this parent must not be a ghost.
 
 
395
                if not parent_id in weave_parents:
 
 
397
                    raise errors.CorruptRepository(self)
 
 
400
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
 
 
401
        signature = gpg_strategy.sign(plaintext)
 
 
402
        self._revision_store.add_revision_signature_text(revision_id,
 
 
404
                                                         self.get_transaction())
 
 
406
    def fileids_altered_by_revision_ids(self, revision_ids):
 
 
407
        """Find the file ids and versions affected by revisions.
 
 
409
        :param revisions: an iterable containing revision ids.
 
 
410
        :return: a dictionary mapping altered file-ids to an iterable of
 
 
411
        revision_ids. Each altered file-ids has the exact revision_ids that
 
 
412
        altered it listed explicitly.
 
 
414
        assert self._serializer.support_altered_by_hack, \
 
 
415
            ("fileids_altered_by_revision_ids only supported for branches " 
 
 
416
             "which store inventory as unnested xml, not on %r" % self)
 
 
417
        selected_revision_ids = set(revision_ids)
 
 
418
        w = self.get_inventory_weave()
 
 
421
        # this code needs to read every new line in every inventory for the
 
 
422
        # inventories [revision_ids]. Seeing a line twice is ok. Seeing a line
 
 
423
        # not present in one of those inventories is unnecessary but not 
 
 
424
        # harmful because we are filtering by the revision id marker in the
 
 
425
        # inventory lines : we only select file ids altered in one of those  
 
 
426
        # revisions. We don't need to see all lines in the inventory because
 
 
427
        # only those added in an inventory in rev X can contain a revision=X
 
 
429
        for line in w.iter_lines_added_or_present_in_versions(selected_revision_ids):
 
 
430
            start = line.find('file_id="')+9
 
 
431
            if start < 9: continue
 
 
432
            end = line.find('"', start)
 
 
434
            file_id = _unescape_xml(line[start:end])
 
 
436
            start = line.find('revision="')+10
 
 
437
            if start < 10: continue
 
 
438
            end = line.find('"', start)
 
 
440
            revision_id = _unescape_xml(line[start:end])
 
 
441
            if revision_id in selected_revision_ids:
 
 
442
                result.setdefault(file_id, set()).add(revision_id)
 
 
446
    def get_inventory_weave(self):
 
 
447
        return self.control_weaves.get_weave('inventory',
 
 
448
            self.get_transaction())
 
 
451
    def get_inventory(self, revision_id):
 
 
452
        """Get Inventory object by hash."""
 
 
453
        return self.deserialise_inventory(
 
 
454
            revision_id, self.get_inventory_xml(revision_id))
 
 
456
    def deserialise_inventory(self, revision_id, xml):
 
 
457
        """Transform the xml into an inventory object. 
 
 
459
        :param revision_id: The expected revision id of the inventory.
 
 
460
        :param xml: A serialised inventory.
 
 
462
        result = self._serializer.read_inventory_from_string(xml)
 
 
463
        result.root.revision = revision_id
 
 
466
    def serialise_inventory(self, inv):
 
 
467
        return self._serializer.write_inventory_to_string(inv)
 
 
470
    def get_inventory_xml(self, revision_id):
 
 
471
        """Get inventory XML as a file object."""
 
 
473
            assert isinstance(revision_id, basestring), type(revision_id)
 
 
474
            iw = self.get_inventory_weave()
 
 
475
            return iw.get_text(revision_id)
 
 
477
            raise errors.HistoryMissing(self, 'inventory', revision_id)
 
 
480
    def get_inventory_sha1(self, revision_id):
 
 
481
        """Return the sha1 hash of the inventory entry
 
 
483
        return self.get_revision(revision_id).inventory_sha1
 
 
486
    def get_revision_graph(self, revision_id=None):
 
 
487
        """Return a dictionary containing the revision graph.
 
 
489
        :param revision_id: The revision_id to get a graph from. If None, then
 
 
490
        the entire revision graph is returned. This is a deprecated mode of
 
 
491
        operation and will be removed in the future.
 
 
492
        :return: a dictionary of revision_id->revision_parents_list.
 
 
494
        # special case NULL_REVISION
 
 
495
        if revision_id == NULL_REVISION:
 
 
497
        weave = self.get_inventory_weave()
 
 
498
        all_revisions = self._eliminate_revisions_not_present(weave.versions())
 
 
499
        entire_graph = dict([(node, weave.get_parents(node)) for 
 
 
500
                             node in all_revisions])
 
 
501
        if revision_id is None:
 
 
503
        elif revision_id not in entire_graph:
 
 
504
            raise errors.NoSuchRevision(self, revision_id)
 
 
506
            # add what can be reached from revision_id
 
 
508
            pending = set([revision_id])
 
 
509
            while len(pending) > 0:
 
 
511
                result[node] = entire_graph[node]
 
 
512
                for revision_id in result[node]:
 
 
513
                    if revision_id not in result:
 
 
514
                        pending.add(revision_id)
 
 
518
    def get_revision_graph_with_ghosts(self, revision_ids=None):
 
 
519
        """Return a graph of the revisions with ghosts marked as applicable.
 
 
521
        :param revision_ids: an iterable of revisions to graph or None for all.
 
 
522
        :return: a Graph object with the graph reachable from revision_ids.
 
 
526
            pending = set(self.all_revision_ids())
 
 
529
            pending = set(revision_ids)
 
 
530
            # special case NULL_REVISION
 
 
531
            if NULL_REVISION in pending:
 
 
532
                pending.remove(NULL_REVISION)
 
 
533
            required = set(pending)
 
 
536
            revision_id = pending.pop()
 
 
538
                rev = self.get_revision(revision_id)
 
 
539
            except errors.NoSuchRevision:
 
 
540
                if revision_id in required:
 
 
543
                result.add_ghost(revision_id)
 
 
545
            for parent_id in rev.parent_ids:
 
 
546
                # is this queued or done ?
 
 
547
                if (parent_id not in pending and
 
 
548
                    parent_id not in done):
 
 
550
                    pending.add(parent_id)
 
 
551
            result.add_node(revision_id, rev.parent_ids)
 
 
552
            done.add(revision_id)
 
 
556
    def get_revision_inventory(self, revision_id):
 
 
557
        """Return inventory of a past revision."""
 
 
558
        # TODO: Unify this with get_inventory()
 
 
559
        # bzr 0.0.6 and later imposes the constraint that the inventory_id
 
 
560
        # must be the same as its revision, so this is trivial.
 
 
561
        if revision_id is None:
 
 
562
            # This does not make sense: if there is no revision,
 
 
563
            # then it is the current tree inventory surely ?!
 
 
564
            # and thus get_root_id() is something that looks at the last
 
 
565
            # commit on the branch, and the get_root_id is an inventory check.
 
 
566
            raise NotImplementedError
 
 
567
            # return Inventory(self.get_root_id())
 
 
569
            return self.get_inventory(revision_id)
 
 
573
        """Return True if this repository is flagged as a shared repository."""
 
 
574
        raise NotImplementedError(self.is_shared)
 
 
577
    def reconcile(self, other=None, thorough=False):
 
 
578
        """Reconcile this repository."""
 
 
579
        from bzrlib.reconcile import RepoReconciler
 
 
580
        reconciler = RepoReconciler(self, thorough=thorough)
 
 
581
        reconciler.reconcile()
 
 
585
    def revision_tree(self, revision_id):
 
 
586
        """Return Tree for a revision on this branch.
 
 
588
        `revision_id` may be None for the empty tree revision.
 
 
590
        # TODO: refactor this to use an existing revision object
 
 
591
        # so we don't need to read it in twice.
 
 
592
        if revision_id is None or revision_id == NULL_REVISION:
 
 
593
            return RevisionTree(self, Inventory(), NULL_REVISION)
 
 
595
            inv = self.get_revision_inventory(revision_id)
 
 
596
            return RevisionTree(self, inv, revision_id)
 
 
599
    def revision_trees(self, revision_ids):
 
 
600
        """Return Tree for a revision on this branch.
 
 
602
        `revision_id` may not be None or 'null:'"""
 
 
603
        assert None not in revision_ids
 
 
604
        assert NULL_REVISION not in revision_ids
 
 
605
        texts = self.get_inventory_weave().get_texts(revision_ids)
 
 
606
        for text, revision_id in zip(texts, revision_ids):
 
 
607
            inv = self.deserialise_inventory(revision_id, text)
 
 
608
            yield RevisionTree(self, inv, revision_id)
 
 
611
    def get_ancestry(self, revision_id):
 
 
612
        """Return a list of revision-ids integrated by a revision.
 
 
614
        The first element of the list is always None, indicating the origin 
 
 
615
        revision.  This might change when we have history horizons, or 
 
 
616
        perhaps we should have a new API.
 
 
618
        This is topologically sorted.
 
 
620
        if revision_id is None:
 
 
622
        if not self.has_revision(revision_id):
 
 
623
            raise errors.NoSuchRevision(self, revision_id)
 
 
624
        w = self.get_inventory_weave()
 
 
625
        candidates = w.get_ancestry(revision_id)
 
 
626
        return [None] + candidates # self._eliminate_revisions_not_present(candidates)
 
 
629
    def print_file(self, file, revision_id):
 
 
630
        """Print `file` to stdout.
 
 
632
        FIXME RBC 20060125 as John Meinel points out this is a bad api
 
 
633
        - it writes to stdout, it assumes that that is valid etc. Fix
 
 
634
        by creating a new more flexible convenience function.
 
 
636
        tree = self.revision_tree(revision_id)
 
 
637
        # use inventory as it was in that revision
 
 
638
        file_id = tree.inventory.path2id(file)
 
 
640
            # TODO: jam 20060427 Write a test for this code path
 
 
641
            #       it had a bug in it, and was raising the wrong
 
 
643
            raise errors.BzrError("%r is not present in revision %s" % (file, revision_id))
 
 
644
        tree.print_file(file_id)
 
 
646
    def get_transaction(self):
 
 
647
        return self.control_files.get_transaction()
 
 
649
    def revision_parents(self, revid):
 
 
650
        return self.get_inventory_weave().parent_names(revid)
 
 
653
    def set_make_working_trees(self, new_value):
 
 
654
        """Set the policy flag for making working trees when creating branches.
 
 
656
        This only applies to branches that use this repository.
 
 
658
        The default is 'True'.
 
 
659
        :param new_value: True to restore the default, False to disable making
 
 
662
        raise NotImplementedError(self.set_make_working_trees)
 
 
664
    def make_working_trees(self):
 
 
665
        """Returns the policy for making working trees on new branches."""
 
 
666
        raise NotImplementedError(self.make_working_trees)
 
 
669
    def sign_revision(self, revision_id, gpg_strategy):
 
 
670
        plaintext = Testament.from_revision(self, revision_id).as_short_text()
 
 
671
        self.store_revision_signature(gpg_strategy, plaintext, revision_id)
 
 
674
    def has_signature_for_revision_id(self, revision_id):
 
 
675
        """Query for a revision signature for revision_id in the repository."""
 
 
676
        return self._revision_store.has_signature(revision_id,
 
 
677
                                                  self.get_transaction())
 
 
680
    def get_signature_text(self, revision_id):
 
 
681
        """Return the text for a signature."""
 
 
682
        return self._revision_store.get_signature_text(revision_id,
 
 
683
                                                       self.get_transaction())
 
 
686
    def check(self, revision_ids):
 
 
687
        """Check consistency of all history of given revision_ids.
 
 
689
        Different repository implementations should override _check().
 
 
691
        :param revision_ids: A non-empty list of revision_ids whose ancestry
 
 
692
             will be checked.  Typically the last revision_id of a branch.
 
 
695
            raise ValueError("revision_ids must be non-empty in %s.check" 
 
 
697
        return self._check(revision_ids)
 
 
699
    def _check(self, revision_ids):
 
 
700
        result = check.Check(self)
 
 
704
    def _warn_if_deprecated(self):
 
 
705
        global _deprecation_warning_done
 
 
706
        if _deprecation_warning_done:
 
 
708
        _deprecation_warning_done = True
 
 
709
        warning("Format %s for %s is deprecated - please use 'bzr upgrade' to get better performance"
 
 
710
                % (self._format, self.bzrdir.transport.base))
 
 
713
class AllInOneRepository(Repository):
 
 
714
    """Legacy support - the repository behaviour for all-in-one branches."""
 
 
716
    def __init__(self, _format, a_bzrdir, _revision_store, control_store, text_store):
 
 
717
        # we reuse one control files instance.
 
 
718
        dir_mode = a_bzrdir._control_files._dir_mode
 
 
719
        file_mode = a_bzrdir._control_files._file_mode
 
 
721
        def get_store(name, compressed=True, prefixed=False):
 
 
722
            # FIXME: This approach of assuming stores are all entirely compressed
 
 
723
            # or entirely uncompressed is tidy, but breaks upgrade from 
 
 
724
            # some existing branches where there's a mixture; we probably 
 
 
725
            # still want the option to look for both.
 
 
726
            relpath = a_bzrdir._control_files._escape(name)
 
 
727
            store = TextStore(a_bzrdir._control_files._transport.clone(relpath),
 
 
728
                              prefixed=prefixed, compressed=compressed,
 
 
731
            #if self._transport.should_cache():
 
 
732
            #    cache_path = os.path.join(self.cache_root, name)
 
 
733
            #    os.mkdir(cache_path)
 
 
734
            #    store = bzrlib.store.CachedStore(store, cache_path)
 
 
737
        # not broken out yet because the controlweaves|inventory_store
 
 
738
        # and text_store | weave_store bits are still different.
 
 
739
        if isinstance(_format, RepositoryFormat4):
 
 
740
            # cannot remove these - there is still no consistent api 
 
 
741
            # which allows access to this old info.
 
 
742
            self.inventory_store = get_store('inventory-store')
 
 
743
            text_store = get_store('text-store')
 
 
744
        super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, _revision_store, control_store, text_store)
 
 
748
        """AllInOne repositories cannot be shared."""
 
 
752
    def set_make_working_trees(self, new_value):
 
 
753
        """Set the policy flag for making working trees when creating branches.
 
 
755
        This only applies to branches that use this repository.
 
 
757
        The default is 'True'.
 
 
758
        :param new_value: True to restore the default, False to disable making
 
 
761
        raise NotImplementedError(self.set_make_working_trees)
 
 
763
    def make_working_trees(self):
 
 
764
        """Returns the policy for making working trees on new branches."""
 
 
768
def install_revision(repository, rev, revision_tree):
 
 
769
    """Install all revision data into a repository."""
 
 
772
    for p_id in rev.parent_ids:
 
 
773
        if repository.has_revision(p_id):
 
 
774
            present_parents.append(p_id)
 
 
775
            parent_trees[p_id] = repository.revision_tree(p_id)
 
 
777
            parent_trees[p_id] = repository.revision_tree(None)
 
 
779
    inv = revision_tree.inventory
 
 
781
    # backwards compatability hack: skip the root id.
 
 
782
    entries = inv.iter_entries()
 
 
784
    # Add the texts that are not already present
 
 
785
    for path, ie in entries:
 
 
786
        w = repository.weave_store.get_weave_or_empty(ie.file_id,
 
 
787
                repository.get_transaction())
 
 
788
        if ie.revision not in w:
 
 
790
            # FIXME: TODO: The following loop *may* be overlapping/duplicate
 
 
791
            # with InventoryEntry.find_previous_heads(). if it is, then there
 
 
792
            # is a latent bug here where the parents may have ancestors of each
 
 
794
            for revision, tree in parent_trees.iteritems():
 
 
795
                if ie.file_id not in tree:
 
 
797
                parent_id = tree.inventory[ie.file_id].revision
 
 
798
                if parent_id in text_parents:
 
 
800
                text_parents.append(parent_id)
 
 
802
            vfile = repository.weave_store.get_weave_or_empty(ie.file_id, 
 
 
803
                repository.get_transaction())
 
 
804
            lines = revision_tree.get_file(ie.file_id).readlines()
 
 
805
            vfile.add_lines(rev.revision_id, text_parents, lines)
 
 
807
        # install the inventory
 
 
808
        repository.add_inventory(rev.revision_id, inv, present_parents)
 
 
809
    except errors.RevisionAlreadyPresent:
 
 
811
    repository.add_revision(rev.revision_id, rev, inv)
 
 
814
class MetaDirRepository(Repository):
 
 
815
    """Repositories in the new meta-dir layout."""
 
 
817
    def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
 
 
818
        super(MetaDirRepository, self).__init__(_format,
 
 
824
        dir_mode = self.control_files._dir_mode
 
 
825
        file_mode = self.control_files._file_mode
 
 
829
        """Return True if this repository is flagged as a shared repository."""
 
 
830
        return self.control_files._transport.has('shared-storage')
 
 
833
    def set_make_working_trees(self, new_value):
 
 
834
        """Set the policy flag for making working trees when creating branches.
 
 
836
        This only applies to branches that use this repository.
 
 
838
        The default is 'True'.
 
 
839
        :param new_value: True to restore the default, False to disable making
 
 
844
                self.control_files._transport.delete('no-working-trees')
 
 
845
            except errors.NoSuchFile:
 
 
848
            self.control_files.put_utf8('no-working-trees', '')
 
 
850
    def make_working_trees(self):
 
 
851
        """Returns the policy for making working trees on new branches."""
 
 
852
        return not self.control_files._transport.has('no-working-trees')
 
 
855
class KnitRepository(MetaDirRepository):
 
 
856
    """Knit format repository."""
 
 
858
    def _warn_if_deprecated(self):
 
 
859
        # This class isn't deprecated
 
 
862
    def _inventory_add_lines(self, inv_vf, revid, parents, lines):
 
 
863
        inv_vf.add_lines_with_ghosts(revid, parents, lines)
 
 
866
    def _all_revision_ids(self):
 
 
867
        """See Repository.all_revision_ids()."""
 
 
868
        # Knits get the revision graph from the index of the revision knit, so
 
 
869
        # it's always possible even if they're on an unlistable transport.
 
 
870
        return self._revision_store.all_revision_ids(self.get_transaction())
 
 
872
    def fileid_involved_between_revs(self, from_revid, to_revid):
 
 
873
        """Find file_id(s) which are involved in the changes between revisions.
 
 
875
        This determines the set of revisions which are involved, and then
 
 
876
        finds all file ids affected by those revisions.
 
 
878
        vf = self._get_revision_vf()
 
 
879
        from_set = set(vf.get_ancestry(from_revid))
 
 
880
        to_set = set(vf.get_ancestry(to_revid))
 
 
881
        changed = to_set.difference(from_set)
 
 
882
        return self._fileid_involved_by_set(changed)
 
 
884
    def fileid_involved(self, last_revid=None):
 
 
885
        """Find all file_ids modified in the ancestry of last_revid.
 
 
887
        :param last_revid: If None, last_revision() will be used.
 
 
890
            changed = set(self.all_revision_ids())
 
 
892
            changed = set(self.get_ancestry(last_revid))
 
 
895
        return self._fileid_involved_by_set(changed)
 
 
898
    def get_ancestry(self, revision_id):
 
 
899
        """Return a list of revision-ids integrated by a revision.
 
 
901
        This is topologically sorted.
 
 
903
        if revision_id is None:
 
 
905
        vf = self._get_revision_vf()
 
 
907
            return [None] + vf.get_ancestry(revision_id)
 
 
908
        except errors.RevisionNotPresent:
 
 
909
            raise errors.NoSuchRevision(self, revision_id)
 
 
912
    def get_revision(self, revision_id):
 
 
913
        """Return the Revision object for a named revision"""
 
 
914
        return self.get_revision_reconcile(revision_id)
 
 
917
    def get_revision_graph(self, revision_id=None):
 
 
918
        """Return a dictionary containing the revision graph.
 
 
920
        :param revision_id: The revision_id to get a graph from. If None, then
 
 
921
        the entire revision graph is returned. This is a deprecated mode of
 
 
922
        operation and will be removed in the future.
 
 
923
        :return: a dictionary of revision_id->revision_parents_list.
 
 
925
        # special case NULL_REVISION
 
 
926
        if revision_id == NULL_REVISION:
 
 
928
        weave = self._get_revision_vf()
 
 
929
        entire_graph = weave.get_graph()
 
 
930
        if revision_id is None:
 
 
931
            return weave.get_graph()
 
 
932
        elif revision_id not in weave:
 
 
933
            raise errors.NoSuchRevision(self, revision_id)
 
 
935
            # add what can be reached from revision_id
 
 
937
            pending = set([revision_id])
 
 
938
            while len(pending) > 0:
 
 
940
                result[node] = weave.get_parents(node)
 
 
941
                for revision_id in result[node]:
 
 
942
                    if revision_id not in result:
 
 
943
                        pending.add(revision_id)
 
 
947
    def get_revision_graph_with_ghosts(self, revision_ids=None):
 
 
948
        """Return a graph of the revisions with ghosts marked as applicable.
 
 
950
        :param revision_ids: an iterable of revisions to graph or None for all.
 
 
951
        :return: a Graph object with the graph reachable from revision_ids.
 
 
954
        vf = self._get_revision_vf()
 
 
955
        versions = set(vf.versions())
 
 
957
            pending = set(self.all_revision_ids())
 
 
960
            pending = set(revision_ids)
 
 
961
            # special case NULL_REVISION
 
 
962
            if NULL_REVISION in pending:
 
 
963
                pending.remove(NULL_REVISION)
 
 
964
            required = set(pending)
 
 
967
            revision_id = pending.pop()
 
 
968
            if not revision_id in versions:
 
 
969
                if revision_id in required:
 
 
970
                    raise errors.NoSuchRevision(self, revision_id)
 
 
972
                result.add_ghost(revision_id)
 
 
973
                # mark it as done so we don't try for it again.
 
 
974
                done.add(revision_id)
 
 
976
            parent_ids = vf.get_parents_with_ghosts(revision_id)
 
 
977
            for parent_id in parent_ids:
 
 
978
                # is this queued or done ?
 
 
979
                if (parent_id not in pending and
 
 
980
                    parent_id not in done):
 
 
982
                    pending.add(parent_id)
 
 
983
            result.add_node(revision_id, parent_ids)
 
 
984
            done.add(revision_id)
 
 
987
    def _get_revision_vf(self):
 
 
988
        """:return: a versioned file containing the revisions."""
 
 
989
        vf = self._revision_store.get_revision_file(self.get_transaction())
 
 
993
    def reconcile(self, other=None, thorough=False):
 
 
994
        """Reconcile this repository."""
 
 
995
        from bzrlib.reconcile import KnitReconciler
 
 
996
        reconciler = KnitReconciler(self, thorough=thorough)
 
 
997
        reconciler.reconcile()
 
 
1000
    def revision_parents(self, revision_id):
 
 
1001
        return self._get_revision_vf().get_parents(revision_id)
 
 
1004
class KnitRepository2(KnitRepository):
 
 
1006
    def __init__(self, _format, a_bzrdir, control_files, _revision_store,
 
 
1007
                 control_store, text_store):
 
 
1008
        KnitRepository.__init__(self, _format, a_bzrdir, control_files,
 
 
1009
                              _revision_store, control_store, text_store)
 
 
1010
        self._serializer = xml6.serializer_v6
 
 
1012
    def deserialise_inventory(self, revision_id, xml):
 
 
1013
        """Transform the xml into an inventory object. 
 
 
1015
        :param revision_id: The expected revision id of the inventory.
 
 
1016
        :param xml: A serialised inventory.
 
 
1018
        result = self._serializer.read_inventory_from_string(xml)
 
 
1019
        assert result.root.revision is not None
 
 
1022
    def serialise_inventory(self, inv):
 
 
1023
        """Transform the inventory object into XML text.
 
 
1025
        :param revision_id: The expected revision id of the inventory.
 
 
1026
        :param xml: A serialised inventory.
 
 
1028
        assert inv.revision_id is not None
 
 
1029
        assert inv.root.revision is not None
 
 
1030
        return KnitRepository.serialise_inventory(self, inv)
 
 
1032
    def get_commit_builder(self, branch, parents, config, timestamp=None, 
 
 
1033
                           timezone=None, committer=None, revprops=None, 
 
 
1035
        """Obtain a CommitBuilder for this repository.
 
 
1037
        :param branch: Branch to commit to.
 
 
1038
        :param parents: Revision ids of the parents of the new revision.
 
 
1039
        :param config: Configuration to use.
 
 
1040
        :param timestamp: Optional timestamp recorded for commit.
 
 
1041
        :param timezone: Optional timezone for timestamp.
 
 
1042
        :param committer: Optional committer to set for commit.
 
 
1043
        :param revprops: Optional dictionary of revision properties.
 
 
1044
        :param revision_id: Optional revision id.
 
 
1046
        return RootCommitBuilder(self, parents, config, timestamp, timezone,
 
 
1047
                                 committer, revprops, revision_id)
 
 
1050
class RepositoryFormat(object):
 
 
1051
    """A repository format.
 
 
1053
    Formats provide three things:
 
 
1054
     * An initialization routine to construct repository data on disk.
 
 
1055
     * a format string which is used when the BzrDir supports versioned
 
 
1057
     * an open routine which returns a Repository instance.
 
 
1059
    Formats are placed in an dict by their format string for reference 
 
 
1060
    during opening. These should be subclasses of RepositoryFormat
 
 
1063
    Once a format is deprecated, just deprecate the initialize and open
 
 
1064
    methods on the format class. Do not deprecate the object, as the 
 
 
1065
    object will be created every system load.
 
 
1067
    Common instance attributes:
 
 
1068
    _matchingbzrdir - the bzrdir format that the repository format was
 
 
1069
    originally written to work with. This can be used if manually
 
 
1070
    constructing a bzrdir and repository, or more commonly for test suite
 
 
1074
    _default_format = None
 
 
1075
    """The default format used for new repositories."""
 
 
1078
    """The known formats."""
 
 
1081
        return "<%s>" % self.__class__.__name__
 
 
1084
    def find_format(klass, a_bzrdir):
 
 
1085
        """Return the format for the repository object in a_bzrdir."""
 
 
1087
            transport = a_bzrdir.get_repository_transport(None)
 
 
1088
            format_string = transport.get("format").read()
 
 
1089
            return klass._formats[format_string]
 
 
1090
        except errors.NoSuchFile:
 
 
1091
            raise errors.NoRepositoryPresent(a_bzrdir)
 
 
1093
            raise errors.UnknownFormatError(format=format_string)
 
 
1095
    def _get_control_store(self, repo_transport, control_files):
 
 
1096
        """Return the control store for this repository."""
 
 
1097
        raise NotImplementedError(self._get_control_store)
 
 
1100
    def get_default_format(klass):
 
 
1101
        """Return the current default format."""
 
 
1102
        return klass._default_format
 
 
1104
    def get_format_string(self):
 
 
1105
        """Return the ASCII format string that identifies this format.
 
 
1107
        Note that in pre format ?? repositories the format string is 
 
 
1108
        not permitted nor written to disk.
 
 
1110
        raise NotImplementedError(self.get_format_string)
 
 
1112
    def get_format_description(self):
 
 
1113
        """Return the short description for this format."""
 
 
1114
        raise NotImplementedError(self.get_format_description)
 
 
1116
    def _get_revision_store(self, repo_transport, control_files):
 
 
1117
        """Return the revision store object for this a_bzrdir."""
 
 
1118
        raise NotImplementedError(self._get_revision_store)
 
 
1120
    def _get_text_rev_store(self,
 
 
1127
        """Common logic for getting a revision store for a repository.
 
 
1129
        see self._get_revision_store for the subclass-overridable method to 
 
 
1130
        get the store for a repository.
 
 
1132
        from bzrlib.store.revision.text import TextRevisionStore
 
 
1133
        dir_mode = control_files._dir_mode
 
 
1134
        file_mode = control_files._file_mode
 
 
1135
        text_store =TextStore(transport.clone(name),
 
 
1137
                              compressed=compressed,
 
 
1139
                              file_mode=file_mode)
 
 
1140
        _revision_store = TextRevisionStore(text_store, serializer)
 
 
1141
        return _revision_store
 
 
1143
    def _get_versioned_file_store(self,
 
 
1148
                                  versionedfile_class=WeaveFile,
 
 
1149
                                  versionedfile_kwargs={},
 
 
1151
        weave_transport = control_files._transport.clone(name)
 
 
1152
        dir_mode = control_files._dir_mode
 
 
1153
        file_mode = control_files._file_mode
 
 
1154
        return VersionedFileStore(weave_transport, prefixed=prefixed,
 
 
1156
                                  file_mode=file_mode,
 
 
1157
                                  versionedfile_class=versionedfile_class,
 
 
1158
                                  versionedfile_kwargs=versionedfile_kwargs,
 
 
1161
    def initialize(self, a_bzrdir, shared=False):
 
 
1162
        """Initialize a repository of this format in a_bzrdir.
 
 
1164
        :param a_bzrdir: The bzrdir to put the new repository in it.
 
 
1165
        :param shared: The repository should be initialized as a sharable one.
 
 
1167
        This may raise UninitializableFormat if shared repository are not
 
 
1168
        compatible the a_bzrdir.
 
 
1171
    def is_supported(self):
 
 
1172
        """Is this format supported?
 
 
1174
        Supported formats must be initializable and openable.
 
 
1175
        Unsupported formats may not support initialization or committing or 
 
 
1176
        some other features depending on the reason for not being supported.
 
 
1180
    def check_conversion_target(self, target_format):
 
 
1181
        raise NotImplementedError(self.check_conversion_target)
 
 
1183
    def open(self, a_bzrdir, _found=False):
 
 
1184
        """Return an instance of this format for the bzrdir a_bzrdir.
 
 
1186
        _found is a private parameter, do not use it.
 
 
1188
        raise NotImplementedError(self.open)
 
 
1191
    def register_format(klass, format):
 
 
1192
        klass._formats[format.get_format_string()] = format
 
 
1195
    def set_default_format(klass, format):
 
 
1196
        klass._default_format = format
 
 
1199
    def unregister_format(klass, format):
 
 
1200
        assert klass._formats[format.get_format_string()] is format
 
 
1201
        del klass._formats[format.get_format_string()]
 
 
1204
class PreSplitOutRepositoryFormat(RepositoryFormat):
 
 
1205
    """Base class for the pre split out repository formats."""
 
 
1207
    rich_root_data = False
 
 
1209
    def initialize(self, a_bzrdir, shared=False, _internal=False):
 
 
1210
        """Create a weave repository.
 
 
1212
        TODO: when creating split out bzr branch formats, move this to a common
 
 
1213
        base for Format5, Format6. or something like that.
 
 
1215
        from bzrlib.weavefile import write_weave_v5
 
 
1216
        from bzrlib.weave import Weave
 
 
1219
            raise errors.IncompatibleFormat(self, a_bzrdir._format)
 
 
1222
            # always initialized when the bzrdir is.
 
 
1223
            return self.open(a_bzrdir, _found=True)
 
 
1225
        # Create an empty weave
 
 
1227
        write_weave_v5(Weave(), sio)
 
 
1228
        empty_weave = sio.getvalue()
 
 
1230
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
 
1231
        dirs = ['revision-store', 'weaves']
 
 
1232
        files = [('inventory.weave', StringIO(empty_weave)),
 
 
1235
        # FIXME: RBC 20060125 don't peek under the covers
 
 
1236
        # NB: no need to escape relative paths that are url safe.
 
 
1237
        control_files = LockableFiles(a_bzrdir.transport, 'branch-lock',
 
 
1239
        control_files.create_lock()
 
 
1240
        control_files.lock_write()
 
 
1241
        control_files._transport.mkdir_multi(dirs,
 
 
1242
                mode=control_files._dir_mode)
 
 
1244
            for file, content in files:
 
 
1245
                control_files.put(file, content)
 
 
1247
            control_files.unlock()
 
 
1248
        return self.open(a_bzrdir, _found=True)
 
 
1250
    def _get_control_store(self, repo_transport, control_files):
 
 
1251
        """Return the control store for this repository."""
 
 
1252
        return self._get_versioned_file_store('',
 
 
1257
    def _get_text_store(self, transport, control_files):
 
 
1258
        """Get a store for file texts for this format."""
 
 
1259
        raise NotImplementedError(self._get_text_store)
 
 
1261
    def open(self, a_bzrdir, _found=False):
 
 
1262
        """See RepositoryFormat.open()."""
 
 
1264
            # we are being called directly and must probe.
 
 
1265
            raise NotImplementedError
 
 
1267
        repo_transport = a_bzrdir.get_repository_transport(None)
 
 
1268
        control_files = a_bzrdir._control_files
 
 
1269
        text_store = self._get_text_store(repo_transport, control_files)
 
 
1270
        control_store = self._get_control_store(repo_transport, control_files)
 
 
1271
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
 
1272
        return AllInOneRepository(_format=self,
 
 
1274
                                  _revision_store=_revision_store,
 
 
1275
                                  control_store=control_store,
 
 
1276
                                  text_store=text_store)
 
 
1278
    def check_conversion_target(self, target_format):
 
 
1282
class RepositoryFormat4(PreSplitOutRepositoryFormat):
 
 
1283
    """Bzr repository format 4.
 
 
1285
    This repository format has:
 
 
1287
     - TextStores for texts, inventories,revisions.
 
 
1289
    This format is deprecated: it indexes texts using a text id which is
 
 
1290
    removed in format 5; initialization and write support for this format
 
 
1295
        super(RepositoryFormat4, self).__init__()
 
 
1296
        self._matchingbzrdir = bzrdir.BzrDirFormat4()
 
 
1298
    def get_format_description(self):
 
 
1299
        """See RepositoryFormat.get_format_description()."""
 
 
1300
        return "Repository format 4"
 
 
1302
    def initialize(self, url, shared=False, _internal=False):
 
 
1303
        """Format 4 branches cannot be created."""
 
 
1304
        raise errors.UninitializableFormat(self)
 
 
1306
    def is_supported(self):
 
 
1307
        """Format 4 is not supported.
 
 
1309
        It is not supported because the model changed from 4 to 5 and the
 
 
1310
        conversion logic is expensive - so doing it on the fly was not 
 
 
1315
    def _get_control_store(self, repo_transport, control_files):
 
 
1316
        """Format 4 repositories have no formal control store at this point.
 
 
1318
        This will cause any control-file-needing apis to fail - this is desired.
 
 
1322
    def _get_revision_store(self, repo_transport, control_files):
 
 
1323
        """See RepositoryFormat._get_revision_store()."""
 
 
1324
        from bzrlib.xml4 import serializer_v4
 
 
1325
        return self._get_text_rev_store(repo_transport,
 
 
1328
                                        serializer=serializer_v4)
 
 
1330
    def _get_text_store(self, transport, control_files):
 
 
1331
        """See RepositoryFormat._get_text_store()."""
 
 
1334
class RepositoryFormat5(PreSplitOutRepositoryFormat):
 
 
1335
    """Bzr control format 5.
 
 
1337
    This repository format has:
 
 
1338
     - weaves for file texts and inventory
 
 
1340
     - TextStores for revisions and signatures.
 
 
1344
        super(RepositoryFormat5, self).__init__()
 
 
1345
        self._matchingbzrdir = bzrdir.BzrDirFormat5()
 
 
1347
    def get_format_description(self):
 
 
1348
        """See RepositoryFormat.get_format_description()."""
 
 
1349
        return "Weave repository format 5"
 
 
1351
    def _get_revision_store(self, repo_transport, control_files):
 
 
1352
        """See RepositoryFormat._get_revision_store()."""
 
 
1353
        """Return the revision store object for this a_bzrdir."""
 
 
1354
        return self._get_text_rev_store(repo_transport,
 
 
1359
    def _get_text_store(self, transport, control_files):
 
 
1360
        """See RepositoryFormat._get_text_store()."""
 
 
1361
        return self._get_versioned_file_store('weaves', transport, control_files, prefixed=False)
 
 
1364
class RepositoryFormat6(PreSplitOutRepositoryFormat):
 
 
1365
    """Bzr control format 6.
 
 
1367
    This repository format has:
 
 
1368
     - weaves for file texts and inventory
 
 
1369
     - hash subdirectory based stores.
 
 
1370
     - TextStores for revisions and signatures.
 
 
1374
        super(RepositoryFormat6, self).__init__()
 
 
1375
        self._matchingbzrdir = bzrdir.BzrDirFormat6()
 
 
1377
    def get_format_description(self):
 
 
1378
        """See RepositoryFormat.get_format_description()."""
 
 
1379
        return "Weave repository format 6"
 
 
1381
    def _get_revision_store(self, repo_transport, control_files):
 
 
1382
        """See RepositoryFormat._get_revision_store()."""
 
 
1383
        return self._get_text_rev_store(repo_transport,
 
 
1389
    def _get_text_store(self, transport, control_files):
 
 
1390
        """See RepositoryFormat._get_text_store()."""
 
 
1391
        return self._get_versioned_file_store('weaves', transport, control_files)
 
 
1394
class MetaDirRepositoryFormat(RepositoryFormat):
 
 
1395
    """Common base class for the new repositories using the metadir layout."""
 
 
1397
    rich_root_data = False
 
 
1400
        super(MetaDirRepositoryFormat, self).__init__()
 
 
1401
        self._matchingbzrdir = bzrdir.BzrDirMetaFormat1()
 
 
1403
    def _create_control_files(self, a_bzrdir):
 
 
1404
        """Create the required files and the initial control_files object."""
 
 
1405
        # FIXME: RBC 20060125 don't peek under the covers
 
 
1406
        # NB: no need to escape relative paths that are url safe.
 
 
1407
        repository_transport = a_bzrdir.get_repository_transport(self)
 
 
1408
        control_files = LockableFiles(repository_transport, 'lock', LockDir)
 
 
1409
        control_files.create_lock()
 
 
1410
        return control_files
 
 
1412
    def _upload_blank_content(self, a_bzrdir, dirs, files, utf8_files, shared):
 
 
1413
        """Upload the initial blank content."""
 
 
1414
        control_files = self._create_control_files(a_bzrdir)
 
 
1415
        control_files.lock_write()
 
 
1417
            control_files._transport.mkdir_multi(dirs,
 
 
1418
                    mode=control_files._dir_mode)
 
 
1419
            for file, content in files:
 
 
1420
                control_files.put(file, content)
 
 
1421
            for file, content in utf8_files:
 
 
1422
                control_files.put_utf8(file, content)
 
 
1424
                control_files.put_utf8('shared-storage', '')
 
 
1426
            control_files.unlock()
 
 
1429
class RepositoryFormat7(MetaDirRepositoryFormat):
 
 
1430
    """Bzr repository 7.
 
 
1432
    This repository format has:
 
 
1433
     - weaves for file texts and inventory
 
 
1434
     - hash subdirectory based stores.
 
 
1435
     - TextStores for revisions and signatures.
 
 
1436
     - a format marker of its own
 
 
1437
     - an optional 'shared-storage' flag
 
 
1438
     - an optional 'no-working-trees' flag
 
 
1441
    def _get_control_store(self, repo_transport, control_files):
 
 
1442
        """Return the control store for this repository."""
 
 
1443
        return self._get_versioned_file_store('',
 
 
1448
    def get_format_string(self):
 
 
1449
        """See RepositoryFormat.get_format_string()."""
 
 
1450
        return "Bazaar-NG Repository format 7"
 
 
1452
    def get_format_description(self):
 
 
1453
        """See RepositoryFormat.get_format_description()."""
 
 
1454
        return "Weave repository format 7"
 
 
1456
    def check_conversion_target(self, target_format):
 
 
1459
    def _get_revision_store(self, repo_transport, control_files):
 
 
1460
        """See RepositoryFormat._get_revision_store()."""
 
 
1461
        return self._get_text_rev_store(repo_transport,
 
 
1468
    def _get_text_store(self, transport, control_files):
 
 
1469
        """See RepositoryFormat._get_text_store()."""
 
 
1470
        return self._get_versioned_file_store('weaves',
 
 
1474
    def initialize(self, a_bzrdir, shared=False):
 
 
1475
        """Create a weave repository.
 
 
1477
        :param shared: If true the repository will be initialized as a shared
 
 
1480
        from bzrlib.weavefile import write_weave_v5
 
 
1481
        from bzrlib.weave import Weave
 
 
1483
        # Create an empty weave
 
 
1485
        write_weave_v5(Weave(), sio)
 
 
1486
        empty_weave = sio.getvalue()
 
 
1488
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
 
1489
        dirs = ['revision-store', 'weaves']
 
 
1490
        files = [('inventory.weave', StringIO(empty_weave)), 
 
 
1492
        utf8_files = [('format', self.get_format_string())]
 
 
1494
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
 
 
1495
        return self.open(a_bzrdir=a_bzrdir, _found=True)
 
 
1497
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
 
1498
        """See RepositoryFormat.open().
 
 
1500
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
 
1501
                                    repository at a slightly different url
 
 
1502
                                    than normal. I.e. during 'upgrade'.
 
 
1505
            format = RepositoryFormat.find_format(a_bzrdir)
 
 
1506
            assert format.__class__ ==  self.__class__
 
 
1507
        if _override_transport is not None:
 
 
1508
            repo_transport = _override_transport
 
 
1510
            repo_transport = a_bzrdir.get_repository_transport(None)
 
 
1511
        control_files = LockableFiles(repo_transport, 'lock', LockDir)
 
 
1512
        text_store = self._get_text_store(repo_transport, control_files)
 
 
1513
        control_store = self._get_control_store(repo_transport, control_files)
 
 
1514
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
 
1515
        return MetaDirRepository(_format=self,
 
 
1517
                                 control_files=control_files,
 
 
1518
                                 _revision_store=_revision_store,
 
 
1519
                                 control_store=control_store,
 
 
1520
                                 text_store=text_store)
 
 
1523
class RepositoryFormatKnit(MetaDirRepositoryFormat):
 
 
1524
    """Bzr repository knit format (generalized). 
 
 
1526
    This repository format has:
 
 
1527
     - knits for file texts and inventory
 
 
1528
     - hash subdirectory based stores.
 
 
1529
     - knits for revisions and signatures
 
 
1530
     - TextStores for revisions and signatures.
 
 
1531
     - a format marker of its own
 
 
1532
     - an optional 'shared-storage' flag
 
 
1533
     - an optional 'no-working-trees' flag
 
 
1537
    def _get_control_store(self, repo_transport, control_files):
 
 
1538
        """Return the control store for this repository."""
 
 
1539
        return VersionedFileStore(
 
 
1542
            file_mode=control_files._file_mode,
 
 
1543
            versionedfile_class=KnitVersionedFile,
 
 
1544
            versionedfile_kwargs={'factory':KnitPlainFactory()},
 
 
1547
    def _get_revision_store(self, repo_transport, control_files):
 
 
1548
        """See RepositoryFormat._get_revision_store()."""
 
 
1549
        from bzrlib.store.revision.knit import KnitRevisionStore
 
 
1550
        versioned_file_store = VersionedFileStore(
 
 
1552
            file_mode=control_files._file_mode,
 
 
1555
            versionedfile_class=KnitVersionedFile,
 
 
1556
            versionedfile_kwargs={'delta':False, 'factory':KnitPlainFactory(),},
 
 
1559
        return KnitRevisionStore(versioned_file_store)
 
 
1561
    def _get_text_store(self, transport, control_files):
 
 
1562
        """See RepositoryFormat._get_text_store()."""
 
 
1563
        return self._get_versioned_file_store('knits',
 
 
1566
                                              versionedfile_class=KnitVersionedFile,
 
 
1567
                                              versionedfile_kwargs={
 
 
1568
                                                  'create_parent_dir':True,
 
 
1569
                                                  'delay_create':True,
 
 
1570
                                                  'dir_mode':control_files._dir_mode,
 
 
1574
    def initialize(self, a_bzrdir, shared=False):
 
 
1575
        """Create a knit format 1 repository.
 
 
1577
        :param a_bzrdir: bzrdir to contain the new repository; must already
 
 
1579
        :param shared: If true the repository will be initialized as a shared
 
 
1582
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
 
1583
        dirs = ['revision-store', 'knits']
 
 
1585
        utf8_files = [('format', self.get_format_string())]
 
 
1587
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
 
 
1588
        repo_transport = a_bzrdir.get_repository_transport(None)
 
 
1589
        control_files = LockableFiles(repo_transport, 'lock', LockDir)
 
 
1590
        control_store = self._get_control_store(repo_transport, control_files)
 
 
1591
        transaction = transactions.WriteTransaction()
 
 
1592
        # trigger a write of the inventory store.
 
 
1593
        control_store.get_weave_or_empty('inventory', transaction)
 
 
1594
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
 
1595
        _revision_store.has_revision_id('A', transaction)
 
 
1596
        _revision_store.get_signature_file(transaction)
 
 
1597
        return self.open(a_bzrdir=a_bzrdir, _found=True)
 
 
1599
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
 
1600
        """See RepositoryFormat.open().
 
 
1602
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
 
1603
                                    repository at a slightly different url
 
 
1604
                                    than normal. I.e. during 'upgrade'.
 
 
1607
            format = RepositoryFormat.find_format(a_bzrdir)
 
 
1608
            assert format.__class__ ==  self.__class__
 
 
1609
        if _override_transport is not None:
 
 
1610
            repo_transport = _override_transport
 
 
1612
            repo_transport = a_bzrdir.get_repository_transport(None)
 
 
1613
        control_files = LockableFiles(repo_transport, 'lock', LockDir)
 
 
1614
        text_store = self._get_text_store(repo_transport, control_files)
 
 
1615
        control_store = self._get_control_store(repo_transport, control_files)
 
 
1616
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
 
1617
        return KnitRepository(_format=self,
 
 
1619
                              control_files=control_files,
 
 
1620
                              _revision_store=_revision_store,
 
 
1621
                              control_store=control_store,
 
 
1622
                              text_store=text_store)
 
 
1625
class RepositoryFormatKnit1(RepositoryFormatKnit):
 
 
1626
    """Bzr repository knit format 1.
 
 
1628
    This repository format has:
 
 
1629
     - knits for file texts and inventory
 
 
1630
     - hash subdirectory based stores.
 
 
1631
     - knits for revisions and signatures
 
 
1632
     - TextStores for revisions and signatures.
 
 
1633
     - a format marker of its own
 
 
1634
     - an optional 'shared-storage' flag
 
 
1635
     - an optional 'no-working-trees' flag
 
 
1638
    This format was introduced in bzr 0.8.
 
 
1640
    def get_format_string(self):
 
 
1641
        """See RepositoryFormat.get_format_string()."""
 
 
1642
        return "Bazaar-NG Knit Repository Format 1"
 
 
1644
    def get_format_description(self):
 
 
1645
        """See RepositoryFormat.get_format_description()."""
 
 
1646
        return "Knit repository format 1"
 
 
1648
    def check_conversion_target(self, target_format):
 
 
1652
class RepositoryFormatKnit2(RepositoryFormatKnit):
 
 
1653
    """Bzr repository knit format 2.
 
 
1655
    THIS FORMAT IS EXPERIMENTAL
 
 
1656
    This repository format has:
 
 
1657
     - knits for file texts and inventory
 
 
1658
     - hash subdirectory based stores.
 
 
1659
     - knits for revisions and signatures
 
 
1660
     - TextStores for revisions and signatures.
 
 
1661
     - a format marker of its own
 
 
1662
     - an optional 'shared-storage' flag
 
 
1663
     - an optional 'no-working-trees' flag
 
 
1665
     - Support for recording full info about the tree root
 
 
1669
    rich_root_data = True
 
 
1671
    def get_format_string(self):
 
 
1672
        """See RepositoryFormat.get_format_string()."""
 
 
1673
        return "Bazaar Knit Repository Format 2\n"
 
 
1675
    def get_format_description(self):
 
 
1676
        """See RepositoryFormat.get_format_description()."""
 
 
1677
        return "Knit repository format 2"
 
 
1679
    def check_conversion_target(self, target_format):
 
 
1680
        if not target_format.rich_root_data:
 
 
1681
            raise errors.BadConversionTarget(
 
 
1682
                'Does not support rich root data.', target_format)
 
 
1684
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
 
1685
        """See RepositoryFormat.open().
 
 
1687
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
 
1688
                                    repository at a slightly different url
 
 
1689
                                    than normal. I.e. during 'upgrade'.
 
 
1692
            format = RepositoryFormat.find_format(a_bzrdir)
 
 
1693
            assert format.__class__ ==  self.__class__
 
 
1694
        if _override_transport is not None:
 
 
1695
            repo_transport = _override_transport
 
 
1697
            repo_transport = a_bzrdir.get_repository_transport(None)
 
 
1698
        control_files = LockableFiles(repo_transport, 'lock', LockDir)
 
 
1699
        text_store = self._get_text_store(repo_transport, control_files)
 
 
1700
        control_store = self._get_control_store(repo_transport, control_files)
 
 
1701
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
 
1702
        return KnitRepository2(_format=self,
 
 
1704
                               control_files=control_files,
 
 
1705
                               _revision_store=_revision_store,
 
 
1706
                               control_store=control_store,
 
 
1707
                               text_store=text_store)
 
 
1711
# formats which have no format string are not discoverable
 
 
1712
# and not independently creatable, so are not registered.
 
 
1713
RepositoryFormat.register_format(RepositoryFormat7())
 
 
1714
_default_format = RepositoryFormatKnit1()
 
 
1715
RepositoryFormat.register_format(_default_format)
 
 
1716
RepositoryFormat.register_format(RepositoryFormatKnit2())
 
 
1717
RepositoryFormat.set_default_format(_default_format)
 
 
1718
_legacy_formats = [RepositoryFormat4(),
 
 
1719
                   RepositoryFormat5(),
 
 
1720
                   RepositoryFormat6()]
 
 
1723
class InterRepository(InterObject):
 
 
1724
    """This class represents operations taking place between two repositories.
 
 
1726
    Its instances have methods like copy_content and fetch, and contain
 
 
1727
    references to the source and target repositories these operations can be 
 
 
1730
    Often we will provide convenience methods on 'repository' which carry out
 
 
1731
    operations with another repository - they will always forward to
 
 
1732
    InterRepository.get(other).method_name(parameters).
 
 
1736
    """The available optimised InterRepository types."""
 
 
1738
    def copy_content(self, revision_id=None, basis=None):
 
 
1739
        raise NotImplementedError(self.copy_content)
 
 
1741
    def fetch(self, revision_id=None, pb=None):
 
 
1742
        """Fetch the content required to construct revision_id.
 
 
1744
        The content is copied from self.source to self.target.
 
 
1746
        :param revision_id: if None all content is copied, if NULL_REVISION no
 
 
1748
        :param pb: optional progress bar to use for progress reports. If not
 
 
1749
                   provided a default one will be created.
 
 
1751
        Returns the copied revision count and the failed revisions in a tuple:
 
 
1754
        raise NotImplementedError(self.fetch)
 
 
1757
    def missing_revision_ids(self, revision_id=None):
 
 
1758
        """Return the revision ids that source has that target does not.
 
 
1760
        These are returned in topological order.
 
 
1762
        :param revision_id: only return revision ids included by this
 
 
1765
        # generic, possibly worst case, slow code path.
 
 
1766
        target_ids = set(self.target.all_revision_ids())
 
 
1767
        if revision_id is not None:
 
 
1768
            source_ids = self.source.get_ancestry(revision_id)
 
 
1769
            assert source_ids[0] is None
 
 
1772
            source_ids = self.source.all_revision_ids()
 
 
1773
        result_set = set(source_ids).difference(target_ids)
 
 
1774
        # this may look like a no-op: its not. It preserves the ordering
 
 
1775
        # other_ids had while only returning the members from other_ids
 
 
1776
        # that we've decided we need.
 
 
1777
        return [rev_id for rev_id in source_ids if rev_id in result_set]
 
 
1780
class InterSameDataRepository(InterRepository):
 
 
1781
    """Code for converting between repositories that represent the same data.
 
 
1783
    Data format and model must match for this to work.
 
 
1786
    _matching_repo_format = RepositoryFormat4()
 
 
1787
    """Repository format for testing with."""
 
 
1790
    def is_compatible(source, target):
 
 
1791
        if not isinstance(source, Repository):
 
 
1793
        if not isinstance(target, Repository):
 
 
1795
        if source._format.rich_root_data == target._format.rich_root_data:
 
 
1801
    def copy_content(self, revision_id=None, basis=None):
 
 
1802
        """Make a complete copy of the content in self into destination.
 
 
1804
        This is a destructive operation! Do not use it on existing 
 
 
1807
        :param revision_id: Only copy the content needed to construct
 
 
1808
                            revision_id and its parents.
 
 
1809
        :param basis: Copy the needed data preferentially from basis.
 
 
1812
            self.target.set_make_working_trees(self.source.make_working_trees())
 
 
1813
        except NotImplementedError:
 
 
1815
        # grab the basis available data
 
 
1816
        if basis is not None:
 
 
1817
            self.target.fetch(basis, revision_id=revision_id)
 
 
1818
        # but don't bother fetching if we have the needed data now.
 
 
1819
        if (revision_id not in (None, NULL_REVISION) and 
 
 
1820
            self.target.has_revision(revision_id)):
 
 
1822
        self.target.fetch(self.source, revision_id=revision_id)
 
 
1825
    def fetch(self, revision_id=None, pb=None):
 
 
1826
        """See InterRepository.fetch()."""
 
 
1827
        from bzrlib.fetch import GenericRepoFetcher
 
 
1828
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
 
 
1829
               self.source, self.source._format, self.target, 
 
 
1830
               self.target._format)
 
 
1831
        f = GenericRepoFetcher(to_repository=self.target,
 
 
1832
                               from_repository=self.source,
 
 
1833
                               last_revision=revision_id,
 
 
1835
        return f.count_copied, f.failed_revisions
 
 
1838
class InterWeaveRepo(InterSameDataRepository):
 
 
1839
    """Optimised code paths between Weave based repositories."""
 
 
1841
    _matching_repo_format = RepositoryFormat7()
 
 
1842
    """Repository format for testing with."""
 
 
1845
    def is_compatible(source, target):
 
 
1846
        """Be compatible with known Weave formats.
 
 
1848
        We don't test for the stores being of specific types because that
 
 
1849
        could lead to confusing results, and there is no need to be 
 
 
1853
            return (isinstance(source._format, (RepositoryFormat5,
 
 
1855
                                                RepositoryFormat7)) and
 
 
1856
                    isinstance(target._format, (RepositoryFormat5,
 
 
1858
                                                RepositoryFormat7)))
 
 
1859
        except AttributeError:
 
 
1863
    def copy_content(self, revision_id=None, basis=None):
 
 
1864
        """See InterRepository.copy_content()."""
 
 
1865
        # weave specific optimised path:
 
 
1866
        if basis is not None:
 
 
1867
            # copy the basis in, then fetch remaining data.
 
 
1868
            basis.copy_content_into(self.target, revision_id)
 
 
1869
            # the basis copy_content_into could miss-set this.
 
 
1871
                self.target.set_make_working_trees(self.source.make_working_trees())
 
 
1872
            except NotImplementedError:
 
 
1874
            self.target.fetch(self.source, revision_id=revision_id)
 
 
1877
                self.target.set_make_working_trees(self.source.make_working_trees())
 
 
1878
            except NotImplementedError:
 
 
1880
            # FIXME do not peek!
 
 
1881
            if self.source.control_files._transport.listable():
 
 
1882
                pb = ui.ui_factory.nested_progress_bar()
 
 
1884
                    self.target.weave_store.copy_all_ids(
 
 
1885
                        self.source.weave_store,
 
 
1887
                        from_transaction=self.source.get_transaction(),
 
 
1888
                        to_transaction=self.target.get_transaction())
 
 
1889
                    pb.update('copying inventory', 0, 1)
 
 
1890
                    self.target.control_weaves.copy_multi(
 
 
1891
                        self.source.control_weaves, ['inventory'],
 
 
1892
                        from_transaction=self.source.get_transaction(),
 
 
1893
                        to_transaction=self.target.get_transaction())
 
 
1894
                    self.target._revision_store.text_store.copy_all_ids(
 
 
1895
                        self.source._revision_store.text_store,
 
 
1900
                self.target.fetch(self.source, revision_id=revision_id)
 
 
1903
    def fetch(self, revision_id=None, pb=None):
 
 
1904
        """See InterRepository.fetch()."""
 
 
1905
        from bzrlib.fetch import GenericRepoFetcher
 
 
1906
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
 
 
1907
               self.source, self.source._format, self.target, self.target._format)
 
 
1908
        f = GenericRepoFetcher(to_repository=self.target,
 
 
1909
                               from_repository=self.source,
 
 
1910
                               last_revision=revision_id,
 
 
1912
        return f.count_copied, f.failed_revisions
 
 
1915
    def missing_revision_ids(self, revision_id=None):
 
 
1916
        """See InterRepository.missing_revision_ids()."""
 
 
1917
        # we want all revisions to satisfy revision_id in source.
 
 
1918
        # but we don't want to stat every file here and there.
 
 
1919
        # we want then, all revisions other needs to satisfy revision_id 
 
 
1920
        # checked, but not those that we have locally.
 
 
1921
        # so the first thing is to get a subset of the revisions to 
 
 
1922
        # satisfy revision_id in source, and then eliminate those that
 
 
1923
        # we do already have. 
 
 
1924
        # this is slow on high latency connection to self, but as as this
 
 
1925
        # disk format scales terribly for push anyway due to rewriting 
 
 
1926
        # inventory.weave, this is considered acceptable.
 
 
1928
        if revision_id is not None:
 
 
1929
            source_ids = self.source.get_ancestry(revision_id)
 
 
1930
            assert source_ids[0] is None
 
 
1933
            source_ids = self.source._all_possible_ids()
 
 
1934
        source_ids_set = set(source_ids)
 
 
1935
        # source_ids is the worst possible case we may need to pull.
 
 
1936
        # now we want to filter source_ids against what we actually
 
 
1937
        # have in target, but don't try to check for existence where we know
 
 
1938
        # we do not have a revision as that would be pointless.
 
 
1939
        target_ids = set(self.target._all_possible_ids())
 
 
1940
        possibly_present_revisions = target_ids.intersection(source_ids_set)
 
 
1941
        actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
 
 
1942
        required_revisions = source_ids_set.difference(actually_present_revisions)
 
 
1943
        required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
 
 
1944
        if revision_id is not None:
 
 
1945
            # we used get_ancestry to determine source_ids then we are assured all
 
 
1946
            # revisions referenced are present as they are installed in topological order.
 
 
1947
            # and the tip revision was validated by get_ancestry.
 
 
1948
            return required_topo_revisions
 
 
1950
            # if we just grabbed the possibly available ids, then 
 
 
1951
            # we only have an estimate of whats available and need to validate
 
 
1952
            # that against the revision records.
 
 
1953
            return self.source._eliminate_revisions_not_present(required_topo_revisions)
 
 
1956
class InterKnitRepo(InterSameDataRepository):
 
 
1957
    """Optimised code paths between Knit based repositories."""
 
 
1959
    _matching_repo_format = RepositoryFormatKnit1()
 
 
1960
    """Repository format for testing with."""
 
 
1963
    def is_compatible(source, target):
 
 
1964
        """Be compatible with known Knit formats.
 
 
1966
        We don't test for the stores being of specific types because that
 
 
1967
        could lead to confusing results, and there is no need to be 
 
 
1971
            return (isinstance(source._format, (RepositoryFormatKnit1)) and
 
 
1972
                    isinstance(target._format, (RepositoryFormatKnit1)))
 
 
1973
        except AttributeError:
 
 
1977
    def fetch(self, revision_id=None, pb=None):
 
 
1978
        """See InterRepository.fetch()."""
 
 
1979
        from bzrlib.fetch import KnitRepoFetcher
 
 
1980
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
 
 
1981
               self.source, self.source._format, self.target, self.target._format)
 
 
1982
        f = KnitRepoFetcher(to_repository=self.target,
 
 
1983
                            from_repository=self.source,
 
 
1984
                            last_revision=revision_id,
 
 
1986
        return f.count_copied, f.failed_revisions
 
 
1989
    def missing_revision_ids(self, revision_id=None):
 
 
1990
        """See InterRepository.missing_revision_ids()."""
 
 
1991
        if revision_id is not None:
 
 
1992
            source_ids = self.source.get_ancestry(revision_id)
 
 
1993
            assert source_ids[0] is None
 
 
1996
            source_ids = self.source._all_possible_ids()
 
 
1997
        source_ids_set = set(source_ids)
 
 
1998
        # source_ids is the worst possible case we may need to pull.
 
 
1999
        # now we want to filter source_ids against what we actually
 
 
2000
        # have in target, but don't try to check for existence where we know
 
 
2001
        # we do not have a revision as that would be pointless.
 
 
2002
        target_ids = set(self.target._all_possible_ids())
 
 
2003
        possibly_present_revisions = target_ids.intersection(source_ids_set)
 
 
2004
        actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
 
 
2005
        required_revisions = source_ids_set.difference(actually_present_revisions)
 
 
2006
        required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
 
 
2007
        if revision_id is not None:
 
 
2008
            # we used get_ancestry to determine source_ids then we are assured all
 
 
2009
            # revisions referenced are present as they are installed in topological order.
 
 
2010
            # and the tip revision was validated by get_ancestry.
 
 
2011
            return required_topo_revisions
 
 
2013
            # if we just grabbed the possibly available ids, then 
 
 
2014
            # we only have an estimate of whats available and need to validate
 
 
2015
            # that against the revision records.
 
 
2016
            return self.source._eliminate_revisions_not_present(required_topo_revisions)
 
 
2019
class InterModel1and2(InterRepository):
 
 
2021
    _matching_repo_format = None
 
 
2024
    def is_compatible(source, target):
 
 
2025
        if not isinstance(source, Repository):
 
 
2027
        if not isinstance(target, Repository):
 
 
2029
        if not source._format.rich_root_data and target._format.rich_root_data:
 
 
2035
    def fetch(self, revision_id=None, pb=None):
 
 
2036
        """See InterRepository.fetch()."""
 
 
2037
        from bzrlib.fetch import Model1toKnit2Fetcher
 
 
2038
        f = Model1toKnit2Fetcher(to_repository=self.target,
 
 
2039
                                 from_repository=self.source,
 
 
2040
                                 last_revision=revision_id,
 
 
2042
        return f.count_copied, f.failed_revisions
 
 
2045
    def copy_content(self, revision_id=None, basis=None):
 
 
2046
        """Make a complete copy of the content in self into destination.
 
 
2048
        This is a destructive operation! Do not use it on existing 
 
 
2051
        :param revision_id: Only copy the content needed to construct
 
 
2052
                            revision_id and its parents.
 
 
2053
        :param basis: Copy the needed data preferentially from basis.
 
 
2056
            self.target.set_make_working_trees(self.source.make_working_trees())
 
 
2057
        except NotImplementedError:
 
 
2059
        # grab the basis available data
 
 
2060
        if basis is not None:
 
 
2061
            self.target.fetch(basis, revision_id=revision_id)
 
 
2062
        # but don't bother fetching if we have the needed data now.
 
 
2063
        if (revision_id not in (None, NULL_REVISION) and 
 
 
2064
            self.target.has_revision(revision_id)):
 
 
2066
        self.target.fetch(self.source, revision_id=revision_id)
 
 
2069
class InterKnit1and2(InterKnitRepo):
 
 
2071
    _matching_repo_format = None
 
 
2074
    def is_compatible(source, target):
 
 
2075
        """Be compatible with Knit1 source and Knit2 target"""
 
 
2077
            return (isinstance(source._format, (RepositoryFormatKnit1)) and
 
 
2078
                    isinstance(target._format, (RepositoryFormatKnit2)))
 
 
2079
        except AttributeError:
 
 
2083
    def fetch(self, revision_id=None, pb=None):
 
 
2084
        """See InterRepository.fetch()."""
 
 
2085
        from bzrlib.fetch import Knit1to2Fetcher
 
 
2086
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
 
 
2087
               self.source, self.source._format, self.target, 
 
 
2088
               self.target._format)
 
 
2089
        f = Knit1to2Fetcher(to_repository=self.target,
 
 
2090
                            from_repository=self.source,
 
 
2091
                            last_revision=revision_id,
 
 
2093
        return f.count_copied, f.failed_revisions
 
 
2096
InterRepository.register_optimiser(InterSameDataRepository)
 
 
2097
InterRepository.register_optimiser(InterWeaveRepo)
 
 
2098
InterRepository.register_optimiser(InterKnitRepo)
 
 
2099
InterRepository.register_optimiser(InterModel1and2)
 
 
2100
InterRepository.register_optimiser(InterKnit1and2)
 
 
2103
class RepositoryTestProviderAdapter(object):
 
 
2104
    """A tool to generate a suite testing multiple repository formats at once.
 
 
2106
    This is done by copying the test once for each transport and injecting
 
 
2107
    the transport_server, transport_readonly_server, and bzrdir_format and
 
 
2108
    repository_format classes into each copy. Each copy is also given a new id()
 
 
2109
    to make it easy to identify.
 
 
2112
    def __init__(self, transport_server, transport_readonly_server, formats):
 
 
2113
        self._transport_server = transport_server
 
 
2114
        self._transport_readonly_server = transport_readonly_server
 
 
2115
        self._formats = formats
 
 
2117
    def adapt(self, test):
 
 
2118
        result = TestSuite()
 
 
2119
        for repository_format, bzrdir_format in self._formats:
 
 
2120
            new_test = deepcopy(test)
 
 
2121
            new_test.transport_server = self._transport_server
 
 
2122
            new_test.transport_readonly_server = self._transport_readonly_server
 
 
2123
            new_test.bzrdir_format = bzrdir_format
 
 
2124
            new_test.repository_format = repository_format
 
 
2125
            def make_new_test_id():
 
 
2126
                new_id = "%s(%s)" % (new_test.id(), repository_format.__class__.__name__)
 
 
2127
                return lambda: new_id
 
 
2128
            new_test.id = make_new_test_id()
 
 
2129
            result.addTest(new_test)
 
 
2133
class InterRepositoryTestProviderAdapter(object):
 
 
2134
    """A tool to generate a suite testing multiple inter repository formats.
 
 
2136
    This is done by copying the test once for each interrepo provider and injecting
 
 
2137
    the transport_server, transport_readonly_server, repository_format and 
 
 
2138
    repository_to_format classes into each copy.
 
 
2139
    Each copy is also given a new id() to make it easy to identify.
 
 
2142
    def __init__(self, transport_server, transport_readonly_server, formats):
 
 
2143
        self._transport_server = transport_server
 
 
2144
        self._transport_readonly_server = transport_readonly_server
 
 
2145
        self._formats = formats
 
 
2147
    def adapt(self, test):
 
 
2148
        result = TestSuite()
 
 
2149
        for interrepo_class, repository_format, repository_format_to in self._formats:
 
 
2150
            new_test = deepcopy(test)
 
 
2151
            new_test.transport_server = self._transport_server
 
 
2152
            new_test.transport_readonly_server = self._transport_readonly_server
 
 
2153
            new_test.interrepo_class = interrepo_class
 
 
2154
            new_test.repository_format = repository_format
 
 
2155
            new_test.repository_format_to = repository_format_to
 
 
2156
            def make_new_test_id():
 
 
2157
                new_id = "%s(%s)" % (new_test.id(), interrepo_class.__name__)
 
 
2158
                return lambda: new_id
 
 
2159
            new_test.id = make_new_test_id()
 
 
2160
            result.addTest(new_test)
 
 
2164
    def default_test_list():
 
 
2165
        """Generate the default list of interrepo permutations to test."""
 
 
2167
        # test the default InterRepository between format 6 and the current 
 
 
2169
        # XXX: robertc 20060220 reinstate this when there are two supported
 
 
2170
        # formats which do not have an optimal code path between them.
 
 
2171
        #result.append((InterRepository,
 
 
2172
        #               RepositoryFormat6(),
 
 
2173
        #               RepositoryFormatKnit1()))
 
 
2174
        for optimiser in InterRepository._optimisers:
 
 
2175
            if optimiser._matching_repo_format is not None:
 
 
2176
                result.append((optimiser,
 
 
2177
                               optimiser._matching_repo_format,
 
 
2178
                               optimiser._matching_repo_format
 
 
2180
        # if there are specific combinations we want to use, we can add them 
 
 
2182
        result.append((InterModel1and2, RepositoryFormat5(),
 
 
2183
                       RepositoryFormatKnit2()))
 
 
2184
        result.append((InterKnit1and2, RepositoryFormatKnit1(),
 
 
2185
                       RepositoryFormatKnit2()))
 
 
2189
class CopyConverter(object):
 
 
2190
    """A repository conversion tool which just performs a copy of the content.
 
 
2192
    This is slow but quite reliable.
 
 
2195
    def __init__(self, target_format):
 
 
2196
        """Create a CopyConverter.
 
 
2198
        :param target_format: The format the resulting repository should be.
 
 
2200
        self.target_format = target_format
 
 
2202
    def convert(self, repo, pb):
 
 
2203
        """Perform the conversion of to_convert, giving feedback via pb.
 
 
2205
        :param to_convert: The disk object to convert.
 
 
2206
        :param pb: a progress bar to use for progress information.
 
 
2211
        # this is only useful with metadir layouts - separated repo content.
 
 
2212
        # trigger an assertion if not such
 
 
2213
        repo._format.get_format_string()
 
 
2214
        self.repo_dir = repo.bzrdir
 
 
2215
        self.step('Moving repository to repository.backup')
 
 
2216
        self.repo_dir.transport.move('repository', 'repository.backup')
 
 
2217
        backup_transport =  self.repo_dir.transport.clone('repository.backup')
 
 
2218
        repo._format.check_conversion_target(self.target_format)
 
 
2219
        self.source_repo = repo._format.open(self.repo_dir,
 
 
2221
            _override_transport=backup_transport)
 
 
2222
        self.step('Creating new repository')
 
 
2223
        converted = self.target_format.initialize(self.repo_dir,
 
 
2224
                                                  self.source_repo.is_shared())
 
 
2225
        converted.lock_write()
 
 
2227
            self.step('Copying content into repository.')
 
 
2228
            self.source_repo.copy_content_into(converted)
 
 
2231
        self.step('Deleting old repository content.')
 
 
2232
        self.repo_dir.transport.delete_tree('repository.backup')
 
 
2233
        self.pb.note('repository converted')
 
 
2235
    def step(self, message):
 
 
2236
        """Update the pb by a step."""
 
 
2238
        self.pb.update(message, self.count, self.total)
 
 
2241
class CommitBuilder(object):
 
 
2242
    """Provides an interface to build up a commit.
 
 
2244
    This allows describing a tree to be committed without needing to 
 
 
2245
    know the internals of the format of the repository.
 
 
2248
    record_root_entry = False
 
 
2249
    def __init__(self, repository, parents, config, timestamp=None, 
 
 
2250
                 timezone=None, committer=None, revprops=None, 
 
 
2252
        """Initiate a CommitBuilder.
 
 
2254
        :param repository: Repository to commit to.
 
 
2255
        :param parents: Revision ids of the parents of the new revision.
 
 
2256
        :param config: Configuration to use.
 
 
2257
        :param timestamp: Optional timestamp recorded for commit.
 
 
2258
        :param timezone: Optional timezone for timestamp.
 
 
2259
        :param committer: Optional committer to set for commit.
 
 
2260
        :param revprops: Optional dictionary of revision properties.
 
 
2261
        :param revision_id: Optional revision id.
 
 
2263
        self._config = config
 
 
2265
        if committer is None:
 
 
2266
            self._committer = self._config.username()
 
 
2268
            assert isinstance(committer, basestring), type(committer)
 
 
2269
            self._committer = committer
 
 
2271
        self.new_inventory = Inventory(None)
 
 
2272
        self._new_revision_id = revision_id
 
 
2273
        self.parents = parents
 
 
2274
        self.repository = repository
 
 
2277
        if revprops is not None:
 
 
2278
            self._revprops.update(revprops)
 
 
2280
        if timestamp is None:
 
 
2281
            timestamp = time.time()
 
 
2282
        # Restrict resolution to 1ms
 
 
2283
        self._timestamp = round(timestamp, 3)
 
 
2285
        if timezone is None:
 
 
2286
            self._timezone = local_time_offset()
 
 
2288
            self._timezone = int(timezone)
 
 
2290
        self._generate_revision_if_needed()
 
 
2292
    def commit(self, message):
 
 
2293
        """Make the actual commit.
 
 
2295
        :return: The revision id of the recorded revision.
 
 
2297
        rev = Revision(timestamp=self._timestamp,
 
 
2298
                       timezone=self._timezone,
 
 
2299
                       committer=self._committer,
 
 
2301
                       inventory_sha1=self.inv_sha1,
 
 
2302
                       revision_id=self._new_revision_id,
 
 
2303
                       properties=self._revprops)
 
 
2304
        rev.parent_ids = self.parents
 
 
2305
        self.repository.add_revision(self._new_revision_id, rev, 
 
 
2306
            self.new_inventory, self._config)
 
 
2307
        return self._new_revision_id
 
 
2309
    def finish_inventory(self):
 
 
2310
        """Tell the builder that the inventory is finished."""
 
 
2311
        if self.new_inventory.root is None:
 
 
2312
            symbol_versioning.warn('Root entry should be supplied to'
 
 
2313
                ' record_entry_contents, as of bzr 0.10.',
 
 
2314
                 DeprecationWarning, stacklevel=2)
 
 
2315
            self.new_inventory.add(InventoryDirectory(ROOT_ID, '', None))
 
 
2316
        self.new_inventory.revision_id = self._new_revision_id
 
 
2317
        self.inv_sha1 = self.repository.add_inventory(
 
 
2318
            self._new_revision_id,
 
 
2323
    def _gen_revision_id(self):
 
 
2324
        """Return new revision-id."""
 
 
2325
        s = '%s-%s-' % (self._config.user_email(), 
 
 
2326
                        compact_date(self._timestamp))
 
 
2327
        s += hexlify(rand_bytes(8))
 
 
2330
    def _generate_revision_if_needed(self):
 
 
2331
        """Create a revision id if None was supplied.
 
 
2333
        If the repository can not support user-specified revision ids
 
 
2334
        they should override this function and raise UnsupportedOperation
 
 
2335
        if _new_revision_id is not None.
 
 
2337
        :raises: UnsupportedOperation
 
 
2339
        if self._new_revision_id is None:
 
 
2340
            self._new_revision_id = self._gen_revision_id()
 
 
2342
    def record_entry_contents(self, ie, parent_invs, path, tree):
 
 
2343
        """Record the content of ie from tree into the commit if needed.
 
 
2345
        Side effect: sets ie.revision when unchanged
 
 
2347
        :param ie: An inventory entry present in the commit.
 
 
2348
        :param parent_invs: The inventories of the parent revisions of the
 
 
2350
        :param path: The path the entry is at in the tree.
 
 
2351
        :param tree: The tree which contains this entry and should be used to 
 
 
2354
        if self.new_inventory.root is None and ie.parent_id is not None:
 
 
2355
            symbol_versioning.warn('Root entry should be supplied to'
 
 
2356
                ' record_entry_contents, as of bzr 0.10.',
 
 
2357
                 DeprecationWarning, stacklevel=2)
 
 
2358
            self.record_entry_contents(tree.inventory.root.copy(), parent_invs,
 
 
2360
        self.new_inventory.add(ie)
 
 
2362
        # ie.revision is always None if the InventoryEntry is considered
 
 
2363
        # for committing. ie.snapshot will record the correct revision 
 
 
2364
        # which may be the sole parent if it is untouched.
 
 
2365
        if ie.revision is not None:
 
 
2368
        # In this revision format, root entries have no knit or weave
 
 
2369
        if ie is self.new_inventory.root:
 
 
2370
            if len(parent_invs):
 
 
2371
                ie.revision = parent_invs[0].root.revision
 
 
2375
        previous_entries = ie.find_previous_heads(
 
 
2377
            self.repository.weave_store,
 
 
2378
            self.repository.get_transaction())
 
 
2379
        # we are creating a new revision for ie in the history store
 
 
2381
        ie.snapshot(self._new_revision_id, path, previous_entries, tree, self)
 
 
2383
    def modified_directory(self, file_id, file_parents):
 
 
2384
        """Record the presence of a symbolic link.
 
 
2386
        :param file_id: The file_id of the link to record.
 
 
2387
        :param file_parents: The per-file parent revision ids.
 
 
2389
        self._add_text_to_weave(file_id, [], file_parents.keys())
 
 
2391
    def modified_file_text(self, file_id, file_parents,
 
 
2392
                           get_content_byte_lines, text_sha1=None,
 
 
2394
        """Record the text of file file_id
 
 
2396
        :param file_id: The file_id of the file to record the text of.
 
 
2397
        :param file_parents: The per-file parent revision ids.
 
 
2398
        :param get_content_byte_lines: A callable which will return the byte
 
 
2400
        :param text_sha1: Optional SHA1 of the file contents.
 
 
2401
        :param text_size: Optional size of the file contents.
 
 
2403
        # mutter('storing text of file {%s} in revision {%s} into %r',
 
 
2404
        #        file_id, self._new_revision_id, self.repository.weave_store)
 
 
2405
        # special case to avoid diffing on renames or 
 
 
2407
        if (len(file_parents) == 1
 
 
2408
            and text_sha1 == file_parents.values()[0].text_sha1
 
 
2409
            and text_size == file_parents.values()[0].text_size):
 
 
2410
            previous_ie = file_parents.values()[0]
 
 
2411
            versionedfile = self.repository.weave_store.get_weave(file_id, 
 
 
2412
                self.repository.get_transaction())
 
 
2413
            versionedfile.clone_text(self._new_revision_id, 
 
 
2414
                previous_ie.revision, file_parents.keys())
 
 
2415
            return text_sha1, text_size
 
 
2417
            new_lines = get_content_byte_lines()
 
 
2418
            # TODO: Rather than invoking sha_strings here, _add_text_to_weave
 
 
2419
            # should return the SHA1 and size
 
 
2420
            self._add_text_to_weave(file_id, new_lines, file_parents.keys())
 
 
2421
            return osutils.sha_strings(new_lines), \
 
 
2422
                sum(map(len, new_lines))
 
 
2424
    def modified_link(self, file_id, file_parents, link_target):
 
 
2425
        """Record the presence of a symbolic link.
 
 
2427
        :param file_id: The file_id of the link to record.
 
 
2428
        :param file_parents: The per-file parent revision ids.
 
 
2429
        :param link_target: Target location of this link.
 
 
2431
        self._add_text_to_weave(file_id, [], file_parents.keys())
 
 
2433
    def _add_text_to_weave(self, file_id, new_lines, parents):
 
 
2434
        versionedfile = self.repository.weave_store.get_weave_or_empty(
 
 
2435
            file_id, self.repository.get_transaction())
 
 
2436
        versionedfile.add_lines(self._new_revision_id, parents, new_lines)
 
 
2437
        versionedfile.clear_cache()
 
 
2440
class _CommitBuilder(CommitBuilder):
 
 
2441
    """Temporary class so old CommitBuilders are detected properly
 
 
2443
    Note: CommitBuilder works whether or not root entry is recorded.
 
 
2446
    record_root_entry = True
 
 
2449
class RootCommitBuilder(CommitBuilder):
 
 
2450
    """This commitbuilder actually records the root id"""
 
 
2452
    record_root_entry = True
 
 
2454
    def record_entry_contents(self, ie, parent_invs, path, tree):
 
 
2455
        """Record the content of ie from tree into the commit if needed.
 
 
2457
        Side effect: sets ie.revision when unchanged
 
 
2459
        :param ie: An inventory entry present in the commit.
 
 
2460
        :param parent_invs: The inventories of the parent revisions of the
 
 
2462
        :param path: The path the entry is at in the tree.
 
 
2463
        :param tree: The tree which contains this entry and should be used to 
 
 
2466
        assert self.new_inventory.root is not None or ie.parent_id is None
 
 
2467
        self.new_inventory.add(ie)
 
 
2469
        # ie.revision is always None if the InventoryEntry is considered
 
 
2470
        # for committing. ie.snapshot will record the correct revision 
 
 
2471
        # which may be the sole parent if it is untouched.
 
 
2472
        if ie.revision is not None:
 
 
2475
        previous_entries = ie.find_previous_heads(
 
 
2477
            self.repository.weave_store,
 
 
2478
            self.repository.get_transaction())
 
 
2479
        # we are creating a new revision for ie in the history store
 
 
2481
        ie.snapshot(self._new_revision_id, path, previous_entries, tree, self)
 
 
2493
def _unescaper(match, _map=_unescape_map):
 
 
2494
    return _map[match.group(1)]
 
 
2500
def _unescape_xml(data):
 
 
2501
    """Unescape predefined XML entities in a string of data."""
 
 
2503
    if _unescape_re is None:
 
 
2504
        _unescape_re = re.compile('\&([^;]*);')
 
 
2505
    return _unescape_re.sub(_unescaper, data)