~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/repofmt/weaverepo.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2007 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
"""Old weave-based repository formats"""
 
19
 
 
20
from StringIO import StringIO
 
21
 
 
22
from bzrlib import (
 
23
    bzrdir,
 
24
    lockable_files,
 
25
    lockdir,
 
26
    weave,
 
27
    weavefile,
 
28
    xml5,
 
29
    )
 
30
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
31
from bzrlib.repository import (
 
32
    MetaDirRepository,
 
33
    MetaDirRepositoryFormat,
 
34
    Repository,
 
35
    RepositoryFormat,
 
36
    )
 
37
from bzrlib.store.text import TextStore
 
38
from bzrlib.trace import mutter
 
39
 
 
40
 
 
41
class AllInOneRepository(Repository):
 
42
    """Legacy support - the repository behaviour for all-in-one branches."""
 
43
 
 
44
    _serializer = xml5.serializer_v5
 
45
 
 
46
    def __init__(self, _format, a_bzrdir, _revision_store, control_store, text_store):
 
47
        # we reuse one control files instance.
 
48
        dir_mode = a_bzrdir._control_files._dir_mode
 
49
        file_mode = a_bzrdir._control_files._file_mode
 
50
 
 
51
        def get_store(name, compressed=True, prefixed=False):
 
52
            # FIXME: This approach of assuming stores are all entirely compressed
 
53
            # or entirely uncompressed is tidy, but breaks upgrade from 
 
54
            # some existing branches where there's a mixture; we probably 
 
55
            # still want the option to look for both.
 
56
            relpath = a_bzrdir._control_files._escape(name)
 
57
            store = TextStore(a_bzrdir._control_files._transport.clone(relpath),
 
58
                              prefixed=prefixed, compressed=compressed,
 
59
                              dir_mode=dir_mode,
 
60
                              file_mode=file_mode)
 
61
            #if self._transport.should_cache():
 
62
            #    cache_path = os.path.join(self.cache_root, name)
 
63
            #    os.mkdir(cache_path)
 
64
            #    store = bzrlib.store.CachedStore(store, cache_path)
 
65
            return store
 
66
 
 
67
        # not broken out yet because the controlweaves|inventory_store
 
68
        # and text_store | weave_store bits are still different.
 
69
        if isinstance(_format, RepositoryFormat4):
 
70
            # cannot remove these - there is still no consistent api 
 
71
            # which allows access to this old info.
 
72
            self.inventory_store = get_store('inventory-store')
 
73
            text_store = get_store('text-store')
 
74
        super(AllInOneRepository, self).__init__(_format, a_bzrdir, a_bzrdir._control_files, _revision_store, control_store, text_store)
 
75
 
 
76
    def get_commit_builder(self, branch, parents, config, timestamp=None,
 
77
                           timezone=None, committer=None, revprops=None,
 
78
                           revision_id=None):
 
79
        self._check_ascii_revisionid(revision_id, self.get_commit_builder)
 
80
        return Repository.get_commit_builder(self, branch, parents, config,
 
81
            timestamp, timezone, committer, revprops, revision_id)
 
82
 
 
83
    @needs_read_lock
 
84
    def is_shared(self):
 
85
        """AllInOne repositories cannot be shared."""
 
86
        return False
 
87
 
 
88
    @needs_write_lock
 
89
    def set_make_working_trees(self, new_value):
 
90
        """Set the policy flag for making working trees when creating branches.
 
91
 
 
92
        This only applies to branches that use this repository.
 
93
 
 
94
        The default is 'True'.
 
95
        :param new_value: True to restore the default, False to disable making
 
96
                          working trees.
 
97
        """
 
98
        raise NotImplementedError(self.set_make_working_trees)
 
99
    
 
100
    def make_working_trees(self):
 
101
        """Returns the policy for making working trees on new branches."""
 
102
        return True
 
103
 
 
104
 
 
105
class WeaveMetaDirRepository(MetaDirRepository):
 
106
    """A subclass of MetaDirRepository to set weave specific policy."""
 
107
 
 
108
    _serializer = xml5.serializer_v5
 
109
 
 
110
    def get_commit_builder(self, branch, parents, config, timestamp=None,
 
111
                           timezone=None, committer=None, revprops=None,
 
112
                           revision_id=None):
 
