277
470
return self.control_files.get_transaction()
279
472
@needs_write_lock
473
def set_make_working_trees(self, new_value):
474
"""Set the policy flag for making working trees when creating branches.
476
This only applies to branches that use this repository.
478
The default is 'True'.
479
:param new_value: True to restore the default, False to disable making
482
# FIXME: split out into a new class/strategy ?
483
if isinstance(self._format, (RepositoryFormat4,
486
raise NotImplementedError(self.set_make_working_trees)
489
self.control_files._transport.delete('no-working-trees')
490
except errors.NoSuchFile:
493
self.control_files.put_utf8('no-working-trees', '')
495
def make_working_trees(self):
496
"""Returns the policy for making working trees on new branches."""
497
# FIXME: split out into a new class/strategy ?
498
if isinstance(self._format, (RepositoryFormat4,
502
return not self.control_files._transport.has('no-working-trees')
280
505
def sign_revision(self, revision_id, gpg_strategy):
281
506
plaintext = Testament.from_revision(self, revision_id).as_short_text()
282
507
self.store_revision_signature(gpg_strategy, plaintext, revision_id)
510
class RepositoryFormat(object):
511
"""A repository format.
513
Formats provide three things:
514
* An initialization routine to construct repository data on disk.
515
* a format string which is used when the BzrDir supports versioned
517
* an open routine which returns a Repository instance.
519
Formats are placed in an dict by their format string for reference
520
during opening. These should be subclasses of RepositoryFormat
523
Once a format is deprecated, just deprecate the initialize and open
524
methods on the format class. Do not deprecate the object, as the
525
object will be created every system load.
527
Common instance attributes:
528
_matchingbzrdir - the bzrdir format that the repository format was
529
originally written to work with. This can be used if manually
530
constructing a bzrdir and repository, or more commonly for test suite
534
_default_format = None
535
"""The default format used for new repositories."""
538
"""The known formats."""
541
def find_format(klass, a_bzrdir):
542
"""Return the format for the repository object in a_bzrdir."""
544
transport = a_bzrdir.get_repository_transport(None)
545
format_string = transport.get("format").read()
546
return klass._formats[format_string]
547
except errors.NoSuchFile:
548
raise errors.NoRepositoryPresent(a_bzrdir)
550
raise errors.UnknownFormatError(format_string)
553
def get_default_format(klass):
554
"""Return the current default format."""
555
return klass._default_format
557
def get_format_string(self):
558
"""Return the ASCII format string that identifies this format.
560
Note that in pre format ?? repositories the format string is
561
not permitted nor written to disk.
563
raise NotImplementedError(self.get_format_string)
565
def initialize(self, a_bzrdir, shared=False):
566
"""Initialize a repository of this format in a_bzrdir.
568
:param a_bzrdir: The bzrdir to put the new repository in it.
569
:param shared: The repository should be initialized as a sharable one.
571
This may raise UninitializableFormat if shared repository are not
572
compatible the a_bzrdir.
575
def is_supported(self):
576
"""Is this format supported?
578
Supported formats must be initializable and openable.
579
Unsupported formats may not support initialization or committing or
580
some other features depending on the reason for not being supported.
584
def open(self, a_bzrdir, _found=False):
585
"""Return an instance of this format for the bzrdir a_bzrdir.
587
_found is a private parameter, do not use it.
590
# we are being called directly and must probe.
591
raise NotImplementedError
592
return Repository(_format=self, a_bzrdir=a_bzrdir)
595
def register_format(klass, format):
596
klass._formats[format.get_format_string()] = format
599
def set_default_format(klass, format):
600
klass._default_format = format
603
def unregister_format(klass, format):
604
assert klass._formats[format.get_format_string()] is format
605
del klass._formats[format.get_format_string()]
608
class PreSplitOutRepositoryFormat(RepositoryFormat):
609
"""Base class for the pre split out repository formats."""
611
def initialize(self, a_bzrdir, shared=False, _internal=False):
612
"""Create a weave repository.
614
TODO: when creating split out bzr branch formats, move this to a common
615
base for Format5, Format6. or something like that.
617
from bzrlib.weavefile import write_weave_v5
618
from bzrlib.weave import Weave
621
raise errors.IncompatibleFormat(self, a_bzrdir._format)
624
# always initialized when the bzrdir is.
625
return Repository(_format=self, a_bzrdir=a_bzrdir)
627
# Create an empty weave
629
bzrlib.weavefile.write_weave_v5(Weave(), sio)
630
empty_weave = sio.getvalue()
632
mutter('creating repository in %s.', a_bzrdir.transport.base)
633
dirs = ['revision-store', 'weaves']
634
lock_file = 'branch-lock'
635
files = [('inventory.weave', StringIO(empty_weave)),
638
# FIXME: RBC 20060125 dont peek under the covers
639
# NB: no need to escape relative paths that are url safe.
640
control_files = LockableFiles(a_bzrdir.transport, 'branch-lock')
641
control_files.lock_write()
642
control_files._transport.mkdir_multi(dirs,
643
mode=control_files._dir_mode)
645
for file, content in files:
646
control_files.put(file, content)
648
control_files.unlock()
649
return Repository(_format=self, a_bzrdir=a_bzrdir)
652
class RepositoryFormat4(PreSplitOutRepositoryFormat):
653
"""Bzr repository format 4.
655
This repository format has:
657
- TextStores for texts, inventories,revisions.
659
This format is deprecated: it indexes texts using a text id which is
660
removed in format 5; initializationa and write support for this format
665
super(RepositoryFormat4, self).__init__()
666
self._matchingbzrdir = bzrdir.BzrDirFormat4()
668
def initialize(self, url, shared=False, _internal=False):
669
"""Format 4 branches cannot be created."""
670
raise errors.UninitializableFormat(self)
672
def is_supported(self):
673
"""Format 4 is not supported.
675
It is not supported because the model changed from 4 to 5 and the
676
conversion logic is expensive - so doing it on the fly was not
682
class RepositoryFormat5(PreSplitOutRepositoryFormat):
683
"""Bzr control format 5.
685
This repository format has:
686
- weaves for file texts and inventory
688
- TextStores for revisions and signatures.
692
super(RepositoryFormat5, self).__init__()
693
self._matchingbzrdir = bzrdir.BzrDirFormat5()
696
class RepositoryFormat6(PreSplitOutRepositoryFormat):
697
"""Bzr control format 6.
699
This repository format has:
700
- weaves for file texts and inventory
701
- hash subdirectory based stores.
702
- TextStores for revisions and signatures.
706
super(RepositoryFormat6, self).__init__()
707
self._matchingbzrdir = bzrdir.BzrDirFormat6()
710
class RepositoryFormat7(RepositoryFormat):
713
This repository format has:
714
- weaves for file texts and inventory
715
- hash subdirectory based stores.
716
- TextStores for revisions and signatures.
717
- a format marker of its own
718
- an optional 'shared-storage' flag
721
def get_format_string(self):
722
"""See RepositoryFormat.get_format_string()."""
723
return "Bazaar-NG Repository format 7"
725
def initialize(self, a_bzrdir, shared=False):
726
"""Create a weave repository.
728
:param shared: If true the repository will be initialized as a shared
731
from bzrlib.weavefile import write_weave_v5
732
from bzrlib.weave import Weave
734
# Create an empty weave
736
bzrlib.weavefile.write_weave_v5(Weave(), sio)
737
empty_weave = sio.getvalue()
739
mutter('creating repository in %s.', a_bzrdir.transport.base)
740
dirs = ['revision-store', 'weaves']
741
files = [('inventory.weave', StringIO(empty_weave)),
743
utf8_files = [('format', self.get_format_string())]
745
# FIXME: RBC 20060125 dont peek under the covers
746
# NB: no need to escape relative paths that are url safe.
748
repository_transport = a_bzrdir.get_repository_transport(self)
749
repository_transport.put(lock_file, StringIO()) # TODO get the file mode from the bzrdir lock files., mode=file_mode)
750
control_files = LockableFiles(repository_transport, 'lock')
751
control_files.lock_write()
752
control_files._transport.mkdir_multi(dirs,
753
mode=control_files._dir_mode)
755
for file, content in files:
756
control_files.put(file, content)
757
for file, content in utf8_files:
758
control_files.put_utf8(file, content)
760
control_files.put_utf8('shared-storage', '')
762
control_files.unlock()
763
return Repository(_format=self, a_bzrdir=a_bzrdir)
766
super(RepositoryFormat7, self).__init__()
767
self._matchingbzrdir = bzrdir.BzrDirMetaFormat1()
770
# formats which have no format string are not discoverable
771
# and not independently creatable, so are not registered.
772
_default_format = RepositoryFormat7()
773
RepositoryFormat.register_format(_default_format)
774
RepositoryFormat.set_default_format(_default_format)
775
_legacy_formats = [RepositoryFormat4(),
780
class InterRepository(object):
781
"""This class represents operations taking place between two repositories.
783
Its instances have methods like copy_content and fetch, and contain
784
references to the source and target repositories these operations can be
787
Often we will provide convenience methods on 'repository' which carry out
788
operations with another repository - they will always forward to
789
InterRepository.get(other).method_name(parameters).
791
# XXX: FIXME: FUTURE: robertc
792
# testing of these probably requires a factory in optimiser type, and
793
# then a test adapter to test each type thoroughly.
797
"""The available optimised InterRepository types."""
799
def __init__(self, source, target):
800
"""Construct a default InterRepository instance. Please use 'get'.
802
Only subclasses of InterRepository should call
803
InterRepository.__init__ - clients should call InterRepository.get
804
instead which will create an optimised InterRepository if possible.
810
def copy_content(self, revision_id=None, basis=None):
811
"""Make a complete copy of the content in self into destination.
813
This is a destructive operation! Do not use it on existing
816
:param revision_id: Only copy the content needed to construct
817
revision_id and its parents.
818
:param basis: Copy the needed data preferentially from basis.
821
self.target.set_make_working_trees(self.source.make_working_trees())
822
except NotImplementedError:
824
# grab the basis available data
825
if basis is not None:
826
self.target.fetch(basis, revision_id=revision_id)
827
# but dont both fetching if we have the needed data now.
828
if (revision_id not in (None, NULL_REVISION) and
829
self.target.has_revision(revision_id)):
831
self.target.fetch(self.source, revision_id=revision_id)
833
def _double_lock(self, lock_source, lock_target):
834
"""Take out too locks, rolling back the first if the second throws."""
839
# we want to ensure that we don't leave source locked by mistake.
840
# and any error on target should not confuse source.
845
def fetch(self, revision_id=None, pb=None):
846
"""Fetch the content required to construct revision_id.
848
The content is copied from source to target.
850
:param revision_id: if None all content is copied, if NULL_REVISION no
852
:param pb: optional progress bar to use for progress reports. If not
853
provided a default one will be created.
855
Returns the copied revision count and the failed revisions in a tuple:
858
from bzrlib.fetch import RepoFetcher
859
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
860
self.source, self.source._format, self.target, self.target._format)
861
f = RepoFetcher(to_repository=self.target,
862
from_repository=self.source,
863
last_revision=revision_id,
865
return f.count_copied, f.failed_revisions
868
def get(klass, repository_source, repository_target):
869
"""Retrieve a InterRepository worker object for these repositories.
871
:param repository_source: the repository to be the 'source' member of
872
the InterRepository instance.
873
:param repository_target: the repository to be the 'target' member of
874
the InterRepository instance.
875
If an optimised InterRepository worker exists it will be used otherwise
876
a default InterRepository instance will be created.
878
for provider in klass._optimisers:
879
if provider.is_compatible(repository_source, repository_target):
880
return provider(repository_source, repository_target)
881
return InterRepository(repository_source, repository_target)
884
"""Take out a logical read lock.
886
This will lock the source branch and the target branch. The source gets
887
a read lock and the target a read lock.
889
self._double_lock(self.source.lock_read, self.target.lock_read)
891
def lock_write(self):
892
"""Take out a logical write lock.
894
This will lock the source branch and the target branch. The source gets
895
a read lock and the target a write lock.
897
self._double_lock(self.source.lock_read, self.target.lock_write)
900
def missing_revision_ids(self, revision_id=None):
901
"""Return the revision ids that source has that target does not.
903
These are returned in topological order.
905
:param revision_id: only return revision ids included by this
908
# generic, possibly worst case, slow code path.
909
target_ids = set(self.source.all_revision_ids())
910
if revision_id is not None:
911
source_ids = self.target.get_ancestry(revision_id)
912
assert source_ids.pop(0) == None
914
source_ids = self.target.all_revision_ids()
915
result_set = set(source_ids).difference(target_ids)
916
# this may look like a no-op: its not. It preserves the ordering
917
# other_ids had while only returning the members from other_ids
918
# that we've decided we need.
919
return [rev_id for rev_id in other_ids if rev_id in result_set]
922
def register_optimiser(klass, optimiser):
923
"""Register an InterRepository optimiser."""
924
klass._optimisers.add(optimiser)
927
"""Release the locks on source and target."""
934
def unregister_optimiser(klass, optimiser):
935
"""Unregister an InterRepository optimiser."""
936
klass._optimisers.remove(optimiser)
939
class InterWeaveRepo(InterRepository):
940
"""Optimised code paths between Weave based repositories."""
942
_matching_repo_format = _default_format
943
"""Repository format for testing with."""
946
def is_compatible(source, target):
947
"""Be compatible with known Weave formats.
949
We dont test for the stores being of specific types becase that
950
could lead to confusing results, and there is no need to be
954
return (isinstance(source._format, (RepositoryFormat5,
956
RepositoryFormat7)) and
957
isinstance(target._format, (RepositoryFormat5,
960
except AttributeError:
964
def copy_content(self, revision_id=None, basis=None):
965
"""See InterRepository.copy_content()."""
966
# weave specific optimised path:
967
if basis is not None:
968
# copy the basis in, then fetch remaining data.
969
basis.copy_content_into(self.target, revision_id)
970
# the basis copy_content_into could misset this.
972
self.target.set_make_working_trees(self.source.make_working_trees())
973
except NotImplementedError:
975
self.target.fetch(self.source, revision_id=revision_id)
978
self.target.set_make_working_trees(self.source.make_working_trees())
979
except NotImplementedError:
982
if self.source.control_files._transport.listable():
983
pb = bzrlib.ui.ui_factory.progress_bar()
984
copy_all(self.source.weave_store,
985
self.target.weave_store, pb=pb)
986
pb.update('copying inventory', 0, 1)
987
self.target.control_weaves.copy_multi(
988
self.source.control_weaves, ['inventory'])
989
copy_all(self.source.revision_store,
990
self.target.revision_store, pb=pb)
992
self.target.fetch(self.source, revision_id=revision_id)
995
def fetch(self, revision_id=None, pb=None):
996
"""See InterRepository.fetch()."""
997
from bzrlib.fetch import RepoFetcher
998
mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
999
self.source, self.source._format, self.target, self.target._format)
1000
f = RepoFetcher(to_repository=self.target,
1001
from_repository=self.source,
1002
last_revision=revision_id,
1004
return f.count_copied, f.failed_revisions
1007
def missing_revision_ids(self, revision_id=None):
1008
"""See InterRepository.missing_revision_ids()."""
1009
# we want all revisions to satisfy revision_id in source.
1010
# but we dont want to stat every file here and there.
1011
# we want then, all revisions other needs to satisfy revision_id
1012
# checked, but not those that we have locally.
1013
# so the first thing is to get a subset of the revisions to
1014
# satisfy revision_id in source, and then eliminate those that
1015
# we do already have.
1016
# this is slow on high latency connection to self, but as as this
1017
# disk format scales terribly for push anyway due to rewriting
1018
# inventory.weave, this is considered acceptable.
1020
if revision_id is not None:
1021
source_ids = self.source.get_ancestry(revision_id)
1022
assert source_ids.pop(0) == None
1024
source_ids = self.source._all_possible_ids()
1025
source_ids_set = set(source_ids)
1026
# source_ids is the worst possible case we may need to pull.
1027
# now we want to filter source_ids against what we actually
1028
# have in target, but dont try to check for existence where we know
1029
# we do not have a revision as that would be pointless.
1030
target_ids = set(self.target._all_possible_ids())
1031
possibly_present_revisions = target_ids.intersection(source_ids_set)
1032
actually_present_revisions = set(self.target._eliminate_revisions_not_present(possibly_present_revisions))
1033
required_revisions = source_ids_set.difference(actually_present_revisions)
1034
required_topo_revisions = [rev_id for rev_id in source_ids if rev_id in required_revisions]
1035
if revision_id is not None:
1036
# we used get_ancestry to determine source_ids then we are assured all
1037
# revisions referenced are present as they are installed in topological order.
1038
# and the tip revision was validated by get_ancestry.
1039
return required_topo_revisions
1041
# if we just grabbed the possibly available ids, then
1042
# we only have an estimate of whats available and need to validate
1043
# that against the revision records.
1044
return self.source._eliminate_revisions_not_present(required_topo_revisions)
1047
InterRepository.register_optimiser(InterWeaveRepo)
1050
class RepositoryTestProviderAdapter(object):
1051
"""A tool to generate a suite testing multiple repository formats at once.
1053
This is done by copying the test once for each transport and injecting
1054
the transport_server, transport_readonly_server, and bzrdir_format and
1055
repository_format classes into each copy. Each copy is also given a new id()
1056
to make it easy to identify.
1059
def __init__(self, transport_server, transport_readonly_server, formats):
1060
self._transport_server = transport_server
1061
self._transport_readonly_server = transport_readonly_server
1062
self._formats = formats
1064
def adapt(self, test):
1065
result = TestSuite()
1066
for repository_format, bzrdir_format in self._formats:
1067
new_test = deepcopy(test)
1068
new_test.transport_server = self._transport_server
1069
new_test.transport_readonly_server = self._transport_readonly_server
1070
new_test.bzrdir_format = bzrdir_format
1071
new_test.repository_format = repository_format
1072
def make_new_test_id():
1073
new_id = "%s(%s)" % (new_test.id(), repository_format.__class__.__name__)
1074
return lambda: new_id
1075
new_test.id = make_new_test_id()
1076
result.addTest(new_test)
1080
class InterRepositoryTestProviderAdapter(object):
1081
"""A tool to generate a suite testing multiple inter repository formats.
1083
This is done by copying the test once for each interrepo provider and injecting
1084
the transport_server, transport_readonly_server, repository_format and
1085
repository_to_format classes into each copy.
1086
Each copy is also given a new id() to make it easy to identify.
1089
def __init__(self, transport_server, transport_readonly_server, formats):
1090
self._transport_server = transport_server
1091
self._transport_readonly_server = transport_readonly_server
1092
self._formats = formats
1094
def adapt(self, test):
1095
result = TestSuite()
1096
for interrepo_class, repository_format, repository_format_to in self._formats:
1097
new_test = deepcopy(test)
1098
new_test.transport_server = self._transport_server
1099
new_test.transport_readonly_server = self._transport_readonly_server
1100
new_test.interrepo_class = interrepo_class
1101
new_test.repository_format = repository_format
1102
new_test.repository_format_to = repository_format_to
1103
def make_new_test_id():
1104
new_id = "%s(%s)" % (new_test.id(), interrepo_class.__name__)
1105
return lambda: new_id
1106
new_test.id = make_new_test_id()
1107
result.addTest(new_test)
1111
def default_test_list():
1112
"""Generate the default list of interrepo permutations to test."""
1114
# test the default InterRepository between format 6 and the current
1116
# XXX: robertc 20060220 reinstate this when there are two supported
1117
# formats which do not have an optimal code path between them.
1118
#result.append((InterRepository, RepositoryFormat6(),
1119
# RepositoryFormat.get_default_format()))
1120
for optimiser in InterRepository._optimisers:
1121
result.append((optimiser,
1122
optimiser._matching_repo_format,
1123
optimiser._matching_repo_format
1125
# if there are specific combinations we want to use, we can add them