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