/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: 2020-01-12 13:56:10 UTC
  • mto: This revision was merged to the branch mainline in revision 7443.
  • Revision ID: jelmer@jelmer.uk-20200112135610-0a9bct6x4cw7he6y
Add strip_segment_parameters function.

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