/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/memorytree.py

  • Committer: Jelmer Vernooij
  • Date: 2020-02-07 02:14:30 UTC
  • mto: This revision was merged to the branch mainline in revision 7492.
  • Revision ID: jelmer@jelmer.uk-20200207021430-m49iq3x4x8xlib6x
Drop python2 support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""MemoryTree object.
 
18
 
 
19
See MemoryTree for more details.
 
20
"""
 
21
 
 
22
from __future__ import absolute_import
 
23
 
 
24
import os
 
25
import stat
 
26
 
 
27
from . import (
 
28
    errors,
 
29
    lock,
 
30
    revision as _mod_revision,
 
31
    )
 
32
from .bzr.inventory import Inventory
 
33
from .bzr.inventorytree import MutableInventoryTree
 
34
from .osutils import sha_file
 
35
from .transport.memory import MemoryTransport
 
36
 
 
37
 
 
38
class MemoryTree(MutableInventoryTree):
 
39
    """A MemoryTree is a specialisation of MutableTree.
 
40
 
 
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
 
49
        self.controldir = branch.controldir
 
50
        self._branch_revision_id = revision_id
 
51
        self._locks = 0
 
52
        self._lock_mode = None
 
53
 
 
54
    def supports_symlinks(self):
 
55
        return True
 
56
 
 
57
    def supports_tree_reference(self):
 
58
        return False
 
59
 
 
60
    def get_config_stack(self):
 
61
        return self.branch.get_config_stack()
 
62
 
 
63
    def is_control_filename(self, filename):
 
64
        # Memory tree doesn't have any control filenames
 
65
        return False
 
66
 
 
67
    def _add(self, files, ids, kinds):
 
68
        """See MutableTree._add."""
 
69
        with self.lock_tree_write():
 
70
            for f, file_id, kind in zip(files, ids, kinds):
 
71
                if kind is None:
 
72
                    st_mode = self._file_transport.stat(f).st_mode
 
73
                    if stat.S_ISREG(st_mode):
 
74
                        kind = 'file'
 
75
                    elif stat.S_ISLNK(st_mode):
 
76
                        kind = 'symlink'
 
77
                    elif stat.S_ISDIR(st_mode):
 
78
                        kind = 'directory'
 
79
                    else:
 
80
                        raise AssertionError('Unknown file kind')
 
81
                if file_id is None:
 
82
                    self._inventory.add_path(f, kind=kind)
 
83
                else:
 
84
                    self._inventory.add_path(f, kind=kind, file_id=file_id)
 
85
 
 
86
    def basis_tree(self):
 
87
        """See Tree.basis_tree()."""
 
88
        return self._basis_tree
 
89
 
 
90
    @staticmethod
 
91
    def create_on_branch(branch):
 
92
        """Create a MemoryTree for branch, using the last-revision of branch."""
 
93
        revision_id = _mod_revision.ensure_null(branch.last_revision())
 
94
        return MemoryTree(branch, revision_id)
 
95
 
 
96
    def _gather_kinds(self, files, kinds):
 
97
        """See MutableTree._gather_kinds.
 
98
 
 
99
        This implementation does not care about the file kind of
 
100
        missing files, so is a no-op.
 
101
        """
 
102
 
 
103
    def get_file(self, path):
 
104
        """See Tree.get_file."""
 
105
        return self._file_transport.get(path)
 
106
 
 
107
    def get_file_sha1(self, path, stat_value=None):
 
108
        """See Tree.get_file_sha1()."""
 
109
        stream = self._file_transport.get(path)
 
110
        return sha_file(stream)
 
111
 
 
112
    def _comparison_data(self, entry, path):
 
113
        """See Tree._comparison_data."""
 
114
        if entry is None:
 
115
            return None, False, None
 
116
        return entry.kind, entry.executable, None
 
117
 
 
118
    def rename_one(self, from_rel, to_rel):
 
119
        with self.lock_tree_write():
 
120
            file_id = self.path2id(from_rel)
 
121
            to_dir, to_tail = os.path.split(to_rel)
 
