/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: 2018-02-18 21:42:57 UTC
  • mto: This revision was merged to the branch mainline in revision 6859.
  • Revision ID: jelmer@jelmer.uk-20180218214257-jpevutp1wa30tz3v
Update TODO to reference Breezy, not Bazaar.

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
 
 
26
from . import (
 
27
    errors,
 
28
    lock,
 
29
    revision as _mod_revision,
 
30
    )
 
31
from .bzr.inventory import Inventory
 
32
from .bzr.inventorytree import MutableInventoryTree
 
33
from .osutils import sha_file
 
34
from .transport.memory import MemoryTransport
 
35
 
 
36
 
 
37
class MemoryTree(MutableInventoryTree):
 
38
    """A MemoryTree is a specialisation of MutableTree.
 
39
 
 
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
 
48
        self.controldir = branch.controldir
 
49
        self._branch_revision_id = revision_id
 
50
        self._locks = 0
 
51
        self._lock_mode = None
 
52
 
 
53
    def get_config_stack(self):
 
54
        return self.branch.get_config_stack()
 
55
 
 
56
    def is_control_filename(self, filename):
 
57
        # Memory tree doesn't have any control filenames
 
58
        return False
 
59
 
 
60
    def _add(self, files, ids, kinds):
 
61
        """See MutableTree._add."""
 
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)
 
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."""
 
78
        revision_id = _mod_revision.ensure_null(branch.last_revision())
 
79
        return MemoryTree(branch, revision_id)
 
80
 
 
81
    def _gather_kinds(self, files, kinds):
 
82
        """See MutableTree._gather_kinds.
 
83
 
 
84
        This implementation does not care about the file kind of
 
85
        missing files, so is a no-op.
 
86
        """
 
87
 
 
88
    def get_file(self, path, file_id=None):
 
89
        """See Tree.get_file."""
 
90
        return self._file_transport.get(path)
 
91
 
 
92
    def get_file_sha1(self, path, file_id=None, stat_value=None):
 
93
        """See Tree.get_file_sha1()."""
 
94
        stream = self._file_transport.get(path)
 
95
        return sha_file(stream)
 
96
 
 
97
    def get_root_id(self):
 
98
        return self.path2id('')
 
99
 
 
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
 
 
106
    def rename_one(self, from_rel, to_rel):
 
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)
 
113
 
 
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
 
119
        kind = self.kind(path, id)
 
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
 
 
134
    def _file_size(self, entry, stat_value):
 
135
        """See Tree._file_size."""
 
136
        if entry is None:
 
137
            return 0
 
138
        return entry.text_size
 
139
 
 
140
    def get_parent_ids(self):
 
141
        """See Tree.get_parent_ids.
 
142
 
 
143
        This implementation returns the current cached value from
 
144
            self._parent_ids.
 
145
        """
 
146
        with self.lock_read():
 
147
            return list(self._parent_ids)
 
148
 
 
149
    def has_filename(self, filename):
 
150
        """See Tree.has_filename()."""
 
151
        return self._file_transport.has(filename)
 
152
 
 
153
    def is_executable(self, path, file_id=None):
 
154
        return self._inventory[file_id].executable
 
155
 
 
156
    def kind(self, path, file_id=None):
 
157
        if file_id is None:
 
158
            file_id = self.path2id(path)
 
159
        return self._inventory[file_id].kind
 
160
 
 
161
    def mkdir(self, path, file_id=None):
 
162
        """See MutableTree.mkdir()."""
 
163
        self.add(path, file_id, 'directory')
 
164
        if file_id is None:
 
165
            file_id = self.path2id(path)
 
166
        self._file_transport.mkdir(path)
 
167
        return file_id
 
168
 
 
169
    def last_revision(self):
 
170
        """See MutableTree.last_revision."""
 
171
        with self.lock_read():
 
172
            return self._branch_revision_id
 
173
 
 
174
    def lock_read(self):
 
175
        """Lock the memory tree for reading.
 
176
 
 
177
        This triggers population of data from the branch for its revision.
 
178
        """
 
179
        self._locks += 1
 
180
        try:
 
181
            if self._locks == 1:
 
182
                self.branch.lock_read()
 
183
                self._lock_mode = "r"
 
184
                self._populate_from_branch()
 
185
            return lock.LogicalLockResult(self.unlock)
 
186
        except:
 
187
            self._locks -= 1
 
188
            raise
 
189
 
 
190
    def lock_tree_write(self):
 
191
        """See MutableTree.lock_tree_write()."""
 
192
        self._locks += 1
 
193
        try:
 
194
            if self._locks == 1:
 
195
                self.branch.lock_read()
 
196
                self._lock_mode = "w"
 
197
                self._populate_from_branch()
 
198
            elif self._lock_mode == "r":
 
199
                raise errors.ReadOnlyError(self)
 
200
        except:
 
201
            self._locks -= 1
 
202
            raise
 
203
        return lock.LogicalLockResult(self.unlock)
 
204
 
 
205
    def lock_write(self):
 
206
        """See MutableTree.lock_write()."""
 
207
        self._locks += 1
 
208
        try:
 
209
            if self._locks == 1:
 
210
                self.branch.lock_write()
 
