~bzr-pqm/bzr/bzr.dev

1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
16
17
"""MemoryTree object.
18
19
See MemoryTree for more details.
20
"""
21
6379.6.7 by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear.
22
from __future__ import absolute_import
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
23
3567.5.1 by John Arbash Meinel
Implement rename_one on MemoryTree, and expose that in the Branch Builder
24
import os
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
25
2598.5.1 by Aaron Bentley
Start eliminating the use of None to indicate null revision
26
from bzrlib import (
27
    errors,
28
    mutabletree,
29
    revision as _mod_revision,
30
    )
5121.2.4 by Jelmer Vernooij
Remove more unused imports.
31
from bzrlib.decorators import needs_read_lock
5802.1.1 by Jelmer Vernooij
Move Inventory._get_mutable_inventory -> mutable_inventory_from_tree.
32
from bzrlib.inventory import Inventory
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
33
from bzrlib.osutils import sha_file
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
34
from bzrlib.mutabletree import needs_tree_write_lock
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
35
from bzrlib.transport.memory import MemoryTransport
36
37
5777.4.1 by Jelmer Vernooij
Split inventory-specific code out of MutableTree into MutableInventoryTree.
38
class MemoryTree(mutabletree.MutableInventoryTree):
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
39
    """A MemoryTree is a specialisation of MutableTree.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
40
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
41
    It maintains nearly no state outside of read_lock and write_lock
42
    transactions. (it keeps a reference to the branch, and its last-revision
43
    only).
44
    """
45
46
    def __init__(self, branch, revision_id):
47
        """Construct a MemoryTree for branch using revision_id."""
48
        self.branch = branch
49
        self.bzrdir = branch.bzrdir
50
        self._branch_revision_id = revision_id
51
        self._locks = 0
52
        self._lock_mode = None
53
6449.6.7 by Jelmer Vernooij
Fix tests.
54
    def get_config_stack(self):
55
        return self.branch.get_config_stack()
56
5699.2.1 by Jelmer Vernooij
Move is_control_filename() from Tree to MutableTree.
57
    def is_control_filename(self, filename):
58
        # Memory tree doesn't have any control filenames
59
        return False
60
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
61
    @needs_tree_write_lock
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
62
    def _add(self, files, ids, kinds):
63
        """See MutableTree._add."""
64
        for f, file_id, kind in zip(files, ids, kinds):
65
            if kind is None:
66
                kind = 'file'
67
            if file_id is None:
68
                self._inventory.add_path(f, kind=kind)
69
            else:
70
                self._inventory.add_path(f, kind=kind, file_id=file_id)
71
72
    def basis_tree(self):
73
        """See Tree.basis_tree()."""
74
        return self._basis_tree
75
76
    @staticmethod
77
    def create_on_branch(branch):
78
        """Create a MemoryTree for branch, using the last-revision of branch."""
2598.5.4 by Aaron Bentley
Restore original Branch.last_revision behavior, fix bits that care
79
        revision_id = _mod_revision.ensure_null(branch.last_revision())
2598.5.1 by Aaron Bentley
Start eliminating the use of None to indicate null revision
80
        return MemoryTree(branch, revision_id)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
81
82
    def _gather_kinds(self, files, kinds):
83
        """See MutableTree._gather_kinds.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
84
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
85
        This implementation does not care about the file kind of
86
        missing files, so is a no-op.
87
        """
88
2743.3.3 by Ian Clatworthy
Skip path lookup for tree.get_file() when we already know the path
89
    def get_file(self, file_id, path=None):
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
90
        """See Tree.get_file."""
2743.3.3 by Ian Clatworthy
Skip path lookup for tree.get_file() when we already know the path
91
        if path is None:
92
            path = self.id2path(file_id)
93
        return self._file_transport.get(path)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
94
2564.2.1 by Ian Clatworthy
refactor commit to support alternative population meothds
95
    def get_file_sha1(self, file_id, path=None, stat_value=None):
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
96
        """See Tree.get_file_sha1()."""
97
        if path is None:
98
            path = self.id2path(file_id)
99
        stream = self._file_transport.get(path)
100
        return sha_file(stream)
101
2946.3.5 by John Arbash Meinel
MemoryTree is not tested under the tree_implementations tests.
102
    def get_root_id(self):
103
        return self.path2id('')
104
2564.2.1 by Ian Clatworthy
refactor commit to support alternative population meothds
105
    def _comparison_data(self, entry, path):
106
        """See Tree._comparison_data."""
107
        if entry is None:
108
            return None, False, None
109
        return entry.kind, entry.executable, None
110
3567.5.1 by John Arbash Meinel
Implement rename_one on MemoryTree, and expose that in the Branch Builder
111
    @needs_tree_write_lock
112
    def rename_one(self, from_rel, to_rel):
113
        file_id = self.path2id(from_rel)
114
        to_dir, to_tail = os.path.split(to_rel)
3514.4.44 by John Arbash Meinel
Revert the path2id fix, because to_dir can be anywhere, not just
115
        to_parent_id = self.path2id(to_dir)