113
        self._check_ascii_revisionid(revision_id, self.get_commit_builder)
 
114
        return MetaDirRepository.get_commit_builder(self, branch, parents,
 
115
            config, timestamp, timezone, committer, revprops, revision_id)
 
116
 
 
117
 
 
118
class PreSplitOutRepositoryFormat(RepositoryFormat):
 
119
    """Base class for the pre split out repository formats."""
 
120
 
 
121
    rich_root_data = False
 
122
    supports_tree_reference = False
 
123
 
 
124
    def initialize(self, a_bzrdir, shared=False, _internal=False):
 
125
        """Create a weave repository.
 
126
        
 
127
        TODO: when creating split out bzr branch formats, move this to a common
 
128
        base for Format5, Format6. or something like that.
 
129
        """
 
130
        if shared:
 
131
            raise errors.IncompatibleFormat(self, a_bzrdir._format)
 
132
 
 
133
        if not _internal:
 
134
            # always initialized when the bzrdir is.
 
135
            return self.open(a_bzrdir, _found=True)
 
136
        
 
137
        # Create an empty weave
 
138
        sio = StringIO()
 
139
        weavefile.write_weave_v5(weave.Weave(), sio)
 
140
        empty_weave = sio.getvalue()
 
141
 
 
142
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
143
        dirs = ['revision-store', 'weaves']
 
144
        files = [('inventory.weave', StringIO(empty_weave)),
 
145
                 ]
 
146
        
 
147
        # FIXME: RBC 20060125 don't peek under the covers
 
148
        # NB: no need to escape relative paths that are url safe.
 
149
        control_files = lockable_files.LockableFiles(a_bzrdir.transport,
 
150
                                'branch-lock', lockable_files.TransportLock)
 
151
        control_files.create_lock()
 
152
        control_files.lock_write()
 
153
        control_files._transport.mkdir_multi(dirs,
 
154
                mode=control_files._dir_mode)
 
155
        try:
 
156
            for file, content in files:
 
157
                control_files.put(file, content)
 
158
        finally:
 
159
            control_files.unlock()
 
160
        return self.open(a_bzrdir, _found=True)
 
161
 
 
162
    def _get_control_store(self, repo_transport, control_files):
 
163
        """Return the control store for this repository."""
 
164
        return self._get_versioned_file_store('',
 
165
                                              repo_transport,
 
166
                                              control_files,
 
167
                                              prefixed=False)
 
168
 
 
169
    def _get_text_store(self, transport, control_files):
 
170
        """Get a store for file texts for this format."""
 
171
        raise NotImplementedError(self._get_text_store)
 
172
 
 
173
    def open(self, a_bzrdir, _found=False):
 
174
        """See RepositoryFormat.open()."""
 
175
        if not _found:
 
176
            # we are being called directly and must probe.
 
177
            raise NotImplementedError
 
178
 
 
179
        repo_transport = a_bzrdir.get_repository_transport(None)
 
180
        control_files = a_bzrdir._control_files
 
181
        text_store = self._get_text_store(repo_transport, control_files)
 
182
        control_store = self._get_control_store(repo_transport, control_files)
 
183
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
184
        return AllInOneRepository(_format=self,
 
185
                                  a_bzrdir=a_bzrdir,
 
186
                                  _revision_store=_revision_store,
 
187
                                  control_store=control_store,
 
188
                                  text_store=text_store)
 
189
 
 
190
    def check_conversion_target(self, target_format):
 
191
        pass
 
192
 
 
193
 
 
194
class RepositoryFormat4(PreSplitOutRepositoryFormat):
 
195
    """Bzr repository format 4.
 
196
 
 
197
    This repository format has:
 
198
     - flat stores
 
199
     - TextStores for texts, inventories,revisions.
 
200
 
 
201
    This format is deprecated: it indexes texts using a text id which is
 
202
    removed in format 5; initialization and write support for this format
 
203
    has been removed.
 
204
    """
 
205
 
 
206
    _matchingbzrdir = bzrdir.BzrDirFormat4()
 
207
 
 
208
    def __init__(self):
 
209
        super(RepositoryFormat4, self).__init__()
 
