/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: Gustav Hartvigsson
  • Date: 2021-01-09 21:36:27 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210109213627-h1xwcutzy9m7a99b
Added 'Case Preserving Working Tree Use Cases' from Canonical Wiki

* Addod a page from the Canonical Bazaar wiki
  with information on the scmeatics of case
  perserving filesystems an a case insensitive
  filesystem works.
  
  * Needs re-work, but this will do as it is the
    same inforamoton as what was on the linked
    page in the currint documentation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Git Trees."""
19
19
 
20
 
from __future__ import absolute_import
21
 
 
22
20
from collections import deque
23
21
import errno
24
22
from io import BytesIO
25
23
import os
26
24
 
 
25
from dulwich.config import (
 
26
    parse_submodules,
 
27
    ConfigFile as GitConfigFile,
 
28
    )
 
29
from dulwich.diff_tree import tree_changes, RenameDetector
 
30
from dulwich.errors import NotTreeError
27
31
from dulwich.index import (
28
32
    blob_from_path_and_stat,
29
33
    cleanup_mode,
30
34
    commit_tree,
31
35
    index_entry_from_stat,
 
36
    Index,
32
37
    )
33
38
from dulwich.object_store import (
34
39
    tree_lookup_path,
59
64
    CURRENT_REVISION,
60
65
    NULL_REVISION,
61
66
    )
62
 
from ..sixish import (
63
 
    text_type,
64
 
    viewitems,
65
 
    )
66
67
 
67
68
from .mapping import (
 
69
    encode_git_path,
 
70
    decode_git_path,
68
71
    mode_is_executable,
69
72
    mode_kind,
70
 
    GitFileIdMap,
71
73
    default_mapping,
72
74
    )
 
75
from .transportgit import (
 
76
    TransportObjectStore,
 
77
    TransportRepo,
 
78
    )
 
79
from ..bzr.inventorytree import InventoryTreeChange
73
80
 
74
81
 
75
82
class GitTreeDirectory(_mod_tree.TreeDirectory):
76
83
 
77
 
    __slots__ = ['file_id', 'name', 'parent_id', 'children']
 
84
    __slots__ = ['file_id', 'name', 'parent_id']
78
85
 
79
86
    def __init__(self, file_id, name, parent_id):
80
87
        self.file_id = file_id
81
88
        self.name = name
82
89
        self.parent_id = parent_id
83
 
        # TODO(jelmer)
84
 
        self.children = {}
85
90
 
86
91
    @property
87
92
    def kind(self):
109
114
 
110
115
class GitTreeFile(_mod_tree.TreeFile):
111
116
 
112
 
    __slots__ = ['file_id', 'name', 'parent_id', 'text_size', 'text_sha1',
113
 
                 'executable']
 
117
    __slots__ = ['file_id', 'name', 'parent_id', 'text_size',
 
118
                 'executable', 'git_sha1']
114
119
 
115
120
    def __init__(self, file_id, name, parent_id, text_size=None,
116
 
                 text_sha1=None, executable=None):
 
121
                 git_sha1=None, executable=None):
117
122
        self.file_id = file_id
118
123
        self.name = name
119
124
        self.parent_id = parent_id
120
125
        self.text_size = text_size
121
 
        self.text_sha1 = text_sha1
 
126
        self.git_sha1 = git_sha1
122
127
        self.executable = executable
123
128
 
124
129
    @property
130
135
                self.file_id == other.file_id and
131
136
                self.name == other.name and
132
137
                self.parent_id == other.parent_id and
133
 
                self.text_sha1 == other.text_sha1 and
 
138
                self.git_sha1 == other.git_sha1 and
134
139
                self.text_size == other.text_size and
135
140
                self.executable == other.executable)
136
141
 
137
142
    def __repr__(self):
138
143
        return ("%s(file_id=%r, name=%r, parent_id=%r, text_size=%r, "
139
 
                "text_sha1=%r, executable=%r)") % (
 
144
                "git_sha1=%r, executable=%r)") % (
140
145
            type(self).__name__, self.file_id, self.name, self.parent_id,
141
 
            self.text_size, self.text_sha1, self.executable)
 
146
            self.text_size, self.git_sha1, self.executable)
142
147
 
143
148
    def copy(self):
144
149
        ret = self.__class__(
145
150
            self.file_id, self.name, self.parent_id)
146
 
        ret.text_sha1 = self.text_sha1
 
151
        ret.git_sha1 = self.git_sha1
147
152
        ret.text_size = self.text_size
148
153
        ret.executable = self.executable
149
154
        return ret
190
195
            self.symlink_target)
191
196
 
192
197
 
193
 
class GitTreeSubmodule(_mod_tree.TreeLink):
 
198
class GitTreeSubmodule(_mod_tree.TreeReference):
194
199
 
195
200
    __slots__ = ['file_id', 'name', 'parent_id', 'reference_revision']
196
201
 
201
206
        self.reference_revision = reference_revision
202
207
 
203
208
    @property
 
209
    def executable(self):
 
210
        return False
 
211
 
 
212
    @property
204
213
    def kind(self):
205
214
        return 'tree-reference'
206
215
 
247
256
    return path
248
257
 
249
258
 
250
 
class GitRevisionTree(revisiontree.RevisionTree):
 
259
class GitTree(_mod_tree.Tree):
 
260
 
 
261
    def iter_git_objects(self):
 
262
        """Iterate over all the objects in the tree.
 
263
 
 
264
        :return :Yields tuples with (path, sha, mode)
 
265
        """
 
266
        raise NotImplementedError(self.iter_git_objects)
 
267
 
 
268
    def git_snapshot(self, want_unversioned=False):
 
269
        """Snapshot a tree, and return tree object.
 
270
 
 
271
        :return: Tree sha and set of extras
 
