~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/repository.py

  • Committer: Aaron Bentley
  • Date: 2006-08-08 06:47:16 UTC
  • mto: (1910.2.43 format-bumps)
  • mto: This revision was merged to the branch mainline in revision 1922.
  • Revision ID: aaron.bentley@utoronto.ca-20060808064716-75bc465292c2708f
Ensure root entry always has a revision

Show diffs side-by-side

added added

removed removed

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