210
 
 
211
    def get_format_description(self):
 
212
        """See RepositoryFormat.get_format_description()."""
 
213
        return "Repository format 4"
 
214
 
 
215
    def initialize(self, url, shared=False, _internal=False):
 
216
        """Format 4 branches cannot be created."""
 
217
        raise errors.UninitializableFormat(self)
 
218
 
 
219
    def is_supported(self):
 
220
        """Format 4 is not supported.
 
221
 
 
222
        It is not supported because the model changed from 4 to 5 and the
 
223
        conversion logic is expensive - so doing it on the fly was not 
 
224
        feasible.
 
225
        """
 
226
        return False
 
227
 
 
228
    def _get_control_store(self, repo_transport, control_files):
 
229
        """Format 4 repositories have no formal control store at this point.
 
230
        
 
231
        This will cause any control-file-needing apis to fail - this is desired.
 
232
        """
 
233
        return None
 
234
    
 
235
    def _get_revision_store(self, repo_transport, control_files):
 
236
        """See RepositoryFormat._get_revision_store()."""
 
237
        from bzrlib.xml4 import serializer_v4
 
238
        return self._get_text_rev_store(repo_transport,
 
239
                                        control_files,
 
240
                                        'revision-store',
 
241
                                        serializer=serializer_v4)
 
242
 
 
243
    def _get_text_store(self, transport, control_files):
 
244
        """See RepositoryFormat._get_text_store()."""
 
245
 
 
246
 
 
247
class RepositoryFormat5(PreSplitOutRepositoryFormat):
 
248
    """Bzr control format 5.
 
249
 
 
250
    This repository format has:
 
251
     - weaves for file texts and inventory
 
252
     - flat stores
 
253
     - TextStores for revisions and signatures.
 
254
    """
 
255
 
 
256
    _versionedfile_class = weave.WeaveFile
 
257
    _matchingbzrdir = bzrdir.BzrDirFormat5()
 
258
 
 
259
    def __init__(self):
 
260
        super(RepositoryFormat5, self).__init__()
 
261
 
 
262
    def get_format_description(self):
 
263
        """See RepositoryFormat.get_format_description()."""
 
264
        return "Weave repository format 5"
 
265
 
 
266
    def _get_revision_store(self, repo_transport, control_files):
 
267
        """See RepositoryFormat._get_revision_store()."""
 
268
        """Return the revision store object for this a_bzrdir."""
 
269
        return self._get_text_rev_store(repo_transport,
 
270
                                        control_files,
 
271
                                        'revision-store',
 
272
                                        compressed=False)
 
273
 
 
274
    def _get_text_store(self, transport, control_files):
 
275
        """See RepositoryFormat._get_text_store()."""
 
276
        return self._get_versioned_file_store('weaves', transport, control_files, prefixed=False)
 
277
 
 
278
 
 
279
class RepositoryFormat6(PreSplitOutRepositoryFormat):
 
280
    """Bzr control format 6.
 
281
 
 
282
    This repository format has:
 
283
     - weaves for file texts and inventory
 
284
     - hash subdirectory based stores.
 
285
     - TextStores for revisions and signatures.
 
286
    """
 
287
 
 
288
    _versionedfile_class = weave.WeaveFile
 
289
    _matchingbzrdir = bzrdir.BzrDirFormat6()
 
290
 
 
291
    def __init__(self):
 
292
        super(RepositoryFormat6, self).__init__()
 
293
 
 
294
    def get_format_description(self):
 
295
        """See RepositoryFormat.get_format_description()."""
 
296
        return "Weave repository format 6"
 
297
 
 
298
    def _get_revision_store(self, repo_transport, control_files):
 
299
        """See RepositoryFormat._get_revision_store()."""
 
300
        return self._get_text_rev_store(repo_transport,
 
301
                                        control_files,
 
302
                                        'revision-store',
 
303
                                        compressed=False,
 
304
                                        prefixed=True)
 
305
 
 
306
    def _get_text_store(self, transport, control_files):
 
307
        """See RepositoryFormat._get_text_store()."""
 
308
        return self._get_versioned_file_store('weaves', transport, control_files)
 
309
 
 
310
 
 
311
class RepositoryFormat7(MetaDirRepositoryFormat):
 
