~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/repository.py

  • Committer: Martin Pool
  • Date: 2006-03-10 06:29:53 UTC
  • mfrom: (1608 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1611.
  • Revision ID: mbp@sourcefrog.net-20060310062953-bc1c7ade75c89a7a
[merge] bzr.dev; pycurl not updated for readv yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
from copy import deepcopy
18
18
from cStringIO import StringIO
19
19
from unittest import TestSuite
20
 
import xml.sax.saxutils
21
20
 
22
21
import bzrlib.bzrdir as bzrdir
23
22
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
23
import bzrlib.errors as errors
24
24
from bzrlib.errors import InvalidRevisionId
 
25
import bzrlib.gpg as gpg
 
26
from bzrlib.graph import Graph
 
27
from bzrlib.inter import InterObject
 
28
from bzrlib.knit import KnitVersionedFile
25
29
from bzrlib.lockable_files import LockableFiles, TransportLock
26
30
from bzrlib.lockdir import LockDir
27
31
from bzrlib.osutils import safe_unicode
28
32
from bzrlib.revision import NULL_REVISION
29
 
import bzrlib.errors as errors
30
 
import bzrlib.gpg as gpg
31
 
from bzrlib.store import copy_all
32
 
from bzrlib.store.weave import WeaveStore
 
33
from bzrlib.store.versioned import VersionedFileStore, WeaveStore
33
34
from bzrlib.store.text import TextStore
34
35
from bzrlib.symbol_versioning import *
35
36
from bzrlib.trace import mutter
36
37
from bzrlib.tree import RevisionTree
 
38
from bzrlib.tsort import topo_sort
37
39
from bzrlib.testament import Testament
38
40
from bzrlib.tree import EmptyTree
39
41
import bzrlib.ui
 
42
from bzrlib.weave import WeaveFile
40
43
import bzrlib.xml5
41
44
 
42
45
 
63
66
        """
64
67
        inv_text = bzrlib.xml5.serializer_v5.write_inventory_to_string(inv)
65
68
        inv_sha1 = bzrlib.osutils.sha_string(inv_text)
66
 
        self.control_weaves.add_text('inventory', revid,
67
 
                   bzrlib.osutils.split_lines(inv_text), parents,
68
 
                   self.get_transaction())
 
69
        inv_vf = self.control_weaves.get_weave('inventory',
 
70
                                               self.get_transaction())
 
71
        inv_vf.add_lines(revid, parents, bzrlib.osutils.split_lines(inv_text))
69
72
        return inv_sha1
70
73
 
71
74
    @needs_write_lock
93
96
            else:
94
97
                # yes, this is not suitable for adding with ghosts.
95
98
                self.add_inventory(rev_id, inv, rev.parent_ids)
96
 
            
97
 
        rev_tmp = StringIO()
98
 
        bzrlib.xml5.serializer_v5.write_revision(rev, rev_tmp)
99
 
        rev_tmp.seek(0)
100
 
        self.revision_store.add(rev_tmp, rev_id)
101
 
        mutter('added revision_id {%s}', rev_id)
 
99
        self._revision_store.add_revision(rev, self.get_transaction())   
102
100
 
103
101
    @needs_read_lock
104
102
    def _all_possible_ids(self):
105
103
        """Return all the possible revisions that we could find."""
106
 
        return self.get_inventory_weave().names()
 
104
        return self.get_inventory_weave().versions()
107
105
 
108
106
    @needs_read_lock
109
107
    def all_revision_ids(self):
113
111
        present: for weaves ghosts may lead to a lack of correctness until
114
112
        the reweave updates the parents list.
115
113
        """
 
114
        if self._revision_store.text_store.listable():
 
115
            return self._revision_store.all_revision_ids(self.get_transaction())
116
116
        result = self._all_possible_ids()
117
117
        return self._eliminate_revisions_not_present(result)
118
118
 
133
133
        """Construct the current default format repository in a_bzrdir."""
134
134
        return RepositoryFormat.get_default_format().initialize(a_bzrdir)
135
135
 
136
 
    def __init__(self, _format, a_bzrdir, control_files, revision_store):
 
136
    def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
137
137
        """instantiate a Repository.
138
138
 
139
139
        :param _format: The format of the repository on disk.
148
148
        # the following are part of the public API for Repository:
149
149
        self.bzrdir = a_bzrdir
150
150
        self.control_files = control_files
151
 
        self.revision_store = revision_store
 
151
        self._revision_store = _revision_store
 
152
        self.text_store = text_store
 
153
        # backwards compatability
 
154
        self.weave_store = text_store
 
155
        # not right yet - should be more semantically clear ? 
 
156
        # 
 
157
        self.control_store = control_store
 
158
        self.control_weaves = control_store
152
159
 
153
160
    def lock_write(self):
154
161
        self.control_files.lock_write()
219
226
        self.copy_content_into(result, revision_id, basis)
220
227
        return result
221
228
 
 
229
    @needs_read_lock
222
230
    def has_revision(self, revision_id):
223
 
        """True if this branch has a copy of the revision.
224
 
 
225
 
        This does not necessarily imply the revision is merge
226
 
        or on the mainline."""
227
 
        return (revision_id is None
228
 
                or self.revision_store.has_id(revision_id))
229
 
 
230
 
    @needs_read_lock
231
 
    def get_revision_xml_file(self, revision_id):
232
 
        """Return XML file object for revision object."""
233
 
        if not revision_id or not isinstance(revision_id, basestring):
234
 
            raise InvalidRevisionId(revision_id=revision_id, branch=self)
235
 
        try:
236
 
            return self.revision_store.get(revision_id)
237
 
        except (IndexError, KeyError):
238
 
            raise bzrlib.errors.NoSuchRevision(self, revision_id)
239
 
 
240
 
    @needs_read_lock
241
 
    def get_revision_xml(self, revision_id):
242
 
        return self.get_revision_xml_file(revision_id).read()
 
231
        """True if this repository has a copy of the revision."""
 
232
        return self._revision_store.has_revision_id(revision_id,
 
233
                                                    self.get_transaction())
243
234
 
244
235
    @needs_read_lock
245
236
    def get_revision_reconcile(self, revision_id):
250
241
        be used by reconcile, or reconcile-alike commands that are correcting
251
242
        or testing the revision graph.
252
243
        """
253
 
        xml_file = self.get_revision_xml_file(revision_id)
 
244
        if not revision_id or not isinstance(revision_id, basestring):
 
245
            raise InvalidRevisionId(revision_id=revision_id, branch=self)
 
246
        return self._revision_store.get_revision(revision_id,
 
247
                                                 self.get_transaction())
254
248
 
255
 
        try:
256
 
            r = bzrlib.xml5.serializer_v5.read_revision(xml_file)
257
 
        except SyntaxError, e:
258
 
            raise bzrlib.errors.BzrError('failed to unpack revision_xml',
259
 
                                         [revision_id,
260
 
                                          str(e)])
261
 
            
262
 
        assert r.revision_id == revision_id
263
 
        return r
 
249
    @needs_read_lock
 
250
    def get_revision_xml(self, revision_id):
 
251
        rev = self.get_revision(revision_id) 
 
252
        rev_tmp = StringIO()
 
253
        # the current serializer..
 
254
        self._revision_store._serializer.write_revision(rev, rev_tmp)
 
255
        rev_tmp.seek(0)
 
256
        return rev_tmp.getvalue()
264
257
 
265
258
    @needs_read_lock
266
259
    def get_revision(self, revision_id):
284
277
        consistency and is only applicable to inventory-weave-for-ancestry
285
278
        using repository formats & fetchers.
286
279
        """
287
 
        weave_parents = inventory.parent_names(revision.revision_id)
288
 
        weave_names = inventory.names()
 
280
        weave_parents = inventory.get_parents(revision.revision_id)
 
281
        weave_names = inventory.versions()
289
282
        for parent_id in revision.parent_ids:
290
283
            if parent_id in weave_names:
291
284
                # this parent must not be a ghost.
293
286
                    # but it is a ghost
294
287
                    raise errors.CorruptRepository(self)
295
288
 
296
 
    @needs_read_lock
297
 
    def get_revision_sha1(self, revision_id):
298
 
        """Hash the stored value of a revision, and return it."""
299
 
        # In the future, revision entries will be signed. At that
300
 
        # point, it is probably best *not* to include the signature
301
 
        # in the revision hash. Because that lets you re-sign
302
 
        # the revision, (add signatures/remove signatures) and still
303
 
        # have all hash pointers stay consistent.
304
 
        # But for now, just hash the contents.
305
 
        return bzrlib.osutils.sha_file(self.get_revision_xml_file(revision_id))
306
 
 
307
289
    @needs_write_lock
308
290
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
309
 
        self.revision_store.add(StringIO(gpg_strategy.sign(plaintext)), 
310
 
                                revision_id, "sig")
 
291
        signature = gpg_strategy.sign(plaintext)
 
292
        self._revision_store.add_revision_signature_text(revision_id,
 
293
                                                         signature,
 
294
                                                         self.get_transaction())
311
295
 
312
296
    def fileid_involved_between_revs(self, from_revid, to_revid):
313
297
        """Find file_id(s) which are involved in the changes between revisions.
315
299
        This determines the set of revisions which are involved, and then
316
300
        finds all file ids affected by those revisions.
317
301
        """
318
 
        # TODO: jam 20060119 This code assumes that w.inclusions will
319
 
        #       always be correct. But because of the presence of ghosts
320
 
        #       it is possible to be wrong.
321
 
        #       One specific example from Robert Collins:
322
 
        #       Two branches, with revisions ABC, and AD
323
 
        #       C is a ghost merge of D.
324
 
        #       Inclusions doesn't recognize D as an ancestor.
325
 
        #       If D is ever merged in the future, the weave
326
 
        #       won't be fixed, because AD never saw revision C
327
 
        #       to cause a conflict which would force a reweave.
328
302
        w = self.get_inventory_weave()
329
 
        from_set = set(w.inclusions([w.lookup(from_revid)]))
330
 
        to_set = set(w.inclusions([w.lookup(to_revid)]))
331
 
        included = to_set.difference(from_set)
332
 
        changed = map(w.idx_to_name, included)
 
303
        from_set = set(w.get_ancestry(from_revid))
 
304
        to_set = set(w.get_ancestry(to_revid))
 
305
        changed = to_set.difference(from_set)
333
306
        return self._fileid_involved_by_set(changed)
334
307
 
335
308
    def fileid_involved(self, last_revid=None):
339
312
        """
340
313
        w = self.get_inventory_weave()
341
314
        if not last_revid:
342
 
            changed = set(w._names)
 
315
            changed = set(w.versions())
343
316
        else:
344
 
            included = w.inclusions([w.lookup(last_revid)])
345
 
            changed = map(w.idx_to_name, included)
 
317
            changed = set(w.get_ancestry(last_revid))
346
318
        return self._fileid_involved_by_set(changed)
347
319
 
348
320
    def fileid_involved_by_set(self, changes):
377
349
 
378
350
        w = self.get_inventory_weave()
379
351
        file_ids = set()
380
 
        for line in w._weave:
381
 
 
382
 
            # it is ugly, but it is due to the weave structure
383
 
            if not isinstance(line, basestring): continue
384
 
 
 
352
 
 
353
        # this code needs to read every line in every inventory for the
 
354
        # inventories [changes]. Seeing a line twice is ok. Seeing a line
 
355
        # not pesent in one of those inventories is unnecessary and not 
 
356
        # harmful because we are filtering by the revision id marker in the
 
357
        # inventory lines to only select file ids altered in one of those  
 
358
        # revisions. We dont need to see all lines in the inventory because
 
359
        # only those added in an inventory in rev X can contain a revision=X
 
360
        # line.
 
361
        for line in w.iter_lines_added_or_present_in_versions(changes):
385
362
            start = line.find('file_id="')+9
386
363
            if start < 9: continue
387
364
            end = line.find('"', start)
388
365
            assert end>= 0
389
 
            file_id = xml.sax.saxutils.unescape(line[start:end])
 
366
            file_id = _unescape_xml(line[start:end])
390
367
 
391
368
            # check if file_id is already present
392
369
            if file_id in file_ids: continue
395
372
            if start < 10: continue
396
373
            end = line.find('"', start)
397
374
            assert end>= 0
398
 
            revision_id = xml.sax.saxutils.unescape(line[start:end])
399
 
 
 
375
            revision_id = _unescape_xml(line[start:end])
400
376
            if revision_id in changes:
401
377
                file_ids.add(file_id)
402
378
        return file_ids
418
394
        try:
419
395
            assert isinstance(revision_id, basestring), type(revision_id)
420
396
            iw = self.get_inventory_weave()
421
 
            return iw.get_text(iw.lookup(revision_id))
 
397
            return iw.get_text(revision_id)
422
398
        except IndexError:
423
399
            raise bzrlib.errors.HistoryMissing(self, 'inventory', revision_id)
424
400
 
435
411
        :return: a dictionary of revision_id->revision_parents_list.
436
412
        """
437
413
        weave = self.get_inventory_weave()
438
 
        all_revisions = self._eliminate_revisions_not_present(weave.names())
439
 
        entire_graph = dict([(node, weave.parent_names(node)) for 
 
414
        all_revisions = self._eliminate_revisions_not_present(weave.versions())
 
415
        entire_graph = dict([(node, weave.get_parents(node)) for 
440
416
                             node in all_revisions])
441
417
        if revision_id is None:
442
418
            return entire_graph
455
431
            return result
456
432
 
457
433
    @needs_read_lock
 
434
    def get_revision_graph_with_ghosts(self, revision_ids=None):
 
435
        """Return a graph of the revisions with ghosts marked as applicable.
 
436
 
 
437
        :param revision_ids: an iterable of revisions to graph or None for all.
 
438
        :return: a Graph object with the graph reachable from revision_ids.
 
439
        """
 
440
        result = Graph()
 
441
        if not revision_ids:
 
442
            pending = set(self.all_revision_ids())
 
443
            required = set([])
 
444
        else:
 
445
            pending = set(revision_ids)
 
446
            required = set(revision_ids)
 
447
        done = set([])
 
448
        while len(pending):
 
449
            revision_id = pending.pop()
 
450
            try:
 
451
                rev = self.get_revision(revision_id)
 
452
            except errors.NoSuchRevision:
 
453
                if revision_id in required:
 
454
                    raise
 
455
                # a ghost
 
456
                result.add_ghost(revision_id)
 
457
                continue
 
458
            for parent_id in rev.parent_ids:
 
459
                # is this queued or done ?
 
460
                if (parent_id not in pending and
 
461
                    parent_id not in done):
 
462
                    # no, queue it.
 
463
                    pending.add(parent_id)
 
464
            result.add_node(revision_id, rev.parent_ids)
 
465
            done.add(revision_id)
 
466
        return result
 
467
 
 
468
    @needs_read_lock
458
469
    def get_revision_inventory(self, revision_id):
459
470
        """Return inventory of a past revision."""
460
471
        # TODO: Unify this with get_inventory()
473
484
    @needs_read_lock
474
485
    def is_shared(self):
475
486
        """Return True if this repository is flagged as a shared repository."""
476
 
        # FIXME format 4-6 cannot be shared, this is technically faulty.
477
 
        return self.control_files._transport.has('shared-storage')
 
487
        raise NotImplementedError(self.is_shared)
478
488
 
 
489
    @needs_write_lock
 
490
    def reconcile(self):
 
491
        """Reconcile this repository."""
 
492
        from bzrlib.reconcile import RepoReconciler
 
493
        reconciler = RepoReconciler(self)
 
494
        reconciler.reconcile()
 
495
        return reconciler
 
496
    
479
497
    @needs_read_lock
480
498
    def revision_tree(self, revision_id):
481
499
        """Return Tree for a revision on this branch.
501
519
        if not self.has_revision(revision_id):
502
520
            raise errors.NoSuchRevision(self, revision_id)
503
521
        w = self.get_inventory_weave()
504
 
        return [None] + map(w.idx_to_name,
505
 
                            w.inclusions([w.lookup(revision_id)]))
 
522
        candidates = w.get_ancestry(revision_id)
 
523
        return [None] + candidates # self._eliminate_revisions_not_present(candidates)
506
524
 
507
525
    @needs_read_lock
508
526
    def print_file(self, file, revision_id):
545
563
        :param new_value: True to restore the default, False to disable making
546
564
                          working trees.
547
565
        """
548
 
        # FIXME: split out into a new class/strategy ?
549
 
        if isinstance(self._format, (RepositoryFormat4,
550
 
                                     RepositoryFormat5,
551
 
                                     RepositoryFormat6)):
552
 
            raise NotImplementedError(self.set_make_working_trees)
553
 
        if new_value:
554
 
            try:
555
 
                self.control_files._transport.delete('no-working-trees')
556
 
            except errors.NoSuchFile:
557
 
                pass
558
 
        else:
559
 
            self.control_files.put_utf8('no-working-trees', '')
 
566
        raise NotImplementedError(self.set_make_working_trees)
560
567
    
561
568
    def make_working_trees(self):
562
569
        """Returns the policy for making working trees on new branches."""
563
 
        # FIXME: split out into a new class/strategy ?
564
 
        if isinstance(self._format, (RepositoryFormat4,
565
 
                                     RepositoryFormat5,
566
 
                                     RepositoryFormat6)):
567
 
            return True
568
 
        return not self.control_files._transport.has('no-working-trees')
 
570
        raise NotImplementedError(self.make_working_trees)
569
571
 
570
572
    @needs_write_lock
571
573
    def sign_revision(self, revision_id, gpg_strategy):
572
574
        plaintext = Testament.from_revision(self, revision_id).as_short_text()
573
575
        self.store_revision_signature(gpg_strategy, plaintext, revision_id)
574
576
 
 
577
    @needs_read_lock
 
578
    def has_signature_for_revision_id(self, revision_id):
 
579
        """Query for a revision signature for revision_id in the repository."""
 
580
        return self._revision_store.has_signature(revision_id,
 
581
                                                  self.get_transaction())
 
582
 
 
583
    @needs_read_lock
 
584
    def get_signature_text(self, revision_id):
 
585
        """Return the text for a signature."""
 
586
        return self._revision_store.get_signature_text(revision_id,
 
587
                                                       self.get_transaction())
 
588
 
575
589
 
576
590
class AllInOneRepository(Repository):
577
591
    """Legacy support - the repository behaviour for all-in-one branches."""
578
592
 
579
 
    def __init__(self, _format, a_bzrdir, revision_store):
 
593
    def __init__(self, _format, a_bzrdir, _revision_store, control_store, text_store):
580
594
        # we reuse one control files instance.
581
595
        dir_mode = a_bzrdir._control_files._dir_mode
582
596
        file_mode = a_bzrdir._control_files._file_mode
614
628
        # not broken out yet because the controlweaves|inventory_store
615
629
        # and text_store | weave_store bits are still different.
616
630
        if isinstance(_format, RepositoryFormat4):
 
631
            # cannot remove these - there is still no consistent api 
 
632
            # which allows access to this old info.
617
633
            self.inventory_store = get_store('inventory-store')
618
 
            self.text_store = get_store('text-store')
619
 
        elif isinstance(_format, RepositoryFormat5):
620
 
            self.control_weaves = get_weave('')
621
 
            self.weave_store = get_weave('weaves')
622
 
        elif isinstance(_format, RepositoryFormat6):
623
 
            self.control_weaves = get_weave('')
624
 
            self.weave_store = get_weave('weaves', prefixed=True)
625
 
        else:
626
 
            raise errors.BzrError('unreachable code: unexpected repository'
627
 
                                  ' format.')
628
 
        revision_store.register_suffix('sig')
629
 
        super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, revision_store)
 
634
            text_store = get_store('text-store')
 
635
        super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, _revision_store, control_store, text_store)
 
636
 
 
637
    @needs_read_lock
 
638
    def is_shared(self):
 
639
        """AllInOne repositories cannot be shared."""
 
640
        return False
 
641
 
 
642
    @needs_write_lock
 
643
    def set_make_working_trees(self, new_value):
 
644
        """Set the policy flag for making working trees when creating branches.
 
645
 
 
646
        This only applies to branches that use this repository.
 
647
 
 
648
        The default is 'True'.
 
649
        :param new_value: True to restore the default, False to disable making
 
650
                          working trees.
 
651
        """
 
652
        raise NotImplementedError(self.set_make_working_trees)
 
653
    
 
654
    def make_working_trees(self):
 
655
        """Returns the policy for making working trees on new branches."""
 
656
        return True
630
657
 
631
658
 
632
659
class MetaDirRepository(Repository):
633
660
    """Repositories in the new meta-dir layout."""
634
661
 
635
 
    def __init__(self, _format, a_bzrdir, control_files, revision_store):
 
662
    def __init__(self, _format, a_bzrdir, control_files, _revision_store, control_store, text_store):
636
663
        super(MetaDirRepository, self).__init__(_format,
637
664
                                                a_bzrdir,
638
665
                                                control_files,
639
 
                                                revision_store)
 
666
                                                _revision_store,
 
667
                                                control_store,
 
668
                                                text_store)
640
669
 
641
670
        dir_mode = self.control_files._dir_mode
642
671
        file_mode = self.control_files._file_mode
655
684
                ws.enable_cache = True
656
685
            return ws
657
686
 
658
 
        if isinstance(self._format, RepositoryFormat7):
659
 
            self.control_weaves = get_weave('')
660
 
            self.weave_store = get_weave('weaves', prefixed=True)
661
 
        elif isinstance(self._format, RepositoryFormatKnit1):
662
 
            self.control_weaves = get_weave('')
663
 
            self.weave_store = get_weave('knits', prefixed=True)
664
 
        else:
665
 
            raise errors.BzrError('unreachable code: unexpected repository'
666
 
                                  ' format.')
667
 
 
 
687
    @needs_read_lock
 
688
    def is_shared(self):
 
689
        """Return True if this repository is flagged as a shared repository."""
 
690
        return self.control_files._transport.has('shared-storage')
 
691
 
 
692
    @needs_write_lock
 
693
    def set_make_working_trees(self, new_value):
 
694
        """Set the policy flag for making working trees when creating branches.
 
695
 
 
696
        This only applies to branches that use this repository.
 
697
 
 
698
        The default is 'True'.
 
699
        :param new_value: True to restore the default, False to disable making
 
700
                          working trees.
 
701
        """
 
702
        if new_value:
 
703
            try:
 
704
                self.control_files._transport.delete('no-working-trees')
 
705
            except errors.NoSuchFile:
 
706
                pass
 
707
        else:
 
708
            self.control_files.put_utf8('no-working-trees', '')
 
709
    
 
710
    def make_working_trees(self):
 
711
        """Returns the policy for making working trees on new branches."""
 
712
        return not self.control_files._transport.has('no-working-trees')
 
713
 
 
714
 
 
715
class KnitRepository(MetaDirRepository):
 
716
    """Knit format repository."""
 
717
 
 
718
    @needs_read_lock
 
719
    def all_revision_ids(self):
 
720
        """See Repository.all_revision_ids()."""
 
721
        return self._revision_store.all_revision_ids(self.get_transaction())
 
722
 
 
723
    def fileid_involved_between_revs(self, from_revid, to_revid):
 
724
        """Find file_id(s) which are involved in the changes between revisions.
 
725
 
 
726
        This determines the set of revisions which are involved, and then
 
727
        finds all file ids affected by those revisions.
 
728
        """
 
729
        vf = self._get_revision_vf()
 
730
        from_set = set(vf.get_ancestry(from_revid))
 
731
        to_set = set(vf.get_ancestry(to_revid))
 
732
        changed = to_set.difference(from_set)
 
733
        return self._fileid_involved_by_set(changed)
 
734
 
 
735
    def fileid_involved(self, last_revid=None):
 
736
        """Find all file_ids modified in the ancestry of last_revid.
 
737
 
 
738
        :param last_revid: If None, last_revision() will be used.
 
739
        """
 
740
        if not last_revid:
 
741
            changed = set(self.all_revision_ids())
 
742
        else:
 
743
            changed = set(self.get_ancestry(last_revid))
 
744
        if None in changed:
 
745
            changed.remove(None)
 
746
        return self._fileid_involved_by_set(changed)
 
747
 
 
748
    @needs_read_lock
 
749
    def get_ancestry(self, revision_id):
 
750
        """Return a list of revision-ids integrated by a revision.
 
751
        
 
752
        This is topologically sorted.
 
753
        """
 
754
        if revision_id is None:
 
755
            return [None]
 
756
        vf = self._get_revision_vf()
 
757
        try:
 
758
            return [None] + vf.get_ancestry(revision_id)
 
759
        except errors.RevisionNotPresent:
 
760
            raise errors.NoSuchRevision(self, revision_id)
 
761
 
 
762
    @needs_read_lock
 
763
    def get_revision(self, revision_id):
 
764
        """Return the Revision object for a named revision"""
 
765
        return self.get_revision_reconcile(revision_id)
 
766
 
 
767
    @needs_read_lock
 
768
    def get_revision_graph(self, revision_id=None):
 
769
        """Return a dictionary containing the revision graph.
 
770
        
 
771
        :return: a dictionary of revision_id->revision_parents_list.
 
772
        """
 
773
        weave = self._get_revision_vf()
 
774
        entire_graph = weave.get_graph()
 
775
        if revision_id is None:
 
776
            return weave.get_graph()
 
777
        elif revision_id not in weave:
 
778
            raise errors.NoSuchRevision(self, revision_id)
 
779
        else:
 
780
            # add what can be reached from revision_id
 
781
            result = {}
 
782
            pending = set([revision_id])
 
783
            while len(pending) > 0:
 
784
                node = pending.pop()
 
785
                result[node] = weave.get_parents(node)
 
786
                for revision_id in result[node]:
 
787
                    if revision_id not in result:
 
788
                        pending.add(revision_id)
 
789
            return result
 
790
 
 
791
    @needs_read_lock
 
792
    def get_revision_graph_with_ghosts(self, revision_ids=None):
 
793
        """Return a graph of the revisions with ghosts marked as applicable.
 
794
 
 
795
        :param revision_ids: an iterable of revisions to graph or None for all.
 
796
        :return: a Graph object with the graph reachable from revision_ids.
 
797
        """
 
798
        result = Graph()
 
799
        vf = self._get_revision_vf()
 
800
        versions = vf.versions()
 
801
        if not revision_ids:
 
802
            pending = set(self.all_revision_ids())
 
803
            required = set([])
 
804
        else:
 
805
            pending = set(revision_ids)
 
806
            required = set(revision_ids)
 
807
        done = set([])
 
808
        while len(pending):
 
809
            revision_id = pending.pop()
 
810
            if not revision_id in versions:
 
811
                if revision_id in required:
 
812
                    raise errors.NoSuchRevision(self, revision_id)
 
813
                # a ghost
 
814
                result.add_ghost(revision_id)
 
815
                continue
 
816
            parent_ids = vf.get_parents_with_ghosts(revision_id)
 
817
            for parent_id in parent_ids:
 
818
                # is this queued or done ?
 
819
                if (parent_id not in pending and
 
820
                    parent_id not in done):
 
821
                    # no, queue it.
 
822
                    pending.add(parent_id)
 
823
            result.add_node(revision_id, parent_ids)
 
824
            done.add(result)
 
825
        return result
 
826
 
 
827
    def _get_revision_vf(self):
 
828
        """:return: a versioned file containing the revisions."""
 
829
        vf = self._revision_store.get_revision_file(self.get_transaction())
 
830
        return vf
 
831
 
 
832
    @needs_write_lock
 
833
    def reconcile(self):
 
834
        """Reconcile this repository."""
 
835
        from bzrlib.reconcile import KnitReconciler
 
836
        reconciler = KnitReconciler(self)
 
837
        reconciler.reconcile()
 
838
        return reconciler
 
839
    
 
840
    def revision_parents(self, revid):
 
841
        return self._get_revision_vf().get_parents(rev_id)
668
842
 
669
843
class RepositoryFormat(object):
670
844
    """A repository format.
708
882
        except KeyError:
709
883
            raise errors.UnknownFormatError(format_string)
710
884
 
 
885
    def _get_control_store(self, repo_transport, control_files):
 
886
        """Return the control store for this repository."""
 
887
        raise NotImplementedError(self._get_control_store)
 
888
    
711
889
    @classmethod
712
890
    def get_default_format(klass):
713
891
        """Return the current default format."""
725
903
        """Return the revision store object for this a_bzrdir."""
726
904
        raise NotImplementedError(self._get_revision_store)
727
905
 
728
 
    def _get_rev_store(self,
729
 
                   transport,
730
 
                   control_files,
731
 
                   name,
732
 
                   compressed=True,
733
 
                   prefixed=False):
 
906
    def _get_text_rev_store(self,
 
907
                            transport,
 
908
                            control_files,
 
909
                            name,
 
910
                            compressed=True,
 
911
                            prefixed=False,
 
912
                            serializer=None):
734
913
        """Common logic for getting a revision store for a repository.
735
914
        
736
 
        see self._get_revision_store for the method to 
 
915
        see self._get_revision_store for the subclass-overridable method to 
737
916
        get the store for a repository.
738
917
        """
739
 
        if name:
740
 
            name = safe_unicode(name)
741
 
        else:
742
 
            name = ''
743
 
        dir_mode = control_files._dir_mode
744
 
        file_mode = control_files._file_mode
745
 
        revision_store =TextStore(transport.clone(name),
746
 
                                  prefixed=prefixed,
747
 
                                  compressed=compressed,
748
 
                                  dir_mode=dir_mode,
749
 
                                  file_mode=file_mode)
750
 
        revision_store.register_suffix('sig')
751
 
        return revision_store
 
918
        from bzrlib.store.revision.text import TextRevisionStore
 
919
        dir_mode = control_files._dir_mode
 
920
        file_mode = control_files._file_mode
 
921
        text_store =TextStore(transport.clone(name),
 
922
                              prefixed=prefixed,
 
923
                              compressed=compressed,
 
924
                              dir_mode=dir_mode,
 
925
                              file_mode=file_mode)
 
926
        _revision_store = TextRevisionStore(text_store, serializer)
 
927
        return _revision_store
 
928
 
 
929
    def _get_versioned_file_store(self,
 
930
                                  name,
 
931
                                  transport,
 
932
                                  control_files,
 
933
                                  prefixed=True,
 
934
                                  versionedfile_class=WeaveFile):
 
935
        weave_transport = control_files._transport.clone(name)
 
936
        dir_mode = control_files._dir_mode
 
937
        file_mode = control_files._file_mode
 
938
        return VersionedFileStore(weave_transport, prefixed=prefixed,
 
939
                                dir_mode=dir_mode,
 
940
                                file_mode=file_mode,
 
941
                                versionedfile_class=versionedfile_class)
752
942
 
753
943
    def initialize(self, a_bzrdir, shared=False):
754
944
        """Initialize a repository of this format in a_bzrdir.
834
1024
            control_files.unlock()
835
1025
        return self.open(a_bzrdir, _found=True)
836
1026
 
 
1027
    def _get_control_store(self, repo_transport, control_files):
 
1028
        """Return the control store for this repository."""
 
1029
        return self._get_versioned_file_store('',
 
1030
                                              repo_transport,
 
1031
                                              control_files,
 
1032
                                              prefixed=False)
 
1033
 
 
1034
    def _get_text_store(self, transport, control_files):
 
1035
        """Get a store for file texts for this format."""
 
1036
        raise NotImplementedError(self._get_text_store)
 
1037
 
837
1038
    def open(self, a_bzrdir, _found=False):
838
1039
        """See RepositoryFormat.open()."""
839
1040
        if not _found:
842
1043
 
843
1044
        repo_transport = a_bzrdir.get_repository_transport(None)
844
1045
        control_files = a_bzrdir._control_files
845
 
        revision_store = self._get_revision_store(repo_transport, control_files)
 
1046
        text_store = self._get_text_store(repo_transport, control_files)
 
1047
        control_store = self._get_control_store(repo_transport, control_files)
 
1048
        _revision_store = self._get_revision_store(repo_transport, control_files)
846
1049
        return AllInOneRepository(_format=self,
847
1050
                                  a_bzrdir=a_bzrdir,
848
 
                                  revision_store=revision_store)
 
1051
                                  _revision_store=_revision_store,
 
1052
                                  control_store=control_store,
 
1053
                                  text_store=text_store)
849
1054
 
850
1055
 
851
1056
class RepositoryFormat4(PreSplitOutRepositoryFormat):
877
1082
        """
878
1083
        return False
879
1084
 
 
1085
    def _get_control_store(self, repo_transport, control_files):
 
1086
        """Format 4 repositories have no formal control store at this point.
 
1087
        
 
1088
        This will cause any control-file-needing apis to fail - this is desired.
 
1089
        """
 
1090
        return None
 
1091
    
880
1092
    def _get_revision_store(self, repo_transport, control_files):
881
1093
        """See RepositoryFormat._get_revision_store()."""
882
 
        return self._get_rev_store(repo_transport,
883
 
                                   control_files,
884
 
                                   'revision-store')
 
1094
        from bzrlib.xml4 import serializer_v4
 
1095
        return self._get_text_rev_store(repo_transport,
 
1096
                                        control_files,
 
1097
                                        'revision-store',
 
1098
                                        serializer=serializer_v4)
 
1099
 
 
1100
    def _get_text_store(self, transport, control_files):
 
1101
        """See RepositoryFormat._get_text_store()."""
885
1102
 
886
1103
 
887
1104
class RepositoryFormat5(PreSplitOutRepositoryFormat):
900
1117
    def _get_revision_store(self, repo_transport, control_files):
901
1118
        """See RepositoryFormat._get_revision_store()."""
902
1119
        """Return the revision store object for this a_bzrdir."""
903
 
        return self._get_rev_store(repo_transport,
904
 
                                   control_files,
905
 
                                   'revision-store',
906
 
                                   compressed=False)
 
1120
        return self._get_text_rev_store(repo_transport,
 
1121
                                        control_files,
 
1122
                                        'revision-store',
 
1123
                                        compressed=False)
 
1124
 
 
1125
    def _get_text_store(self, transport, control_files):
 
1126
        """See RepositoryFormat._get_text_store()."""
 
1127
        return self._get_versioned_file_store('weaves', transport, control_files, prefixed=False)
907
1128
 
908
1129
 
909
1130
class RepositoryFormat6(PreSplitOutRepositoryFormat):
921
1142
 
922
1143
    def _get_revision_store(self, repo_transport, control_files):
923
1144
        """See RepositoryFormat._get_revision_store()."""
924
 
        return self._get_rev_store(repo_transport,
925
 
                                   control_files,
926
 
                                   'revision-store',
927
 
                                   compressed=False,
928
 
                                   prefixed=True)
 
1145
        return self._get_text_rev_store(repo_transport,
 
1146
                                        control_files,
 
1147
                                        'revision-store',
 
1148
                                        compressed=False,
 
1149
                                        prefixed=True)
 
1150
 
 
1151
    def _get_text_store(self, transport, control_files):
 
1152
        """See RepositoryFormat._get_text_store()."""
 
1153
        return self._get_versioned_file_store('weaves', transport, control_files)
929
1154
 
930
1155
 
931
1156
class MetaDirRepositoryFormat(RepositoryFormat):
944
1169
        control_files.create_lock()
945
1170
        return control_files
946
1171
 
947
 
    def _get_revision_store(self, repo_transport, control_files):
948
 
        """See RepositoryFormat._get_revision_store()."""
949
 
        return self._get_rev_store(repo_transport,
950
 
                                   control_files,
951
 
                                   'revision-store',
952
 
                                   compressed=False,
953
 
                                   prefixed=True,
954
 
                                   )
955
 
 
956
 
    def open(self, a_bzrdir, _found=False, _override_transport=None):
957
 
        """See RepositoryFormat.open().
958
 
        
959
 
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
960
 
                                    repository at a slightly different url
961
 
                                    than normal. I.e. during 'upgrade'.
962
 
        """
963
 
        if not _found:
964
 
            format = RepositoryFormat.find_format(a_bzrdir)
965
 
            assert format.__class__ ==  self.__class__
966
 
        if _override_transport is not None:
967
 
            repo_transport = _override_transport
968
 
        else:
969
 
            repo_transport = a_bzrdir.get_repository_transport(None)
970
 
        control_files = LockableFiles(repo_transport, 'lock', LockDir)
971
 
        revision_store = self._get_revision_store(repo_transport, control_files)
972
 
        return MetaDirRepository(_format=self,
973
 
                                 a_bzrdir=a_bzrdir,
974
 
                                 control_files=control_files,
975
 
                                 revision_store=revision_store)
976
 
 
977
1172
    def _upload_blank_content(self, a_bzrdir, dirs, files, utf8_files, shared):
978
1173
        """Upload the initial blank content."""
979
1174
        control_files = self._create_control_files(a_bzrdir)
1003
1198
     - an optional 'no-working-trees' flag
1004
1199
    """
1005
1200
 
 
1201
    def _get_control_store(self, repo_transport, control_files):
 
1202
        """Return the control store for this repository."""
 
1203
        return self._get_versioned_file_store('',
 
1204
                                              repo_transport,
 
1205
                                              control_files,
 
1206
                                              prefixed=False)
 
1207
 
1006
1208
    def get_format_string(self):
1007
1209
        """See RepositoryFormat.get_format_string()."""
1008
1210
        return "Bazaar-NG Repository format 7"
1009
1211
 
 
1212
    def _get_revision_store(self, repo_transport, control_files):
 
1213
        """See RepositoryFormat._get_revision_store()."""
 
1214
        return self._get_text_rev_store(repo_transport,
 
1215
                                        control_files,
 
1216
                                        'revision-store',
 
1217
                                        compressed=False,
 
1218
                                        prefixed=True,
 
1219
                                        )
 
1220
 
 
1221
    def _get_text_store(self, transport, control_files):
 
1222
        """See RepositoryFormat._get_text_store()."""
 
1223
        return self._get_versioned_file_store('weaves',
 
1224
                                              transport,
 
1225
                                              control_files)
 
1226
 
1010
1227
    def initialize(self, a_bzrdir, shared=False):
1011
1228
        """Create a weave repository.
1012
1229
 
1030
1247
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
1031
1248
        return self.open(a_bzrdir=a_bzrdir, _found=True)
1032
1249
 
 
1250
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
1251
        """See RepositoryFormat.open().
 
1252
        
 
1253
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
1254
                                    repository at a slightly different url
 
1255
                                    than normal. I.e. during 'upgrade'.
 
1256
        """
 
1257
        if not _found:
 
1258
            format = RepositoryFormat.find_format(a_bzrdir)
 
1259
            assert format.__class__ ==  self.__class__
 
1260
        if _override_transport is not None:
 
1261
            repo_transport = _override_transport
 
1262
        else:
 
1263
            repo_transport = a_bzrdir.get_repository_transport(None)
 
1264
        control_files = LockableFiles(repo_transport, 'lock', LockDir)
 
1265
        text_store = self._get_text_store(repo_transport, control_files)
 
1266
        control_store = self._get_control_store(repo_transport, control_files)
 
1267
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
1268
        return MetaDirRepository(_format=self,
 
1269
                                 a_bzrdir=a_bzrdir,
 
1270
                                 control_files=control_files,
 
1271
                                 _revision_store=_revision_store,
 
1272
                                 control_store=control_store,
 
1273
                                 text_store=text_store)
 
1274
 
1033
1275
 
1034
1276
class RepositoryFormatKnit1(MetaDirRepositoryFormat):
1035
1277
    """Bzr repository knit format 1.
1045
1287
     - a LockDir lock
1046
1288
    """
1047
1289
 
 
1290
    def _get_control_store(self, repo_transport, control_files):
 
1291
        """Return the control store for this repository."""
 
1292
        return self._get_versioned_file_store('',
 
1293
                                              repo_transport,
 
1294
                                              control_files,
 
1295
                                              prefixed=False,
 
1296
                                              versionedfile_class=KnitVersionedFile)
 
1297
 
1048
1298
    def get_format_string(self):
1049
1299
        """See RepositoryFormat.get_format_string()."""
1050
1300
        return "Bazaar-NG Knit Repository Format 1"
1051
1301
 
 
1302
    def _get_revision_store(self, repo_transport, control_files):
 
1303
        """See RepositoryFormat._get_revision_store()."""
 
1304
        from bzrlib.store.revision.knit import KnitRevisionStore
 
1305
        versioned_file_store = VersionedFileStore(
 
1306
            repo_transport,
 
1307
            file_mode = control_files._file_mode,
 
1308
            prefixed=False,
 
1309
            precious=True,
 
1310
            versionedfile_class=KnitVersionedFile)
 
1311
        return KnitRevisionStore(versioned_file_store)
 
1312
 
 
1313
    def _get_text_store(self, transport, control_files):
 
1314
        """See RepositoryFormat._get_text_store()."""
 
1315
        return self._get_versioned_file_store('knits',
 
1316
                                              transport,
 
1317
                                              control_files,
 
1318
                                              versionedfile_class=KnitVersionedFile)
 
1319
 
1052
1320
    def initialize(self, a_bzrdir, shared=False):
1053
1321
        """Create a knit format 1 repository.
1054
1322
 
1066
1334
        empty_weave = sio.getvalue()
1067
1335
 
1068
1336
        mutter('creating repository in %s.', a_bzrdir.transport.base)
1069
 
        dirs = ['revision-store', 'knits']
1070
 
        files = [('inventory.weave', StringIO(empty_weave)), 
 
1337
        dirs = ['revision-store', 'knits', 'control']
 
1338
        files = [('control/inventory.weave', StringIO(empty_weave)), 
1071
1339
                 ]
1072
1340
        utf8_files = [('format', self.get_format_string())]
1073
1341
        
1074
1342
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
 
1343
        repo_transport = a_bzrdir.get_repository_transport(None)
 
1344
        control_files = LockableFiles(repo_transport, 'lock', LockDir)
 
1345
        control_store = self._get_control_store(repo_transport, control_files)
 
1346
        transaction = bzrlib.transactions.WriteTransaction()
 
1347
        # trigger a write of the inventory store.
 
1348
        control_store.get_weave_or_empty('inventory', transaction)
 
1349
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
1350
        _revision_store.has_revision_id('A', transaction)
 
1351
        _revision_store.get_signature_file(transaction)
1075
1352
        return self.open(a_bzrdir=a_bzrdir, _found=True)
1076
1353
 
 
1354
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
1355
        """See RepositoryFormat.open().
 
1356
        
 
1357
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
1358
                                    repository at a slightly different url
 
1359
                                    than normal. I.e. during 'upgrade'.
 
1360
        """
 
1361
        if not _found:
 
1362
            format = RepositoryFormat.find_format(a_bzrdir)
 
1363
            assert format.__class__ ==  self.__class__
 
1364
        if _override_transport is not None:
 
1365
            repo_transport = _override_transport
 
1366
        else:
 
1367
            repo_transport = a_bzrdir.get_repository_transport(None)
 
1368
        control_files = LockableFiles(repo_transport, 'lock', LockDir)
 
1369
        text_store = self._get_text_store(repo_transport, control_files)
 
1370
        control_store = self._get_control_store(repo_transport, control_files)
 
1371
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
1372
        return KnitRepository(_format=self,
 
1373
                              a_bzrdir=a_bzrdir,
 
1374
                              control_files=control_files,
 
1375
                              _revision_store=_revision_store,
 
1376
                              control_store=control_store,
 
1377
                              text_store=text_store)
 
1378
 
1077
1379
 
1078
1380
# formats which have no format string are not discoverable
1079
1381
# and not independently creatable, so are not registered.
1086
1388
                   RepositoryFormat6()]
1087
1389
 
1088
1390
 
1089
 
class InterRepository(object):
 
1391
class InterRepository(InterObject):
1090
1392
    """This class represents operations taking place between two repositories.
1091
1393
 
1092
1394
    Its instances have methods like copy_content and fetch, and contain
1097
1399
    operations with another repository - they will always forward to
1098
1400
    InterRepository.get(other).method_name(parameters).
1099
1401
    """
1100
 
    # XXX: FIXME: FUTURE: robertc
1101
 
    # testing of these probably requires a factory in optimiser type, and 
1102
 
    # then a test adapter to test each type thoroughly.
1103
 
    #
1104
1402
 
1105
1403
    _optimisers = set()
1106
1404
    """The available optimised InterRepository types."""
1107
1405
 
1108
 
    def __init__(self, source, target):
1109
 
        """Construct a default InterRepository instance. Please use 'get'.
1110
 
        
1111
 
        Only subclasses of InterRepository should call 
1112
 
        InterRepository.__init__ - clients should call InterRepository.get
1113
 
        instead which will create an optimised InterRepository if possible.
1114
 
        """
1115
 
        self.source = source
1116
 
        self.target = target
1117
 
 
1118
1406
    @needs_write_lock
1119
1407
    def copy_content(self, revision_id=None, basis=None):
1120
1408
        """Make a complete copy of the content in self into destination.
1133
1421
        # grab the basis available data
1134
1422
        if basis is not None:
1135
1423
            self.target.fetch(basis, revision_id=revision_id)
1136
 
        # but dont both fetching if we have the needed data now.
 
1424
        # but dont bother fetching if we have the needed data now.
1137
1425
        if (revision_id not in (None, NULL_REVISION) and 
1138
1426
            self.target.has_revision(revision_id)):
1139
1427
            return
1164
1452
        Returns the copied revision count and the failed revisions in a tuple:
1165
1453
        (copied, failures).
1166
1454
        """
1167
 
        from bzrlib.fetch import RepoFetcher
 
1455
        from bzrlib.fetch import GenericRepoFetcher
1168
1456
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
1169
1457
               self.source, self.source._format, self.target, self.target._format)
1170
 
        f = RepoFetcher(to_repository=self.target,
1171
 
                        from_repository=self.source,
1172
 
                        last_revision=revision_id,
1173
 
                        pb=pb)
 
1458
        f = GenericRepoFetcher(to_repository=self.target,
 
1459
                               from_repository=self.source,
 
1460
                               last_revision=revision_id,
 
1461
                               pb=pb)
1174
1462
        return f.count_copied, f.failed_revisions
1175
1463
 
1176
 
    @classmethod
1177
 
    def get(klass, repository_source, repository_target):
1178
 
        """Retrieve a InterRepository worker object for these repositories.
1179
 
 
1180
 
        :param repository_source: the repository to be the 'source' member of
1181
 
                                  the InterRepository instance.
1182
 
        :param repository_target: the repository to be the 'target' member of
1183
 
                                the InterRepository instance.
1184
 
        If an optimised InterRepository worker exists it will be used otherwise
1185
 
        a default InterRepository instance will be created.
1186
 
        """
1187
 
        for provider in klass._optimisers:
1188
 
            if provider.is_compatible(repository_source, repository_target):
1189
 
                return provider(repository_source, repository_target)
1190
 
        return InterRepository(repository_source, repository_target)
1191
 
 
1192
1464
    def lock_read(self):
1193
1465
        """Take out a logical read lock.
1194
1466
 
1227
1499
        # that we've decided we need.
1228
1500
        return [rev_id for rev_id in source_ids if rev_id in result_set]
1229
1501
 
1230
 
    @classmethod
1231
 
    def register_optimiser(klass, optimiser):
1232
 
        """Register an InterRepository optimiser."""
1233
 
        klass._optimisers.add(optimiser)
1234
 
 
1235
1502
    def unlock(self):
1236
1503
        """Release the locks on source and target."""
1237
1504
        try:
1239
1506
        finally:
1240
1507
            self.source.unlock()
1241
1508
 
1242
 
    @classmethod
1243
 
    def unregister_optimiser(klass, optimiser):
1244
 
        """Unregister an InterRepository optimiser."""
1245
 
        klass._optimisers.remove(optimiser)
1246
 
 
1247
1509
 
1248
1510
class InterWeaveRepo(InterRepository):
1249
1511
    """Optimised code paths between Weave based repositories."""
1289
1551
                pass
1290
1552
            # FIXME do not peek!
1291
1553
            if self.source.control_files._transport.listable():
1292
 
                pb = bzrlib.ui.ui_factory.progress_bar()
1293
 
                copy_all(self.source.weave_store,
1294
 
                    self.target.weave_store, pb=pb)
1295
 
                pb.update('copying inventory', 0, 1)
1296
 
                self.target.control_weaves.copy_multi(
1297
 
                    self.source.control_weaves, ['inventory'])
1298
 
                copy_all(self.source.revision_store,
1299
 
                    self.target.revision_store, pb=pb)
 
1554
                pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1555
                try:
 
1556
                    self.target.weave_store.copy_all_ids(
 
1557
                        self.source.weave_store,
 
1558
                        pb=pb,
 
1559
                        from_transaction=self.source.get_transaction(),
 
1560
                        to_transaction=self.target.get_transaction())
 
1561
                    pb.update('copying inventory', 0, 1)
 
1562
                    self.target.control_weaves.copy_multi(
 
1563
                        self.source.control_weaves, ['inventory'],
 
1564
                        from_transaction=self.source.get_transaction(),
 
1565
                        to_transaction=self.target.get_transaction())
 
1566
                    self.target._revision_store.text_store.copy_all_ids(
 
1567
                        self.source._revision_store.text_store,
 
1568
                        pb=pb)
 
1569
                finally:
 
1570
                    pb.finished()
1300
1571
            else:
1301
1572
                self.target.fetch(self.source, revision_id=revision_id)
1302
1573
 
1303
1574
    @needs_write_lock
1304
1575
    def fetch(self, revision_id=None, pb=None):
1305
1576
        """See InterRepository.fetch()."""
1306
 
        from bzrlib.fetch import RepoFetcher
 
1577
        from bzrlib.fetch import GenericRepoFetcher
1307
1578
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
1308
1579
               self.source, self.source._format, self.target, self.target._format)
