~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/memorytree.py

  • Committer: Jelmer Vernooij
  • Date: 2011-04-05 01:12:15 UTC
  • mto: This revision was merged to the branch mainline in revision 5757.
  • Revision ID: jelmer@samba.org-20110405011215-8g6izwf3uz8v4174
Remove some unnecessary imports, clean up lazy imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 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
 
 
17
"""MemoryTree object.
 
18
 
 
19
See MemoryTree for more details.
 
20
"""
 
21
 
 
22
 
 
23
import os
 
24
 
 
25
from bzrlib import (
 
26
    errors,
 
27
    mutabletree,
 
28
    revision as _mod_revision,
 
29
    )
 
30
from bzrlib.decorators import needs_read_lock
 
31
from bzrlib.osutils import sha_file
 
32
from bzrlib.mutabletree import needs_tree_write_lock
 
33
from bzrlib.transport.memory import MemoryTransport
 
34
 
 
35
 
 
36
class MemoryTree(mutabletree.MutableTree):
 
37
    """A MemoryTree is a specialisation of MutableTree.
 
38
 
 
39
    It maintains nearly no state outside of read_lock and write_lock
 
40
    transactions. (it keeps a reference to the branch, and its last-revision
 
41
    only).
 
42
    """
 
43
 
 
44
    def __init__(self, branch, revision_id):
 
45
        """Construct a MemoryTree for branch using revision_id."""
 
46
        self.branch = branch
 
47
        self.bzrdir = branch.bzrdir
 
48
        self._branch_revision_id = revision_id
 
49
        self._locks = 0
 
50
        self._lock_mode = None
 
51
 
 
52
    def is_control_filename(self, filename):
 
53
        # Memory tree doesn't have any control filenames
 
54
        return False
 
55
 
 
56
    @needs_tree_write_lock
 
57
    def _add(self, files, ids, kinds):
 
58
        """See MutableTree._add."""
 
59
        for f, file_id, kind in zip(files, ids, kinds):
 
60
            if kind is None:
 
61
                kind = 'file'
 
62
            if file_id is None:
 
63
                self._inventory.add_path(f, kind=kind)
 
64
            else:
 
65
                self._inventory.add_path(f, kind=kind, file_id=file_id)
 
66
 
 
67
    def basis_tree(self):
 
68
        """See Tree.basis_tree()."""
 
69
        return self._basis_tree
 
70
 
 
71
    @staticmethod
 
72
    def create_on_branch(branch):
 
73
        """Create a MemoryTree for branch, using the last-revision of branch."""
 
74
        revision_id = _mod_revision.ensure_null(branch.last_revision())
 
75
        return MemoryTree(branch, revision_id)
 
76
 
 
77
    def _gather_kinds(self, files, kinds):
 
78
        """See MutableTree._gather_kinds.
 
79
 
 
80
        This implementation does not care about the file kind of
 
81
        missing files, so is a no-op.
 
82
        """
 
83
 
 
84
    def get_file(self, file_id, path=None):
 
85
        """See Tree.get_file."""
 
86
        if path is None:
 
87
            path = self.id2path(file_id)
 
88
        return self._file_transport.get(path)
 
89
 
 
90
    def get_file_sha1(self, file_id, path=None, stat_value=None):
 
91
        """See Tree.get_file_sha1()."""
 
92
        if path is None:
 
93
            path = self.id2path(file_id)
 
94
        stream = self._file_transport.get(path)
 
95
        return sha_file(stream)
 
96
 
 
97
    def get_root_id(self):
 
98
        return self.path2id('')
 
99
 
 
100
    def _comparison_data(self, entry, path):
 
101
        """See Tree._comparison_data."""
 
102
        if entry is None:
 
103
            return None, False, None
 
104
        return entry.kind, entry.executable, None
 
105
 
 
106
    @needs_tree_write_lock
 
107
    def rename_one(self, from_rel, to_rel):
 
108
        file_id = self.path2id(from_rel)
 
109
        to_dir, to_tail = os.path.split(to_rel)
 
110
        to_parent_id = self.path2id(to_dir)
 
111
        self._file_transport.move(from_rel, to_rel)
 
112
        self._inventory.rename(file_id, to_parent_id, to_tail)
 
