/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-03-22 20:02:36 UTC
  • mto: (7490.7.7 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200322200236-fsbl91ktcn6fcbdd
Fix tests.

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
 
20
22
from collections import deque
21
23
import errno
22
24
from io import BytesIO
26
28
    parse_submodules,
27
29
    ConfigFile as GitConfigFile,
28
30
    )
29
 
from dulwich.diff_tree import tree_changes, RenameDetector
 
31
from dulwich.diff_tree import tree_changes
30
32
from dulwich.errors import NotTreeError
31
33
from dulwich.index import (
32
34
    blob_from_path_and_stat,
64
66
    CURRENT_REVISION,
65
67
    NULL_REVISION,
66
68
    )
 
69
from ..sixish import (
 
70
    text_type,
 
71
    viewitems,
 
72
    )
67
73
 
68
74
from .mapping import (
69
 
    encode_git_path,
70
 
    decode_git_path,
71
75
    mode_is_executable,
72
76
    mode_kind,
73
77
    default_mapping,
298
302
            info = self._submodule_info()[relpath]
299
303
        except KeyError:
300
304
            nested_repo_transport = self._repository.controldir.user_transport.clone(
301
 
                decode_git_path(relpath))
 
305
                relpath.decode('utf-8'))
302
306
        else:
303
307
            nested_repo_transport = self._repository.controldir.control_transport.clone(
304
 
                posixpath.join('modules', decode_git_path(info[1])))
 
308
                posixpath.join('modules', info[1].decode('utf-8')))
305
309
        nested_controldir = _mod_controldir.ControlDir.open_from_transport(
306
310
            nested_repo_transport)
307
311
        return nested_controldir.find_repository()
310
314
        return self._get_submodule_repository(relpath)._git.object_store
311
315
 
312
316
    def get_nested_tree(self, path):
313
 
        encoded_path = encode_git_path(path)
 
317
        encoded_path = path.encode('utf-8')
314
318
        nested_repo = self._get_submodule_repository(encoded_path)
315
319
        ref_rev = self.get_reference_revision(path)
316
320
        return nested_repo.revision_tree(ref_rev)
323
327
        if self.commit_id == ZERO_SHA:
324
328
            return NULL_REVISION
325
329
        (unused_path, commit_id) = change_scanner.find_last_change_revision(
326
 
            encode_git_path(path), self.commit_id)
 
330
            path.encode('utf-8'), self.commit_id)
327
331
        return self._repository.lookup_foreign_revision_id(
328
332
            commit_id, self.mapping)
329
333
 
370
374
            tree = store[tree_id]
371
375
            for name, mode, hexsha in tree.items():
372
376
                subpath = posixpath.join(path, name)
373
 
                ret.add(decode_git_path(subpath))
 
377
                ret.add(subpath.decode('utf-8'))
374
378
                if stat.S_ISDIR(mode):
375
379
                    todo.append((store, subpath, hexsha))
376
380
        return ret
379
383
        if self.tree is None:
380
384
            raise errors.NoSuchFile(path)
381
385
 
382
 
        encoded_path = encode_git_path(path)
 
386
        encoded_path = path.encode('utf-8')
383
387
        parts = encoded_path.split(b'/')
384
388
        hexsha = self.tree
385
389
        store = self.store
446
450
            parent_path = posixpath.dirname(from_dir)
447
451
            parent_id = self.mapping.generate_file_id(parent_path)
448
452
            if mode_kind(mode) == 'directory':
449
 
                root_ie = self._get_dir_ie(encode_git_path(from_dir), parent_id)
 
453
                root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
450
454
            else:
451
455
                root_ie = self._get_file_ie(
452
 
                    store, encode_git_path(from_dir),
 
456
                    store, from_dir.encode("utf-8"),
453
457
                    posixpath.basename(from_dir), mode, hexsha)
454
458
        if include_root:
455
459
            yield (from_dir, "V", root_ie.kind, root_ie)
456
460
        todo = []
457
461
        if root_ie.kind == 'directory':