1309
 
        f = RepoFetcher(to_repository=self.target,
1310
 
                        from_repository=self.source,
1311
 
                        last_revision=revision_id,
1312
 
                        pb=pb)
 
1580
        f = GenericRepoFetcher(to_repository=self.target,
 
1581
                               from_repository=self.source,
 
1582
                               last_revision=revision_id,
 
1583
                               pb=pb)
1313
1584
        return f.count_copied, f.failed_revisions
1314
1585
 
1315
1586
    @needs_read_lock
1353
1624
            return self.source._eliminate_revisions_not_present(required_topo_revisions)
1354
1625
 
1355
1626
 
 
1627
class InterKnitRepo(InterRepository):
 
1628
    """Optimised code paths between Knit based repositories."""
 
1629
 
 
1630
    _matching_repo_format = RepositoryFormatKnit1()
 
1631
    """Repository format for testing with."""
 
1632
 
 
1633
    @staticmethod
 
1634
    def is_compatible(source, target):
 
1635
        """Be compatible with known Knit formats.
 
1636
        
 
1637
        We dont test for the stores being of specific types becase that
 
1638
        could lead to confusing results, and there is no need to be 
 
1639
        overly general.
 
1640
        """
 
1641
        try:
 
1642
            return (isinstance(source._format, (RepositoryFormatKnit1)) and
 
1643
                    isinstance(target._format, (RepositoryFormatKnit1)))
 
