/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/tree.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009-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 Trees."""
19
 
 
20
 
from collections import deque
21
 
import errno
22
 
from io import BytesIO
23
 
import os
24
 
 
25
 
from dulwich.config import (
26
 
    parse_submodules,
27
 
    ConfigFile as GitConfigFile,
28
 
    )
29
 
from dulwich.diff_tree import tree_changes
30
 
from dulwich.errors import NotTreeError
31
 
from dulwich.index import (
32
 
    blob_from_path_and_stat,
33
 
    cleanup_mode,
34
 
    commit_tree,
35
 
    index_entry_from_stat,
36
 
    Index,
37
 
    )
38
 
from dulwich.object_store import (
39
 
    tree_lookup_path,
40
 
    OverlayObjectStore,
41
 
    )
42
 
from dulwich.objects import (
43
 
    Blob,
44
 
    Tree,
45
 
    ZERO_SHA,
46
 
    S_IFGITLINK,
47
 
    S_ISGITLINK,
48
 
    )
49
 
import stat
50
 
import posixpath
51
 
 
52
 
from .. import (
53
 
    controldir as _mod_controldir,
54
 
    delta,
55
 
    errors,
56
 
    mutabletree,
57
 
    osutils,
58
 
    revisiontree,
59
 
    trace,
60
 
    tree as _mod_tree,
61
 
    workingtree,
62
 
    )
63
 
from ..revision import (
64
 
    CURRENT_REVISION,
65
 
    NULL_REVISION,
66
 
    )
67
 
 
68
 
from .mapping import (
69
 
    mode_is_executable,
70
 
    mode_kind,
71
 
    default_mapping,
72
 
    )
73
 
from .transportgit import (
74
 
    TransportObjectStore,
75
 
    TransportRepo,
76
 
    )
77
 
 
78
 
 
79
 
class GitTreeDirectory(_mod_tree.TreeDirectory):
80
 
 
81
 
    __slots__ = ['file_id', 'name', 'parent_id', 'children']
82
 
 
83
 
    def __init__(self, file_id, name, parent_id):
84
 
        self.file_id = file_id
85
 
        self.name = name
86
 
        self.parent_id = parent_id
87
 
        # TODO(jelmer)
88
 
        self.children = {}
89
 
 
90
 
    @property
91
 
    def kind(self):
92
 
        return 'directory'
93
 
 
94
 
    @property
95
 
    def executable(self):
96
 
        return False
97
 
 
98
 
    def copy(self):
99
 
        return self.__class__(
100
 
            self.file_id, self.name, self.parent_id)
101
 
 
102
 
    def __repr__(self):
103
 
        return "%s(file_id=%r, name=%r, parent_id=%r)" % (
104
 
            self.__class__.__name__, self.file_id, self.name,
105
 
            self.parent_id)
106
 
 
107
 
    def __eq__(self, other):
108
 
        return (self.kind == other.kind and
109
 
                self.file_id == other.file_id and
110
 
                self.name == other.name and
111
 
                self.parent_id == other.parent_id)
112
 
 
113
 
 
114
 
class GitTreeFile(_mod_tree.TreeFile):
115
 
 
116
 
    __slots__ = ['file_id', 'name', 'parent_id', 'text_size', 'text_sha1',
117
 
                 'executable']
118
 
 
119
 
    def __init__(self, file_id, name, parent_id, text_size=None,
120
 
                 text_sha1=None, executable=None):
121
 
        self.file_id = file_id
122
 
        self.name = name
123
 
        self.parent_id = parent_id
124
 
        self.text_size = text_size
125
 
        self.text_sha1 = text_sha1
126
 
        self.executable = executable
127
 
 
128
 
    @property
129
 
    def kind(self):
130
 
        return 'file'
131
 
 
132
 
    def __eq__(self, other):
133
 
        return (self.kind == other.kind and
134
 
                self.file_id == other.file_id and
135
 
                self.name == other.name and
136
 
                self.parent_id == other.parent_id and
137
 
                self.text_sha1 == other.text_sha1 and
138
 
                self.text_size == other.text_size and
139
 
                self.executable == other.executable)
140
 
 
141
 
    def __repr__(self):
142
 
        return ("%s(file_id=%r, name=%r, parent_id=%r, text_size=%r, "
143
 
                "text_sha1=%r, executable=%r)") % (
144
 
            type(self).__name__, self.file_id, self.name, self.parent_id,
145
 
            self.text_size, self.text_sha1, self.executable)
146
 
 
147
 
    def copy(self):
148
 
        ret = self.__class__(
149
 
            self.file_id, self.name, self.parent_id)
150
 
        ret.text_sha1 = self.text_sha1
151
 
        ret.text_size = self.text_size
152
 
        ret.executable = self.executable
153
 
        return ret
154
 
 
155
 
 
156
 
class GitTreeSymlink(_mod_tree.TreeLink):
157
 
 
158
 
    __slots__ = ['file_id', 'name', 'parent_id', 'symlink_target']
159
 
 
160
 
    def __init__(self, file_id, name, parent_id,
161
 
                 symlink_target=None):
162
 
        self.file_id = file_id
163
 
        self.name = name
164
 
        self.parent_id = parent_id
165
 
        self.symlink_target = symlink_target
166
 
 
167
 
    @property
168
 
    def kind(self):
169
 
        return 'symlink'
170
 
 
171
 
    @property
172
 
    def executable(self):
173
 
        return False
174
 
 
175
 
    @property
176
 
    def text_size(self):
177
 
        return None
178
 
 
179
 
    def __repr__(self):
180
 
        return "%s(file_id=%r, name=%r, parent_id=%r, symlink_target=%r)" % (
181
 
            type(self).__name__, self.file_id, self.name, self.parent_id,
182
 
            self.symlink_target)
183
 
 
184
 
    def __eq__(self, other):
185
 
        return (self.kind == other.kind and
186
 
                self.file_id == other.file_id and
187
 
                self.name == other.name and
188
 
                self.parent_id == other.parent_id and
189
 
                self.symlink_target == other.symlink_target)
190
 
 
191
 
    def copy(self):
192
 
        return self.__class__(
193
 
            self.file_id, self.name, self.parent_id,
194
 
            self.symlink_target)
195
 
 
196
 
 
197
 
class GitTreeSubmodule(_mod_tree.TreeReference):
198
 
 
199
 
    __slots__ = ['file_id', 'name', 'parent_id', 'reference_revision']
200
 
 
201
 
    def __init__(self, file_id, name, parent_id, reference_revision=None):
202
 
        self.file_id = file_id
203
 
        self.name = name
204
 
        self.parent_id = parent_id
205
 
        self.reference_revision = reference_revision
206
 
 
207
 
    @property
208
 
    def executable(self):
209
 
        return False
210
 
 
211
 
    @property
212
 
    def kind(self):
213
 
        return 'tree-reference'
214
 
 
215
 
    def __repr__(self):
216
 
        return ("%s(file_id=%r, name=%r, parent_id=%r, "
217
 
                "reference_revision=%r)") % (
218
 
            type(self).__name__, self.file_id, self.name, self.parent_id,
219
 
            self.reference_revision)
220
 
 
221
 
    def __eq__(self, other):
222
 
        return (self.kind == other.kind and
223
 
                self.file_id == other.file_id and
224
 
                self.name == other.name and
225
 
                self.parent_id == other.parent_id and
226
 
                self.reference_revision == other.reference_revision)
227
 
 
228
 
    def copy(self):
229
 
        return self.__class__(
230
 
            self.file_id, self.name, self.parent_id,
231
 
            self.reference_revision)
232
 
 
233
 
 
234
 
entry_factory = {
235
 
    'directory': GitTreeDirectory,
236
 
    'file': GitTreeFile,
237
 
    'symlink': GitTreeSymlink,
238
 
    'tree-reference': GitTreeSubmodule,
239
 
    }
240
 
 
241
 
 
242
 
def ensure_normalized_path(path):
243
 
    """Check whether path is normalized.
244
 
 
245
 
    :raises InvalidNormalization: When path is not normalized, and cannot be
246
 
        accessed on this platform by the normalized path.
247
 
    :return: The NFC normalised version of path.
