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