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