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

  • Committer: Robert Collins
  • Date: 2010-05-11 08:36:16 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100511083616-b8fjb19zomwupid0
Make all lock methods return Result objects, rather than lock_read returning self, as per John's review.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2018 Jelmer Vernooij <jelmer@jelmer.uk>
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
 
 
18
 
"""Git Memory Trees."""
19
 
 
20
 
from __future__ import absolute_import
21
 
 
22
 
import os
23
 
import posixpath
24
 
import stat
25
 
 
26
 
from dulwich.index import (
27
 
    index_entry_from_stat,
28
 
    )
29
 
from dulwich.objects import (
30
 
    Blob,
31
 
    Tree,
32
 
    )
33
 
 
34
 
from breezy import (
35
 
    errors,
36
 
    lock,
37
 
    osutils,
38
 
    revision as _mod_revision,
39
 
    tree as _mod_tree,
40
 
    urlutils,
41
 
    )
42
 
from breezy.transport.memory import MemoryTransport
43
 
 
44
 
from .mapping import (
45
 
    decode_git_path,
46
 
    encode_git_path,
47
 
    )
48
 
from .tree import MutableGitIndexTree
49
 
 
50
 
 
51
 
class GitMemoryTree(MutableGitIndexTree, _mod_tree.Tree):
52
 
    """A Git memory tree."""
53
 
 
54
 
    def __init__(self, branch, store, head):
55
 
        MutableGitIndexTree.__init__(self)
56
 
        self.branch = branch
57
 
        self.mapping = self.branch.repository.get_mapping()
58
 
        self.store = store
59
 
        self.index = {}
60
 
        self._locks = 0
61
 
        self._lock_mode = None
62
 
        self._populate_from_branch()
63
 
 
64
 
    def _supports_executable(self):
65
 
        return True
66
 
 
67
 
    @property
68
 
    def controldir(self):
69
 
        return self.branch.controldir
70
 
 
71
 
    def is_control_filename(self, path):
72
 
        return False
73
 
 
74
 
    def _gather_kinds(self, files, kinds):
75
 
        """See MutableTree._gather_kinds.
76
 
        """
77
 
        with self.lock_tree_write():
78
 
            for pos, f in enumerate(files):
79
 
                if kinds[pos] is None:
80
 
                    kinds[pos] = self.kind(f)
81
 
 
82
 
    def put_file_bytes_non_atomic(self, path, bytes):
83
 
        """See MutableTree.put_file_bytes_non_atomic."""
84
 
        self._file_transport.put_bytes(path, bytes)
85
 
 
86
 
    def _populate_from_branch(self):
87
 
        """Populate the in-tree state from the branch."""
88
 
        if self.branch.head is None:
89
 
            self._parent_ids = []
90
 
        else:
91
 
            self._parent_ids = [self.last_revision()]
92
 
        self._file_transport = MemoryTransport()
93
 
        if self.branch.head is None:
94
 
            tree = Tree()
95
 
        else:
96
 
            tree_id = self.store[self.branch.head].tree
97
 
            tree = self.store[tree_id]
98
 
 
99
 
        trees = [("", tree)]
100
 
        while trees:
101
 
            (path, tree) = trees.pop()
102
 
            for name, mode, sha in tree.iteritems():
103
 
                subpath = posixpath.join(path, decode_git_path(name))
104
 
                if stat.S_ISDIR(mode):
105
 
                    self._file_transport.mkdir(subpath)
106
 
                    trees.append((subpath, self.store[sha]))
107
 
                elif stat.S_ISREG(mode):
108
 
                    self._file_transport.put_bytes(
109
 
                        subpath, self.store[sha].data)
110
 
                    self._index_add_entry(subpath, 'file')
111
 
                else:
112
 
                    raise NotImplementedError(self._populate_from_branch)
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
 
            return lock.LogicalLockResult(self.unlock)
126
 
        except BaseException:
127
 
            self._locks -= 1
128
 
            raise
129
 
 
130
 
    def lock_tree_write(self):
131
 
        """See MutableTree.lock_tree_write()."""
132
 
        self._locks += 1
133
 
        try:
134
 
            if self._locks == 1:
135
 
                self.branch.lock_read()
136
 
                self._lock_mode = "w"
137
 
                self._populate_from_branch()
138
 
            elif self._lock_mode == "r":
139
 
                raise errors.ReadOnlyError(self)
140
 
        except BaseException:
141
 
            self._locks -= 1
142
 
            raise
143
 
        return lock.LogicalLockResult(self.unlock)
144
 
 
145
 
    def lock_write(self):
146
 
        """See MutableTree.lock_write()."""
147
 
        self._locks += 1
148
 
        try:
149
 
            if self._locks == 1:
150
 
                self.branch.lock_write()
151
 
                self._lock_mode = "w"
152
 
                self._populate_from_branch()