272
        """
 
273
        raise NotImplementedError(self.snapshot)
 
274
 
 
275
    def preview_transform(self, pb=None):
 
276
        from .transform import GitTransformPreview
 
277
        return GitTransformPreview(self, pb=pb)
 
278
 
 
279
    def find_related_paths_across_trees(self, paths, trees=[],
 
280
                                        require_versioned=True):
 
281
        if paths is None:
 
282
            return None
 
283
        if require_versioned:
 
284
            trees = [self] + (trees if trees is not None else [])
 
285
            unversioned = set()
 
286
            for p in paths:
 
287
                for t in trees:
 
288
                    if t.is_versioned(p):
 
289
                        break
 
290
                else:
 
291
                    unversioned.add(p)
 
292
            if unversioned:
 
293
                raise errors.PathsNotVersionedError(unversioned)
 
294
        return filter(self.is_versioned, paths)
 
295
 
 
296
    def _submodule_info(self):
 
297
        if self._submodules is None:
 
298
            try:
 
299
                with self.get_file('.gitmodules') as f:
 
300
                    config = GitConfigFile.from_file(f)
 
301
                    self._submodules = {
 
302
                        path: (url, section)
 
303
                        for path, url, section in parse_submodules(config)}
 
304
            except errors.NoSuchFile:
 
305
                self._submodules = {}
 
306
        return self._submodules
 
307
 
 
308
 
 
309
class GitRevisionTree(revisiontree.RevisionTree, GitTree):
251
310
    """Revision tree implementation based on Git objects."""
252
311
 
253
312
    def __init__(self, repository, revision_id):
254
313
        self._revision_id = revision_id
255
314
        self._repository = repository
 
315
        self._submodules = None
256
316
        self.store = repository._git.object_store
257
317
        if not isinstance(revision_id, bytes):
258
318
            raise TypeError(revision_id)
261
321
        if revision_id == NULL_REVISION:
262
322
            self.tree = None
263
323
            self.mapping = default_mapping
264
 
            self._fileid_map = GitFileIdMap(
265
 
                {},
266
 
                default_mapping)
267
324
        else:
268
325
            try:
269
326
                commit = self.store[self.commit_id]
270
327
            except KeyError:
271
328
                raise errors.NoSuchRevision(repository, revision_id)
272
329
            self.tree = commit.tree
273
 
            self._fileid_map = self.mapping.get_fileid_map(
274
 
                self.store.__getitem__, self.tree)
275
 
 
276
 
    def _get_nested_repository(self, path):
277
 
        nested_repo_transport = self._repository.user_transport.clone(path)
 
330
 
 
331
    def git_snapshot(self, want_unversioned=False):
 
332
        return self.tree, set()
 
333
 
 
334
    def _get_submodule_repository(self, relpath):
 
335
        if not isinstance(relpath, bytes):
 
336
            raise TypeError(relpath)
 
337
        try:
 
338
            info = self._submodule_info()[relpath]
 
339
        except KeyError:
 
340
            nested_repo_transport = self._repository.controldir.user_transport.clone(
 
341
                decode_git_path(relpath))
 
342
        else:
 
343
            nested_repo_transport = self._repository.controldir.control_transport.clone(
 
344
                posixpath.join('modules', decode_git_path(info[1])))
278
345
        nested_controldir = _mod_controldir.ControlDir.open_from_transport(
279
346
            nested_repo_transport)
280
347
        return nested_controldir.find_repository()
281
348
 
 
349
    def _get_submodule_store(self, relpath):
 
350
        return self._get_submodule_repository(relpath)._git.object_store
 
351
 
 
352
    def get_nested_tree(self, path):
 
353
        encoded_path = encode_git_path(path)
 
354
        nested_repo = self._get_submodule_repository(encoded_path)
 
355
        ref_rev = self.get_reference_revision(path)
 
356
        return nested_repo.revision_tree(ref_rev)
 
357
 
282
358
    def supports_rename_tracking(self):
283
359
        return False
284
360
 
287
363
        if self.commit_id == ZERO_SHA:
288
364
            return NULL_REVISION
289
365
        (unused_path, commit_id) = change_scanner.find_last_change_revision(
290
 
            path.encode('utf-8'), self.commit_id)
 
366
            encode_git_path(path), self.commit_id)
291
367
        return self._repository.lookup_foreign_revision_id(
292
368
            commit_id, self.mapping)
293
369
 
302
378
            raise _mod_tree.FileTimestampUnavailable(path)
303
379
        return rev.timestamp
304
380
 
305
 
    def id2path(self, file_id):
 
381
    def id2path(self, file_id, recurse='down'):
306
382
        try:
307
 
            path = self._fileid_map.lookup_path(file_id)
 
383
            path = self.mapping.parse_file_id(file_id)
308
384
        except ValueError:
309
385
            raise errors.NoSuchId(self, file_id)
310
386
        if self.is_versioned(path):
319
395
            return None
320
396
        if not self.is_versioned(path):
321
397
            return None
322
 
        return self._fileid_map.lookup_file_id(osutils.safe_unicode(path))
 
398
        return self.mapping.generate_file_id(osutils.safe_unicode(path))
323
399
 
324
400
    def all_file_ids(self):
325
401
        raise errors.UnsupportedOperation(self.all_file_ids, self)
334
410
            tree = store[tree_id]
335
411
            for name, mode, hexsha in tree.items():
336
412
                subpath = posixpath.join(path, name)
337
 
                ret.add(subpath.decode('utf-8'))
 
413
                ret.add(decode_git_path(subpath))
338
414
                if stat.S_ISDIR(mode):
339
415
                    todo.append((store, subpath, hexsha))
340
416
        return ret
341
417
 
342
 
    def get_root_id(self):
343
 
        if self.tree is None:
344
 
            return None
345
 
        return self.path2id("")
346
 
 
347
 
    def has_or_had_id(self, file_id):
348
 
        try:
349
 
            self.id2path(file_id)
350
 
        except errors.NoSuchId:
351
 
            return False
352
 
        return True
353
 
 
354
 
    def has_id(self, file_id):
355
 
        try:
356
 
            path = self.id2path(file_id)
357
 
        except errors.NoSuchId:
358
 
            return False
359
 
        return self.has_filename(path)
360
 
 
361
418
    def _lookup_path(self, path):
362
419
        if self.tree is None:
363
420
            raise errors.NoSuchFile(path)
364
 
        try:
365
 
            (mode, hexsha) = tree_lookup_path(
366
 
                self.store.__getitem__, self.tree, path.encode('utf-8'))
367
 
        except KeyError:
368
 
            raise errors.NoSuchFile(self, path)
369
 
        else:
370
 
            return (self.store, mode, hexsha)
 
421
 
 
422
        encoded_path = encode_git_path(path)
 
423
        parts = encoded_path.split(b'/')
 
424
        hexsha = self.tree
 
425
        store = self.store
 
426
        mode = None
 
427
        for i, p in enumerate(parts):
 
428
            if not p:
 
429
                continue
 
430
            obj = store[hexsha]
 
431
            if not isinstance(obj, Tree):
 
432
                raise NotTreeError(hexsha)
 
433
            try:
 
434
                mode, hexsha = obj[p]
 
435
            except KeyError:
 
436
                raise errors.NoSuchFile(path)
 
437
            if S_ISGITLINK(mode) and i != len(parts) - 1:
 
438
                store = self._get_submodule_store(b'/'.join(parts[:i + 1]))
 
439
                hexsha = store[hexsha].tree
 
440
        return (store, mode, hexsha)
371
441
 
372
442
    def is_executable(self, path):
373
443
        (store, mode, hexsha) = self._lookup_path(path)
391
461
        else:
392
462
            return True
393
463
 
394
 
    def list_files(self, include_root=False, from_dir=None, recursive=True):
 
464
    def list_files(self, include_root=False, from_dir=None, recursive=True,
 
465
                   recurse_nested=False):
395
466
        if self.tree is None:
396
467
            return
397
468
        if from_dir is None or from_dir == '.':
401
472
            root_ie = self._get_dir_ie(b"", None)
402
473
        else:
403
474
            parent_path = posixpath.dirname(from_dir)
404
 
            parent_id = self._fileid_map.lookup_file_id(parent_path)
 
475
            parent_id = self.mapping.generate_file_id(parent_path)
405
476
            if mode_kind(mode) == 'directory':
406
 
                root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
 
477
                root_ie = self._get_dir_ie(encode_git_path(from_dir), parent_id)
407
478
            else:
408
479
                root_ie = self._get_file_ie(
409
 
                    store, from_dir.encode("utf-8"),
 
480
                    store, encode_git_path(from_dir),
410
481
                    posixpath.basename(from_dir), mode, hexsha)
411
482
        if include_root:
412
 
            yield (from_dir, "V", root_ie.kind, root_ie.file_id, root_ie)
 
483
            yield (from_dir, "V", root_ie.kind, root_ie)
413
484
        todo = []
414
485
        if root_ie.kind == 'directory':
415
 
            todo.append((store, from_dir.encode("utf-8"),
 
486
            todo.append((store, encode_git_path(from_dir),
416
487
                         b"", hexsha, root_ie.file_id))
417
488
        while todo:
418
489
            (store, path, relpath, hexsha, parent_id) = todo.pop()
422
493
                    continue
423
494
                child_path = posixpath.join(path, name)
424
495
                child_relpath = posixpath.join(relpath, name)
 
496
                if S_ISGITLINK(mode) and recurse_nested:
 
497
                    mode = stat.S_IFDIR
 
498
                    store = self._get_submodule_store(child_relpath)
 
499
                    hexsha = store[hexsha].tree
425
500
                if stat.S_ISDIR(mode):
426
501
                    ie = self._get_dir_ie(child_path, parent_id)
427
502
                    if recursive:
431
506
                else:
432
507
                    ie = self._get_file_ie(
433
508
                        store, child_path, name, mode, hexsha, parent_id)
434
 
                yield (child_relpath.decode('utf-8'), "V", ie.kind, ie.file_id,
435
 
                       ie)
 
509
                yield (decode_git_path(child_relpath), "V", ie.kind, ie)
436
510
 
437
511
    def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
438
512
        if not isinstance(path, bytes):
440
514
        if not isinstance(name, bytes):
441
515
            raise TypeError(name)
442
516
        kind = mode_kind(mode)
443
 
        path = path.decode('utf-8')
444
 
        name = name.decode("utf-8")
445
 
        file_id = self._fileid_map.lookup_file_id(path)
 
517
        path = decode_git_path(path)
 
518
        name = decode_git_path(name)
 
519
        file_id = self.mapping.generate_file_id(path)
446
520
        ie = entry_factory[kind](file_id, name, parent_id)
447
521
        if kind == 'symlink':
448
 
            ie.symlink_target = store[hexsha].data.decode('utf-8')
 
522
            ie.symlink_target = decode_git_path(store[hexsha].data)
449
523
        elif kind == 'tree-reference':
450
524
            ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(
451
525
                hexsha)
452
526
        else:
453
 
            data = store[hexsha].data
454
 
            ie.text_sha1 = osutils.sha_string(data)
455
 
            ie.text_size = len(data)
 
527
            ie.git_sha1 = hexsha
 
528
            ie.text_size = None
456
529
            ie.executable = mode_is_executable(mode)
457
530
        return ie
458
531
 
459
532
    def _get_dir_ie(self, path, parent_id):
460
 
        path = path.decode('utf-8')
461
 
        file_id = self._fileid_map.lookup_file_id(path)
 
533
        path = decode_git_path(path)
 
534
        file_id = self.mapping.generate_file_id(path)
462
535
        return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
463
536
 
464
 
    def iter_child_entries(self, path, file_id=None):
 
537
    def iter_child_entries(self, path):
465
538
        (store, mode, tree_sha) = self._lookup_path(path)
466
539
 
467
540
        if mode is not None and not stat.S_ISDIR(mode):
468
541
            return
469
542
 
470
 
        encoded_path = path.encode('utf-8')
 
543
        encoded_path = encode_git_path(path)
471
544
        file_id = self.path2id(path)
472
545
        tree = store[tree_sha]
473
546
        for name, mode, hexsha in tree.iteritems():
480
553
                yield self._get_file_ie(store, child_path, name, mode, hexsha,
481
554
                                        file_id)
482
555
 
483
 
    def iter_entries_by_dir(self, specific_files=None, yield_parents=False):
 
556
    def iter_entries_by_dir(self, specific_files=None,
 
557
                            recurse_nested=False):
484
558
        if self.tree is None:
485
559
            return
486
 
        if yield_parents:
487
 
            # TODO(jelmer): Support yield parents
488
 
            raise NotImplementedError
489
560
        if specific_files is not None:
490
561
            if specific_files in ([""], []):
491
562
                specific_files = None
492
563
            else:
493
 
                specific_files = set([p.encode('utf-8')
 
564
                specific_files = set([encode_git_path(p)
494
565
                                      for p in specific_files])
495
 
        todo = deque([(self.store, b"", self.tree, self.get_root_id())])
 
566
        todo = deque([(self.store, b"", self.tree, self.path2id(''))])
496
567
        if specific_files is None or u"" in specific_files:
497
568
            yield u"", self._get_dir_ie(b"", None)
498
569
        while todo:
503
574
                if self.mapping.is_special_file(name):
504
575
                    continue
505
576
                child_path = posixpath.join(path, name)
506
 
                child_path_decoded = child_path.decode('utf-8')
 
577
                child_path_decoded = decode_git_path(child_path)
 
578
                if recurse_nested and S_ISGITLINK(mode):
 
579
                    mode = stat.S_IFDIR
 
580
                    store = self._get_submodule_store(child_path)
 
581
                    hexsha = store[hexsha].tree
507
582
                if stat.S_ISDIR(mode):
508
583
                    if (specific_files is None or
509
584
                            any([p for p in specific_files if p.startswith(
525
600
        if self.supports_tree_reference():
526
601
            for path, entry in self.iter_entries_by_dir():
527
602
                if entry.kind == 'tree-reference':
528
 
                    yield path, self.mapping.generate_file_id(b'')
 
603
                    yield path
529
604
 
530
605
    def get_revision_id(self):
531
606
        """See RevisionTree.get_revision_id."""
558
633
        """See RevisionTree.get_symlink_target."""
559
634
        (store, mode, hexsha) = self._lookup_path(path)
560
635
        if stat.S_ISLNK(mode):
561
 
            return store[hexsha].data.decode('utf-8')
 
636
            return decode_git_path(store[hexsha].data)
562
637
        else:
563
638
            return None
564
639
 
566
641
        """See RevisionTree.get_symlink_target."""
567
642
        (store, mode, hexsha) = self._lookup_path(path)
568
643
        if S_ISGITLINK(mode):
569
 
            nested_repo = self._get_nested_repository(path)
570
 
            return nested_repo.lookup_foreign_revision_id(hexsha)
 
644
            try:
 
645
                nested_repo = self._get_submodule_repository(encode_git_path(path))
 
646
            except errors.NotBranchError:
 
647
                return self.mapping.revision_id_foreign_to_bzr(hexsha)
 
648
            else:
 
649
                return nested_repo.lookup_foreign_revision_id(hexsha)
571
650
        else:
572
651
            return None
573
652
 
589
668
            return (kind, len(contents), executable,
590
669
                    osutils.sha_string(contents))
591
670
        elif kind == 'symlink':
592
 
            return (kind, None, None, store[hexsha].data.decode('utf-8'))
 
671
            return (kind, None, None, decode_git_path(store[hexsha].data))
593
672
        elif kind == 'tree-reference':
594
 
            nested_repo = self._get_nested_repository(path)
 
673
            nested_repo = self._get_submodule_repository(encode_git_path(path))
595
674
            return (kind, None, None,
596
675
                    nested_repo.lookup_foreign_revision_id(hexsha))
597
676
        else:
598
677
            return (kind, None, None, None)
599
678
 
600
 
    def find_related_paths_across_trees(self, paths, trees=[],
601
 
                                        require_versioned=True):
602
 
        if paths is None:
603
 
            return None
604
 
        if require_versioned:
605
 
            trees = [self] + (trees if trees is not None else [])
606
 
            unversioned = set()
607
 
            for p in paths:
608
 
                for t in trees:
609
 
                    if t.is_versioned(p):
610
 
                        break
611
 
                else:
612
 
                    unversioned.add(p)
613
 
            if unversioned:
614
 
                raise errors.PathsNotVersionedError(unversioned)
615
 
        return filter(self.is_versioned, paths)
616
 
 
617
679
    def _iter_tree_contents(self, include_trees=False):
618
680
        if self.tree is None:
619
681
            return iter([])
647
709
    def walkdirs(self, prefix=u""):
648
710
        (store, mode, hexsha) = self._lookup_path(prefix)
649
711
        todo = deque(
650
 
            [(store, prefix.encode('utf-8'), hexsha, self.path2id(prefix))])
 
712
            [(store, encode_git_path(prefix), hexsha)])
651
713
        while todo:
652
 
            store, path, tree_sha, parent_id = todo.popleft()
653
 
            path_decoded = path.decode('utf-8')
 
714
            store, path, tree_sha = todo.popleft()
 
715
            path_decoded = decode_git_path(path)
654
716
            tree = store[tree_sha]
655
717
            children = []
656
718
            for name, mode, hexsha in tree.iteritems():
657
719
                if self.mapping.is_special_file(name):
658
720
                    continue
659
721
                child_path = posixpath.join(path, name)
660
 
                file_id = self.path2id(child_path.decode('utf-8'))
661
722
                if stat.S_ISDIR(mode):
662
 
                    todo.append((store, child_path, hexsha, file_id))
 
723
                    todo.append((store, child_path, hexsha))
663
724
                children.append(
664
 
                    (child_path.decode('utf-8'), name.decode('utf-8'),
 
725
                    (decode_git_path(child_path), decode_git_path(name),
665
726
                        mode_kind(mode), None,
666
 
                        file_id, mode_kind(mode)))
667
 
            yield (path_decoded, parent_id), children
668
 
 
669
 
 
670
 
def tree_delta_from_git_changes(changes, mapping,
671
 
                                fileid_maps, specific_files=None,
 
727
                        mode_kind(mode)))
 
728
            yield path_decoded, children
 
729
 
 
730
 
 
731
def tree_delta_from_git_changes(changes, mappings,
 
732
                                specific_files=None,
672
733
                                require_versioned=False, include_root=False,
673
 
                                target_extras=None):
 
734
                                source_extras=None, target_extras=None):
674
735
    """Create a TreeDelta from two git trees.