122
            to_parent_id = self.path2id(to_dir)
 
123
            self._file_transport.move(from_rel, to_rel)
 
124
            self._inventory.rename(file_id, to_parent_id, to_tail)
 
125
 
 
126
    def path_content_summary(self, path):
 
127
        """See Tree.path_content_summary."""
 
128
        id = self.path2id(path)
 
129
        if id is None:
 
130
            return 'missing', None, None, None
 
131
        kind = self.kind(path, id)
 
132
        if kind == 'file':
 
133
            bytes = self._file_transport.get_bytes(path)
 
134
            size = len(bytes)
 
135
            executable = self._inventory[id].executable
 
136
            sha1 = None  # no stat cache
 
137
            return (kind, size, executable, sha1)
 
138
        elif kind == 'directory':
 
139
            # memory tree does not support nested trees yet.
 
140
            return kind, None, None, None
 
141
        elif kind == 'symlink':
 
142
            return kind, None, None, self._inventory[id].symlink_target
 
143
        else:
 
144
            raise NotImplementedError('unknown kind')
 
145
 
 
146
    def get_parent_ids(self):
 
147
        """See Tree.get_parent_ids.
 
148
 
 
149
        This implementation returns the current cached value from
 
150
            self._parent_ids.
 
151
        """
 
152
        with self.lock_read():
 
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, path):
 
160
        return self._inventory.get_entry_by_path(path).executable
 
161
 
 
162
    def kind(self, path):
 
163
        return self._inventory.get_entry_by_path(path).kind
 
164
 
 
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
 
 
173
    def last_revision(self):
 
174
        """See MutableTree.last_revision."""
 
175
        with self.lock_read():
 
176
            return self._branch_revision_id
 
177
 
 
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
            return lock.LogicalLockResult(self.unlock)
 
190
        except BaseException:
 
191
            self._locks -= 1
 
192
            raise
 
193
 
 
194
    def lock_tree_write(self):
 
195
        """See MutableTree.lock_tree_write()."""
 
196
        self._locks += 1
 
197
        try:
 
198
            if self._locks == 1:
 
199
                self.branch.lock_read()
 
200
                self._lock_mode = "w"
 
201
                self._populate_from_branch()
 
202
            elif self._lock_mode == "r":
 
203
                raise errors.ReadOnlyError(self)
 
204
        except BaseException:
 
205
            self._locks -= 1
 
206
            raise
 
207
        return lock.LogicalLockResult(self.unlock)
 
208
 
 
209
    def lock_write(self):
 
210
        """See MutableTree.lock_write()."""
 
211
        self._locks += 1
 
212
        try:
 
213
            if self._locks == 1:
 
214
                self.branch.lock_write()
 
215
                self._lock_mode = "w"
 
216
                self._populate_from_branch()
 
217
            elif self._lock_mode == "r":
 
218
                raise errors.ReadOnlyError(self)
 
219
            return lock.LogicalLockResult(self.unlock)
 
220
        except BaseException:
 
221
            self._locks -= 1
 
222
            raise
 
223
 
 
224
    def _populate_from_branch(self):
 
225
        """Populate the in-tree state from the branch."""
 
226
        self._set_basis()
 
227
        if self._branch_revision_id == _mod_revision.NULL_REVISION:
 
228
            self._parent_ids = []
 
229
        else:
 
230
            self._parent_ids = [self._branch_revision_id]
 
231
        self._inventory = Inventory(None, self._basis_tree.get_revision_id())
 
232
        self._file_transport = MemoryTransport()
 
233
        # TODO copy the revision trees content, or do it lazy, or something.
 
234
        inventory_entries = self._basis_tree.iter_entries_by_dir()
 
235
        for path, entry in inventory_entries:
 
236
            self._inventory.add(entry.copy())
 
237
            if path == '':
 
238
                continue
 
239
            if entry.kind == 'directory':
 
240
                self._file_transport.mkdir(path)
 
241
            elif entry.kind == 'symlink':
 
242
                self._file_transport.symlink(entry.symlink_target, path)
 
243
            elif entry.kind == 'file':
 
