/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

Merge bzr.dev.

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
    @needs_read_lock
 
110
    def last_revision(self):
 
111
        """See MutableTree.last_revision."""
 
112
        return self._branch_revision_id
 
113
 
 
114
    def lock_read(self):
 
115
        """Lock the memory tree for reading.
 
116
 
 
117
        This triggers population of data from the branch for its revision.
 
118
        """
 
119
        self._locks += 1
 
120
        try:
 
121
            if self._locks == 1:
 
122
                self.branch.lock_read()
 
123
                self._lock_mode = "r"
 
124
                self._populate_from_branch()
 
125
        except:
 
126
            self._locks -= 1
 
127
            raise
 
128
 
 
129
    def lock_write(self):
 
130
        """See MutableTree.lock_write()."""
 
131
        self._locks += 1
 
132
        try:
 
133
            if self._locks == 1:
 
134
                self.branch.lock_write()
 
135
                self._lock_mode = "w"
 
136
                self._populate_from_branch()
 
137
            elif self._lock_mode == "r":
 
138
                raise errors.ReadOnlyError(self)
 
139
        except:
 
140
            self._locks -= 1
 
141
            raise
 
142
 
 
143
    def _populate_from_branch(self):
 
144
        """Populate the in-tree state from the branch."""
 
145
        self._basis_tree = self.branch.repository.revision_tree(
 
146
            self._branch_revision_id)
 
147
        if self._branch_revision_id is None:
 
148
            self._parent_ids = []
 
149
        else:
 
150
            self._parent_ids = [self._branch_revision_id]
 
151
        self._inventory = deepcopy(self._basis_tree._inventory)
 
152
        self._file_transport = MemoryTransport()
 
153
        # TODO copy the revision trees content, or do it lazy, or something.
 
154
        inventory_entries = self._inventory.iter_entries()
 
155
        inventory_entries.next()
 
156
        for path, entry in inventory_entries:
 
157
            if entry.kind == 'directory':
 
158
                self._file_transport.mkdir(path)
 
159
            elif entry.kind == 'file':
 
160
                self._file_transport.put_file(path,
 
161
                    self._basis_tree.get_file(entry.file_id))
 
162
            else:
 
163
                raise NotImplementedError(self._populate_from_branch)
 
164
 
 
165
    def put_file_bytes_non_atomic(self, file_id, bytes):
 
166
        """See MutableTree.put_file_bytes_non_atomic."""
 
167
        self._file_transport.put_bytes(self.id2path(file_id), bytes)
 
168
 
 
169
    def unlock(self):
 
170
        """Release a lock.
 
171
 
 
172
        This frees all cached state when the last lock context for the tree is
 
173
        left.
 
174
        """
 
175
        if self._locks == 1:
 
176
            self._basis_tree = None
 
177
            self._parent_ids = []
 
178
            self._inventory = None
 
179
            try:
 
180
                self.branch.unlock()
 
181
            finally:
 
182
                self._locks = 0
 
183
                self._lock_mode = None
 
184
        else:
 
185
            self._locks -= 1
 
186
 
 
187
    @needs_write_lock
 
188
    def unversion(self, file_ids):
 
189
        """Remove the file ids in file_ids from the current versioned set.
 
190
 
 
191
        When a file_id is unversioned, all of its children are automatically
 
192
        unversioned.
 
193
 
 
194
        :param file_ids: The file ids to stop versioning.
 
195
        :raises: NoSuchId if any fileid is not currently versioned.
 
196
        """
 
197
        # XXX: This should be in mutabletree, but the inventory-save action
 
198
        # is not relevant to memory tree. Until that is done in unlock by
 
199
        # working tree, we cannot share the implementation.
 
200
        for file_id in file_ids:
 
201
            if self._inventory.has_id(file_id):
 
202
                self._inventory.remove_recursive_id(file_id)
 
203
            else:
 
204
                raise errors.NoSuchId(self, file_id)
 
205
 
 
206
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
 
207
        """See MutableTree.set_parent_trees()."""
 
208
        if len(parents_list) == 0:
 
209
            self._parent_ids = []
 
210
            self._basis_tree = self.branch.repository.revisiontree(None)
 
211
        else:
 
212
            if parents_list[0][1] is None and not allow_leftmost_as_ghost:
 
213
                # a ghost in the left most parent
 
214
                raise errors.GhostRevisionUnusableHere(parents_list[0][0])
 
215
            self._parent_ids = [parent_id for parent_id, tree in parents_list]
 
216
            if parents_list[0][1] is None:
 
217
                self._basis_tree = self.branch.repository.revisiontree(None)
 
218
            else:
 
219
                self._basis_tree = parents_list[0][1]
 
220
            self._branch_revision_id = parents_list[0][0]