3567.5.1 by John Arbash Meinel
Implement rename_one on MemoryTree, and expose that in the Branch Builder
116
        self._file_transport.move(from_rel, to_rel)
117
        self._inventory.rename(file_id, to_parent_id, to_tail)
3514.4.38 by John Arbash Meinel
Use direct access to the inventory instead of path2id.
118
2776.4.2 by Robert Collins
nuke _read_tree_state and snapshot from inventory, moving responsibility into the commit builder.
119
    def path_content_summary(self, path):
120
        """See Tree.path_content_summary."""
121
        id = self.path2id(path)
122
        if id is None:
123
            return 'missing', None, None, None
124
        kind = self.kind(id)
125
        if kind == 'file':
126
            bytes = self._file_transport.get_bytes(path)
127
            size = len(bytes)
128
            executable = self._inventory[id].executable
129
            sha1 = None # no stat cache
130
            return (kind, size, executable, sha1)
131
        elif kind == 'directory':
132
            # memory tree does not support nested trees yet.
133
            return kind, None, None, None
134
        elif kind == 'symlink':
135
            raise NotImplementedError('symlink support')
136
        else:
137
            raise NotImplementedError('unknown kind')
138
2564.2.1 by Ian Clatworthy
refactor commit to support alternative population meothds
139
    def _file_size(self, entry, stat_value):
140
        """See Tree._file_size."""
141
        if entry is None:
142
            return 0
143
        return entry.text_size
144
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
145
    @needs_read_lock
146
    def get_parent_ids(self):
147
        """See Tree.get_parent_ids.
148
149
        This implementation returns the current cached value from
150
            self._parent_ids.
151
        """
152
        return list(self._parent_ids)
153
154
    def has_filename(self, filename):
155
        """See Tree.has_filename()."""
156
        return self._file_transport.has(filename)
157
158
    def is_executable(self, file_id, path=None):
159
        return self._inventory[file_id].executable
160
1959.4.2 by Aaron Bentley
Merge bzr.dev
161
    def kind(self, file_id):
162
        return self._inventory[file_id].kind
163
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
164
    def mkdir(self, path, file_id=None):
165
        """See MutableTree.mkdir()."""
166
        self.add(path, file_id, 'directory')
167
        if file_id is None:
168
            file_id = self.path2id(path)
169
        self._file_transport.mkdir(path)
170
        return file_id
171
1986.1.6 by Robert Collins
Add MemoryTree.last_revision.
172
    @needs_read_lock
173
    def last_revision(self):
174
        """See MutableTree.last_revision."""
175
        return self._branch_revision_id
176
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
177
    def lock_read(self):
178
        """Lock the memory tree for reading.
179
180
        This triggers population of data from the branch for its revision.
181
        """
182
        self._locks += 1
183
        try:
184
            if self._locks == 1:
185
                self.branch.lock_read()
186
                self._lock_mode = "r"
187
                self._populate_from_branch()
188
        except:
189
            self._locks -= 1
190
            raise
191
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
192
    def lock_tree_write(self):
193
        """See MutableTree.lock_tree_write()."""
194
        self._locks += 1
195
        try:
196
            if self._locks == 1:
197
                self.branch.lock_read()
198
                self._lock_mode = "w"
199
                self._populate_from_branch()
200
            elif self._lock_mode == "r":
201
                raise errors.ReadOnlyError(self)
202
        except:
203
            self._locks -= 1
204
            raise
205
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
206
    def lock_write(self):
207
        """See MutableTree.lock_write()."""
208
        self._locks += 1
209
        try:
210
            if self._locks == 1:
211
                self.branch.lock_write()
212
                self._lock_mode = "w"
213
                self._populate_from_branch()
214
            elif self._lock_mode == "r":
215
                raise errors.ReadOnlyError(self)
216
        except:
217
            self._locks -= 1
218
            raise
219
220
    def _populate_from_branch(self):
221
        """Populate the in-tree state from the branch."""
4190.1.4 by Robert Collins
Cache ghosts when we can get them from a RemoteRepository in get_parent_map.
222
        self._set_basis()
3668.5.4 by Jelmer Vernooij
Eliminate more uses of Repository.revision_tree(None).
223
        if self._branch_revision_id == _mod_revision.NULL_REVISION:
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
224
            self._parent_ids = []
225
        else:
226
            self._parent_ids = [self._branch_revision_id]
5802.1.1 by Jelmer Vernooij
Move Inventory._get_mutable_inventory -> mutable_inventory_from_tree.
227
        self._inventory = Inventory(None, self._basis_tree.get_revision_id())
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
228
        self._file_transport = MemoryTransport()
229
        # TODO copy the revision trees content, or do it lazy, or something.
5802.1.1 by Jelmer Vernooij
Move Inventory._get_mutable_inventory -> mutable_inventory_from_tree.
230
        inventory_entries = self._basis_tree.iter_entries_by_dir()
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
231
        for path, entry in inventory_entries:
5802.1.1 by Jelmer Vernooij
Move Inventory._get_mutable_inventory -> mutable_inventory_from_tree.
232
            self._inventory.add(entry.copy())
