/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 bzrlib/memorytree.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
See MemoryTree for more details.
20
20
"""
21
21
 
 
22
from __future__ import absolute_import
 
23
 
22
24
import os
23
 
import stat
24
25
 
25
 
from . import (
 
26
from bzrlib import (
26
27
    errors,
27
 
    lock,
 
28
    mutabletree,
28
29
    revision as _mod_revision,
29
30
    )
30
 
from .bzr.inventory import Inventory
31
 
from .bzr.inventorytree import MutableInventoryTree
32
 
from .osutils import sha_file
33
 
from .transport.memory import MemoryTransport
34
 
 
35
 
 
36
 
class MemoryTree(MutableInventoryTree):
 
31
from bzrlib.decorators import needs_read_lock
 
32
from bzrlib.inventory import Inventory
 
33
from bzrlib.osutils import sha_file
 
34
from bzrlib.mutabletree import needs_tree_write_lock
 
35
from bzrlib.transport.memory import MemoryTransport
 
36
 
 
37
 
 
38
class MemoryTree(mutabletree.MutableInventoryTree):
37
39
    """A MemoryTree is a specialisation of MutableTree.
38
40
 
39
41
    It maintains nearly no state outside of read_lock and write_lock
44
46
    def __init__(self, branch, revision_id):
45
47
        """Construct a MemoryTree for branch using revision_id."""
46
48
        self.branch = branch
47
 
        self.controldir = branch.controldir
 
49
        self.bzrdir = branch.bzrdir
48
50
        self._branch_revision_id = revision_id
49
51
        self._locks = 0
50
52
        self._lock_mode = None
51
53
 
52
 
    def supports_symlinks(self):
53
 
        return True
54
 
 
55
 
    def supports_tree_reference(self):
56
 
        return False
57
 
 
58
54
    def get_config_stack(self):
59
55
        return self.branch.get_config_stack()
60
56
 
62
58
        # Memory tree doesn't have any control filenames
63
59
        return False
64
60
 
 
61
    @needs_tree_write_lock
65
62
    def _add(self, files, ids, kinds):
66
63
        """See MutableTree._add."""
67
 
        with self.lock_tree_write():
68
 
            for f, file_id, kind in zip(files, ids, kinds):
69
 
                if kind is None:
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')
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)
 
64
        for f, file_id, kind in zip(files, ids, kinds):
 
65
            if kind is None:
 
66
                kind = 'file'
 
67
            if file_id is None:
 
68
                self._inventory.add_path(f, kind=kind)
 
69
            else:
 
70
                self._inventory.add_path(f, kind=kind, file_id=file_id)
83
71
 
84
72
    def basis_tree(self):
85
73
        """See Tree.basis_tree()."""
98
86
        missing files, so is a no-op.
