/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

Update NEWS.

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""MemoryTree object.
 
18
 
 
19
See MemoryTree for more details.
 
20
"""
 
21
 
 
22
 
 
23
from copy import deepcopy
 
24
 
 
25
from bzrlib import errors, mutabletree
 
26
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
27
from bzrlib.osutils import sha_file
 
28
from bzrlib.transport.memory import MemoryTransport
 
29
 
 
30
 
 
31
class MemoryTree(mutabletree.MutableTree):
 
32
    """A MemoryTree is a specialisation of MutableTree.
 
33
    
 
34
    It maintains nearly no state outside of read_lock and write_lock
 
35
    transactions. (it keeps a reference to the branch, and its last-revision
 
36
    only).
 
37
    """
 
38
 
 
39
    def __init__(self, branch, revision_id):
 
40
        """Construct a MemoryTree for branch using revision_id."""
 
41
        self.branch = branch
 
42
        self.bzrdir = branch.bzrdir
 
43
        self._branch_revision_id = revision_id
 
44
        self._locks = 0
 
45
        self._lock_mode = None
 
46
 
 
47
    @needs_write_lock
 
48
    def _add(self, files, ids, kinds):
 
49
        """See MutableTree._add."""
 
50
        for f, file_id, kind in zip(files, ids, kinds):
 
51
            if kind is None:
 
52
                kind = 'file'
 
53
            if file_id is None:
 
54
                self._inventory.add_path(f, kind=kind)
 
55
            else:
 
56
                self._inventory.add_path(f, kind=kind, file_id=file_id)
 
57
 
 
58
    def basis_tree(self):
 
59
        """See Tree.basis_tree()."""
 
60
        return self._basis_tree
 
61
 
 
62
    @staticmethod
 
63
    def create_on_branch(branch):
 
64
        """Create a MemoryTree for branch, using the last-revision of branch."""
 
65
        return MemoryTree(branch, branch.last_revision())
 
66
 
 
67
    def _gather_kinds(self, files, kinds):
 
68
        """See MutableTree._gather_kinds.
 
69
        
 
70
        This implementation does not care about the file kind of
 
71
        missing files, so is a no-op.
 
72
        """
 
73
 
 
74
    def get_file(self, file_id):
 
75
        """See Tree.get_file."""
 
76
        return self._file_transport.get(self.id2path(file_id))
 
77
 
 
78
    def get_file_sha1(self, file_id, path=None):
 
79
        """See Tree.get_file_sha1()."""
 
80
        if path is None:
 
81
            path = self.id2path(file_id)
 
82
        stream = self._file_transport.get(path)
 
83
        return sha_file(stream)
 
84
 
 
85
    @needs_read_lock
 
86
    def get_parent_ids(self):
 
87
        """See Tree.get_parent_ids.
 
88
 
 
89
        This implementation returns the current cached value from
 
90
            self._parent_ids.
 
91
        """
 
92
        return list(self._parent_ids)
 
93
 
 
94
    def has_filename(self, filename):
 
95
        """See Tree.has_filename()."""
 
96
        return self._file_transport.has(filename)
 
97
 
 
98
    def is_executable(self, file_id, path=None):
 
99
        return self._inventory[file_id].executable
 
100
 
 
101
    def mkdir(self, path, file_id=None):
 
102
        """See MutableTree.mkdir()."""
 
103
        self.add(path, file_id, 'directory')
 
104
        if file_id is None:
 
105
            file_id = self.path2id(path)
 
106
        self._file_transport.mkdir(path)
 
107
        return file_id
 
108
 
 
109
    def lock_read(self):
 
110
        """Lock the memory tree for reading.
 
111
 
 
112
        This triggers population of data from the branch for its revision.
 
