/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-07-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

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