/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: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

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