244
                self._file_transport.put_file(
 
245
                    path, self._basis_tree.get_file(path))
 
246
            else:
 
247
                raise NotImplementedError(self._populate_from_branch)
 
248
 
 
249
    def put_file_bytes_non_atomic(self, path, bytes):
 
250
        """See MutableTree.put_file_bytes_non_atomic."""
 
251
        self._file_transport.put_bytes(path, bytes)
 
252
 
 
253
    def unlock(self):
 
254
        """Release a lock.
 
255
 
 
256
        This frees all cached state when the last lock context for the tree is
 
257
        left.
 
258
        """
 
259
        if self._locks == 1:
 
260
            self._basis_tree = None
 
261
            self._parent_ids = []
 
262
            self._inventory = None
 
263
            try:
 
264
                self.branch.unlock()
 
265
            finally:
 
266
                self._locks = 0
 
267
                self._lock_mode = None
 
268
        else:
 
269
            self._locks -= 1
 
270
 
 
271
    def unversion(self, paths):
 
272
        """Remove the paths from the current versioned set.
 
273
 
 
274
        When a file_id is unversioned, all of its children are automatically
 
275
        unversioned.
 
276
 
 
277
        :param paths: The paths to stop versioning.
 
278
        :raises: NoSuchId if any fileid is not currently versioned.
 
279
        """
 
280
        with self.lock_tree_write():
 
281
            # XXX: This should be in mutabletree, but the inventory-save action
 
282
            # is not relevant to memory tree. Until that is done in unlock by
 
283
            # working tree, we cannot share the implementation.
 
284
            file_ids = set()
 
285
            for path in paths:
 
286
                file_id = self.path2id(path)
 
287
                if file_id is None:
 
288
                    raise errors.NoSuchFile(path)
 
289
                file_ids.add(file_id)
 
290
            for file_id in file_ids:
 
291
                if self._inventory.has_id(file_id):
 
292
                    self._inventory.remove_recursive_id(file_id)
 
293
 
 
294
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
 
295
        """See MutableTree.set_parent_trees()."""
 
296
        for revision_id in revision_ids:
 
297
            _mod_revision.check_not_reserved_id(revision_id)
 
298
        if len(revision_ids) == 0:
 
299
            self._parent_ids = []
 
300
            self._branch_revision_id = _mod_revision.NULL_REVISION
 
301
        else:
 
302
            self._parent_ids = revision_ids
 
303
            self._branch_revision_id = revision_ids[0]
 
304
        self._allow_leftmost_as_ghost = allow_leftmost_as_ghost
 
305
        self._set_basis()
 
306
 
 
307
    def _set_basis(self):
 
308
        try:
 
309
            self._basis_tree = self.branch.repository.revision_tree(
 
310
                self._branch_revision_id)
 
311
        except errors.NoSuchRevision:
 
312
            if self._allow_leftmost_as_ghost:
 
313
                self._basis_tree = self.branch.repository.revision_tree(
 
314
                    _mod_revision.NULL_REVISION)
 
315
            else:
 
316
                raise
 
317
 
 
318
    def get_symlink_target(self, path):
 
319
        with self.lock_read():
 
320
            return self._file_transport.readlink(path)
 
321
 
 
322
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
 
323
        """See MutableTree.set_parent_trees()."""
 
324
        if len(parents_list) == 0:
 
325
            self._parent_ids = []
 
326
            self._basis_tree = self.branch.repository.revision_tree(
 
327
                _mod_revision.NULL_REVISION)
 
328
        else:
 
329
            if parents_list[0][1] is None and not allow_leftmost_as_ghost:
 
330
                # a ghost in the left most parent
 
331
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
 
332
            self._parent_ids = [parent_id for parent_id, tree in parents_list]
 
333
            if parents_list[0][1] is None or parents_list[0][1] == b'null:':
 
334
                self._basis_tree = self.branch.repository.revision_tree(
 
335
                    _mod_revision.NULL_REVISION)
 
336
            else:
 
337
                self._basis_tree = parents_list[0][1]
 
338
            self._branch_revision_id = parents_list[0][0]