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