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