/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
0.360.5 by Jelmer Vernooij
Use head from branch.
61
    @property
62
    def controldir(self):
63
        return self.branch.controldir
64
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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:
0.360.4 by Jelmer Vernooij
Implement MemoryTree.rename_one, MemoryTree.mkdir.
74
                    kinds[pos] = self.kind(f)
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
75
7192.5.1 by Jelmer Vernooij
Remove more file ids.
76
    def put_file_bytes_non_atomic(self, path, bytes):
0.360.1 by Jelmer Vernooij
Implement GitMemoryTree.
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."""
0.360.5 by Jelmer Vernooij
Use head from 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
            self._basis_fileid_map = GitFileIdMap({}, self.mapping)
90
        else:
91
            tree_id = self.store[self.branch.head].tree
92
            self._basis_fileid_map = self.mapping.get_fileid_map(
93
                self.store.__getitem__, tree_id)
94
            tree = self.store[tree_id]
95
        self._fileid_map = self._basis_fileid_map.copy()
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():
7045.1.11 by Jelmer Vernooij
Some annotate fixes.
101
                subpath = posixpath.join(path, name.decode('utf-8'))
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(
188
                self._file_transport.readlink(path).encode('utf-8'))
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)