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