113
 
 
114
    def path_content_summary(self, path):
 
115
        """See Tree.path_content_summary."""
 
116
        id = self.path2id(path)
 
117
        if id is None:
 
118
            return 'missing', None, None, None
 
119
        kind = self.kind(id)
 
120
        if kind == 'file':
 
121
            bytes = self._file_transport.get_bytes(path)
 
122
            size = len(bytes)
 
123
            executable = self._inventory[id].executable
 
124
            sha1 = None # no stat cache
 
125
            return (kind, size, executable, sha1)
 
126
        elif kind == 'directory':
 
127
            # memory tree does not support nested trees yet.
 
128
            return kind, None, None, None
 
129
        elif kind == 'symlink':
 
130
            raise NotImplementedError('symlink support')
 
131
        else:
 
132
            raise NotImplementedError('unknown kind')
 
133
 
 
134
    def _file_size(self, entry, stat_value):
 
135
        """See Tree._file_size."""
 
136
        if entry is None:
 
137
            return 0
 
138
        return entry.text_size
 
139
 
 
140
    @needs_read_lock
 
141
    def get_parent_ids(self):
 
142
        """See Tree.get_parent_ids.
 
143
 
 
144
        This implementation returns the current cached value from
 
145
            self._parent_ids.
 
146
        """
 
147
        return list(self._parent_ids)
 
148
 
 
149
    def has_filename(self, filename):
 
150
        """See Tree.has_filename()."""
 
151
        return self._file_transport.has(filename)
 
152
 
 
153
    def is_executable(self, file_id, path=None):
 
154
        return self._inventory[file_id].executable
 
155
 
 
156
    def kind(self, file_id):
 
157
        return self._inventory[file_id].kind
 
158
 
 
159
    def mkdir(self, path, file_id=None):
 
160
        """See MutableTree.mkdir()."""
 
161
        self.add(path, file_id, 'directory')
 
162
        if file_id is None:
 
163
            file_id = self.path2id(path)
 
164
        self._file_transport.mkdir(path)
 
165
        return file_id
 
166
 
 
167
    @needs_read_lock
 
168
    def last_revision(self):
 
169
        """See MutableTree.last_revision."""
 
170
        return self._branch_revision_id
 
171
 
 
172
    def lock_read(self):
 
173
        """Lock the memory tree for reading.
 
174
 
 
175
        This triggers population of data from the branch for its revision.
 
176
        """
 
177
        self._locks += 1
 
178
        try:
 
179
            if self._locks == 1:
 
180
                self.branch.lock_read()
 
181
                self._lock_mode = "r"
 
182
                self._populate_from_branch()
 
183
        except:
 
184
            self._locks -= 1
 
185
            raise
 
186
 
 
187
    def lock_tree_write(self):
 
188
        """See MutableTree.lock_tree_write()."""
 
189
        self._locks += 1
 
190
        try:
 
191
            if self._locks == 1:
 
192
                self.branch.lock_read()
 
193
                self._lock_mode = "w"
 
194
                self._populate_from_branch()
 
195
            elif self._lock_mode == "r":
 
196
                raise errors.ReadOnlyError(self)
 
197
        except:
 
198
            self._locks -= 1
 
199
            raise
 
200
 
 
201
    def lock_write(self):
 
202
        """See MutableTree.lock_write()."""
 
203
        self._locks += 1
 
204
        try:
 
205
            if self._locks == 1:
 
206
                self.branch.lock_write()
 
207
                self._lock_mode = "w"
 
208
                self._populate_from_branch()
 
209
            elif self._lock_mode == "r":
 
210
                raise errors.ReadOnlyError(self)
 
211
        except:
 
212
            self._locks -= 1
 
213
            raise
 
214
 
 
215
    def _populate_from_branch(self):
 
216
        """Populate the in-tree state from the branch."""
 
217
        self._set_basis()
 
218
        if self._branch_revision_id == _mod_revision.NULL_REVISION:
 
219
            self._parent_ids = []
 
220
        else:
 
221
            self._parent_ids = [self._branch_revision_id]
 
222
        self._inventory = self._basis_tree._inventory._get_mutable_inventory()
 
223
        self._file_transport = MemoryTransport()
 