248
 
    """
249
 
    norm_path, can_access = osutils.normalized_filename(path)
250
 
    if norm_path != path:
251
 
        if can_access:
252
 
            return norm_path
253
 
        else:
254
 
            raise errors.InvalidNormalization(path)
255
 
    return path
256
 
 
257
 
 
258
 
class GitRevisionTree(revisiontree.RevisionTree):
259
 
    """Revision tree implementation based on Git objects."""
260
 
 
261
 
    def __init__(self, repository, revision_id):
262
 
        self._revision_id = revision_id
263
 
        self._repository = repository
264
 
        self._submodules = None
265
 
        self.store = repository._git.object_store
266
 
        if not isinstance(revision_id, bytes):
267
 
            raise TypeError(revision_id)
268
 
        self.commit_id, self.mapping = repository.lookup_bzr_revision_id(
269
 
            revision_id)
270
 
        if revision_id == NULL_REVISION:
271
 
            self.tree = None
272
 
            self.mapping = default_mapping
273
 
        else:
274
 
            try:
275
 
                commit = self.store[self.commit_id]
276
 
            except KeyError:
277
 
                raise errors.NoSuchRevision(repository, revision_id)
278
 
            self.tree = commit.tree
279
 
 
280
 
    def _submodule_info(self):
281
 
        if self._submodules is None:
282
 
            try:
283
 
                with self.get_file('.gitmodules') as f:
284
 
                    config = GitConfigFile.from_file(f)
285
 
                    self._submodules = {
286
 
                        path: (url, section)
287
 
                        for path, url, section in parse_submodules(config)}
288
 
            except errors.NoSuchFile:
289
 
                self._submodules = {}
290
 
        return self._submodules
291
 
 
292
 
    def _get_submodule_repository(self, relpath):
293
 
        if not isinstance(relpath, bytes):
294
 
            raise TypeError(relpath)
295
 
        try:
296
 
            info = self._submodule_info()[relpath]
297
 
        except KeyError:
298
 
            nested_repo_transport = self._repository.controldir.user_transport.clone(
299
 
                relpath.decode('utf-8'))
300
 
        else:
301
 
            nested_repo_transport = self._repository.controldir.control_transport.clone(
302
 
                posixpath.join('modules', info[1].decode('utf-8')))
303
 
        nested_controldir = _mod_controldir.ControlDir.open_from_transport(
304
 
            nested_repo_transport)
305
 
        return nested_controldir.find_repository()
306
 
 
307
 
    def _get_submodule_store(self, relpath):
308
 
        return self._get_submodule_repository(relpath)._git.object_store
309
 
 
310
 
    def get_nested_tree(self, path):
311
 
        encoded_path = path.encode('utf-8')
312
 
        nested_repo = self._get_submodule_repository(encoded_path)
313
 
        ref_rev = self.get_reference_revision(path)
314
 
        return nested_repo.revision_tree(ref_rev)
315
 
 
316
 
    def supports_rename_tracking(self):
317
 
        return False
318
 
 
319
 
    def get_file_revision(self, path):
320
 
        change_scanner = self._repository._file_change_scanner
321
 
        if self.commit_id == ZERO_SHA:
322
 
            return NULL_REVISION
323
 
        (unused_path, commit_id) = change_scanner.find_last_change_revision(
324
 
            path.encode('utf-8'), self.commit_id)
325
 
        return self._repository.lookup_foreign_revision_id(
326
 
            commit_id, self.mapping)
327
 
 
328
 
    def get_file_mtime(self, path):
329
 
        try:
330
 
            revid = self.get_file_revision(path)
331
 
        except KeyError:
332
 
            raise errors.NoSuchFile(path)
333
 
        try:
334
 
            rev = self._repository.get_revision(revid)
335
 
        except errors.NoSuchRevision:
336
 
            raise _mod_tree.FileTimestampUnavailable(path)
337
 
        return rev.timestamp
338
 
 
339
 
    def id2path(self, file_id, recurse='down'):
340
 
        try:
341
 
            path = self.mapping.parse_file_id(file_id)
342
 
        except ValueError:
343
 
            raise errors.NoSuchId(self, file_id)
344
 
        if self.is_versioned(path):
345
 
            return path
346
 
        raise errors.NoSuchId(self, file_id)
347
 
 
348
 
    def is_versioned(self, path):
349
 
        return self.has_filename(path)
350
 
 
351
 
    def path2id(self, path):
352
 
        if self.mapping.is_special_file(path):
353
 
            return None
354
 
        if not self.is_versioned(path):
355
 
            return None
356
 
        return self.mapping.generate_file_id(osutils.safe_unicode(path))
357
 
 
358
 
    def all_file_ids(self):
359
 
        raise errors.UnsupportedOperation(self.all_file_ids, self)
360
 
 
361
 
    def all_versioned_paths(self):
362
 
        ret = {u''}
363
 
        todo = [(self.store, b'', self.tree)]
364
 
        while todo:
365
 
            (store, path, tree_id) = todo.pop()
366
 
            if tree_id is None:
367
 
                continue
368
 
            tree = store[tree_id]
369
 
            for name, mode, hexsha in tree.items():
370
 
                subpath = posixpath.join(path, name)
371
 
                ret.add(subpath.decode('utf-8'))
372
 
                if stat.S_ISDIR(mode):
373
 
                    todo.append((store, subpath, hexsha))
374
 
        return ret
375
 
 
376
 
    def _lookup_path(self, path):
377
 
        if self.tree is None:
378
 
            raise errors.NoSuchFile(path)
379
 
 
380
 
        encoded_path = path.encode('utf-8')
381
 
        parts = encoded_path.split(b'/')
382
 
        hexsha = self.tree
383
 
        store = self.store
384
 
        mode = None
385
 
        for i, p in enumerate(parts):
386
 
            if not p:
387
 
                continue
388
 
            obj = store[hexsha]
389
 
            if not isinstance(obj, Tree):
390
 
                raise NotTreeError(hexsha)
391
 
            try:
392
 
                mode, hexsha = obj[p]
393
 
            except KeyError:
394
 
                raise errors.NoSuchFile(path)
395
 
            if S_ISGITLINK(mode) and i != len(parts) - 1:
396
 
                store = self._get_submodule_store(b'/'.join(parts[:i + 1]))
397
 
                hexsha = store[hexsha].tree
398
 
        return (store, mode, hexsha)
399
 
 
400
 
    def is_executable(self, path):
401
 
        (store, mode, hexsha) = self._lookup_path(path)
402
 
        if mode is None:
403
 
            # the tree root is a directory
404
 
            return False
405
 
        return mode_is_executable(mode)
406
 
 
407
 
    def kind(self, path):
408
 
        (store, mode, hexsha) = self._lookup_path(path)
409
 
        if mode is None:
410
 
            # the tree root is a directory
411
 
            return "directory"
412
 
        return mode_kind(mode)
413
 
 
414
 
    def has_filename(self, path):
415
 
        try:
416
 
            self._lookup_path(path)
417
 
        except errors.NoSuchFile:
418
 
            return False
419
 
        else:
420
 
            return True
421
 
 
422
 
    def _submodule_info(self):
423
 
        if self._submodules is None:
424
 
            try:
425
 
                with self.get_file('.gitmodules') as f:
426
 
                    config = GitConfigFile.from_file(f)
427
 
                    self._submodules = {
428
 
                        path: (url, section)
429
 
                        for path, url, section in parse_submodules(config)}
430
 
            except errors.NoSuchFile:
431
 
                self._submodules = {}
432
 
        return self._submodules
433
 
 
434
 
    def list_files(self, include_root=False, from_dir=None, recursive=True,
435
 
                   recurse_nested=False):
436
 
        if self.tree is None:
437
 
            return
438
 
        if from_dir is None or from_dir == '.':
439
 
            from_dir = u""
440
 
        (store, mode, hexsha) = self._lookup_path(from_dir)
441
 
        if mode is None:  # Root
442
 
            root_ie = self._get_dir_ie(b"", None)
443
 
        else:
444
 
            parent_path = posixpath.dirname(from_dir)
445
 
            parent_id = self.mapping.generate_file_id(parent_path)
446
 
            if mode_kind(mode) == 'directory':
447
 
                root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
448
 
            else:
449
 
                root_ie = self._get_file_ie(
450
 
                    store, from_dir.encode("utf-8"),
451
 
                    posixpath.basename(from_dir), mode, hexsha)
452
 
        if include_root:
453
 
            yield (from_dir, "V", root_ie.kind, root_ie)
454
 
        todo = []
455
 
        if root_ie.kind == 'directory':
456
 
            todo.append((store, from_dir.encode("utf-8"),
457
 
                         b"", hexsha, root_ie.file_id))
458
 
        while todo:
459
 
            (store, path, relpath, hexsha, parent_id) = todo.pop()
460
 
            tree = store[hexsha]
461
 
            for name, mode, hexsha in tree.iteritems():
462
 
                if self.mapping.is_special_file(name):
463
 
                    continue
464
 
                child_path = posixpath.join(path, name)
465
 
                child_relpath = posixpath.join(relpath, name)
466
 
                if S_ISGITLINK(mode) and recurse_nested:
467
 
                    mode = stat.S_IFDIR
468
 
                    store = self._get_submodule_store(child_relpath)
469
 
                    hexsha = store[hexsha].tree
470
 
                if stat.S_ISDIR(mode):
471
 
                    ie = self._get_dir_ie(child_path, parent_id)
472
 
                    if recursive:
473
 
                        todo.append(
474
 
                            (store, child_path, child_relpath, hexsha,
475
 
                             ie.file_id))
476
 
                else:
477
 
                    ie = self._get_file_ie(
478
 
                        store, child_path, name, mode, hexsha, parent_id)
479
 
                yield (child_relpath.decode('utf-8'), "V", ie.kind, ie)
480
 
 
481
 
    def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
482
 
        if not isinstance(path, bytes):
483
 
            raise TypeError(path)
484
 
        if not isinstance(name, bytes):
485
 
            raise TypeError(name)
486
 
        kind = mode_kind(mode)
487
 
        path = path.decode('utf-8')
488
 
        name = name.decode("utf-8")
489
 
        file_id = self.mapping.generate_file_id(path)
490
 
        ie = entry_factory[kind](file_id, name, parent_id)
491
 
        if kind == 'symlink':
492
 
            ie.symlink_target = store[hexsha].data.decode('utf-8')
493
 
        elif kind == 'tree-reference':
494
 
            ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(
495
 
                hexsha)
496
 
        else:
497
 
            data = store[hexsha].data
498
 
            ie.text_sha1 = osutils.sha_string(data)
499
 
            ie.text_size = len(data)
500
 
            ie.executable = mode_is_executable(mode)
501
 
        return ie
502
 
 
503
 
    def _get_dir_ie(self, path, parent_id):
504
 
        path = path.decode('utf-8')
505
 
        file_id = self.mapping.generate_file_id(path)
506
 
        return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
507
 
 
508
 
    def iter_child_entries(self, path):
509
 
        (store, mode, tree_sha) = self._lookup_path(path)
510
 
 
511
 
        if mode is not None and not stat.S_ISDIR(mode):
512
 
            return
513
 
 
514
 
        encoded_path = path.encode('utf-8')
515
 
        file_id = self.path2id(path)
516
 
        tree = store[tree_sha]
517
 
        for name, mode, hexsha in tree.iteritems():
518
 
            if self.mapping.is_special_file(name):
519
 
                continue
520
 
            child_path = posixpath.join(encoded_path, name)
521
 
            if stat.S_ISDIR(mode):
522
 
                yield self._get_dir_ie(child_path, file_id)
523
 
            else:
524
 
                yield self._get_file_ie(store, child_path, name, mode, hexsha,
525
 
                                        file_id)
526
 
 
527
 
    def iter_entries_by_dir(self, specific_files=None,
528
 
                            recurse_nested=False):
529
 
        if self.tree is None:
530
 
            return
531
 
        if specific_files is not None:
532
 
            if specific_files in ([""], []):
533
 
                specific_files = None
534
 
            else:
535
 
                specific_files = set([p.encode('utf-8')
536
 
                                      for p in specific_files])
537
 
        todo = deque([(self.store, b"", self.tree, self.path2id(''))])
538
 
        if specific_files is None or u"" in specific_files:
539
 
            yield u"", self._get_dir_ie(b"", None)
540
 
        while todo:
541
 
            store, path, tree_sha, parent_id = todo.popleft()
542
 
            tree = store[tree_sha]
543
 
            extradirs = []
544
 
            for name, mode, hexsha in tree.iteritems():
545
 
                if self.mapping.is_special_file(name):
546
 
                    continue
547
 
                child_path = posixpath.join(path, name)
548
 
                child_path_decoded = child_path.decode('utf-8')
549
 
                if recurse_nested and S_ISGITLINK(mode):
550
 
                    mode = stat.S_IFDIR
551
 
                    store = self._get_submodule_store(child_path)
552
 
                    hexsha = store[hexsha].tree
553
 
                if stat.S_ISDIR(mode):
554
 
                    if (specific_files is None or
555
 
                            any([p for p in specific_files if p.startswith(
556
 
                                child_path)])):
557
 
                        extradirs.append(
558
 
                            (store, child_path, hexsha,
559
 
                             self.path2id(child_path_decoded)))
560
 
                if specific_files is None or child_path in specific_files:
561
 
                    if stat.S_ISDIR(mode):
562
 
                        yield (child_path_decoded,
563
 
                               self._get_dir_ie(child_path, parent_id))
564
 
                    else:
565
 
                        yield (child_path_decoded,
566
 
                               self._get_file_ie(store, child_path, name, mode,
567
 
                                                 hexsha, parent_id))
568
 
            todo.extendleft(reversed(extradirs))
569
 
 
570
 
    def iter_references(self):
571
 
        if self.supports_tree_reference():
572
 
            for path, entry in self.iter_entries_by_dir():
573
 
                if entry.kind == 'tree-reference':
574
 
                    yield path
575
 
 
576
 
    def get_revision_id(self):
577
 
        """See RevisionTree.get_revision_id."""
578
 
        return self._revision_id
579
 
 
580
 
    def get_file_sha1(self, path, stat_value=None):
581
 
        if self.tree is None:
582
 
            raise errors.NoSuchFile(path)
583
 
        return osutils.sha_string(self.get_file_text(path))
584
 
 
585
 
    def get_file_verifier(self, path, stat_value=None):
586
 
        (store, mode, hexsha) = self._lookup_path(path)
587
 
        return ("GIT", hexsha)
588
 
 
589
 
    def get_file_size(self, path):
590
 
        (store, mode, hexsha) = self._lookup_path(path)
591
 
        if stat.S_ISREG(mode):
592
 
            return len(store[hexsha].data)
593
 
        return None
594
 
 
595
 
    def get_file_text(self, path):
596
 
        """See RevisionTree.get_file_text."""
597
 
        (store, mode, hexsha) = self._lookup_path(path)
598
 
        if stat.S_ISREG(mode):
599
 
            return store[hexsha].data
600
 
        else:
601
 
            return b""
602
 
 
603
 
    def get_symlink_target(self, path):
604
 
        """See RevisionTree.get_symlink_target."""
605
 
        (store, mode, hexsha) = self._lookup_path(path)
606
 
        if stat.S_ISLNK(mode):
607
 
            return store[hexsha].data.decode('utf-8')
608
 
        else:
609
 
            return None
610
 
 
611
 
    def get_reference_revision(self, path):
612
 
        """See RevisionTree.get_symlink_target."""
613
 
        (store, mode, hexsha) = self._lookup_path(path)
614
 
        if S_ISGITLINK(mode):
615
 
            try:
616
 
                nested_repo = self._get_submodule_repository(path.encode('utf-8'))
617
 
            except errors.NotBranchError:
618
 
                return self.mapping.revision_id_foreign_to_bzr(hexsha)
619
 
            else:
620
 
                return nested_repo.lookup_foreign_revision_id(hexsha)
621
 
        else:
622
 
            return None
623
 
 
624
 
    def _comparison_data(self, entry, path):
625
 
        if entry is None:
626
 
            return None, False, None
627
 
        return entry.kind, entry.executable, None
628
 
 
629
 
    def path_content_summary(self, path):
630
 
        """See Tree.path_content_summary."""
631
 
        try:
632
 
            (store, mode, hexsha) = self._lookup_path(path)
633
 
        except errors.NoSuchFile:
634
 
            return ('missing', None, None, None)
635
 
        kind = mode_kind(mode)
636
 
        if kind == 'file':
637
 
            executable = mode_is_executable(mode)
638
 
            contents = store[hexsha].data
639
 
            return (kind, len(contents), executable,
640
 
                    osutils.sha_string(contents))
641
 
        elif kind == 'symlink':
642
 
            return (kind, None, None, store[hexsha].data.decode('utf-8'))
643
 
        elif kind == 'tree-reference':
644
 
            nested_repo = self._get_submodule_repository(path.encode('utf-8'))
645
 
            return (kind, None, None,
646
 
                    nested_repo.lookup_foreign_revision_id(hexsha))
647
 
        else:
648
 
            return (kind, None, None, None)
649
 
 
650
 
    def find_related_paths_across_trees(self, paths, trees=[],
651
 
                                        require_versioned=True):
652
 
        if paths is None:
653
 
            return None
654
 
        if require_versioned:
655
 
            trees = [self] + (trees if trees is not None else [])
656
 
            unversioned = set()
657
 
            for p in paths:
658
 
                for t in trees:
659
 
                    if t.is_versioned(p):
660
 
                        break
661
 
                else:
662
 
                    unversioned.add(p)
663
 
            if unversioned:
664
 
                raise errors.PathsNotVersionedError(unversioned)
665
 
        return filter(self.is_versioned, paths)
666
 
 
667
 
    def _iter_tree_contents(self, include_trees=False):
668
 
        if self.tree is None:
669
 
            return iter([])
670
 
        return self.store.iter_tree_contents(
671
 
            self.tree, include_trees=include_trees)
672
 
 
673
 
    def annotate_iter(self, path, default_revision=CURRENT_REVISION):
674
 
        """Return an iterator of revision_id, line tuples.
