/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1553.5.48 by Martin Pool
Fix some LockableFiles deprecation warnings
1
# Copyright (C) 2005, 2006 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1534.4.39 by Robert Collins
Basic BzrDir support.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1534.4.39 by Robert Collins
Basic BzrDir support.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1534.4.39 by Robert Collins
Basic BzrDir support.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""BzrDir logic. The BzrDir is the basic control directory used by bzr.
18
19
At format 7 this was split out into Branch, Repository and Checkout control
20
directories.
21
"""
22
1773.4.3 by Martin Pool
[merge] bzr.dev
23
# TODO: remove unittest dependency; put that stuff inside the test suite
24
1534.4.39 by Robert Collins
Basic BzrDir support.
25
from copy import deepcopy
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
26
from cStringIO import StringIO
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
27
import os
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
28
from stat import S_ISDIR
1534.4.39 by Robert Collins
Basic BzrDir support.
29
from unittest import TestSuite
30
31
import bzrlib
32
import bzrlib.errors as errors
1553.5.48 by Martin Pool
Fix some LockableFiles deprecation warnings
33
from bzrlib.lockable_files import LockableFiles, TransportLock
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
34
from bzrlib.lockdir import LockDir
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
35
from bzrlib.osutils import (
36
                            abspath,
37
                            pathjoin,
38
                            safe_unicode,
39
                            sha_strings,
40
                            sha_string,
41
                            )
1551.8.20 by Aaron Bentley
Fix BzrDir.create_workingtree for NULL_REVISION
42
import bzrlib.revision
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
43
from bzrlib.store.revision.text import TextRevisionStore
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
44
from bzrlib.store.text import TextStore
1563.2.25 by Robert Collins
Merge in upstream.
45
from bzrlib.store.versioned import WeaveStore
1534.4.39 by Robert Collins
Basic BzrDir support.
46
from bzrlib.trace import mutter
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
47
from bzrlib.transactions import WriteTransaction
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
48
from bzrlib.transport import get_transport
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
49
from bzrlib.transport.local import LocalTransport
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
50
import bzrlib.urlutils as urlutils
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
51
from bzrlib.weave import Weave
52
from bzrlib.xml4 import serializer_v4
1732.1.26 by John Arbash Meinel
Switch to using bzrlib.xml5.serializer_v5 so that a plugin can override it if we want
53
import bzrlib.xml5
1534.4.39 by Robert Collins
Basic BzrDir support.
54
55
56
class BzrDir(object):
57
    """A .bzr control diretory.
58
    
59
    BzrDir instances let you create or open any of the things that can be
60
    found within .bzr - checkouts, branches and repositories.
61
    
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
62
    transport
