/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: Jelmer Vernooij
  • Date: 2019-07-12 01:01:45 UTC
  • mto: This revision was merged to the branch mainline in revision 7375.
  • Revision ID: jelmer@jelmer.uk-20190712010145-m7224qumb8w068zw
Fix importing from remote git repositories.

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