~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

  • Committer: Robert Collins
  • Date: 2006-02-11 11:58:06 UTC
  • mto: (1534.1.22 integration)
  • mto: This revision was merged to the branch mainline in revision 1554.
  • Revision ID: robertc@robertcollins.net-20060211115806-732dabc1e35714ed
Give format3 working trees their own last-revision marker.

Show diffs side-by-side

added added

removed removed

Lines of Context:
79
79
                            realpath,
80
80
                            relpath,
81
81
                            rename)
 
82
from bzrlib.revision import NULL_REVISION
82
83
from bzrlib.symbol_versioning import *
83
84
from bzrlib.textui import show_status
84
85
import bzrlib.tree
188
189
    """
189
190
 
190
191
    def __init__(self, basedir='.',
191
 
                 branch=None,
 
192
                 branch=DEPRECATED_PARAMETER,
192
193
                 _inventory=None,
193
194
                 _control_files=None,
194
195
                 _internal=False,
204
205
        self._format = _format
205
206
        self.bzrdir = _bzrdir
206
207
        if not _internal:
207
 
            # created via open etc.
 
208
            # not created via open etc.
 
209
            warn("WorkingTree() is deprecated ass of bzr version 0.8. "
 
210
                 "Please use bzrdir.open_workingtree or WorkingTree.open().",
 
211
                 DeprecationWarning,
 
212
                 stacklevel=2)
208
213
            wt = WorkingTree.open(basedir)
209
214
            self.branch = wt.branch
210
215
            self.basedir = wt.basedir
219
224
            "base directory %r is not a string" % basedir
220
225
        basedir = safe_unicode(basedir)
221
226
        mutter("openeing working tree %r", basedir)
222
 
        if branch is None:
223
 
            branch = Branch.open(basedir)
224
 
        assert isinstance(branch, Branch), \
225
 
            "branch %r is not a Branch" % branch
226
 
        self.branch = branch
 
227
        if deprecated_passed(branch):
 
228
            if not _internal:
 
229
                warn("WorkingTree(..., branch=XXX) is deprecated as of bzr 0.8."
 
230
                     " Please use bzrdir.open_workingtree().",
 
231
                     DeprecationWarning,
 
232
                     stacklevel=2
 
233
                     )
 
234
            self.branch = branch
 
235
        else:
 
236
            self.branch = self.bzrdir.open_branch()
 
237
        assert isinstance(self.branch, Branch), \
 
238
            "branch %r is not a Branch" % self.branch
227
239
        self.basedir = realpath(basedir)
228
240
        # if branch is at our basedir and is a format 6 or less
229
241
        if isinstance(self._format, WorkingTreeFormat2):
779
791
        >>> from bzrlib.bzrdir import ScratchDir
780
792
        >>> d = ScratchDir(files=['foo', 'foo~'])
781
793
        >>> b = d.open_branch()
782
 
        >>> tree = WorkingTree(b.base, b)
 
794
        >>> tree = d.open_workingtree()
783
795
        >>> map(str, tree.unknowns())
784
796
        ['foo']
785
797
        >>> tree.add('foo')
919
931
    def kind(self, file_id):
920
932
        return file_kind(self.id2abspath(file_id))
921
933
 
 
934
    @needs_read_lock
922
935
    def last_revision(self):
923
936
        """Return the last revision id of this working tree.
924
937
 
949
962
    def _basis_inventory_name(self, revision_id):
950
963
        return 'basis-inventory.%s' % revision_id
951
964
 
 
965
    @needs_write_lock
952
966
    def set_last_revision(self, new_revision, old_revision=None):
953
 
        if old_revision is not None:
954
 
            try:
955
 
                path = self._basis_inventory_name(old_revision)
956
 
                path = self._control_files._escape(path)
957
 
                self._control_files._transport.delete(path)
958
 
            except NoSuchFile:
959
 
                pass
 
967
        """Change the last revision in the working tree."""
 
968
        self._remove_old_basis(old_revision)
 
969
        if self._change_last_revision(new_revision):
 
970
            self._cache_basis_inventory(new_revision)
 
971
 
 
972
    def _change_last_revision(self, new_revision):
 
973
        """Template method part of set_last_revision to perform the change."""
960
974
        if new_revision is None:
961
975
            self.branch.set_revision_history([])
962
 
            return
 
