~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
5699.2.1 by Jelmer Vernooij
Move is_control_filename() from Tree to MutableTree.
54
    def is_control_filename(self, filename):
55
        # Memory tree doesn't have any control filenames
56
        return False
57
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
58
    @needs_tree_write_lock
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
59
    def _add(self, files, ids, kinds):
60
        """See MutableTree._add."""
61
        for f, file_id, kind in zip(files, ids, kinds):
62
            if kind is None:
63
                kind = 'file'
64
            if file_id is None:
65
                self._inventory.add_path(f, kind=kind)
66
            else:
67
                self._inventory.add_path(f, kind=kind, file_id=file_id)
68
69
    def basis_tree(self):
70
        """See Tree.basis_tree()."""
71
        return self._basis_tree
72
73
    @staticmethod
74
    def create_on_branch(branch):
75
        """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
76
        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
77
        return MemoryTree(branch, revision_id)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
78
79
    def _gather_kinds(self, files, kinds):
80
        """See MutableTree._gather_kinds.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
81
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
82
        This implementation does not care about the file kind of
83
        missing files, so is a no-op.
84
        """
85
2743.3.3 by Ian Clatworthy
Skip path lookup for tree.get_file() when we already know the path
86
    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
87
        """See Tree.get_file."""
2743.3.3 by Ian Clatworthy
Skip path lookup for tree.get_file() when we already know the path
88
        if path is None:
89
            path = self.id2path(file_id)
90
        return self._file_transport.get(path)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
91
2564.2.1 by Ian Clatworthy
refactor commit to support alternative population meothds
92
    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
93
        """See Tree.get_file_sha1()."""
94
        if path is None:
95
            path = self.id2path(file_id)
96
        stream = self._file_transport.get(path)
97
        return sha_file(stream)
98
2946.3.5 by John Arbash Meinel
MemoryTree is not tested under the tree_implementations tests.
99
    def get_root_id(self):
100
        return self.path2id('')
101
2564.2.1 by Ian Clatworthy
refactor commit to support alternative population meothds
102
    def _comparison_data(self, entry, path):
103
        """See Tree._comparison_data."""
104
        if entry is None:
105
            return None, False, None
106
        return entry.kind, entry.executable, None
107
3567.5.1 by John Arbash Meinel
Implement rename_one on MemoryTree, and expose that in the Branch Builder
108
    @needs_tree_write_lock
109
    def rename_one(self, from_rel, to_rel):
110
        file_id = self.path2id(from_rel)
111
        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
112
        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
113
        self._file_transport.move(from_rel, to_rel)
114
        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.
115
2776.4.2 by Robert Collins
nuke _read_tree_state and snapshot from inventory, moving responsibility into the commit builder.
116
    def path_content_summary(self, path):
117
        """See Tree.path_content_summary."""
118
        id = self.path2id(path)
119
        if id is None:
120
            return 'missing', None, None, None
121
        kind = self.kind(id)
122
        if kind == 'file':
123
            bytes = self._file_transport.get_bytes(path)
124
            size = len(bytes)
125
            executable = self._inventory[id].executable
126
            sha1 = None # no stat cache
127
            return (kind, size, executable, sha1)
128
        elif kind == 'directory':
129
            # memory tree does not support nested trees yet.
130
            return kind, None, None, None
131
        elif kind == 'symlink':
132
            raise NotImplementedError('symlink support')
133
        else:
134
            raise NotImplementedError('unknown kind')
135
2564.2.1 by Ian Clatworthy
refactor commit to support alternative population meothds
136
    def _file_size(self, entry, stat_value):
137
        """See Tree._file_size."""
138
        if entry is None:
139
            return 0
140
        return entry.text_size
141
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
142
    @needs_read_lock
143
    def get_parent_ids(self):
144
        """See Tree.get_parent_ids.
145
146
        This implementation returns the current cached value from
147
            self._parent_ids.
148
        """
149
        return list(self._parent_ids)
150
151
    def has_filename(self, filename):
152
        """See Tree.has_filename()."""
153
        return self._file_transport.has(filename)
154
155
    def is_executable(self, file_id, path=None):
156
        return self._inventory[file_id].executable
157
1959.4.2 by Aaron Bentley
Merge bzr.dev
158
    def kind(self, file_id):
159
        return self._inventory[file_id].kind
160
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
161
    def mkdir(self, path, file_id=None):
162
        """See MutableTree.mkdir()."""
163
        self.add(path, file_id, 'directory')
164
        if file_id is None:
165
            file_id = self.path2id(path)
166
        self._file_transport.mkdir(path)
167
        return file_id
168
1986.1.6 by Robert Collins
Add MemoryTree.last_revision.
169
    @needs_read_lock
170
    def last_revision(self):
171
        """See MutableTree.last_revision."""
172
        return self._branch_revision_id
173
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
174
    def lock_read(self):
175
        """Lock the memory tree for reading.
176
177
        This triggers population of data from the branch for its revision.
