/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.363.1 by Jelmer Vernooij
Fix copyright header in memorytree.py.
1
# Copyright (C) 2018 Jelmer Vernooij <jelmer@jelmer.uk>
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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
0.363.1 by Jelmer Vernooij
Fix copyright header in memorytree.py.
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
16
17
18
"""Git Memory Trees."""
19
20
from __future__ import absolute_import
21
0.360.4 by Jelmer Vernooij
Implement MemoryTree.rename_one, MemoryTree.mkdir.
22
import os
23
import posixpath
24
import stat
25
6973.1.1 by Jelmer Vernooij
Make InterIndexGitTree suitable for use with MemoryGitTree.
26
from dulwich.index import (
27
    index_entry_from_stat,
28
    )
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
29
from dulwich.objects import (
6973.1.1 by Jelmer Vernooij
Make InterIndexGitTree suitable for use with MemoryGitTree.
30
    Blob,
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
31
    Tree,
32
    )
33
34
from breezy import (
0.360.5 by Jelmer Vernooij
Use head from branch.
35
    errors,
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
36
    lock,
37
    osutils,
38
    revision as _mod_revision,
39
    tree as _mod_tree,
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
40
    urlutils,
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
41
    )
42
from breezy.transport.memory import MemoryTransport
43
44
from .mapping import GitFileIdMap
45
from .tree import MutableGitIndexTree
46
7143.15.2 by Jelmer Vernooij
Run autopep8.
47
48
class GitMemoryTree(MutableGitIndexTree, _mod_tree.Tree):
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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
7122.7.6 by Jelmer Vernooij
Fix GitMemoryTree tests.
61
    def _supports_executable(self):
62
        return True
63
0.360.5 by Jelmer Vernooij
Use head from branch.
64
    @property
65
    def controldir(self):
66
        return self.branch.controldir
67
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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:
0.360.4 by Jelmer Vernooij
Implement MemoryTree.rename_one, MemoryTree.mkdir.
77
                    kinds[pos] = self.kind(f)
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
78
7192.5.1 by Jelmer Vernooij
Remove more file ids.
79
    def put_file_bytes_non_atomic(self, path, bytes):
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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."""
0.360.5 by Jelmer Vernooij
Use head from 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()
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
99
0.360.5 by Jelmer Vernooij
Use head from branch.
100
        trees = [("", tree)]
101
        while trees:
102
            (path, tree) = trees.pop()
103
            for name, mode, sha in tree.iteritems():
7045.1.11 by Jelmer Vernooij
Some annotate fixes.
104
                subpath = posixpath.join(path, name.decode('utf-8'))
0.360.5 by Jelmer Vernooij
Use head from branch.
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):
7143.15.2 by Jelmer Vernooij
Run autopep8.
109
                    self._file_transport.put_bytes(
110
                        subpath, self.store[sha].data)
0.360.5 by Jelmer Vernooij
Use head from branch.
111
                    self._index_add_entry(subpath, 'file')
112
                else:
113
                    raise NotImplementedError(self._populate_from_branch)
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
127
        except BaseException:
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
141
        except BaseException:
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
157
        except BaseException:
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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):
0.360.4 by Jelmer Vernooij
Implement MemoryTree.rename_one, MemoryTree.mkdir.
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
6973.1.1 by Jelmer Vernooij
Make InterIndexGitTree suitable for use with MemoryGitTree.
184
    def _live_entry(self, path):
7045.3.2 by Jelmer Vernooij
Fix tests.
185
        path = urlutils.quote_from_bytes(path)
6973.1.1 by Jelmer Vernooij
Make InterIndexGitTree suitable for use with MemoryGitTree.
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):
7143.15.2 by Jelmer Vernooij
Run autopep8.
190
            blob = Blob.from_string(
191
                self._file_transport.readlink(path).encode('utf-8'))
6973.1.1 by Jelmer Vernooij
Make InterIndexGitTree suitable for use with MemoryGitTree.
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
7192.5.1 by Jelmer Vernooij
Remove more file ids.
198
    def get_file_with_stat(self, path):
199
        return (self.get_file(path), self._lstat(path))
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
200
7192.5.1 by Jelmer Vernooij
Remove more file ids.
201
    def get_file(self, path):
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
202
        """See Tree.get_file."""
203
        return self._file_transport.get(path)
204
7192.5.1 by Jelmer Vernooij
Remove more file ids.
205
    def get_file_sha1(self, path, stat_value=None):
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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():
0.360.5 by Jelmer Vernooij
Use head from branch.
222
            if self.branch.head is None:
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
223
                return _mod_revision.NULL_REVISION
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
224
            return self.branch.repository.lookup_foreign_revision_id(
225
                self.branch.head)
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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
0.360.3 by Jelmer Vernooij
Implement has_filename / set_parent_ids.
234
    def has_filename(self, path):
235
        return self._file_transport.has(path)
236
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
237
    def _set_merges_from_parent_ids(self, rhs_parent_ids):
0.360.5 by Jelmer Vernooij
Use head from branch.
238
        if self.branch.head is None:
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
239
            self._parent_ids = []
240
        else:
241
            self._parent_ids = [self.last_revision()]
242
        self._parent_ids.extend(rhs_parent_ids)
0.360.3 by Jelmer Vernooij
Implement has_filename / set_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 = []
0.360.5 by Jelmer Vernooij
Use head from branch.
247
            self.branch.head = None
0.360.3 by Jelmer Vernooij
Implement has_filename / set_parent_ids.
248
        else:
249
            self._parent_ids = parent_ids
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
250
            self.branch.head = self.branch.repository.lookup_bzr_revision_id(
251
                parent_ids[0])[0]
0.360.4 by Jelmer Vernooij
Implement MemoryTree.rename_one, MemoryTree.mkdir.
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)
7058.6.5 by Jelmer Vernooij
Add GitMemoryTree.get_symlink_target.
264
265
    def get_symlink_target(self, path):
266
        with self.lock_read():
267
            return self._file_transport.readlink(path)