/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-05-24 00:39:50 UTC
  • mto: This revision was merged to the branch mainline in revision 7504.
  • Revision ID: jelmer@jelmer.uk-20200524003950-bbc545r76vc5yajg
Add github action.

Show diffs side-by-side

added added

removed removed

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