/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/git/memorytree.py

  • Committer: Jelmer Vernooij
  • Date: 2017-07-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

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
 
import os
21
 
import posixpath
22
 
import stat
23
 
 
24
 
from dulwich.index import (
25
 
    index_entry_from_stat,
26
 
    )
27
 
from dulwich.objects import (
28
 
    Blob,
29
 
    Tree,
30
 
    )
31
 
 
32
 
from breezy import (
33
 
    errors,
34
 
    lock,
35
 
    osutils,
36
 
    revision as _mod_revision,
37
 
    tree as _mod_tree,
38
 
    urlutils,
39
 
    )
40
 
from breezy.transport.memory import MemoryTransport
41
 
 
42
 
from .mapping import (
43
 
    decode_git_path,
44
 
    encode_git_path,
45
 
    )
46
 
from .tree import MutableGitIndexTree
47
 
 
48
 
 
49
 
class GitMemoryTree(MutableGitIndexTree, _mod_tree.Tree):
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
 
 
62
 
    def _supports_executable(self):
63
 
        return True
64
 
 
65
 
    @property
66
 
    def controldir(self):
67
 
        return self.branch.controldir
68
 
 
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:
78
 
                    kinds[pos] = self.kind(f)
79
 
 
80
 
    def put_file_bytes_non_atomic(self, path, bytes):
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."""
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]
96
 
 
97
 
        trees = [("", tree)]
98
 
        while trees:
99
 
            (path, tree) = trees.pop()
100
 
            for name, mode, sha in tree.iteritems():
101
 
                subpath = posixpath.join(path, decode_git_path(name))
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):
106
 
                    self._file_transport.put_bytes(
107
 
                        subpath, self.store[sha].data)
108
 
                    self._index_add_entry(subpath, 'file')
109
 
                else:
110
 
                    raise NotImplementedError(self._populate_from_branch)
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)
124
 
        except BaseException:
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)
138
 
        except BaseException:
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)
154
 
        except BaseException:
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):
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
 
 
181
 
    def _live_entry(self, path):
182
 
        path = urlutils.quote_from_bytes(path)
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):
187
 
            blob = Blob.from_string(
188
 
                encode_git_path(self._file_transport.readlink(path)))
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
 
 
195
 
    def get_file_with_stat(self, path):
196
 
        return (self.get_file(path), self._lstat(path))
197
 
 
198
 
    def get_file(self, path):
199
 
        """See Tree.get_file."""
200
 
        return self._file_transport.get(path)
201
 
 
202
 
    def get_file_sha1(self, path, stat_value=None):
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():
219
 
            if self.branch.head is None:
220
 
                return _mod_revision.NULL_REVISION
221
 
            return self.branch.repository.lookup_foreign_revision_id(
222
 
                self.branch.head)
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
 
 
231
 
    def has_filename(self, path):
232
 
        return self._file_transport.has(path)
233
 
 
234
 
    def _set_merges_from_parent_ids(self, rhs_parent_ids):
235
 
        if self.branch.head is None:
236
 
            self._parent_ids = []
237
 
        else:
238
 
            self._parent_ids = [self.last_revision()]
239
 
        self._parent_ids.extend(rhs_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 = []
244
 
            self.branch.head = None
245
 
        else:
246
 
            self._parent_ids = parent_ids
247
 
            self.branch.head = self.branch.repository.lookup_bzr_revision_id(
248
 
                parent_ids[0])[0]
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)
261
 
 
262
 
    def get_symlink_target(self, path):
263
 
        with self.lock_read():
264
 
            return self._file_transport.readlink(path)