~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bzrdir.py

  • Committer: Martin Pool
  • Date: 2006-11-02 10:20:19 UTC
  • mfrom: (2114 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2119.
  • Revision ID: mbp@sourcefrog.net-20061102102019-9a5a02f485dff6f6
merge bzr.dev and reconcile several changes, also some test fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
# TODO: Can we move specific formats into separate modules to make this file
29
29
# smaller?
30
30
 
31
 
from copy import deepcopy
32
31
from cStringIO import StringIO
33
32
import os
 
33
 
 
34
from bzrlib.lazy_import import lazy_import
 
35
lazy_import(globals(), """
 
36
from copy import deepcopy
34
37
from stat import S_ISDIR
35
 
from unittest import TestSuite
 
38
import unittest
36
39
 
37
40
import bzrlib
38
 
import bzrlib.errors as errors
39
 
from bzrlib.lockable_files import LockableFiles, TransportLock
40
 
from bzrlib.lockdir import LockDir
 
41
from bzrlib import (
 
42
    errors,
 
43
    lockable_files,
 
44
    lockdir,
 
45
    revision as _mod_revision,
 
46
    urlutils,
 
47
    xml4,
 
48
    xml5,
 
49
    )
41
50
from bzrlib.osutils import (
42
 
                            abspath,
43
 
                            pathjoin,
44
 
                            safe_unicode,
45
 
                            sha_strings,
46
 
                            sha_string,
47
 
                            )
48
 
import bzrlib.revision
 
51
    safe_unicode,
 
52
    sha_strings,
 
53
    sha_string,
 
54
    )
49
55
from bzrlib.store.revision.text import TextRevisionStore
50
56
from bzrlib.store.text import TextStore
51
57
from bzrlib.store.versioned import WeaveStore
52
 
from bzrlib.trace import mutter
53
58
from bzrlib.transactions import WriteTransaction
54
59
from bzrlib.transport import get_transport
 
60
from bzrlib.weave import Weave
 
61
""")
 
62
 
 
63
from bzrlib.trace import mutter
55
64
from bzrlib.transport.local import LocalTransport
56
 
import bzrlib.urlutils as urlutils
57
 
from bzrlib.weave import Weave
58
 
from bzrlib.xml4 import serializer_v4
59
 
import bzrlib.xml5
60
65
 
61
66
 
62
67
class BzrDir(object):
188
193
    def _make_tail(self, url):
189
194
        head, tail = urlutils.split(url)
190
195
        if tail and tail != '.':
191
 
            t = bzrlib.transport.get_transport(head)
 
196
            t = get_transport(head)
192
197
            try:
193
198
                t.mkdir(tail)
194
199
            except errors.FileExists:
210
215
                    "not one of %r" % cls)
211
216
        head, tail = urlutils.split(base)
212
217
        if tail and tail != '.':
213
 
            t = bzrlib.transport.get_transport(head)
 
218
            t = get_transport(head)
214
219
            try:
215
220
                t.mkdir(tail)
216
221
            except errors.FileExists:
680
685
        # TODO: jam 20060426 we probably need a test in here in the
681
686
        #       case that the newly sprouted branch is a remote one
682
687
        if result_repo is None or result_repo.make_working_trees():
683
 
            result.create_workingtree()
 
688
            wt = result.create_workingtree()
 
689
            if wt.inventory.root is None:
 
690
                try:
 
691
                    wt.set_root_id(self.open_workingtree.get_root_id())
 
692
                except errors.NoWorkingTree:
 
693
                    pass
684
694
        return result
685
695
 
686
696
 
690
700
    def __init__(self, _transport, _format):
691
701
        """See BzrDir.__init__."""
692
702
        super(BzrDirPreSplitOut, self).__init__(_transport, _format)
693
 
        assert self._format._lock_class == TransportLock
 
703
        assert self._format._lock_class == lockable_files.TransportLock
694
704
        assert self._format._lock_file_name == 'branch-lock'
