/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""MemoryTree object.
18
19
See MemoryTree for more details.
20
"""
21
22
23
from copy import deepcopy
24
25
from bzrlib import errors, mutabletree
26
from bzrlib.decorators import needs_read_lock, needs_write_lock
27
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.
28
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
29
from bzrlib.transport.memory import MemoryTransport
30
31
32
class MemoryTree(mutabletree.MutableTree):
33
    """A MemoryTree is a specialisation of MutableTree.
34
    
35
    It maintains nearly no state outside of read_lock and write_lock
36
    transactions. (it keeps a reference to the branch, and its last-revision
37
    only).
38
    """
39
40
    def __init__(self, branch, revision_id):
41
        """Construct a MemoryTree for branch using revision_id."""
42
        self.branch = branch
43
        self.bzrdir = branch.bzrdir
44
        self._branch_revision_id = revision_id
45
        self._locks = 0
46
        self._lock_mode = None
47
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
48
    @needs_tree_write_lock
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
49
    def _add(self, files, ids, kinds):
50
        """See MutableTree._add."""
51
        for f, file_id, kind in zip(files, ids, kinds):
52
            if kind is None:
53
                kind = 'file'
54
            if file_id is None:
55
                self._inventory.add_path(f, kind=kind)
56
            else:
57
                self._inventory.add_path(f, kind=kind, file_id=file_id)
58
59
    def basis_tree(self):
60
        """See Tree.basis_tree()."""
61
        return self._basis_tree
62
63
    @staticmethod
64
    def create_on_branch(branch):
65
        """Create a MemoryTree for branch, using the last-revision of branch."""
66
        return MemoryTree(branch, branch.last_revision())
67
68
    def _gather_kinds(self, files, kinds):
69
        """See MutableTree._gather_kinds.
70
        
71
        This implementation does not care about the file kind of
72
        missing files, so is a no-op.
73
        """
74
75
    def get_file(self, file_id):
76
        """See Tree.get_file."""
77
        return self._file_transport.get(self.id2path(file_id))
78
2564.2.1 by Ian Clatworthy
refactor commit to support alternative population meothds
79
    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
80
        """See Tree.get_file_sha1()."""
81
        if path is None:
82
            path = self.id2path(file_id)
83
        stream = self._file_transport.get(path)
84
        return sha_file(stream)
85
2564.2.1 by Ian Clatworthy
refactor commit to support alternative population meothds
86
    def _comparison_data(self, entry, path):
87
        """See Tree._comparison_data."""
88
        if entry is None:
89
            return None, False, None
90
        return entry.kind, entry.executable, None
91
92
    def _file_size(self, entry, stat_value):
93
        """See Tree._file_size."""
94
        if entry is None:
95
            return 0
96
        return entry.text_size
97
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
98
    @needs_read_lock
99
    def get_parent_ids(self):
100
        """See Tree.get_parent_ids.
101
102
        This implementation returns the current cached value from
103
            self._parent_ids.
104
        """
105
        return list(self._parent_ids)
106
107
    def has_filename(self, filename):
108
        """See Tree.has_filename()."""
109
        return self._file_transport.has(filename)
110
111
    def is_executable(self, file_id, path=None):
112
        return self._inventory[file_id].executable
113
1959.4.2 by Aaron Bentley
Merge bzr.dev
114
    def kind(self, file_id):
115
        return self._inventory[file_id].kind
116
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
117
    def mkdir(self, path, file_id=None):
118
        """See MutableTree.mkdir()."""
119
        self.add(path, file_id, 'directory')
120
        if file_id is None:
121
            file_id = self.path2id(path)
122
        self._file_transport.mkdir(path)
123
        return file_id
124
1986.1.6 by Robert Collins
Add MemoryTree.last_revision.
125
    @needs_read_lock
126
    def last_revision(self):
127
        """See MutableTree.last_revision."""
128
        return self._branch_revision_id
129
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
130
    def lock_read(self):
131
        """Lock the memory tree for reading.
132
133
        This triggers population of data from the branch for its revision.
