/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/plugins/git/tree.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-07-03 03:20:44 UTC
  • mfrom: (7018.3.10 git-fixes)
  • Revision ID: breezy.the.bot@gmail.com-20180703032044-t5a5w5y0tmzrbezc
Port a few more bits of the git plugin to python 3.

Merged from https://code.launchpad.net/~jelmer/brz/git-fixes2/+merge/348803

Show diffs side-by-side

added added

removed removed

Lines of Context:
312
312
    def path2id(self, path):
313
313
        if self.mapping.is_special_file(path):
314
314
            return None
315
 
        return self._fileid_map.lookup_file_id(path.encode('utf-8'))
 
315
        return self._fileid_map.lookup_file_id(osutils.safe_unicode(path))
316
316
 
317
317
    def all_file_ids(self):
318
318
        return set(self._fileid_map.all_file_ids())
403
403
                    posixpath.basename(from_dir), mode, hexsha)
404
404
        if from_dir != "" or include_root:
405
405
            yield (from_dir, "V", root_ie.kind, root_ie.file_id, root_ie)
406
 
        todo = set()
 
406
        todo = []
407
407
        if root_ie.kind == 'directory':
408
 
            todo.add((store, from_dir.encode("utf-8"), hexsha, root_ie.file_id))
 
408
            todo.append((store, from_dir.encode("utf-8"), hexsha, root_ie.file_id))
409
409
        while todo:
410
410
            (store, path, hexsha, parent_id) = todo.pop()
411
411
            tree = store[hexsha]
416
416
                if stat.S_ISDIR(mode):
417
417
                    ie = self._get_dir_ie(child_path, parent_id)
418
418
                    if recursive:
419
 
                        todo.add((store, child_path, hexsha, ie.file_id))
 
419
                        todo.append((store, child_path, hexsha, ie.file_id))
420
420
                else:
421
421
                    ie = self._get_file_ie(store, child_path, name, mode, hexsha, parent_id)
422
422
                yield child_path.decode('utf-8'), "V", ie.kind, ie.file_id, ie
423
423
 
424
424
    def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
425
 
        if type(path) is not bytes:
 
425
        if not isinstance(path, bytes):
426
426
            raise TypeError(path)
427
 
        if type(name) is not bytes:
 
427
        if not isinstance(name, bytes):
428
428
            raise TypeError(name)
429
429
        kind = mode_kind(mode)
 
430
        path = path.decode('utf-8')
 
431
        name = name.decode("utf-8")
430
432
        file_id = self._fileid_map.lookup_file_id(path)
431
 
        ie = entry_factory[kind](file_id, name.decode("utf-8"), parent_id)
 
433
        ie = entry_factory[kind](file_id, name, parent_id)
432
434
        if kind == 'symlink':
433
435
            ie.symlink_target = store[hexsha].data.decode('utf-8')
434
436
        elif kind == 'tree-reference':
441
443
        return ie
442
444
 
443
445
    def _get_dir_ie(self, path, parent_id):
 
446
        path = path.decode('utf-8')
444
447
        file_id = self._fileid_map.lookup_file_id(path)
445
 
        return GitTreeDirectory(file_id,
446
 
            posixpath.basename(path).decode("utf-8"), parent_id)
 
448
        return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
447
449
 
448
450
    def iter_child_entries(self, path, file_id=None):
449
451
        (store, mode, tree_sha) = self._lookup_path(path)
619
621
        target_extras = set()
620
622
    ret = delta.TreeDelta()
621
623
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
622
 
        if newpath == u'' and not include_root:
 
624
        if newpath == b'' and not include_root:
623
625
            continue
