28
28
# TODO: Can we move specific formats into separate modules to make this file
31
from copy import deepcopy
32
31
from cStringIO import StringIO
35
from bzrlib.lazy_import import lazy_import
36
lazy_import(globals(), """
37
from copy import deepcopy
34
38
from stat import S_ISDIR
35
from unittest import TestSuite
38
import bzrlib.errors as errors
39
from bzrlib.lockable_files import LockableFiles, TransportLock
40
from bzrlib.lockdir import LockDir
47
revision as _mod_revision,
48
repository as _mod_repository,
41
54
from bzrlib.osutils import (
48
import bzrlib.revision
49
59
from bzrlib.store.revision.text import TextRevisionStore
50
60
from bzrlib.store.text import TextStore
51
61
from bzrlib.store.versioned import WeaveStore
52
from bzrlib.trace import mutter
53
62
from bzrlib.transactions import WriteTransaction
54
63
from bzrlib.transport import get_transport
64
from bzrlib.weave import Weave
67
from bzrlib.trace import mutter
55
68
from bzrlib.transport.local import LocalTransport
56
import bzrlib.urlutils as urlutils
57
from bzrlib.weave import Weave
58
from bzrlib.xml4 import serializer_v4
62
71
class BzrDir(object):
197
206
# TODO: Should take a Transport
199
def create(cls, base):
208
def create(cls, base, format=None):
200
209
"""Create a new BzrDir at the url 'base'.
202
211
This will call the current default formats initialize with base
203
212
as the only parameter.
205
If you need a specific format, consider creating an instance
206
of that and calling initialize().
214
:param format: If supplied, the format of branch to create. If not
215
supplied, the default is used.
208
217
if cls is not BzrDir:
209
raise AssertionError("BzrDir.create always creates the default format, "
210
"not one of %r" % cls)
218
raise AssertionError("BzrDir.create always creates the default"
219
" format, not one of %r" % cls)
211
220
head, tail = urlutils.split(base)
212
221
if tail and tail != '.':
213
t = bzrlib.transport.get_transport(head)
222
t = get_transport(head)
216
225
except errors.FileExists:
218
return BzrDirFormat.get_default_format().initialize(safe_unicode(base))
228
format = BzrDirFormat.get_default_format()
229
return format.initialize(safe_unicode(base))
220
231
def create_branch(self):
221
232
"""Create a branch in this BzrDir.
548
558
raise errors.NotBranchError(path=url)
549
559
a_transport = new_t
562
def open_containing_tree_or_branch(klass, location):
563
"""Return the branch and working tree contained by a location.
565
Returns (tree, branch, relpath).
566
If there is no tree at containing the location, tree will be None.
567
If there is no branch containing the location, an exception will be
569
relpath is the portion of the path that is contained by the branch.
571
bzrdir, relpath = klass.open_containing(location)
573
tree = bzrdir.open_workingtree()
574
except (errors.NoWorkingTree, errors.NotLocalUrl):
576
branch = bzrdir.open_branch()
579
return tree, branch, relpath
551
581
def open_repository(self, _unsupported=False):
552
582
"""Open the repository object at this BzrDir if one is present.
1920
1971
converter = CopyConverter(self.target_format.repository_format)
1921
1972
converter.convert(repo, pb)
1922
1973
return to_convert
1976
class BzrDirFormatInfo(object):
1978
def __init__(self, native, deprecated):
1979
self.deprecated = deprecated
1980
self.native = native
1983
class BzrDirFormatRegistry(registry.Registry):
1984
"""Registry of user-selectable BzrDir subformats.
1986
Differs from BzrDirFormat._control_formats in that it provides sub-formats,
1987
e.g. BzrDirMeta1 with weave repository. Also, it's more user-oriented.
1990
def register_metadir(self, key, repo, help, native=True, deprecated=False):
1991
"""Register a metadir subformat.
1993
repo is the repository format name as a string.
1995
# This should be expanded to support setting WorkingTree and Branch
1996
# formats, once BzrDirMetaFormat1 supports that.
1998
import bzrlib.repository
1999
repo_format = getattr(bzrlib.repository, repo)
2000
bd = BzrDirMetaFormat1()
2001
bd.repository_format = repo_format()
2003
self.register(key, helper, help, native, deprecated)
2005
def register(self, key, factory, help, native=True, deprecated=False):
2006
"""Register a BzrDirFormat factory.
2008
The factory must be a callable that takes one parameter: the key.
2009
It must produce an instance of the BzrDirFormat when called.
2011
This function mainly exists to prevent the info object from being
2014
registry.Registry.register(self, key, factory, help,
2015
BzrDirFormatInfo(native, deprecated))
2017
def register_lazy(self, key, module_name, member_name, help, native=True,
2019
registry.Registry.register_lazy(self, key, module_name, member_name,
2020
help, BzrDirFormatInfo(native, deprecated))
2022
def set_default(self, key):
2023
"""Set the 'default' key to be a clone of the supplied key.
2025
This method must be called once and only once.
2027
registry.Registry.register(self, 'default', self.get(key),
2028
self.get_help(key), info=self.get_info(key))
2030
def set_default_repository(self, key):
2031
"""Set the FormatRegistry default and Repository default.
2033
This is a transitional method while Repository.set_default_format
2036
if 'default' in self:
2037
self.remove('default')
2038
self.set_default(key)
2039
format = self.get('default')()
2040
assert isinstance(format, BzrDirMetaFormat1)
2041
from bzrlib import repository
2042
repository.RepositoryFormat._set_default_format(
2043
format.repository_format)
2045
def make_bzrdir(self, key):
2046
return self.get(key)()
2048
def help_topic(self, topic):
2049
output = textwrap.dedent("""\
2050
Bazaar directory formats
2051
------------------------
2053
These formats can be used for creating branches, working trees, and
2057
default_help = self.get_help('default')
2059
for key in self.keys():
2060
if key == 'default':
2062
help = self.get_help(key)
2063
if help == default_help:
2064
default_realkey = key
2066
help_pairs.append((key, help))
2068
def wrapped(key, help, info):
2070
help = '(native) ' + help
2071
return ' %s:\n%s\n\n' % (key,
2072
textwrap.fill(help, initial_indent=' ',
2073
subsequent_indent=' '))
2074
output += wrapped('%s/default' % default_realkey, default_help,
2075
self.get_info('default'))
2076
deprecated_pairs = []
2077
for key, help in help_pairs:
2078
info = self.get_info(key)
2080
deprecated_pairs.append((key, help))
2082
output += wrapped(key, help, info)
2083
if len(deprecated_pairs) > 0:
2084
output += "Deprecated formats\n------------------\n\n"
2085
for key, help in deprecated_pairs:
2086
info = self.get_info(key)
2087
output += wrapped(key, help, info)
2092
format_registry = BzrDirFormatRegistry()
2093
format_registry.register('weave', BzrDirFormat6,
2094
'Pre-0.8 format. Slower than knit and does not'
2095
' support checkouts or shared repositories.', deprecated=True)
2096
format_registry.register_metadir('knit', 'RepositoryFormatKnit1',
2097
'Format using knits. Recommended.')
2098
format_registry.set_default('knit')
2099
format_registry.register_metadir('metaweave', 'RepositoryFormat7',
2100
'Transitional format in 0.8. Slower than knit.',
2102
format_registry.register_metadir('experimental-knit2', 'RepositoryFormatKnit2',
2103
'Experimental successor to knit. Use at your own risk.')