/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-02-13 23:57:28 UTC
  • mfrom: (7490 work)
  • mto: This revision was merged to the branch mainline in revision 7492.
  • Revision ID: jelmer@jelmer.uk-20200213235728-m6ds0mm3mbs4y182
Merge trunk.

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,
66
68
    )
67
69
 
68
70
from .mapping import (
69
 
    encode_git_path,
70
 
    decode_git_path,
71
71
    mode_is_executable,
72
72
    mode_kind,
73
73
    default_mapping,
298
298
            info = self._submodule_info()[relpath]
299
299
        except KeyError:
300
300
            nested_repo_transport = self._repository.controldir.user_transport.clone(
301
 
                decode_git_path(relpath))
 
301
                relpath.decode('utf-8'))
302
302
        else:
303
303
            nested_repo_transport = self._repository.controldir.control_transport.clone(
304
 
                posixpath.join('modules', decode_git_path(info[1])))
 
304
                posixpath.join('modules', info[1].decode('utf-8')))
305
305
        nested_controldir = _mod_controldir.ControlDir.open_from_transport(
306
306
            nested_repo_transport)
307
307
        return nested_controldir.find_repository()
310
310
        return self._get_submodule_repository(relpath)._git.object_store
311
311
 
312
312
    def get_nested_tree(self, path):
313
 
        encoded_path = encode_git_path(path)
 
313
        encoded_path = path.encode('utf-8')
314
314
        nested_repo = self._get_submodule_repository(encoded_path)
315
315
        ref_rev = self.get_reference_revision(path)
316
316
        return nested_repo.revision_tree(ref_rev)
323
323
        if self.commit_id == ZERO_SHA:
324
324
            return NULL_REVISION
325
325
        (unused_path, commit_id) = change_scanner.find_last_change_revision(
326
 
            encode_git_path(path), self.commit_id)
 
326
            path.encode('utf-8'), self.commit_id)
327
327
        return self._repository.lookup_foreign_revision_id(
328
328
            commit_id, self.mapping)
329
329
 
370
370
            tree = store[tree_id]
371
371
            for name, mode, hexsha in tree.items():
372
372
                subpath = posixpath.join(path, name)
373
 
                ret.add(decode_git_path(subpath))
 
373
                ret.add(subpath.decode('utf-8'))
374
374
                if stat.S_ISDIR(mode):
375
375
                    todo.append((store, subpath, hexsha))
376
376
        return ret
379
379
        if self.tree is None:
380
380
            raise errors.NoSuchFile(path)
381
381
 
382
 
        encoded_path = encode_git_path(path)
 
382
        encoded_path = path.encode('utf-8')
383
383
        parts = encoded_path.split(b'/')
384
384
        hexsha = self.tree
385
385
        store = self.store
446
446
            parent_path = posixpath.dirname(from_dir)
447
447
            parent_id = self.mapping.generate_file_id(parent_path)
448
448
            if mode_kind(mode) == 'directory':
449
 
                root_ie = self._get_dir_ie(encode_git_path(from_dir), parent_id)
 
449
                root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
450
450
            else:
451
451
                root_ie = self._get_file_ie(
452
 
                    store, encode_git_path(from_dir),
 
452
                    store, from_dir.encode("utf-8"),
453
453
                    posixpath.basename(from_dir), mode, hexsha)
454
454
        if include_root:
455
455
            yield (from_dir, "V", root_ie.kind, root_ie)
456
456
        todo = []
457
457
        if root_ie.kind == 'directory':
