/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
1
# Copyright (C) 2006-2010 Canonical Ltd
2
#
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.
7
#
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.
12
#
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
17
"""Weave-era BzrDir formats."""
18
6379.6.3 by Jelmer Vernooij
Use absolute_import.
19
from __future__ import absolute_import
20
6670.4.1 by Jelmer Vernooij
Update imports.
21
from ...bzr.bzrdir import (
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
22
    BzrDir,
23
    BzrDirFormat,
24
    BzrDirMetaFormat1,
25
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
26
from ...controldir import (
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
27
    ControlDir,
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
28
    Converter,
6734.1.13 by Jelmer Vernooij
Move MustHaveWorkingTree.
29
    MustHaveWorkingTree,
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
30
    format_registry,
31
    )
6734.1.13 by Jelmer Vernooij
Move MustHaveWorkingTree.
32
from ... import (
33
    errors,
34
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
35
from ...lazy_import import lazy_import
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
36
lazy_import(globals(), """
37
import os
38
import warnings
39
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
40
from breezy import (
6734.1.11 by Jelmer Vernooij
Move UnstackableBranchFormat.
41
    branch as _mod_branch,,
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
42
    graph,
43
    lockable_files,
44
    lockdir,
45
    osutils,
46
    revision as _mod_revision,
47
    trace,
48
    ui,
49
    urlutils,
6670.4.1 by Jelmer Vernooij
Update imports.
50
    )
51
from breezy.bzr import (
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
52
    versionedfile,
53
    weave,
54
    xml5,
55
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
56
from breezy.i18n import gettext
6686.2.1 by Jelmer Vernooij
Move breezy.store to breezy.plugins.weave_fmt, its only user.
57
from breezy.plugins.weave_fmt.store.versioned import VersionedFileStore
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
58
from breezy.transactions import WriteTransaction
59
from breezy.transport import (
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
60
    get_transport,
61
    local,
62
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
63
from breezy.plugins.weave_fmt import xml4
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
64
""")
65
66
67
class BzrDirFormatAllInOne(BzrDirFormat):
68
    """Common class for formats before meta-dirs."""
69
70
    fixed_components = True
71
72
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
73
        create_prefix=False, force_new_repo=False, stacked_on=None,
74
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
75
        shared_repo=False):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
76
        """See ControlDir.initialize_on_transport_ex."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
77
        require_stacking = (stacked_on is not None)
78
        # Format 5 cannot stack, but we've been asked to - actually init
79
        # a Meta1Dir
80
        if require_stacking:
81
            format = BzrDirMetaFormat1()
82
            return format.initialize_on_transport_ex(transport,
83
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
84
                force_new_repo=force_new_repo, stacked_on=stacked_on,
85
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
86
                make_working_trees=make_working_trees, shared_repo=shared_repo)
87
        return BzrDirFormat.initialize_on_transport_ex(self, transport,
88
            use_existing_dir=use_existing_dir, create_prefix=create_prefix,
89
            force_new_repo=force_new_repo, stacked_on=stacked_on,
90
            stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
91
            make_working_trees=make_working_trees, shared_repo=shared_repo)
92
6213.1.18 by Jelmer Vernooij
Fix some more tests.
93
    @classmethod
94
    def from_string(cls, format_string):
95
        if format_string != cls.get_format_string():
96
            raise AssertionError("unexpected format string %r" % format_string)
6213.1.19 by Jelmer Vernooij
Fix tests.
97
        return cls()
6213.1.18 by Jelmer Vernooij
Fix some more tests.
98
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
99
100
class BzrDirFormat5(BzrDirFormatAllInOne):
101
    """Bzr control format 5.
102
103
    This format is a combined format for working tree, branch and repository.
104
    It has:
105
     - Format 2 working trees [always]
106
     - Format 4 branches [always]
107
     - Format 5 repositories [always]
108
       Unhashed stores in the repository.
109
    """
110
111
    _lock_class = lockable_files.TransportLock
112
5712.4.7 by Jelmer Vernooij
More fixes.
113
    def __eq__(self, other):
6619.3.18 by Jelmer Vernooij
Run 2to3 idioms fixer.
114
        return isinstance(self, type(other))
5712.4.7 by Jelmer Vernooij
More fixes.
115
6213.1.16 by Jelmer Vernooij
Feature support in repository.
116
    @classmethod
117
    def get_format_string(cls):
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
118
        """See BzrDirFormat.get_format_string()."""
119
        return "Bazaar-NG branch, format 5\n"
120
121
    def get_branch_format(self):
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
122
        from .branch import BzrBranchFormat4
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
123
        return BzrBranchFormat4()
124
125
    def get_format_description(self):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
126
        """See ControlDirFormat.get_format_description()."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
127
        return "All-in-one format 5"
128
129
    def get_converter(self, format=None):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
130
        """See ControlDirFormat.get_converter()."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
131
        # there is one and only one upgrade path here.
132
        return ConvertBzrDir5To6()
133
134
    def _initialize_for_clone(self, url):
135
        return self.initialize_on_transport(get_transport(url), _cloning=True)
136
137
    def initialize_on_transport(self, transport, _cloning=False):
138
        """Format 5 dirs always have working tree, branch and repository.
139
140
        Except when they are being cloned.
141
        """
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
142
        from .branch import BzrBranchFormat4
143
        from .repository import RepositoryFormat5
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
144
        result = (super(BzrDirFormat5, self).initialize_on_transport(transport))
145
        RepositoryFormat5().initialize(result, _internal=True)
146
        if not _cloning:
147
            branch = BzrBranchFormat4().initialize(result)
148
            result._init_workingtree()
149
        return result
150
151
    def network_name(self):
152
        return self.get_format_string()
153
154
    def _open(self, transport):
155
        """See BzrDirFormat._open."""
156
        return BzrDir5(transport, self)
157
158
    def __return_repository_format(self):
159
        """Circular import protection."""
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
160
        from .repository import RepositoryFormat5
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
161
        return RepositoryFormat5()
162
    repository_format = property(__return_repository_format)
163
164
165
class BzrDirFormat6(BzrDirFormatAllInOne):
166
    """Bzr control format 6.
167
168
    This format is a combined format for working tree, branch and repository.
169
    It has:
170
     - Format 2 working trees [always]
171
     - Format 4 branches [always]
172
     - Format 6 repositories [always]
173
    """
174
175
    _lock_class = lockable_files.TransportLock
176
5712.4.7 by Jelmer Vernooij
More fixes.
177
    def __eq__(self, other):
6619.3.18 by Jelmer Vernooij
Run 2to3 idioms fixer.
178
        return isinstance(self, type(other))
5712.4.7 by Jelmer Vernooij
More fixes.
179
6213.1.16 by Jelmer Vernooij
Feature support in repository.
180
    @classmethod
181
    def get_format_string(cls):
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
182
        """See BzrDirFormat.get_format_string()."""
183
        return "Bazaar-NG branch, format 6\n"
184
185
    def get_format_description(self):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
186
        """See ControlDirFormat.get_format_description()."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
187
        return "All-in-one format 6"
188
189
    def get_branch_format(self):
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
190
        from .branch import BzrBranchFormat4
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
191
        return BzrBranchFormat4()
192
193
    def get_converter(self, format=None):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
194
        """See ControlDirFormat.get_converter()."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
195
        # there is one and only one upgrade path here.
196
        return ConvertBzrDir6ToMeta()
197
198
    def _initialize_for_clone(self, url):
199
        return self.initialize_on_transport(get_transport(url), _cloning=True)
200
201
    def initialize_on_transport(self, transport, _cloning=False):
202
        """Format 6 dirs always have working tree, branch and repository.
203
204
        Except when they are being cloned.
205
        """
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
206
        from .branch import BzrBranchFormat4
207
        from .repository import RepositoryFormat6
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
208
        result = super(BzrDirFormat6, self).initialize_on_transport(transport)
209
        RepositoryFormat6().initialize(result, _internal=True)
210
        if not _cloning:
211
            branch = BzrBranchFormat4().initialize(result)
212
            result._init_workingtree()
213
        return result
214
215
    def network_name(self):
216
        return self.get_format_string()
217
218
    def _open(self, transport):
219
        """See BzrDirFormat._open."""
220
        return BzrDir6(transport, self)
221
222
    def __return_repository_format(self):
223
        """Circular import protection."""
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
224
        from .repository import RepositoryFormat6
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
225
        return RepositoryFormat6()
226
    repository_format = property(__return_repository_format)
227
228
229
class ConvertBzrDir4To5(Converter):
230
    """Converts format 4 bzr dirs to format 5."""
231
232
    def __init__(self):
233
        super(ConvertBzrDir4To5, self).__init__()
234
        self.converted_revs = set()
235
        self.absent_revisions = set()
236
        self.text_count = 0
237
        self.revisions = {}
238
239
    def convert(self, to_convert, pb):
240
        """See Converter.convert()."""
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
241
        self.controldir = to_convert
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
242
        if pb is not None:
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
243
            warnings.warn(gettext("pb parameter to convert() is deprecated"))
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
244
        self.pb = ui.ui_factory.nested_progress_bar()
245
        try:
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
246
            ui.ui_factory.note(gettext('starting upgrade from format 4 to 5'))
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
247
            if isinstance(self.controldir.transport, local.LocalTransport):
248
                self.controldir.get_workingtree_transport(None).delete('stat-cache')
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
249
            self._convert_to_weaves()
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
250
            return ControlDir.open(self.controldir.user_url)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
251
        finally:
252
            self.pb.finished()
253
254
    def _convert_to_weaves(self):
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
255
        ui.ui_factory.note(gettext(
256
          'note: upgrade may be faster if all store files are ungzipped first'))
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
257
        try:
258
            # TODO permissions
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
259
            stat = self.controldir.transport.stat('weaves')
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
260
            if not S_ISDIR(stat.st_mode):
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
261
                self.controldir.transport.delete('weaves')
262
                self.controldir.transport.mkdir('weaves')
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
263
        except errors.NoSuchFile:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
264
            self.controldir.transport.mkdir('weaves')
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
265
        # deliberately not a WeaveFile as we want to build it up slowly.
266
        self.inv_weave = weave.Weave('inventory')
267
        # holds in-memory weaves for all files
268
        self.text_weaves = {}
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
269
        self.controldir.transport.delete('branch-format')
270
        self.branch = self.controldir.open_branch()
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
271
        self._convert_working_inv()
6165.4.25 by Jelmer Vernooij
Fix plugin use of revision_history.
272
        rev_history = self.branch._revision_history()
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
273
        # to_read is a stack holding the revisions we still need to process;
274
        # appending to it adds new highest-priority revisions
275
        self.known_revisions = set(rev_history)
276
        self.to_read = rev_history[-1:]
277
        while self.to_read:
278
            rev_id = self.to_read.pop()
279
            if (rev_id not in self.revisions
280
                and rev_id not in self.absent_revisions):
281
                self._load_one_rev(rev_id)
282
        self.pb.clear()
283
        to_import = self._make_order()
284
        for i, rev_id in enumerate(to_import):
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
285
            self.pb.update(gettext('converting revision'), i, len(to_import))
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
286
            self._convert_one_rev(rev_id)
287
        self.pb.clear()
288
        self._write_all_weaves()
289
        self._write_all_revs()
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
290
        ui.ui_factory.note(gettext('upgraded to weaves:'))
6150.3.10 by Jonathan Riddell
do not include space padding in gettext()
291
        ui.ui_factory.note('  ' + gettext('%6d revisions and inventories') %
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
292
                                                        len(self.revisions))
6150.3.10 by Jonathan Riddell
do not include space padding in gettext()
293
        ui.ui_factory.note('  ' + gettext('%6d revisions not present') %
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
294
                                                    len(self.absent_revisions))
6150.3.10 by Jonathan Riddell
do not include space padding in gettext()
295
        ui.ui_factory.note('  ' + gettext('%6d texts') % self.text_count)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
296
        self._cleanup_spare_files_after_format4()
297
        self.branch._transport.put_bytes(
298
            'branch-format',
299
            BzrDirFormat5().get_format_string(),
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
300
            mode=self.controldir._get_file_mode())
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
301
302
    def _cleanup_spare_files_after_format4(self):
303
        # FIXME working tree upgrade foo.
304
        for n in 'merged-patches', 'pending-merged-patches':
305
            try:
306
                ## assert os.path.getsize(p) == 0
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
307
                self.controldir.transport.delete(n)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
308
            except errors.NoSuchFile:
309
                pass
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
310
        self.controldir.transport.delete_tree('inventory-store')
311
        self.controldir.transport.delete_tree('text-store')
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
312
313
    def _convert_working_inv(self):
314
        inv = xml4.serializer_v4.read_inventory(
315
                self.branch._transport.get('inventory'))
316
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv, working=True)
317
        self.branch._transport.put_bytes('inventory', new_inv_xml,
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
318
            mode=self.controldir._get_file_mode())
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
319
320
    def _write_all_weaves(self):
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
321
        controlweaves = VersionedFileStore(self.controldir.transport, prefixed=False,
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
322
            versionedfile_class=weave.WeaveFile)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
323
        weave_transport = self.controldir.transport.clone('weaves')
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
324
        weaves = VersionedFileStore(weave_transport, prefixed=False,
325
                versionedfile_class=weave.WeaveFile)
326
        transaction = WriteTransaction()
327
328
        try:
329
            i = 0
330
            for file_id, file_weave in self.text_weaves.items():
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
331
                self.pb.update(gettext('writing weave'), i,
332
                                                        len(self.text_weaves))
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
333
                weaves._put_weave(file_id, file_weave, transaction)
334
                i += 1
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
335
            self.pb.update(gettext('inventory'), 0, 1)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
336
            controlweaves._put_weave('inventory', self.inv_weave, transaction)
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
337
            self.pb.update(gettext('inventory'), 1, 1)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
338
        finally:
339
            self.pb.clear()
340
341
    def _write_all_revs(self):
342
        """Write all revisions out in new form."""
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
343
        self.controldir.transport.delete_tree('revision-store')
344
        self.controldir.transport.mkdir('revision-store')
345
        revision_transport = self.controldir.transport.clone('revision-store')
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
346
        # TODO permissions
6670.4.12 by Jelmer Vernooij
Move inventorytree to breezy.bzr.
347
        from ...bzr.xml5 import serializer_v5
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
348
        from .repository import RevisionTextStore
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
349
        revision_store = RevisionTextStore(revision_transport,
350
            serializer_v5, False, versionedfile.PrefixMapper(),
351
            lambda:True, lambda:True)
352
        try:
353
            for i, rev_id in enumerate(self.converted_revs):
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
354
                self.pb.update(gettext('write revision'), i,
355
                                                len(self.converted_revs))
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
356
                text = serializer_v5.write_revision_to_string(
357
                    self.revisions[rev_id])
358
                key = (rev_id,)
359
                revision_store.add_lines(key, None, osutils.split_lines(text))
360
        finally:
361
            self.pb.clear()
362
363
    def _load_one_rev(self, rev_id):
364
        """Load a revision object into memory.
365
366
        Any parents not either loaded or abandoned get queued to be
367
        loaded."""
6150.3.11 by Jonathan Riddell
syntax fixes
368
        self.pb.update(gettext('loading revision'),
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
369
                       len(self.revisions),
370
                       len(self.known_revisions))
371
        if not self.branch.repository.has_revision(rev_id):
372
            self.pb.clear()
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
373
            ui.ui_factory.note(gettext('revision {%s} not present in branch; '
374
                         'will be converted as a ghost') %
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
375
                         rev_id)
376
            self.absent_revisions.add(rev_id)
377
        else:
378
            rev = self.branch.repository.get_revision(rev_id)
379
            for parent_id in rev.parent_ids:
380
                self.known_revisions.add(parent_id)
381
                self.to_read.append(parent_id)
382
            self.revisions[rev_id] = rev
383
384
    def _load_old_inventory(self, rev_id):
385
        f = self.branch.repository.inventory_store.get(rev_id)
386
        try:
387
            old_inv_xml = f.read()
388
        finally:
389
            f.close()
390
        inv = xml4.serializer_v4.read_inventory_from_string(old_inv_xml)
391
        inv.revision_id = rev_id
392
        rev = self.revisions[rev_id]
393
        return inv
394
395
    def _load_updated_inventory(self, rev_id):
396
        inv_xml = self.inv_weave.get_text(rev_id)
397
        inv = xml5.serializer_v5.read_inventory_from_string(inv_xml, rev_id)
398
        return inv
399
400
    def _convert_one_rev(self, rev_id):
401
        """Convert revision and all referenced objects to new format."""
402
        rev = self.revisions[rev_id]
403
        inv = self._load_old_inventory(rev_id)
404
        present_parents = [p for p in rev.parent_ids
405
                           if p not in self.absent_revisions]
406
        self._convert_revision_contents(rev, inv, present_parents)
407
        self._store_new_inv(rev, inv, present_parents)
408
        self.converted_revs.add(rev_id)
409
410
    def _store_new_inv(self, rev, inv, present_parents):
411
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv)
412
        new_inv_sha1 = osutils.sha_string(new_inv_xml)
413
        self.inv_weave.add_lines(rev.revision_id,
414
                                 present_parents,
415
                                 new_inv_xml.splitlines(True))
416
        rev.inventory_sha1 = new_inv_sha1
417
418
    def _convert_revision_contents(self, rev, inv, present_parents):
419
        """Convert all the files within a revision.
420
421
        Also upgrade the inventory to refer to the text revision ids."""
422
        rev_id = rev.revision_id
423
        trace.mutter('converting texts of revision {%s}', rev_id)
6631.3.1 by Martin
Run 2to3 map fixer and refactor after
424
        parent_invs = list(map(self._load_updated_inventory, present_parents))
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
425
        entries = inv.iter_entries()
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
426
        next(entries)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
427
        for path, ie in entries:
428
            self._convert_file_version(rev, ie, parent_invs)
429
430
    def _convert_file_version(self, rev, ie, parent_invs):
431
        """Convert one version of one file.
432
433
        The file needs to be added into the weave if it is a merge
434
        of >=2 parents or if it's changed from its parent.
435
        """
436
        file_id = ie.file_id
437
        rev_id = rev.revision_id
438
        w = self.text_weaves.get(file_id)
439
        if w is None:
440
            w = weave.Weave(file_id)
441
            self.text_weaves[file_id] = w
442
        text_changed = False
443
        parent_candiate_entries = ie.parent_candidates(parent_invs)
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
444
        heads = graph.Graph(self).heads(parent_candiate_entries)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
445
        # XXX: Note that this is unordered - and this is tolerable because
446
        # the previous code was also unordered.
447
        previous_entries = dict((head, parent_candiate_entries[head]) for head
448
            in heads)
449
        self.snapshot_ie(previous_entries, ie, w, rev_id)
450
451
    def get_parent_map(self, revision_ids):
452
        """See graph.StackedParentsProvider.get_parent_map"""
453
        return dict((revision_id, self.revisions[revision_id])
454
                    for revision_id in revision_ids
455
                     if revision_id in self.revisions)
456
457
    def snapshot_ie(self, previous_revisions, ie, w, rev_id):
458
        # TODO: convert this logic, which is ~= snapshot to
459
        # a call to:. This needs the path figured out. rather than a work_tree
460
        # a v4 revision_tree can be given, or something that looks enough like
461
        # one to give the file content to the entry if it needs it.
462
        # and we need something that looks like a weave store for snapshot to
463
        # save against.
464
        #ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
465
        if len(previous_revisions) == 1:
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
466
            previous_ie = next(iter(previous_revisions.values()))
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
467
            if ie._unchanged(previous_ie):
468
                ie.revision = previous_ie.revision
469
                return
470
        if ie.has_text():
471
            f = self.branch.repository._text_store.get(ie.text_id)
472
            try:
473
                file_lines = f.readlines()
474
            finally:
475
                f.close()
476
            w.add_lines(rev_id, previous_revisions, file_lines)
477
            self.text_count += 1
478
        else:
479
            w.add_lines(rev_id, previous_revisions, [])
480
        ie.revision = rev_id
481
482
    def _make_order(self):
483
        """Return a suitable order for importing revisions.
484
485
        The order must be such that an revision is imported after all
486
        its (present) parents.
487
        """
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
488
        todo = set(self.revisions)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
489
        done = self.absent_revisions.copy()
490
        order = []
491
        while todo:
492
            # scan through looking for a revision whose parents
493
            # are all done
494
            for rev_id in sorted(list(todo)):
495
                rev = self.revisions[rev_id]
496
                parent_ids = set(rev.parent_ids)
497
                if parent_ids.issubset(done):
498
                    # can take this one now
499
                    order.append(rev_id)
500
                    todo.remove(rev_id)
501
                    done.add(rev_id)
502
        return order
503
504
505
class ConvertBzrDir5To6(Converter):
506
    """Converts format 5 bzr dirs to format 6."""
507
508
    def convert(self, to_convert, pb):
509
        """See Converter.convert()."""
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
510
        self.controldir = to_convert
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
511
        pb = ui.ui_factory.nested_progress_bar()
512
        try:
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
513
            ui.ui_factory.note(gettext('starting upgrade from format 5 to 6'))
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
514
            self._convert_to_prefixed()
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
515
            return ControlDir.open(self.controldir.user_url)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
516
        finally:
517
            pb.finished()
518
519
    def _convert_to_prefixed(self):
6686.2.1 by Jelmer Vernooij
Move breezy.store to breezy.plugins.weave_fmt, its only user.
520
        from .store import TransportStore
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
521
        self.controldir.transport.delete('branch-format')
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
522
        for store_name in ["weaves", "revision-store"]:
6150.3.6 by Jonathan Riddell
more gettext()ing
523
            ui.ui_factory.note(gettext("adding prefixes to %s") % store_name)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
524
            store_transport = self.controldir.transport.clone(store_name)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
525
            store = TransportStore(store_transport, prefixed=True)
526
            for urlfilename in store_transport.list_dir('.'):
527
                filename = urlutils.unescape(urlfilename)
528
                if (filename.endswith(".weave") or
529
                    filename.endswith(".gz") or
530
                    filename.endswith(".sig")):
531
                    file_id, suffix = os.path.splitext(filename)
532
                else:
533
                    file_id = filename
534
                    suffix = ''
535
                new_name = store._mapper.map((file_id,)) + suffix
536
                # FIXME keep track of the dirs made RBC 20060121
537
                try:
538
                    store_transport.move(filename, new_name)
539
                except errors.NoSuchFile: # catches missing dirs strangely enough
540
                    store_transport.mkdir(osutils.dirname(new_name))
541
                    store_transport.move(filename, new_name)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
542
        self.controldir.transport.put_bytes(
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
543
            'branch-format',
544
            BzrDirFormat6().get_format_string(),
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
545
            mode=self.controldir._get_file_mode())
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
546
547
548
class ConvertBzrDir6ToMeta(Converter):
549
    """Converts format 6 bzr dirs to metadirs."""
550
551
    def convert(self, to_convert, pb):
552
        """See Converter.convert()."""
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
553
        from .repository import RepositoryFormat7
6670.4.1 by Jelmer Vernooij
Update imports.
554
        from ...bzr.fullhistory import BzrBranchFormat5
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
555
        self.controldir = to_convert
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
556
        self.pb = ui.ui_factory.nested_progress_bar()
557
        self.count = 0
558
        self.total = 20 # the steps we know about
559
        self.garbage_inventories = []
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
560
        self.dir_mode = self.controldir._get_dir_mode()
561
        self.file_mode = self.controldir._get_file_mode()
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
562
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
563
        ui.ui_factory.note(gettext('starting upgrade from format 6 to metadir'))
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
564
        self.controldir.transport.put_bytes(
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
565
                'branch-format',
566
                "Converting to format 6",
567
                mode=self.file_mode)
568
        # its faster to move specific files around than to open and use the apis...
569
        # first off, nuke ancestry.weave, it was never used.
570
        try:
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
571
            self.step(gettext('Removing ancestry.weave'))
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
572
            self.controldir.transport.delete('ancestry.weave')
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
573
        except errors.NoSuchFile:
574
            pass
575
        # find out whats there
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
576
        self.step(gettext('Finding branch files'))
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
577
        last_revision = self.controldir.open_branch().last_revision()
578
        bzrcontents = self.controldir.transport.list_dir('.')
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
579
        for name in bzrcontents:
580
            if name.startswith('basis-inventory.'):
581
                self.garbage_inventories.append(name)
582
        # create new directories for repository, working tree and branch
583
        repository_names = [('inventory.weave', True),
584
                            ('revision-store', True),
585
                            ('weaves', True)]
6150.3.10 by Jonathan Riddell
do not include space padding in gettext()
586
        self.step(gettext('Upgrading repository') + '  ')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
587
        self.controldir.transport.mkdir('repository', mode=self.dir_mode)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
588
        self.make_lock('repository')
589
        # we hard code the formats here because we are converting into
590
        # the meta format. The meta format upgrader can take this to a
591
        # future format within each component.
592
        self.put_format('repository', RepositoryFormat7())
593
        for entry in repository_names:
594
            self.move_entry('repository', entry)
595
6150.3.10 by Jonathan Riddell
do not include space padding in gettext()
596
        self.step(gettext('Upgrading branch') + '      ')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
597
        self.controldir.transport.mkdir('branch', mode=self.dir_mode)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
598
        self.make_lock('branch')
599
        self.put_format('branch', BzrBranchFormat5())
600
        branch_files = [('revision-history', True),
601
                        ('branch-name', True),
602
                        ('parent', False)]
603
        for entry in branch_files:
604
            self.move_entry('branch', entry)
605
606
        checkout_files = [('pending-merges', True),
607
                          ('inventory', True),
608
                          ('stat-cache', False)]
609
        # If a mandatory checkout file is not present, the branch does not have
610
        # a functional checkout. Do not create a checkout in the converted
611
        # branch.
612
        for name, mandatory in checkout_files:
613
            if mandatory and name not in bzrcontents:
614
                has_checkout = False
615
                break
616
        else:
617
            has_checkout = True
618
        if not has_checkout:
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
619
            ui.ui_factory.note(gettext('No working tree.'))
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
620
            # If some checkout files are there, we may as well get rid of them.
621
            for name, mandatory in checkout_files:
622
                if name in bzrcontents:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
623
                    self.controldir.transport.delete(name)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
624
        else:
6670.4.1 by Jelmer Vernooij
Update imports.
625
            from ...bzr.workingtree_3 import WorkingTreeFormat3
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
626
            self.step(gettext('Upgrading working tree'))
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
627
            self.controldir.transport.mkdir('checkout', mode=self.dir_mode)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
628
            self.make_lock('checkout')
629
            self.put_format(
630
                'checkout', WorkingTreeFormat3())
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
631
            self.controldir.transport.delete_multi(
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
632
                self.garbage_inventories, self.pb)
633
            for entry in checkout_files:
634
                self.move_entry('checkout', entry)
635
            if last_revision is not None:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
636
                self.controldir.transport.put_bytes(
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
637
                    'checkout/last-revision', last_revision)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
638
        self.controldir.transport.put_bytes(
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
639
            'branch-format',
640
            BzrDirMetaFormat1().get_format_string(),
641
            mode=self.file_mode)
642
        self.pb.finished()
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
643
        return ControlDir.open(self.controldir.user_url)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
644
645
    def make_lock(self, name):
646
        """Make a lock for the new control dir name."""
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
647
        self.step(gettext('Make %s lock') % name)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
648
        ld = lockdir.LockDir(self.controldir.transport,
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
649
                             '%s/lock' % name,
650
                             file_modebits=self.file_mode,
651
                             dir_modebits=self.dir_mode)
652
        ld.create()
653
654
    def move_entry(self, new_dir, entry):
655
        """Move then entry name into new_dir."""
656
        name = entry[0]
657
        mandatory = entry[1]
6150.3.2 by Jonathan Riddell
gettext() on weave plugin
658
        self.step(gettext('Moving %s') % name)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
659
        try:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
660
            self.controldir.transport.move(name, '%s/%s' % (new_dir, name))
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
661
        except errors.NoSuchFile:
662
            if mandatory:
663
                raise
664
665
    def put_format(self, dirname, format):
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
666
        self.controldir.transport.put_bytes('%s/format' % dirname,
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
667
            format.get_format_string(),
668
            self.file_mode)
669
670
671
class BzrDirFormat4(BzrDirFormat):
672
    """Bzr dir format 4.
673
674
    This format is a combined format for working tree, branch and repository.
675
    It has:
676
     - Format 1 working trees [always]
677
     - Format 4 branches [always]
678
     - Format 4 repositories [always]
679
680
    This format is deprecated: it indexes texts using a text it which is
681
    removed in format 5; write support for this format has been removed.
682
    """
683
684
    _lock_class = lockable_files.TransportLock
685
5712.4.7 by Jelmer Vernooij
More fixes.
686
    def __eq__(self, other):
6619.3.18 by Jelmer Vernooij
Run 2to3 idioms fixer.
687
        return isinstance(self, type(other))
5712.4.7 by Jelmer Vernooij
More fixes.
688
6213.1.16 by Jelmer Vernooij
Feature support in repository.
689
    @classmethod
690
    def get_format_string(cls):
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
691
        """See BzrDirFormat.get_format_string()."""
692
        return "Bazaar-NG branch, format 0.0.4\n"
693
694
    def get_format_description(self):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
695
        """See ControlDirFormat.get_format_description()."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
696
        return "All-in-one format 4"
697
698
    def get_converter(self, format=None):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
699
        """See ControlDirFormat.get_converter()."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
700
        # there is one and only one upgrade path here.
701
        return ConvertBzrDir4To5()
702
703
    def initialize_on_transport(self, transport):
704
        """Format 4 branches cannot be created."""
705
        raise errors.UninitializableFormat(self)
706
707
    def is_supported(self):
708
        """Format 4 is not supported.
709
710
        It is not supported because the model changed from 4 to 5 and the
711
        conversion logic is expensive - so doing it on the fly was not
712
        feasible.
713
        """
714
        return False
715
716
    def network_name(self):
717
        return self.get_format_string()
718
719
    def _open(self, transport):
720
        """See BzrDirFormat._open."""
721
        return BzrDir4(transport, self)
722
723
    def __return_repository_format(self):
724
        """Circular import protection."""
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
725
        from .repository import RepositoryFormat4
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
726
        return RepositoryFormat4()
727
    repository_format = property(__return_repository_format)
728
6213.1.19 by Jelmer Vernooij
Fix tests.
729
    @classmethod
730
    def from_string(cls, format_string):
731
        if format_string != cls.get_format_string():
732
            raise AssertionError("unexpected format string %r" % format_string)
733
        return cls()
734
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
735
736
class BzrDirPreSplitOut(BzrDir):
737
    """A common class for the all-in-one formats."""
738
739
    def __init__(self, _transport, _format):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
740
        """See ControlDir.__init__."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
741
        super(BzrDirPreSplitOut, self).__init__(_transport, _format)
742
        self._control_files = lockable_files.LockableFiles(
743
                                            self.get_branch_transport(None),
744
                                            self._format._lock_file_name,
745
                                            self._format._lock_class)
746
747
    def break_lock(self):
748
        """Pre-splitout bzrdirs do not suffer from stale locks."""
749
        raise NotImplementedError(self.break_lock)
750
751
    def cloning_metadir(self, require_stacking=False):
752
        """Produce a metadir suitable for cloning with."""
753
        if require_stacking:
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
754
            return format_registry.make_controldir('1.6')
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
755
        return self._format.__class__()
756
757
    def clone(self, url, revision_id=None, force_new_repo=False,
758
              preserve_stacking=False):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
759
        """See ControlDir.clone().
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
760
761
        force_new_repo has no effect, since this family of formats always
762
        require a new repository.
763
        preserve_stacking has no effect, since no source branch using this
764
        family of formats can be stacked, so there is no stacking to preserve.
765
        """
766
        self._make_tail(url)
767
        result = self._format._initialize_for_clone(url)
768
        self.open_repository().clone(result, revision_id=revision_id)
769
        from_branch = self.open_branch()
770
        from_branch.clone(result, revision_id=revision_id)
771
        try:
772
            tree = self.open_workingtree()
773
        except errors.NotLocalUrl:
774
            # make a new one, this format always has to have one.
775
            result._init_workingtree()
776
        else:
777
            tree.clone(result)
778
        return result
779
6123.9.12 by Jelmer Vernooij
Add append_revisions_only argument to BranchFormat.initialize.
780
    def create_branch(self, name=None, repository=None,
781
                      append_revisions_only=None):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
782
        """See ControlDir.create_branch."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
783
        if repository is not None:
784
            raise NotImplementedError(
785
                "create_branch(repository=<not None>) on %r" % (self,))
6123.9.12 by Jelmer Vernooij
Add append_revisions_only argument to BranchFormat.initialize.
786
        return self._format.get_branch_format().initialize(self, name=name,
787
            append_revisions_only=append_revisions_only)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
788
789
    def destroy_branch(self, name=None):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
790
        """See ControlDir.destroy_branch."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
791
        raise errors.UnsupportedOperation(self.destroy_branch, self)
792
793
    def create_repository(self, shared=False):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
794
        """See ControlDir.create_repository."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
795
        if shared:
796
            raise errors.IncompatibleFormat('shared repository', self._format)
797
        return self.open_repository()
798
799
    def destroy_repository(self):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
800
        """See ControlDir.destroy_repository."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
801
        raise errors.UnsupportedOperation(self.destroy_repository, self)
802
803
    def create_workingtree(self, revision_id=None, from_branch=None,
804
                           accelerator_tree=None, hardlink=False):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
805
        """See ControlDir.create_workingtree."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
806
        # The workingtree is sometimes created when the bzrdir is created,
807
        # but not when cloning.
808
809
        # this looks buggy but is not -really-
810
        # because this format creates the workingtree when the bzrdir is
811
        # created
812
        # clone and sprout will have set the revision_id
813
        # and that will have set it for us, its only
814
        # specific uses of create_workingtree in isolation
815
        # that can do wonky stuff here, and that only
816
        # happens for creating checkouts, which cannot be
817
        # done on this format anyway. So - acceptable wart.
818
        if hardlink:
819
            warning("can't support hardlinked working trees in %r"
820
                % (self,))
821
        try:
822
            result = self.open_workingtree(recommend_upgrade=False)
823
        except errors.NoSuchFile:
824
            result = self._init_workingtree()
825
        if revision_id is not None:
826
            if revision_id == _mod_revision.NULL_REVISION:
827
                result.set_parent_ids([])
828
            else:
829
                result.set_parent_ids([revision_id])
830
        return result
831
832
    def _init_workingtree(self):
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
833
        from .workingtree import WorkingTreeFormat2
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
834
        try:
835
            return WorkingTreeFormat2().initialize(self)
836
        except errors.NotLocalUrl:
837
            # Even though we can't access the working tree, we need to
838
            # create its control files.
839
            return WorkingTreeFormat2()._stub_initialize_on_transport(
840
                self.transport, self._control_files._file_mode)
841
842
    def destroy_workingtree(self):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
843
        """See ControlDir.destroy_workingtree."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
844
        raise errors.UnsupportedOperation(self.destroy_workingtree, self)
845
846
    def destroy_workingtree_metadata(self):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
847
        """See ControlDir.destroy_workingtree_metadata."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
848
        raise errors.UnsupportedOperation(self.destroy_workingtree_metadata,
849
                                          self)
850
851
    def get_branch_transport(self, branch_format, name=None):
852
        """See BzrDir.get_branch_transport()."""
853
        if name is not None:
854
            raise errors.NoColocatedBranchSupport(self)
855
        if branch_format is None:
856
            return self.transport
857
        try:
858
            branch_format.get_format_string()
859
        except NotImplementedError:
860
            return self.transport
861
        raise errors.IncompatibleFormat(branch_format, self._format)
862
863
    def get_repository_transport(self, repository_format):
864
        """See BzrDir.get_repository_transport()."""
865
        if repository_format is None:
866
            return self.transport
867
        try:
868
            repository_format.get_format_string()
869
        except NotImplementedError:
870
            return self.transport
871
        raise errors.IncompatibleFormat(repository_format, self._format)
872
873
    def get_workingtree_transport(self, workingtree_format):
874
        """See BzrDir.get_workingtree_transport()."""
875
        if workingtree_format is None:
876
            return self.transport
877
        try:
878
            workingtree_format.get_format_string()
879
        except NotImplementedError:
880
            return self.transport
881
        raise errors.IncompatibleFormat(workingtree_format, self._format)
882
6630.1.1 by Jelmer Vernooij
Remove deprecated functionality.
883
    def needs_format_conversion(self, format):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
884
        """See ControlDir.needs_format_conversion()."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
885
        # if the format is not the same as the system default,
886
        # an upgrade is needed.
887
        return not isinstance(self._format, format.__class__)
888
889
    def open_branch(self, name=None, unsupported=False,
6305.3.3 by Jelmer Vernooij
Fix use of possible_transports.
890
                    ignore_fallbacks=False, possible_transports=None):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
891
        """See ControlDir.open_branch."""
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
892
        from .branch import BzrBranchFormat4
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
893
        format = BzrBranchFormat4()
5717.1.10 by Jelmer Vernooij
Fix typo.
894
        format.check_support_status(unsupported)
6305.3.3 by Jelmer Vernooij
Fix use of possible_transports.
895
        return format.open(self, name, _found=True,
896
            possible_transports=possible_transports)
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
897
898
    def sprout(self, url, revision_id=None, force_new_repo=False,
899
               possible_transports=None, accelerator_tree=None,
900
               hardlink=False, stacked=False, create_tree_if_local=True,
901
               source_branch=None):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
902
        """See ControlDir.sprout()."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
903
        if source_branch is not None:
904
            my_branch = self.open_branch()
905
            if source_branch.base != my_branch.base:
906
                raise AssertionError(
907
                    "source branch %r is not within %r with branch %r" %
908
                    (source_branch, self, my_branch))
909
        if stacked:
6734.1.11 by Jelmer Vernooij
Move UnstackableBranchFormat.
910
            raise _mod_branch.UnstackableBranchFormat(
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
911
                self._format, self.root_transport.base)
912
        if not create_tree_if_local:
6734.1.13 by Jelmer Vernooij
Move MustHaveWorkingTree.
913
            raise MustHaveWorkingTree(
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
914
                self._format, self.root_transport.base)
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
915
        from .workingtree import WorkingTreeFormat2
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
916
        self._make_tail(url)
917
        result = self._format._initialize_for_clone(url)
918
        try:
919
            self.open_repository().clone(result, revision_id=revision_id)
920
        except errors.NoRepositoryPresent:
921
            pass
922
        try:
923
            self.open_branch().sprout(result, revision_id=revision_id)
924
        except errors.NotBranchError:
925
            pass
926
927
        # we always want a working tree
928
        WorkingTreeFormat2().initialize(result,
929
                                        accelerator_tree=accelerator_tree,
930
                                        hardlink=hardlink)
931
        return result
932
6437.7.1 by Jelmer Vernooij
Add ControlDir.set_branch_reference.
933
    def set_branch_reference(self, target_branch, name=None):
6670.4.1 by Jelmer Vernooij
Update imports.
934
        from ...bzr.branch import BranchReferenceFormat
6437.7.4 by Jelmer Vernooij
Fix test.
935
        if name is not None:
936
            raise errors.NoColocatedBranchSupport(self)
6437.7.1 by Jelmer Vernooij
Add ControlDir.set_branch_reference.
937
        raise errors.IncompatibleFormat(BranchReferenceFormat, self._format)
938
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
939
940
class BzrDir4(BzrDirPreSplitOut):
941
    """A .bzr version 4 control object.
942
943
    This is a deprecated format and may be removed after sept 2006.
944
    """
945
946
    def create_repository(self, shared=False):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
947
        """See ControlDir.create_repository."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
948
        return self._format.repository_format.initialize(self, shared)
949
6630.1.1 by Jelmer Vernooij
Remove deprecated functionality.
950
    def needs_format_conversion(self, format):
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
951
        """Format 4 dirs are always in need of conversion."""
952
        return True
953
954
    def open_repository(self):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
955
        """See ControlDir.open_repository."""
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
956
        from .repository import RepositoryFormat4
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
957
        return RepositoryFormat4().open(self, _found=True)
958
959
960
class BzrDir5(BzrDirPreSplitOut):
961
    """A .bzr version 5 control object.
962
963
    This is a deprecated format and may be removed after sept 2006.
964
    """
965
966
    def has_workingtree(self):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
967
        """See ControlDir.has_workingtree."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
968
        return True
969
    
970
    def open_repository(self):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
971
        """See ControlDir.open_repository."""
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
972
        from .repository import RepositoryFormat5
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
973
        return RepositoryFormat5().open(self, _found=True)
974
6402.1.2 by Jelmer Vernooij
Fix tests.
975
    def open_workingtree(self, unsupported=False,
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
976
            recommend_upgrade=True):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
977
        """See ControlDir.create_workingtree."""
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
978
        from .workingtree import WorkingTreeFormat2
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
979
        wt_format = WorkingTreeFormat2()
980
        # we don't warn here about upgrades; that ought to be handled for the
981
        # bzrdir as a whole
982
        return wt_format.open(self, _found=True)
983
984
985
class BzrDir6(BzrDirPreSplitOut):
986
    """A .bzr version 6 control object.
987
988
    This is a deprecated format and may be removed after sept 2006.
989
    """
990
991
    def has_workingtree(self):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
992
        """See ControlDir.has_workingtree."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
993
        return True
994
995
    def open_repository(self):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
996
        """See ControlDir.open_repository."""
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
997
        from .repository import RepositoryFormat6
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
998
        return RepositoryFormat6().open(self, _found=True)
999
6402.1.2 by Jelmer Vernooij
Fix tests.
1000
    def open_workingtree(self, unsupported=False, recommend_upgrade=True):
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
1001
        """See ControlDir.create_workingtree."""
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
1002
        # we don't warn here about upgrades; that ought to be handled for the
1003
        # bzrdir as a whole
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
1004
        from .workingtree import WorkingTreeFormat2
5712.4.1 by Jelmer Vernooij
Move weave-era BzrDir directories to a separate file.
1005
        return WorkingTreeFormat2().open(self, _found=True)