675
736
 
676
737
    source and target are iterators over tuples with:
677
738
        (filename, sha, mode)
678
739
    """
679
 
    (old_fileid_map, new_fileid_map) = fileid_maps
 
740
    (old_mapping, new_mapping) = mappings
680
741
    if target_extras is None:
681
742
        target_extras = set()
 
743
    if source_extras is None:
 
744
        source_extras = set()
682
745
    ret = delta.TreeDelta()
683
 
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
 
746
    added = []
 
747
    for (change_type, old, new) in changes:
 
748
        (oldpath, oldmode, oldsha) = old
 
749
        (newpath, newmode, newsha) = new
684
750
        if newpath == b'' and not include_root:
685
751
            continue
686
 
        if oldpath is None:
 
752
        copied = (change_type == 'copy')
 
753
        if oldpath is not None:
 
754
            oldpath_decoded = decode_git_path(oldpath)
 
755
        else:
687
756
            oldpath_decoded = None
 
757
        if newpath is not None:
 
758
            newpath_decoded = decode_git_path(newpath)
688
759
        else:
689
 
            oldpath_decoded = oldpath.decode('utf-8')
690
 
        if newpath is None:
691
760
            newpath_decoded = None
692
 
        else:
693
 
            newpath_decoded = newpath.decode('utf-8')
694
761
        if not (specific_files is None or
695
762
                (oldpath is not None and
696
763
                    osutils.is_inside_or_parent_of_any(
699
766
                    osutils.is_inside_or_parent_of_any(
700
767
                        specific_files, newpath_decoded))):
701
768
            continue
702
 
        if mapping.is_special_file(oldpath):
 
769
 
 
770
        if oldpath is None:
 
771
            oldexe = None
 
772
            oldkind = None
 
773
            oldname = None
 
774
            oldparent = None
 
775
            oldversioned = False
 
776
        else:
 
777
            oldversioned = (oldpath not in source_extras)
 
778
            if oldmode:
 
779
                oldexe = mode_is_executable(oldmode)
 
780
                oldkind = mode_kind(oldmode)
 
781
            else:
 
782
                oldexe = False
 
783
                oldkind = None
 
784
            if oldpath == b'':
 
785
                oldparent = None
 
786
                oldname = u''
 
787
            else:
 
788
                (oldparentpath, oldname) = osutils.split(oldpath_decoded)
 
789
                oldparent = old_mapping.generate_file_id(oldparentpath)
 
790
        if newpath is None:
 
791
            newexe = None
 
792
            newkind = None
 
793
            newname = None
 
794
            newparent = None
 
795
            newversioned = False
 
796
        else:
 
797
            newversioned = (newpath not in target_extras)
 
798
            if newmode:
 
799
                newexe = mode_is_executable(newmode)
 
800
                newkind = mode_kind(newmode)
 
801
            else:
 
802
                newexe = False
 
803
                newkind = None
 
804
            if newpath_decoded == u'':
 
805
                newparent = None
 
806
                newname = u''
 
807
            else:
 
808
                newparentpath, newname = osutils.split(newpath_decoded)
 
809
                newparent = new_mapping.generate_file_id(newparentpath)
 
810
        if oldversioned and not copied:
 
811
            fileid = old_mapping.generate_file_id(oldpath_decoded)
 
812
        elif newversioned:
 
813
            fileid = new_mapping.generate_file_id(newpath_decoded)
 
814
        else:
 
815
            fileid = None
 
816
        if old_mapping.is_special_file(oldpath):
703
817
            oldpath = None
704
 
        if mapping.is_special_file(newpath):
 
818
        if new_mapping.is_special_file(newpath):
705
819
            newpath = None
706
820
        if oldpath is None and newpath is None:
707
821
            continue
708
 
        if oldpath is None:
709
 
            if newpath in target_extras:
710
 
                ret.unversioned.append(
711
 
                    (osutils.normalized_filename(newpath)[0], None,
712
 
                     mode_kind(newmode)))
713
 
            else:
714
 
                file_id = new_fileid_map.lookup_file_id(newpath_decoded)
715
 
                ret.added.append(
716
 
                    (newpath_decoded, file_id, mode_kind(newmode)))
 
822
        change = InventoryTreeChange(
 
823
            fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
 
824
            (oldversioned, newversioned),
 
825
            (oldparent, newparent), (oldname, newname),
 
826
            (oldkind, newkind), (oldexe, newexe),
 
827
            copied=copied)
 
828
        if newpath is not None and not newversioned and newkind != 'directory':
 
829
            change.file_id = None
 
830
            ret.unversioned.append(change)
 
831
        elif change_type == 'add':
 
832
            added.append((newpath, newkind))
717
833
        elif newpath is None or newmode == 0:
718
 
            file_id = old_fileid_map.lookup_file_id(oldpath_decoded)
719
 
            ret.removed.append((oldpath_decoded, file_id, mode_kind(oldmode)))
720
 
        elif oldpath != newpath:
721
 
            file_id = old_fileid_map.lookup_file_id(oldpath_decoded)
722
 
            ret.renamed.append(
723
 
                (oldpath_decoded, newpath.decode('utf-8'), file_id,
724
 
                 mode_kind(newmode), (oldsha != newsha),
725
 
                 (oldmode != newmode)))
 
834
            ret.removed.append(change)
 
835
        elif change_type == 'delete':
 
836
            ret.removed.append(change)
 
837
        elif change_type == 'copy':
 
838
            if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
 
839
                continue
 
840
            ret.copied.append(change)
 
841
        elif change_type == 'rename':
 
842
            if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
 
843
                continue
 
844
            ret.renamed.append(change)
726
845
        elif mode_kind(oldmode) != mode_kind(newmode):
727
 
            file_id = new_fileid_map.lookup_file_id(newpath_decoded)
728
 
            ret.kind_changed.append(
729
 
                (newpath_decoded, file_id, mode_kind(oldmode),
730
 
                 mode_kind(newmode)))
 
846
            ret.kind_changed.append(change)
731
847
        elif oldsha != newsha or oldmode != newmode:
732
848
            if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
733
849
                continue
734
 
            file_id = new_fileid_map.lookup_file_id(newpath_decoded)
735
 
            ret.modified.append(
736
 
                (newpath_decoded, file_id, mode_kind(newmode),
737
 
                 (oldsha != newsha), (oldmode != newmode)))
 
850
            ret.modified.append(change)
738
851
        else:
739
 
            file_id = new_fileid_map.lookup_file_id(newpath_decoded)
740
 
            ret.unchanged.append(
741
 
                (newpath_decoded, file_id, mode_kind(newmode)))
 
852
            ret.unchanged.append(change)
 
853
 
 
854
    implicit_dirs = {b''}
 
855
    for path, kind in added:
 
856
        if kind == 'directory' or path in target_extras:
 
857
            continue
 
858
        implicit_dirs.update(osutils.parent_directories(path))
 
859
 
 
860
    for path, kind in added:
 
861
        if kind == 'directory' and path not in implicit_dirs:
 
862
            continue
 
863
        path_decoded = decode_git_path(path)
 
864
        parent_path, basename = osutils.split(path_decoded)
 
865
        parent_id = new_mapping.generate_file_id(parent_path)
 
866
        file_id = new_mapping.generate_file_id(path_decoded)
 
867
        ret.added.append(
 
868
            InventoryTreeChange(
 
869
                file_id, (None, path_decoded), True,
 
870
                (False, True),
 
871
                (None, parent_id),
 
872
                (None, basename), (None, kind), (None, False)))
742
873
 
743
874
    return ret
744
875
 
745
876
 
746
877
def changes_from_git_changes(changes, mapping, specific_files=None,
747
 
                             include_unchanged=False, target_extras=None):
 
878
                             include_unchanged=False, source_extras=None,
 
879
                             target_extras=None):
748
880
    """Create a iter_changes-like generator from a git stream.