675
 
 
676
 
        For working trees (and mutable trees in general), the special
677
 
        revision_id 'current:' will be used for lines that are new in this
678
 
        tree, e.g. uncommitted changes.
679
 
        :param default_revision: For lines that don't match a basis, mark them
680
 
            with this revision id. Not all implementations will make use of
681
 
            this value.
682
 
        """
683
 
        with self.lock_read():
684
 
            # Now we have the parents of this content
685
 
            from breezy.annotate import Annotator
686
 
            from .annotate import AnnotateProvider
687
 
            annotator = Annotator(AnnotateProvider(
688
 
                self._repository._file_change_scanner))
689
 
            this_key = (path, self.get_file_revision(path))
690
 
            annotations = [(key[-1], line)
691
 
                           for key, line in annotator.annotate_flat(this_key)]
692
 
            return annotations
693
 
 
694
 
    def _get_rules_searcher(self, default_searcher):
695
 
        return default_searcher
696
 
 
697
 
    def walkdirs(self, prefix=u""):
698
 
        (store, mode, hexsha) = self._lookup_path(prefix)
699
 
        todo = deque(
700
 
            [(store, prefix.encode('utf-8'), hexsha, self.path2id(prefix))])
701
 
        while todo:
702
 
            store, path, tree_sha, parent_id = todo.popleft()
703
 
            path_decoded = path.decode('utf-8')
704
 
            tree = store[tree_sha]
705
 
            children = []
706
 
            for name, mode, hexsha in tree.iteritems():
707
 
                if self.mapping.is_special_file(name):
708
 
                    continue
709
 
                child_path = posixpath.join(path, name)
710
 
                file_id = self.path2id(child_path.decode('utf-8'))
711
 
                if stat.S_ISDIR(mode):
712
 
                    todo.append((store, child_path, hexsha, file_id))
713
 
                children.append(
714
 
                    (child_path.decode('utf-8'), name.decode('utf-8'),
715
 
                        mode_kind(mode), None,
716
 
                        file_id, mode_kind(mode)))
717
 
            yield (path_decoded, parent_id), children
718
 
 
719
 
 
720
 
def tree_delta_from_git_changes(changes, mappings,
721
 
                                specific_files=None,
722
 
                                require_versioned=False, include_root=False,
723
 
                                target_extras=None):
724
 
    """Create a TreeDelta from two git trees.