99
87
        """
100
88
 
101
 
    def get_file(self, path):
 
89
    def get_file(self, file_id, path=None):
102
90
        """See Tree.get_file."""
 
91
        if path is None:
 
92
            path = self.id2path(file_id)
103
93
        return self._file_transport.get(path)
104
94
 
105
 
    def get_file_sha1(self, path, stat_value=None):
 
95
    def get_file_sha1(self, file_id, path=None, stat_value=None):
106
96
        """See Tree.get_file_sha1()."""
 
97
        if path is None:
 
98
            path = self.id2path(file_id)
107
99
        stream = self._file_transport.get(path)
108
100
        return sha_file(stream)
109
101
 
 
102
    def get_root_id(self):
 
103
        return self.path2id('')
 
104
 
110
105
    def _comparison_data(self, entry, path):
111
106
        """See Tree._comparison_data."""
112
107
        if entry is None:
113
108
            return None, False, None
114
109
        return entry.kind, entry.executable, None
115
110
 
 
111
    @needs_tree_write_lock
116
112
    def rename_one(self, from_rel, to_rel):
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)
 
113
        file_id = self.path2id(from_rel)
 
114
        to_dir, to_tail = os.path.split(to_rel)
 
115
        to_parent_id = self.path2id(to_dir)
 
116
        self._file_transport.move(from_rel, to_rel)
 
117
        self._inventory.rename(file_id, to_parent_id, to_tail)
123
118
 
124
119
    def path_content_summary(self, path):
125
120
        """See Tree.path_content_summary."""
126
121
        id = self.path2id(path)
127
122
        if id is None:
128
123
            return 'missing', None, None, None
129
 
        kind = self.kind(path, id)
 
124
        kind = self.kind(id)
130
125
        if kind == 'file':
131
126
            bytes = self._file_transport.get_bytes(path)
132
127
            size = len(bytes)
133
128
            executable = self._inventory[id].executable
134
 
            sha1 = None  # no stat cache
 
129
            sha1 = None # no stat cache
135
130
            return (kind, size, executable, sha1)
136
131
        elif kind == 'directory':
137
132
            # memory tree does not support nested trees yet.
138
133
            return kind, None, None, None
139
134
        elif kind == 'symlink':
140
 
            return kind, None, None, self._inventory[id].symlink_target
 
135
            raise NotImplementedError('symlink support')
141
136
        else:
142
137
            raise NotImplementedError('unknown kind')
143
138
 
 
139
    def _file_size(self, entry, stat_value):
 
140
        """See Tree._file_size."""
 
141
        if entry is None:
 
142
            return 0
 
143
        return entry.text_size
 
144
 
 
145
    @needs_read_lock
144
146
    def get_parent_ids(self):
145
147
        """See Tree.get_parent_ids.
146
148
 
147
149
        This implementation returns the current cached value from
148
150
            self._parent_ids.
149
151
        """
150
 
        with self.lock_read():
151
 
            return list(self._parent_ids)
 
152
        return list(self._parent_ids)
152
153
 
153
154
    def has_filename(self, filename):
154
155
        """See Tree.has_filename()."""
155
156
        return self._file_transport.has(filename)
156
157
 
157
 
    def is_executable(self, path):
158
 
        return self._inventory.get_entry_by_path(path).executable
 
158
    def is_executable(self, file_id, path=None):
 
159
        return self._inventory[file_id].executable
159
160
 
160
 
    def kind(self, path):
161
 
        return self._inventory.get_entry_by_path(path).kind
 
161
    def kind(self, file_id):
 
162
        return self._inventory[file_id].kind
162
163
 
163
164
    def mkdir(self, path, file_id=None):
164
165
        """See MutableTree.mkdir()."""
168
169
        self._file_transport.mkdir(path)
169
170
        return file_id
170
171
 
 
172
    @needs_read_lock
171
173
    def last_revision(self):
172
174
        """See MutableTree.last_revision."""
173
 
        with self.lock_read():
174
 
            return self._branch_revision_id
 
175
        return self._branch_revision_id
175
176
 
176
177
    def lock_read(self):
177
178
        """Lock the memory tree for reading.
184
185
                self.branch.lock_read()
185
186
                self._lock_mode = "r"
186
187
                self._populate_from_branch()
187
 
            return lock.LogicalLockResult(self.unlock)
188
 
        except BaseException:
 
188
        except:
189
189
            self._locks -= 1
190
190
            raise
191
191
 
199
199
                self._populate_from_branch()
200
200
            elif self._lock_mode == "r":
201
201
                raise errors.ReadOnlyError(self)
202
 
        except BaseException:
 
202
        except:
203
203
            self._locks -= 1
204
204
            raise
205
 
        return lock.LogicalLockResult(self.unlock)
206
205
 
207
206
    def lock_write(self):
208
207
        """See MutableTree.lock_write()."""
214
213
                self._populate_from_branch()
215
214
            elif self._lock_mode == "r":
216
215
                raise errors.ReadOnlyError(self)
217
 
            return lock.LogicalLockResult(self.unlock)
218
 
        except BaseException:
 