458
 
            todo.append((store, encode_git_path(from_dir),
 
462
            todo.append((store, from_dir.encode("utf-8"),
459
463
                         b"", hexsha, root_ie.file_id))
460
464
        while todo:
461
465
            (store, path, relpath, hexsha, parent_id) = todo.pop()
478
482
                else:
479
483
                    ie = self._get_file_ie(
480
484
                        store, child_path, name, mode, hexsha, parent_id)
481
 
                yield (decode_git_path(child_relpath), "V", ie.kind, ie)
 
485
                yield (child_relpath.decode('utf-8'), "V", ie.kind, ie)
482
486
 
483
487
    def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
484
488
        if not isinstance(path, bytes):
486
490
        if not isinstance(name, bytes):
487
491
            raise TypeError(name)
488
492
        kind = mode_kind(mode)
489
 
        path = decode_git_path(path)
490
 
        name = decode_git_path(name)
 
493
        path = path.decode('utf-8')
 
494
        name = name.decode("utf-8")
491
495
        file_id = self.mapping.generate_file_id(path)
492
496
        ie = entry_factory[kind](file_id, name, parent_id)
493
497
        if kind == 'symlink':
494
 
            ie.symlink_target = decode_git_path(store[hexsha].data)
 
498
            ie.symlink_target = store[hexsha].data.decode('utf-8')
495
499
        elif kind == 'tree-reference':
496
500
            ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(
497
501
                hexsha)
503
507
        return ie
504
508
 
505
509
    def _get_dir_ie(self, path, parent_id):
506
 
        path = decode_git_path(path)
 
510
        path = path.decode('utf-8')
507
511
        file_id = self.mapping.generate_file_id(path)
508
512
        return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
509
513
 
513
517
        if mode is not None and not stat.S_ISDIR(mode):
514
518
            return
515
519
 
516
 
        encoded_path = encode_git_path(path)
 
520
        encoded_path = path.encode('utf-8')
517
521
        file_id = self.path2id(path)
518
522
        tree = store[tree_sha]
519
523
        for name, mode, hexsha in tree.iteritems():
534
538
            if specific_files in ([""], []):
535
539
                specific_files = None
536
540
            else:
537
 
                specific_files = set([encode_git_path(p)
 
541
                specific_files = set([p.encode('utf-8')
538
542
                                      for p in specific_files])
539
543
        todo = deque([(self.store, b"", self.tree, self.path2id(''))])
540
544
        if specific_files is None or u"" in specific_files:
547
551
                if self.mapping.is_special_file(name):
548
552
                    continue
549
553
                child_path = posixpath.join(path, name)
550
 
                child_path_decoded = decode_git_path(child_path)
 
554
                child_path_decoded = child_path.decode('utf-8')
551
555
                if recurse_nested and S_ISGITLINK(mode):
552
556
                    mode = stat.S_IFDIR
553
557
                    store = self._get_submodule_store(child_path)
606
610
        """See RevisionTree.get_symlink_target."""
607
611
        (store, mode, hexsha) = self._lookup_path(path)
608
612
        if stat.S_ISLNK(mode):
609
 
            return decode_git_path(store[hexsha].data)
 
613
            return store[hexsha].data.decode('utf-8')
610
614
        else:
611
615
            return None
612
616
 
615
619
        (store, mode, hexsha) = self._lookup_path(path)
616
620
        if S_ISGITLINK(mode):
617
621
            try:
618
 
                nested_repo = self._get_submodule_repository(encode_git_path(path))
 
622
                nested_repo = self._get_submodule_repository(path.encode('utf-8'))
619
623
            except errors.NotBranchError:
620
624
                return self.mapping.revision_id_foreign_to_bzr(hexsha)
621
625
            else:
641
645
            return (kind, len(contents), executable,
642
646
                    osutils.sha_string(contents))
643
647
        elif kind == 'symlink':
644
 
            return (kind, None, None, decode_git_path(store[hexsha].data))
 
648
            return (kind, None, None, store[hexsha].data.decode('utf-8'))
645
649
        elif kind == 'tree-reference':
646
 
            nested_repo = self._get_submodule_repository(encode_git_path(path))
 
650
            nested_repo = self._get_submodule_repository(path.encode('utf-8'))
647
651
            return (kind, None, None,
648
652
                    nested_repo.lookup_foreign_revision_id(hexsha))
649
653
        else:
699
703
    def walkdirs(self, prefix=u""):
700
704
        (store, mode, hexsha) = self._lookup_path(prefix)
701
705
        todo = deque(
702
 
            [(store, encode_git_path(prefix), hexsha, self.path2id(prefix))])
 
706
            [(store, prefix.encode('utf-8'), hexsha, self.path2id(prefix))])
703
707
        while todo:
704
708
            store, path, tree_sha, parent_id = todo.popleft()
705
 
            path_decoded = decode_git_path(path)
 
709
            path_decoded = path.decode('utf-8')
706
710
            tree = store[tree_sha]
707
711
            children = []
708
712
            for name, mode, hexsha in tree.iteritems():
709
713
                if self.mapping.is_special_file(name):
710
714
                    continue
711
715
                child_path = posixpath.join(path, name)
712
 
                file_id = self.path2id(decode_git_path(child_path))
 
716
                file_id = self.path2id(child_path.decode('utf-8'))
713
717
                if stat.S_ISDIR(mode):
714
718
                    todo.append((store, child_path, hexsha, file_id))
715
719
                children.append(
716
 
                    (decode_git_path(child_path), decode_git_path(name),
 
720
                    (child_path.decode('utf-8'), name.decode('utf-8'),
717
721
                        mode_kind(mode), None,
718
722
                        file_id, mode_kind(mode)))
719
723
            yield (path_decoded, parent_id), children
720
724
 
721
 
    def preview_transform(self, pb=None):
722
 
        from .transform import GitTransformPreview
723
 
        return GitTransformPreview(self, pb=pb)
724
 
 
725
725
 
726
726
def tree_delta_from_git_changes(changes, mappings,
727
727
                                specific_files=None,
728
728
                                require_versioned=False, include_root=False,
729
 
                                source_extras=None, target_extras=None):
 
729
                                target_extras=None):
730
730
    """Create a TreeDelta from two git trees.
731
731
 
732
732
    source and target are iterators over tuples with:
735
735
    (old_mapping, new_mapping) = mappings
736
736
    if target_extras is None:
737
737
        target_extras = set()
738
 
    if source_extras is None:
739
 
        source_extras = set()
740
738
    ret = delta.TreeDelta()
741
739
    added = []
742
 
    for (change_type, old, new) in changes:
743
 
        (oldpath, oldmode, oldsha) = old
744
 
        (newpath, newmode, newsha) = new
 
740
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
745
741
        if newpath == b'' and not include_root:
746
742
            continue
747
 
        copied = (change_type == 'copy')
748
743
        if oldpath is not None:
749
 
            oldpath_decoded = decode_git_path(oldpath)
 
744
            oldpath_decoded = oldpath.decode('utf-8')
750
745
        else:
751
746
            oldpath_decoded = None
752
747
        if newpath is not None:
753
 
            newpath_decoded = decode_git_path(newpath)
 
748
            newpath_decoded = newpath.decode('utf-8')
754
749
        else:
755
750
            newpath_decoded = None
756
751
        if not (specific_files is None or
762
757
                        specific_files, newpath_decoded))):
763
758
            continue
764
759
 
765
 
        if oldpath is None:
 
760
        if oldpath_decoded is None:
 
761
            fileid = new_mapping.generate_file_id(newpath_decoded)
766
762
            oldexe = None
767
763
            oldkind = None
768
764
            oldname = None
769
765
            oldparent = None
770
766
            oldversioned = False
771
767
        else:
772
 
            oldversioned = (oldpath not in source_extras)
 
768
            oldversioned = True
773
769
            if oldmode:
774
770
                oldexe = mode_is_executable(oldmode)
775
771
                oldkind = mode_kind(oldmode)
776
772
            else:
777
773
                oldexe = False
778
774
                oldkind = None
779
 
            if oldpath == b'':
 
775
            if oldpath_decoded == u'':
780
776
                oldparent = None
781
777
                oldname = u''
782
778
            else:
783
779
                (oldparentpath, oldname) = osutils.split(oldpath_decoded)
784
780
                oldparent = old_mapping.generate_file_id(oldparentpath)
785
 
        if newpath is None:
 
781
            fileid = old_mapping.generate_file_id(oldpath_decoded)
 
782
        if newpath_decoded is None:
786
783
            newexe = None
787
784
            newkind = None
788
785
            newname = None
789
786
            newparent = None
790
787
            newversioned = False
791
788
        else:
792
 
            newversioned = (newpath not in target_extras)
 
789
            newversioned = (newpath_decoded not in target_extras)
793
790
            if newmode:
794
791
                newexe = mode_is_executable(newmode)
795
792
                newkind = mode_kind(newmode)
802
799
            else:
803
800
                newparentpath, newname = osutils.split(newpath_decoded)
804
801
                newparent = new_mapping.generate_file_id(newparentpath)
805
 
        if oldversioned and not copied:
806
 
            fileid = old_mapping.generate_file_id(oldpath_decoded)
807
 
        elif newversioned:
808
 
            fileid = new_mapping.generate_file_id(newpath_decoded)
809
 
        else:
810
 
            fileid = None
811
802
        if old_mapping.is_special_file(oldpath):
812
803
            oldpath = None
813
804
        if new_mapping.is_special_file(newpath):
818
809
            fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
819
810
            (oldversioned, newversioned),
820
811
            (oldparent, newparent), (oldname, newname),
821
 
            (oldkind, newkind), (oldexe, newexe),
822
 
            copied=copied)
823
 
        if newpath is not None and not newversioned and newkind != 'directory':
824
 
            change.file_id = None
825
 
            ret.unversioned.append(change)
826
 
        elif change_type == 'add':
 
812
            (oldkind, newkind), (oldexe, newexe))
 
813
        if oldpath is None:
827
814
            added.append((newpath, newkind))
828
815
        elif newpath is None or newmode == 0:
829
816
            ret.removed.append(change)
830
 
        elif change_type == 'delete':
831
 
            ret.removed.append(change)
832
 
        elif change_type == 'copy':
833
 
            if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
834
 
                continue
835
 
            ret.copied.append(change)
836
 
        elif change_type == 'rename':
837
 
            if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
838
 
                continue
 
817
        elif oldpath != newpath:
839
818
            ret.renamed.append(change)
840
819
        elif mode_kind(oldmode) != mode_kind(newmode):
841
820
            ret.kind_changed.append(change)
855
834
    for path, kind in added:
856
835
        if kind == 'directory' and path not in implicit_dirs:
857
836
            continue
858
 
        path_decoded = decode_git_path(path)
 
837
        path_decoded = osutils.normalized_filename(path)[0]
859
838
        parent_path, basename = osutils.split(path_decoded)
860
839
        parent_id = new_mapping.generate_file_id(parent_path)
861
 
        file_id = new_mapping.generate_file_id(path_decoded)
862
 
        ret.added.append(
863
 
            _mod_tree.TreeChange(
864
 
                file_id, (None, path_decoded), True,
865
 
                (False, True),
866
 
                (None, parent_id),
 
840
        if path in target_extras:
 
841
            ret.unversioned.append(_mod_tree.TreeChange(
 
842
                None, (None, path_decoded),
 
843
                True, (False, False), (None, parent_id),
867
844
                (None, basename), (None, kind), (None, False)))
 
845
        else:
 
846
            file_id = new_mapping.generate_file_id(path_decoded)
 
847
            ret.added.append(
 
848
                _mod_tree.TreeChange(
 
849
                    file_id, (None, path_decoded), True,
 
850
                    (False, True),
 
851
                    (None, parent_id),
 
852
                    (None, basename), (None, kind), (None, False)))
868
853
 
869
854
    return ret
870
855
 
871
856
 
872
857
def changes_from_git_changes(changes, mapping, specific_files=None,
873
 
                             include_unchanged=False, source_extras=None,
874
 
                             target_extras=None):
 
858
                             include_unchanged=False, target_extras=None):
875
859
    """Create a iter_changes-like generator from a git stream.
876
860
 
877
861
    source and target are iterators over tuples with:
879
863
    """
880
864
    if target_extras is None:
881
865
        target_extras = set()
882
 
    if source_extras is None:
883
 
        source_extras = set()
884
 
    for (change_type, old, new) in changes:
885
 
        if change_type == 'unchanged' and not include_unchanged:
886
 
            continue
887
 
        (oldpath, oldmode, oldsha) = old
888
 
        (newpath, newmode, newsha) = new
 
866
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
889
867
        if oldpath is not None:
890
 
            oldpath_decoded = decode_git_path(oldpath)
 
868
            oldpath_decoded = oldpath.decode('utf-8')
891
869
        else:
892
870
            oldpath_decoded = None
893
871
        if newpath is not None:
894
 
            newpath_decoded = decode_git_path(newpath)
 
872
            newpath_decoded = newpath.decode('utf-8')
895
873
        else:
896
874
            newpath_decoded = None
897
875
        if not (specific_files is None or
906
884
            continue
907
885
        if newpath is not None and mapping.is_special_file(newpath):
908
886
            continue
909
 
        if oldpath is None:
 
887
        if oldpath_decoded is None:
 
888
            fileid = mapping.generate_file_id(newpath_decoded)
910
889
            oldexe = None
911
890
            oldkind = None
912
891
            oldname = None
913
892
            oldparent = None
914
893
            oldversioned = False
915
894
        else:
916
 
            oldversioned = (oldpath not in source_extras)
 
895
            oldversioned = True
917
896
            if oldmode:
918
897
                oldexe = mode_is_executable(oldmode)
919
898
                oldkind = mode_kind(oldmode)
926
905
            else:
927
906
                (oldparentpath, oldname) = osutils.split(oldpath_decoded)
928
907
                oldparent = mapping.generate_file_id(oldparentpath)
929
 
        if newpath is None:
 
908
            fileid = mapping.generate_file_id(oldpath_decoded)
 
909
        if newpath_decoded is None:
930
910
            newexe = None
931
911
            newkind = None
932
912
            newname = None
933
913
            newparent = None
934
914
            newversioned = False
935
915
        else:
936
 
            newversioned = (newpath not in target_extras)
 
916
            newversioned = (newpath_decoded not in target_extras)
937
917
            if newmode:
938
918
                newexe = mode_is_executable(newmode)
939
919
                newkind = mode_kind(newmode)
947
927
                newparentpath, newname = osutils.split(newpath_decoded)
948
928
                newparent = mapping.generate_file_id(newparentpath)
949
929
        if (not include_unchanged and
950
 
                oldkind == 'directory' and newkind == 'directory' and
 
930
            oldkind == 'directory' and newkind == 'directory' and
951
931
                oldpath_decoded == newpath_decoded):
952
932
            continue
953
 
        if oldversioned and change_type != 'copy':
954
 
            fileid = mapping.generate_file_id(oldpath_decoded)
955
 
        elif newversioned:
956
 
            fileid = mapping.generate_file_id(newpath_decoded)
957
 
        else:
958
 
            fileid = None
959
933
        yield _mod_tree.TreeChange(
960
934
            fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
961
935
            (oldversioned, newversioned),
962
936
            (oldparent, newparent), (oldname, newname),
963
 
            (oldkind, newkind), (oldexe, newexe),
964
 
            copied=(change_type == 'copy'))
 
937
            (oldkind, newkind), (oldexe, newexe))
965
938
 
966
939
 
967
940
class InterGitTrees(_mod_tree.InterTree):
980
953
                extra_trees=None, require_versioned=False, include_root=False,
981
954
                want_unversioned=False):
982
955
        with self.lock_read():
983
 
            changes, source_extras, target_extras = self._iter_git_changes(
 
956
            changes, target_extras = self._iter_git_changes(
984
957
                want_unchanged=want_unchanged,
985
958
                require_versioned=require_versioned,
986
959
                specific_files=specific_files,
989
962
            return tree_delta_from_git_changes(
990
963
                changes, (self.source.mapping, self.target.mapping),
991
964
                specific_files=specific_files,
992
 
                include_root=include_root,
993
 
                source_extras=source_extras, target_extras=target_extras)
 
965
                include_root=include_root, target_extras=target_extras)
994
966
 
995
967
    def iter_changes(self, include_unchanged=False, specific_files=None,
996
968
                     pb=None, extra_trees=[], require_versioned=True,
997
969
                     want_unversioned=False):
998
970
        with self.lock_read():
999
 
            changes, source_extras, target_extras = self._iter_git_changes(
 
971
            changes, target_extras = self._iter_git_changes(
1000
972
                want_unchanged=include_unchanged,
1001
973
                require_versioned=require_versioned,
1002
974
                specific_files=specific_files,
1006
978
                changes, self.target.mapping,
1007
979
                specific_files=specific_files,
1008
980
                include_unchanged=include_unchanged,
1009
 
                source_extras=source_extras,
1010
981
                target_extras=target_extras)
1011
982
 
1012
983
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1013
984
                          require_versioned=False, extra_trees=None,
1014
 
                          want_unversioned=False, include_trees=True):
 
985
                          want_unversioned=False):
1015
986
        raise NotImplementedError(self._iter_git_changes)
1016
987
 
1017
988
    def find_target_path(self, path, recurse='none'):
1025
996
    def find_target_paths(self, paths, recurse='none'):
1026
997
        paths = set(paths)
1027
998
        ret = {}
1028
 
        changes = self._iter_git_changes(
1029
 
            specific_files=paths, include_trees=False)[0]
1030
 
        for (change_type, old, new) in changes:
1031
 
            if old[0] is None:
1032
 
                continue
1033
 
            oldpath = decode_git_path(old[0])
 
999
        changes = self._iter_git_changes(specific_files=paths)[0]
 
1000
        for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
1034
1001
            if oldpath in paths:
1035
 
                ret[oldpath] = decode_git_path(new[0]) if new[0] else None
 
1002
                ret[oldpath] = newpath
1036
1003
        for path in paths:
1037
1004
            if path not in ret:
1038
1005
                if self.source.has_filename(path):
1047
1014
    def find_source_paths(self, paths, recurse='none'):
1048
1015
        paths = set(paths)
1049
1016
        ret = {}
1050
 
        changes = self._iter_git_changes(
1051
 
            specific_files=paths, include_trees=False)[0]
1052
 
        for (change_type, old, new) in changes:
1053
 
            if new[0] is None:
1054
 
                continue
1055
 
            newpath = decode_git_path(new[0])
 
1017
        changes = self._iter_git_changes(specific_files=paths)[0]
 
1018
        for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
1056
1019
            if newpath in paths:
1057
 
                ret[newpath] = decode_git_path(old[0]) if old[0] else None
 
1020
                ret[newpath] = oldpath
1058
1021
        for path in paths:
1059
1022
            if path not in ret:
1060
1023
                if self.target.has_filename(path):
1081
1044
 
1082
1045
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1083
1046
                          require_versioned=True, extra_trees=None,
1084
 
                          want_unversioned=False, include_trees=True):
 
1047
                          want_unversioned=False):
1085
1048
        trees = [self.source]
1086
1049
        if extra_trees is not None:
1087
1050
            trees.extend(extra_trees)
1097
1060
                    self.target._repository._git.object_store])
1098
1061
        else:
1099
1062
            store = self.source._repository._git.object_store
1100
 
        rename_detector = RenameDetector(store)
1101
 
        changes = tree_changes(
1102
 
            store, self.source.tree, self.target.tree,
1103
 
            want_unchanged=want_unchanged, include_trees=include_trees,
1104
 
            change_type_same=True, rename_detector=rename_detector)
1105
 
        return changes, set(), set()
 
1063
        return store.tree_changes(
 
1064
            self.source.tree, self.target.tree, want_unchanged=want_unchanged,
 
1065
            include_trees=True, change_type_same=True), set()
1106
1066
 
1107
1067
 
1108
1068
_mod_tree.InterTree.register_optimiser(InterGitRevisionTrees)
1119
1079
 
1120
1080
    def is_versioned(self, path):
1121
1081
        with self.lock_read():
1122
 
            path = encode_git_path(path.rstrip('/'))
 
1082
            path = path.rstrip('/').encode('utf-8')
1123
1083
            (index, subpath) = self._lookup_index(path)
1124
1084
            return (subpath in index or self._has_dir(path))
1125
1085
 
1261
1221
                # old index
1262
1222
                stat_val = os.stat_result(
1263
1223
                    (stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1264
 
            blob.set_raw_string(encode_git_path(self.get_symlink_target(path)))
 
1224
            blob.set_raw_string(
 
1225
                self.get_symlink_target(path).encode("utf-8"))
1265
1226
            # Add object to the repository if it didn't exist yet
1266
1227
            if blob.id not in self.store:
1267
1228
                self.store.add_object(blob)
1284
1245
            raise AssertionError("unknown kind '%s'" % kind)
1285
1246
        # Add an entry to the index or update the existing entry
1286
1247
        ensure_normalized_path(path)
1287
 
        encoded_path = encode_git_path(path)
 
1248
        encoded_path = path.encode("utf-8")
1288
1249
        if b'\r' in encoded_path or b'\n' in encoded_path:
1289
1250
            # TODO(jelmer): Why do we need to do this?
1290
1251
            trace.mutter('ignoring path with invalid newline in it: %r', path)
1329
1290
                    recurse_nested=recurse_nested):
1330
1291
                if self.mapping.is_special_file(path):
1331
1292
                    continue
1332
 
                path = decode_git_path(path)
 
1293
                path = path.decode("utf-8")
1333
1294
                if specific_files is not None and path not in specific_files:
1334
1295
                    continue
1335
1296
                (parent, name) = posixpath.split(path)
1349
1310
                    key = (posixpath.dirname(path), path)
1350
1311
                    if key not in ret and self.is_versioned(path):
1351
1312
                        ret[key] = self._get_dir_ie(path, self.path2id(key[0]))
1352
 
            return ((path, ie) for ((_, path), ie) in sorted(ret.items()))
 
1313
            return ((path, ie) for ((_, path), ie) in sorted(viewitems(ret)))
1353
1314
 
1354
1315
    def iter_references(self):
1355
1316
        if self.supports_tree_reference():
1364
1325
                                posixpath.basename(path).strip("/"), parent_id)
1365
1326
 
1366
1327
    def _get_file_ie(self, name, path, value, parent_id):
1367
 
        if not isinstance(name, str):
 
1328
        if not isinstance(name, text_type):
1368
1329
            raise TypeError(name)
1369
 
        if not isinstance(path, str):
 
1330
        if not isinstance(path, text_type):
1370
1331
            raise TypeError(path)
1371
1332
        if not isinstance(value, tuple) or len(value) != 10:
1372
1333
            raise TypeError(value)
1415
1376
    def _unversion_path(self, path):
1416
1377
        if self._lock_mode is None:
1417
1378
            raise errors.ObjectNotLocked(self)
1418
 
        encoded_path = encode_git_path(path)
 
1379
        encoded_path = path.encode("utf-8")
1419
1380
        count = 0
1420
1381
        (index, subpath) = self._lookup_index(encoded_path)
1421
1382
        try:
1448
1409
        for (old_path, new_path, file_id, ie) in delta:
1449
1410
            if old_path is not None:
1450
1411
                (index, old_subpath) = self._lookup_index(
1451
 
                    encode_git_path(old_path))
 
1412
                    old_path.encode('utf-8'))
1452
1413
                if old_subpath in index:
1453
1414
                    self._index_del_entry(index, old_subpath)
1454
1415
                    self._versioned_dirs = None
1474
1435
            return rename_tuples
1475
1436
 
1476
1437
    def rename_one(self, from_rel, to_rel, after=None):
1477
 
        from_path = encode_git_path(from_rel)
 
1438
        from_path = from_rel.encode("utf-8")
1478
1439
        to_rel, can_access = osutils.normalized_filename(to_rel)
1479
1440
        if not can_access:
1480
1441
            raise errors.InvalidNormalization(to_rel)
1481
 
        to_path = encode_git_path(to_rel)
 
1442
        to_path = to_rel.encode("utf-8")
1482
1443
        with self.lock_tree_write():
1483
1444
            if not after:
1484
1445
                # Perhaps it's already moved?
1604
1565
            return (kind, None, None, None)
1605
1566
 
1606
1567
    def stored_kind(self, relpath):
1607
 
        (index, index_path) = self._lookup_index(encode_git_path(relpath))
 
1568
        (index, index_path) = self._lookup_index(relpath.encode('utf-8'))
1608
1569
        if index is None:
1609
1570
            return kind
1610
1571
        try:
1628
1589
    def _live_entry(self, relpath):
1629
1590
        raise NotImplementedError(self._live_entry)
1630
1591
 
1631
 
    def transform(self, pb=None):
1632
 
        from .transform import GitTreeTransform
1633
 
        return GitTreeTransform(self, pb=pb)
1634
 
 
1635
 
    def preview_transform(self, pb=None):
1636
 
        from .transform import GitTransformPreview
1637
 
        return GitTransformPreview(self, pb=pb)
1638
 
 
1639
 
 
1640
 
class InterToIndexGitTree(InterGitTrees):
 
1592
    def get_transform(self, pb=None):
 
1593
        from ..transform import TreeTransform
 
1594
        return TreeTransform(self, pb=pb)
 
1595
 
 
1596
 
 
1597
 
 
1598
class InterIndexGitTree(InterGitTrees):
1641
1599
    """InterTree that works between a Git revision tree and an index."""
1642
1600
 
1643
1601
    def __init__(self, source, target):
1644
 
        super(InterToIndexGitTree, self).__init__(source, target)
1645
 
        if self.source.store == self.target.store:
1646
 
            self.store = self.source.store
1647
 
        else:
1648
 
            self.store = OverlayObjectStore(
1649
 
                [self.source.store, self.target.store])
1650
 
        self.rename_detector = RenameDetector(self.store)
 
1602
        super(InterIndexGitTree, self).__init__(source, target)
 
1603
        self._index = target.index
1651
1604
 
1652
1605
    @classmethod
1653
1606
    def is_compatible(cls, source, target):
1656
1609
 
1657
1610
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1658
1611
                          require_versioned=False, extra_trees=None,
1659
 
                          want_unversioned=False, include_trees=True):
 
1612
                          want_unversioned=False):
1660
1613
        trees = [self.source]
1661
1614
        if extra_trees is not None:
1662
1615
            trees.extend(extra_trees)
1666
1619
                require_versioned=require_versioned)
1667
1620
        # TODO(jelmer): Restrict to specific_files, for performance reasons.
1668
1621
        with self.lock_read():
1669
 
            changes, target_extras = changes_between_git_tree_and_working_copy(
 
1622
            return changes_between_git_tree_and_working_copy(
1670
1623
                self.source.store, self.source.tree,
1671
1624
                self.target, want_unchanged=want_unchanged,
1672
 
                want_unversioned=want_unversioned,
1673
 
                rename_detector=self.rename_detector,
1674
 
                include_trees=include_trees)
1675
 
            return changes, set(), target_extras
1676
 
 
1677
 
 
1678
 
_mod_tree.InterTree.register_optimiser(InterToIndexGitTree)
1679
 
 
1680
 
 
1681
 
class InterFromIndexGitTree(InterGitTrees):
1682
 
    """InterTree that works between a Git revision tree and an index."""
1683
 
 
1684
 
    def __init__(self, source, target):
1685
 
        super(InterFromIndexGitTree, self).__init__(source, target)
1686
 
        if self.source.store == self.target.store:
1687
 
            self.store = self.source.store
1688
 
        else:
1689
 
            self.store = OverlayObjectStore(
1690
 
                [self.source.store, self.target.store])
1691
 
        self.rename_detector = RenameDetector(self.store)
1692
 
 
1693
 
    @classmethod
1694
 
    def is_compatible(cls, source, target):
1695
 
        return (isinstance(target, GitRevisionTree) and
1696
 
                isinstance(source, MutableGitIndexTree))
1697
 
 
1698
 
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1699
 
                          require_versioned=False, extra_trees=None,
1700
 
                          want_unversioned=False, include_trees=True):
1701
 
        trees = [self.source]
1702
 
        if extra_trees is not None:
1703
 
            trees.extend(extra_trees)
1704
 
        if specific_files is not None:
1705
 
            specific_files = self.target.find_related_paths_across_trees(
1706
 
                specific_files, trees,
1707
 
                require_versioned=require_versioned)
1708
 
        # TODO(jelmer): Restrict to specific_files, for performance reasons.
1709
 
        with self.lock_read():
1710
 
            from_tree_sha, extras = snapshot_workingtree(self.source, want_unversioned=want_unversioned)
1711
 
            return tree_changes(
1712
 
                self.store, from_tree_sha, self.target.tree,
1713
 
                include_trees=include_trees,
1714
 
                rename_detector=self.rename_detector,
1715
 
                want_unchanged=want_unchanged, change_type_same=True), extras
1716
 
 
1717
 
 
1718
 
_mod_tree.InterTree.register_optimiser(InterFromIndexGitTree)
1719
 
 
1720
 
 
1721
 
class InterIndexGitTree(InterGitTrees):
1722
 
    """InterTree that works between a Git revision tree and an index."""
1723
 
 
1724
 
    def __init__(self, source, target):
1725
 
        super(InterIndexGitTree, self).__init__(source, target)
1726
 
        if self.source.store == self.target.store:
1727
 
            self.store = self.source.store
1728
 
        else:
1729
 
            self.store = OverlayObjectStore(
1730
 
                [self.source.store, self.target.store])
1731
 
        self.rename_detector = RenameDetector(self.store)
1732
 
 
1733
 
    @classmethod
1734
 
    def is_compatible(cls, source, target):
1735
 
        return (isinstance(target, MutableGitIndexTree) and
1736
 
                isinstance(source, MutableGitIndexTree))
1737
 
 
1738
 
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1739
 
                          require_versioned=False, extra_trees=None,
1740
 
                          want_unversioned=False, include_trees=True):
1741
 
        trees = [self.source]
1742
 
        if extra_trees is not None:
1743
 
            trees.extend(extra_trees)
1744
 
        if specific_files is not None:
1745
 
            specific_files = self.target.find_related_paths_across_trees(
1746
 
                specific_files, trees,
1747
 
                require_versioned=require_versioned)
1748
 
        # TODO(jelmer): Restrict to specific_files, for performance reasons.
1749
 
        with self.lock_read():
1750
 
            from_tree_sha, from_extras = snapshot_workingtree(
1751
 
                self.source, want_unversioned=want_unversioned)
1752
 
            to_tree_sha, to_extras = snapshot_workingtree(
1753
 
                self.target, want_unversioned=want_unversioned)
1754
 
            changes = tree_changes(
1755
 
                self.store, from_tree_sha, to_tree_sha,
1756
 
                include_trees=include_trees,
1757
 
                rename_detector=self.rename_detector,
1758
 
                want_unchanged=want_unchanged, change_type_same=True)
1759
 
            return changes, from_extras, to_extras
 
1625
                want_unversioned=want_unversioned)
1760
1626
 
1761
1627
 
1762
1628
_mod_tree.InterTree.register_optimiser(InterIndexGitTree)
1763
1629
 
1764
1630
 
1765
 
def snapshot_workingtree(target, want_unversioned=False):
 
1631
def changes_between_git_tree_and_working_copy(store, from_tree_sha, target,
 
1632
                                              want_unchanged=False,
 
1633
                                              want_unversioned=False):
 
1634
    """Determine the changes between a git tree and a working tree with index.
 
1635
 
 
1636
    """
1766
1637
    extras = set()
1767
1638
    blobs = {}
1768
1639
    # Report dirified directories to commit_tree first, so that they can be
1786
1657
                    blobs[path] = (index_entry.sha, index_entry.mode)
1787
1658
                else:
1788
1659
                    dirified.append((path, Tree().id, stat.S_IFDIR))
1789
 
                    target.store.add_object(Tree())
 
1660
                    store.add_object(Tree())
1790
1661
            else:
1791
1662
                mode = live_entry.mode
1792
1663
                if not trust_executable:
1794
1665
                        mode |= 0o111
1795
1666
                    else:
1796
1667
                        mode &= ~0o111
1797
 
                if live_entry.sha != index_entry.sha:
1798
 
                    rp = decode_git_path(path)
1799
 
                    if stat.S_ISREG(live_entry.mode):
1800
 
                        blob = Blob()
1801
 
                        with target.get_file(rp) as f:
1802
 
                            blob.data = f.read()
1803
 
                    elif stat.S_ISLNK(live_entry.mode):
1804
 
                        blob = Blob()
1805
 
                        blob.data = target.get_symlink_target(rp).encode(osutils._fs_enc)
1806
 
                    else:
1807
 
                        blob = None
1808
 
                    if blob is not None:
1809
 
                        target.store.add_object(blob)
1810
1668
                blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
1811
1669
    if want_unversioned:
1812
 
        for e in target._iter_files_recursive(include_dirs=False):
 
1670
        for e in target.extras():
 
1671
            st = target._lstat(e)
1813
1672
            try:
1814
 
                e, accessible = osutils.normalized_filename(e)
 
1673
                np, accessible = osutils.normalized_filename(e)
1815
1674
            except UnicodeDecodeError:
1816
1675
                raise errors.BadFilenameEncoding(
1817
1676
                    e, osutils._fs_enc)
1818
 
            np = encode_git_path(e)
1819
 
            if np in blobs:
1820
 
                continue
1821
 
            st = target._lstat(e)
1822
1677
            if stat.S_ISDIR(st.st_mode):
1823
1678
                blob = Tree()
1824
 
            elif stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode):
 
1679
            else:
1825
1680
                blob = blob_from_path_and_stat(
1826
1681
                    target.abspath(e).encode(osutils._fs_enc), st)
1827
 
            else:
1828
 
                continue
1829
 
            target.store.add_object(blob)
 
1682
            store.add_object(blob)
 
1683
            np = np.encode('utf-8')
1830
1684
            blobs[np] = (blob.id, cleanup_mode(st.st_mode))
1831
1685
            extras.add(np)
1832
 
    return commit_tree(
1833
 
        target.store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()]), extras
1834
 
 
1835
 
 
1836
 
def changes_between_git_tree_and_working_copy(source_store, from_tree_sha, target,
1837
 
                                              want_unchanged=False,
1838
 
                                              want_unversioned=False,
1839
 
                                              rename_detector=None,
1840
 
                                              include_trees=True):
1841
 
    """Determine the changes between a git tree and a working tree with index.
1842
 
 
1843
 
    """
1844
 
    to_tree_sha, extras = snapshot_workingtree(target, want_unversioned=want_unversioned)
1845
 
    store = OverlayObjectStore([source_store, target.store])
1846
 
    return tree_changes(
1847
 
        store, from_tree_sha, to_tree_sha, include_trees=include_trees,
1848
 
        rename_detector=rename_detector,
 
1686
    to_tree_sha = commit_tree(
 
1687
        store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()])
 
1688
    return store.tree_changes(
 
1689
        from_tree_sha, to_tree_sha, include_trees=True,
1849
1690
        want_unchanged=want_unchanged, change_type_same=True), extras