458
 
            todo.append((store, encode_git_path(from_dir),
 
458
            todo.append((store, from_dir.encode("utf-8"),
459
459
                         b"", hexsha, root_ie.file_id))
460
460
        while todo:
461
461
            (store, path, relpath, hexsha, parent_id) = todo.pop()
478
478
                else:
479
479
                    ie = self._get_file_ie(
480
480
                        store, child_path, name, mode, hexsha, parent_id)
481
 
                yield (decode_git_path(child_relpath), "V", ie.kind, ie)
 
481
                yield (child_relpath.decode('utf-8'), "V", ie.kind, ie)
482
482
 
483
483
    def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
484
484
        if not isinstance(path, bytes):
486
486
        if not isinstance(name, bytes):
487
487
            raise TypeError(name)
488
488
        kind = mode_kind(mode)
489
 
        path = decode_git_path(path)
490
 
        name = decode_git_path(name)
 
489
        path = path.decode('utf-8')
 
490
        name = name.decode("utf-8")
491
491
        file_id = self.mapping.generate_file_id(path)
492
492
        ie = entry_factory[kind](file_id, name, parent_id)
493
493
        if kind == 'symlink':
494
 
            ie.symlink_target = decode_git_path(store[hexsha].data)
 
494
            ie.symlink_target = store[hexsha].data.decode('utf-8')
495
495
        elif kind == 'tree-reference':
496
496
            ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(
497
497
                hexsha)
503
503
        return ie
504
504
 
505
505
    def _get_dir_ie(self, path, parent_id):
506
 
        path = decode_git_path(path)
 
506
        path = path.decode('utf-8')
507
507
        file_id = self.mapping.generate_file_id(path)
508
508
        return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
509
509
 
513
513
        if mode is not None and not stat.S_ISDIR(mode):
514
514
            return
515
515
 
516
 
        encoded_path = encode_git_path(path)
 
516
        encoded_path = path.encode('utf-8')
517
517
        file_id = self.path2id(path)
518
518
        tree = store[tree_sha]
519
519
        for name, mode, hexsha in tree.iteritems():
534
534
            if specific_files in ([""], []):
535
535
                specific_files = None
536
536
            else:
537
 
                specific_files = set([encode_git_path(p)
 
537
                specific_files = set([p.encode('utf-8')
538
538
                                      for p in specific_files])
539
539
        todo = deque([(self.store, b"", self.tree, self.path2id(''))])
540
540
        if specific_files is None or u"" in specific_files:
547
547
                if self.mapping.is_special_file(name):
548
548
                    continue
549
549
                child_path = posixpath.join(path, name)
550
 
                child_path_decoded = decode_git_path(child_path)
 
550
                child_path_decoded = child_path.decode('utf-8')
551
551
                if recurse_nested and S_ISGITLINK(mode):
552
552
                    mode = stat.S_IFDIR
553
553
                    store = self._get_submodule_store(child_path)
606
606
        """See RevisionTree.get_symlink_target."""
607
607
        (store, mode, hexsha) = self._lookup_path(path)
608
608
        if stat.S_ISLNK(mode):
609
 
            return decode_git_path(store[hexsha].data)
 
609
            return store[hexsha].data.decode('utf-8')
610
610
        else:
611
611
            return None
612
612
 
615
615
        (store, mode, hexsha) = self._lookup_path(path)
616
616
        if S_ISGITLINK(mode):
617
617
            try:
618
 
                nested_repo = self._get_submodule_repository(encode_git_path(path))
 
618
                nested_repo = self._get_submodule_repository(path.encode('utf-8'))
619
619
            except errors.NotBranchError:
620
620
                return self.mapping.revision_id_foreign_to_bzr(hexsha)
621
621
            else:
641
641
            return (kind, len(contents), executable,
642
642
                    osutils.sha_string(contents))
643
643
        elif kind == 'symlink':
644
 
            return (kind, None, None, decode_git_path(store[hexsha].data))
 
644
            return (kind, None, None, store[hexsha].data.decode('utf-8'))
645
645
        elif kind == 'tree-reference':
646
 
            nested_repo = self._get_submodule_repository(encode_git_path(path))
 
646
            nested_repo = self._get_submodule_repository(path.encode('utf-8'))
647
647
            return (kind, None, None,
648
648
                    nested_repo.lookup_foreign_revision_id(hexsha))
649
649
        else:
699
699
    def walkdirs(self, prefix=u""):
700
700
        (store, mode, hexsha) = self._lookup_path(prefix)
701
701
        todo = deque(
702
 
            [(store, encode_git_path(prefix), hexsha, self.path2id(prefix))])
 
702
            [(store, prefix.encode('utf-8'), hexsha, self.path2id(prefix))])
703
703
        while todo:
704
704
            store, path, tree_sha, parent_id = todo.popleft()
705
 
            path_decoded = decode_git_path(path)
 
705
            path_decoded = path.decode('utf-8')
706
706
            tree = store[tree_sha]
707
707
            children = []
708
708
            for name, mode, hexsha in tree.iteritems():
709
709
                if self.mapping.is_special_file(name):
710
710
                    continue
711
711
                child_path = posixpath.join(path, name)
712
 
                file_id = self.path2id(decode_git_path(child_path))
 
712
                file_id = self.path2id(child_path.decode('utf-8'))
713
713
                if stat.S_ISDIR(mode):
714
714
                    todo.append((store, child_path, hexsha, file_id))
715
715
                children.append(
716
 
                    (decode_git_path(child_path), decode_git_path(name),
 
716
                    (child_path.decode('utf-8'), name.decode('utf-8'),
717
717
                        mode_kind(mode), None,
718
718
                        file_id, mode_kind(mode)))
719
719
            yield (path_decoded, parent_id), children
720
720
 
721
 
    def preview_transform(self, pb=None):
722
 
        from .transform import GitTransformPreview
723
 
        return GitTransformPreview(self, pb=pb)
724
 
 
725
721
 
726
722
def tree_delta_from_git_changes(changes, mappings,
727
723
                                specific_files=None,
728
724
                                require_versioned=False, include_root=False,
729
 
                                source_extras=None, target_extras=None):
 
725
                                target_extras=None):
730
726
    """Create a TreeDelta from two git trees.
731
727
 
732
728
    source and target are iterators over tuples with:
735
731
    (old_mapping, new_mapping) = mappings
736
732
    if target_extras is None:
737
733
        target_extras = set()
738
 
    if source_extras is None:
739
 
        source_extras = set()
740
734
    ret = delta.TreeDelta()
741
735
    added = []
742
 
    for (change_type, old, new) in changes:
743
 
        (oldpath, oldmode, oldsha) = old
744
 
        (newpath, newmode, newsha) = new
 
736
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
745
737
        if newpath == b'' and not include_root:
746
738
            continue
747
 
        copied = (change_type == 'copy')
748
739
        if oldpath is not None:
749
 
            oldpath_decoded = decode_git_path(oldpath)
 
740
            oldpath_decoded = oldpath.decode('utf-8')
750
741
        else:
751
742
            oldpath_decoded = None
752
743
        if newpath is not None:
753
 
            newpath_decoded = decode_git_path(newpath)
 
744
            newpath_decoded = newpath.decode('utf-8')
754
745
        else:
755
746
            newpath_decoded = None
756
747
        if not (specific_files is None or
762
753
                        specific_files, newpath_decoded))):
763
754
            continue
764
755
 
765
 
        if oldpath is None:
 
756
        if oldpath_decoded is None:
 
757
            fileid = new_mapping.generate_file_id(newpath_decoded)
766
758
            oldexe = None
767
759
            oldkind = None
768
760
            oldname = None
769
761
            oldparent = None
770
762
            oldversioned = False
771
763
        else:
772
 
            oldversioned = (oldpath not in source_extras)
 
764
            oldversioned = True
773
765
            if oldmode:
774
766
                oldexe = mode_is_executable(oldmode)
775
767
                oldkind = mode_kind(oldmode)
776
768
            else:
777
769
                oldexe = False
778
770
                oldkind = None
779
 
            if oldpath == b'':
 
771
            if oldpath_decoded == u'':
780
772
                oldparent = None
781
773
                oldname = u''
782
774
            else:
783
775
                (oldparentpath, oldname) = osutils.split(oldpath_decoded)
784
776
                oldparent = old_mapping.generate_file_id(oldparentpath)
785
 
        if newpath is None:
 
777
            fileid = old_mapping.generate_file_id(oldpath_decoded)
 
778
        if newpath_decoded is None:
786
779
            newexe = None
787
780
            newkind = None
788
781
            newname = None
789
782
            newparent = None
790
783
            newversioned = False
791
784
        else:
792
 
            newversioned = (newpath not in target_extras)
 
785
            newversioned = (newpath_decoded not in target_extras)
793
786
            if newmode:
794
787
                newexe = mode_is_executable(newmode)
795
788
                newkind = mode_kind(newmode)
802
795
            else:
803
796
                newparentpath, newname = osutils.split(newpath_decoded)
804
797
                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
798
        if old_mapping.is_special_file(oldpath):
812
799
            oldpath = None
813
800
        if new_mapping.is_special_file(newpath):
818
805
            fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
819
806
            (oldversioned, newversioned),
820
807
            (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':
 
808
            (oldkind, newkind), (oldexe, newexe))
 
809
        if oldpath is None:
827
810
            added.append((newpath, newkind))
828
811
        elif newpath is None or newmode == 0:
829
812
            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
 
813
        elif oldpath != newpath:
839
814
            ret.renamed.append(change)
840
815
        elif mode_kind(oldmode) != mode_kind(newmode):
841
816
            ret.kind_changed.append(change)
855
830
    for path, kind in added:
856
831
        if kind == 'directory' and path not in implicit_dirs:
857
832
            continue
858
 
        path_decoded = decode_git_path(path)
 
833
        path_decoded = osutils.normalized_filename(path)[0]
859
834
        parent_path, basename = osutils.split(path_decoded)
860
835
        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),
 
836
        if path in target_extras:
 
837
            ret.unversioned.append(_mod_tree.TreeChange(
 
838
                None, (None, path_decoded),
 
839
                True, (False, False), (None, parent_id),
867
840
                (None, basename), (None, kind), (None, False)))
 
841
        else:
 
842
            file_id = new_mapping.generate_file_id(path_decoded)
 
843
            ret.added.append(
 
844
                _mod_tree.TreeChange(
 
845
                    file_id, (None, path_decoded), True,
 
846
                    (False, True),
 
847
                    (None, parent_id),
 
848
                    (None, basename), (None, kind), (None, False)))
868
849
 
869
850
    return ret
870
851
 
871
852
 
872
853
def changes_from_git_changes(changes, mapping, specific_files=None,
873
 
                             include_unchanged=False, source_extras=None,
874
 
                             target_extras=None):
 
854
                             include_unchanged=False, target_extras=None):
875
855
    """Create a iter_changes-like generator from a git stream.
876
856
 
877
857
    source and target are iterators over tuples with:
879
859
    """
880
860
    if target_extras is None:
881
861
        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
 
862
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
889
863
        if oldpath is not None:
890
 
            oldpath_decoded = decode_git_path(oldpath)
 
864
            oldpath_decoded = oldpath.decode('utf-8')
891
865
        else:
892
866
            oldpath_decoded = None
893
867
        if newpath is not None:
894
 
            newpath_decoded = decode_git_path(newpath)
 
868
            newpath_decoded = newpath.decode('utf-8')
895
869
        else:
896
870
            newpath_decoded = None
897
871
        if not (specific_files is None or
906
880
            continue
907
881
        if newpath is not None and mapping.is_special_file(newpath):
908
882
            continue
909
 
        if oldpath is None:
 
883
        if oldpath_decoded is None:
 
884
            fileid = mapping.generate_file_id(newpath_decoded)
910
885
            oldexe = None
911
886
            oldkind = None
912
887
            oldname = None
913
888
            oldparent = None
914
889
            oldversioned = False
915
890
        else:
916
 
            oldversioned = (oldpath not in source_extras)
 
891
            oldversioned = True
917
892
            if oldmode:
918
893
                oldexe = mode_is_executable(oldmode)
919
894
                oldkind = mode_kind(oldmode)
926
901
            else:
927
902
                (oldparentpath, oldname) = osutils.split(oldpath_decoded)
928
903
                oldparent = mapping.generate_file_id(oldparentpath)
929
 
        if newpath is None:
 
904
            fileid = mapping.generate_file_id(oldpath_decoded)
 
905
        if newpath_decoded is None:
930
906
            newexe = None
931
907
            newkind = None
932
908
            newname = None
933
909
            newparent = None
934
910
            newversioned = False
935
911
        else:
936
 
            newversioned = (newpath not in target_extras)
 
912
            newversioned = (newpath_decoded not in target_extras)
937
913
            if newmode:
938
914
                newexe = mode_is_executable(newmode)
939
915
                newkind = mode_kind(newmode)
947
923
                newparentpath, newname = osutils.split(newpath_decoded)
948
924
                newparent = mapping.generate_file_id(newparentpath)
949
925
        if (not include_unchanged and
950
 
                oldkind == 'directory' and newkind == 'directory' and
 
926
            oldkind == 'directory' and newkind == 'directory' and
951
927
                oldpath_decoded == newpath_decoded):
952
928
            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
929
        yield _mod_tree.TreeChange(
960
930
            fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
961
931
            (oldversioned, newversioned),
962
932
            (oldparent, newparent), (oldname, newname),
963
 
            (oldkind, newkind), (oldexe, newexe),
964
 
            copied=(change_type == 'copy'))
 
933
            (oldkind, newkind), (oldexe, newexe))
965
934
 
966
935
 
967
936
class InterGitTrees(_mod_tree.InterTree):
980
949
                extra_trees=None, require_versioned=False, include_root=False,
981
950
                want_unversioned=False):
982
951
        with self.lock_read():
983
 
            changes, source_extras, target_extras = self._iter_git_changes(
 
952
            changes, target_extras = self._iter_git_changes(
984
953
                want_unchanged=want_unchanged,
985
954
                require_versioned=require_versioned,
986
955
                specific_files=specific_files,
989
958
            return tree_delta_from_git_changes(
990
959
                changes, (self.source.mapping, self.target.mapping),
991
960
                specific_files=specific_files,
992
 
                include_root=include_root,
993
 
                source_extras=source_extras, target_extras=target_extras)
 
961
                include_root=include_root, target_extras=target_extras)
994
962
 
995
963
    def iter_changes(self, include_unchanged=False, specific_files=None,
996
964
                     pb=None, extra_trees=[], require_versioned=True,
997
965
                     want_unversioned=False):
998
966
        with self.lock_read():
999
 
            changes, source_extras, target_extras = self._iter_git_changes(
 
967
            changes, target_extras = self._iter_git_changes(
1000
968
                want_unchanged=include_unchanged,
1001
969
                require_versioned=require_versioned,
1002
970
                specific_files=specific_files,
1006
974
                changes, self.target.mapping,
1007
975
                specific_files=specific_files,
1008
976
                include_unchanged=include_unchanged,
1009
 
                source_extras=source_extras,
1010
977
                target_extras=target_extras)
1011
978
 
1012
979
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1013
980
                          require_versioned=False, extra_trees=None,
1014
 
                          want_unversioned=False, include_trees=True):
 
981
                          want_unversioned=False):
1015
982
        raise NotImplementedError(self._iter_git_changes)
1016
983
 
1017
984
    def find_target_path(self, path, recurse='none'):
1025
992
    def find_target_paths(self, paths, recurse='none'):
1026
993
        paths = set(paths)
1027
994
        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])
 
995
        changes = self._iter_git_changes(specific_files=paths)[0]
 
996
        for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
1034
997
            if oldpath in paths:
1035
 
                ret[oldpath] = decode_git_path(new[0]) if new[0] else None
 
998
                ret[oldpath] = newpath
1036
999
        for path in paths:
1037
1000
            if path not in ret:
1038
1001
                if self.source.has_filename(path):
1047
1010
    def find_source_paths(self, paths, recurse='none'):
1048
1011
        paths = set(paths)
1049
1012
        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])
 
1013
        changes = self._iter_git_changes(specific_files=paths)[0]
 
1014
        for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
1056
1015
            if newpath in paths:
1057
 
                ret[newpath] = decode_git_path(old[0]) if old[0] else None
 
1016
                ret[newpath] = oldpath
1058
1017
        for path in paths:
1059
1018
            if path not in ret:
1060
1019
                if self.target.has_filename(path):
1081
1040
 
1082
1041
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1083
1042
                          require_versioned=True, extra_trees=None,
1084
 
                          want_unversioned=False, include_trees=True):
 
1043
                          want_unversioned=False):
1085
1044
        trees = [self.source]
1086
1045
        if extra_trees is not None:
1087
1046
            trees.extend(extra_trees)
1097
1056
                    self.target._repository._git.object_store])
1098
1057
        else:
1099
1058
            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()
 
1059
        return store.tree_changes(
 
1060
            self.source.tree, self.target.tree, want_unchanged=want_unchanged,
 
1061
            include_trees=True, change_type_same=True), set()
1106
1062
 
1107
1063
 
1108
1064
_mod_tree.InterTree.register_optimiser(InterGitRevisionTrees)
1119
1075
 
1120
1076
    def is_versioned(self, path):
1121
1077
        with self.lock_read():
1122
 
            path = encode_git_path(path.rstrip('/'))
 
1078
            path = path.rstrip('/').encode('utf-8')
1123
1079
            (index, subpath) = self._lookup_index(path)
1124
1080
            return (subpath in index or self._has_dir(path))
1125
1081
 
1261
1217
                # old index
1262
1218
                stat_val = os.stat_result(
1263
1219
                    (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)))
 
1220
            blob.set_raw_string(
 
1221
                self.get_symlink_target(path).encode("utf-8"))
1265
1222
            # Add object to the repository if it didn't exist yet
1266
1223
            if blob.id not in self.store:
1267
1224
                self.store.add_object(blob)
1284
1241
            raise AssertionError("unknown kind '%s'" % kind)
1285
1242
        # Add an entry to the index or update the existing entry
1286
1243
        ensure_normalized_path(path)
1287
 
        encoded_path = encode_git_path(path)
 
1244
        encoded_path = path.encode("utf-8")
1288
1245
        if b'\r' in encoded_path or b'\n' in encoded_path:
1289
1246
            # TODO(jelmer): Why do we need to do this?
1290
1247
            trace.mutter('ignoring path with invalid newline in it: %r', path)
1329
1286
                    recurse_nested=recurse_nested):
1330
1287
                if self.mapping.is_special_file(path):
1331
1288
                    continue
1332
 
                path = decode_git_path(path)
 
1289
                path = path.decode("utf-8")
1333
1290
                if specific_files is not None and path not in specific_files:
1334
1291
                    continue
1335
1292
                (parent, name) = posixpath.split(path)
1415
1372
    def _unversion_path(self, path):
1416
1373
        if self._lock_mode is None:
1417
1374
            raise errors.ObjectNotLocked(self)
1418
 
        encoded_path = encode_git_path(path)
 
1375
        encoded_path = path.encode("utf-8")
1419
1376
        count = 0
1420
1377
        (index, subpath) = self._lookup_index(encoded_path)
1421
1378
        try:
1448
1405
        for (old_path, new_path, file_id, ie) in delta:
1449
1406
            if old_path is not None:
1450
1407
                (index, old_subpath) = self._lookup_index(
1451
 
                    encode_git_path(old_path))
 
1408
                    old_path.encode('utf-8'))
1452
1409
                if old_subpath in index:
1453
1410
                    self._index_del_entry(index, old_subpath)
1454
1411
                    self._versioned_dirs = None
1474
1431
            return rename_tuples
1475
1432
 
1476
1433
    def rename_one(self, from_rel, to_rel, after=None):
1477
 
        from_path = encode_git_path(from_rel)
 
1434
        from_path = from_rel.encode("utf-8")
1478
1435
        to_rel, can_access = osutils.normalized_filename(to_rel)
1479
1436
        if not can_access:
1480
1437
            raise errors.InvalidNormalization(to_rel)
1481
 
        to_path = encode_git_path(to_rel)
 
1438
        to_path = to_rel.encode("utf-8")
1482
1439
        with self.lock_tree_write():
1483
1440
            if not after:
1484
1441
                # Perhaps it's already moved?
1604
1561
            return (kind, None, None, None)
1605
1562
 
1606
1563
    def stored_kind(self, relpath):
1607
 
        (index, index_path) = self._lookup_index(encode_git_path(relpath))
 
1564
        (index, index_path) = self._lookup_index(relpath.encode('utf-8'))
1608
1565
        if index is None:
1609
1566
            return kind
1610
1567
        try:
1628
1585
    def _live_entry(self, relpath):
1629
1586
        raise NotImplementedError(self._live_entry)
1630
1587
 
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):
 
1588
    def get_transform(self, pb=None):
 
1589
        from ..transform import TreeTransform
 
1590
        return TreeTransform(self, pb=pb)
 
1591
 
 
1592
 
 
1593
 
 
1594
class InterIndexGitTree(InterGitTrees):
1641
1595
    """InterTree that works between a Git revision tree and an index."""
1642
1596
 
1643
1597
    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)
 
1598
        super(InterIndexGitTree, self).__init__(source, target)
 
1599
        self._index = target.index
1651
1600
 
1652
1601
    @classmethod
1653
1602
    def is_compatible(cls, source, target):
1656
1605
 
1657
1606
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1658
1607
                          require_versioned=False, extra_trees=None,
1659
 
                          want_unversioned=False, include_trees=True):
 
1608
                          want_unversioned=False):
1660
1609
        trees = [self.source]
1661
1610
        if extra_trees is not None:
1662
1611
            trees.extend(extra_trees)
1666
1615
                require_versioned=require_versioned)
1667
1616
        # TODO(jelmer): Restrict to specific_files, for performance reasons.
1668
1617
        with self.lock_read():
1669
 
            changes, target_extras = changes_between_git_tree_and_working_copy(
 
1618
            return changes_between_git_tree_and_working_copy(
1670
1619
                self.source.store, self.source.tree,
1671
1620
                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
 
1621
                want_unversioned=want_unversioned)
1760
1622
 
1761
1623
 
1762
1624
_mod_tree.InterTree.register_optimiser(InterIndexGitTree)
1763
1625
 
1764
1626
 
1765
 
def snapshot_workingtree(target, want_unversioned=False):
 
1627
def changes_between_git_tree_and_working_copy(store, from_tree_sha, target,
 
1628
                                              want_unchanged=False,
 
1629
                                              want_unversioned=False):
 
1630
    """Determine the changes between a git tree and a working tree with index.
 
1631
 
 
1632
    """
1766
1633
    extras = set()
1767
1634
    blobs = {}
1768
1635
    # Report dirified directories to commit_tree first, so that they can be
1786
1653
                    blobs[path] = (index_entry.sha, index_entry.mode)
1787
1654
                else:
1788
1655
                    dirified.append((path, Tree().id, stat.S_IFDIR))
1789
 
                    target.store.add_object(Tree())
 
1656
                    store.add_object(Tree())
1790
1657
            else:
1791
1658
                mode = live_entry.mode
1792
1659
                if not trust_executable:
1794
1661
                        mode |= 0o111
1795
1662
                    else:
1796
1663
                        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
1664
                blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
1811
1665
    if want_unversioned:
1812
 
        for e in target._iter_files_recursive(include_dirs=False):
 
1666
        for e in target.extras():
 
1667
            st = target._lstat(e)
1813
1668
            try:
1814
 
                e, accessible = osutils.normalized_filename(e)
 
1669
                np, accessible = osutils.normalized_filename(e)
1815
1670
            except UnicodeDecodeError:
1816
1671
                raise errors.BadFilenameEncoding(
1817
1672
                    e, osutils._fs_enc)
1818
 
            np = encode_git_path(e)
1819
 
            if np in blobs:
1820
 
                continue
1821
 
            st = target._lstat(e)
1822
1673
            if stat.S_ISDIR(st.st_mode):
1823
1674
                blob = Tree()
1824
 
            elif stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode):
 
1675
            else:
1825
1676
                blob = blob_from_path_and_stat(
1826
1677
                    target.abspath(e).encode(osutils._fs_enc), st)
1827
 
            else:
1828
 
                continue
1829
 
            target.store.add_object(blob)
 
1678
            store.add_object(blob)
 
1679
            np = np.encode('utf-8')
1830
1680
            blobs[np] = (blob.id, cleanup_mode(st.st_mode))
1831
1681
            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,
 
1682
    to_tree_sha = commit_tree(
 
1683
        store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()])
 
1684
    return store.tree_changes(
 
1685
        from_tree_sha, to_tree_sha, include_trees=True,
1849
1686
        want_unchanged=want_unchanged, change_type_same=True), extras