312
    """Bzr repository 7.
 
313
 
 
314
    This repository format has:
 
315
     - weaves for file texts and inventory
 
316
     - hash subdirectory based stores.
 
317
     - TextStores for revisions and signatures.
 
318
     - a format marker of its own
 
319
     - an optional 'shared-storage' flag
 
320
     - an optional 'no-working-trees' flag
 
321
    """
 
322
 
 
323
    _versionedfile_class = weave.WeaveFile
 
324
 
 
325
    def _get_control_store(self, repo_transport, control_files):
 
326
        """Return the control store for this repository."""
 
327
        return self._get_versioned_file_store('',
 
328
                                              repo_transport,
 
329
                                              control_files,
 
330
                                              prefixed=False)
 
331
 
 
332
    def get_format_string(self):
 
333
        """See RepositoryFormat.get_format_string()."""
 
334
        return "Bazaar-NG Repository format 7"
 
335
 
 
336
    def get_format_description(self):
 
337
        """See RepositoryFormat.get_format_description()."""
 
338
        return "Weave repository format 7"
 
339
 
 
340
    def check_conversion_target(self, target_format):
 
341
        pass
 
342
 
 
343
    def _get_revision_store(self, repo_transport, control_files):
 
344
        """See RepositoryFormat._get_revision_store()."""
 
345
        return self._get_text_rev_store(repo_transport,
 
346
                                        control_files,
 
347
                                        'revision-store',
 
348
                                        compressed=False,
 
349
                                        prefixed=True,
 
350
                                        )
 
351
 
 
352
    def _get_text_store(self, transport, control_files):
 
353
        """See RepositoryFormat._get_text_store()."""
 
354
        return self._get_versioned_file_store('weaves',
 
355
                                              transport,
 
356
                                              control_files)
 
357
 
 
358
    def initialize(self, a_bzrdir, shared=False):
 
359
        """Create a weave repository.
 
360
 
 
361
        :param shared: If true the repository will be initialized as a shared
 
362
                       repository.
 
363
        """
 
364
        # Create an empty weave
 
365
        sio = StringIO()
 
366
        weavefile.write_weave_v5(weave.Weave(), sio)
 
367
        empty_weave = sio.getvalue()
 
368
 
 
369
        mutter('creating repository in %s.', a_bzrdir.transport.base)
 
370
        dirs = ['revision-store', 'weaves']
 
371
        files = [('inventory.weave', StringIO(empty_weave)), 
 
372
                 ]
 
373
        utf8_files = [('format', self.get_format_string())]
 
374
 
 
375
        self._upload_blank_content(a_bzrdir, dirs, files, utf8_files, shared)
 
376
        return self.open(a_bzrdir=a_bzrdir, _found=True)
 
377
 
 
378
    def open(self, a_bzrdir, _found=False, _override_transport=None):
 
379
        """See RepositoryFormat.open().
 
380
        
 
381
        :param _override_transport: INTERNAL USE ONLY. Allows opening the
 
382
                                    repository at a slightly different url
 
383
                                    than normal. I.e. during 'upgrade'.
 
384
        """
 
385
        if not _found:
 
386
            format = RepositoryFormat.find_format(a_bzrdir)
 
387
            assert format.__class__ ==  self.__class__
 
388
        if _override_transport is not None:
 
389
            repo_transport = _override_transport
 
390
        else:
 
391
            repo_transport = a_bzrdir.get_repository_transport(None)
 
392
        control_files = lockable_files.LockableFiles(repo_transport,
 
393
                                'lock', lockdir.LockDir)
 
394
        text_store = self._get_text_store(repo_transport, control_files)
 
395
        control_store = self._get_control_store(repo_transport, control_files)
 
396
        _revision_store = self._get_revision_store(repo_transport, control_files)
 
397
        return WeaveMetaDirRepository(_format=self,
 
398
            a_bzrdir=a_bzrdir,
 
399
            control_files=control_files,
 
400
            _revision_store=_revision_store,
 
401
            control_store=control_store,
 
402
            text_store=text_store)
 
403
 
 
404
 
 
405
_legacy_formats = [RepositoryFormat4(),
 
406
                   RepositoryFormat5(),
 
407
                   RepositoryFormat6()]