725
 
 
726
 
    source and target are iterators over tuples with:
727
 
        (filename, sha, mode)
728
 
    """
729
 
    (old_mapping, new_mapping) = mappings
730
 
    if target_extras is None:
731
 
        target_extras = set()
732
 
    ret = delta.TreeDelta()
733
 
    added = []
734
 
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
735
 
        if newpath == b'' and not include_root:
736
 
            continue
737
 
        if oldpath is not None:
738
 
            oldpath_decoded = oldpath.decode('utf-8')
739
 
        else:
740
 
            oldpath_decoded = None
741
 
        if newpath is not None:
742
 
            newpath_decoded = newpath.decode('utf-8')
743
 
        else:
744
 
            newpath_decoded = None
745
 
        if not (specific_files is None or
746
 
                (oldpath is not None and
747
 
                    osutils.is_inside_or_parent_of_any(
748
 
                        specific_files, oldpath_decoded)) or
749
 
                (newpath is not None and
750
 
                    osutils.is_inside_or_parent_of_any(
751
 
                        specific_files, newpath_decoded))):
752
 
            continue
753
 
 
754
 
        if oldpath_decoded is None:
755
 
            fileid = new_mapping.generate_file_id(newpath_decoded)
756
 
            oldexe = None
757
 
            oldkind = None
758
 
            oldname = None
759
 
            oldparent = None
760
 
            oldversioned = False
761
 
        else:
762
 
            oldversioned = True
763
 
            if oldmode:
764
 
                oldexe = mode_is_executable(oldmode)
765
 
                oldkind = mode_kind(oldmode)
766
 
            else:
767
 
                oldexe = False
768
 
                oldkind = None
769
 
            if oldpath_decoded == u'':
770
 
                oldparent = None
771
 
                oldname = u''
772
 
            else:
773
 
                (oldparentpath, oldname) = osutils.split(oldpath_decoded)
774
 
                oldparent = old_mapping.generate_file_id(oldparentpath)
775
 
            fileid = old_mapping.generate_file_id(oldpath_decoded)
776
 
        if newpath_decoded is None:
777
 
            newexe = None
778
 
            newkind = None
779
 
            newname = None
780
 
            newparent = None
781
 
            newversioned = False
782
 
        else:
783
 
            newversioned = (newpath_decoded not in target_extras)
784
 
            if newmode:
785
 
                newexe = mode_is_executable(newmode)
786
 
                newkind = mode_kind(newmode)
787
 
            else:
788
 
                newexe = False
789
 
                newkind = None
790
 
            if newpath_decoded == u'':
791
 
                newparent = None
792
 
                newname = u''
793
 
            else:
794
 
                newparentpath, newname = osutils.split(newpath_decoded)
795
 
                newparent = new_mapping.generate_file_id(newparentpath)
796
 
        if old_mapping.is_special_file(oldpath):
797
 
            oldpath = None
798
 
        if new_mapping.is_special_file(newpath):
799
 
            newpath = None
800
 
        if oldpath is None and newpath is None:
801
 
            continue
802
 
        change = _mod_tree.TreeChange(
803
 
            fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
804
 
            (oldversioned, newversioned),
805
 
            (oldparent, newparent), (oldname, newname),
806
 
            (oldkind, newkind), (oldexe, newexe))
807
 
        if oldpath is None:
808
 
            added.append((newpath, newkind))
809
 
        elif newpath is None or newmode == 0:
810
 
            ret.removed.append(change)
811
 
        elif oldpath != newpath:
812
 
            ret.renamed.append(change)
813
 
        elif mode_kind(oldmode) != mode_kind(newmode):
814
 
            ret.kind_changed.append(change)
815
 
        elif oldsha != newsha or oldmode != newmode:
816
 
            if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
817
 
                continue
818
 
            ret.modified.append(change)
819
 
        else:
820
 
            ret.unchanged.append(change)
821
 
 
822
 
    implicit_dirs = {b''}
823
 
    for path, kind in added:
824
 
        if kind == 'directory' or path in target_extras:
825
 
            continue
826
 
        implicit_dirs.update(osutils.parent_directories(path))
827
 
 
828
 
    for path, kind in added:
829
 
        if kind == 'directory' and path not in implicit_dirs:
830
 
            continue
831
 
        path_decoded = osutils.normalized_filename(path)[0]
832
 
        parent_path, basename = osutils.split(path_decoded)
833
 
        parent_id = new_mapping.generate_file_id(parent_path)
834
 
        if path in target_extras:
835
 
            ret.unversioned.append(_mod_tree.TreeChange(
836
 
                None, (None, path_decoded),
837
 
                True, (False, False), (None, parent_id),
838
 
                (None, basename), (None, kind), (None, False)))
839
 
        else:
840
 
            file_id = new_mapping.generate_file_id(path_decoded)
841
 
            ret.added.append(
842
 
                _mod_tree.TreeChange(
843
 
                    file_id, (None, path_decoded), True,
844
 
                    (False, True),
845
 
                    (None, parent_id),
846
 
                    (None, basename), (None, kind), (None, False)))
847
 
 
848
 
    return ret
849
 
 
850
 
 
851
 
def changes_from_git_changes(changes, mapping, specific_files=None,
852
 
                             include_unchanged=False, target_extras=None):
853
 
    """Create a iter_changes-like generator from a git stream.
854
 
 
855
 
    source and target are iterators over tuples with:
856
 
        (filename, sha, mode)