63
        the transport which this bzr dir is rooted at (i.e. file:///.../.bzr/)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
64
    root_transport
65
        a transport connected to the directory this bzr was opened from.
1534.4.39 by Robert Collins
Basic BzrDir support.
66
    """
67
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
68
    def break_lock(self):
69
        """Invoke break_lock on the first object in the bzrdir.
70
71
        If there is a tree, the tree is opened and break_lock() called.
72
        Otherwise, branch is tried, and finally repository.
73
        """
74
        try:
75
            thing_to_unlock = self.open_workingtree()
76
        except (errors.NotLocalUrl, errors.NoWorkingTree):
77
            try:
78
                thing_to_unlock = self.open_branch()
79
            except errors.NotBranchError:
80
                try:
81
                    thing_to_unlock = self.open_repository()
82
                except errors.NoRepositoryPresent:
83
                    return
84
        thing_to_unlock.break_lock()
85
1534.5.16 by Robert Collins
Review feedback.
86
    def can_convert_format(self):
87
        """Return true if this bzrdir is one whose format we can convert from."""
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
88
        return True
89
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
90
    @staticmethod
91
    def _check_supported(format, allow_unsupported):
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
92
        """Check whether format is a supported format.
93
94
        If allow_unsupported is True, this is a no-op.
95
        """
96
        if not allow_unsupported and not format.is_supported():
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
97
            # see open_downlevel to open legacy branches.
1740.5.6 by Martin Pool
Clean up many exception classes.
98
            raise errors.UnsupportedFormatError(format=format)
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
99
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
100
    def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
101
        """Clone this bzrdir and its contents to url verbatim.
102
103
        If urls last component does not exist, it will be created.
104
105
        if revision_id is not None, then the clone operation may tune
106
            itself to download less data.
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
107
        :param force_new_repo: Do not use a shared repository for the target 
108
                               even if one is available.
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
109
        """
110
        self._make_tail(url)
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
111
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
112
        result = self._format.initialize(url)
113
        try:
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
114
            local_repo = self.find_repository()
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
115
        except errors.NoRepositoryPresent:
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
116
            local_repo = None
117
        if local_repo:
118
            # may need to copy content in
119
            if force_new_repo:
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
120
                result_repo = local_repo.clone(
121
                    result,
122
                    revision_id=revision_id,
123
                    basis=basis_repo)
124
                result_repo.set_make_working_trees(local_repo.make_working_trees())
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
125
            else:
126
                try:
127
                    result_repo = result.find_repository()
128
                    # fetch content this dir needs.
129
                    if basis_repo:
130
                        # XXX FIXME RBC 20060214 need tests for this when the basis
131
                        # is incomplete
132
                        result_repo.fetch(basis_repo, revision_id=revision_id)
133
                    result_repo.fetch(local_repo, revision_id=revision_id)
134
                except errors.NoRepositoryPresent:
135
                    # needed to make one anyway.
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
136
                    result_repo = local_repo.clone(
137
                        result,
138
                        revision_id=revision_id,
139
                        basis=basis_repo)
140
                    result_repo.set_make_working_trees(local_repo.make_working_trees())
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
141
        # 1 if there is a branch present
142
        #   make sure its content is available in the target repository
143
        #   clone it.
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
144
        try:
145
            self.open_branch().clone(result, revision_id=revision_id)
146
        except errors.NotBranchError:
147
            pass
148
        try:
149
            self.open_workingtree().clone(result, basis=basis_tree)
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
150
        except (errors.NoWorkingTree, errors.NotLocalUrl):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
151
            pass
152
        return result
153
154
    def _get_basis_components(self, basis):
155
        """Retrieve the basis components that are available at basis."""
156
        if basis is None:
157
            return None, None, None
158
        try:
159
            basis_tree = basis.open_workingtree()
160
            basis_branch = basis_tree.branch
161
            basis_repo = basis_branch.repository
162
        except (errors.NoWorkingTree, errors.NotLocalUrl):
163
            basis_tree = None
164
            try:
165
                basis_branch = basis.open_branch()
166
                basis_repo = basis_branch.repository
167
            except errors.NotBranchError:
168
                basis_branch = None
169
                try:
170
                    basis_repo = basis.open_repository()
171
                except errors.NoRepositoryPresent:
172
                    basis_repo = None
173
        return basis_repo, basis_branch, basis_tree
174
1685.1.61 by Martin Pool
[broken] Change BzrDir._make_tail to use urlutils.split
175
    # TODO: This should be given a Transport, and should chdir up; otherwise
176
    # this will open a new connection.
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
177
    def _make_tail(self, url):
1685.1.61 by Martin Pool
[broken] Change BzrDir._make_tail to use urlutils.split
178
        head, tail = urlutils.split(url)
179
        if tail and tail != '.':
180
            t = bzrlib.transport.get_transport(head)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
181
            try:
1685.1.61 by Martin Pool
[broken] Change BzrDir._make_tail to use urlutils.split
182
                t.mkdir(tail)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
183
            except errors.FileExists:
184
                pass
185
1685.1.63 by Martin Pool
Small Transport fixups
186
    # TODO: Should take a Transport
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
187
    @classmethod
188
    def create(cls, base):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
189
        """Create a new BzrDir at the url 'base'.
1534.4.39 by Robert Collins
Basic BzrDir support.
190
        
191
        This will call the current default formats initialize with base
192
        as the only parameter.
193
194
        If you need a specific format, consider creating an instance
195
        of that and calling initialize().
196
        """
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
197
        if cls is not BzrDir:
198
            raise AssertionError("BzrDir.create always creates the default format, "
199
                    "not one of %r" % cls)
1685.1.62 by Martin Pool
[broken] Change BzrDir.create to use urlutils.split
200
        head, tail = urlutils.split(base)
201
        if tail and tail != '.':
202
            t = bzrlib.transport.get_transport(head)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
203
            try:
1685.1.62 by Martin Pool
[broken] Change BzrDir.create to use urlutils.split
204
                t.mkdir(tail)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
205
            except errors.FileExists:
206
                pass
1534.4.39 by Robert Collins
Basic BzrDir support.
207
        return BzrDirFormat.get_default_format().initialize(safe_unicode(base))
208
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
209
    def create_branch(self):
210
        """Create a branch in this BzrDir.
211
212
        The bzrdirs format will control what branch format is created.
213
        For more control see BranchFormatXX.create(a_bzrdir).
214
        """
215
        raise NotImplementedError(self.create_branch)
216
217
    @staticmethod
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
218
    def create_branch_and_repo(base, force_new_repo=False):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
219
        """Create a new BzrDir, Branch and Repository at the url 'base'.
220
221
        This will use the current default BzrDirFormat, and use whatever 
222
        repository format that that uses via bzrdir.create_branch and
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
223
        create_repository. If a shared repository is available that is used
224
        preferentially.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
225
226
        The created Branch object is returned.
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
227
228
        :param base: The URL to create the branch at.
229
        :param force_new_repo: If True a new repository is always created.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
230
        """
231
        bzrdir = BzrDir.create(base)
1534.6.11 by Robert Collins
Review feedback.
232
        bzrdir._find_or_create_repository(force_new_repo)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
233
        return bzrdir.create_branch()
1534.6.11 by Robert Collins
Review feedback.
234
235
    def _find_or_create_repository(self, force_new_repo):
236
        """Create a new repository if needed, returning the repository."""
237
        if force_new_repo:
238
            return self.create_repository()
239
        try:
240
            return self.find_repository()
241
        except errors.NoRepositoryPresent:
242
            return self.create_repository()
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
243
        
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
244
    @staticmethod
1558.5.3 by Aaron Bentley
Init command can produce sharing branches
245
    def create_branch_convenience(base, force_new_repo=False,
246
                                  force_new_tree=None, format=None):
1534.6.10 by Robert Collins
Finish use of repositories support.
247
        """Create a new BzrDir, Branch and Repository at the url 'base'.
248
249
        This is a convenience function - it will use an existing repository
250
        if possible, can be told explicitly whether to create a working tree or
1534.6.12 by Robert Collins
Typo found by John Meinel.
251
        not.
1534.6.10 by Robert Collins
Finish use of repositories support.
252
253
        This will use the current default BzrDirFormat, and use whatever 
254
        repository format that that uses via bzrdir.create_branch and
255
        create_repository. If a shared repository is available that is used
256
        preferentially. Whatever repository is used, its tree creation policy
257
        is followed.
258
259
        The created Branch object is returned.
260
        If a working tree cannot be made due to base not being a file:// url,
1563.1.6 by Robert Collins
Add tests for sftp push, and NonLocalTets for BzrDir.create_branch_convenience, before fixing the failure of it to work on non-local urls.
261
        no error is raised unless force_new_tree is True, in which case no 
262
        data is created on disk and NotLocalUrl is raised.
1534.6.10 by Robert Collins
Finish use of repositories support.
263
264
        :param base: The URL to create the branch at.
265
        :param force_new_repo: If True a new repository is always created.
266
        :param force_new_tree: If True or False force creation of a tree or 
267
                               prevent such creation respectively.
1558.5.3 by Aaron Bentley
Init command can produce sharing branches
268
        :param format: Override for the for the bzrdir format to create
1534.6.10 by Robert Collins
Finish use of repositories support.
269
        """
1563.1.6 by Robert Collins
Add tests for sftp push, and NonLocalTets for BzrDir.create_branch_convenience, before fixing the failure of it to work on non-local urls.
270
        if force_new_tree:
271
            # check for non local urls
272
            t = get_transport(safe_unicode(base))
273
            if not isinstance(t, LocalTransport):
274
                raise errors.NotLocalUrl(base)
1558.5.3 by Aaron Bentley
Init command can produce sharing branches
275
        if format is None:
276
            bzrdir = BzrDir.create(base)
277
        else:
278
            bzrdir = format.initialize(base)
1534.6.11 by Robert Collins
Review feedback.
279
        repo = bzrdir._find_or_create_repository(force_new_repo)
1534.6.10 by Robert Collins
Finish use of repositories support.
280
        result = bzrdir.create_branch()
281
        if force_new_tree or (repo.make_working_trees() and 
282
                              force_new_tree is None):
1563.1.6 by Robert Collins
Add tests for sftp push, and NonLocalTets for BzrDir.create_branch_convenience, before fixing the failure of it to work on non-local urls.
283
            try:
284
                bzrdir.create_workingtree()
285
            except errors.NotLocalUrl:
286
                pass
1534.6.10 by Robert Collins
Finish use of repositories support.
287
        return result
1551.8.4 by Aaron Bentley
Tweak import style
288
        
1551.8.2 by Aaron Bentley
Add create_checkout_convenience
289
    @staticmethod
1534.6.1 by Robert Collins
allow API creation of shared repositories
290
    def create_repository(base, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
291
        """Create a new BzrDir and Repository at the url 'base'.
292
293
        This will use the current default BzrDirFormat, and use whatever 
294
        repository format that that uses for bzrdirformat.create_repository.
295
1534.6.1 by Robert Collins
allow API creation of shared repositories
296
        ;param shared: Create a shared repository rather than a standalone
297
                       repository.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
298
        The Repository object is returned.
299
300
        This must be overridden as an instance method in child classes, where
301
        it should take no parameters and construct whatever repository format
302
        that child class desires.
303
        """
304
        bzrdir = BzrDir.create(base)
1841.2.1 by Jelmer Vernooij
Fix handling of `shared' parameter in BzrDir.create_repository().
305
        return bzrdir.create_repository(shared)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
306
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
307
    @staticmethod
308
    def create_standalone_workingtree(base):
309
        """Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
310
311
        'base' must be a local path or a file:// url.
312
313
        This will use the current default BzrDirFormat, and use whatever 
314
        repository format that that uses for bzrdirformat.create_workingtree,
315
        create_branch and create_repository.
316
317
        The WorkingTree object is returned.
318
        """
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
319
        t = get_transport(safe_unicode(base))
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
320
        if not isinstance(t, LocalTransport):
321
            raise errors.NotLocalUrl(base)
1534.6.10 by Robert Collins
Finish use of repositories support.
322
        bzrdir = BzrDir.create_branch_and_repo(safe_unicode(base),
323
                                               force_new_repo=True).bzrdir
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
324
        return bzrdir.create_workingtree()
325
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
326
    def create_workingtree(self, revision_id=None):
327
        """Create a working tree at this BzrDir.
328
        
329
        revision_id: create it as of this revision id.
330
        """
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
331
        raise NotImplementedError(self.create_workingtree)
332
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
333
    def find_repository(self):
334
        """Find the repository that should be used for a_bzrdir.
335
336
        This does not require a branch as we use it to find the repo for
337
        new branches as well as to hook existing branches up to their
338
        repository.
339
        """
340
        try:
341
            return self.open_repository()
342
        except errors.NoRepositoryPresent:
343
            pass
344
        next_transport = self.root_transport.clone('..')
345
        while True:
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
346
            # find the next containing bzrdir
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
347
            try:
1534.6.11 by Robert Collins
Review feedback.
348
                found_bzrdir = BzrDir.open_containing_from_transport(
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
349
                    next_transport)[0]
350
            except errors.NotBranchError:
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
351
                # none found
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
352
                raise errors.NoRepositoryPresent(self)
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
353
            # does it have a repository ?
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
354
            try:
355
                repository = found_bzrdir.open_repository()
356
            except errors.NoRepositoryPresent:
357
                next_transport = found_bzrdir.root_transport.clone('..')
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
358
                if (found_bzrdir.root_transport.base == next_transport.base):
359
                    # top of the file system
360
                    break
361
                else:
362
                    continue
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
363
            if ((found_bzrdir.root_transport.base == 
364
                 self.root_transport.base) or repository.is_shared()):
365
                return repository
366
            else:
367
                raise errors.NoRepositoryPresent(self)
368
        raise errors.NoRepositoryPresent(self)
369
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
370
    def get_branch_transport(self, branch_format):
371
        """Get the transport for use by branch format in this BzrDir.
372
373
        Note that bzr dirs that do not support format strings will raise
374
        IncompatibleFormat if the branch format they are given has
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
375
        a format string, and vice versa.
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
376
377
        If branch_format is None, the transport is returned with no 
378
        checking. if it is not None, then the returned transport is
379
        guaranteed to point to an existing directory ready for use.
380
        """
381
        raise NotImplementedError(self.get_branch_transport)
382
        
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
383
    def get_repository_transport(self, repository_format):
384
        """Get the transport for use by repository format in this BzrDir.
385
386
        Note that bzr dirs that do not support format strings will raise
387
        IncompatibleFormat if the repository format they are given has
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
388
        a format string, and vice versa.
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
389
390
        If repository_format is None, the transport is returned with no 
391
        checking. if it is not None, then the returned transport is
392
        guaranteed to point to an existing directory ready for use.
393
        """
394
        raise NotImplementedError(self.get_repository_transport)
395
        
1534.4.53 by Robert Collins
Review feedback from John Meinel.
396
    def get_workingtree_transport(self, tree_format):
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
397
        """Get the transport for use by workingtree format in this BzrDir.
398
399
        Note that bzr dirs that do not support format strings will raise
400
        IncompatibleFormat if the workingtree format they are given has
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
401
        a format string, and vice versa.
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
402
403
        If workingtree_format is None, the transport is returned with no 
404
        checking. if it is not None, then the returned transport is
405
        guaranteed to point to an existing directory ready for use.
406
        """
407
        raise NotImplementedError(self.get_workingtree_transport)
408
        
1534.4.39 by Robert Collins
Basic BzrDir support.
409
    def __init__(self, _transport, _format):
410
        """Initialize a Bzr control dir object.
411
        
412
        Only really common logic should reside here, concrete classes should be
413
        made with varying behaviours.
414
1534.4.53 by Robert Collins
Review feedback from John Meinel.
415
        :param _format: the format that is creating this BzrDir instance.
416
        :param _transport: the transport this dir is based at.
1534.4.39 by Robert Collins
Basic BzrDir support.
417
        """
418
        self._format = _format
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
419
        self.transport = _transport.clone('.bzr')
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
420
        self.root_transport = _transport
1534.4.39 by Robert Collins
Basic BzrDir support.
421
1713.1.9 by Robert Collins
Paired performance tuning of bzr add. (Robert Collins, Martin Pool).
422
    def is_control_filename(self, filename):
423
        """True if filename is the name of a path which is reserved for bzrdir's.
424
        
425
        :param filename: A filename within the root transport of this bzrdir.
426
427
        This is true IF and ONLY IF the filename is part of the namespace reserved
428
        for bzr control dirs. Currently this is the '.bzr' directory in the root
429
        of the root_transport. it is expected that plugins will need to extend
430
        this in the future - for instance to make bzr talk with svn working
431
        trees.
432
        """
433
        # this might be better on the BzrDirFormat class because it refers to 
434
        # all the possible bzrdir disk formats. 
435
        # This method is tested via the workingtree is_control_filename tests- 
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
436
        # it was extracted from WorkingTree.is_control_filename. If the methods
1713.1.9 by Robert Collins
Paired performance tuning of bzr add. (Robert Collins, Martin Pool).
437
        # contract is extended beyond the current trivial  implementation please
438
        # add new tests for it to the appropriate place.
439
        return filename == '.bzr' or filename.startswith('.bzr/')
440
1534.5.16 by Robert Collins
Review feedback.
441
    def needs_format_conversion(self, format=None):
442
        """Return true if this bzrdir needs convert_format run on it.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
443
        
444
        For instance, if the repository format is out of date but the 
445
        branch and working tree are not, this should return True.
1534.5.13 by Robert Collins
Correct buggy test.
446
447
        :param format: Optional parameter indicating a specific desired
1534.5.16 by Robert Collins
Review feedback.
448
                       format we plan to arrive at.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
449
        """
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
450
        raise NotImplementedError(self.needs_format_conversion)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
451
1534.4.39 by Robert Collins
Basic BzrDir support.
452
    @staticmethod
453
    def open_unsupported(base):
454
        """Open a branch which is not supported."""
455
        return BzrDir.open(base, _unsupported=True)
456
        
457
    @staticmethod
458
    def open(base, _unsupported=False):
1534.4.53 by Robert Collins
Review feedback from John Meinel.
459
        """Open an existing bzrdir, rooted at 'base' (url)
1534.4.39 by Robert Collins
Basic BzrDir support.
460
        
461
        _unsupported is a private parameter to the BzrDir class.
462
        """
463
        t = get_transport(base)
1908.3.1 by Carl Friedrich Bolz
Clean up some mutter() calls.
464
        # mutter("trying to open %r with transport %r", base, t)
1534.4.39 by Robert Collins
Basic BzrDir support.
465
        format = BzrDirFormat.find_format(t)
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
466
        BzrDir._check_supported(format, _unsupported)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
467
        return format.open(t, _found=True)
468
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
469
    def open_branch(self, unsupported=False):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
470
        """Open the branch object at this BzrDir if one is present.
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
471
472
        If unsupported is True, then no longer supported branch formats can
473
        still be opened.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
474
        
475
        TODO: static convenience version of this?
476
        """
477
        raise NotImplementedError(self.open_branch)
1534.4.39 by Robert Collins
Basic BzrDir support.
478
479
    @staticmethod
480
    def open_containing(url):
481
        """Open an existing branch which contains url.
482
        
1534.6.3 by Robert Collins
find_repository sufficiently robust.
483
        :param url: url to search from.
1534.6.11 by Robert Collins
Review feedback.
484
        See open_containing_from_transport for more detail.
1534.6.3 by Robert Collins
find_repository sufficiently robust.
485
        """
1534.6.11 by Robert Collins
Review feedback.
486
        return BzrDir.open_containing_from_transport(get_transport(url))
1534.6.3 by Robert Collins
find_repository sufficiently robust.
487
    
488
    @staticmethod
1534.6.11 by Robert Collins
Review feedback.
489
    def open_containing_from_transport(a_transport):
1534.6.3 by Robert Collins
find_repository sufficiently robust.
490
        """Open an existing branch which contains a_transport.base
491
492
        This probes for a branch at a_transport, and searches upwards from there.
1534.4.39 by Robert Collins
Basic BzrDir support.
493
494
        Basically we keep looking up until we find the control directory or
495
        run into the root.  If there isn't one, raises NotBranchError.
496
        If there is one and it is either an unrecognised format or an unsupported 
497
        format, UnknownFormatError or UnsupportedFormatError are raised.
498
        If there is one, it is returned, along with the unused portion of url.
1685.1.27 by John Arbash Meinel
BzrDir works in URLs, but WorkingTree works in unicode paths
499
1685.1.28 by John Arbash Meinel
Changing open_containing to always return a unicode path.
500
        :return: The BzrDir that contains the path, and a Unicode path 
501
                for the rest of the URL.
1534.4.39 by Robert Collins
Basic BzrDir support.
502
        """
503
        # this gets the normalised url back. I.e. '.' -> the full path.
1534.6.3 by Robert Collins
find_repository sufficiently robust.
504
        url = a_transport.base
1534.4.39 by Robert Collins
Basic BzrDir support.
505
        while True:
506
            try:
1534.6.3 by Robert Collins
find_repository sufficiently robust.
507
                format = BzrDirFormat.find_format(a_transport)
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
508
                BzrDir._check_supported(format, False)
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
509
                return format.open(a_transport), urlutils.unescape(a_transport.relpath(url))
1534.4.39 by Robert Collins
Basic BzrDir support.
510
            except errors.NotBranchError, e:
1685.1.60 by Martin Pool
[broken] NotBranchError should unescape the url if possible
511
                ## mutter('not a branch in: %r %s', a_transport.base, e)
512
                pass
1534.6.3 by Robert Collins
find_repository sufficiently robust.
513
            new_t = a_transport.clone('..')
514
            if new_t.base == a_transport.base:
1534.4.39 by Robert Collins
Basic BzrDir support.
515
                # reached the root, whatever that may be
516
                raise errors.NotBranchError(path=url)
1534.6.3 by Robert Collins
find_repository sufficiently robust.
517
            a_transport = new_t
1534.4.39 by Robert Collins
Basic BzrDir support.
518
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
519
    def open_repository(self, _unsupported=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
520
        """Open the repository object at this BzrDir if one is present.
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
521
522
        This will not follow the Branch object pointer - its strictly a direct
523
        open facility. Most client code should use open_branch().repository to
524
        get at a repository.
525
526
        _unsupported is a private parameter, not part of the api.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
527
        TODO: static convenience version of this?
528
        """
529
        raise NotImplementedError(self.open_repository)
530
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
531
    def open_workingtree(self, _unsupported=False):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
532
        """Open the workingtree object at this BzrDir if one is present.
533
        
534
        TODO: static convenience version of this?
535
        """
536
        raise NotImplementedError(self.open_workingtree)
537
1662.1.19 by Martin Pool
Better error message when initting existing tree
538
    def has_branch(self):
539
        """Tell if this bzrdir contains a branch.
540
        
541
        Note: if you're going to open the branch, you should just go ahead
542
        and try, and not ask permission first.  (This method just opens the 
543
        branch and discards it, and that's somewhat expensive.) 
544
        """
545
        try:
546
            self.open_branch()
547
            return True
548
        except errors.NotBranchError:
549
            return False
550
551
    def has_workingtree(self):
552
        """Tell if this bzrdir contains a working tree.
553
554
        This will still raise an exception if the bzrdir has a workingtree that
555
        is remote & inaccessible.
556
        
557
        Note: if you're going to open the working tree, you should just go ahead
558
        and try, and not ask permission first.  (This method just opens the 
559
        workingtree and discards it, and that's somewhat expensive.) 
560
        """
561
        try:
562
            self.open_workingtree()
563
            return True
564
        except errors.NoWorkingTree:
565
            return False
566
1534.6.9 by Robert Collins
sprouting into shared repositories
567
    def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
568
        """Create a copy of this bzrdir prepared for use as a new line of
569
        development.
570
571
        If urls last component does not exist, it will be created.
572
573
        Attributes related to the identity of the source branch like
574
        branch nickname will be cleaned, a working tree is created
575
        whether one existed before or not; and a local branch is always
576
        created.
577
578
        if revision_id is not None, then the clone operation may tune
579
            itself to download less data.
580
        """
581
        self._make_tail(url)
582
        result = self._format.initialize(url)
583
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
584
        try:
585
            source_branch = self.open_branch()
586
            source_repository = source_branch.repository
587
        except errors.NotBranchError:
588
            source_branch = None
589
            try:
590
                source_repository = self.open_repository()
591
            except errors.NoRepositoryPresent:
1534.6.9 by Robert Collins
sprouting into shared repositories
592
                # copy the entire basis one if there is one
593
                # but there is no repository.
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
594
                source_repository = basis_repo
1534.6.9 by Robert Collins
sprouting into shared repositories
595
        if force_new_repo:
596
            result_repo = None
597
        else:
598
            try:
599
                result_repo = result.find_repository()
600
            except errors.NoRepositoryPresent:
601
                result_repo = None
602
        if source_repository is None and result_repo is not None:
603
            pass
604
        elif source_repository is None and result_repo is None:
605
            # no repo available, make a new one
606
            result.create_repository()
607
        elif source_repository is not None and result_repo is None:
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
608
            # have source, and want to make a new target repo
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
609
            # we don't clone the repo because that preserves attributes
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
610
            # like is_shared(), and we have not yet implemented a 
611
            # repository sprout().
612
            result_repo = result.create_repository()
613
        if result_repo is not None:
1534.6.9 by Robert Collins
sprouting into shared repositories
614
            # fetch needed content into target.
615
            if basis_repo:
616
                # XXX FIXME RBC 20060214 need tests for this when the basis
617
                # is incomplete
618
                result_repo.fetch(basis_repo, revision_id=revision_id)
1910.4.10 by Andrew Bennetts
Skip various test_sprout* tests when sprouting to non-local bzrdirs that can't have working trees; plus fix a test method naming clash and the bug it revealed in bzrdir.sprout.
619
            if source_repository is not None:
620
                result_repo.fetch(source_repository, revision_id=revision_id)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
621
        if source_branch is not None:
622
            source_branch.sprout(result, revision_id=revision_id)
623
        else:
624
            result.create_branch()
1685.1.20 by John Arbash Meinel
More changes to get 'bzr branch' and 'bzr pull' to work
625
        # TODO: jam 20060426 we probably need a test in here in the
626
        #       case that the newly sprouted branch is a remote one
1558.5.8 by Aaron Bentley
Bugfix when result_repo is None
627
        if result_repo is None or result_repo.make_working_trees():
1907.1.1 by Aaron Bentley
Unshelved all changes except those related to removing RootEntry
628
            result.create_workingtree()
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
629
        return result
630
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
631
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
632
class BzrDirPreSplitOut(BzrDir):
633
    """A common class for the all-in-one formats."""
634
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
635
    def __init__(self, _transport, _format):
636
        """See BzrDir.__init__."""
637
        super(BzrDirPreSplitOut, self).__init__(_transport, _format)
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
638
        assert self._format._lock_class == TransportLock
639
        assert self._format._lock_file_name == 'branch-lock'
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
640
        self._control_files = LockableFiles(self.get_branch_transport(None),
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
641
                                            self._format._lock_file_name,
642
                                            self._format._lock_class)
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
643
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
644
    def break_lock(self):
645
        """Pre-splitout bzrdirs do not suffer from stale locks."""
646
        raise NotImplementedError(self.break_lock)
647
1534.6.8 by Robert Collins
Test the use of clone on empty bzrdir with force_new_repo.
648
    def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
649
        """See BzrDir.clone()."""
650
        from bzrlib.workingtree import WorkingTreeFormat2
651
        self._make_tail(url)
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
652
        result = self._format._initialize_for_clone(url)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
653
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
654
        self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
1692.7.9 by Martin Pool
Don't create broken standalone branches over sftp (Malone #43064)
655
        from_branch = self.open_branch()
656
        from_branch.clone(result, revision_id=revision_id)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
657
        try:
658
            self.open_workingtree().clone(result, basis=basis_tree)
659
        except errors.NotLocalUrl:
660
            # make a new one, this format always has to have one.
1563.2.38 by Robert Collins
make push preserve tree formats.
661
            try:
662
                WorkingTreeFormat2().initialize(result)
663
            except errors.NotLocalUrl:
1692.7.9 by Martin Pool
Don't create broken standalone branches over sftp (Malone #43064)
664
                # but we cannot do it for remote trees.
665
                to_branch = result.open_branch()
666
                WorkingTreeFormat2().stub_initialize_remote(to_branch.control_files)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
667
        return result
668
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
669
    def create_branch(self):
670
        """See BzrDir.create_branch."""
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
671
        return self.open_branch()
672
1534.6.1 by Robert Collins
allow API creation of shared repositories
673
    def create_repository(self, shared=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
674
        """See BzrDir.create_repository."""
1534.6.1 by Robert Collins
allow API creation of shared repositories
675
        if shared:
676
            raise errors.IncompatibleFormat('shared repository', self._format)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
677
        return self.open_repository()
678
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
679
    def create_workingtree(self, revision_id=None):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
680
        """See BzrDir.create_workingtree."""
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
681
        # this looks buggy but is not -really-
682
        # clone and sprout will have set the revision_id
683
        # and that will have set it for us, its only
684
        # specific uses of create_workingtree in isolation
685
        # that can do wonky stuff here, and that only
686
        # happens for creating checkouts, which cannot be 
687
        # done on this format anyway. So - acceptable wart.
688
        result = self.open_workingtree()
1508.1.24 by Robert Collins
Add update command for use with checkouts.
689
        if revision_id is not None:
1551.8.20 by Aaron Bentley
Fix BzrDir.create_workingtree for NULL_REVISION
690
            if revision_id == bzrlib.revision.NULL_REVISION:
691
                result.set_parent_ids([])
692
            else:
693
                result.set_parent_ids([revision_id])
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
694
        return result
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
695
696
    def get_branch_transport(self, branch_format):
697
        """See BzrDir.get_branch_transport()."""
698
        if branch_format is None:
699
            return self.transport
700
        try:
701
            branch_format.get_format_string()
702
        except NotImplementedError:
703
            return self.transport
704
        raise errors.IncompatibleFormat(branch_format, self._format)
705
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
706
    def get_repository_transport(self, repository_format):
707
        """See BzrDir.get_repository_transport()."""
708
        if repository_format is None:
709
            return self.transport
710
        try:
711
            repository_format.get_format_string()
712
        except NotImplementedError:
713
            return self.transport
714
        raise errors.IncompatibleFormat(repository_format, self._format)
715
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
716
    def get_workingtree_transport(self, workingtree_format):
717
        """See BzrDir.get_workingtree_transport()."""
718
        if workingtree_format is None:
719
            return self.transport
720
        try:
721
            workingtree_format.get_format_string()
722
        except NotImplementedError:
723
            return self.transport
724
        raise errors.IncompatibleFormat(workingtree_format, self._format)
725
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
726
    def needs_format_conversion(self, format=None):
727
        """See BzrDir.needs_format_conversion()."""
728
        # if the format is not the same as the system default,
729
        # an upgrade is needed.
730
        if format is None:
731
            format = BzrDirFormat.get_default_format()
732
        return not isinstance(self._format, format.__class__)
733
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
734
    def open_branch(self, unsupported=False):
735
        """See BzrDir.open_branch."""
736
        from bzrlib.branch import BzrBranchFormat4
737
        format = BzrBranchFormat4()
738
        self._check_supported(format, unsupported)
739
        return format.open(self, _found=True)
740
1910.4.15 by Robert Collins
Fix a minor bug in BzrDirPreSplitOut.sprout - it did not define the force_new_repo parameter - and we were not testing that it did or didn't until the test cleanup.
741
    def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
742
        """See BzrDir.sprout()."""
743
        from bzrlib.workingtree import WorkingTreeFormat2
744
        self._make_tail(url)
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
745
        result = self._format._initialize_for_clone(url)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
746
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
747
        try:
748
            self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
749
        except errors.NoRepositoryPresent:
750
            pass
751
        try:
752
            self.open_branch().sprout(result, revision_id=revision_id)
753
        except errors.NotBranchError:
754
            pass
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
755
        # we always want a working tree
756
        WorkingTreeFormat2().initialize(result)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
757
        return result
758
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
759
760
class BzrDir4(BzrDirPreSplitOut):
1508.1.25 by Robert Collins
Update per review comments.
761
    """A .bzr version 4 control object.
762
    
763
    This is a deprecated format and may be removed after sept 2006.
764
    """
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
765
1534.6.1 by Robert Collins
allow API creation of shared repositories
766
    def create_repository(self, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
767
        """See BzrDir.create_repository."""
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
768
        return self._format.repository_format.initialize(self, shared)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
769
1534.5.16 by Robert Collins
Review feedback.
770
    def needs_format_conversion(self, format=None):
771
        """Format 4 dirs are always in need of conversion."""
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
772
        return True
773
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
774
    def open_repository(self):
775
        """See BzrDir.open_repository."""
776
        from bzrlib.repository import RepositoryFormat4
777
        return RepositoryFormat4().open(self, _found=True)
778
779
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
780
class BzrDir5(BzrDirPreSplitOut):
1508.1.25 by Robert Collins
Update per review comments.
781
    """A .bzr version 5 control object.
782
783
    This is a deprecated format and may be removed after sept 2006.
784
    """
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
785
786
    def open_repository(self):
787
        """See BzrDir.open_repository."""
788
        from bzrlib.repository import RepositoryFormat5
789
        return RepositoryFormat5().open(self, _found=True)
790
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
791
    def open_workingtree(self, _unsupported=False):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
792
        """See BzrDir.create_workingtree."""
793
        from bzrlib.workingtree import WorkingTreeFormat2
794
        return WorkingTreeFormat2().open(self, _found=True)
795
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
796
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
797
class BzrDir6(BzrDirPreSplitOut):
1508.1.25 by Robert Collins
Update per review comments.
798
    """A .bzr version 6 control object.
799
800
    This is a deprecated format and may be removed after sept 2006.
801
    """
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
802
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
803
    def open_repository(self):
804
        """See BzrDir.open_repository."""
805
        from bzrlib.repository import RepositoryFormat6
806
        return RepositoryFormat6().open(self, _found=True)
807
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
808
    def open_workingtree(self, _unsupported=False):
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
809
        """See BzrDir.create_workingtree."""
810
        from bzrlib.workingtree import WorkingTreeFormat2
811
        return WorkingTreeFormat2().open(self, _found=True)
812
813
814
class BzrDirMeta1(BzrDir):
815
    """A .bzr meta version 1 control object.
816
    
817
    This is the first control object where the 
1553.5.67 by Martin Pool
doc
818
    individual aspects are really split out: there are separate repository,
819
    workingtree and branch subdirectories and any subset of the three can be
820
    present within a BzrDir.
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
821
    """
822
1534.5.16 by Robert Collins
Review feedback.
823
    def can_convert_format(self):
824
        """See BzrDir.can_convert_format()."""
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
825
        return True
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
826
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
827
    def create_branch(self):
828
        """See BzrDir.create_branch."""
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
829
        from bzrlib.branch import BranchFormat
830
        return BranchFormat.get_default_format().initialize(self)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
831
1534.6.1 by Robert Collins
allow API creation of shared repositories
832
    def create_repository(self, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
833
        """See BzrDir.create_repository."""
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
834
        return self._format.repository_format.initialize(self, shared)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
835
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
836
    def create_workingtree(self, revision_id=None):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
837
        """See BzrDir.create_workingtree."""
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
838
        from bzrlib.workingtree import WorkingTreeFormat
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
839
        return WorkingTreeFormat.get_default_format().initialize(self, revision_id)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
840
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
841
    def _get_mkdir_mode(self):
842
        """Figure out the mode to use when creating a bzrdir subdir."""
843
        temp_control = LockableFiles(self.transport, '', TransportLock)
844
        return temp_control._dir_mode
845
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
846
    def get_branch_transport(self, branch_format):
847
        """See BzrDir.get_branch_transport()."""
848
        if branch_format is None:
849
            return self.transport.clone('branch')
850
        try:
851
            branch_format.get_format_string()
852
        except NotImplementedError:
853
            raise errors.IncompatibleFormat(branch_format, self._format)
854
        try:
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
855
            self.transport.mkdir('branch', mode=self._get_mkdir_mode())
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
856
        except errors.FileExists:
857
            pass
858
        return self.transport.clone('branch')
859
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
860
    def get_repository_transport(self, repository_format):
861
        """See BzrDir.get_repository_transport()."""
862
        if repository_format is None:
863
            return self.transport.clone('repository')
864
        try:
865
            repository_format.get_format_string()
866
        except NotImplementedError:
867
            raise errors.IncompatibleFormat(repository_format, self._format)
868
        try:
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
869
            self.transport.mkdir('repository', mode=self._get_mkdir_mode())
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
870
        except errors.FileExists:
871
            pass
872
        return self.transport.clone('repository')
873
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
874
    def get_workingtree_transport(self, workingtree_format):
875
        """See BzrDir.get_workingtree_transport()."""
876
        if workingtree_format is None:
877
            return self.transport.clone('checkout')
878
        try:
879
            workingtree_format.get_format_string()
880
        except NotImplementedError:
881
            raise errors.IncompatibleFormat(workingtree_format, self._format)
882
        try:
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
883
            self.transport.mkdir('checkout', mode=self._get_mkdir_mode())
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
884
        except errors.FileExists:
885
            pass
886
        return self.transport.clone('checkout')
887
1534.5.16 by Robert Collins
Review feedback.
888
    def needs_format_conversion(self, format=None):
889
        """See BzrDir.needs_format_conversion()."""
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
890
        if format is None:
891
            format = BzrDirFormat.get_default_format()
892
        if not isinstance(self._format, format.__class__):
893
            # it is not a meta dir format, conversion is needed.
894
            return True
895
        # we might want to push this down to the repository?
896
        try:
897
            if not isinstance(self.open_repository()._format,
898
                              format.repository_format.__class__):
899
                # the repository needs an upgrade.
900
                return True
901
        except errors.NoRepositoryPresent:
902
            pass
903
        # currently there are no other possible conversions for meta1 formats.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
904
        return False
905
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
906
    def open_branch(self, unsupported=False):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
907
        """See BzrDir.open_branch."""
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
908
        from bzrlib.branch import BranchFormat
909
        format = BranchFormat.find_format(self)
910
        self._check_supported(format, unsupported)
911
        return format.open(self, _found=True)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
912
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
913
    def open_repository(self, unsupported=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
914
        """See BzrDir.open_repository."""
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
915
        from bzrlib.repository import RepositoryFormat
916
        format = RepositoryFormat.find_format(self)
917
        self._check_supported(format, unsupported)
918
        return format.open(self, _found=True)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
919
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
920
    def open_workingtree(self, unsupported=False):
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
921
        """See BzrDir.open_workingtree."""
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
922
        from bzrlib.workingtree import WorkingTreeFormat
923
        format = WorkingTreeFormat.find_format(self)
924
        self._check_supported(format, unsupported)
925
        return format.open(self, _found=True)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
926
1534.4.39 by Robert Collins
Basic BzrDir support.
927
928
class BzrDirFormat(object):
929
    """An encapsulation of the initialization and open routines for a format.
930
931
    Formats provide three things:
932
     * An initialization routine,
933
     * a format string,
934
     * an open routine.
935
936
    Formats are placed in an dict by their format string for reference 
937
    during bzrdir opening. These should be subclasses of BzrDirFormat
938
    for consistency.
939
940
    Once a format is deprecated, just deprecate the initialize and open
941
    methods on the format class. Do not deprecate the object, as the 
942
    object will be created every system load.
943
    """
944
945
    _default_format = None
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
946
    """The default format used for new .bzr dirs."""
1534.4.39 by Robert Collins
Basic BzrDir support.
947
948
    _formats = {}
949
    """The known formats."""
950
1733.1.7 by Jelmer Vernooij
Change set of control dir formats to list.
951
    _control_formats = []
1733.1.6 by Jelmer Vernooij
Fix a couple of minor issues after review by Martin.
952
    """The registered control formats - .bzr, ....
953
    
1733.1.7 by Jelmer Vernooij
Change set of control dir formats to list.
954
    This is a list of BzrDirFormat objects.
1733.1.6 by Jelmer Vernooij
Fix a couple of minor issues after review by Martin.
955
    """
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
956
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
957
    _lock_file_name = 'branch-lock'
958
959
    # _lock_class must be set in subclasses to the lock type, typ.
960
    # TransportLock or LockDir
961
1534.4.39 by Robert Collins
Basic BzrDir support.
962
    @classmethod
963
    def find_format(klass, transport):
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
964
        """Return the format present at transport."""
965
        for format in klass._control_formats:
966
            try:
967
                return format.probe_transport(transport)
968
            except errors.NotBranchError:
969
                # this format does not find a control dir here.
970
                pass
971
        raise errors.NotBranchError(path=transport.base)
972
973
    @classmethod
974
    def probe_transport(klass, transport):
975
        """Return the .bzrdir style transport present at URL."""
1534.4.39 by Robert Collins
Basic BzrDir support.
976
        try:
977
            format_string = transport.get(".bzr/branch-format").read()
1733.2.3 by Michael Ellerman
Don't coallesce try blocks, it can lead to confusing exceptions.
978
        except errors.NoSuchFile:
979
            raise errors.NotBranchError(path=transport.base)
980
981
        try:
1534.4.39 by Robert Collins
Basic BzrDir support.
982
            return klass._formats[format_string]
983
        except KeyError:
1740.5.6 by Martin Pool
Clean up many exception classes.
984
            raise errors.UnknownFormatError(format=format_string)
1534.4.39 by Robert Collins
Basic BzrDir support.
985
986
    @classmethod
987
    def get_default_format(klass):
988
        """Return the current default format."""
989
        return klass._default_format
990
991
    def get_format_string(self):
992
        """Return the ASCII format string that identifies this format."""
993
        raise NotImplementedError(self.get_format_string)
994
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
995
    def get_format_description(self):
996
        """Return the short description for this format."""
997
        raise NotImplementedError(self.get_format_description)
998
1534.5.16 by Robert Collins
Review feedback.
999
    def get_converter(self, format=None):
1000
        """Return the converter to use to convert bzrdirs needing converts.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1001
1002
        This returns a bzrlib.bzrdir.Converter object.
1003
1004
        This should return the best upgrader to step this format towards the
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1005
        current default format. In the case of plugins we can/should provide
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1006
        some means for them to extend the range of returnable converters.
1534.5.13 by Robert Collins
Correct buggy test.
1007
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1008
        :param format: Optional format to override the default format of the 
1534.5.13 by Robert Collins
Correct buggy test.
1009
                       library.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1010
        """
1534.5.16 by Robert Collins
Review feedback.
1011
        raise NotImplementedError(self.get_converter)
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1012
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1013
    def initialize(self, url):
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1014
        """Create a bzr control dir at this url and return an opened copy.
1015
        
1016
        Subclasses should typically override initialize_on_transport
1017
        instead of this method.
1018
        """
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1019
        return self.initialize_on_transport(get_transport(url))
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1020
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1021
    def initialize_on_transport(self, transport):
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1022
        """Initialize a new bzrdir in the base directory of a Transport."""
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1023
        # Since we don't have a .bzr directory, inherit the
1534.4.39 by Robert Collins
Basic BzrDir support.
1024
        # mode from the root directory
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1025
        temp_control = LockableFiles(transport, '', TransportLock)
1534.4.39 by Robert Collins
Basic BzrDir support.
1026
        temp_control._transport.mkdir('.bzr',
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
1027
                                      # FIXME: RBC 20060121 don't peek under
1534.4.39 by Robert Collins
Basic BzrDir support.
1028
                                      # the covers
1029
                                      mode=temp_control._dir_mode)
1030
        file_mode = temp_control._file_mode
1031
        del temp_control
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1032
        mutter('created control directory in ' + transport.base)
1033
        control = transport.clone('.bzr')
1534.4.39 by Robert Collins
Basic BzrDir support.
1034
        utf8_files = [('README', 
1035
                       "This is a Bazaar-NG control directory.\n"
1036
                       "Do not change any files in this directory.\n"),
1037
                      ('branch-format', self.get_format_string()),
1038
                      ]
1039
        # NB: no need to escape relative paths that are url safe.
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1040
        control_files = LockableFiles(control, self._lock_file_name, 
1041
                                      self._lock_class)
1553.5.60 by Martin Pool
New LockableFiles.create_lock() method
1042
        control_files.create_lock()
1534.4.39 by Robert Collins
Basic BzrDir support.
1043
        control_files.lock_write()
1044
        try:
1045
            for file, content in utf8_files:
1046
                control_files.put_utf8(file, content)
1047
        finally:
1048
            control_files.unlock()
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1049
        return self.open(transport, _found=True)
1534.4.39 by Robert Collins
Basic BzrDir support.
1050
1051
    def is_supported(self):
1052
        """Is this format supported?
1053
1054
        Supported formats must be initializable and openable.
1055
        Unsupported formats may not support initialization or committing or 
1056
        some other features depending on the reason for not being supported.
1057
        """
1058
        return True
1059
1733.1.3 by Robert Collins
Extend the test suite to run bzrdir conformance tests on non .bzr based control dirs.
1060
    @classmethod
1061
    def known_formats(klass):
1062
        """Return all the known formats.
1063
        
1064
        Concrete formats should override _known_formats.
1065
        """
1733.1.6 by Jelmer Vernooij
Fix a couple of minor issues after review by Martin.
1066
        # There is double indirection here to make sure that control 
1067
        # formats used by more than one dir format will only be probed 
1068
        # once. This can otherwise be quite expensive for remote connections.
1733.1.3 by Robert Collins
Extend the test suite to run bzrdir conformance tests on non .bzr based control dirs.
1069
        result = set()
1070
        for format in klass._control_formats:
1071
            result.update(format._known_formats())
1072
        return result
1073
    
1074
    @classmethod
1075
    def _known_formats(klass):
1076
        """Return the known format instances for this control format."""
1077
        return set(klass._formats.values())
1078
1534.4.39 by Robert Collins
Basic BzrDir support.
1079
    def open(self, transport, _found=False):
1080
        """Return an instance of this format for the dir transport points at.
1081
        
1082
        _found is a private parameter, do not use it.
1083
        """
1084
        if not _found:
1085
            assert isinstance(BzrDirFormat.find_format(transport),
1086
                              self.__class__)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1087
        return self._open(transport)
1088
1089
    def _open(self, transport):
1090
        """Template method helper for opening BzrDirectories.
1091
1092
        This performs the actual open and any additional logic or parameter
1093
        passing.
1094
        """
1095
        raise NotImplementedError(self._open)
1534.4.39 by Robert Collins
Basic BzrDir support.
1096
1097
    @classmethod
1098
    def register_format(klass, format):
1099
        klass._formats[format.get_format_string()] = format
1100
1101
    @classmethod
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1102
    def register_control_format(klass, format):
1103
        """Register a format that does not use '.bzrdir' for its control dir.
1104
1105
        TODO: This should be pulled up into a 'ControlDirFormat' base class
1106
        which BzrDirFormat can inherit from, and renamed to register_format 
1107
        there. It has been done without that for now for simplicity of
1108
        implementation.
1109
        """
1733.1.7 by Jelmer Vernooij
Change set of control dir formats to list.
1110
        klass._control_formats.append(format)
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1111
1112
    @classmethod
1534.4.39 by Robert Collins
Basic BzrDir support.
1113
    def set_default_format(klass, format):
1114
        klass._default_format = format
1115
1534.5.1 by Robert Collins
Give info some reasonable output and tests.
1116
    def __str__(self):
1117
        return self.get_format_string()[:-1]
1118
1534.4.39 by Robert Collins
Basic BzrDir support.
1119
    @classmethod
1120
    def unregister_format(klass, format):
1121
        assert klass._formats[format.get_format_string()] is format
1122
        del klass._formats[format.get_format_string()]
1123
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1124
    @classmethod
1125
    def unregister_control_format(klass, format):
1126
        klass._control_formats.remove(format)
1127
1128
1129
# register BzrDirFormat as a control format
1130
BzrDirFormat.register_control_format(BzrDirFormat)
1131
1534.4.39 by Robert Collins
Basic BzrDir support.
1132
1133
class BzrDirFormat4(BzrDirFormat):
1134
    """Bzr dir format 4.
1135
1136
    This format is a combined format for working tree, branch and repository.
1137
    It has:
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1138
     - Format 1 working trees [always]
1139
     - Format 4 branches [always]
1140
     - Format 4 repositories [always]
1534.4.39 by Robert Collins
Basic BzrDir support.
1141
1142
    This format is deprecated: it indexes texts using a text it which is
1143
    removed in format 5; write support for this format has been removed.
1144
    """
1145
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1146
    _lock_class = TransportLock
1147
1534.4.39 by Robert Collins
Basic BzrDir support.
1148
    def get_format_string(self):
1149
        """See BzrDirFormat.get_format_string()."""
1150
        return "Bazaar-NG branch, format 0.0.4\n"
1151
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1152
    def get_format_description(self):
1153
        """See BzrDirFormat.get_format_description()."""
1154
        return "All-in-one format 4"
1155
1534.5.16 by Robert Collins
Review feedback.
1156
    def get_converter(self, format=None):
1157
        """See BzrDirFormat.get_converter()."""
1534.5.13 by Robert Collins
Correct buggy test.
1158
        # there is one and only one upgrade path here.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1159
        return ConvertBzrDir4To5()
1160
        
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1161
    def initialize_on_transport(self, transport):
1534.4.39 by Robert Collins
Basic BzrDir support.
1162
        """Format 4 branches cannot be created."""
1163
        raise errors.UninitializableFormat(self)
1164
1165
    def is_supported(self):
1166
        """Format 4 is not supported.
1167
1168
        It is not supported because the model changed from 4 to 5 and the
1169
        conversion logic is expensive - so doing it on the fly was not 
1170
        feasible.
1171
        """
1172
        return False
1173
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1174
    def _open(self, transport):
1175
        """See BzrDirFormat._open."""
1176
        return BzrDir4(transport, self)
1177
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1178
    def __return_repository_format(self):
1179
        """Circular import protection."""
1180
        from bzrlib.repository import RepositoryFormat4
1181
        return RepositoryFormat4(self)
1182
    repository_format = property(__return_repository_format)
1183
1534.4.39 by Robert Collins
Basic BzrDir support.
1184
1185
class BzrDirFormat5(BzrDirFormat):
1186
    """Bzr control format 5.
1187
1188
    This format is a combined format for working tree, branch and repository.
1189
    It has:
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1190
     - Format 2 working trees [always] 
1191
     - Format 4 branches [always] 
1534.4.53 by Robert Collins
Review feedback from John Meinel.
1192
     - Format 5 repositories [always]
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1193
       Unhashed stores in the repository.
1534.4.39 by Robert Collins
Basic BzrDir support.
1194
    """
1195
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1196
    _lock_class = TransportLock
1197
1534.4.39 by Robert Collins
Basic BzrDir support.
1198
    def get_format_string(self):
1199
        """See BzrDirFormat.get_format_string()."""
1200
        return "Bazaar-NG branch, format 5\n"
1201
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1202
    def get_format_description(self):
1203
        """See BzrDirFormat.get_format_description()."""
1204
        return "All-in-one format 5"
1205
1534.5.16 by Robert Collins
Review feedback.
1206
    def get_converter(self, format=None):
1207
        """See BzrDirFormat.get_converter()."""
1534.5.13 by Robert Collins
Correct buggy test.
1208
        # there is one and only one upgrade path here.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1209
        return ConvertBzrDir5To6()
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1210
1211
    def _initialize_for_clone(self, url):
1212
        return self.initialize_on_transport(get_transport(url), _cloning=True)
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1213
        
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1214
    def initialize_on_transport(self, transport, _cloning=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1215
        """Format 5 dirs always have working tree, branch and repository.
1216
        
1217
        Except when they are being cloned.
1218
        """
1219
        from bzrlib.branch import BzrBranchFormat4
1220
        from bzrlib.repository import RepositoryFormat5
1221
        from bzrlib.workingtree import WorkingTreeFormat2
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1222
        result = (super(BzrDirFormat5, self).initialize_on_transport(transport))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1223
        RepositoryFormat5().initialize(result, _internal=True)
1224
        if not _cloning:
1910.5.1 by Andrew Bennetts
Make some old formats create at least a stub working tree rather than incomplete bzrdirs, and change some tests to use the test suite transport rather than hard-coded to local-only.
1225
            branch = BzrBranchFormat4().initialize(result)
1226
            try:
1227
                WorkingTreeFormat2().initialize(result)
1228
            except errors.NotLocalUrl:
1229
                # Even though we can't access the working tree, we need to
1230
                # create its control files.
1231
                WorkingTreeFormat2().stub_initialize_remote(branch.control_files)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1232
        return result
1233
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1234
    def _open(self, transport):
1235
        """See BzrDirFormat._open."""
1236
        return BzrDir5(transport, self)
1237
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1238
    def __return_repository_format(self):
1239
        """Circular import protection."""
1240
        from bzrlib.repository import RepositoryFormat5
1241
        return RepositoryFormat5(self)
1242
    repository_format = property(__return_repository_format)
1243
1534.4.39 by Robert Collins
Basic BzrDir support.
1244
1245
class BzrDirFormat6(BzrDirFormat):
1246
    """Bzr control format 6.
1247
1248
    This format is a combined format for working tree, branch and repository.
1249
    It has:
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1250
     - Format 2 working trees [always] 
1251
     - Format 4 branches [always] 
1252
     - Format 6 repositories [always]
1534.4.39 by Robert Collins
Basic BzrDir support.
1253
    """
1254
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1255
    _lock_class = TransportLock
1256
1534.4.39 by Robert Collins
Basic BzrDir support.
1257
    def get_format_string(self):
1258
        """See BzrDirFormat.get_format_string()."""
1259
        return "Bazaar-NG branch, format 6\n"
1260
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1261
    def get_format_description(self):
1262
        """See BzrDirFormat.get_format_description()."""
1263
        return "All-in-one format 6"
1264
1534.5.16 by Robert Collins
Review feedback.
1265
    def get_converter(self, format=None):
1266
        """See BzrDirFormat.get_converter()."""
1534.5.13 by Robert Collins
Correct buggy test.
1267
        # there is one and only one upgrade path here.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1268
        return ConvertBzrDir6ToMeta()
1269
        
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1270
    def _initialize_for_clone(self, url):
1271
        return self.initialize_on_transport(get_transport(url), _cloning=True)
1272
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1273
    def initialize_on_transport(self, transport, _cloning=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1274
        """Format 6 dirs always have working tree, branch and repository.
1275
        
1276
        Except when they are being cloned.
1277
        """
1278
        from bzrlib.branch import BzrBranchFormat4
1279
        from bzrlib.repository import RepositoryFormat6
1280
        from bzrlib.workingtree import WorkingTreeFormat2
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1281
        result = super(BzrDirFormat6, self).initialize_on_transport(transport)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1282
        RepositoryFormat6().initialize(result, _internal=True)
1283
        if not _cloning:
1910.5.1 by Andrew Bennetts
Make some old formats create at least a stub working tree rather than incomplete bzrdirs, and change some tests to use the test suite transport rather than hard-coded to local-only.
1284
            branch = BzrBranchFormat4().initialize(result)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1285
            try:
1286
                WorkingTreeFormat2().initialize(result)
1287
            except errors.NotLocalUrl:
1910.5.1 by Andrew Bennetts
Make some old formats create at least a stub working tree rather than incomplete bzrdirs, and change some tests to use the test suite transport rather than hard-coded to local-only.
1288
                # Even though we can't access the working tree, we need to
1289
                # create its control files.
1290
                WorkingTreeFormat2().stub_initialize_remote(branch.control_files)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1291
        return result
1292
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1293
    def _open(self, transport):
1294
        """See BzrDirFormat._open."""
1295
        return BzrDir6(transport, self)
1296
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1297
    def __return_repository_format(self):
1298
        """Circular import protection."""
1299
        from bzrlib.repository import RepositoryFormat6
1300
        return RepositoryFormat6(self)
1301
    repository_format = property(__return_repository_format)
1302
1534.4.39 by Robert Collins
Basic BzrDir support.
1303
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1304
class BzrDirMetaFormat1(BzrDirFormat):
1305
    """Bzr meta control format 1
1306
1307
    This is the first format with split out working tree, branch and repository
1308
    disk storage.
1309
    It has:
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1310
     - Format 3 working trees [optional]
1311
     - Format 5 branches [optional]
1312
     - Format 7 repositories [optional]
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1313
    """
1314
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1315
    _lock_class = LockDir
1316
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1317
    def get_converter(self, format=None):
1318
        """See BzrDirFormat.get_converter()."""
1319
        if format is None:
1320
            format = BzrDirFormat.get_default_format()
1321
        if not isinstance(self, format.__class__):
1322
            # converting away from metadir is not implemented
1323
            raise NotImplementedError(self.get_converter)
1324
        return ConvertMetaToMeta(format)
1325
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1326
    def get_format_string(self):
1327
        """See BzrDirFormat.get_format_string()."""
1328
        return "Bazaar-NG meta directory, format 1\n"
1329
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1330
    def get_format_description(self):
1331
        """See BzrDirFormat.get_format_description()."""
1332
        return "Meta directory format 1"
1333
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1334
    def _open(self, transport):
1335
        """See BzrDirFormat._open."""
1336
        return BzrDirMeta1(transport, self)
1337
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1338
    def __return_repository_format(self):
1339
        """Circular import protection."""
1340
        if getattr(self, '_repository_format', None):
1341
            return self._repository_format
1342
        from bzrlib.repository import RepositoryFormat
1343
        return RepositoryFormat.get_default_format()
1344
1345
    def __set_repository_format(self, value):
1346
        """Allow changint the repository format for metadir formats."""
1347
        self._repository_format = value
1553.5.72 by Martin Pool
Clean up test for Branch5 lockdirs
1348
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1349
    repository_format = property(__return_repository_format, __set_repository_format)
1350
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1351
1534.4.39 by Robert Collins
Basic BzrDir support.
1352
BzrDirFormat.register_format(BzrDirFormat4())
1353
BzrDirFormat.register_format(BzrDirFormat5())
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1354
BzrDirFormat.register_format(BzrDirFormat6())
1355
__default_format = BzrDirMetaFormat1()
1534.4.39 by Robert Collins
Basic BzrDir support.
1356
BzrDirFormat.register_format(__default_format)
1357
BzrDirFormat.set_default_format(__default_format)
1358
1359
1360
class BzrDirTestProviderAdapter(object):
1361
    """A tool to generate a suite testing multiple bzrdir formats at once.
1362
1363
    This is done by copying the test once for each transport and injecting
1364
    the transport_server, transport_readonly_server, and bzrdir_format
1365
    classes into each copy. Each copy is also given a new id() to make it
1366
    easy to identify.
1367
    """
1368
1369
    def __init__(self, transport_server, transport_readonly_server, formats):
1370
        self._transport_server = transport_server
1371
        self._transport_readonly_server = transport_readonly_server
1372
        self._formats = formats
1373
    
1374
    def adapt(self, test):
1375
        result = TestSuite()
1376
        for format in self._formats:
1377
            new_test = deepcopy(test)
1378
            new_test.transport_server = self._transport_server
1379
            new_test.transport_readonly_server = self._transport_readonly_server
1380
            new_test.bzrdir_format = format
1381
            def make_new_test_id():
1382
                new_id = "%s(%s)" % (new_test.id(), format.__class__.__name__)
1383
                return lambda: new_id
1384
            new_test.id = make_new_test_id()
1385
            result.addTest(new_test)
1386
        return result
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1387
1388
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1389
class Converter(object):
1390
    """Converts a disk format object from one format to another."""
1391
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1392
    def convert(self, to_convert, pb):
1393
        """Perform the conversion of to_convert, giving feedback via pb.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1394
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1395
        :param to_convert: The disk object to convert.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1396
        :param pb: a progress bar to use for progress information.
1397
        """
1398
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1399
    def step(self, message):
1400
        """Update the pb by a step."""
1401
        self.count +=1
1402
        self.pb.update(message, self.count, self.total)
1403
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1404
1405
class ConvertBzrDir4To5(Converter):
1406
    """Converts format 4 bzr dirs to format 5."""
1407
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1408
    def __init__(self):
1409
        super(ConvertBzrDir4To5, self).__init__()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1410
        self.converted_revs = set()
1411
        self.absent_revisions = set()
1412
        self.text_count = 0
1413
        self.revisions = {}
1414
        
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1415
    def convert(self, to_convert, pb):
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1416
        """See Converter.convert()."""
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1417
        self.bzrdir = to_convert
1418
        self.pb = pb
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1419
        self.pb.note('starting upgrade from format 4 to 5')
1420
        if isinstance(self.bzrdir.transport, LocalTransport):
1421
            self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
1422
        self._convert_to_weaves()
1423
        return BzrDir.open(self.bzrdir.root_transport.base)
1424
1425
    def _convert_to_weaves(self):
1426
        self.pb.note('note: upgrade may be faster if all store files are ungzipped first')
1427
        try:
1428
            # TODO permissions
1429
            stat = self.bzrdir.transport.stat('weaves')
1430
            if not S_ISDIR(stat.st_mode):
1431
                self.bzrdir.transport.delete('weaves')
1432
                self.bzrdir.transport.mkdir('weaves')
1433
        except errors.NoSuchFile:
1434
            self.bzrdir.transport.mkdir('weaves')
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
1435
        # deliberately not a WeaveFile as we want to build it up slowly.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1436
        self.inv_weave = Weave('inventory')
1437
        # holds in-memory weaves for all files
1438
        self.text_weaves = {}
1439
        self.bzrdir.transport.delete('branch-format')
1440
        self.branch = self.bzrdir.open_branch()
1441
        self._convert_working_inv()
1442
        rev_history = self.branch.revision_history()
1443
        # to_read is a stack holding the revisions we still need to process;
1444
        # appending to it adds new highest-priority revisions
1445
        self.known_revisions = set(rev_history)
1446
        self.to_read = rev_history[-1:]
1447
        while self.to_read:
1448
            rev_id = self.to_read.pop()
1449
            if (rev_id not in self.revisions
1450
                and rev_id not in self.absent_revisions):
1451
                self._load_one_rev(rev_id)
1452
        self.pb.clear()
1453
        to_import = self._make_order()
1454
        for i, rev_id in enumerate(to_import):
1455
            self.pb.update('converting revision', i, len(to_import))
1456
            self._convert_one_rev(rev_id)
1457
        self.pb.clear()
1458
        self._write_all_weaves()
1459
        self._write_all_revs()
1460
        self.pb.note('upgraded to weaves:')
1461
        self.pb.note('  %6d revisions and inventories', len(self.revisions))
1462
        self.pb.note('  %6d revisions not present', len(self.absent_revisions))
1463
        self.pb.note('  %6d texts', self.text_count)
1464
        self._cleanup_spare_files_after_format4()
1465
        self.branch.control_files.put_utf8('branch-format', BzrDirFormat5().get_format_string())
1466
1467
    def _cleanup_spare_files_after_format4(self):
1468
        # FIXME working tree upgrade foo.
1469
        for n in 'merged-patches', 'pending-merged-patches':
1470
            try:
1471
                ## assert os.path.getsize(p) == 0
1472
                self.bzrdir.transport.delete(n)
1473
            except errors.NoSuchFile:
1474
                pass
1475
        self.bzrdir.transport.delete_tree('inventory-store')
1476
        self.bzrdir.transport.delete_tree('text-store')
1477
1478
    def _convert_working_inv(self):
1479
        inv = serializer_v4.read_inventory(self.branch.control_files.get('inventory'))
1732.1.26 by John Arbash Meinel
Switch to using bzrlib.xml5.serializer_v5 so that a plugin can override it if we want
1480
        new_inv_xml = bzrlib.xml5.serializer_v5.write_inventory_to_string(inv)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1481
        # FIXME inventory is a working tree change.
1482
        self.branch.control_files.put('inventory', new_inv_xml)
1483
1484
    def _write_all_weaves(self):
1485
        controlweaves = WeaveStore(self.bzrdir.transport, prefixed=False)
1486
        weave_transport = self.bzrdir.transport.clone('weaves')
1487
        weaves = WeaveStore(weave_transport, prefixed=False)
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
1488
        transaction = WriteTransaction()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1489
1490
        try:
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
1491
            i = 0
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1492
            for file_id, file_weave in self.text_weaves.items():
1493
                self.pb.update('writing weave', i, len(self.text_weaves))
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
1494
                weaves._put_weave(file_id, file_weave, transaction)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1495
                i += 1
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
1496
            self.pb.update('inventory', 0, 1)
1497
            controlweaves._put_weave('inventory', self.inv_weave, transaction)
1498
            self.pb.update('inventory', 1, 1)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1499
        finally:
1500
            self.pb.clear()
1501
1502
    def _write_all_revs(self):
1503
        """Write all revisions out in new form."""
1504
        self.bzrdir.transport.delete_tree('revision-store')
1505
        self.bzrdir.transport.mkdir('revision-store')
1506
        revision_transport = self.bzrdir.transport.clone('revision-store')
1507
        # TODO permissions
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1508
        _revision_store = TextRevisionStore(TextStore(revision_transport,
1509
                                                      prefixed=False,
1510
                                                      compressed=True))
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1511
        try:
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
1512
            transaction = bzrlib.transactions.WriteTransaction()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1513
            for i, rev_id in enumerate(self.converted_revs):
1514
                self.pb.update('write revision', i, len(self.converted_revs))
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1515
                _revision_store.add_revision(self.revisions[rev_id], transaction)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1516
        finally:
1517
            self.pb.clear()
1518
            
1519
    def _load_one_rev(self, rev_id):
1520
        """Load a revision object into memory.
1521
1522
        Any parents not either loaded or abandoned get queued to be
1523
        loaded."""
1524
        self.pb.update('loading revision',
1525
                       len(self.revisions),
1526
                       len(self.known_revisions))
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1527
        if not self.branch.repository.has_revision(rev_id):
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1528
            self.pb.clear()
1529
            self.pb.note('revision {%s} not present in branch; '
1530
                         'will be converted as a ghost',
1531
                         rev_id)
1532
            self.absent_revisions.add(rev_id)
1533
        else:
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1534
            rev = self.branch.repository._revision_store.get_revision(rev_id,
1535
                self.branch.repository.get_transaction())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1536
            for parent_id in rev.parent_ids:
1537
                self.known_revisions.add(parent_id)
1538
                self.to_read.append(parent_id)
1539
            self.revisions[rev_id] = rev
1540
1541
    def _load_old_inventory(self, rev_id):
1542
        assert rev_id not in self.converted_revs
1543
        old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
1544
        inv = serializer_v4.read_inventory_from_string(old_inv_xml)
1545
        rev = self.revisions[rev_id]
1546
        if rev.inventory_sha1:
1547
            assert rev.inventory_sha1 == sha_string(old_inv_xml), \
1548
                'inventory sha mismatch for {%s}' % rev_id
1549
        return inv
1550
1551
    def _load_updated_inventory(self, rev_id):
1552
        assert rev_id in self.converted_revs
1553
        inv_xml = self.inv_weave.get_text(rev_id)
1732.1.26 by John Arbash Meinel
Switch to using bzrlib.xml5.serializer_v5 so that a plugin can override it if we want
1554
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(inv_xml)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1555
        return inv
1556
1557
    def _convert_one_rev(self, rev_id):
1558
        """Convert revision and all referenced objects to new format."""
1559
        rev = self.revisions[rev_id]
1560
        inv = self._load_old_inventory(rev_id)
1561
        present_parents = [p for p in rev.parent_ids
1562
                           if p not in self.absent_revisions]
1563
        self._convert_revision_contents(rev, inv, present_parents)
1564
        self._store_new_weave(rev, inv, present_parents)
1565
        self.converted_revs.add(rev_id)
1566
1567
    def _store_new_weave(self, rev, inv, present_parents):
1568
        # the XML is now updated with text versions
1569
        if __debug__:
1907.1.8 by Aaron Bentley
Remove is_root
1570
            entries = inv.iter_entries()
1571
            entries.next()
1572
            for path, ie in entries:
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1573
                assert hasattr(ie, 'revision'), \
1574
                    'no revision on {%s} in {%s}' % \
1575
                    (file_id, rev.revision_id)
1732.1.26 by John Arbash Meinel
Switch to using bzrlib.xml5.serializer_v5 so that a plugin can override it if we want
1576
        new_inv_xml = bzrlib.xml5.serializer_v5.write_inventory_to_string(inv)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1577
        new_inv_sha1 = sha_string(new_inv_xml)
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1578
        self.inv_weave.add_lines(rev.revision_id, 
1579
                                 present_parents,
1580
                                 new_inv_xml.splitlines(True))
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1581
        rev.inventory_sha1 = new_inv_sha1
1582
1583
    def _convert_revision_contents(self, rev, inv, present_parents):
1584
        """Convert all the files within a revision.
1585
1586
        Also upgrade the inventory to refer to the text revision ids."""
1587
        rev_id = rev.revision_id
1588
        mutter('converting texts of revision {%s}',
1589
               rev_id)
1590
        parent_invs = map(self._load_updated_inventory, present_parents)
1907.1.8 by Aaron Bentley
Remove is_root
1591
        entries = inv.iter_entries()
1592
        entries.next()
1593
        for path, ie in entries:
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1594
            self._convert_file_version(rev, ie, parent_invs)
1595
1596
    def _convert_file_version(self, rev, ie, parent_invs):
1597
        """Convert one version of one file.
1598
1599
        The file needs to be added into the weave if it is a merge
1600
        of >=2 parents or if it's changed from its parent.
1601
        """
1602
        file_id = ie.file_id
1603
        rev_id = rev.revision_id
1604
        w = self.text_weaves.get(file_id)
1605
        if w is None:
1606
            w = Weave(file_id)
1607
            self.text_weaves[file_id] = w
1608
        text_changed = False
1596.2.20 by Robert Collins
optimise commit to only access weaves for merged, or altered files during commit.
1609
        previous_entries = ie.find_previous_heads(parent_invs,
1610
                                                  None,
1611
                                                  None,
1612
                                                  entry_vf=w)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1613
        for old_revision in previous_entries:
1614
                # if this fails, its a ghost ?
1907.1.1 by Aaron Bentley
Unshelved all changes except those related to removing RootEntry
1615
                assert old_revision in self.converted_revs 
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1616
        self.snapshot_ie(previous_entries, ie, w, rev_id)
1617
        del ie.text_id
1618
        assert getattr(ie, 'revision', None) is not None
1619
1620
    def snapshot_ie(self, previous_revisions, ie, w, rev_id):
1621
        # TODO: convert this logic, which is ~= snapshot to
1622
        # a call to:. This needs the path figured out. rather than a work_tree
1623
        # a v4 revision_tree can be given, or something that looks enough like
1624
        # one to give the file content to the entry if it needs it.
1625
        # and we need something that looks like a weave store for snapshot to 
1626
        # save against.
1627
        #ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
1628
        if len(previous_revisions) == 1:
1629
            previous_ie = previous_revisions.values()[0]
1630
            if ie._unchanged(previous_ie):
1631
                ie.revision = previous_ie.revision
1632
                return
1633
        if ie.has_text():
1634
            text = self.branch.repository.text_store.get(ie.text_id)
1635
            file_lines = text.readlines()
1636
            assert sha_strings(file_lines) == ie.text_sha1
1637
            assert sum(map(len, file_lines)) == ie.text_size
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
1638
            w.add_lines(rev_id, previous_revisions, file_lines)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1639
            self.text_count += 1
1640
        else:
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
1641
            w.add_lines(rev_id, previous_revisions, [])
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1642
        ie.revision = rev_id
1643
1644
    def _make_order(self):
1645
        """Return a suitable order for importing revisions.
1646
1647
        The order must be such that an revision is imported after all
1648
        its (present) parents.
1649
        """
1650
        todo = set(self.revisions.keys())
1651
        done = self.absent_revisions.copy()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1652
        order = []
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1653
        while todo:
1654
            # scan through looking for a revision whose parents
1655
            # are all done
1656
            for rev_id in sorted(list(todo)):
1657
                rev = self.revisions[rev_id]
1658
                parent_ids = set(rev.parent_ids)
1659
                if parent_ids.issubset(done):
1660
                    # can take this one now
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1661
                    order.append(rev_id)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1662
                    todo.remove(rev_id)
1663
                    done.add(rev_id)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1664
        return order
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1665
1666
1667
class ConvertBzrDir5To6(Converter):
1668
    """Converts format 5 bzr dirs to format 6."""
1669
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1670
    def convert(self, to_convert, pb):
1671
        """See Converter.convert()."""
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1672
        self.bzrdir = to_convert
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1673
        self.pb = pb
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1674
        self.pb.note('starting upgrade from format 5 to 6')
1675
        self._convert_to_prefixed()
1676
        return BzrDir.open(self.bzrdir.root_transport.base)
1677
1678
    def _convert_to_prefixed(self):
1608.2.1 by Martin Pool
[merge] Storage filename escaping
1679
        from bzrlib.store import TransportStore
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1680
        self.bzrdir.transport.delete('branch-format')
1681
        for store_name in ["weaves", "revision-store"]:
1608.2.1 by Martin Pool
[merge] Storage filename escaping
1682
            self.pb.note("adding prefixes to %s" % store_name)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1683
            store_transport = self.bzrdir.transport.clone(store_name)
1608.2.1 by Martin Pool
[merge] Storage filename escaping
1684
            store = TransportStore(store_transport, prefixed=True)
1608.1.1 by Martin Pool
[patch] LocalTransport.list_dir should return url-quoted strings (ddaa)
1685
            for urlfilename in store_transport.list_dir('.'):
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
1686
                filename = urlutils.unescape(urlfilename)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1687
                if (filename.endswith(".weave") or
1688
                    filename.endswith(".gz") or
1689
                    filename.endswith(".sig")):
1690
                    file_id = os.path.splitext(filename)[0]
1691
                else:
1692
                    file_id = filename
1608.2.1 by Martin Pool
[merge] Storage filename escaping
1693
                prefix_dir = store.hash_prefix(file_id)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1694
                # FIXME keep track of the dirs made RBC 20060121
1695
                try:
1696
                    store_transport.move(filename, prefix_dir + '/' + filename)
1697
                except errors.NoSuchFile: # catches missing dirs strangely enough
1698
                    store_transport.mkdir(prefix_dir)
1699
                    store_transport.move(filename, prefix_dir + '/' + filename)
1700
        self.bzrdir._control_files.put_utf8('branch-format', BzrDirFormat6().get_format_string())
1701
1702
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1703
class ConvertBzrDir6ToMeta(Converter):
1704
    """Converts format 6 bzr dirs to metadirs."""
1705
1706
    def convert(self, to_convert, pb):
1707
        """See Converter.convert()."""
1708
        self.bzrdir = to_convert
1709
        self.pb = pb
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1710
        self.count = 0
1711
        self.total = 20 # the steps we know about
1712
        self.garbage_inventories = []
1713
1534.5.13 by Robert Collins
Correct buggy test.
1714
        self.pb.note('starting upgrade from format 6 to metadir')
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1715
        self.bzrdir._control_files.put_utf8('branch-format', "Converting to format 6")
1716
        # its faster to move specific files around than to open and use the apis...
1717
        # first off, nuke ancestry.weave, it was never used.
1718
        try:
1719
            self.step('Removing ancestry.weave')
1720
            self.bzrdir.transport.delete('ancestry.weave')
1721
        except errors.NoSuchFile:
1722
            pass
1723
        # find out whats there
1724
        self.step('Finding branch files')
1666.1.3 by Robert Collins
Fix and test upgrades from bzrdir 6 over SFTP.
1725
        last_revision = self.bzrdir.open_branch().last_revision()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1726
        bzrcontents = self.bzrdir.transport.list_dir('.')
1727
        for name in bzrcontents:
1728
            if name.startswith('basis-inventory.'):
1729
                self.garbage_inventories.append(name)
1730
        # create new directories for repository, working tree and branch
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
1731
        self.dir_mode = self.bzrdir._control_files._dir_mode
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1732
        self.file_mode = self.bzrdir._control_files._file_mode
1733
        repository_names = [('inventory.weave', True),
1734
                            ('revision-store', True),
1735
                            ('weaves', True)]
1736
        self.step('Upgrading repository  ')
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
1737
        self.bzrdir.transport.mkdir('repository', mode=self.dir_mode)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1738
        self.make_lock('repository')
1739
        # we hard code the formats here because we are converting into
1740
        # the meta format. The meta format upgrader can take this to a 
1741
        # future format within each component.
1742
        self.put_format('repository', bzrlib.repository.RepositoryFormat7())
1743
        for entry in repository_names:
1744
            self.move_entry('repository', entry)
1745
1746
        self.step('Upgrading branch      ')
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
1747
        self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1748
        self.make_lock('branch')
1749
        self.put_format('branch', bzrlib.branch.BzrBranchFormat5())
1750
        branch_files = [('revision-history', True),
1751
                        ('branch-name', True),
1752
                        ('parent', False)]
1753
        for entry in branch_files:
1754
            self.move_entry('branch', entry)
1755
1756
        checkout_files = [('pending-merges', True),
1757
                          ('inventory', True),
1758
                          ('stat-cache', False)]
1959.3.1 by John Arbash Meinel
David Allouche: bzr upgrade should work if there is no working tree
1759
        # If a mandatory checkout file is not present, the branch does not have
1760
        # a functional checkout. Do not create a checkout in the converted
1761
        # branch.
1762
        for name, mandatory in checkout_files:
1763
            if mandatory and name not in bzrcontents:
1764
                has_checkout = False
1765
                break
1766
        else:
1767
            has_checkout = True
1768
        if not has_checkout:
1769
            self.pb.note('No working tree.')
1770
            # If some checkout files are there, we may as well get rid of them.
1771
            for name, mandatory in checkout_files:
1772
                if name in bzrcontents:
1773
                    self.bzrdir.transport.delete(name)
1774
        else:
1775
            self.step('Upgrading working tree')
1776
            self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
1777
            self.make_lock('checkout')
1778
            self.put_format(
1779
                'checkout', bzrlib.workingtree.WorkingTreeFormat3())
1780
            self.bzrdir.transport.delete_multi(
1781
                self.garbage_inventories, self.pb)
1782
            for entry in checkout_files:
1783
                self.move_entry('checkout', entry)
1784
            if last_revision is not None:
1785
                self.bzrdir._control_files.put_utf8(
1786
                    'checkout/last-revision', last_revision)
1787
        self.bzrdir._control_files.put_utf8(
1788
            'branch-format', BzrDirMetaFormat1().get_format_string())
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1789
        return BzrDir.open(self.bzrdir.root_transport.base)
1790
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1791
    def make_lock(self, name):
1792
        """Make a lock for the new control dir name."""
1793
        self.step('Make %s lock' % name)
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
1794
        ld = LockDir(self.bzrdir.transport, 
1795
                     '%s/lock' % name,
1796
                     file_modebits=self.file_mode,
1797
                     dir_modebits=self.dir_mode)
1798
        ld.create()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1799
1800
    def move_entry(self, new_dir, entry):
1801
        """Move then entry name into new_dir."""
1802
        name = entry[0]
1803
        mandatory = entry[1]
1804
        self.step('Moving %s' % name)
1805
        try:
1806
            self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
1807
        except errors.NoSuchFile:
1808
            if mandatory:
1809
                raise
1810
1811
    def put_format(self, dirname, format):
1812
        self.bzrdir._control_files.put_utf8('%s/format' % dirname, format.get_format_string())
1813
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1814
1815
class ConvertMetaToMeta(Converter):
1816
    """Converts the components of metadirs."""
1817
1818
    def __init__(self, target_format):
1819
        """Create a metadir to metadir converter.
1820
1821
        :param target_format: The final metadir format that is desired.
1822
        """
1823
        self.target_format = target_format
1824
1825
    def convert(self, to_convert, pb):
1826
        """See Converter.convert()."""
1827
        self.bzrdir = to_convert
1828
        self.pb = pb
1829
        self.count = 0
1830
        self.total = 1
1831
        self.step('checking repository format')
1832
        try:
1833
            repo = self.bzrdir.open_repository()
1834
        except errors.NoRepositoryPresent:
1835
            pass
1836
        else:
1837
            if not isinstance(repo._format, self.target_format.repository_format.__class__):
1838
                from bzrlib.repository import CopyConverter
1839
                self.pb.note('starting repository conversion')
1840
                converter = CopyConverter(self.target_format.repository_format)
1841
                converter.convert(repo, pb)
1842
        return to_convert