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