113
        """
 
114
        self._locks += 1
 
115
        try:
 
116
            if self._locks == 1:
 
117
                self.branch.lock_read()
 
118
                self._lock_mode = "r"
 
119
                self._populate_from_branch()
 
120
        except:
 
121
            self._locks -= 1
 
122
            raise
 
123
 
 
124
    def lock_write(self):
 
125
        """See MutableTree.lock_write()."""
 
126
        self._locks += 1
 
127
        try:
 
128
            if self._locks == 1:
 
129
                self.branch.lock_write()
 
130
                self._lock_mode = "w"
 
131
                self._populate_from_branch()
 
132
            elif self._lock_mode == "r":
 
133
                raise errors.ReadOnlyError(self)
 
134
        except:
 
135
            self._locks -= 1
 
136
            raise
 
137
 
 
138
    def _populate_from_branch(self):
 
139
        """Populate the in-tree state from the branch."""
 
140
        self._basis_tree = self.branch.repository.revision_tree(
 
141
            self._branch_revision_id)
 
142
        if self._branch_revision_id is None:
 
143
            self._parent_ids = []
 
144
        else:
 
145
            self._parent_ids = [self._branch_revision_id]
 
146
        self._inventory = deepcopy(self._basis_tree._inventory)
 
147
        self._file_transport = MemoryTransport()
 
148
        # TODO copy the revision trees content, or do it lazy, or something.
 
149
        inventory_entries = self._inventory.iter_entries()
 
150
        inventory_entries.next()
 
151
        for path, entry in inventory_entries:
 
152
            if entry.kind == 'directory':
 
153
                self._file_transport.mkdir(path)
 
154
            elif entry.kind == 'file':
 
155
                self._file_transport.put_file(path,
 
156
                    self._basis_tree.get_file(entry.file_id))
 
157
            else:
 
158
                raise NotImplementedError(self._populate_from_branch)
 
159
 
 
160
    def put_file_bytes_non_atomic(self, file_id, bytes):
 
161
        """See MutableTree.put_file_bytes_non_atomic."""
 
162
        self._file_transport.put_bytes(self.id2path(file_id), bytes)
 
163
 
 
164
    def unlock(self):
 
165
        """Release a lock.
 
166
 
 
167
        This frees all cached state when the last lock context for the tree is
 
168
        left.
 
169
        """
 
170
        if self._locks == 1:
 
171
            self._basis_tree = None
 
172
            self._parent_ids = []
 
173
            self._inventory = None
 
174
            try:
 
175
                self.branch.unlock()
 
176
            finally:
 
177
                self._locks = 0
 
178
                self._lock_mode = None
 
179
        else:
 
180
            self._locks -= 1
 
181
 
 
182
    @needs_write_lock
 
183
    def unversion(self, file_ids):
 
184
        """Remove the file ids in file_ids from the current versioned set.
 
185
 
 
186
        When a file_id is unversioned, all of its children are automatically
 
187
        unversioned.
 
188
 
 
189
        :param file_ids: The file ids to stop versioning.
 
190
        :raises: NoSuchId if any fileid is not currently versioned.
 
191
        """
 
192
        # XXX: This should be in mutabletree, but the inventory-save action
 
193
        # is not relevant to memory tree. Until that is done in unlock by
 
194
        # working tree, we cannot share the implementation.
 
195
        for file_id in file_ids:
 
196
            if self._inventory.has_id(file_id):
 
197
                self._inventory.remove_recursive_id(file_id)
 
198
            else:
 
199
                raise errors.NoSuchId(self, file_id)
 
200
 
 
201
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
 
202
        """See MutableTree.set_parent_trees()."""
 
203
        if len(parents_list) == 0:
 
204
            self._parent_ids = []
 
205
            self._basis_tree = self.branch.repository.revisiontree(None)
 
206
        else:
 
207
            if parents_list[0][1] is None and not allow_leftmost_as_ghost:
 
208
                # a ghost in the left most parent
 
209
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
 
210
            self._parent_ids = [parent_id for parent_id, tree in parents_list]
 
211
            if parents_list[0][1] is None:
 
212
                self._basis_tree = self.branch.repository.revisiontree(None)
 
213
            else:
 
214
                self._basis_tree = parents_list[0][1]