/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
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
22
3567.5.1 by John Arbash Meinel
Implement rename_one on MemoryTree, and expose that in the Branch Builder
23
import os
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
24
2598.5.1 by Aaron Bentley
Start eliminating the use of None to indicate null revision
25
from bzrlib import (
26
    errors,
27
    mutabletree,
28
    revision as _mod_revision,
5777.3.1 by Jelmer Vernooij
Split InventoryTree out of Tree.
29
    revisiontree,
30
    tree,
2598.5.1 by Aaron Bentley
Start eliminating the use of None to indicate null revision
31
    )
5121.2.4 by Jelmer Vernooij
Remove more unused imports.
32
from bzrlib.decorators import needs_read_lock
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.3.4 by Jelmer Vernooij
Add note to release notes, fix PEP8 issue.
38
class MemoryTree(mutabletree.MutableTree, tree.InventoryTree):
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]
4244.2.1 by Vincent Ladeuil
inv._get_mutable_inventory() - prepare for CHKInventory
224
        self._inventory = self._basis_tree._inventory._get_mutable_inventory()
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.
227
        inventory_entries = self._inventory.iter_entries()
228
        for path, entry in inventory_entries:
1731.1.50 by Aaron Bentley
Merge bzr.dev
229
            if path == '':
230
                continue
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
231
            if entry.kind == 'directory':
232
                self._file_transport.mkdir(path)
233
            elif entry.kind == 'file':
1986.1.4 by Robert Collins
Fixup deprecations from bzr.dev.
234
                self._file_transport.put_file(path,
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
235
                    self._basis_tree.get_file(entry.file_id))
236
            else:
237
                raise NotImplementedError(self._populate_from_branch)
238
239
    def put_file_bytes_non_atomic(self, file_id, bytes):
240
        """See MutableTree.put_file_bytes_non_atomic."""
1986.1.4 by Robert Collins
Fixup deprecations from bzr.dev.
241
        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
242
243
    def unlock(self):
244
        """Release a lock.
245
246
        This frees all cached state when the last lock context for the tree is
247
        left.
248
        """
249
        if self._locks == 1:
250
            self._basis_tree = None
251
            self._parent_ids = []
252
            self._inventory = None
253
            try:
254
                self.branch.unlock()
255
            finally:
256
                self._locks = 0
257
                self._lock_mode = None
258
        else:
259
            self._locks -= 1
260
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
261
    @needs_tree_write_lock
1986.1.3 by Robert Collins
Merge bzr.dev.
262
    def unversion(self, file_ids):
263
        """Remove the file ids in file_ids from the current versioned set.
264
265
        When a file_id is unversioned, all of its children are automatically
266
        unversioned.
267
268
        :param file_ids: The file ids to stop versioning.
269
        :raises: NoSuchId if any fileid is not currently versioned.
270
        """
271
        # XXX: This should be in mutabletree, but the inventory-save action
272
        # is not relevant to memory tree. Until that is done in unlock by
273
        # working tree, we cannot share the implementation.
274
        for file_id in file_ids:
275
            if self._inventory.has_id(file_id):
276
                self._inventory.remove_recursive_id(file_id)
277
            else:
278
                raise errors.NoSuchId(self, file_id)
279
2418.5.1 by John Arbash Meinel
Make a Branch helper which can create a very basic MemoryTree with history.
280
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
281
        """See MutableTree.set_parent_trees()."""
2598.5.2 by Aaron Bentley
Got all tests passing with Branch returning 'null:' for null revision
282
        for revision_id in revision_ids:
283
            _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.
284
        if len(revision_ids) == 0:
285
            self._parent_ids = []
4190.1.4 by Robert Collins
Cache ghosts when we can get them from a RemoteRepository in get_parent_map.
286
            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.
287
        else:
288
            self._parent_ids = revision_ids
289
            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.
290
        self._allow_leftmost_as_ghost = allow_leftmost_as_ghost
291
        self._set_basis()
292
    
293
    def _set_basis(self):
294
        try:
295
            self._basis_tree = self.branch.repository.revision_tree(
296
                self._branch_revision_id)
297
        except errors.NoSuchRevision:
298
            if self._allow_leftmost_as_ghost:
299
                self._basis_tree = self.branch.repository.revision_tree(
300
                    _mod_revision.NULL_REVISION)
301
            else:
302
                raise
2418.5.1 by John Arbash Meinel
Make a Branch helper which can create a very basic MemoryTree with history.
303
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
304
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
305
        """See MutableTree.set_parent_trees()."""
306
        if len(parents_list) == 0:
307
            self._parent_ids = []
3668.5.1 by Jelmer Vernooij
Use NULL_REVISION rather than None for Repository.revision_tree().
308
            self._basis_tree = self.branch.repository.revision_tree(
309
                                   _mod_revision.NULL_REVISION)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
310
        else:
311
            if parents_list[0][1] is None and not allow_leftmost_as_ghost:
312
                # a ghost in the left most parent
313
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
314
            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
315
            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().
316
                self._basis_tree = self.branch.repository.revision_tree(
317
                                       _mod_revision.NULL_REVISION)
1986.1.2 by Robert Collins
Various changes to allow non-workingtree specific tests to run entirely
318
            else:
319
                self._basis_tree = parents_list[0][1]
1986.1.6 by Robert Collins
Add MemoryTree.last_revision.
320
            self._branch_revision_id = parents_list[0][0]