1
# Copyright (C) 2005 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""BzrDir logic. The BzrDir is the basic control directory used by bzr.
19
At format 7 this was split out into Branch, Repository and Checkout control
23
from copy import deepcopy
24
from cStringIO import StringIO
25
from unittest import TestSuite
29
import bzrlib.errors as errors
30
from bzrlib.lockable_files import LockableFiles
31
from bzrlib.osutils import safe_unicode
32
from bzrlib.trace import mutter
33
from bzrlib.symbol_versioning import *
34
from bzrlib.transport import get_transport
35
from bzrlib.transport.local import LocalTransport
39
"""A .bzr control diretory.
41
BzrDir instances let you create or open any of the things that can be
42
found within .bzr - checkouts, branches and repositories.
45
the transport which this bzr dir is rooted at (i.e. file:///.../.bzr/)
47
a transport connected to the directory this bzr was opened from.
50
def _check_supported(self, format, allow_unsupported):
51
"""Check whether format is a supported format.
53
If allow_unsupported is True, this is a no-op.
55
if not allow_unsupported and not format.is_supported():
56
raise errors.UnsupportedFormatError(format)
58
def clone(self, url, revision_id=None, basis=None):
59
"""Clone this bzrdir and its contents to url verbatim.
61
If urls last component does not exist, it will be created.
63
if revision_id is not None, then the clone operation may tune
64
itself to download less data.
67
result = self._format.initialize(url)
68
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
70
self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
71
except errors.NoRepositoryPresent:
74
self.open_branch().clone(result, revision_id=revision_id)
75
except errors.NotBranchError:
78
self.open_workingtree().clone(result, basis=basis_tree)
79
except (errors.NotBranchError, errors.NotLocalUrl):
83
def _get_basis_components(self, basis):
84
"""Retrieve the basis components that are available at basis."""
86
return None, None, None
88
basis_tree = basis.open_workingtree()
89
basis_branch = basis_tree.branch
90
basis_repo = basis_branch.repository
91
except (errors.NoWorkingTree, errors.NotLocalUrl):
94
basis_branch = basis.open_branch()
95
basis_repo = basis_branch.repository
96
except errors.NotBranchError:
99
basis_repo = basis.open_repository()
100
except errors.NoRepositoryPresent:
102
return basis_repo, basis_branch, basis_tree
104
def _make_tail(self, url):
105
segments = url.split('/')
106
if segments and segments[-1] not in ('', '.'):
107
parent = '/'.join(segments[:-1])
108
t = bzrlib.transport.get_transport(parent)
110
t.mkdir(segments[-1])
111
except errors.FileExists:
116
"""Create a new BzrDir at the url 'base'.
118
This will call the current default formats initialize with base
119
as the only parameter.
121
If you need a specific format, consider creating an instance
122
of that and calling initialize().
124
segments = base.split('/')
125
if segments and segments[-1] not in ('', '.'):
126
parent = '/'.join(segments[:-1])
127
t = bzrlib.transport.get_transport(parent)
129
t.mkdir(segments[-1])
130
except errors.FileExists:
132
return BzrDirFormat.get_default_format().initialize(safe_unicode(base))
134
def create_branch(self):
135
"""Create a branch in this BzrDir.
137
The bzrdirs format will control what branch format is created.
138
For more control see BranchFormatXX.create(a_bzrdir).
140
raise NotImplementedError(self.create_branch)
143
def create_branch_and_repo(base):
144
"""Create a new BzrDir, Branch and Repository at the url 'base'.
146
This will use the current default BzrDirFormat, and use whatever
147
repository format that that uses via bzrdir.create_branch and
150
The created Branch object is returned.
152
bzrdir = BzrDir.create(base)
153
bzrdir.create_repository()
154
return bzrdir.create_branch()
157
def create_repository(base, shared=False):
158
"""Create a new BzrDir and Repository at the url 'base'.
160
This will use the current default BzrDirFormat, and use whatever
161
repository format that that uses for bzrdirformat.create_repository.
163
;param shared: Create a shared repository rather than a standalone
165
The Repository object is returned.
167
This must be overridden as an instance method in child classes, where
168
it should take no parameters and construct whatever repository format
169
that child class desires.
171
bzrdir = BzrDir.create(base)
172
return bzrdir.create_repository()
175
def create_standalone_workingtree(base):
176
"""Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
178
'base' must be a local path or a file:// url.
180
This will use the current default BzrDirFormat, and use whatever
181
repository format that that uses for bzrdirformat.create_workingtree,
182
create_branch and create_repository.
184
The WorkingTree object is returned.
186
t = get_transport(safe_unicode(base))
187
if not isinstance(t, LocalTransport):
188
raise errors.NotLocalUrl(base)
189
bzrdir = BzrDir.create_branch_and_repo(safe_unicode(base)).bzrdir
190
return bzrdir.create_workingtree()
192
def create_workingtree(self):
193
"""Create a working tree at this BzrDir"""
194
raise NotImplementedError(self.create_workingtree)
196
def get_branch_transport(self, branch_format):
197
"""Get the transport for use by branch format in this BzrDir.
199
Note that bzr dirs that do not support format strings will raise
200
IncompatibleFormat if the branch format they are given has
201
a format string, and vice verca.
203
If branch_format is None, the transport is returned with no
204
checking. if it is not None, then the returned transport is
205
guaranteed to point to an existing directory ready for use.
207
raise NotImplementedError(self.get_branch_transport)
209
def get_repository_transport(self, repository_format):
210
"""Get the transport for use by repository format in this BzrDir.
212
Note that bzr dirs that do not support format strings will raise
213
IncompatibleFormat if the repository format they are given has
214
a format string, and vice verca.
216
If repository_format is None, the transport is returned with no
217
checking. if it is not None, then the returned transport is
218
guaranteed to point to an existing directory ready for use.
220
raise NotImplementedError(self.get_repository_transport)
222
def get_workingtree_transport(self, tree_format):
223
"""Get the transport for use by workingtree format in this BzrDir.
225
Note that bzr dirs that do not support format strings will raise
226
IncompatibleFormat if the workingtree format they are given has
227
a format string, and vice verca.
229
If workingtree_format is None, the transport is returned with no
230
checking. if it is not None, then the returned transport is
231
guaranteed to point to an existing directory ready for use.
233
raise NotImplementedError(self.get_workingtree_transport)
235
def __init__(self, _transport, _format):
236
"""Initialize a Bzr control dir object.
238
Only really common logic should reside here, concrete classes should be
239
made with varying behaviours.
241
:param _format: the format that is creating this BzrDir instance.
242
:param _transport: the transport this dir is based at.
244
self._format = _format
245
self.transport = _transport.clone('.bzr')
246
self.root_transport = _transport
249
def open_unsupported(base):
250
"""Open a branch which is not supported."""
251
return BzrDir.open(base, _unsupported=True)
254
def open(base, _unsupported=False):
255
"""Open an existing bzrdir, rooted at 'base' (url)
257
_unsupported is a private parameter to the BzrDir class.
259
t = get_transport(base)
260
mutter("trying to open %r with transport %r", base, t)
261
format = BzrDirFormat.find_format(t)
262
if not _unsupported and not format.is_supported():
263
# see open_downlevel to open legacy branches.
264
raise errors.UnsupportedFormatError(
265
'sorry, format %s not supported' % format,
266
['use a different bzr version',
267
'or remove the .bzr directory'
268
' and "bzr init" again'])
269
return format.open(t, _found=True)
271
def open_branch(self, unsupported=False):
272
"""Open the branch object at this BzrDir if one is present.
274
If unsupported is True, then no longer supported branch formats can
277
TODO: static convenience version of this?
279
raise NotImplementedError(self.open_branch)
282
def open_containing(url):
283
"""Open an existing branch which contains url.
285
This probes for a branch at url, and searches upwards from there.
287
Basically we keep looking up until we find the control directory or
288
run into the root. If there isn't one, raises NotBranchError.
289
If there is one and it is either an unrecognised format or an unsupported
290
format, UnknownFormatError or UnsupportedFormatError are raised.
291
If there is one, it is returned, along with the unused portion of url.
293
t = get_transport(url)
294
# this gets the normalised url back. I.e. '.' -> the full path.
298
format = BzrDirFormat.find_format(t)
299
return format.open(t), t.relpath(url)
300
except errors.NotBranchError, e:
301
mutter('not a branch in: %r %s', t.base, e)
302
new_t = t.clone('..')
303
if new_t.base == t.base:
304
# reached the root, whatever that may be
305
raise errors.NotBranchError(path=url)
308
def open_repository(self, _unsupported=False):
309
"""Open the repository object at this BzrDir if one is present.
311
This will not follow the Branch object pointer - its strictly a direct
312
open facility. Most client code should use open_branch().repository to
315
_unsupported is a private parameter, not part of the api.
316
TODO: static convenience version of this?
318
raise NotImplementedError(self.open_repository)
320
def open_workingtree(self, _unsupported=False):
321
"""Open the workingtree object at this BzrDir if one is present.
323
TODO: static convenience version of this?
325
raise NotImplementedError(self.open_workingtree)
327
def sprout(self, url, revision_id=None, basis=None):
328
"""Create a copy of this bzrdir prepared for use as a new line of
331
If urls last component does not exist, it will be created.
333
Attributes related to the identity of the source branch like
334
branch nickname will be cleaned, a working tree is created
335
whether one existed before or not; and a local branch is always
338
if revision_id is not None, then the clone operation may tune
339
itself to download less data.
342
result = self._format.initialize(url)
343
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
345
source_branch = self.open_branch()
346
source_repository = source_branch.repository
347
except errors.NotBranchError:
350
source_repository = self.open_repository()
351
except errors.NoRepositoryPresent:
352
# copy the basis one if there is one
353
source_repository = basis_repo
354
if source_repository is not None:
355
source_repository.clone(result,
356
revision_id=revision_id,
359
# no repo available, make a new one
360
result.create_repository()
361
if source_branch is not None:
362
source_branch.sprout(result, revision_id=revision_id)
364
result.create_branch()
366
self.open_workingtree().clone(result,
367
revision_id=revision_id,
369
except (errors.NotBranchError, errors.NotLocalUrl):
370
result.create_workingtree()
374
class BzrDirPreSplitOut(BzrDir):
375
"""A common class for the all-in-one formats."""
377
def clone(self, url, revision_id=None, basis=None):
378
"""See BzrDir.clone()."""
379
from bzrlib.workingtree import WorkingTreeFormat2
381
result = self._format.initialize(url, _cloning=True)
382
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
383
self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
384
self.open_branch().clone(result, revision_id=revision_id)
386
self.open_workingtree().clone(result, basis=basis_tree)
387
except errors.NotLocalUrl:
388
# make a new one, this format always has to have one.
389
WorkingTreeFormat2().initialize(result)
392
def create_branch(self):
393
"""See BzrDir.create_branch."""
394
return self.open_branch()
396
def create_repository(self, shared=False):
397
"""See BzrDir.create_repository."""
399
raise errors.IncompatibleFormat('shared repository', self._format)
400
return self.open_repository()
402
def create_workingtree(self):
403
"""See BzrDir.create_workingtree."""
404
return self.open_workingtree()
406
def get_branch_transport(self, branch_format):
407
"""See BzrDir.get_branch_transport()."""
408
if branch_format is None:
409
return self.transport
411
branch_format.get_format_string()
412
except NotImplementedError:
413
return self.transport
414
raise errors.IncompatibleFormat(branch_format, self._format)
416
def get_repository_transport(self, repository_format):
417
"""See BzrDir.get_repository_transport()."""
418
if repository_format is None:
419
return self.transport
421
repository_format.get_format_string()
422
except NotImplementedError:
423
return self.transport
424
raise errors.IncompatibleFormat(repository_format, self._format)
426
def get_workingtree_transport(self, workingtree_format):
427
"""See BzrDir.get_workingtree_transport()."""
428
if workingtree_format is None:
429
return self.transport
431
workingtree_format.get_format_string()
432
except NotImplementedError:
433
return self.transport
434
raise errors.IncompatibleFormat(workingtree_format, self._format)
436
def open_branch(self, unsupported=False):
437
"""See BzrDir.open_branch."""
438
from bzrlib.branch import BzrBranchFormat4
439
format = BzrBranchFormat4()
440
self._check_supported(format, unsupported)
441
return format.open(self, _found=True)
443
def sprout(self, url, revision_id=None, basis=None):
444
"""See BzrDir.sprout()."""
445
from bzrlib.workingtree import WorkingTreeFormat2
447
result = self._format.initialize(url, _cloning=True)
448
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
450
self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
451
except errors.NoRepositoryPresent:
454
self.open_branch().sprout(result, revision_id=revision_id)
455
except errors.NotBranchError:
458
self.open_workingtree().clone(result, basis=basis_tree)
459
except (errors.NotBranchError, errors.NotLocalUrl):
460
# we always want a working tree
461
WorkingTreeFormat2().initialize(result)
465
class BzrDir4(BzrDirPreSplitOut):
466
"""A .bzr version 4 control object."""
468
def create_repository(self, shared=False):
469
"""See BzrDir.create_repository."""
470
from bzrlib.repository import RepositoryFormat4
471
return RepositoryFormat4().initialize(self, shared)
473
def open_repository(self):
474
"""See BzrDir.open_repository."""
475
from bzrlib.repository import RepositoryFormat4
476
return RepositoryFormat4().open(self, _found=True)
479
class BzrDir5(BzrDirPreSplitOut):
480
"""A .bzr version 5 control object."""
482
def open_repository(self):
483
"""See BzrDir.open_repository."""
484
from bzrlib.repository import RepositoryFormat5
485
return RepositoryFormat5().open(self, _found=True)
487
def open_workingtree(self, _unsupported=False):
488
"""See BzrDir.create_workingtree."""
489
from bzrlib.workingtree import WorkingTreeFormat2
490
return WorkingTreeFormat2().open(self, _found=True)
493
class BzrDir6(BzrDirPreSplitOut):
494
"""A .bzr version 6 control object."""
496
def open_repository(self):
497
"""See BzrDir.open_repository."""
498
from bzrlib.repository import RepositoryFormat6
499
return RepositoryFormat6().open(self, _found=True)
501
def open_workingtree(self, _unsupported=False):
502
"""See BzrDir.create_workingtree."""
503
from bzrlib.workingtree import WorkingTreeFormat2
504
return WorkingTreeFormat2().open(self, _found=True)
507
class BzrDirMeta1(BzrDir):
508
"""A .bzr meta version 1 control object.
510
This is the first control object where the
511
individual formats are really split out.
514
def create_branch(self):
515
"""See BzrDir.create_branch."""
516
from bzrlib.branch import BranchFormat
517
return BranchFormat.get_default_format().initialize(self)
519
def create_repository(self, shared=False):
520
"""See BzrDir.create_repository."""
521
from bzrlib.repository import RepositoryFormat
522
return RepositoryFormat.get_default_format().initialize(self, shared)
524
def create_workingtree(self):
525
"""See BzrDir.create_workingtree."""
526
from bzrlib.workingtree import WorkingTreeFormat
527
return WorkingTreeFormat.get_default_format().initialize(self)
529
def get_branch_transport(self, branch_format):
530
"""See BzrDir.get_branch_transport()."""
531
if branch_format is None:
532
return self.transport.clone('branch')
534
branch_format.get_format_string()
535
except NotImplementedError:
536
raise errors.IncompatibleFormat(branch_format, self._format)
538
self.transport.mkdir('branch')
539
except errors.FileExists:
541
return self.transport.clone('branch')
543
def get_repository_transport(self, repository_format):
544
"""See BzrDir.get_repository_transport()."""
545
if repository_format is None:
546
return self.transport.clone('repository')
548
repository_format.get_format_string()
549
except NotImplementedError:
550
raise errors.IncompatibleFormat(repository_format, self._format)
552
self.transport.mkdir('repository')
553
except errors.FileExists:
555
return self.transport.clone('repository')
557
def get_workingtree_transport(self, workingtree_format):
558
"""See BzrDir.get_workingtree_transport()."""
559
if workingtree_format is None:
560
return self.transport.clone('checkout')
562
workingtree_format.get_format_string()
563
except NotImplementedError:
564
raise errors.IncompatibleFormat(workingtree_format, self._format)
566
self.transport.mkdir('checkout')
567
except errors.FileExists:
569
return self.transport.clone('checkout')
571
def open_branch(self, unsupported=False):
572
"""See BzrDir.open_branch."""
573
from bzrlib.branch import BranchFormat
574
format = BranchFormat.find_format(self)
575
self._check_supported(format, unsupported)
576
return format.open(self, _found=True)
578
def open_repository(self, unsupported=False):
579
"""See BzrDir.open_repository."""
580
from bzrlib.repository import RepositoryFormat
581
format = RepositoryFormat.find_format(self)
582
self._check_supported(format, unsupported)
583
return format.open(self, _found=True)
585
def open_workingtree(self, unsupported=False):
586
"""See BzrDir.create_workingtree."""
587
from bzrlib.workingtree import WorkingTreeFormat
588
format = WorkingTreeFormat.find_format(self)
589
self._check_supported(format, unsupported)
590
return format.open(self, _found=True)
593
class BzrDirFormat(object):
594
"""An encapsulation of the initialization and open routines for a format.
596
Formats provide three things:
597
* An initialization routine,
601
Formats are placed in an dict by their format string for reference
602
during bzrdir opening. These should be subclasses of BzrDirFormat
605
Once a format is deprecated, just deprecate the initialize and open
606
methods on the format class. Do not deprecate the object, as the
607
object will be created every system load.
610
_default_format = None
611
"""The default format used for new .bzr dirs."""
614
"""The known formats."""
617
def find_format(klass, transport):
618
"""Return the format registered for URL."""
620
format_string = transport.get(".bzr/branch-format").read()
621
return klass._formats[format_string]
622
except errors.NoSuchFile:
623
raise errors.NotBranchError(path=transport.base)
625
raise errors.UnknownFormatError(format_string)
628
def get_default_format(klass):
629
"""Return the current default format."""
630
return klass._default_format
632
def get_format_string(self):
633
"""Return the ASCII format string that identifies this format."""
634
raise NotImplementedError(self.get_format_string)
636
def initialize(self, url):
637
"""Create a bzr control dir at this url and return an opened copy."""
638
# Since we don't have a .bzr directory, inherit the
639
# mode from the root directory
640
t = get_transport(url)
641
temp_control = LockableFiles(t, '')
642
temp_control._transport.mkdir('.bzr',
643
# FIXME: RBC 20060121 dont peek under
645
mode=temp_control._dir_mode)
646
file_mode = temp_control._file_mode
648
mutter('created control directory in ' + t.base)
649
control = t.clone('.bzr')
650
lock_file = 'branch-lock'
651
utf8_files = [('README',
652
"This is a Bazaar-NG control directory.\n"
653
"Do not change any files in this directory.\n"),
654
('branch-format', self.get_format_string()),
656
# NB: no need to escape relative paths that are url safe.
657
control.put(lock_file, StringIO(), mode=file_mode)
658
control_files = LockableFiles(control, lock_file)
659
control_files.lock_write()
661
for file, content in utf8_files:
662
control_files.put_utf8(file, content)
664
control_files.unlock()
665
return self.open(t, _found=True)
667
def is_supported(self):
668
"""Is this format supported?
670
Supported formats must be initializable and openable.
671
Unsupported formats may not support initialization or committing or
672
some other features depending on the reason for not being supported.
676
def open(self, transport, _found=False):
677
"""Return an instance of this format for the dir transport points at.
679
_found is a private parameter, do not use it.
682
assert isinstance(BzrDirFormat.find_format(transport),
684
return self._open(transport)
686
def _open(self, transport):
687
"""Template method helper for opening BzrDirectories.
689
This performs the actual open and any additional logic or parameter
692
raise NotImplementedError(self._open)
695
def register_format(klass, format):
696
klass._formats[format.get_format_string()] = format
699
def set_default_format(klass, format):
700
klass._default_format = format
703
def unregister_format(klass, format):
704
assert klass._formats[format.get_format_string()] is format
705
del klass._formats[format.get_format_string()]
708
class BzrDirFormat4(BzrDirFormat):
711
This format is a combined format for working tree, branch and repository.
713
- Format 1 working trees [always]
714
- Format 4 branches [always]
715
- Format 4 repositories [always]
717
This format is deprecated: it indexes texts using a text it which is
718
removed in format 5; write support for this format has been removed.
721
def get_format_string(self):
722
"""See BzrDirFormat.get_format_string()."""
723
return "Bazaar-NG branch, format 0.0.4\n"
725
def initialize(self, url):
726
"""Format 4 branches cannot be created."""
727
raise errors.UninitializableFormat(self)
729
def is_supported(self):
730
"""Format 4 is not supported.
732
It is not supported because the model changed from 4 to 5 and the
733
conversion logic is expensive - so doing it on the fly was not
738
def _open(self, transport):
739
"""See BzrDirFormat._open."""
740
return BzrDir4(transport, self)
743
class BzrDirFormat5(BzrDirFormat):
744
"""Bzr control format 5.
746
This format is a combined format for working tree, branch and repository.
748
- Format 2 working trees [always]
749
- Format 4 branches [always]
750
- Format 5 repositories [always]
751
Unhashed stores in the repository.
754
def get_format_string(self):
755
"""See BzrDirFormat.get_format_string()."""
756
return "Bazaar-NG branch, format 5\n"
758
def initialize(self, url, _cloning=False):
759
"""Format 5 dirs always have working tree, branch and repository.
761
Except when they are being cloned.
763
from bzrlib.branch import BzrBranchFormat4
764
from bzrlib.repository import RepositoryFormat5
765
from bzrlib.workingtree import WorkingTreeFormat2
766
result = super(BzrDirFormat5, self).initialize(url)
767
RepositoryFormat5().initialize(result, _internal=True)
769
BzrBranchFormat4().initialize(result)
770
WorkingTreeFormat2().initialize(result)
773
def _open(self, transport):
774
"""See BzrDirFormat._open."""
775
return BzrDir5(transport, self)
778
class BzrDirFormat6(BzrDirFormat):
779
"""Bzr control format 6.
781
This format is a combined format for working tree, branch and repository.
783
- Format 2 working trees [always]
784
- Format 4 branches [always]
785
- Format 6 repositories [always]
788
def get_format_string(self):
789
"""See BzrDirFormat.get_format_string()."""
790
return "Bazaar-NG branch, format 6\n"
792
def initialize(self, url, _cloning=False):
793
"""Format 6 dirs always have working tree, branch and repository.
795
Except when they are being cloned.
797
from bzrlib.branch import BzrBranchFormat4
798
from bzrlib.repository import RepositoryFormat6
799
from bzrlib.workingtree import WorkingTreeFormat2
800
result = super(BzrDirFormat6, self).initialize(url)
801
RepositoryFormat6().initialize(result, _internal=True)
803
BzrBranchFormat4().initialize(result)
805
WorkingTreeFormat2().initialize(result)
806
except errors.NotLocalUrl:
807
# emulate pre-check behaviour for working tree and silently
812
def _open(self, transport):
813
"""See BzrDirFormat._open."""
814
return BzrDir6(transport, self)
817
class BzrDirMetaFormat1(BzrDirFormat):
818
"""Bzr meta control format 1
820
This is the first format with split out working tree, branch and repository
823
- Format 3 working trees [optional]
824
- Format 5 branches [optional]
825
- Format 7 repositories [optional]
828
def get_format_string(self):
829
"""See BzrDirFormat.get_format_string()."""
830
return "Bazaar-NG meta directory, format 1\n"
832
def _open(self, transport):
833
"""See BzrDirFormat._open."""
834
return BzrDirMeta1(transport, self)
837
BzrDirFormat.register_format(BzrDirFormat4())
838
BzrDirFormat.register_format(BzrDirFormat5())
839
BzrDirFormat.register_format(BzrDirMetaFormat1())
840
__default_format = BzrDirFormat6()
841
BzrDirFormat.register_format(__default_format)
842
BzrDirFormat.set_default_format(__default_format)
845
class BzrDirTestProviderAdapter(object):
846
"""A tool to generate a suite testing multiple bzrdir formats at once.
848
This is done by copying the test once for each transport and injecting
849
the transport_server, transport_readonly_server, and bzrdir_format
850
classes into each copy. Each copy is also given a new id() to make it
854
def __init__(self, transport_server, transport_readonly_server, formats):
855
self._transport_server = transport_server
856
self._transport_readonly_server = transport_readonly_server
857
self._formats = formats
859
def adapt(self, test):
861
for format in self._formats:
862
new_test = deepcopy(test)
863
new_test.transport_server = self._transport_server
864
new_test.transport_readonly_server = self._transport_readonly_server
865
new_test.bzrdir_format = format
866
def make_new_test_id():
867
new_id = "%s(%s)" % (new_test.id(), format.__class__.__name__)
868
return lambda: new_id
869
new_test.id = make_new_test_id()
870
result.addTest(new_test)
874
class ScratchDir(BzrDir6):
875
"""Special test class: a bzrdir that cleans up itself..
878
>>> base = d.transport.base
881
>>> b.transport.__del__()
886
def __init__(self, files=[], dirs=[], transport=None):
887
"""Make a test branch.
889
This creates a temporary directory and runs init-tree in it.
891
If any files are listed, they are created in the working copy.
893
if transport is None:
894
transport = bzrlib.transport.local.ScratchTransport()
895
# local import for scope restriction
896
BzrDirFormat6().initialize(transport.base)
897
super(ScratchDir, self).__init__(transport, BzrDirFormat6())
898
self.create_repository()
900
self.create_workingtree()
902
super(ScratchDir, self).__init__(transport, BzrDirFormat6())
904
# BzrBranch creates a clone to .bzr and then forgets about the
905
# original transport. A ScratchTransport() deletes itself and
906
# everything underneath it when it goes away, so we need to
907
# grab a local copy to prevent that from happening
908
self._transport = transport
911
self._transport.mkdir(d)
914
self._transport.put(f, 'content of %s' % f)
918
>>> orig = ScratchDir(files=["file1", "file2"])
919
>>> os.listdir(orig.base)
920
[u'.bzr', u'file1', u'file2']
921
>>> clone = orig.clone()
922
>>> if os.name != 'nt':
923
... os.path.samefile(orig.base, clone.base)
925
... orig.base == clone.base
928
>>> os.listdir(clone.base)
929
[u'.bzr', u'file1', u'file2']
931
from shutil import copytree
932
from bzrlib.osutils import mkdtemp
935
copytree(self.base, base, symlinks=True)
937
transport=bzrlib.transport.local.ScratchTransport(base))