/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
7490.70.1 by Jelmer Vernooij
Add functions for encoding/decoding git paths.
44
from .mapping import (
45
    decode_git_path,
46
    encode_git_path,
47
    )
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
48
from .tree import MutableGitIndexTree
49
7143.15.2 by Jelmer Vernooij
Run autopep8.
50
51
class GitMemoryTree(MutableGitIndexTree, _mod_tree.Tree):
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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
7122.7.6 by Jelmer Vernooij
Fix GitMemoryTree tests.
64
    def _supports_executable(self):
65
        return True
66
0.360.5 by Jelmer Vernooij
Use head from branch.
67
    @property
68
    def controldir(self):
69
        return self.branch.controldir
70
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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:
0.360.4 by Jelmer Vernooij
Implement MemoryTree.rename_one, MemoryTree.mkdir.
80
                    kinds[pos] = self.kind(f)
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
81
7192.5.1 by Jelmer Vernooij
Remove more file ids.
82
    def put_file_bytes_non_atomic(self, path, bytes):
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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."""
0.360.5 by Jelmer Vernooij
Use head from 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]
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
98
0.360.5 by Jelmer Vernooij
Use head from branch.
99
        trees = [("", tree)]
100
        while trees:
101
            (path, tree) = trees.pop()
102
            for name, mode, sha in tree.iteritems():
7490.70.1 by Jelmer Vernooij
Add functions for encoding/decoding git paths.
103
                subpath = posixpath.join(path, decode_git_path(name))
0.360.5 by Jelmer Vernooij
Use head from branch.
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):
7143.15.2 by Jelmer Vernooij
Run autopep8.
108
                    self._file_transport.put_bytes(
109
                        subpath, self.store[sha].data)
0.360.5 by Jelmer Vernooij
Use head from branch.
110
                    self._index_add_entry(subpath, 'file')
111
                else:
112
                    raise NotImplementedError(self._populate_from_branch)
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
126
        except BaseException:
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
140
        except BaseException:
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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)
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
156
        except BaseException:
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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):
0.360.4 by Jelmer Vernooij
Implement MemoryTree.rename_one, MemoryTree.mkdir.
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
6973.1.1 by Jelmer Vernooij
Make InterIndexGitTree suitable for use with MemoryGitTree.
183
    def _live_entry(self, path):
7045.3.2 by Jelmer Vernooij
Fix tests.
184
        path = urlutils.quote_from_bytes(path)
6973.1.1 by Jelmer Vernooij
Make InterIndexGitTree suitable for use with MemoryGitTree.
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):
7143.15.2 by Jelmer Vernooij
Run autopep8.
189
            blob = Blob.from_string(
7490.70.1 by Jelmer Vernooij
Add functions for encoding/decoding git paths.
190
                encode_git_path(self._file_transport.readlink(path)))
6973.1.1 by Jelmer Vernooij
Make InterIndexGitTree suitable for use with MemoryGitTree.
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
7192.5.1 by Jelmer Vernooij
Remove more file ids.
197
    def get_file_with_stat(self, path):
198
        return (self.get_file(path), self._lstat(path))
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
199
7192.5.1 by Jelmer Vernooij
Remove more file ids.
200
    def get_file(self, path):
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
201
        """See Tree.get_file."""
202
        return self._file_transport.get(path)
203
7192.5.1 by Jelmer Vernooij
Remove more file ids.
204
    def get_file_sha1(self, path, stat_value=None):
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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():
0.360.5 by Jelmer Vernooij
Use head from branch.
221
            if self.branch.head is None:
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
222
                return _mod_revision.NULL_REVISION
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
223
            return self.branch.repository.lookup_foreign_revision_id(
224
                self.branch.head)
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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
0.360.3 by Jelmer Vernooij
Implement has_filename / set_parent_ids.
233
    def has_filename(self, path):
234
        return self._file_transport.has(path)
235
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
236
    def _set_merges_from_parent_ids(self, rhs_parent_ids):
0.360.5 by Jelmer Vernooij
Use head from branch.
237
        if self.branch.head is None:
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
238
            self._parent_ids = []
239
        else:
240
            self._parent_ids = [self.last_revision()]
241
        self._parent_ids.extend(rhs_parent_ids)
0.360.3 by Jelmer Vernooij
Implement has_filename / set_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 = []
0.360.5 by Jelmer Vernooij
Use head from branch.
246
            self.branch.head = None
0.360.3 by Jelmer Vernooij
Implement has_filename / set_parent_ids.
247
        else:
248
            self._parent_ids = parent_ids
7143.15.3 by Jelmer Vernooij
Fix pep8 issues in breezy.git.
249
            self.branch.head = self.branch.repository.lookup_bzr_revision_id(
250
                parent_ids[0])[0]
0.360.4 by Jelmer Vernooij
Implement MemoryTree.rename_one, MemoryTree.mkdir.
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)
7058.6.5 by Jelmer Vernooij
Add GitMemoryTree.get_symlink_target.
263
264
    def get_symlink_target(self, path):
265
        with self.lock_read():
266
            return self._file_transport.readlink(path)