134
        """
135
        self._locks += 1
136
        try:
137
            if self._locks == 1:
138
                self.branch.lock_read()
139
                self._lock_mode = "r"
140
                self._populate_from_branch()
141
        except:
142
            self._locks -= 1
143
            raise
144
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
145
    def lock_tree_write(self):
146
        """See MutableTree.lock_tree_write()."""
147
        self._locks += 1
148
        try:
149
            if self._locks == 1:
150
                self.branch.lock_read()
151
                self._lock_mode = "w"
152
                self._populate_from_branch()
153
            elif self._lock_mode == "r":
154
                raise errors.ReadOnlyError(self)
155
        except:
156
            self._locks -= 1
157
            raise
158
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
159
    def lock_write(self):
160
        """See MutableTree.lock_write()."""
161
        self._locks += 1
162
        try:
163
            if self._locks == 1:
164
                self.branch.lock_write()
165
                self._lock_mode = "w"
166
                self._populate_from_branch()
167
            elif self._lock_mode == "r":
168
                raise errors.ReadOnlyError(self)
169
        except:
170
            self._locks -= 1
171
            raise
172
173
    def _populate_from_branch(self):
174
        """Populate the in-tree state from the branch."""
175
        self._basis_tree = self.branch.repository.revision_tree(
176
            self._branch_revision_id)
177
        if self._branch_revision_id is None:
178
            self._parent_ids = []
179
        else:
180
            self._parent_ids = [self._branch_revision_id]
181
        self._inventory = deepcopy(self._basis_tree._inventory)
182
        self._file_transport = MemoryTransport()
183
        # TODO copy the revision trees content, or do it lazy, or something.
184
        inventory_entries = self._inventory.iter_entries()
185
        for path, entry in inventory_entries:
1731.1.50 by Aaron Bentley
Merge bzr.dev
186
            if path == '':
187
                continue
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
188
            if entry.kind == 'directory':
189
                self._file_transport.mkdir(path)
190
            elif entry.kind == 'file':
1986.1.4 by Robert Collins
Fixup deprecations from bzr.dev.
191
                self._file_transport.put_file(path,
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
192
                    self._basis_tree.get_file(entry.file_id))
193
            else:
194
                raise NotImplementedError(self._populate_from_branch)
195
196
    def put_file_bytes_non_atomic(self, file_id, bytes):
197
        """See MutableTree.put_file_bytes_non_atomic."""
1986.1.4 by Robert Collins
Fixup deprecations from bzr.dev.
198
        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
199
200
    def unlock(self):
201
        """Release a lock.
202
203
        This frees all cached state when the last lock context for the tree is
204
        left.
205
        """
206
        if self._locks == 1:
207
            self._basis_tree = None
208
            self._parent_ids = []
209
            self._inventory = None
210
            try:
211
                self.branch.unlock()
212
            finally:
213
                self._locks = 0
214
                self._lock_mode = None
215
        else:
216
            self._locks -= 1
217
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
218
    @needs_tree_write_lock
1986.1.3 by Robert Collins
Merge bzr.dev.
219
    def unversion(self, file_ids):
220
        """Remove the file ids in file_ids from the current versioned set.
221
222
        When a file_id is unversioned, all of its children are automatically
223
        unversioned.
224
225
        :param file_ids: The file ids to stop versioning.
226
        :raises: NoSuchId if any fileid is not currently versioned.
227
        """
228
        # XXX: This should be in mutabletree, but the inventory-save action
229
        # is not relevant to memory tree. Until that is done in unlock by
230
        # working tree, we cannot share the implementation.
231
        for file_id in file_ids:
232
            if self._inventory.has_id(file_id):
233
                self._inventory.remove_recursive_id(file_id)
234
            else:
235
                raise errors.NoSuchId(self, file_id)
236
2418.5.1 by John Arbash Meinel
Make a Branch helper which can create a very basic MemoryTree with history.
237
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
238
        """See MutableTree.set_parent_trees()."""
239
        if len(revision_ids) == 0:
240
            self._parent_ids = []
241
            self._basis_tree = self.branch.repository.revision_tree(None)
242
        else:
243
            self._parent_ids = revision_ids
244
            self._basis_tree = self.branch.repository.revision_tree(
245
                                    revision_ids[0])
246
            self._branch_revision_id = revision_ids[0]
247
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
248
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
249
        """See MutableTree.set_parent_trees()."""
250
        if len(parents_list) == 0:
251
            self._parent_ids = []
2418.5.1 by John Arbash Meinel
Make a Branch helper which can create a very basic MemoryTree with history.
252
            self._basis_tree = self.branch.repository.revision_tree(None)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
253
        else:
254
            if parents_list[0][1] is None and not allow_leftmost_as_ghost:
255
                # a ghost in the left most parent
256
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
257
            self._parent_ids = [parent_id for parent_id, tree in parents_list]
258
            if parents_list[0][1] is None:
2418.5.1 by John Arbash Meinel
Make a Branch helper which can create a very basic MemoryTree with history.
259
                self._basis_tree = self.branch.repository.revision_tree(None)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
260
            else:
261
                self._basis_tree = parents_list[0][1]
1986.1.6 by Robert Collins
Add MemoryTree.last_revision.
262
            self._branch_revision_id = parents_list[0][0]