749
881
 
750
882
    source and target are iterators over tuples with:
752
884
    """
753
885
    if target_extras is None:
754
886
        target_extras = set()
755
 
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
 
887
    if source_extras is None:
 
888
        source_extras = set()
 
889
    for (change_type, old, new) in changes:
 
890
        if change_type == 'unchanged' and not include_unchanged:
 
891
            continue
 
892
        (oldpath, oldmode, oldsha) = old
 
893
        (newpath, newmode, newsha) = new
756
894
        if oldpath is not None:
757
 
            oldpath_decoded = oldpath.decode('utf-8')
 
895
            oldpath_decoded = decode_git_path(oldpath)
758
896
        else:
759
897
            oldpath_decoded = None
760
898
        if newpath is not None:
761
 
            newpath_decoded = newpath.decode('utf-8')
 
899
            newpath_decoded = decode_git_path(newpath)
762
900
        else:
763
901
            newpath_decoded = None
764
902
        if not (specific_files is None or
773
911
            continue
774
912
        if newpath is not None and mapping.is_special_file(newpath):
775
913
            continue
776
 
        if oldpath_decoded is None:
777
 
            fileid = mapping.generate_file_id(newpath_decoded)
 
914
        if oldpath is None:
778
915
            oldexe = None
779
916
            oldkind = None
780
917
            oldname = None
781
918
            oldparent = None
782
919
            oldversioned = False
783
920
        else:
784
 
            oldversioned = True
 
921
            oldversioned = (oldpath not in source_extras)
785
922
            if oldmode:
786
923
                oldexe = mode_is_executable(oldmode)
787
924
                oldkind = mode_kind(oldmode)
794
931
            else:
795
932
                (oldparentpath, oldname) = osutils.split(oldpath_decoded)
796
933
                oldparent = mapping.generate_file_id(oldparentpath)
797
 
            fileid = mapping.generate_file_id(oldpath_decoded)
798
 
        if newpath_decoded is None:
 
934
        if newpath is None:
799
935
            newexe = None
800
936
            newkind = None
801
937
            newname = None
802
938
            newparent = None
803
939
            newversioned = False
804
940
        else:
805
 
            newversioned = (newpath_decoded not in target_extras)
 
941
            newversioned = (newpath not in target_extras)
806
942
            if newmode:
807
943
                newexe = mode_is_executable(newmode)
808
944
                newkind = mode_kind(newmode)
816
952
                newparentpath, newname = osutils.split(newpath_decoded)
817
953
                newparent = mapping.generate_file_id(newparentpath)
818
954
        if (not include_unchanged and
819
 
            oldkind == 'directory' and newkind == 'directory' and
 
955
                oldkind == 'directory' and newkind == 'directory' and
820
956
                oldpath_decoded == newpath_decoded):
821
957
            continue
822
 
        yield (fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
823
 
               (oldversioned, newversioned),
824
 
               (oldparent, newparent), (oldname, newname),
825
 
               (oldkind, newkind), (oldexe, newexe))
 
958
        if oldversioned and change_type != 'copy':
 
959
            fileid = mapping.generate_file_id(oldpath_decoded)
 
960
        elif newversioned:
 
961
            fileid = mapping.generate_file_id(newpath_decoded)
 
962
        else:
 
963
            fileid = None
 
964
        if oldkind == 'directory' and newkind == 'directory':
 
965
            modified = False
 
966
        else:
 
967
            modified = (oldsha != newsha) or (oldmode != newmode)
 
968
        yield InventoryTreeChange(
 
969
            fileid, (oldpath_decoded, newpath_decoded),
 
970
            modified,
 
971
            (oldversioned, newversioned),
 
972
            (oldparent, newparent), (oldname, newname),
 
973
            (oldkind, newkind), (oldexe, newexe),
 
974
            copied=(change_type == 'copy'))
826
975
 
827
976
 
828
977
class InterGitTrees(_mod_tree.InterTree):
832
981
    _matching_to_tree_format = None
833
982
    _test_mutable_trees_to_test_trees = None
834
983
 
 
984
    def __init__(self, source, target):
 
985
        super(InterGitTrees, self).__init__(source, target)
 
986
        if self.source.store == self.target.store:
 
987
            self.store = self.source.store
 
988
        else:
 
989
            self.store = OverlayObjectStore(
 
990
                [self.source.store, self.target.store])
 
991
        self.rename_detector = RenameDetector(self.store)
 
992
 
835
993
    @classmethod
836
994
    def is_compatible(cls, source, target):
837
 
        return (isinstance(source, GitRevisionTree) and
838
 
                isinstance(target, GitRevisionTree))
 
995
        return isinstance(source, GitTree) and isinstance(target, GitTree)
839
996
 
840
997
    def compare(self, want_unchanged=False, specific_files=None,
841
998
                extra_trees=None, require_versioned=False, include_root=False,
842
999
                want_unversioned=False):
843
1000
        with self.lock_read():
844
 
            changes, target_extras = self._iter_git_changes(
 
1001
            changes, source_extras, target_extras = self._iter_git_changes(
845
1002
                want_unchanged=want_unchanged,
846
1003
                require_versioned=require_versioned,
847
1004
                specific_files=specific_files,
848
1005
                extra_trees=extra_trees,
849
1006
                want_unversioned=want_unversioned)
850
 
            source_fileid_map = self.source._fileid_map
851
 
            target_fileid_map = self.target._fileid_map
852
1007
            return tree_delta_from_git_changes(
853
 
                changes, self.target.mapping,
854
 
                (source_fileid_map, target_fileid_map),
 
1008
                changes, (self.source.mapping, self.target.mapping),
855
1009
                specific_files=specific_files,
856
 
                include_root=include_root, target_extras=target_extras)
 
1010
                include_root=include_root,
 
1011
                source_extras=source_extras, target_extras=target_extras)
857
1012
 
858
1013
    def iter_changes(self, include_unchanged=False, specific_files=None,
859
1014
                     pb=None, extra_trees=[], require_versioned=True,
860
1015
                     want_unversioned=False):
861
1016
        with self.lock_read():
862
 
            changes, target_extras = self._iter_git_changes(
 
1017
            changes, source_extras, target_extras = self._iter_git_changes(
863
1018
                want_unchanged=include_unchanged,
864
1019
                require_versioned=require_versioned,
865
1020
                specific_files=specific_files,
869
1024
                changes, self.target.mapping,
870
1025
                specific_files=specific_files,
871
1026
                include_unchanged=include_unchanged,
 
1027
                source_extras=source_extras,
872
1028
                target_extras=target_extras)
873
1029
 
874
1030
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
875
1031
                          require_versioned=False, extra_trees=None,
876
 
                          want_unversioned=False):
877
 
        raise NotImplementedError(self._iter_git_changes)
878
 
 
879
 
 
880
 
class InterGitRevisionTrees(InterGitTrees):
881
 
    """InterTree that works between two git revision trees."""
882
 
 
883
 
    _matching_from_tree_format = None
884
 
    _matching_to_tree_format = None
885
 
    _test_mutable_trees_to_test_trees = None
886
 
 
887
 
    @classmethod
888
 
    def is_compatible(cls, source, target):
889
 
        return (isinstance(source, GitRevisionTree) and
890
 
                isinstance(target, GitRevisionTree))
891
 
 
892
 
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
893
 
                          require_versioned=True, extra_trees=None,
894
 
                          want_unversioned=False):
 
1032
                          want_unversioned=False, include_trees=True):
895
1033
        trees = [self.source]
896
1034
        if extra_trees is not None:
897
1035
            trees.extend(extra_trees)
899
1037
            specific_files = self.target.find_related_paths_across_trees(
900
1038
                specific_files, trees,
901
1039
                require_versioned=require_versioned)
902
 
 
903
 
        if (self.source._repository._git.object_store !=
904
 
                self.target._repository._git.object_store):
905
 
            store = OverlayObjectStore(
906
 
                [self.source._repository._git.object_store,
907
 
                    self.target._repository._git.object_store])
908
 
        else:
909
 
            store = self.source._repository._git.object_store
910
 
        return store.tree_changes(
911
 
            self.source.tree, self.target.tree, want_unchanged=want_unchanged,
912
 
            include_trees=True, change_type_same=True), set()
913
 
 
914
 
 
915
 
_mod_tree.InterTree.register_optimiser(InterGitRevisionTrees)
916
 
 
917
 
 
918
 
class MutableGitIndexTree(mutabletree.MutableTree):
 
1040
        # TODO(jelmer): Restrict to specific_files, for performance reasons.
 
1041
        with self.lock_read():
 
1042
            from_tree_sha, from_extras = self.source.git_snapshot(
 
1043
                want_unversioned=want_unversioned)
 
1044
            to_tree_sha, to_extras = self.target.git_snapshot(
 
1045
                want_unversioned=want_unversioned)
 
1046
            changes = tree_changes(
 
1047
                self.store, from_tree_sha, to_tree_sha,
 
1048
                include_trees=include_trees,
 
1049
                rename_detector=self.rename_detector,
 
1050
                want_unchanged=want_unchanged, change_type_same=True)
 
1051
            return changes, from_extras, to_extras
 
1052
 
 
1053
    def find_target_path(self, path, recurse='none'):
 
1054
        ret = self.find_target_paths([path], recurse=recurse)
 
1055
        return ret[path]
 
1056
 
 
1057
    def find_source_path(self, path, recurse='none'):
 
1058
        ret = self.find_source_paths([path], recurse=recurse)
 
1059
        return ret[path]
 
1060
 
 
1061
    def find_target_paths(self, paths, recurse='none'):
 
1062
        paths = set(paths)
 
1063
        ret = {}
 
1064
        changes = self._iter_git_changes(
 
1065
            specific_files=paths, include_trees=False)[0]
 
1066
        for (change_type, old, new) in changes:
 
1067
            if old[0] is None:
 
1068
                continue
 
1069
            oldpath = decode_git_path(old[0])
 
1070
            if oldpath in paths:
 
1071
                ret[oldpath] = decode_git_path(new[0]) if new[0] else None
 
1072
        for path in paths:
 
1073
            if path not in ret:
 
1074
                if self.source.has_filename(path):
 
1075
                    if self.target.has_filename(path):
 
1076
                        ret[path] = path
 
1077
                    else:
 
1078
                        ret[path] = None
 
1079
                else:
 
1080
                    raise errors.NoSuchFile(path)
 
1081
        return ret
 
1082
 
 
1083
    def find_source_paths(self, paths, recurse='none'):
 
1084
        paths = set(paths)
 
1085
        ret = {}
 
1086
        changes = self._iter_git_changes(
 
1087
            specific_files=paths, include_trees=False)[0]
 
1088
        for (change_type, old, new) in changes:
 
1089
            if new[0] is None:
 
1090
                continue
 
1091
            newpath = decode_git_path(new[0])
 
1092
            if newpath in paths:
 
1093
                ret[newpath] = decode_git_path(old[0]) if old[0] else None
 
1094
        for path in paths:
 
1095
            if path not in ret:
 
1096
                if self.target.has_filename(path):
 
1097
                    if self.source.has_filename(path):
 
1098
                        ret[path] = path
 
1099
                    else:
 
1100
                        ret[path] = None
 
1101
                else:
 
1102
                    raise errors.NoSuchFile(path)
 
1103
        return ret
 
1104
 
 
1105
 
 
1106
_mod_tree.InterTree.register_optimiser(InterGitTrees)
 
1107
 
 
1108
 
 
1109
class MutableGitIndexTree(mutabletree.MutableTree, GitTree):
919
1110
 
920
1111
    def __init__(self):
921
1112
        self._lock_mode = None
922
1113
        self._lock_count = 0
923
1114
        self._versioned_dirs = None
924
1115
        self._index_dirty = False
 
1116
        self._submodules = None
 
1117
 
 
1118
    def git_snapshot(self, want_unversioned=False):
 
1119
        return snapshot_workingtree(self, want_unversioned=want_unversioned)
925
1120
 
926
1121
    def is_versioned(self, path):
927
1122
        with self.lock_read():
928
 
            path = path.rstrip('/').encode('utf-8')
 
1123
            path = encode_git_path(path.rstrip('/'))
929
1124
            (index, subpath) = self._lookup_index(path)
930
1125
            return (subpath in index or self._has_dir(path))
931
1126
 
942
1137
        if self._lock_mode is None:
943
1138
            raise errors.ObjectNotLocked(self)
944
1139
        self._versioned_dirs = set()
945
 
        # TODO(jelmer): Browse over all indexes
946
 
        for p, i in self._recurse_index_entries():
 
1140
        for p, sha, mode in self.iter_git_objects():
947
1141
            self._ensure_versioned_dir(posixpath.dirname(p))
948
1142
 
949
1143
    def _ensure_versioned_dir(self, dirname):
959
1153
        with self.lock_read():
960
1154
            path = path.rstrip('/')
961
1155
            if self.is_versioned(path.rstrip('/')):
962
 
                return self._fileid_map.lookup_file_id(
 
1156
                return self.mapping.generate_file_id(
963
1157
                    osutils.safe_unicode(path))
964
1158
            return None
965
1159
 
966
 
    def has_id(self, file_id):
967
 
        try:
968
 
            self.id2path(file_id)
969
 
        except errors.NoSuchId:
970
 
            return False
971
 
        else:
972
 
            return True
973
 
 
974
 
    def id2path(self, file_id):
 
1160
    def id2path(self, file_id, recurse='down'):
975
1161
        if file_id is None:
976
1162
            return ''
977
1163
        if type(file_id) is not bytes:
978
1164
            raise TypeError(file_id)
979
1165
        with self.lock_read():
980
1166
            try:
981
 
                path = self._fileid_map.lookup_path(file_id)
 
1167
                path = self.mapping.parse_file_id(file_id)
982
1168
            except ValueError:
983
1169
                raise errors.NoSuchId(self, file_id)
984
1170
            if self.is_versioned(path):
986
1172
            raise errors.NoSuchId(self, file_id)
987
1173
 
988
1174
    def _set_root_id(self, file_id):
989
 
        self._fileid_map.set_file_id("", file_id)
990
 
 
991
 
    def get_root_id(self):
992
 
        return self.path2id(u"")
 
1175
        raise errors.UnsupportedOperation(self._set_root_id, self)
993
1176
 
994
1177
    def _add(self, files, ids, kinds):
995
1178
        for (path, file_id, kind) in zip(files, ids, kinds):
1006
1189
    def _lookup_index(self, encoded_path):
1007
1190
        if not isinstance(encoded_path, bytes):
1008
1191
            raise TypeError(encoded_path)
1009
 
        # TODO(jelmer): Look in other indexes
1010
 
        return self.index, encoded_path
 
1192
        # Common case:
 
1193
        if encoded_path in self.index:
 
1194
            return self.index, encoded_path
 
1195
        # TODO(jelmer): Perhaps have a cache with paths under which some
 
1196
        # submodules exist?
 
1197
        index = self.index
 
1198
        remaining_path = encoded_path
 
1199
        while True:
 
1200
            parts = remaining_path.split(b'/')
 
1201
            for i in range(1, len(parts)):
 
1202
                basepath = b'/'.join(parts[:i])
 
1203
                try:
 
1204
                    (ctime, mtime, dev, ino, mode, uid, gid, size, sha,
 
1205
                     flags) = index[basepath]
 
1206
                except KeyError:
 
1207
                    continue
 
1208
                else:
 
1209
                    if S_ISGITLINK(mode):
 
1210
                        index = self._get_submodule_index(basepath)
 
1211
                        remaining_path = b'/'.join(parts[i:])
 
1212
                        break
 
1213
                    else:
 
1214
                        return index, remaining_path
 
1215
            else:
 
1216
                return index, remaining_path
 
1217
        return index, remaining_path
1011
1218
 
1012
1219
    def _index_del_entry(self, index, path):
1013
1220
        del index[path]
1014
1221
        # TODO(jelmer): Keep track of dirty per index
1015
1222
        self._index_dirty = True
1016
1223
 
1017
 
    def _index_add_entry(self, path, kind, flags=0, reference_revision=None):
 
1224
    def _apply_index_changes(self, changes):
 
1225
        for (path, kind, executability, reference_revision,
 
1226
             symlink_target) in changes:
 
1227
            if kind is None or kind == 'directory':
 
1228
                (index, subpath) = self._lookup_index(
 
1229
                    encode_git_path(path))
 
1230
                try:
 
1231
                    self._index_del_entry(index, subpath)
 
1232
                except KeyError:
 
1233
                    pass
 
1234
                else:
 
1235
                    self._versioned_dirs = None
 
1236
            else:
 
1237
                self._index_add_entry(
 
1238
                    path, kind,
 
1239
                    reference_revision=reference_revision,
 
1240
                    symlink_target=symlink_target)
 
1241
        self.flush()
 
1242
 
 
1243
    def _index_add_entry(
 
1244
            self, path, kind, flags=0, reference_revision=None,
 
1245
            symlink_target=None):
1018
1246
        if kind == "directory":
1019
1247
            # Git indexes don't contain directories
1020
1248
            return
1021
 
        if kind == "file":
 
1249
        elif kind == "file":
1022
1250
            blob = Blob()
1023
1251
            try:
1024
1252
                file, stat_val = self.get_file_with_stat(path)
1043
1271
                # old index
1044
1272
                stat_val = os.stat_result(
1045
1273
                    (stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1046
 
            blob.set_raw_string(
1047
 
                self.get_symlink_target(path).encode("utf-8"))
 
1274
            if symlink_target is None:
 
1275
                symlink_target = self.get_symlink_target(path)
 
1276
            blob.set_raw_string(encode_git_path(symlink_target))
1048
1277
            # Add object to the repository if it didn't exist yet
1049
1278
            if blob.id not in self.store:
1050
1279
                self.store.add_object(blob)
1067
1296
            raise AssertionError("unknown kind '%s'" % kind)
1068
1297
        # Add an entry to the index or update the existing entry
1069
1298
        ensure_normalized_path(path)
1070
 
        encoded_path = path.encode("utf-8")
 
1299
        encoded_path = encode_git_path(path)
1071
1300
        if b'\r' in encoded_path or b'\n' in encoded_path:
1072
1301
            # TODO(jelmer): Why do we need to do this?
1073
1302
            trace.mutter('ignoring path with invalid newline in it: %r', path)
1078
1307
        if self._versioned_dirs is not None:
1079
1308
            self._ensure_versioned_dir(index_path)
1080
1309
 
1081
 
    def _recurse_index_entries(self, index=None, basepath=b""):
 
1310
    def iter_git_objects(self):
 
1311
        for p, entry in self._recurse_index_entries():
 
1312
            yield p, entry.sha, entry.mode
 
1313
 
 
1314
    def _recurse_index_entries(self, index=None, basepath=b"",
 
1315
                               recurse_nested=False):
1082
1316
        # Iterate over all index entries
1083
1317
        with self.lock_read():
1084
1318
            if index is None:
1085
1319
                index = self.index
1086
1320
            for path, value in index.items():
1087
 
                yield (posixpath.join(basepath, path), value)
1088
1321
                (ctime, mtime, dev, ino, mode, uid, gid, size, sha,
1089
1322
                 flags) = value
1090
 
                if S_ISGITLINK(mode):
1091
 
                    pass  # TODO(jelmer): dive into submodule
 
1323
                if S_ISGITLINK(mode) and recurse_nested:
 
1324
                    subindex = self._get_submodule_index(path)
 
1325
                    for entry in self._recurse_index_entries(
 
1326
                            index=subindex, basepath=path,
 
1327
                            recurse_nested=recurse_nested):
 
1328
                        yield entry
 
1329
                else:
 
1330
                    yield (posixpath.join(basepath, path), value)
1092
1331
 
1093
 
    def iter_entries_by_dir(self, specific_files=None, yield_parents=False):
1094
 
        if yield_parents:
1095
 
            raise NotImplementedError(self.iter_entries_by_dir)
 
1332
    def iter_entries_by_dir(self, specific_files=None,
 
1333
                            recurse_nested=False):
1096
1334
        with self.lock_read():
1097
1335
            if specific_files is not None:
1098
1336
                specific_files = set(specific_files)
1103
1341
            if specific_files is None or u"" in specific_files:
1104
1342
                ret[(u"", u"")] = root_ie
1105
1343
            dir_ids = {u"": root_ie.file_id}
1106
 
            for path, value in self._recurse_index_entries():
 
1344
            for path, value in self._recurse_index_entries(
 
1345
                    recurse_nested=recurse_nested):
1107
1346
                if self.mapping.is_special_file(path):
1108
1347
                    continue
1109
 
                path = path.decode("utf-8")
 
1348
                path = decode_git_path(path)
1110
1349
                if specific_files is not None and path not in specific_files:
1111
1350
                    continue
1112
1351
                (parent, name) = posixpath.split(path)
1114
1353
                    file_ie = self._get_file_ie(name, path, value, None)
1115
1354
                except errors.NoSuchFile:
1116
1355
                    continue
1117
 
                if yield_parents or specific_files is None:
 
1356
                if specific_files is None:
1118
1357
                    for (dir_path, dir_ie) in self._add_missing_parent_ids(
1119
1358
                            parent, dir_ids):
1120
1359
                        ret[(posixpath.dirname(dir_path), dir_path)] = dir_ie
1121
1360
                file_ie.parent_id = self.path2id(parent)
1122
1361
                ret[(posixpath.dirname(path), path)] = file_ie
1123
 
            return ((path, ie) for ((_, path), ie) in sorted(viewitems(ret)))
 
1362
            # Special casing for directories
 
1363
            if specific_files:
 
1364
                for path in specific_files:
 
1365
                    key = (posixpath.dirname(path), path)
 
1366
                    if key not in ret and self.is_versioned(path):
 
1367
                        ret[key] = self._get_dir_ie(path, self.path2id(key[0]))
 
1368
            return ((path, ie) for ((_, path), ie) in sorted(ret.items()))
1124
1369
 
1125
1370
    def iter_references(self):
1126
 
        # TODO(jelmer): Implement a more efficient version of this
1127
 
        for path, entry in self.iter_entries_by_dir():
1128
 
            if entry.kind == 'tree-reference':
1129
 
                yield path, self.mapping.generate_file_id(b'')
 
1371
        if self.supports_tree_reference():
 
1372
            # TODO(jelmer): Implement a more efficient version of this
 
1373
            for path, entry in self.iter_entries_by_dir():
 
1374
                if entry.kind == 'tree-reference':
 
1375
                    yield path
1130
1376
 
1131
1377
    def _get_dir_ie(self, path, parent_id):
1132
1378
        file_id = self.path2id(path)
1134
1380
                                posixpath.basename(path).strip("/"), parent_id)
1135
1381
 
1136
1382
    def _get_file_ie(self, name, path, value, parent_id):
1137
 
        if not isinstance(name, text_type):
 
1383
        if not isinstance(name, str):
1138
1384
            raise TypeError(name)
1139
 
        if not isinstance(path, text_type):
 
1385
        if not isinstance(path, str):
1140
1386
            raise TypeError(path)
1141
1387
        if not isinstance(value, tuple) or len(value) != 10:
1142
1388
            raise TypeError(value)
1151
1397
        elif kind == 'tree-reference':
1152
1398
            ie.reference_revision = self.get_reference_revision(path)
1153
1399
        else:
1154
 
            try:
1155
 
                data = self.get_file_text(path)
1156
 
            except errors.NoSuchFile:
1157
 
                data = None
1158
 
            except IOError as e:
1159
 
                if e.errno != errno.ENOENT:
1160
 
                    raise
1161
 
                data = None
1162
 
            if data is None:
1163
 
                data = self.branch.repository._git.object_store[sha].data
1164
 
            ie.text_sha1 = osutils.sha_string(data)
1165
 
            ie.text_size = len(data)
 
1400
            ie.git_sha1 = sha
 
1401
            ie.text_size = size
1166
1402
            ie.executable = bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
1167
1403
        return ie
1168
1404
 
1185
1421
    def _unversion_path(self, path):
1186
1422
        if self._lock_mode is None:
1187
1423
            raise errors.ObjectNotLocked(self)
1188
 
        encoded_path = path.encode("utf-8")
 
1424
        encoded_path = encode_git_path(path)
1189
1425
        count = 0
1190
1426
        (index, subpath) = self._lookup_index(encoded_path)
1191
1427
        try:
1218
1454
        for (old_path, new_path, file_id, ie) in delta:
1219
1455
            if old_path is not None:
1220
1456
                (index, old_subpath) = self._lookup_index(
1221
 
                    old_path.encode('utf-8'))
 
1457
                    encode_git_path(old_path))
1222
1458
                if old_subpath in index:
1223
1459
                    self._index_del_entry(index, old_subpath)
1224
1460
                    self._versioned_dirs = None
1244
1480
            return rename_tuples
1245
1481
 
1246
1482
    def rename_one(self, from_rel, to_rel, after=None):
1247
 
        from_path = from_rel.encode("utf-8")
 
1483
        from_path = encode_git_path(from_rel)
1248
1484
        to_rel, can_access = osutils.normalized_filename(to_rel)
1249
1485
        if not can_access:
1250
1486
            raise errors.InvalidNormalization(to_rel)
1251
 
        to_path = to_rel.encode("utf-8")
 
1487
        to_path = encode_git_path(to_rel)
1252
1488
        with self.lock_tree_write():
1253
1489
            if not after:
1254
1490
                # Perhaps it's already moved?
1329
1565
            self._versioned_dirs = None
1330
1566
            self.flush()
1331
1567
 
1332
 
    def find_related_paths_across_trees(self, paths, trees=[],
1333
 
                                        require_versioned=True):
1334
 
        if paths is None:
1335
 
            return None
1336
 
 
1337
 
        if require_versioned:
1338
 
            trees = [self] + (trees if trees is not None else [])
1339
 
            unversioned = set()
1340
 
            for p in paths:
1341
 
                for t in trees:
1342
 
                    if t.is_versioned(p):
1343
 
                        break
1344
 
                else:
1345
 
                    unversioned.add(p)
1346
 
            if unversioned:
1347
 
                raise errors.PathsNotVersionedError(unversioned)
1348
 
 
1349
 
        return filter(self.is_versioned, paths)
1350
 
 
1351
1568
    def path_content_summary(self, path):
1352
1569
        """See Tree.path_content_summary."""
1353
1570
        try:
1373
1590
        else:
1374
1591
            return (kind, None, None, None)
1375
1592
 
 
1593
    def stored_kind(self, relpath):
 
1594
        if relpath == '':
 
1595
            return 'directory'
 
1596
        (index, index_path) = self._lookup_index(encode_git_path(relpath))
 
1597
        if index is None:
 
1598
            return None
 
1599
        try:
 
1600
            mode = index[index_path].mode
 
1601
        except KeyError:
 
1602
            for p in index:
 
1603
                if osutils.is_inside(
 
1604
                        decode_git_path(index_path), decode_git_path(p)):
 
1605
                    return 'directory'
 
1606
            return None
 
1607
        else:
 
1608
            return mode_kind(mode)
 
1609
 
1376
1610
    def kind(self, relpath):
1377
1611
        kind = osutils.file_kind(self.abspath(relpath))
1378
1612
        if kind == 'directory':
1379
 
            (index, index_path) = self._lookup_index(relpath.encode('utf-8'))
1380
 
            if index is None:
1381
 
                return kind
1382
 
            try:
1383
 
                mode = index[index_path].mode
1384
 
            except KeyError:
1385
 
                return kind
1386
 
            else:
1387
 
                if S_ISGITLINK(mode):
1388
 
                    return 'tree-reference'
1389
 
                return 'directory'
 
1613
            if self._directory_is_tree_reference(relpath):
 
1614
                return 'tree-reference'
 
1615
            return 'directory'
1390
1616
        else:
1391
1617
            return kind
1392
1618
 
1393
1619
    def _live_entry(self, relpath):
1394
1620
        raise NotImplementedError(self._live_entry)
1395
1621
 
1396
 
 
1397
 
class InterIndexGitTree(InterGitTrees):
1398
 
    """InterTree that works between a Git revision tree and an index."""
1399
 
 
1400
 
    def __init__(self, source, target):
1401
 
        super(InterIndexGitTree, self).__init__(source, target)
1402
 
        self._index = target.index
1403
 
 
1404
 
    @classmethod
1405
 
    def is_compatible(cls, source, target):
1406
 
        return (isinstance(source, GitRevisionTree) and
1407
 
                isinstance(target, MutableGitIndexTree))
1408
 
 
1409
 
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1410
 
                          require_versioned=False, extra_trees=None,
1411
 
                          want_unversioned=False):
1412
 
        trees = [self.source]
1413
 
        if extra_trees is not None:
1414
 
            trees.extend(extra_trees)
1415
 
        if specific_files is not None:
1416
 
            specific_files = self.target.find_related_paths_across_trees(
1417
 
                specific_files, trees,
1418
 
                require_versioned=require_versioned)
1419
 
        # TODO(jelmer): Restrict to specific_files, for performance reasons.
 
1622
    def transform(self, pb=None):
 
1623
        from .transform import GitTreeTransform
 
1624
        return GitTreeTransform(self, pb=pb)
 
1625
 
 
1626
    def has_changes(self, _from_tree=None):
 
1627
        """Quickly check that the tree contains at least one commitable change.
 