1644
        except AttributeError:
 
1645
            return False
 
1646
 
 
1647
    @needs_write_lock
 
1648
    def fetch(self, revision_id=None, pb=None):
 
1649
        """See InterRepository.fetch()."""
 
1650
        from bzrlib.fetch import KnitRepoFetcher
 
1651
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
 
1652
               self.source, self.source._format, self.target, self.target._format)
 
1653
        f = KnitRepoFetcher(to_repository=self.target,
 
1654
                            from_repository=self.source,
 
1655
                            last_revision=revision_id,
 
1656
                            pb=pb)
 
1657
        return f.count_copied, f.failed_revisions
 
1658
 
 
1659
    @needs_read_lock
 
1660
    def missing_revision_ids(self, revision_id=None):
 
1661
        """See InterRepository.missing_revision_ids()."""
 
1662
        if revision_id is not None:
 
1663
            source_ids = self.source.get_ancestry(revision_id)
 
1664
            assert source_ids.pop(0) == None
 
1665
        else:
 
1666
            source_ids = self.source._all_possible_ids()
 
1667
        source_ids_set = set(source_ids)
 
1668
        # source_ids is the worst possible case we may need to pull.
 
1669
        # now we want to filter source_ids against what we actually
 
1670
        # have in target, but dont try to check for existence where we know
 