976
            return False
963
977
        # current format is locked in with the branch
964
978
        revision_history = self.branch.revision_history()
965
979
        try:
967
981
        except ValueError:
968
982
            raise errors.NoSuchRevision(self.branch, new_revision)
969
983
        self.branch.set_revision_history(revision_history[:position + 1])
 
984
        return True
 
985
 
 
986
    def _cache_basis_inventory(self, new_revision):
 
987
        """Cache new_revision as the basis inventory."""
970
988
        try:
971
989
            xml = self.branch.repository.get_inventory_xml(new_revision)
972
990
            path = self._basis_inventory_name(new_revision)
974
992
        except WeaveRevisionNotPresent:
975
993
            pass
976
994
 
 
995
    def _remove_old_basis(self, old_revision):
 
996
        """Remove the old basis inventory 'old_revision'."""
 
997
        if old_revision is not None:
 
998
            try:
 
999
                path = self._basis_inventory_name(old_revision)
 
1000
                path = self._control_files._escape(path)
 
1001
                self._control_files._transport.delete(path)
 
1002
            except NoSuchFile:
 
1003
                pass
 
1004
 
977
1005
    def read_basis_inventory(self, revision_id):
978
1006
        """Read the cached basis inventory."""
979
1007
        path = self._basis_inventory_name(revision_id)
1119
1147
        self._control_files.put('inventory', sio)
1120
1148
        self._set_inventory(inv)
1121
1149
        mutter('wrote working inventory')
1122
 
            
 
1150
 
 
1151
 
 
1152
class WorkingTree3(WorkingTree):
 
1153
    """This is the Format 3 working tree.
 
1154
 
 
1155
    This differs from the base WorkingTree by:
 
1156
     - having its own file lock
 
1157
     - having its own last-revision property.
 
1158
    """
 
1159
 
 
1160
    @needs_read_lock
 
1161
    def last_revision(self):
 
1162
        """See WorkingTree.last_revision."""
 
1163
        try:
 
1164
            return self._control_files.get_utf8('last-revision').read()
 
1165
        except NoSuchFile:
 
1166
            return None
 
1167
 
 
1168
    def _change_last_revision(self, revision_id):
 
1169
        """See WorkingTree._change_last_revision."""
 
1170
        if revision_id is None or revision_id == NULL_REVISION:
 
1171
            try:
 
1172
                self._control_files._transport.delete('last-revision')
 
1173
            except errors.NoSuchFile:
 
1174
                pass
 
1175
            return False
 
1176
        else:
 
1177
            try:
 
1178
                self.branch.revision_history().index(revision_id)
 
1179
            except ValueError:
 
1180
                raise errors.NoSuchRevision(self.branch, revision_id)
 
1181
            self._control_files.put_utf8('last-revision', revision_id)
 
1182
            return True
 
1183
 
1123
1184
 
1124
1185
CONFLICT_SUFFIXES = ('.THIS', '.BASE', '.OTHER')
1125
1186
def get_conflicted_stem(path):
1173
1234
            format_string = transport.get("format").read()
1174
1235
            return klass._formats[format_string]
1175
1236
        except NoSuchFile:
1176
 
            raise errors.NotBranchError(path=transport.base)
 
1237
            raise errors.NoWorkingTree(base=transport.base)
1177
1238
        except KeyError:
1178
1239
            raise errors.UnknownFormatError(format_string)
1179
1240
 
1279
1340
        revision = branch.last_revision()
1280
1341
        basis_tree = branch.repository.revision_tree(revision)
1281
1342
        inv = basis_tree.inventory
1282
 
        wt = WorkingTree(a_bzrdir.root_transport.base,
 
1343
        wt = WorkingTree3(a_bzrdir.root_transport.base,
1283
1344
                         branch,
1284
1345
                         inv,
1285
1346
                         _internal=True,
1307
1368
            raise NotImplementedError
1308
1369
        if not isinstance(a_bzrdir.transport, LocalTransport):
1309
1370
            raise errors.NotLocalUrl(a_bzrdir.transport.base)
1310
 
        return WorkingTree(a_bzrdir.root_transport.base,
 
1371
        return WorkingTree3(a_bzrdir.root_transport.base,
1311
1372
                           _internal=True,
1312
1373
                           _format=self,
1313
1374
                           _bzrdir=a_bzrdir)