211
                self._lock_mode = "w"
 
212
                self._populate_from_branch()
 
213
            elif self._lock_mode == "r":
 
214
                raise errors.ReadOnlyError(self)
 
215
            return lock.LogicalLockResult(self.unlock)
 
216
        except:
 
217
            self._locks -= 1
 
218
            raise
 
219
 
 
220
    def _populate_from_branch(self):
 
221
        """Populate the in-tree state from the branch."""
 
222
        self._set_basis()
 
223
        if self._branch_revision_id == _mod_revision.NULL_REVISION:
 
224
            self._parent_ids = []
 
225
        else:
 
226
            self._parent_ids = [self._branch_revision_id]
 
227
        self._inventory = Inventory(None, self._basis_tree.get_revision_id())
 
228
        self._file_transport = MemoryTransport()
 
229
        # TODO copy the revision trees content, or do it lazy, or something.
 
230
        inventory_entries = self._basis_tree.iter_entries_by_dir()
 
231
        for path, entry in inventory_entries:
 
232
            self._inventory.add(entry.copy())
 
233
            if path == '':
 
234
                continue
 
235
            if entry.kind == 'directory':
 
236
                self._file_transport.mkdir(path)
 
237
            elif entry.kind == 'file':
 
238
                self._file_transport.put_file(path,
 
239
                    self._basis_tree.get_file(path, entry.file_id))
 
240
            else:
 
241
                raise NotImplementedError(self._populate_from_branch)
 
242
 
 
243
    def put_file_bytes_non_atomic(self, path, bytes, file_id=None):
 
244
        """See MutableTree.put_file_bytes_non_atomic."""
 
245
        self._file_transport.put_bytes(path, bytes)
 
246
 
 
247
    def unlock(self):
 
248
        """Release a lock.
 
249
 
 
250
        This frees all cached state when the last lock context for the tree is
 
251
        left.
 
252
        """
 
253
        if self._locks == 1:
 
254
            self._basis_tree = None
 
255
            self._parent_ids = []
 
256
            self._inventory = None
 
257
            try:
 
258
                self.branch.unlock()
 
259
            finally:
 
260
                self._locks = 0
 
261
                self._lock_mode = None
 
262
        else:
 
263
            self._locks -= 1
 
264
 
 
265
    def unversion(self, paths, file_ids=None):
 
266
        """Remove the paths from the current versioned set.
 
267
 
 
268
        When a file_id is unversioned, all of its children are automatically
 
269
        unversioned.
 
270
 
 
271
        :param paths: The paths to stop versioning.
 
272
        :raises: NoSuchId if any fileid is not currently versioned.
 
273
        """
 
274
        with self.lock_tree_write():
 
275
            # XXX: This should be in mutabletree, but the inventory-save action
 
276
            # is not relevant to memory tree. Until that is done in unlock by
 
277
            # working tree, we cannot share the implementation.
 
278
            if file_ids is None:
 
279
                file_ids = {self.path2id(path) for path in paths}
 
280
            for file_id in file_ids:
 
281
                if self._inventory.has_id(file_id):
 
282
                    self._inventory.remove_recursive_id(file_id)
 
283
                else:
 
284
                    raise errors.NoSuchId(self, file_id)
 
285
 
 
286
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
 
287
        """See MutableTree.set_parent_trees()."""
 
288
        for revision_id in revision_ids:
 
289
            _mod_revision.check_not_reserved_id(revision_id)
 
290
        if len(revision_ids) == 0:
 
291
            self._parent_ids = []
 
292
            self._branch_revision_id = _mod_revision.NULL_REVISION
 
293
        else:
 
294
            self._parent_ids = revision_ids
 
295
            self._branch_revision_id = revision_ids[0]
 
296
        self._allow_leftmost_as_ghost = allow_leftmost_as_ghost
 
297
        self._set_basis()
 
298
 
 
299
    def _set_basis(self):
 
300
        try:
 
301
            self._basis_tree = self.branch.repository.revision_tree(
 
302
                self._branch_revision_id)
 
303
        except errors.NoSuchRevision:
 
304
            if self._allow_leftmost_as_ghost:
 
305
                self._basis_tree = self.branch.repository.revision_tree(
 
306
                    _mod_revision.NULL_REVISION)
 
307
            else:
 
308
                raise
 
309
 
 
310
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
 
311
        """See MutableTree.set_parent_trees()."""
 
312
        if len(parents_list) == 0:
 
313
            self._parent_ids = []
 
314
            self._basis_tree = self.branch.repository.revision_tree(
 
315
                                   _mod_revision.NULL_REVISION)
 
316
        else:
 
317
            if parents_list[0][1] is None and not allow_leftmost_as_ghost:
 
318
                # a ghost in the left most parent
 
319
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
 
320
            self._parent_ids = [parent_id for parent_id, tree in parents_list]
 
321
            if parents_list[0][1] is None or parents_list[0][1] == 'null:':
 
322
                self._basis_tree = self.branch.repository.revision_tree(
 
323
                                       _mod_revision.NULL_REVISION)
 
324
            else:
 
325
                self._basis_tree = parents_list[0][1]
 
326
            self._branch_revision_id = parents_list[0][0]