857
 
    """
858
 
    if target_extras is None:
859
 
        target_extras = set()
860
 
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
861
 
        if oldpath is not None:
862
 
            oldpath_decoded = oldpath.decode('utf-8')
863
 
        else:
864
 
            oldpath_decoded = None
865
 
        if newpath is not None:
866
 
            newpath_decoded = newpath.decode('utf-8')
867
 
        else:
868
 
            newpath_decoded = None
869
 
        if not (specific_files is None or
870
 
                (oldpath_decoded is not None and
871
 
                    osutils.is_inside_or_parent_of_any(
872
 
                        specific_files, oldpath_decoded)) or
873
 
                (newpath_decoded is not None and
874
 
                    osutils.is_inside_or_parent_of_any(
875
 
                        specific_files, newpath_decoded))):
876
 
            continue
877
 
        if oldpath is not None and mapping.is_special_file(oldpath):
878
 
            continue
879
 
        if newpath is not None and mapping.is_special_file(newpath):
880
 
            continue
881
 
        if oldpath_decoded is None:
882
 
            fileid = mapping.generate_file_id(newpath_decoded)
883
 
            oldexe = None
884
 
            oldkind = None
885
 
            oldname = None
886
 
            oldparent = None
887
 
            oldversioned = False
888
 
        else:
889
 
            oldversioned = True
890
 
            if oldmode:
891
 
                oldexe = mode_is_executable(oldmode)
892
 
                oldkind = mode_kind(oldmode)
893
 
            else:
894
 
                oldexe = False
895
 
                oldkind = None
896
 
            if oldpath_decoded == u'':
897
 
                oldparent = None
898
 
                oldname = u''
899
 
            else:
900
 
                (oldparentpath, oldname) = osutils.split(oldpath_decoded)
901
 
                oldparent = mapping.generate_file_id(oldparentpath)
902
 
            fileid = mapping.generate_file_id(oldpath_decoded)
903
 
        if newpath_decoded is None:
904
 
            newexe = None
905
 
            newkind = None
906
 
            newname = None
907
 
            newparent = None
908
 
            newversioned = False
909
 
        else:
910
 
            newversioned = (newpath_decoded not in target_extras)
911
 
            if newmode:
912
 
                newexe = mode_is_executable(newmode)
913
 
                newkind = mode_kind(newmode)
914
 
            else:
915
 
                newexe = False
916
 
                newkind = None
917
 
            if newpath_decoded == u'':
918
 
                newparent = None
919
 
                newname = u''
920
 
            else:
921
 
                newparentpath, newname = osutils.split(newpath_decoded)
922
 
                newparent = mapping.generate_file_id(newparentpath)
923
 
        if (not include_unchanged and
924
 
            oldkind == 'directory' and newkind == 'directory' and
925
 
                oldpath_decoded == newpath_decoded):
926
 
            continue
927
 
        yield _mod_tree.TreeChange(
928
 
            fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
929
 
            (oldversioned, newversioned),
930
 
            (oldparent, newparent), (oldname, newname),
931
 
            (oldkind, newkind), (oldexe, newexe))
932
 
 
933
 
 
934
 
class InterGitTrees(_mod_tree.InterTree):
935
 
    """InterTree that works between two git trees."""
936
 
 
937
 
    _matching_from_tree_format = None
938
 
    _matching_to_tree_format = None
939
 
    _test_mutable_trees_to_test_trees = None
940
 
 
941
 
    @classmethod
942
 
    def is_compatible(cls, source, target):
943
 
        return (isinstance(source, GitRevisionTree) and
944
 
                isinstance(target, GitRevisionTree))
945
 
 
946
 
    def compare(self, want_unchanged=False, specific_files=None,
947
 
                extra_trees=None, require_versioned=False, include_root=False,
948
 
                want_unversioned=False):
949
 
        with self.lock_read():
950
 
            changes, target_extras = self._iter_git_changes(
951
 
                want_unchanged=want_unchanged,
952
 
                require_versioned=require_versioned,
953
 
                specific_files=specific_files,
954
 
                extra_trees=extra_trees,
955
 
                want_unversioned=want_unversioned)
956
 
            return tree_delta_from_git_changes(
957
 
                changes, (self.source.mapping, self.target.mapping),
958
 
                specific_files=specific_files,
959
 
                include_root=include_root, target_extras=target_extras)
960
 
 
961
 
    def iter_changes(self, include_unchanged=False, specific_files=None,
962
 
                     pb=None, extra_trees=[], require_versioned=True,
963
 
                     want_unversioned=False):
964
 
        with self.lock_read():
965
 
            changes, target_extras = self._iter_git_changes(
966
 
                want_unchanged=include_unchanged,
967
 
                require_versioned=require_versioned,
968
 
                specific_files=specific_files,
969
 
                extra_trees=extra_trees,
970
 
                want_unversioned=want_unversioned)
971
 
            return changes_from_git_changes(
972
 
                changes, self.target.mapping,
973
 
                specific_files=specific_files,
974
 
                include_unchanged=include_unchanged,
975
 
                target_extras=target_extras)
976
 
 
977
 
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
978
 
                          require_versioned=False, extra_trees=None,
979
 
                          want_unversioned=False):
980
 
        raise NotImplementedError(self._iter_git_changes)
981
 
 
982
 
    def find_target_path(self, path, recurse='none'):
983
 
        ret = self.find_target_paths([path], recurse=recurse)
984
 
        return ret[path]
985
 
 
986
 
    def find_source_path(self, path, recurse='none'):
987
 
        ret = self.find_source_paths([path], recurse=recurse)
988
 
        return ret[path]
989
 
 
990
 
    def find_target_paths(self, paths, recurse='none'):
991
 
        paths = set(paths)
992
 
        ret = {}
993
 
        changes = self._iter_git_changes(specific_files=paths)[0]
994
 
        for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
995
 
            if oldpath in paths:
996
 
                ret[oldpath] = newpath
997
 
        for path in paths:
998
 
            if path not in ret:
999
 
                if self.source.has_filename(path):
1000
 
                    if self.target.has_filename(path):
1001
 
                        ret[path] = path
1002
 
                    else:
1003
 
                        ret[path] = None
1004
 
                else:
1005
 
                    raise errors.NoSuchFile(path)
1006
 
        return ret
1007
 
 
1008
 
    def find_source_paths(self, paths, recurse='none'):
1009
 
        paths = set(paths)
1010
 
        ret = {}
1011
 
        changes = self._iter_git_changes(specific_files=paths)[0]
1012
 
        for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
1013
 
            if newpath in paths:
1014
 
                ret[newpath] = oldpath
1015
 
        for path in paths:
1016
 
            if path not in ret:
1017
 
                if self.target.has_filename(path):
1018
 
                    if self.source.has_filename(path):
1019
 
                        ret[path] = path
1020
 
                    else:
1021
 
                        ret[path] = None
1022
 
                else:
1023
 
                    raise errors.NoSuchFile(path)
1024
 
        return ret
1025
 
 
1026
 
 
1027
 
class InterGitRevisionTrees(InterGitTrees):
1028
 
    """InterTree that works between two git revision trees."""
1029
 
 
1030
 
    _matching_from_tree_format = None
1031
 
    _matching_to_tree_format = None
1032
 
    _test_mutable_trees_to_test_trees = None
1033
 
 
1034
 
    @classmethod
1035
 
    def is_compatible(cls, source, target):
1036
 
        return (isinstance(source, GitRevisionTree) and
1037
 
                isinstance(target, GitRevisionTree))
1038
 
 
1039
 
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1040
 
                          require_versioned=True, extra_trees=None,
1041
 
                          want_unversioned=False):
1042
 
        trees = [self.source]
1043
 
        if extra_trees is not None:
1044
 
            trees.extend(extra_trees)
1045
 
        if specific_files is not None:
1046
 
            specific_files = self.target.find_related_paths_across_trees(
1047
 
                specific_files, trees,
1048
 
                require_versioned=require_versioned)
1049
 
 
1050
 
        if (self.source._repository._git.object_store !=
1051
 
                self.target._repository._git.object_store):
1052
 
            store = OverlayObjectStore(
1053
 
                [self.source._repository._git.object_store,
1054
 
                    self.target._repository._git.object_store])
1055
 
        else:
1056
 
            store = self.source._repository._git.object_store
1057
 
        return store.tree_changes(
1058
 
            self.source.tree, self.target.tree, want_unchanged=want_unchanged,
1059
 
            include_trees=True, change_type_same=True), set()
1060
 
 
1061
 
 
1062
 
_mod_tree.InterTree.register_optimiser(InterGitRevisionTrees)
1063
 
 
1064
 
 
1065
 
class MutableGitIndexTree(mutabletree.MutableTree):
1066
 
 
1067
 
    def __init__(self):
1068
 
        self._lock_mode = None
1069
 
        self._lock_count = 0
1070
 
        self._versioned_dirs = None
1071
 
        self._index_dirty = False
1072
 
        self._submodules = None
1073
 
 
1074
 
    def is_versioned(self, path):
1075
 
        with self.lock_read():
1076
 
            path = path.rstrip('/').encode('utf-8')
1077
 
            (index, subpath) = self._lookup_index(path)
1078
 
            return (subpath in index or self._has_dir(path))
1079
 
 
1080
 
    def _has_dir(self, path):
1081
 
        if not isinstance(path, bytes):
1082
 
            raise TypeError(path)
1083
 
        if path == b"":
1084
 
            return True
1085
 
        if self._versioned_dirs is None:
1086
 
            self._load_dirs()
1087
 
        return path in self._versioned_dirs
1088
 
 
1089
 
    def _load_dirs(self):
1090
 
        if self._lock_mode is None:
1091
 
            raise errors.ObjectNotLocked(self)
1092
 
        self._versioned_dirs = set()
1093
 
        for p, i in self._recurse_index_entries():
1094
 
            self._ensure_versioned_dir(posixpath.dirname(p))
1095
 
 
1096
 
    def _ensure_versioned_dir(self, dirname):
1097
 
        if not isinstance(dirname, bytes):
1098
 
            raise TypeError(dirname)
1099
 
        if dirname in self._versioned_dirs:
1100
 
            return
1101
 
        if dirname != b"":
1102
 
            self._ensure_versioned_dir(posixpath.dirname(dirname))
1103
 
        self._versioned_dirs.add(dirname)
1104
 
 
1105
 
    def path2id(self, path):
1106
 
        with self.lock_read():
1107
 
            path = path.rstrip('/')
1108
 
            if self.is_versioned(path.rstrip('/')):
1109
 
                return self.mapping.generate_file_id(
1110
 
                    osutils.safe_unicode(path))
1111
 
            return None
1112
 
 
1113
 
    def id2path(self, file_id, recurse='down'):
1114
 
        if file_id is None:
1115
 
            return ''
1116
 
        if type(file_id) is not bytes:
1117
 
            raise TypeError(file_id)
1118
 
        with self.lock_read():
1119
 
            try:
1120
 
                path = self.mapping.parse_file_id(file_id)
1121
 
            except ValueError:
1122
 
                raise errors.NoSuchId(self, file_id)
1123
 
            if self.is_versioned(path):
1124
 
                return path
1125
 
            raise errors.NoSuchId(self, file_id)
1126
 
 
1127
 
    def _set_root_id(self, file_id):
1128
 
        raise errors.UnsupportedOperation(self._set_root_id, self)
1129
 
 
1130
 
    def _add(self, files, ids, kinds):
1131
 
        for (path, file_id, kind) in zip(files, ids, kinds):
1132
 
            if file_id is not None:
1133
 
                raise workingtree.SettingFileIdUnsupported()
1134
 
            path, can_access = osutils.normalized_filename(path)
1135
 
            if not can_access:
1136
 
                raise errors.InvalidNormalization(path)
1137
 
            self._index_add_entry(path, kind)
1138
 
 
1139
 
    def _read_submodule_head(self, path):
1140
 
        raise NotImplementedError(self._read_submodule_head)
1141
 
 
1142
 
    def _submodule_info(self):
1143
 
        if self._submodules is None:
1144
 
            try:
1145
 
                with self.get_file('.gitmodules') as f:
1146
 
                    config = GitConfigFile.from_file(f)
1147
 
                    self._submodules = {
1148
 
                        path: (url, section)
1149
 
                        for path, url, section in parse_submodules(config)}
1150
 
            except errors.NoSuchFile:
1151
 
                self._submodules = {}
1152
 
        return self._submodules
1153
 
 
1154
 
    def _lookup_index(self, encoded_path):
1155
 
        if not isinstance(encoded_path, bytes):
1156
 
            raise TypeError(encoded_path)
1157
 
        # Common case:
1158
 
        if encoded_path in self.index:
1159
 
            return self.index, encoded_path
1160
 
        # TODO(jelmer): Perhaps have a cache with paths under which some
1161
 
        # submodules exist?
1162
 
        index = self.index
1163
 
        remaining_path = encoded_path
1164
 
        while True:
1165
 
            parts = remaining_path.split(b'/')
1166
 
            for i in range(1, len(parts)):
1167
 
                basepath = b'/'.join(parts[:i])
1168
 
                try:
1169
 
                    (ctime, mtime, dev, ino, mode, uid, gid, size, sha,
1170
 
                     flags) = index[basepath]
1171
 
                except KeyError:
1172
 
                    continue
1173
 
                else:
1174
 
                    if S_ISGITLINK(mode):
1175
 
                        index = self._get_submodule_index(basepath)
1176
 
                        remaining_path = b'/'.join(parts[i:])
1177
 
                        break
1178
 
                    else:
1179
 
                        return index, remaining_path
1180
 
            else:
1181
 
                return index, remaining_path
1182
 
        return index, remaining_path
1183
 
 
1184
 
    def _index_del_entry(self, index, path):
1185
 
        del index[path]
1186
 
        # TODO(jelmer): Keep track of dirty per index
1187
 
        self._index_dirty = True
1188
 
 
1189
 
    def _index_add_entry(self, path, kind, flags=0, reference_revision=None):
1190
 
        if kind == "directory":
1191
 
            # Git indexes don't contain directories
1192
 
            return
1193
 
        if kind == "file":
1194
 
            blob = Blob()
1195
 
            try:
1196
 
                file, stat_val = self.get_file_with_stat(path)
1197
 
            except (errors.NoSuchFile, IOError):
1198
 
                # TODO: Rather than come up with something here, use the old
1199
 
                # index
1200
 
                file = BytesIO()
1201
 
                stat_val = os.stat_result(
1202
 
                    (stat.S_IFREG | 0o644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1203
 
            with file:
1204
 
                blob.set_raw_string(file.read())
1205
 
            # Add object to the repository if it didn't exist yet
1206
 
            if blob.id not in self.store:
1207
 
                self.store.add_object(blob)
1208
 
            hexsha = blob.id
1209
 
        elif kind == "symlink":
1210
 
            blob = Blob()
1211
 
            try:
1212
 
                stat_val = self._lstat(path)
1213
 
            except EnvironmentError:
1214
 
                # TODO: Rather than come up with something here, use the
1215
 
                # old index
1216
 
                stat_val = os.stat_result(
1217
 
                    (stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1218
 
            blob.set_raw_string(
1219
 
                self.get_symlink_target(path).encode("utf-8"))
1220
 
            # Add object to the repository if it didn't exist yet
1221
 
            if blob.id not in self.store:
1222
 
                self.store.add_object(blob)
1223
 
            hexsha = blob.id
1224
 
        elif kind == "tree-reference":
1225
 
            if reference_revision is not None:
1226
 
                hexsha = self.branch.lookup_bzr_revision_id(
1227
 
                    reference_revision)[0]
1228
 
            else:
1229
 
                hexsha = self._read_submodule_head(path)
1230
 
                if hexsha is None:
1231
 
                    raise errors.NoCommits(path)
1232
 
            try:
1233
 
                stat_val = self._lstat(path)
1234
 
            except EnvironmentError:
1235
 
                stat_val = os.stat_result(
1236
 
                    (S_IFGITLINK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1237
 
            stat_val = os.stat_result((S_IFGITLINK, ) + stat_val[1:])
1238
 
        else:
1239
 
            raise AssertionError("unknown kind '%s'" % kind)
1240
 
        # Add an entry to the index or update the existing entry
1241
 
        ensure_normalized_path(path)
1242
 
        encoded_path = path.encode("utf-8")
1243
 
        if b'\r' in encoded_path or b'\n' in encoded_path:
1244
 
            # TODO(jelmer): Why do we need to do this?
1245
 
            trace.mutter('ignoring path with invalid newline in it: %r', path)
1246
 
            return
1247
 
        (index, index_path) = self._lookup_index(encoded_path)
1248
 
        index[index_path] = index_entry_from_stat(stat_val, hexsha, flags)
1249
 
        self._index_dirty = True
1250
 
        if self._versioned_dirs is not None:
1251
 
            self._ensure_versioned_dir(index_path)
1252
 
 
1253
 
    def _recurse_index_entries(self, index=None, basepath=b"",
1254
 
                               recurse_nested=False):
1255
 
        # Iterate over all index entries
1256
 
        with self.lock_read():
1257
 
            if index is None:
1258
 
                index = self.index
1259
 
            for path, value in index.items():
1260
 
                (ctime, mtime, dev, ino, mode, uid, gid, size, sha,
1261
 
                 flags) = value
1262
 
                if S_ISGITLINK(mode) and recurse_nested:
1263
 
                    subindex = self._get_submodule_index(path)
1264
 
                    for entry in self._recurse_index_entries(
1265
 
                            index=subindex, basepath=path,
1266
 
                            recurse_nested=recurse_nested):
1267
 
                        yield entry
1268
 
                else:
1269
 
                    yield (posixpath.join(basepath, path), value)
1270
 
 
1271
 
    def iter_entries_by_dir(self, specific_files=None,
1272
 
                            recurse_nested=False):
1273
 
        with self.lock_read():
1274
 
            if specific_files is not None:
1275
 
                specific_files = set(specific_files)
1276
 
            else:
1277
 
                specific_files = None
1278
 
            root_ie = self._get_dir_ie(u"", None)
1279
 
            ret = {}
1280
 
            if specific_files is None or u"" in specific_files:
1281
 
                ret[(u"", u"")] = root_ie
1282
 
            dir_ids = {u"": root_ie.file_id}
1283
 
            for path, value in self._recurse_index_entries(
1284
 
                    recurse_nested=recurse_nested):
1285
 
                if self.mapping.is_special_file(path):
1286
 
                    continue
1287
 
                path = path.decode("utf-8")
1288
 
                if specific_files is not None and path not in specific_files:
1289
 
                    continue
1290
 
                (parent, name) = posixpath.split(path)
1291
 
                try:
1292
 
                    file_ie = self._get_file_ie(name, path, value, None)
1293
 
                except errors.NoSuchFile:
1294
 
                    continue
1295
 
                if specific_files is None:
1296
 
                    for (dir_path, dir_ie) in self._add_missing_parent_ids(
1297
 
                            parent, dir_ids):
1298
 
                        ret[(posixpath.dirname(dir_path), dir_path)] = dir_ie
1299
 
                file_ie.parent_id = self.path2id(parent)
1300
 
                ret[(posixpath.dirname(path), path)] = file_ie
1301
 
            # Special casing for directories
1302
 
            if specific_files:
1303
 
                for path in specific_files:
1304
 
                    key = (posixpath.dirname(path), path)
1305
 
                    if key not in ret and self.is_versioned(path):
1306
 
                        ret[key] = self._get_dir_ie(path, self.path2id(key[0]))
1307
 
            return ((path, ie) for ((_, path), ie) in sorted(ret.items()))
1308
 
 
1309
 
    def iter_references(self):
1310
 
        if self.supports_tree_reference():
1311
 
            # TODO(jelmer): Implement a more efficient version of this
1312
 
            for path, entry in self.iter_entries_by_dir():
1313
 
                if entry.kind == 'tree-reference':
1314
 
                    yield path
1315
 
 
1316
 
    def _get_dir_ie(self, path, parent_id):
1317
 
        file_id = self.path2id(path)
1318
 
        return GitTreeDirectory(file_id,
1319
 
                                posixpath.basename(path).strip("/"), parent_id)
1320
 
 
1321
 
    def _get_file_ie(self, name, path, value, parent_id):
1322
 
        if not isinstance(name, str):
1323
 
            raise TypeError(name)
1324
 
        if not isinstance(path, str):
1325
 
            raise TypeError(path)
1326
 
        if not isinstance(value, tuple) or len(value) != 10:
1327
 
            raise TypeError(value)
1328
 
        (ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
1329
 
        file_id = self.path2id(path)
1330
 
        if not isinstance(file_id, bytes):
1331
 
            raise TypeError(file_id)
1332
 
        kind = mode_kind(mode)
1333
 
        ie = entry_factory[kind](file_id, name, parent_id)
1334
 
        if kind == 'symlink':
1335
 
            ie.symlink_target = self.get_symlink_target(path)
1336
 
        elif kind == 'tree-reference':
1337
 
            ie.reference_revision = self.get_reference_revision(path)
1338
 
        else:
1339
 
            try:
1340
 
                data = self.get_file_text(path)
1341
 
            except errors.NoSuchFile:
1342
 
                data = None
1343
 
            except IOError as e:
1344
 
                if e.errno != errno.ENOENT:
1345
 
                    raise
1346
 
                data = None
1347
 
            if data is None:
1348
 
                data = self.branch.repository._git.object_store[sha].data
1349
 
            ie.text_sha1 = osutils.sha_string(data)
1350
 
            ie.text_size = len(data)
1351
 
            ie.executable = bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
1352
 
        return ie
1353
 
 
1354
 
    def _add_missing_parent_ids(self, path, dir_ids):
1355
 
        if path in dir_ids:
1356
 
            return []
1357
 
        parent = posixpath.dirname(path).strip("/")
1358
 
        ret = self._add_missing_parent_ids(parent, dir_ids)
1359
 
        parent_id = dir_ids[parent]
1360
 
        ie = self._get_dir_ie(path, parent_id)
1361
 
        dir_ids[path] = ie.file_id
1362
 
        ret.append((path, ie))
1363
 
        return ret
1364
 
 
1365
 
    def _comparison_data(self, entry, path):
1366
 
        if entry is None:
1367
 
            return None, False, None
1368
 
        return entry.kind, entry.executable, None
1369
 
 
1370
 
    def _unversion_path(self, path):
1371
 
        if self._lock_mode is None:
1372
 
            raise errors.ObjectNotLocked(self)
1373
 
        encoded_path = path.encode("utf-8")
1374
 
        count = 0
1375
 
        (index, subpath) = self._lookup_index(encoded_path)
1376
 
        try:
1377
 
            self._index_del_entry(index, encoded_path)
1378
 
        except KeyError:
1379
 
            # A directory, perhaps?
1380
 
            # TODO(jelmer): Deletes that involve submodules?
1381
 
            for p in list(index):
1382
 
                if p.startswith(subpath + b"/"):
1383
 
                    count += 1
1384
 
                    self._index_del_entry(index, p)
1385
 
        else:
1386
 
            count = 1
1387
 
        self._versioned_dirs = None
1388
 
        return count
1389
 
 
1390
 
    def unversion(self, paths):
1391
 
        with self.lock_tree_write():
1392
 
            for path in paths:
1393
 
                if self._unversion_path(path) == 0:
1394
 
                    raise errors.NoSuchFile(path)
1395
 
            self._versioned_dirs = None
1396
 
            self.flush()
1397
 
 
1398
 
    def flush(self):
1399
 
        pass
1400
 
 
1401
 
    def update_basis_by_delta(self, revid, delta):
1402
 
        # TODO(jelmer): This shouldn't be called, it's inventory specific.
1403
 
        for (old_path, new_path, file_id, ie) in delta:
1404
 
            if old_path is not None:
1405
 
                (index, old_subpath) = self._lookup_index(
1406
 
                    old_path.encode('utf-8'))
1407
 
                if old_subpath in index:
1408
 
                    self._index_del_entry(index, old_subpath)
1409
 
                    self._versioned_dirs = None
1410
 
            if new_path is not None and ie.kind != 'directory':
1411
 
                self._index_add_entry(new_path, ie.kind)
1412
 
        self.flush()
1413
 
        self._set_merges_from_parent_ids([])
1414
 
 
1415
 
    def move(self, from_paths, to_dir=None, after=None):
1416
 
        rename_tuples = []
1417
 
        with self.lock_tree_write():
1418
 
            to_abs = self.abspath(to_dir)
1419
 
            if not os.path.isdir(to_abs):
1420
 
                raise errors.BzrMoveFailedError('', to_dir,
1421
 
                                                errors.NotADirectory(to_abs))
1422
 
 
1423
 
            for from_rel in from_paths:
1424
 
                from_tail = os.path.split(from_rel)[-1]
1425
 
                to_rel = os.path.join(to_dir, from_tail)
1426
 
                self.rename_one(from_rel, to_rel, after=after)
1427
 
                rename_tuples.append((from_rel, to_rel))
1428
 
            self.flush()
1429
 
            return rename_tuples
1430
 
 
1431
 
    def rename_one(self, from_rel, to_rel, after=None):
1432
 
        from_path = from_rel.encode("utf-8")
1433
 
        to_rel, can_access = osutils.normalized_filename(to_rel)
1434
 
        if not can_access:
1435
 
            raise errors.InvalidNormalization(to_rel)
1436
 
        to_path = to_rel.encode("utf-8")
1437
 
        with self.lock_tree_write():
1438
 
            if not after:
1439
 
                # Perhaps it's already moved?
1440
 
                after = (
1441
 
                    not self.has_filename(from_rel) and
1442
 
                    self.has_filename(to_rel) and
1443
 
                    not self.is_versioned(to_rel))
1444
 
            if after:
1445
 
                if not self.has_filename(to_rel):
1446
 
                    raise errors.BzrMoveFailedError(
1447
 
                        from_rel, to_rel, errors.NoSuchFile(to_rel))
1448
 
                if self.basis_tree().is_versioned(to_rel):
1449
 
                    raise errors.BzrMoveFailedError(
1450
 
                        from_rel, to_rel, errors.AlreadyVersionedError(to_rel))
1451
 
 
1452
 
                kind = self.kind(to_rel)
1453
 
            else:
1454
 
                try:
1455
 
                    to_kind = self.kind(to_rel)
1456
 
                except errors.NoSuchFile:
1457
 
                    exc_type = errors.BzrRenameFailedError
1458
 
                    to_kind = None
1459
 
                else:
1460
 
                    exc_type = errors.BzrMoveFailedError
1461
 
                if self.is_versioned(to_rel):
1462
 
                    raise exc_type(from_rel, to_rel,
1463
 
                                   errors.AlreadyVersionedError(to_rel))
1464
 
                if not self.has_filename(from_rel):
1465
 
                    raise errors.BzrMoveFailedError(
1466
 
                        from_rel, to_rel, errors.NoSuchFile(from_rel))
1467
 
                kind = self.kind(from_rel)
1468
 
                if not self.is_versioned(from_rel) and kind != 'directory':
1469
 
                    raise exc_type(from_rel, to_rel,
1470
 
                                   errors.NotVersionedError(from_rel))
1471
 
                if self.has_filename(to_rel):
1472
 
                    raise errors.RenameFailedFilesExist(
1473
 
                        from_rel, to_rel, errors.FileExists(to_rel))
1474
 
 
1475
 
                kind = self.kind(from_rel)
1476
 
 
1477
 
            if not after and kind != 'directory':
1478
 
                (index, from_subpath) = self._lookup_index(from_path)
1479
 
                if from_subpath not in index:
1480
 
                    # It's not a file
1481
 
                    raise errors.BzrMoveFailedError(
1482
 
                        from_rel, to_rel,
1483
 
                        errors.NotVersionedError(path=from_rel))
1484
 
 
1485
 
            if not after:
1486
 
                try:
1487
 
                    self._rename_one(from_rel, to_rel)
1488
 
                except OSError as e:
1489
 
                    if e.errno == errno.ENOENT:
1490
 
                        raise errors.BzrMoveFailedError(
1491
 
                            from_rel, to_rel, errors.NoSuchFile(to_rel))
1492
 
                    raise
1493
 
            if kind != 'directory':
1494
 
                (index, from_index_path) = self._lookup_index(from_path)
1495
 
                try:
1496
 
                    self._index_del_entry(index, from_path)
1497
 
                except KeyError:
1498
 
                    pass
1499
 
                self._index_add_entry(to_rel, kind)
1500
 
            else:
1501
 
                todo = [(p, i) for (p, i) in self._recurse_index_entries()
1502
 
                        if p.startswith(from_path + b'/')]
1503
 
                for child_path, child_value in todo:
1504
 
                    (child_to_index, child_to_index_path) = self._lookup_index(
1505
 
                        posixpath.join(to_path, posixpath.relpath(child_path, from_path)))
1506
 
                    child_to_index[child_to_index_path] = child_value
1507
 
                    # TODO(jelmer): Mark individual index as dirty
1508
 
                    self._index_dirty = True
1509
 
                    (child_from_index, child_from_index_path) = self._lookup_index(
1510
 
                        child_path)
1511
 
                    self._index_del_entry(
1512
 
                        child_from_index, child_from_index_path)
1513
 
 
1514
 
            self._versioned_dirs = None
1515
 
            self.flush()
1516
 
 
1517
 
    def find_related_paths_across_trees(self, paths, trees=[],
1518
 
                                        require_versioned=True):
1519
 
        if paths is None:
1520
 
            return None
1521
 
 
1522
 
        if require_versioned:
1523
 
            trees = [self] + (trees if trees is not None else [])
1524
 
            unversioned = set()
1525
 
            for p in paths:
1526
 
                for t in trees:
1527
 
                    if t.is_versioned(p):
1528
 
                        break
1529
 
                else:
1530
 
                    unversioned.add(p)
1531
 
            if unversioned:
1532
 
                raise errors.PathsNotVersionedError(unversioned)
1533
 
 
1534
 
        return filter(self.is_versioned, paths)
1535
 
 
1536
 
    def path_content_summary(self, path):
1537
 
        """See Tree.path_content_summary."""
1538
 
        try:
1539
 
            stat_result = self._lstat(path)
1540
 
        except OSError as e:
1541
 
            if getattr(e, 'errno', None) == errno.ENOENT:
1542
 
                # no file.
1543
 
                return ('missing', None, None, None)
1544
 
            # propagate other errors
1545
 
            raise
1546
 
        kind = mode_kind(stat_result.st_mode)
1547
 
        if kind == 'file':
1548
 
            return self._file_content_summary(path, stat_result)
1549
 
        elif kind == 'directory':
1550
 
            # perhaps it looks like a plain directory, but it's really a
1551
 
            # reference.
1552
 
            if self._directory_is_tree_reference(path):
1553
 
                kind = 'tree-reference'
1554
 
            return kind, None, None, None
1555
 
        elif kind == 'symlink':
1556
 
            target = osutils.readlink(self.abspath(path))
1557
 
            return ('symlink', None, None, target)
1558
 
        else:
1559
 
            return (kind, None, None, None)
1560
 
 
1561
 
    def stored_kind(self, relpath):
1562
 
        (index, index_path) = self._lookup_index(relpath.encode('utf-8'))
1563
 
        if index is None:
1564
 
            return kind
1565
 
        try:
1566
 
            mode = index[index_path].mode
1567
 
        except KeyError:
1568
 
            return kind
1569
 
        else:
1570
 
            if S_ISGITLINK(mode):
1571
 
                return 'tree-reference'
1572
 
            return 'directory'
1573
 
 
1574
 
    def kind(self, relpath):
1575
 
        kind = osutils.file_kind(self.abspath(relpath))
1576
 
        if kind == 'directory':
1577
 
            if self._directory_is_tree_reference(relpath):
1578
 
                return 'tree-reference'
1579
 
            return 'directory'
1580
 
        else:
1581
 
            return kind
1582
 
 
1583
 
    def _live_entry(self, relpath):
1584
 
        raise NotImplementedError(self._live_entry)
1585
 
 
1586
 
    def get_transform(self, pb=None):
1587
 
        from ..transform import TreeTransform
1588
 
        return TreeTransform(self, pb=pb)
1589
 
 
1590
 
 
1591
 
 
1592
 
class InterIndexGitTree(InterGitTrees):
1593
 
    """InterTree that works between a Git revision tree and an index."""
1594
 
 
1595
 
    def __init__(self, source, target):
1596
 
        super(InterIndexGitTree, self).__init__(source, target)
1597
 
        self._index = target.index
1598
 
 
1599
 
    @classmethod
1600
 
    def is_compatible(cls, source, target):
1601
 
        return (isinstance(source, GitRevisionTree) and
1602
 
                isinstance(target, MutableGitIndexTree))
1603
 
 
1604
 
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1605
 
                          require_versioned=False, extra_trees=None,
1606
 
                          want_unversioned=False):
1607
 
        trees = [self.source]
1608
 
        if extra_trees is not None:
1609
 
            trees.extend(extra_trees)
1610
 
        if specific_files is not None:
1611
 
            specific_files = self.target.find_related_paths_across_trees(
1612
 
                specific_files, trees,
1613
 
                require_versioned=require_versioned)
1614
 
        # TODO(jelmer): Restrict to specific_files, for performance reasons.
1615
 
        with self.lock_read():
1616
 
            return changes_between_git_tree_and_working_copy(
1617
 
                self.source.store, self.source.tree,
1618
 
                self.target, want_unchanged=want_unchanged,
1619
 
                want_unversioned=want_unversioned)
1620
 
 
1621
 
 
1622
 
_mod_tree.InterTree.register_optimiser(InterIndexGitTree)
1623
 
 
1624
 
 
1625
 
def changes_between_git_tree_and_working_copy(store, from_tree_sha, target,
1626
 
                                              want_unchanged=False,
1627
 
                                              want_unversioned=False):
1628
 
    """Determine the changes between a git tree and a working tree with index.