153
 
            elif self._lock_mode == "r":
154
 
                raise errors.ReadOnlyError(self)
155
 
            return lock.LogicalLockResult(self.unlock)
156
 
        except BaseException:
157
 
            self._locks -= 1
158
 
            raise
159
 
 
160
 
    def unlock(self):
161
 
        """Release a lock.
162
 
 
163
 
        This frees all cached state when the last lock context for the tree is
164
 
        left.
165
 
        """
166
 
        if self._locks == 1:
167
 
            self._parent_ids = []
168
 
            self.index = {}
169
 
            try:
170
 
                self.branch.unlock()
171
 
            finally:
172
 
                self._locks = 0
173
 
                self._lock_mode = None
174
 
        else:
175
 
            self._locks -= 1
176
 
 
177
 
    def _lstat(self, path):
178
 
        mem_stat = self._file_transport.stat(path)
179
 
        stat_val = os.stat_result(
180
 
            (mem_stat.st_mode, 0, 0, 0, 0, 0, mem_stat.st_size, 0, 0, 0))
181
 
        return stat_val
182
 
 
183
 
    def _live_entry(self, path):
184
 
        path = urlutils.quote_from_bytes(path)
185
 
        stat_val = self._lstat(path)
186
 
        if stat.S_ISDIR(stat_val.st_mode):
187
 
            return None
188
 
        elif stat.S_ISLNK(stat_val.st_mode):
189
 
            blob = Blob.from_string(
190
 
                encode_git_path(self._file_transport.readlink(path)))
191
 
        elif stat.S_ISREG(stat_val.st_mode):
192
 
            blob = Blob.from_string(self._file_transport.get_bytes(path))
193
 
        else:
194
 
            raise AssertionError('unknown type %d' % stat_val.st_mode)
195
 
        return index_entry_from_stat(stat_val, blob.id, 0)
196
 
 
197
 
    def get_file_with_stat(self, path):
198
 
        return (self.get_file(path), self._lstat(path))
199
 
 
200
 
    def get_file(self, path):
201
 
        """See Tree.get_file."""
202
 
        return self._file_transport.get(path)
203
 
 
204
 
    def get_file_sha1(self, path, stat_value=None):
205
 
        """See Tree.get_file_sha1()."""
206
 
        stream = self._file_transport.get(path)
207
 
        return osutils.sha_file(stream)
208
 
 
209
 
    def get_parent_ids(self):
210
 
        """See Tree.get_parent_ids.
211
 
 
212
 
        This implementation returns the current cached value from
213
 
            self._parent_ids.
214
 
        """
215
 
        with self.lock_read():
216
 
            return list(self._parent_ids)
217
 
 
218
 
    def last_revision(self):
219
 
        """See MutableTree.last_revision."""
220
 
        with self.lock_read():
221
 
            if self.branch.head is None:
222
 
                return _mod_revision.NULL_REVISION
223
 
            return self.branch.repository.lookup_foreign_revision_id(
224
 
                self.branch.head)
225
 
 
226
 
    def basis_tree(self):
227
 
        """See Tree.basis_tree()."""
228
 
        return self.branch.repository.revision_tree(self.last_revision())
229
 
 
230
 
    def get_config_stack(self):
231
 
        return self.branch.get_config_stack()
232
 
 
233
 
    def has_filename(self, path):
234
 
        return self._file_transport.has(path)
235
 
 
236
 
    def _set_merges_from_parent_ids(self, rhs_parent_ids):
237
 
        if self.branch.head is None:
238
 
            self._parent_ids = []
239
 
        else:
240
 
            self._parent_ids = [self.last_revision()]
241
 
        self._parent_ids.extend(rhs_parent_ids)
242
 
 
243
 
    def set_parent_ids(self, parent_ids, allow_leftmost_as_ghost=False):
244
 
        if len(parent_ids) == 0:
245
 
            self._parent_ids = []
246
 
            self.branch.head = None
247
 
        else:
248
 
            self._parent_ids = parent_ids
249
 
            self.branch.head = self.branch.repository.lookup_bzr_revision_id(
250
 
                parent_ids[0])[0]
251
 
 
252
 
    def mkdir(self, path, file_id=None):
253
 
        """See MutableTree.mkdir()."""
254
 
        self.add(path, None, 'directory')
255
 
        self._file_transport.mkdir(path)
256
 
 
257
 
    def _rename_one(self, from_rel, to_rel):
258
 
        self._file_transport.rename(from_rel, to_rel)
259
 
 
260
 
    def kind(self, p):
261
 
        stat_value = self._file_transport.stat(p)
262
 
        return osutils.file_kind_from_stat_mode(stat_value.st_mode)
263
 
 
264
 
    def get_symlink_target(self, path):
265
 
        with self.lock_read():
266
 
            return self._file_transport.readlink(path)