/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-06-27 14:48:19 UTC
  • mto: (7490.40.32 work)
  • mto: This revision was merged to the branch mainline in revision 7519.
  • Revision ID: jelmer@jelmer.uk-20200627144819-saq1xtc00v73w5q7
Add functions for encoding/decoding git paths.

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
    )
73
73
 
74
74
from .mapping import (
 
75
    encode_git_path,
 
76
    decode_git_path,
75
77
    mode_is_executable,
76
78
    mode_kind,
77
79
    default_mapping,
302
304
            info = self._submodule_info()[relpath]
303
305
        except KeyError:
304
306
            nested_repo_transport = self._repository.controldir.user_transport.clone(
305
 
                relpath.decode('utf-8'))
 
307
                decode_git_path(relpath))
306
308
        else:
307
309
            nested_repo_transport = self._repository.controldir.control_transport.clone(
308
 
                posixpath.join('modules', info[1].decode('utf-8')))
 
310
                posixpath.join('modules', decode_git_path(info[1])))
309
311
        nested_controldir = _mod_controldir.ControlDir.open_from_transport(
310
312
            nested_repo_transport)
311
313
        return nested_controldir.find_repository()
314
316
        return self._get_submodule_repository(relpath)._git.object_store
315
317
 
316
318
    def get_nested_tree(self, path):
317
 
        encoded_path = path.encode('utf-8')
 
319
        encoded_path = encode_git_path(path)
318
320
        nested_repo = self._get_submodule_repository(encoded_path)
319
321
        ref_rev = self.get_reference_revision(path)
320
322
        return nested_repo.revision_tree(ref_rev)
327
329
        if self.commit_id == ZERO_SHA:
328
330
            return NULL_REVISION
329
331
        (unused_path, commit_id) = change_scanner.find_last_change_revision(
330
 
            path.encode('utf-8'), self.commit_id)
 
332
            encode_git_path(path), self.commit_id)
331
333
        return self._repository.lookup_foreign_revision_id(
332
334
            commit_id, self.mapping)
333
335
 
374
376
            tree = store[tree_id]
375
377
            for name, mode, hexsha in tree.items():
376
378
                subpath = posixpath.join(path, name)
377
 
                ret.add(subpath.decode('utf-8'))
 
379
                ret.add(decode_git_path(subpath))
378
380
                if stat.S_ISDIR(mode):
379
381
                    todo.append((store, subpath, hexsha))
380
382
        return ret
482
484
                else:
483
485
                    ie = self._get_file_ie(
484
486
                        store, child_path, name, mode, hexsha, parent_id)
485
 
                yield (child_relpath.decode('utf-8'), "V", ie.kind, ie)
 
487
                yield (decode_git_path(child_relpath), "V", ie.kind, ie)
486
488
 
487
489
    def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
488
490
        if not isinstance(path, bytes):
490
492
        if not isinstance(name, bytes):
491
493
            raise TypeError(name)
492
494
        kind = mode_kind(mode)
493
 
        path = path.decode('utf-8')
494
 
        name = name.decode("utf-8")
 
495
        path = decode_git_path(path)
 
496
        name = decode_git_path(name)
495
497
        file_id = self.mapping.generate_file_id(path)
496
498
        ie = entry_factory[kind](file_id, name, parent_id)
497
499
        if kind == 'symlink':
498
 
            ie.symlink_target = store[hexsha].data.decode('utf-8')
 
500
            ie.symlink_target = decode_git_path(store[hexsha].data)
499
501
        elif kind == 'tree-reference':
500
502
            ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(
501
503
                hexsha)
507
509
        return ie
508
510
 
509
511
    def _get_dir_ie(self, path, parent_id):
510
 
        path = path.decode('utf-8')
 
512
        path = decode_git_path(path)
511
513
        file_id = self.mapping.generate_file_id(path)
512
514
        return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
513
515
 
517
519
        if mode is not None and not stat.S_ISDIR(mode):
518
520
            return
519
521
 
520
 
        encoded_path = path.encode('utf-8')
 
522
        encoded_path = encode_git_path(path)
521
523
        file_id = self.path2id(path)
522
524
        tree = store[tree_sha]
523
525
        for name, mode, hexsha in tree.iteritems():
538
540
            if specific_files in ([""], []):
539
541
                specific_files = None
540
542
            else:
541
 
                specific_files = set([p.encode('utf-8')
 
543
                specific_files = set([encode_git_path(p)
542
544
                                      for p in specific_files])
543
545
        todo = deque([(self.store, b"", self.tree, self.path2id(''))])
544
546
        if specific_files is None or u"" in specific_files:
551
553
                if self.mapping.is_special_file(name):
552
554
                    continue
553
555
                child_path = posixpath.join(path, name)
554
 
                child_path_decoded = child_path.decode('utf-8')
 
556
                child_path_decoded = decode_git_path(child_path)
555
557
                if recurse_nested and S_ISGITLINK(mode):
556
558
                    mode = stat.S_IFDIR
557
559
                    store = self._get_submodule_store(child_path)
610
612
        """See RevisionTree.get_symlink_target."""
611
613
        (store, mode, hexsha) = self._lookup_path(path)
612
614
        if stat.S_ISLNK(mode):
613
 
            return store[hexsha].data.decode('utf-8')
 
615
            return decode_git_path(store[hexsha].data)
614
616
        else:
615
617
            return None
616
618
 
619
621
        (store, mode, hexsha) = self._lookup_path(path)
620
622
        if S_ISGITLINK(mode):
621
623
            try:
622
 
                nested_repo = self._get_submodule_repository(path.encode('utf-8'))
 
624
                nested_repo = self._get_submodule_repository(encode_git_path(path))
623
625
            except errors.NotBranchError:
624
626
                return self.mapping.revision_id_foreign_to_bzr(hexsha)
625
627
            else:
645
647
            return (kind, len(contents), executable,
646
648
                    osutils.sha_string(contents))
647
649
        elif kind == 'symlink':
648
 
            return (kind, None, None, store[hexsha].data.decode('utf-8'))
 
650
            return (kind, None, None, decode_git_path(store[hexsha].data))
649
651
        elif kind == 'tree-reference':
650
 
            nested_repo = self._get_submodule_repository(path.encode('utf-8'))
 
652
            nested_repo = self._get_submodule_repository(encode_git_path(path))
651
653
            return (kind, None, None,
652
654
                    nested_repo.lookup_foreign_revision_id(hexsha))
653
655
        else:
703
705
    def walkdirs(self, prefix=u""):
704
706
        (store, mode, hexsha) = self._lookup_path(prefix)
705
707
        todo = deque(
706
 
            [(store, prefix.encode('utf-8'), hexsha, self.path2id(prefix))])
 
708
            [(store, encode_git_path(prefix), hexsha, self.path2id(prefix))])
707
709
        while todo:
708
710
            store, path, tree_sha, parent_id = todo.popleft()
709
 
            path_decoded = path.decode('utf-8')
 
711
            path_decoded = decode_git_path(path)
710
712
            tree = store[tree_sha]
711
713
            children = []
712
714
            for name, mode, hexsha in tree.iteritems():
713
715
                if self.mapping.is_special_file(name):
714
716
                    continue
715
717
                child_path = posixpath.join(path, name)
716
 
                file_id = self.path2id(child_path.decode('utf-8'))
 
718
                file_id = self.path2id(decode_git_path(child_path))
717
719
                if stat.S_ISDIR(mode):
718
720
                    todo.append((store, child_path, hexsha, file_id))
719
721
                children.append(
720
 
                    (child_path.decode('utf-8'), name.decode('utf-8'),
 
722
                    (decode_git_path(child_path), decode_git_path(name),
721
723
                        mode_kind(mode), None,
722
724
                        file_id, mode_kind(mode)))
723
725
            yield (path_decoded, parent_id), children
746
748
            continue
747
749
        copied = (change_type == 'copy')
748
750
        if oldpath is not None:
749
 
            oldpath_decoded = oldpath.decode('utf-8')
 
751
            oldpath_decoded = decode_git_path(oldpath)
750
752
        else:
751
753
            oldpath_decoded = None
752
754
        if newpath is not None:
753
 
            newpath_decoded = newpath.decode('utf-8')
 
755
            newpath_decoded = decode_git_path(newpath)
754
756
        else:
755
757
            newpath_decoded = None
756
758
        if not (specific_files is None or
887
889
        (oldpath, oldmode, oldsha) = old
888
890
        (newpath, newmode, newsha) = new
889
891
        if oldpath is not None:
890
 
            oldpath_decoded = oldpath.decode('utf-8')
 
892
            oldpath_decoded = decode_git_path(oldpath)
891
893
        else:
892
894
            oldpath_decoded = None
893
895
        if newpath is not None:
894
 
            newpath_decoded = newpath.decode('utf-8')
 
896
            newpath_decoded = decode_git_path(newpath)
895
897
        else:
896
898
            newpath_decoded = None
897
899
        if not (specific_files is None or
1115
1117
 
1116
1118
    def is_versioned(self, path):
1117
1119
        with self.lock_read():
1118
 
            path = path.rstrip('/').encode('utf-8')
 
1120
            path = encode_git_path(path.rstrip('/'))
1119
1121
            (index, subpath) = self._lookup_index(path)
1120
1122
            return (subpath in index or self._has_dir(path))
1121
1123
 
1281
1283
            raise AssertionError("unknown kind '%s'" % kind)
1282
1284
        # Add an entry to the index or update the existing entry
1283
1285
        ensure_normalized_path(path)
1284
 
        encoded_path = path.encode("utf-8")
 
1286
        encoded_path = encode_git_path(path)
1285
1287
        if b'\r' in encoded_path or b'\n' in encoded_path:
1286
1288
            # TODO(jelmer): Why do we need to do this?
1287
1289
            trace.mutter('ignoring path with invalid newline in it: %r', path)
1326
1328
                    recurse_nested=recurse_nested):
1327
1329
                if self.mapping.is_special_file(path):
1328
1330
                    continue
1329
 
                path = path.decode("utf-8")
 
1331
                path = decode_git_path(path)
1330
1332
                if specific_files is not None and path not in specific_files:
1331
1333
                    continue
1332
1334
                (parent, name) = posixpath.split(path)
1412
1414
    def _unversion_path(self, path):
1413
1415
        if self._lock_mode is None:
1414
1416
            raise errors.ObjectNotLocked(self)
1415
 
        encoded_path = path.encode("utf-8")
 
1417
        encoded_path = encode_git_path(path)
1416
1418
        count = 0
1417
1419
        (index, subpath) = self._lookup_index(encoded_path)
1418
1420
        try:
1445
1447
        for (old_path, new_path, file_id, ie) in delta:
1446
1448
            if old_path is not None:
1447
1449
                (index, old_subpath) = self._lookup_index(
1448
 
                    old_path.encode('utf-8'))
 
1450
                    encode_git_path(old_path))
1449
1451
                if old_subpath in index:
1450
1452
                    self._index_del_entry(index, old_subpath)
1451
1453
                    self._versioned_dirs = None
1471
1473
            return rename_tuples
1472
1474
 
1473
1475
    def rename_one(self, from_rel, to_rel, after=None):
1474
 
        from_path = from_rel.encode("utf-8")
 
1476
        from_path = encode_git_path(from_rel)
1475
1477
        to_rel, can_access = osutils.normalized_filename(to_rel)
1476
1478
        if not can_access:
1477
1479
            raise errors.InvalidNormalization(to_rel)
1478
 
        to_path = to_rel.encode("utf-8")
 
1480
        to_path = encode_git_path(to_rel)
1479
1481
        with self.lock_tree_write():
1480
1482
            if not after:
1481
1483
                # Perhaps it's already moved?
1601
1603
            return (kind, None, None, None)
1602
1604
 
1603
1605
    def stored_kind(self, relpath):
1604
 
        (index, index_path) = self._lookup_index(relpath.encode('utf-8'))
 
1606
        (index, index_path) = self._lookup_index(encode_git_path(relpath))
1605
1607
        if index is None:
1606
1608
            return kind
1607
1609
        try:
1711
1713
                    else:
1712
1714
                        mode &= ~0o111
1713
1715
                if live_entry.sha != index_entry.sha:
1714
 
                    rp = path.decode('utf-8')
 
1716
                    rp = decode_git_path(path)
1715
1717
                    if stat.S_ISREG(live_entry.mode):
1716
1718
                        blob = Blob()
1717
1719
                        with target.get_file(rp) as f:
1731
1733
            except UnicodeDecodeError:
1732
1734
                raise errors.BadFilenameEncoding(
1733
1735
                    e, osutils._fs_enc)
1734
 
            np = e.encode('utf-8')
 
1736
            np = encode_git_path(e)
1735
1737
            if np in blobs:
1736
1738
                continue
1737
1739
            st = target._lstat(e)