695
 
        self._control_files = LockableFiles(self.get_branch_transport(None),
 
705
        self._control_files = lockable_files.LockableFiles(
 
706
                                            self.get_branch_transport(None),
696
707
                                            self._format._lock_file_name,
697
708
                                            self._format._lock_class)
698
709
 
742
753
        # done on this format anyway. So - acceptable wart.
743
754
        result = self.open_workingtree()
744
755
        if revision_id is not None:
745
 
            if revision_id == bzrlib.revision.NULL_REVISION:
 
756
            if revision_id == _mod_revision.NULL_REVISION:
746
757
                result.set_parent_ids([])
747
758
            else:
748
759
                result.set_parent_ids([revision_id])
915
926
 
916
927
    def _get_mkdir_mode(self):
917
928
        """Figure out the mode to use when creating a bzrdir subdir."""
918
 
        temp_control = LockableFiles(self.transport, '', TransportLock)
 
929
        temp_control = lockable_files.LockableFiles(self.transport, '',
 
930
                                     lockable_files.TransportLock)
919
931
        return temp_control._dir_mode
920
932
 
921
933
    def get_branch_transport(self, branch_format):
1097
1109
        """Initialize a new bzrdir in the base directory of a Transport."""
1098
1110
        # Since we don't have a .bzr directory, inherit the
1099
1111
        # mode from the root directory
1100
 
        temp_control = LockableFiles(transport, '', TransportLock)
 
1112
        temp_control = lockable_files.LockableFiles(transport,
 
1113
                            '', lockable_files.TransportLock)
1101
1114
        temp_control._transport.mkdir('.bzr',
1102
1115
                                      # FIXME: RBC 20060121 don't peek under
1103
1116
                                      # the covers
1112
1125
                      ('branch-format', self.get_format_string()),
1113
1126
                      ]
1114
1127
        # NB: no need to escape relative paths that are url safe.
1115
 
        control_files = LockableFiles(control, self._lock_file_name, 
1116
 
                                      self._lock_class)
 
1128
        control_files = lockable_files.LockableFiles(control,
 
1129
                            self._lock_file_name, self._lock_class)
1117
1130
        control_files.create_lock()
1118
1131
        control_files.lock_write()
1119
1132
        try:
1161
1174
        _found is a private parameter, do not use it.
1162
1175
        """
1163
1176
        if not _found:
1164
 
            assert isinstance(BzrDirFormat.find_format(transport),
1165
 
                              self.__class__)
 
1177
            found_format = BzrDirFormat.find_format(transport)
 
1178
            if not isinstance(found_format, self.__class__):
 
1179
                raise AssertionError("%s was asked to open %s, but it seems to need "
 
1180
                        "format %s" 
 
1181
                        % (self, transport, found_format))
1166
1182
        return self._open(transport)
1167
1183
 
1168
1184
    def _open(self, transport):
1222
1238
    removed in format 5; write support for this format has been removed.
1223
1239
    """
1224
1240
 
1225
 
    _lock_class = TransportLock
 
1241
    _lock_class = lockable_files.TransportLock
1226
1242
 
1227
1243
    def get_format_string(self):
1228
1244
        """See BzrDirFormat.get_format_string()."""
1272
1288
       Unhashed stores in the repository.
1273
1289
    """
1274
1290
 
1275
 
    _lock_class = TransportLock
 
1291
    _lock_class = lockable_files.TransportLock
1276
1292
 
1277
1293
    def get_format_string(self):
1278
1294
        """See BzrDirFormat.get_format_string()."""
1331
1347
     - Format 6 repositories [always]
1332
1348
    """
1333
1349
 
1334
 
    _lock_class = TransportLock
 
1350
    _lock_class = lockable_files.TransportLock
1335
1351
 
1336
1352
    def get_format_string(self):
1337
1353
        """See BzrDirFormat.get_format_string()."""
1391
1407
     - Format 7 repositories [optional]
1392
1408
    """
1393
1409
 
1394
 
    _lock_class = LockDir
 
1410
    _lock_class = lockdir.LockDir
1395
1411
 
1396
1412
    def get_converter(self, format=None):
1397
1413
        """See BzrDirFormat.get_converter()."""
1451
1467
        self._formats = formats
1452
1468
    
1453
1469
    def adapt(self, test):
1454
 
        result = TestSuite()
 
1470
        result = unittest.TestSuite()
1455
1471
        for format in self._formats:
1456
1472
            new_test = deepcopy(test)
1457
1473
            new_test.transport_server = self._transport_server
1555
1571
        self.bzrdir.transport.delete_tree('text-store')
1556
1572
 
1557
1573
    def _convert_working_inv(self):
1558
 
        inv = serializer_v4.read_inventory(self.branch.control_files.get('inventory'))
1559
 
        new_inv_xml = bzrlib.xml5.serializer_v5.write_inventory_to_string(inv)
 
1574
        inv = xml4.serializer_v4.read_inventory(
 
1575
                    self.branch.control_files.get('inventory'))
 
1576
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv)
1560
1577
        # FIXME inventory is a working tree change.
1561
1578
        self.branch.control_files.put('inventory', StringIO(new_inv_xml))
1562
1579
 
1588
1605
                                                      prefixed=False,
1589
1606
                                                      compressed=True))
1590
1607
        try:
1591
 
            transaction = bzrlib.transactions.WriteTransaction()
 
1608
            transaction = WriteTransaction()
1592
1609
            for i, rev_id in enumerate(self.converted_revs):
1593
1610
                self.pb.update('write revision', i, len(self.converted_revs))
1594
1611
                _revision_store.add_revision(self.revisions[rev_id], transaction)
1620
1637
    def _load_old_inventory(self, rev_id):
1621
1638
        assert rev_id not in self.converted_revs
1622
1639
        old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
1623
 
        inv = serializer_v4.read_inventory_from_string(old_inv_xml)
 
1640
        inv = xml4.serializer_v4.read_inventory_from_string(old_inv_xml)
1624
1641
        inv.revision_id = rev_id
1625
1642
        rev = self.revisions[rev_id]
1626
1643
        if rev.inventory_sha1:
1631
1648
    def _load_updated_inventory(self, rev_id):
1632
1649
        assert rev_id in self.converted_revs
1633
1650
        inv_xml = self.inv_weave.get_text(rev_id)
1634
 
        inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(inv_xml)
 
1651
        inv = xml5.serializer_v5.read_inventory_from_string(inv_xml)
1635
1652
        return inv
1636
1653
 
1637
1654
    def _convert_one_rev(self, rev_id):
1653
1670
                assert getattr(ie, 'revision', None) is not None, \
1654
1671
                    'no revision on {%s} in {%s}' % \
1655
1672
                    (file_id, rev.revision_id)
1656
 
        new_inv_xml = bzrlib.xml5.serializer_v5.write_inventory_to_string(inv)
 
1673
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv)
1657
1674
        new_inv_sha1 = sha_string(new_inv_xml)
1658
1675
        self.inv_weave.add_lines(rev.revision_id, 
1659
1676
                                 present_parents,
1692
1709
                                                  entry_vf=w)
1693
1710
        for old_revision in previous_entries:
1694
1711
                # if this fails, its a ghost ?
1695
 
                assert old_revision in self.converted_revs 
 
1712
                assert old_revision in self.converted_revs, \
 
1713
                    "Revision {%s} not in converted_revs" % old_revision
1696
1714
        self.snapshot_ie(previous_entries, ie, w, rev_id)
1697
1715
        del ie.text_id
1698
1716
        assert getattr(ie, 'revision', None) is not None
1871
1889
    def make_lock(self, name):
1872
1890
        """Make a lock for the new control dir name."""
1873
1891
        self.step('Make %s lock' % name)
1874
 
        ld = LockDir(self.bzrdir.transport, 
1875
 
                     '%s/lock' % name,
1876
 
                     file_modebits=self.file_mode,
1877
 
                     dir_modebits=self.dir_mode)
 
1892
        ld = lockdir.LockDir(self.bzrdir.transport,
 
1893
                             '%s/lock' % name,
 
1894
                             file_modebits=self.file_mode,
 
1895
                             dir_modebits=self.dir_mode)
1878
1896
        ld.create()
1879
1897
 
1880
1898
    def move_entry(self, new_dir, entry):