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