/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/git/memorytree.py

  • Committer: Jelmer Vernooij
  • Date: 2018-07-08 14:45:27 UTC
  • mto: This revision was merged to the branch mainline in revision 7036.
  • Revision ID: jelmer@jelmer.uk-20180708144527-codhlvdcdg9y0nji
Fix a bunch of merge tests.

Show diffs side-by-side

added added

removed removed

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