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