178
        """
179
        self._locks += 1
180
        try:
181
            if self._locks == 1:
182
                self.branch.lock_read()
183
                self._lock_mode = "r"
184
                self._populate_from_branch()
185
        except:
186
            self._locks -= 1
187
            raise
188
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
189
    def lock_tree_write(self):
190
        """See MutableTree.lock_tree_write()."""
191
        self._locks += 1
192
        try:
193
            if self._locks == 1:
194
                self.branch.lock_read()
195
                self._lock_mode = "w"
196
                self._populate_from_branch()
197
            elif self._lock_mode == "r":
198
                raise errors.ReadOnlyError(self)
199
        except:
200
            self._locks -= 1
201
            raise
202
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
203
    def lock_write(self):
204
        """See MutableTree.lock_write()."""
205
        self._locks += 1
206
        try:
207
            if self._locks == 1:
208
                self.branch.lock_write()
209
                self._lock_mode = "w"
210
                self._populate_from_branch()
211
            elif self._lock_mode == "r":
212
                raise errors.ReadOnlyError(self)
213
        except:
214
            self._locks -= 1
215
            raise
216
217
    def _populate_from_branch(self):
218
        """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.
219
        self._set_basis()
3668.5.4 by Jelmer Vernooij
Eliminate more uses of Repository.revision_tree(None).
220
        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
221
            self._parent_ids = []
222
        else:
223
            self._parent_ids = [self._branch_revision_id]
5802.1.1 by Jelmer Vernooij
Move Inventory._get_mutable_inventory -> mutable_inventory_from_tree.
224
        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
225
        self._file_transport = MemoryTransport()
226
        # 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.
227
        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
228
        for path, entry in inventory_entries:
5802.1.1 by Jelmer Vernooij
Move Inventory._get_mutable_inventory -> mutable_inventory_from_tree.
229
            self._inventory.add(entry.copy())
1731.1.50 by Aaron Bentley
Merge bzr.dev
230
            if path == '':
231
                continue
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
232
            if entry.kind == 'directory':
233
                self._file_transport.mkdir(path)
234
            elif entry.kind == 'file':
1986.1.4 by Robert Collins
Fixup deprecations from bzr.dev.
235
                self._file_transport.put_file(path,
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
236
                    self._basis_tree.get_file(entry.file_id))
237
            else:
238
                raise NotImplementedError(self._populate_from_branch)
239
240
    def put_file_bytes_non_atomic(self, file_id, bytes):
241
        """See MutableTree.put_file_bytes_non_atomic."""
1986.1.4 by Robert Collins
Fixup deprecations from bzr.dev.
242
        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
243
244
    def unlock(self):
245
        """Release a lock.
246
247
        This frees all cached state when the last lock context for the tree is
248
        left.
249
        """
250
        if self._locks == 1:
251
            self._basis_tree = None
252
            self._parent_ids = []
253
            self._inventory = None
254
            try:
255
                self.branch.unlock()
256
            finally:
257
                self._locks = 0
258
                self._lock_mode = None
259
        else:
260
            self._locks -= 1
261
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
262
    @needs_tree_write_lock
1986.1.3 by Robert Collins
Merge bzr.dev.
263
    def unversion(self, file_ids):
264
        """Remove the file ids in file_ids from the current versioned set.
265
266
        When a file_id is unversioned, all of its children are automatically
267
        unversioned.
268
269
        :param file_ids: The file ids to stop versioning.
270
        :raises: NoSuchId if any fileid is not currently versioned.
271
        """
272
        # XXX: This should be in mutabletree, but the inventory-save action
273
        # is not relevant to memory tree. Until that is done in unlock by
274
        # working tree, we cannot share the implementation.
275
        for file_id in file_ids:
276
            if self._inventory.has_id(file_id):
277
                self._inventory.remove_recursive_id(file_id)
278
            else:
279
                raise errors.NoSuchId(self, file_id)
280
2418.5.1 by John Arbash Meinel
Make a Branch helper which can create a very basic MemoryTree with history.
281
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
282
        """See MutableTree.set_parent_trees()."""
2598.5.2 by Aaron Bentley
Got all tests passing with Branch returning 'null:' for null revision
283
        for revision_id in revision_ids:
284
            _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.
285
        if len(revision_ids) == 0:
286
            self._parent_ids = []
4190.1.4 by Robert Collins
Cache ghosts when we can get them from a RemoteRepository in get_parent_map.
287
            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.
288
        else:
289
            self._parent_ids = revision_ids
290
            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.
291
        self._allow_leftmost_as_ghost = allow_leftmost_as_ghost
292
        self._set_basis()
293
    
294
    def _set_basis(self):
295
        try:
296
            self._basis_tree = self.branch.repository.revision_tree(
297
                self._branch_revision_id)
298
        except errors.NoSuchRevision:
299
            if self._allow_leftmost_as_ghost:
300
                self._basis_tree = self.branch.repository.revision_tree(
301
                    _mod_revision.NULL_REVISION)
302
            else:
303
                raise
2418.5.1 by John Arbash Meinel
Make a Branch helper which can create a very basic MemoryTree with history.
304
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
305
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
306
        """See MutableTree.set_parent_trees()."""
307
        if len(parents_list) == 0:
308
            self._parent_ids = []
3668.5.1 by Jelmer Vernooij
Use NULL_REVISION rather than None for Repository.revision_tree().
309
            self._basis_tree = self.branch.repository.revision_tree(
310
                                   _mod_revision.NULL_REVISION)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
311
        else:
312
            if parents_list[0][1] is None and not allow_leftmost_as_ghost:
313
                # a ghost in the left most parent
314
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
315
            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
316
            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().
317
                self._basis_tree = self.branch.repository.revision_tree(
318
                                       _mod_revision.NULL_REVISION)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
319
            else:
320
                self._basis_tree = parents_list[0][1]
1986.1.6 by Robert Collins
Add MemoryTree.last_revision.
321
            self._branch_revision_id = parents_list[0][0]