/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: 2017-08-07 11:49:46 UTC
  • mto: (6747.3.4 avoid-set-revid-3)
  • mto: This revision was merged to the branch mainline in revision 6750.
  • Revision ID: jelmer@jelmer.uk-20170807114946-luclmxuawyzhpiot
Avoid setting revision_ids.

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