1629
 
 
1630
 
    """
1631
 
    extras = set()
1632
 
    blobs = {}
1633
 
    # Report dirified directories to commit_tree first, so that they can be
1634
 
    # replaced with non-empty directories if they have contents.
1635
 
    dirified = []
1636
 
    trust_executable = target._supports_executable()
1637
 
    for path, index_entry in target._recurse_index_entries():
1638
 
        try:
1639
 
            live_entry = target._live_entry(path)
1640
 
        except EnvironmentError as e:
1641
 
            if e.errno == errno.ENOENT:
1642
 
                # Entry was removed; keep it listed, but mark it as gone.
1643
 
                blobs[path] = (ZERO_SHA, 0)
1644
 
            else:
1645
 
                raise
1646
 
        else:
1647
 
            if live_entry is None:
1648
 
                # Entry was turned into a directory.
1649
 
                # Maybe it's just a submodule that's not checked out?
1650
 
                if S_ISGITLINK(index_entry.mode):
1651
 
                    blobs[path] = (index_entry.sha, index_entry.mode)
1652
 
                else:
1653
 
                    dirified.append((path, Tree().id, stat.S_IFDIR))
1654
 
                    store.add_object(Tree())
1655
 
            else:
1656
 
                mode = live_entry.mode
1657
 
                if not trust_executable:
1658
 
                    if mode_is_executable(index_entry.mode):
1659
 
                        mode |= 0o111
1660
 
                    else:
1661
 
                        mode &= ~0o111
1662
 
                blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
1663
 
    if want_unversioned:
1664
 
        for e in target.extras():
1665
 
            st = target._lstat(e)
1666
 
            try:
1667
 
                np, accessible = osutils.normalized_filename(e)
1668
 
            except UnicodeDecodeError:
1669
 
                raise errors.BadFilenameEncoding(
1670
 
                    e, osutils._fs_enc)
1671
 
            if stat.S_ISDIR(st.st_mode):
1672
 
                blob = Tree()
1673
 
            else:
1674
 
                blob = blob_from_path_and_stat(
1675
 
                    target.abspath(e).encode(osutils._fs_enc), st)
1676
 
            store.add_object(blob)
1677
 
            np = np.encode('utf-8')
1678
 
            blobs[np] = (blob.id, cleanup_mode(st.st_mode))
1679
 
            extras.add(np)
1680
 
    to_tree_sha = commit_tree(
1681
 
        store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()])
1682
 
    return store.tree_changes(
1683
 
        from_tree_sha, to_tree_sha, include_trees=True,
1684
 
        want_unchanged=want_unchanged, change_type_same=True), extras