216
        except:
219
217
            self._locks -= 1
220
218
            raise
221
219
 
236
234
                continue
237
235
            if entry.kind == 'directory':
238
236
                self._file_transport.mkdir(path)
239
 
            elif entry.kind == 'symlink':
240
 
                self._file_transport.symlink(entry.symlink_target, path)
241
237
            elif entry.kind == 'file':
242
 
                self._file_transport.put_file(
243
 
                    path, self._basis_tree.get_file(path))
 
238
                self._file_transport.put_file(path,
 
239
                    self._basis_tree.get_file(entry.file_id))
244
240
            else:
245
241
                raise NotImplementedError(self._populate_from_branch)
246
242
 
247
 
    def put_file_bytes_non_atomic(self, path, bytes):
 
243
    def put_file_bytes_non_atomic(self, file_id, bytes):
248
244
        """See MutableTree.put_file_bytes_non_atomic."""
249
 
        self._file_transport.put_bytes(path, bytes)
 
245
        self._file_transport.put_bytes(self.id2path(file_id), bytes)
250
246
 
251
247
    def unlock(self):
252
248
        """Release a lock.
266
262
        else:
267
263
            self._locks -= 1
268
264
 
269
 
    def unversion(self, paths):
270
 
        """Remove the paths from the current versioned set.
 
265
    @needs_tree_write_lock
 
266
    def unversion(self, file_ids):
 
267
        """Remove the file ids in file_ids from the current versioned set.
271
268
 
272
269
        When a file_id is unversioned, all of its children are automatically
273
270
        unversioned.
274
271
 
275
 
        :param paths: The paths to stop versioning.
 
272
        :param file_ids: The file ids to stop versioning.
276
273
        :raises: NoSuchId if any fileid is not currently versioned.
277
274
        """
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.
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)
288
 
            for file_id in file_ids:
289
 
                if self._inventory.has_id(file_id):
290
 
                    self._inventory.remove_recursive_id(file_id)
 
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
        for file_id in file_ids:
 
279
            if self._inventory.has_id(file_id):
 
280
                self._inventory.remove_recursive_id(file_id)
 
281
            else:
 
282
                raise errors.NoSuchId(self, file_id)
291
283
 
292
284
    def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
293
285
        """See MutableTree.set_parent_trees()."""
301
293
            self._branch_revision_id = revision_ids[0]
302
294
        self._allow_leftmost_as_ghost = allow_leftmost_as_ghost
303
295
        self._set_basis()
304
 
 
 
296
    
305
297
    def _set_basis(self):
306
298
        try:
307
299
            self._basis_tree = self.branch.repository.revision_tree(
313
305
            else:
314
306
                raise
315
307
 
316
 
    def get_symlink_target(self, path):
317
 
        with self.lock_read():
318
 
            return self._file_transport.readlink(path)
319
 
 
320
308
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
321
309
        """See MutableTree.set_parent_trees()."""
322
310
        if len(parents_list) == 0:
323
311
            self._parent_ids = []
324
312
            self._basis_tree = self.branch.repository.revision_tree(
325
 
                _mod_revision.NULL_REVISION)
 
313
                                   _mod_revision.NULL_REVISION)
326
314
        else:
327
315
            if parents_list[0][1] is None and not allow_leftmost_as_ghost:
328
316
                # a ghost in the left most parent
329
317
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
330
318
            self._parent_ids = [parent_id for parent_id, tree in parents_list]
331
 
            if parents_list[0][1] is None or parents_list[0][1] == b'null:':
 
319
            if parents_list[0][1] is None or parents_list[0][1] == 'null:':
332
320
                self._basis_tree = self.branch.repository.revision_tree(
333
 
                    _mod_revision.NULL_REVISION)
 
321
                                       _mod_revision.NULL_REVISION)
334
322
            else:
335
323
                self._basis_tree = parents_list[0][1]
336
324
            self._branch_revision_id = parents_list[0][0]