624
626
        if not (specific_files is None or
625
627
                (oldpath is not None and osutils.is_inside_or_parent_of_any(specific_files, oldpath)) or
636
638
                ret.unversioned.append(
637
639
                    (osutils.normalized_filename(newpath)[0], None, mode_kind(newmode)))
638
640
            else:
639
 
                file_id = new_fileid_map.lookup_file_id(newpath)
640
 
                ret.added.append((newpath.decode('utf-8'), file_id, mode_kind(newmode)))
 
641
                newpath_decoded = newpath.decode('utf-8')
 
642
                file_id = new_fileid_map.lookup_file_id(newpath_decoded)
 
643
                ret.added.append((newpath_decoded, file_id, mode_kind(newmode)))
641
644
        elif newpath is None or newmode == 0:
642
 
            file_id = old_fileid_map.lookup_file_id(oldpath)
643
 
            ret.removed.append((oldpath.decode('utf-8'), file_id, mode_kind(oldmode)))
 
645
            oldpath_decoded = oldpath.decode('utf-8')
 
646
            file_id = old_fileid_map.lookup_file_id(oldpath_decoded)
 
647
            ret.removed.append((oldpath_decoded, file_id, mode_kind(oldmode)))
644
648
        elif oldpath != newpath:
645
 
            file_id = old_fileid_map.lookup_file_id(oldpath)
 
649
            oldpath_decoded = oldpath.decode('utf-8')
 
650
            file_id = old_fileid_map.lookup_file_id(oldpath_decoded)
646
651
            ret.renamed.append(
647
 
                (oldpath.decode('utf-8'), newpath.decode('utf-8'), file_id,
 
652
                (oldpath_decoded, newpath.decode('utf-8'), file_id,
648
653
                mode_kind(newmode), (oldsha != newsha),
649
654
                (oldmode != newmode)))
650
655
        elif mode_kind(oldmode) != mode_kind(newmode):
651
 
            file_id = new_fileid_map.lookup_file_id(newpath)
 
656
            newpath_decoded = newpath.decode('utf-8')
 
657
            file_id = new_fileid_map.lookup_file_id(newpath_decoded)
652
658
            ret.kind_changed.append(
653
 
                (newpath.decode('utf-8'), file_id, mode_kind(oldmode),
 
659
                (newpath_decoded, file_id, mode_kind(oldmode),
654
660
                mode_kind(newmode)))
655
661
        elif oldsha != newsha or oldmode != newmode:
656
662
            if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
657
663
                continue
658
 
            file_id = new_fileid_map.lookup_file_id(newpath)
 
664
            newpath_decoded = newpath.decode('utf-8')
 
665
            file_id = new_fileid_map.lookup_file_id(newpath_decoded)
659
666
            ret.modified.append(
660
 
                (newpath.decode('utf-8'), file_id, mode_kind(newmode),
 
667
                (newpath, file_id, mode_kind(newmode),
661
668
                (oldsha != newsha), (oldmode != newmode)))
662
669
        else:
663
 
            file_id = new_fileid_map.lookup_file_id(newpath)
664
 
            ret.unchanged.append((newpath.decode('utf-8'), file_id, mode_kind(newmode)))
 
670
            newpath_decoded = newpath.decode('utf-8')
 
671
            file_id = new_fileid_map.lookup_file_id(newpath_decoded)
 
672
            ret.unchanged.append((newpath, file_id, mode_kind(newmode)))
665
673
 
666
674
    return ret
667
675
 
841
849
            return (subpath in index or self._has_dir(path))
842
850
 
843
851
    def _has_dir(self, path):
844
 
        if path == "":
 
852
        if not isinstance(path, bytes):
 
853
            raise TypeError(path)
 
854
        if path == b"":
845
855
            return True
846
856
        if self._versioned_dirs is None:
847
857
            self._load_dirs()
856
866
            self._ensure_versioned_dir(posixpath.dirname(p))
857
867
 
858
868
    def _ensure_versioned_dir(self, dirname):
 
869
        if not isinstance(dirname, bytes):
 
870
            raise TypeError(dirname)
859
871
        if dirname in self._versioned_dirs:
860
872
            return
861
 
        if dirname != "":
 
873
        if dirname != b"":
862
874
            self._ensure_versioned_dir(posixpath.dirname(dirname))
863
875
        self._versioned_dirs.add(dirname)
864
876
 
866
878
        with self.lock_read():
867
879
            path = path.rstrip('/')
868
880
            if self.is_versioned(path.rstrip('/')):
869
 
                return self._fileid_map.lookup_file_id(path.encode("utf-8"))
 
881
                return self._fileid_map.lookup_file_id(osutils.safe_unicode(path))
870
882
            return None
871
883
 
872
884
    def has_id(self, file_id):
982
994
        if self._versioned_dirs is not None:
983
995
            self._ensure_versioned_dir(index_path)
984
996
 
985
 
    def _recurse_index_entries(self, index=None, basepath=""):
 
997
    def _recurse_index_entries(self, index=None, basepath=b""):
986
998
        # Iterate over all index entries
987
999
        with self.lock_read():
988
1000
            if index is None:
1046
1058
            raise TypeError(value)
1047
1059
        (ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
1048
1060
        file_id = self.path2id(path)
1049
 
        if type(file_id) != str:
1050
 
            raise AssertionError
 
1061
        if not isinstance(file_id, bytes):
 
1062
            raise TypeError(file_id)
1051
1063
        kind = mode_kind(mode)
1052
1064
        ie = entry_factory[kind](file_id, name, parent_id)
1053
1065
        if kind == 'symlink':