1671
        # we do not have a revision as that would be pointless.
 
1672
        target_ids = set(self.target._all_possible_ids())
 
1673
        possibly_present_revisions = target_ids.intersection(source_ids_set)
 
1674
        actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
 
1675
        required_revisions = source_ids_set.difference(actually_present_revisions)
 
1676
        required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
 
1677
        if revision_id is not None:
 
1678
            # we used get_ancestry to determine source_ids then we are assured all
 
1679
            # revisions referenced are present as they are installed in topological order.
 
1680
            # and the tip revision was validated by get_ancestry.
 
1681
            return required_topo_revisions
 
1682
        else:
 
1683
            # if we just grabbed the possibly available ids, then 
 
1684
            # we only have an estimate of whats available and need to validate
 
1685
            # that against the revision records.
 
1686
            return self.source._eliminate_revisions_not_present(required_topo_revisions)
 
1687
 
1356
1688
InterRepository.register_optimiser(InterWeaveRepo)
 
1689
InterRepository.register_optimiser(InterKnitRepo)
1357
1690
 
1358
1691
 
1359
1692
class RepositoryTestProviderAdapter(object):
1486
1819
        """Update the pb by a step."""
1487
1820
        self.count +=1
1488
1821
        self.pb.update(message, self.count, self.total)
 
1822
 
 
1823
 
 
1824
# Copied from xml.sax.saxutils
 
1825
def _unescape_xml(data):
 
1826
    """Unescape &amp;, &lt;, and &gt; in a string of data.
 
1827
    """
 
1828
    data = data.replace("&lt;", "<")
 
1829
    data = data.replace("&gt;", ">")
 
1830
    # must do ampersand last
 
1831
    return data.replace("&amp;", "&")