/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: 2005-10-19 10:11:57 UTC
  • mfrom: (1185.16.78)
  • mto: This revision was merged to the branch mainline in revision 1470.
  • Revision ID: robertc@robertcollins.net-20051019101157-17438d311e746b4f
mergeĀ fromĀ upstream

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