1628
 
 
1629
        :param _from_tree: tree to compare against to find changes (default to
 
1630
            the basis tree and is intended to be used by tests).
 
1631
 
 
1632
        :return: True if a change is found. False otherwise
 
1633
        """
1420
1634
        with self.lock_read():
1421
 
            return changes_between_git_tree_and_working_copy(
1422
 
                self.source.store, self.source.tree,
1423
 
                self.target, want_unchanged=want_unchanged,
1424
 
                want_unversioned=want_unversioned)
1425
 
 
1426
 
 
1427
 
_mod_tree.InterTree.register_optimiser(InterIndexGitTree)
1428
 
 
1429
 
 
1430
 
def changes_between_git_tree_and_working_copy(store, from_tree_sha, target,
1431
 
                                              want_unchanged=False,
1432
 
                                              want_unversioned=False):
1433
 
    """Determine the changes between a git tree and a working tree with index.
1434
 
 
1435
 
    """
 
1635
            # Check pending merges
 
1636
            if len(self.get_parent_ids()) > 1:
 
1637
                return True
 
1638
            if _from_tree is None:
 
1639
                _from_tree = self.basis_tree()
 
1640
            changes = self.iter_changes(_from_tree)
 
1641
            if self.supports_symlinks():
 
1642
                # Fast path for has_changes.
 
1643
                try:
 
1644
                    change = next(changes)
 
1645
                    if change.path[1] == '':
 
1646
                        next(changes)
 
1647
                    return True
 
1648
                except StopIteration:
 
1649
                    # No changes
 
1650
                    return False
 
1651
            else:
 
1652
                # Slow path for has_changes.
 
1653
                # Handle platforms that do not support symlinks in the
 
1654
                # conditional below. This is slower than the try/except
 
1655
                # approach below that but we don't have a choice as we
 
1656
                # need to be sure that all symlinks are removed from the
 
1657
                # entire changeset. This is because in platforms that
 
1658
                # do not support symlinks, they show up as None in the
 
1659
                # working copy as compared to the repository.
 
1660
                # Also, exclude root as mention in the above fast path.
 
1661
                changes = filter(
 
1662
                    lambda c: c[6][0] != 'symlink' and c[4] != (None, None),
 
1663
                    changes)
 
1664
                try:
 
1665
                    next(iter(changes))
 
1666
                except StopIteration:
 
1667
                    return False
 
1668
                return True
 
1669
 
 
1670
 
 
1671
def snapshot_workingtree(target, want_unversioned=False):
1436
1672
    extras = set()
1437
1673
    blobs = {}
1438
1674
    # Report dirified directories to commit_tree first, so that they can be
1439
1675
    # replaced with non-empty directories if they have contents.
1440
1676
    dirified = []
 
1677
    trust_executable = target._supports_executable()
1441
1678
    for path, index_entry in target._recurse_index_entries():
1442
1679
        try:
1443
1680
            live_entry = target._live_entry(path)
1445
1682
            if e.errno == errno.ENOENT:
1446
1683
                # Entry was removed; keep it listed, but mark it as gone.
1447
1684
                blobs[path] = (ZERO_SHA, 0)
1448
 
            elif e.errno == errno.EISDIR:
1449
 
                # TODO(jelmer): Only do this if 'path' appears in .gitmodules?
 
1685
            else:
 
1686
                raise
 
1687
        else:
 
1688
            if live_entry is None:
 
1689
                # Entry was turned into a directory.
 
1690
                # Maybe it's just a submodule that's not checked out?
1450
1691
                if S_ISGITLINK(index_entry.mode):
1451
1692
                    blobs[path] = (index_entry.sha, index_entry.mode)
1452
1693
                else:
1453
 
                    # Entry was turned into a directory
1454
1694
                    dirified.append((path, Tree().id, stat.S_IFDIR))
1455
 
                    store.add_object(Tree())
 
1695
                    target.store.add_object(Tree())
1456
1696
            else:
1457
 
                raise
1458
 
        else:
1459
 
            blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
 
1697
                mode = live_entry.mode
 
1698
                if not trust_executable:
 
1699
                    if mode_is_executable(index_entry.mode):
 
1700
                        mode |= 0o111
 
1701
                    else:
 
1702
                        mode &= ~0o111
 
1703
                if live_entry.sha != index_entry.sha:
 
1704
                    rp = decode_git_path(path)
 
1705
                    if stat.S_ISREG(live_entry.mode):
 
1706
                        blob = Blob()
 
1707
                        with target.get_file(rp) as f:
 
1708
                            blob.data = f.read()
 
1709
                    elif stat.S_ISLNK(live_entry.mode):
 
1710
                        blob = Blob()
 
1711
                        blob.data = target.get_symlink_target(rp).encode(osutils._fs_enc)
 
1712
                    else:
 
1713
                        blob = None
 
1714
                    if blob is not None:
 
1715
                        target.store.add_object(blob)
 
1716
                blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
1460
1717
    if want_unversioned:
1461
 
        for e in target.extras():
1462
 
            st = target._lstat(e)
 
1718
        for extra in target._iter_files_recursive(include_dirs=False):
1463
1719
            try:
1464
 
                np, accessible = osutils.normalized_filename(e)
 
1720
                extra, accessible = osutils.normalized_filename(extra)
1465
1721
            except UnicodeDecodeError:
1466
1722
                raise errors.BadFilenameEncoding(
1467
 
                    e, osutils._fs_enc)
 
1723
                    extra, osutils._fs_enc)
 
1724
            np = encode_git_path(extra)
 
1725
            if np in blobs:
 
1726
                continue
 
1727
            st = target._lstat(extra)
1468
1728
            if stat.S_ISDIR(st.st_mode):
1469
1729
                blob = Tree()
1470
 
            else:
 
1730
            elif stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode):
1471
1731
                blob = blob_from_path_and_stat(
1472
 
                    target.abspath(e).encode(osutils._fs_enc), st)
1473
 
            store.add_object(blob)
1474
 
            np = np.encode('utf-8')
 
1732
                    target.abspath(extra).encode(osutils._fs_enc), st)
 
1733
            else:
 
1734
                continue
 
1735
            target.store.add_object(blob)
1475
1736
            blobs[np] = (blob.id, cleanup_mode(st.st_mode))
1476
1737
            extras.add(np)
1477
 
    to_tree_sha = commit_tree(
1478
 
        store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()])
1479
 
    return store.tree_changes(
1480
 
        from_tree_sha, to_tree_sha, include_trees=True,
1481
 
        want_unchanged=want_unchanged, change_type_same=True), extras
 
1738
    return commit_tree(
 
1739
        target.store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()]), extras