~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bzrdir.py

  • Committer: John Arbash Meinel
  • Date: 2006-02-21 16:43:22 UTC
  • mfrom: (1560 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1562.
  • Revision ID: john@arbash-meinel.com-20060221164322-b007aa882582a66e
[merge] bzr.dev, cleanup conflicts, fixup http tests for new TestCase layout.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
"""
22
22
 
23
23
from copy import deepcopy
 
24
import os
24
25
from cStringIO import StringIO
25
26
from unittest import TestSuite
26
27
 
27
 
 
28
28
import bzrlib
29
29
import bzrlib.errors as errors
30
30
from bzrlib.lockable_files import LockableFiles
31
31
from bzrlib.osutils import safe_unicode
 
32
from bzrlib.osutils import (
 
33
                            abspath,
 
34
                            pathjoin,
 
35
                            safe_unicode,
 
36
                            sha_strings,
 
37
                            sha_string,
 
38
                            )
 
39
from bzrlib.store.text import TextStore
 
40
from bzrlib.store.weave import WeaveStore
 
41
from bzrlib.symbol_versioning import *
32
42
from bzrlib.trace import mutter
33
 
from bzrlib.symbol_versioning import *
 
43
from bzrlib.transactions import PassThroughTransaction
34
44
from bzrlib.transport import get_transport
35
45
from bzrlib.transport.local import LocalTransport
 
46
from bzrlib.weave import Weave
 
47
from bzrlib.weavefile import read_weave, write_weave
 
48
from bzrlib.xml4 import serializer_v4
 
49
from bzrlib.xml5 import serializer_v5
36
50
 
37
51
 
38
52
class BzrDir(object):
47
61
        a transport connected to the directory this bzr was opened from.
48
62
    """
49
63
 
 
64
    def can_convert_format(self):
 
65
        """Return true if this bzrdir is one whose format we can convert from."""
 
66
        return True
 
67
 
50
68
    def _check_supported(self, format, allow_unsupported):
51
69
        """Check whether format is a supported format.
52
70
 
55
73
        if not allow_unsupported and not format.is_supported():
56
74
            raise errors.UnsupportedFormatError(format)
57
75
 
58
 
    def clone(self, url, revision_id=None, basis=None):
 
76
    def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
59
77
        """Clone this bzrdir and its contents to url verbatim.
60
78
 
61
79
        If urls last component does not exist, it will be created.
62
80
 
63
81
        if revision_id is not None, then the clone operation may tune
64
82
            itself to download less data.
 
83
        :param force_new_repo: Do not use a shared repository for the target 
 
84
                               even if one is available.
65
85
        """
66
86
        self._make_tail(url)
 
87
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
67
88
        result = self._format.initialize(url)
68
 
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
69
89
        try:
70
 
            self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
 
90
            local_repo = self.find_repository()
71
91
        except errors.NoRepositoryPresent:
72
 
            pass
 
92
            local_repo = None
 
93
        if local_repo:
 
94
            # may need to copy content in
 
95
            if force_new_repo:
 
96
                local_repo.clone(result, revision_id=revision_id, basis=basis_repo)
 
97
            else:
 
98
                try:
 
99
                    result_repo = result.find_repository()
 
100
                    # fetch content this dir needs.
 
101
                    if basis_repo:
 
102
                        # XXX FIXME RBC 20060214 need tests for this when the basis
 
103
                        # is incomplete
 
104
                        result_repo.fetch(basis_repo, revision_id=revision_id)
 
105
                    result_repo.fetch(local_repo, revision_id=revision_id)
 
106
                except errors.NoRepositoryPresent:
 
107
                    # needed to make one anyway.
 
108
                    local_repo.clone(result, revision_id=revision_id, basis=basis_repo)
 
109
        # 1 if there is a branch present
 
110
        #   make sure its content is available in the target repository
 
111
        #   clone it.
73
112
        try:
74
113
            self.open_branch().clone(result, revision_id=revision_id)
75
114
        except errors.NotBranchError:
140
179
        raise NotImplementedError(self.create_branch)
141
180
 
142
181
    @staticmethod
143
 
    def create_branch_and_repo(base):
 
182
    def create_branch_and_repo(base, force_new_repo=False):
144
183
        """Create a new BzrDir, Branch and Repository at the url 'base'.
145
184
 
146
185
        This will use the current default BzrDirFormat, and use whatever 
147
186
        repository format that that uses via bzrdir.create_branch and
148
 
        create_repository.
 
187
        create_repository. If a shared repository is available that is used
 
188
        preferentially.
149
189
 
150
190
        The created Branch object is returned.
 
191
 
 
192
        :param base: The URL to create the branch at.
 
193
        :param force_new_repo: If True a new repository is always created.
151
194
        """
152
195
        bzrdir = BzrDir.create(base)
153
 
        bzrdir.create_repository()
 
196
        bzrdir._find_or_create_repository(force_new_repo)
154
197
        return bzrdir.create_branch()
155
 
        
156
 
    @staticmethod
157
 
    def create_repository(base):
 
198
 
 
199
    def _find_or_create_repository(self, force_new_repo):
 
200
        """Create a new repository if needed, returning the repository."""
 
201
        if force_new_repo:
 
202
            return self.create_repository()
 
203
        try:
 
204
            return self.find_repository()
 
205
        except errors.NoRepositoryPresent:
 
206
            return self.create_repository()
 
207
        
 
208
    @staticmethod
 
209
    def create_branch_convenience(base, force_new_repo=False, force_new_tree=None):
 
210
        """Create a new BzrDir, Branch and Repository at the url 'base'.
 
211
 
 
212
        This is a convenience function - it will use an existing repository
 
213
        if possible, can be told explicitly whether to create a working tree or
 
214
        not.
 
215
 
 
216
        This will use the current default BzrDirFormat, and use whatever 
 
217
        repository format that that uses via bzrdir.create_branch and
 
218
        create_repository. If a shared repository is available that is used
 
219
        preferentially. Whatever repository is used, its tree creation policy
 
220
        is followed.
 
221
 
 
222
        The created Branch object is returned.
 
223
        If a working tree cannot be made due to base not being a file:// url,
 
224
        no error is raised.
 
225
 
 
226
        :param base: The URL to create the branch at.
 
227
        :param force_new_repo: If True a new repository is always created.
 
228
        :param force_new_tree: If True or False force creation of a tree or 
 
229
                               prevent such creation respectively.
 
230
        """
 
231
        bzrdir = BzrDir.create(base)
 
232
        repo = bzrdir._find_or_create_repository(force_new_repo)
 
233
        result = bzrdir.create_branch()
 
234
        if force_new_tree or (repo.make_working_trees() and 
 
235
                              force_new_tree is None):
 
236
            bzrdir.create_workingtree()
 
237
        return result
 
238
        
 
239
    @staticmethod
 
240
    def create_repository(base, shared=False):
158
241
        """Create a new BzrDir and Repository at the url 'base'.
159
242
 
160
243
        This will use the current default BzrDirFormat, and use whatever 
161
244
        repository format that that uses for bzrdirformat.create_repository.
162
245
 
 
246
        ;param shared: Create a shared repository rather than a standalone
 
247
                       repository.
163
248
        The Repository object is returned.
164
249
 
165
250
        This must be overridden as an instance method in child classes, where
184
269
        t = get_transport(safe_unicode(base))
185
270
        if not isinstance(t, LocalTransport):
186
271
            raise errors.NotLocalUrl(base)
187
 
        bzrdir = BzrDir.create_branch_and_repo(safe_unicode(base)).bzrdir
 
272
        bzrdir = BzrDir.create_branch_and_repo(safe_unicode(base),
 
273
                                               force_new_repo=True).bzrdir
188
274
        return bzrdir.create_workingtree()
189
275
 
190
276
    def create_workingtree(self, revision_id=None):
194
280
        """
195
281
        raise NotImplementedError(self.create_workingtree)
196
282
 
 
283
    def find_repository(self):
 
284
        """Find the repository that should be used for a_bzrdir.
 
285
 
 
286
        This does not require a branch as we use it to find the repo for
 
287
        new branches as well as to hook existing branches up to their
 
288
        repository.
 
289
        """
 
290
        try:
 
291
            return self.open_repository()
 
292
        except errors.NoRepositoryPresent:
 
293
            pass
 
294
        next_transport = self.root_transport.clone('..')
 
295
        while True:
 
296
            try:
 
297
                found_bzrdir = BzrDir.open_containing_from_transport(
 
298
                    next_transport)[0]
 
299
            except errors.NotBranchError:
 
300
                raise errors.NoRepositoryPresent(self)
 
301
            try:
 
302
                repository = found_bzrdir.open_repository()
 
303
            except errors.NoRepositoryPresent:
 
304
                next_transport = found_bzrdir.root_transport.clone('..')
 
305
                continue
 
306
            if ((found_bzrdir.root_transport.base == 
 
307
                 self.root_transport.base) or repository.is_shared()):
 
308
                return repository
 
309
            else:
 
310
                raise errors.NoRepositoryPresent(self)
 
311
        raise errors.NoRepositoryPresent(self)
 
312
 
197
313
    def get_branch_transport(self, branch_format):
198
314
        """Get the transport for use by branch format in this BzrDir.
199
315
 
246
362
        self.transport = _transport.clone('.bzr')
247
363
        self.root_transport = _transport
248
364
 
 
365
    def needs_format_conversion(self, format=None):
 
366
        """Return true if this bzrdir needs convert_format run on it.
 
367
        
 
368
        For instance, if the repository format is out of date but the 
 
369
        branch and working tree are not, this should return True.
 
370
 
 
371
        :param format: Optional parameter indicating a specific desired
 
372
                       format we plan to arrive at.
 
373
        """
 
374
        # for now, if the format is not the same as the system default,
 
375
        # an upgrade is needed. In the future we will want to scan
 
376
        # the individual repository/branch/checkout formats too
 
377
        if format is None:
 
378
            format = BzrDirFormat.get_default_format().__class__
 
379
        return not isinstance(self._format, format)
 
380
 
249
381
    @staticmethod
250
382
    def open_unsupported(base):
251
383
        """Open a branch which is not supported."""
283
415
    def open_containing(url):
284
416
        """Open an existing branch which contains url.
285
417
        
286
 
        This probes for a branch at url, and searches upwards from there.
 
418
        :param url: url to search from.
 
419
        See open_containing_from_transport for more detail.
 
420
        """
 
421
        return BzrDir.open_containing_from_transport(get_transport(url))
 
422
    
 
423
    @staticmethod
 
424
    def open_containing_from_transport(a_transport):
 
425
        """Open an existing branch which contains a_transport.base
 
426
 
 
427
        This probes for a branch at a_transport, and searches upwards from there.
287
428
 
288
429
        Basically we keep looking up until we find the control directory or
289
430
        run into the root.  If there isn't one, raises NotBranchError.
291
432
        format, UnknownFormatError or UnsupportedFormatError are raised.
292
433
        If there is one, it is returned, along with the unused portion of url.
293
434
        """
294
 
        t = get_transport(url)
295
435
        # this gets the normalised url back. I.e. '.' -> the full path.
296
 
        url = t.base
 
436
        url = a_transport.base
297
437
        while True:
298
438
            try:
299
 
                format = BzrDirFormat.find_format(t)
300
 
                return format.open(t), t.relpath(url)
 
439
                format = BzrDirFormat.find_format(a_transport)
 
440
                return format.open(a_transport), a_transport.relpath(url)
301
441
            except errors.NotBranchError, e:
302
 
                mutter('not a branch in: %r %s', t.base, e)
303
 
            new_t = t.clone('..')
304
 
            if new_t.base == t.base:
 
442
                mutter('not a branch in: %r %s', a_transport.base, e)
 
443
            new_t = a_transport.clone('..')
 
444
            if new_t.base == a_transport.base:
305
445
                # reached the root, whatever that may be
306
446
                raise errors.NotBranchError(path=url)
307
 
            t = new_t
 
447
            a_transport = new_t
308
448
 
309
449
    def open_repository(self, _unsupported=False):
310
450
        """Open the repository object at this BzrDir if one is present.
325
465
        """
326
466
        raise NotImplementedError(self.open_workingtree)
327
467
 
328
 
    def sprout(self, url, revision_id=None, basis=None):
 
468
    def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
329
469
        """Create a copy of this bzrdir prepared for use as a new line of
330
470
        development.
331
471
 
350
490
            try:
351
491
                source_repository = self.open_repository()
352
492
            except errors.NoRepositoryPresent:
353
 
                # copy the basis one if there is one
 
493
                # copy the entire basis one if there is one
 
494
                # but there is no repository.
354
495
                source_repository = basis_repo
355
 
        if source_repository is not None:
 
496
        if force_new_repo:
 
497
            result_repo = None
 
498
        else:
 
499
            try:
 
500
                result_repo = result.find_repository()
 
501
            except errors.NoRepositoryPresent:
 
502
                result_repo = None
 
503
        if source_repository is None and result_repo is not None:
 
504
            pass
 
505
        elif source_repository is None and result_repo is None:
 
506
            # no repo available, make a new one
 
507
            result.create_repository()
 
508
        elif source_repository is not None and result_repo is None:
 
509
            # have soure, and want to make a new target repo
356
510
            source_repository.clone(result,
357
511
                                    revision_id=revision_id,
358
512
                                    basis=basis_repo)
359
513
        else:
360
 
            # no repo available, make a new one
361
 
            result.create_repository()
 
514
            # fetch needed content into target.
 
515
            if basis_repo:
 
516
                # XXX FIXME RBC 20060214 need tests for this when the basis
 
517
                # is incomplete
 
518
                result_repo.fetch(basis_repo, revision_id=revision_id)
 
519
            result_repo.fetch(source_repository, revision_id=revision_id)
362
520
        if source_branch is not None:
363
521
            source_branch.sprout(result, revision_id=revision_id)
364
522
        else:
375
533
class BzrDirPreSplitOut(BzrDir):
376
534
    """A common class for the all-in-one formats."""
377
535
 
378
 
    def clone(self, url, revision_id=None, basis=None):
 
536
    def __init__(self, _transport, _format):
 
537
        """See BzrDir.__init__."""
 
538
        super(BzrDirPreSplitOut, self).__init__(_transport, _format)
 
539
        self._control_files = LockableFiles(self.get_branch_transport(None),
 
540
                                            'branch-lock')
 
541
 
 
542
    def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
379
543
        """See BzrDir.clone()."""
380
544
        from bzrlib.workingtree import WorkingTreeFormat2
381
545
        self._make_tail(url)
394
558
        """See BzrDir.create_branch."""
395
559
        return self.open_branch()
396
560
 
397
 
    def create_repository(self):
 
561
    def create_repository(self, shared=False):
398
562
        """See BzrDir.create_repository."""
 
563
        if shared:
 
564
            raise errors.IncompatibleFormat('shared repository', self._format)
399
565
        return self.open_repository()
400
566
 
401
567
    def create_workingtree(self, revision_id=None):
477
643
    This is a deprecated format and may be removed after sept 2006.
478
644
    """
479
645
 
480
 
    def create_repository(self):
 
646
    def create_repository(self, shared=False):
481
647
        """See BzrDir.create_repository."""
482
648
        from bzrlib.repository import RepositoryFormat4
483
 
        return RepositoryFormat4().initialize(self)
 
649
        return RepositoryFormat4().initialize(self, shared)
 
650
 
 
651
    def needs_format_conversion(self, format=None):
 
652
        """Format 4 dirs are always in need of conversion."""
 
653
        return True
484
654
 
485
655
    def open_repository(self):
486
656
        """See BzrDir.open_repository."""
529
699
    individual formats are really split out.
530
700
    """
531
701
 
 
702
    def can_convert_format(self):
 
703
        """See BzrDir.can_convert_format()."""
 
704
        return False
 
705
 
532
706
    def create_branch(self):
533
707
        """See BzrDir.create_branch."""
534
708
        from bzrlib.branch import BranchFormat
535
709
        return BranchFormat.get_default_format().initialize(self)
536
710
 
537
 
    def create_repository(self):
 
711
    def create_repository(self, shared=False):
538
712
        """See BzrDir.create_repository."""
539
713
        from bzrlib.repository import RepositoryFormat
540
 
        return RepositoryFormat.get_default_format().initialize(self)
 
714
        return RepositoryFormat.get_default_format().initialize(self, shared)
541
715
 
542
716
    def create_workingtree(self, revision_id=None):
543
717
        """See BzrDir.create_workingtree."""
586
760
            pass
587
761
        return self.transport.clone('checkout')
588
762
 
 
763
    def needs_format_conversion(self, format=None):
 
764
        """See BzrDir.needs_format_conversion()."""
 
765
        # currently there are no possible conversions for meta1 formats.
 
766
        return False
 
767
 
589
768
    def open_branch(self, unsupported=False):
590
769
        """See BzrDir.open_branch."""
591
770
        from bzrlib.branch import BranchFormat
651
830
        """Return the ASCII format string that identifies this format."""
652
831
        raise NotImplementedError(self.get_format_string)
653
832
 
 
833
    def get_converter(self, format=None):
 
834
        """Return the converter to use to convert bzrdirs needing converts.
 
835
 
 
836
        This returns a bzrlib.bzrdir.Converter object.
 
837
 
 
838
        This should return the best upgrader to step this format towards the
 
839
        current default format. In the case of plugins we can/shouold provide
 
840
        some means for them to extend the range of returnable converters.
 
841
 
 
842
        :param format: Optional format to override the default foramt of the 
 
843
                       library.
 
844
        """
 
845
        raise NotImplementedError(self.get_converter)
 
846
 
654
847
    def initialize(self, url):
655
848
        """Create a bzr control dir at this url and return an opened copy."""
656
849
        # Since we don't have a .bzr directory, inherit the
717
910
    def set_default_format(klass, format):
718
911
        klass._default_format = format
719
912
 
 
913
    def __str__(self):
 
914
        return self.get_format_string()[:-1]
 
915
 
720
916
    @classmethod
721
917
    def unregister_format(klass, format):
722
918
        assert klass._formats[format.get_format_string()] is format
740
936
        """See BzrDirFormat.get_format_string()."""
741
937
        return "Bazaar-NG branch, format 0.0.4\n"
742
938
 
 
939
    def get_converter(self, format=None):
 
940
        """See BzrDirFormat.get_converter()."""
 
941
        # there is one and only one upgrade path here.
 
942
        return ConvertBzrDir4To5()
 
943
        
743
944
    def initialize(self, url):
744
945
        """Format 4 branches cannot be created."""
745
946
        raise errors.UninitializableFormat(self)
773
974
        """See BzrDirFormat.get_format_string()."""
774
975
        return "Bazaar-NG branch, format 5\n"
775
976
 
 
977
    def get_converter(self, format=None):
 
978
        """See BzrDirFormat.get_converter()."""
 
979
        # there is one and only one upgrade path here.
 
980
        return ConvertBzrDir5To6()
 
981
        
776
982
    def initialize(self, url, _cloning=False):
777
983
        """Format 5 dirs always have working tree, branch and repository.
778
984
        
807
1013
        """See BzrDirFormat.get_format_string()."""
808
1014
        return "Bazaar-NG branch, format 6\n"
809
1015
 
 
1016
    def get_converter(self, format=None):
 
1017
        """See BzrDirFormat.get_converter()."""
 
1018
        # there is one and only one upgrade path here.
 
1019
        return ConvertBzrDir6ToMeta()
 
1020
        
810
1021
    def initialize(self, url, _cloning=False):
811
1022
        """Format 6 dirs always have working tree, branch and repository.
812
1023
        
953
1164
        copytree(self.base, base, symlinks=True)
954
1165
        return ScratchDir(
955
1166
            transport=bzrlib.transport.local.ScratchTransport(base))
 
1167
 
 
1168
 
 
1169
class Converter(object):
 
1170
    """Converts a disk format object from one format to another."""
 
1171
 
 
1172
    def convert(self, to_convert, pb):
 
1173
        """Perform the conversion of to_convert, giving feedback via pb.
 
1174
 
 
1175
        :param to_convert: The disk object to convert.
 
1176
        :param pb: a progress bar to use for progress information.
 
1177
        """
 
1178
 
 
1179
 
 
1180
class ConvertBzrDir4To5(Converter):
 
1181
    """Converts format 4 bzr dirs to format 5."""
 
1182
 
 
1183
    def __init__(self):
 
1184
        super(ConvertBzrDir4To5, self).__init__()
 
1185
        self.converted_revs = set()
 
1186
        self.absent_revisions = set()
 
1187
        self.text_count = 0
 
1188
        self.revisions = {}
 
1189
        
 
1190
    def convert(self, to_convert, pb):
 
1191
        """See Converter.convert()."""
 
1192
        self.bzrdir = to_convert
 
1193
        self.pb = pb
 
1194
        self.pb.note('starting upgrade from format 4 to 5')
 
1195
        if isinstance(self.bzrdir.transport, LocalTransport):
 
1196
            self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
 
1197
        self._convert_to_weaves()
 
1198
        return BzrDir.open(self.bzrdir.root_transport.base)
 
1199
 
 
1200
    def _convert_to_weaves(self):
 
1201
        self.pb.note('note: upgrade may be faster if all store files are ungzipped first')
 
1202
        try:
 
1203
            # TODO permissions
 
1204
            stat = self.bzrdir.transport.stat('weaves')
 
1205
            if not S_ISDIR(stat.st_mode):
 
1206
                self.bzrdir.transport.delete('weaves')
 
1207
                self.bzrdir.transport.mkdir('weaves')
 
1208
        except errors.NoSuchFile:
 
1209
            self.bzrdir.transport.mkdir('weaves')
 
1210
        self.inv_weave = Weave('inventory')
 
1211
        # holds in-memory weaves for all files
 
1212
        self.text_weaves = {}
 
1213
        self.bzrdir.transport.delete('branch-format')
 
1214
        self.branch = self.bzrdir.open_branch()
 
1215
        self._convert_working_inv()
 
1216
        rev_history = self.branch.revision_history()
 
1217
        # to_read is a stack holding the revisions we still need to process;
 
1218
        # appending to it adds new highest-priority revisions
 
1219
        self.known_revisions = set(rev_history)
 
1220
        self.to_read = rev_history[-1:]
 
1221
        while self.to_read:
 
1222
            rev_id = self.to_read.pop()
 
1223
            if (rev_id not in self.revisions
 
1224
                and rev_id not in self.absent_revisions):
 
1225
                self._load_one_rev(rev_id)
 
1226
        self.pb.clear()
 
1227
        to_import = self._make_order()
 
1228
        for i, rev_id in enumerate(to_import):
 
1229
            self.pb.update('converting revision', i, len(to_import))
 
1230
            self._convert_one_rev(rev_id)
 
1231
        self.pb.clear()
 
1232
        self._write_all_weaves()
 
1233
        self._write_all_revs()
 
1234
        self.pb.note('upgraded to weaves:')
 
1235
        self.pb.note('  %6d revisions and inventories', len(self.revisions))
 
1236
        self.pb.note('  %6d revisions not present', len(self.absent_revisions))
 
1237
        self.pb.note('  %6d texts', self.text_count)
 
1238
        self._cleanup_spare_files_after_format4()
 
1239
        self.branch.control_files.put_utf8('branch-format', BzrDirFormat5().get_format_string())
 
1240
 
 
1241
    def _cleanup_spare_files_after_format4(self):
 
1242
        # FIXME working tree upgrade foo.
 
1243
        for n in 'merged-patches', 'pending-merged-patches':
 
1244
            try:
 
1245
                ## assert os.path.getsize(p) == 0
 
1246
                self.bzrdir.transport.delete(n)
 
1247
            except errors.NoSuchFile:
 
1248
                pass
 
1249
        self.bzrdir.transport.delete_tree('inventory-store')
 
1250
        self.bzrdir.transport.delete_tree('text-store')
 
1251
 
 
1252
    def _convert_working_inv(self):
 
1253
        inv = serializer_v4.read_inventory(self.branch.control_files.get('inventory'))
 
1254
        new_inv_xml = serializer_v5.write_inventory_to_string(inv)
 
1255
        # FIXME inventory is a working tree change.
 
1256
        self.branch.control_files.put('inventory', new_inv_xml)
 
1257
 
 
1258
    def _write_all_weaves(self):
 
1259
        controlweaves = WeaveStore(self.bzrdir.transport, prefixed=False)
 
1260
        weave_transport = self.bzrdir.transport.clone('weaves')
 
1261
        weaves = WeaveStore(weave_transport, prefixed=False)
 
1262
        transaction = PassThroughTransaction()
 
1263
 
 
1264
        controlweaves.put_weave('inventory', self.inv_weave, transaction)
 
1265
        i = 0
 
1266
        try:
 
1267
            for file_id, file_weave in self.text_weaves.items():
 
1268
                self.pb.update('writing weave', i, len(self.text_weaves))
 
1269
                weaves.put_weave(file_id, file_weave, transaction)
 
1270
                i += 1
 
1271
        finally:
 
1272
            self.pb.clear()
 
1273
 
 
1274
    def _write_all_revs(self):
 
1275
        """Write all revisions out in new form."""
 
1276
        self.bzrdir.transport.delete_tree('revision-store')
 
1277
        self.bzrdir.transport.mkdir('revision-store')
 
1278
        revision_transport = self.bzrdir.transport.clone('revision-store')
 
1279
        # TODO permissions
 
1280
        revision_store = TextStore(revision_transport,
 
1281
                                   prefixed=False,
 
1282
                                   compressed=True)
 
1283
        try:
 
1284
            for i, rev_id in enumerate(self.converted_revs):
 
1285
                self.pb.update('write revision', i, len(self.converted_revs))
 
1286
                rev_tmp = StringIO()
 
1287
                serializer_v5.write_revision(self.revisions[rev_id], rev_tmp)
 
1288
                rev_tmp.seek(0)
 
1289
                revision_store.add(rev_tmp, rev_id)
 
1290
        finally:
 
1291
            self.pb.clear()
 
1292
            
 
1293
    def _load_one_rev(self, rev_id):
 
1294
        """Load a revision object into memory.
 
1295
 
 
1296
        Any parents not either loaded or abandoned get queued to be
 
1297
        loaded."""
 
1298
        self.pb.update('loading revision',
 
1299
                       len(self.revisions),
 
1300
                       len(self.known_revisions))
 
1301
        if not self.branch.repository.revision_store.has_id(rev_id):
 
1302
            self.pb.clear()
 
1303
            self.pb.note('revision {%s} not present in branch; '
 
1304
                         'will be converted as a ghost',
 
1305
                         rev_id)
 
1306
            self.absent_revisions.add(rev_id)
 
1307
        else:
 
1308
            rev_xml = self.branch.repository.revision_store.get(rev_id).read()
 
1309
            rev = serializer_v4.read_revision_from_string(rev_xml)
 
1310
            for parent_id in rev.parent_ids:
 
1311
                self.known_revisions.add(parent_id)
 
1312
                self.to_read.append(parent_id)
 
1313
            self.revisions[rev_id] = rev
 
1314
 
 
1315
    def _load_old_inventory(self, rev_id):
 
1316
        assert rev_id not in self.converted_revs
 
1317
        old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
 
1318
        inv = serializer_v4.read_inventory_from_string(old_inv_xml)
 
1319
        rev = self.revisions[rev_id]
 
1320
        if rev.inventory_sha1:
 
1321
            assert rev.inventory_sha1 == sha_string(old_inv_xml), \
 
1322
                'inventory sha mismatch for {%s}' % rev_id
 
1323
        return inv
 
1324
 
 
1325
    def _load_updated_inventory(self, rev_id):
 
1326
        assert rev_id in self.converted_revs
 
1327
        inv_xml = self.inv_weave.get_text(rev_id)
 
1328
        inv = serializer_v5.read_inventory_from_string(inv_xml)
 
1329
        return inv
 
1330
 
 
1331
    def _convert_one_rev(self, rev_id):
 
1332
        """Convert revision and all referenced objects to new format."""
 
1333
        rev = self.revisions[rev_id]
 
1334
        inv = self._load_old_inventory(rev_id)
 
1335
        present_parents = [p for p in rev.parent_ids
 
1336
                           if p not in self.absent_revisions]
 
1337
        self._convert_revision_contents(rev, inv, present_parents)
 
1338
        self._store_new_weave(rev, inv, present_parents)
 
1339
        self.converted_revs.add(rev_id)
 
1340
 
 
1341
    def _store_new_weave(self, rev, inv, present_parents):
 
1342
        # the XML is now updated with text versions
 
1343
        if __debug__:
 
1344
            for file_id in inv:
 
1345
                ie = inv[file_id]
 
1346
                if ie.kind == 'root_directory':
 
1347
                    continue
 
1348
                assert hasattr(ie, 'revision'), \
 
1349
                    'no revision on {%s} in {%s}' % \
 
1350
                    (file_id, rev.revision_id)
 
1351
        new_inv_xml = serializer_v5.write_inventory_to_string(inv)
 
1352
        new_inv_sha1 = sha_string(new_inv_xml)
 
1353
        self.inv_weave.add(rev.revision_id, 
 
1354
                           present_parents,
 
1355
                           new_inv_xml.splitlines(True),
 
1356
                           new_inv_sha1)
 
1357
        rev.inventory_sha1 = new_inv_sha1
 
1358
 
 
1359
    def _convert_revision_contents(self, rev, inv, present_parents):
 
1360
        """Convert all the files within a revision.
 
1361
 
 
1362
        Also upgrade the inventory to refer to the text revision ids."""
 
1363
        rev_id = rev.revision_id
 
1364
        mutter('converting texts of revision {%s}',
 
1365
               rev_id)
 
1366
        parent_invs = map(self._load_updated_inventory, present_parents)
 
1367
        for file_id in inv:
 
1368
            ie = inv[file_id]
 
1369
            self._convert_file_version(rev, ie, parent_invs)
 
1370
 
 
1371
    def _convert_file_version(self, rev, ie, parent_invs):
 
1372
        """Convert one version of one file.
 
1373
 
 
1374
        The file needs to be added into the weave if it is a merge
 
1375
        of >=2 parents or if it's changed from its parent.
 
1376
        """
 
1377
        if ie.kind == 'root_directory':
 
1378
            return
 
1379
        file_id = ie.file_id
 
1380
        rev_id = rev.revision_id
 
1381
        w = self.text_weaves.get(file_id)
 
1382
        if w is None:
 
1383
            w = Weave(file_id)
 
1384
            self.text_weaves[file_id] = w
 
1385
        text_changed = False
 
1386
        previous_entries = ie.find_previous_heads(parent_invs, w)
 
1387
        for old_revision in previous_entries:
 
1388
                # if this fails, its a ghost ?
 
1389
                assert old_revision in self.converted_revs 
 
1390
        self.snapshot_ie(previous_entries, ie, w, rev_id)
 
1391
        del ie.text_id
 
1392
        assert getattr(ie, 'revision', None) is not None
 
1393
 
 
1394
    def snapshot_ie(self, previous_revisions, ie, w, rev_id):
 
1395
        # TODO: convert this logic, which is ~= snapshot to
 
1396
        # a call to:. This needs the path figured out. rather than a work_tree
 
1397
        # a v4 revision_tree can be given, or something that looks enough like
 
1398
        # one to give the file content to the entry if it needs it.
 
1399
        # and we need something that looks like a weave store for snapshot to 
 
1400
        # save against.
 
1401
        #ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
 
1402
        if len(previous_revisions) == 1:
 
1403
            previous_ie = previous_revisions.values()[0]
 
1404
            if ie._unchanged(previous_ie):
 
1405
                ie.revision = previous_ie.revision
 
1406
                return
 
1407
        parent_indexes = map(w.lookup, previous_revisions)
 
1408
        if ie.has_text():
 
1409
            text = self.branch.repository.text_store.get(ie.text_id)
 
1410
            file_lines = text.readlines()
 
1411
            assert sha_strings(file_lines) == ie.text_sha1
 
1412
            assert sum(map(len, file_lines)) == ie.text_size
 
1413
            w.add(rev_id, parent_indexes, file_lines, ie.text_sha1)
 
1414
            self.text_count += 1
 
1415
        else:
 
1416
            w.add(rev_id, parent_indexes, [], None)
 
1417
        ie.revision = rev_id
 
1418
 
 
1419
    def _make_order(self):
 
1420
        """Return a suitable order for importing revisions.
 
1421
 
 
1422
        The order must be such that an revision is imported after all
 
1423
        its (present) parents.
 
1424
        """
 
1425
        todo = set(self.revisions.keys())
 
1426
        done = self.absent_revisions.copy()
 
1427
        order = []
 
1428
        while todo:
 
1429
            # scan through looking for a revision whose parents
 
1430
            # are all done
 
1431
            for rev_id in sorted(list(todo)):
 
1432
                rev = self.revisions[rev_id]
 
1433
                parent_ids = set(rev.parent_ids)
 
1434
                if parent_ids.issubset(done):
 
1435
                    # can take this one now
 
1436
                    order.append(rev_id)
 
1437
                    todo.remove(rev_id)
 
1438
                    done.add(rev_id)
 
1439
        return order
 
1440
 
 
1441
 
 
1442
class ConvertBzrDir5To6(Converter):
 
1443
    """Converts format 5 bzr dirs to format 6."""
 
1444
 
 
1445
    def convert(self, to_convert, pb):
 
1446
        """See Converter.convert()."""
 
1447
        self.bzrdir = to_convert
 
1448
        self.pb = pb
 
1449
        self.pb.note('starting upgrade from format 5 to 6')
 
1450
        self._convert_to_prefixed()
 
1451
        return BzrDir.open(self.bzrdir.root_transport.base)
 
1452
 
 
1453
    def _convert_to_prefixed(self):
 
1454
        from bzrlib.store import hash_prefix
 
1455
        self.bzrdir.transport.delete('branch-format')
 
1456
        for store_name in ["weaves", "revision-store"]:
 
1457
            self.pb.note("adding prefixes to %s" % store_name) 
 
1458
            store_transport = self.bzrdir.transport.clone(store_name)
 
1459
            for filename in store_transport.list_dir('.'):
 
1460
                if (filename.endswith(".weave") or
 
1461
                    filename.endswith(".gz") or
 
1462
                    filename.endswith(".sig")):
 
1463
                    file_id = os.path.splitext(filename)[0]
 
1464
                else:
 
1465
                    file_id = filename
 
1466
                prefix_dir = hash_prefix(file_id)
 
1467
                # FIXME keep track of the dirs made RBC 20060121
 
1468
                try:
 
1469
                    store_transport.move(filename, prefix_dir + '/' + filename)
 
1470
                except errors.NoSuchFile: # catches missing dirs strangely enough
 
1471
                    store_transport.mkdir(prefix_dir)
 
1472
                    store_transport.move(filename, prefix_dir + '/' + filename)
 
1473
        self.bzrdir._control_files.put_utf8('branch-format', BzrDirFormat6().get_format_string())
 
1474
 
 
1475
 
 
1476
class ConvertBzrDir6ToMeta(Converter):
 
1477
    """Converts format 6 bzr dirs to metadirs."""
 
1478
 
 
1479
    def convert(self, to_convert, pb):
 
1480
        """See Converter.convert()."""
 
1481
        self.bzrdir = to_convert
 
1482
        self.pb = pb
 
1483
        self.count = 0
 
1484
        self.total = 20 # the steps we know about
 
1485
        self.garbage_inventories = []
 
1486
 
 
1487
        self.pb.note('starting upgrade from format 6 to metadir')
 
1488
        self.bzrdir._control_files.put_utf8('branch-format', "Converting to format 6")
 
1489
        # its faster to move specific files around than to open and use the apis...
 
1490
        # first off, nuke ancestry.weave, it was never used.
 
1491
        try:
 
1492
            self.step('Removing ancestry.weave')
 
1493
            self.bzrdir.transport.delete('ancestry.weave')
 
1494
        except errors.NoSuchFile:
 
1495
            pass
 
1496
        # find out whats there
 
1497
        self.step('Finding branch files')
 
1498
        last_revision = self.bzrdir.open_workingtree().last_revision()
 
1499
        bzrcontents = self.bzrdir.transport.list_dir('.')
 
1500
        for name in bzrcontents:
 
1501
            if name.startswith('basis-inventory.'):
 
1502
                self.garbage_inventories.append(name)
 
1503
        # create new directories for repository, working tree and branch
 
1504
        dir_mode = self.bzrdir._control_files._dir_mode
 
1505
        self.file_mode = self.bzrdir._control_files._file_mode
 
1506
        repository_names = [('inventory.weave', True),
 
1507
                            ('revision-store', True),
 
1508
                            ('weaves', True)]
 
1509
        self.step('Upgrading repository  ')
 
1510
        self.bzrdir.transport.mkdir('repository', mode=dir_mode)
 
1511
        self.make_lock('repository')
 
1512
        # we hard code the formats here because we are converting into
 
1513
        # the meta format. The meta format upgrader can take this to a 
 
1514
        # future format within each component.
 
1515
        self.put_format('repository', bzrlib.repository.RepositoryFormat7())
 
1516
        for entry in repository_names:
 
1517
            self.move_entry('repository', entry)
 
1518
 
 
1519
        self.step('Upgrading branch      ')
 
1520
        self.bzrdir.transport.mkdir('branch', mode=dir_mode)
 
1521
        self.make_lock('branch')
 
1522
        self.put_format('branch', bzrlib.branch.BzrBranchFormat5())
 
1523
        branch_files = [('revision-history', True),
 
1524
                        ('branch-name', True),
 
1525
                        ('parent', False)]
 
1526
        for entry in branch_files:
 
1527
            self.move_entry('branch', entry)
 
1528
 
 
1529
        self.step('Upgrading working tree')
 
1530
        self.bzrdir.transport.mkdir('checkout', mode=dir_mode)
 
1531
        self.make_lock('checkout')
 
1532
        self.put_format('checkout', bzrlib.workingtree.WorkingTreeFormat3())
 
1533
        self.bzrdir.transport.delete_multi(self.garbage_inventories, self.pb)
 
1534
        checkout_files = [('pending-merges', True),
 
1535
                          ('inventory', True),
 
1536
                          ('stat-cache', False)]
 
1537
        for entry in checkout_files:
 
1538
            self.move_entry('checkout', entry)
 
1539
        if last_revision is not None:
 
1540
            self.bzrdir._control_files.put_utf8('checkout/last-revision',
 
1541
                                                last_revision)
 
1542
        self.bzrdir._control_files.put_utf8('branch-format', BzrDirMetaFormat1().get_format_string())
 
1543
        return BzrDir.open(self.bzrdir.root_transport.base)
 
1544
 
 
1545
    def make_lock(self, name):
 
1546
        """Make a lock for the new control dir name."""
 
1547
        self.step('Make %s lock' % name)
 
1548
        self.bzrdir.transport.put('%s/lock' % name, StringIO(), mode=self.file_mode)
 
1549
 
 
1550
    def move_entry(self, new_dir, entry):
 
1551
        """Move then entry name into new_dir."""
 
1552
        name = entry[0]
 
1553
        mandatory = entry[1]
 
1554
        self.step('Moving %s' % name)
 
1555
        try:
 
1556
            self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
 
1557
        except errors.NoSuchFile:
 
1558
            if mandatory:
 
1559
                raise
 
1560
 
 
1561
    def put_format(self, dirname, format):
 
1562
        self.bzrdir._control_files.put_utf8('%s/format' % dirname, format.get_format_string())
 
1563
 
 
1564
    def step(self, message):
 
1565
        """Update the pb by a step."""
 
1566
        self.count +=1
 
1567
        self.pb.update('Upgrading repository  ', self.count, self.total)
 
1568