224
        # TODO copy the revision trees content, or do it lazy, or something.
 
225
        inventory_entries = self._inventory.iter_entries()
 
226
        for path, entry in inventory_entries:
 
227
            if path == '':
 
228
                continue
 
229
            if entry.kind == 'directory':
 
230
                self._file_transport.mkdir(path)
 
231
            elif entry.kind == 'file':
 
232
                self._file_transport.put_file(path,
 
233
                    self._basis_tree.get_file(entry.file_id))
 
234
            else:
 
235
                raise NotImplementedError(self._populate_from_branch)
 
236
 
 
237
    def put_file_bytes_non_atomic(self, file_id, bytes):
 
238
        """See MutableTree.put_file_bytes_non_atomic."""
 
239
        self._file_transport.put_bytes(self.id2path(file_id), bytes)
 
240
 
 
241
    def unlock(self):
 
242
        """Release a lock.
 
243
 
 
244
        This frees all cached state when the last lock context for the tree is
 
245
        left.
 
246
        """
 
247
        if self._locks == 1:
 
248
            self._basis_tree = None
 
249
            self._parent_ids = []
 
250
            self._inventory = None
 
251
            try:
 
252
                self.branch.unlock()
 
253
            finally:
 
254
                self._locks = 0
 
255
                self._lock_mode = None
 
256
        else:
 
257
            self._locks -= 1
 
258
 
 
259
    @needs_tree_write_lock
 
260
    def unversion(self, file_ids):
 
261
        """Remove the file ids in file_ids from the current versioned set.
 
262
 
 
263
        When a file_id is unversioned, all of its children are automatically
 
264
        unversioned.
 
265
 
 
266
        :param file_ids: The file ids to stop versioning.
 
267
        :raises: NoSuchId if any fileid is not currently versioned.
 
268
        """
 
269
        # XXX: This should be in mutabletree, but the inventory-save action
 
270
        # is not relevant to memory tree. Until that is done in unlock by
 
271
        # working tree, we cannot share the implementation.
 
272
        for file_id in file_ids:
 
273
            if self._inventory.has_id(file_id):
 
274
                self._inventory.remove_recursive_id(file_id)
 
275
            else:
 
276
                raise errors.NoSuchId(self, file_id)
 
277
 
 
278
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
 
279
        """See MutableTree.set_parent_trees()."""
 
280
        for revision_id in revision_ids:
 
281
            _mod_revision.check_not_reserved_id(revision_id)
 
282
        if len(revision_ids) == 0:
 
283
            self._parent_ids = []
 
284
            self._branch_revision_id = _mod_revision.NULL_REVISION
 
285
        else:
 
286
            self._parent_ids = revision_ids
 
287
            self._branch_revision_id = revision_ids[0]
 
288
        self._allow_leftmost_as_ghost = allow_leftmost_as_ghost
 
289
        self._set_basis()
 
290
    
 
291
    def _set_basis(self):
 
292
        try:
 
293
            self._basis_tree = self.branch.repository.revision_tree(
 
294
                self._branch_revision_id)
 
295
        except errors.NoSuchRevision:
 
296
            if self._allow_leftmost_as_ghost:
 
297
                self._basis_tree = self.branch.repository.revision_tree(
 
298
                    _mod_revision.NULL_REVISION)
 
299
            else:
 
300
                raise
 
301
 
 
302
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
 
303
        """See MutableTree.set_parent_trees()."""
 
304
        if len(parents_list) == 0:
 
305
            self._parent_ids = []
 
306
            self._basis_tree = self.branch.repository.revision_tree(
 
307
                                   _mod_revision.NULL_REVISION)
 
308
        else:
 
309
            if parents_list[0][1] is None and not allow_leftmost_as_ghost:
 
310
                # a ghost in the left most parent
 
311
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
 
312
            self._parent_ids = [parent_id for parent_id, tree in parents_list]
 
313
            if parents_list[0][1] is None or parents_list[0][1] == 'null:':
 
314
                self._basis_tree = self.branch.repository.revision_tree(
 
315
                                       _mod_revision.NULL_REVISION)
 
316
            else:
 
317
                self._basis_tree = parents_list[0][1]
 
318
            self._branch_revision_id = parents_list[0][0]