1731.1.50 by Aaron Bentley
Merge bzr.dev
233
            if path == '':
234
                continue
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
235
            if entry.kind == 'directory':
236
                self._file_transport.mkdir(path)
237
            elif entry.kind == 'file':
1986.1.4 by Robert Collins
Fixup deprecations from bzr.dev.
238
                self._file_transport.put_file(path,
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
239
                    self._basis_tree.get_file(entry.file_id))
240
            else:
241
                raise NotImplementedError(self._populate_from_branch)
242
243
    def put_file_bytes_non_atomic(self, file_id, bytes):
244
        """See MutableTree.put_file_bytes_non_atomic."""
1986.1.4 by Robert Collins
Fixup deprecations from bzr.dev.
245
        self._file_transport.put_bytes(self.id2path(file_id), bytes)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
246
247
    def unlock(self):
248
        """Release a lock.
249
250
        This frees all cached state when the last lock context for the tree is
251
        left.
252
        """
253
        if self._locks == 1:
254
            self._basis_tree = None
255
            self._parent_ids = []
256
            self._inventory = None
257
            try:
258
                self.branch.unlock()
259
            finally:
260
                self._locks = 0
261
                self._lock_mode = None
262
        else:
263
            self._locks -= 1
264
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
265
    @needs_tree_write_lock
1986.1.3 by Robert Collins
Merge bzr.dev.
266
    def unversion(self, file_ids):
267
        """Remove the file ids in file_ids from the current versioned set.
268
269
        When a file_id is unversioned, all of its children are automatically
270
        unversioned.
271
272
        :param file_ids: The file ids to stop versioning.
273
        :raises: NoSuchId if any fileid is not currently versioned.
274
        """
275
        # XXX: This should be in mutabletree, but the inventory-save action
276
        # is not relevant to memory tree. Until that is done in unlock by
277
        # working tree, we cannot share the implementation.
278
        for file_id in file_ids:
279
            if self._inventory.has_id(file_id):
280
                self._inventory.remove_recursive_id(file_id)
281
            else:
282
                raise errors.NoSuchId(self, file_id)
283
2418.5.1 by John Arbash Meinel
Make a Branch helper which can create a very basic MemoryTree with history.
284
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
285
        """See MutableTree.set_parent_trees()."""
2598.5.2 by Aaron Bentley
Got all tests passing with Branch returning 'null:' for null revision
286
        for revision_id in revision_ids:
287
            _mod_revision.check_not_reserved_id(revision_id)
2418.5.1 by John Arbash Meinel
Make a Branch helper which can create a very basic MemoryTree with history.
288
        if len(revision_ids) == 0:
289
            self._parent_ids = []
4190.1.4 by Robert Collins
Cache ghosts when we can get them from a RemoteRepository in get_parent_map.
290
            self._branch_revision_id = _mod_revision.NULL_REVISION
2418.5.1 by John Arbash Meinel
Make a Branch helper which can create a very basic MemoryTree with history.
291
        else:
292
            self._parent_ids = revision_ids
293
            self._branch_revision_id = revision_ids[0]
4190.1.4 by Robert Collins
Cache ghosts when we can get them from a RemoteRepository in get_parent_map.
294
        self._allow_leftmost_as_ghost = allow_leftmost_as_ghost
295
        self._set_basis()
296
    
297
    def _set_basis(self):
298
        try:
299
            self._basis_tree = self.branch.repository.revision_tree(
300
                self._branch_revision_id)
301
        except errors.NoSuchRevision:
302
            if self._allow_leftmost_as_ghost:
303
                self._basis_tree = self.branch.repository.revision_tree(
304
                    _mod_revision.NULL_REVISION)
305
            else:
306
                raise
2418.5.1 by John Arbash Meinel
Make a Branch helper which can create a very basic MemoryTree with history.
307
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
308
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
309
        """See MutableTree.set_parent_trees()."""
310
        if len(parents_list) == 0:
311
            self._parent_ids = []
3668.5.1 by Jelmer Vernooij
Use NULL_REVISION rather than None for Repository.revision_tree().
312
            self._basis_tree = self.branch.repository.revision_tree(
313
                                   _mod_revision.NULL_REVISION)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
314
        else:
315
            if parents_list[0][1] is None and not allow_leftmost_as_ghost:
316
                # a ghost in the left most parent
317
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
318
            self._parent_ids = [parent_id for parent_id, tree in parents_list]
2598.5.2 by Aaron Bentley
Got all tests passing with Branch returning 'null:' for null revision
319
            if parents_list[0][1] is None or parents_list[0][1] == 'null:':
3668.5.1 by Jelmer Vernooij
Use NULL_REVISION rather than None for Repository.revision_tree().
320
                self._basis_tree = self.branch.repository.revision_tree(
321
                                       _mod_revision.NULL_REVISION)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
322
            else:
323
                self._basis_tree = parents_list[0][1]
1986.1.6 by Robert Collins
Add MemoryTree.last_revision.
324
            self._branch_revision_id = parents_list[0][0]