/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: 2019-08-11 13:21:03 UTC
  • mfrom: (7379 work)
  • mto: This revision was merged to the branch mainline in revision 7388.
  • Revision ID: jelmer@jelmer.uk-20190811132103-u3ne03yf37c1h57n
merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
from .mapping import (
68
68
    mode_is_executable,
69
69
    mode_kind,
70
 
    GitFileIdMap,
71
70
    default_mapping,
72
71
    )
73
72
 
253
252
    def __init__(self, repository, revision_id):
254
253
        self._revision_id = revision_id
255
254
        self._repository = repository
 
255
        self._submodules = None
256
256
        self.store = repository._git.object_store
257
257
        if not isinstance(revision_id, bytes):
258
258
            raise TypeError(revision_id)
261
261
        if revision_id == NULL_REVISION:
262
262
            self.tree = None
263
263
            self.mapping = default_mapping
264
 
            self._fileid_map = GitFileIdMap(
265
 
                {},
266
 
                default_mapping)
267
264
        else:
268
265
            try:
269
266
                commit = self.store[self.commit_id]
270
267
            except KeyError:
271
268
                raise errors.NoSuchRevision(repository, revision_id)
272
269
            self.tree = commit.tree
273
 
            self._fileid_map = self.mapping.get_fileid_map(
274
 
                self.store.__getitem__, self.tree)
275
 
 
276
 
    def _get_nested_repository(self, path):
277
 
        nested_repo_transport = self._repository.user_transport.clone(path)
 
270
 
 
271
    def _submodule_info(self):
 
272
        if self._submodules is None:
 
273
            try:
 
274
                with self.get_file('.gitmodules') as f:
 
275
                    config = GitConfigFile.from_file(f)
 
276
                    self._submodules = {
 
277
                        path: (url, section)
 
278
                        for path, url, section in parse_submodules(config)}
 
279
            except errors.NoSuchFile:
 
280
                self._submodules = {}
 
281
        return self._submodules
 
282
 
 
283
    def _get_submodule_repository(self, relpath):
 
284
        if not isinstance(relpath, bytes):
 
285
            raise TypeError(relpath)
 
286
        try:
 
287
            info = self._submodule_info()[relpath]
 
288
        except KeyError:
 
289
            nested_repo_transport = self._repository.user_transport.clone(relpath.decode('utf-8'))
 
290
        else:
 
291
            nested_repo_transport = self._repository.control_transport.clone(
 
292
                posixpath.join('modules', info[0]))
278
293
        nested_controldir = _mod_controldir.ControlDir.open_from_transport(
279
294
            nested_repo_transport)
280
295
        return nested_controldir.find_repository()
281
296
 
 
297
    def get_nested_tree(self, path):
 
298
        encoded_path = path.encode('utf-8')
 
299
        nested_repo = self._get_submodule_repository(encoded_path)
 
300
        ref_rev = self.get_reference_revision(path)
 
301
        return nested_repo.revision_tree(ref_rev)
 
302
 
282
303
    def supports_rename_tracking(self):
283
304
        return False
284
305
 
304
325
 
305
326
    def id2path(self, file_id):
306
327
        try:
307
 
            path = self._fileid_map.lookup_path(file_id)
 
328
            path = self.mapping.parse_file_id(file_id)
308
329
        except ValueError:
309
330
            raise errors.NoSuchId(self, file_id)
310
331
        if self.is_versioned(path):
319
340
            return None
320
341
        if not self.is_versioned(path):
321
342
            return None
322
 
        return self._fileid_map.lookup_file_id(osutils.safe_unicode(path))
 
343
        return self.mapping.generate_file_id(osutils.safe_unicode(path))
323
344
 
324
345
    def all_file_ids(self):
325
346
        raise errors.UnsupportedOperation(self.all_file_ids, self)
339
360
                    todo.append((store, subpath, hexsha))
340
361
        return ret
341
362
 
342
 
    def get_root_id(self):
343
 
        if self.tree is None:
344
 
            return None
345
 
        return self.path2id("")
346
 
 
347
363
    def has_or_had_id(self, file_id):
348
364
        try:
349
365
            self.id2path(file_id)
401
417
            root_ie = self._get_dir_ie(b"", None)
402
418
        else:
403
419
            parent_path = posixpath.dirname(from_dir)
404
 
            parent_id = self._fileid_map.lookup_file_id(parent_path)
 
420
            parent_id = self.mapping.generate_file_id(parent_path)
405
421
            if mode_kind(mode) == 'directory':
406
422
                root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
407
423
            else:
441
457
        kind = mode_kind(mode)
442
458
        path = path.decode('utf-8')
443
459
        name = name.decode("utf-8")
444
 
        file_id = self._fileid_map.lookup_file_id(path)
 
460
        file_id = self.mapping.generate_file_id(path)
445
461
        ie = entry_factory[kind](file_id, name, parent_id)
446
462
        if kind == 'symlink':
447
463
            ie.symlink_target = store[hexsha].data.decode('utf-8')
457
473
 
458
474
    def _get_dir_ie(self, path, parent_id):
459
475
        path = path.decode('utf-8')
460
 
        file_id = self._fileid_map.lookup_file_id(path)
 
476
        file_id = self.mapping.generate_file_id(path)
461
477
        return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
462
478
 
463
479
    def iter_child_entries(self, path):
491
507
            else:
492
508
                specific_files = set([p.encode('utf-8')
493
509
                                      for p in specific_files])
494
 
        todo = deque([(self.store, b"", self.tree, self.get_root_id())])
 
510
        todo = deque([(self.store, b"", self.tree, self.path2id(''))])
495
511
        if specific_files is None or u"" in specific_files:
496
512
            yield u"", self._get_dir_ie(b"", None)
497
513
        while todo:
565
581
        """See RevisionTree.get_symlink_target."""
566
582
        (store, mode, hexsha) = self._lookup_path(path)
567
583
        if S_ISGITLINK(mode):
568
 
            nested_repo = self._get_nested_repository(path)
 
584
            nested_repo = self._get_submodule_repository(path.encode('utf-8'))
569
585
            return nested_repo.lookup_foreign_revision_id(hexsha)
570
586
        else:
571
587
            return None
590
606
        elif kind == 'symlink':
591
607
            return (kind, None, None, store[hexsha].data.decode('utf-8'))
592
608
        elif kind == 'tree-reference':
593
 
            nested_repo = self._get_nested_repository(path)
 
609
            nested_repo = self._get_submodule_repository(path.encode('utf-8'))
594
610
            return (kind, None, None,
595
611
                    nested_repo.lookup_foreign_revision_id(hexsha))
596
612
        else:
666
682
            yield (path_decoded, parent_id), children
667
683
 
668
684
 
669
 
def tree_delta_from_git_changes(changes, mapping,
670
 
                                fileid_maps, specific_files=None,
 
685
def tree_delta_from_git_changes(changes, mappings,
 
686
                                specific_files=None,
671
687
                                require_versioned=False, include_root=False,
672
688
                                target_extras=None):
673
689
    """Create a TreeDelta from two git trees.
675
691
    source and target are iterators over tuples with:
676
692
        (filename, sha, mode)
677
693
    """
678
 
    (old_fileid_map, new_fileid_map) = fileid_maps
 
694
    (old_mapping, new_mapping) = mappings
679
695
    if target_extras is None:
680
696
        target_extras = set()
681
697
    ret = delta.TreeDelta()
699
715
                    osutils.is_inside_or_parent_of_any(
700
716
                        specific_files, newpath_decoded))):
701
717
            continue
702
 
        if mapping.is_special_file(oldpath):
 
718
        if old_mapping.is_special_file(oldpath):
703
719
            oldpath = None
704
 
        if mapping.is_special_file(newpath):
 
720
        if new_mapping.is_special_file(newpath):
705
721
            newpath = None
706
722
        if oldpath is None and newpath is None:
707
723
            continue
708
724
        if oldpath is None:
709
725
            added.append((newpath, mode_kind(newmode)))
710
726
        elif newpath is None or newmode == 0:
711
 
            file_id = old_fileid_map.lookup_file_id(oldpath_decoded)
 
727
            file_id = old_mapping.generate_file_id(oldpath_decoded)
712
728
            ret.removed.append((oldpath_decoded, file_id, mode_kind(oldmode)))
713
729
        elif oldpath != newpath:
714
 
            file_id = old_fileid_map.lookup_file_id(oldpath_decoded)
 
730
            file_id = old_mapping.generate_file_id(oldpath_decoded)
715
731
            ret.renamed.append(
716
732
                (oldpath_decoded, newpath.decode('utf-8'), file_id,
717
733
                 mode_kind(newmode), (oldsha != newsha),
718
734
                 (oldmode != newmode)))
719
735
        elif mode_kind(oldmode) != mode_kind(newmode):
720
 
            file_id = new_fileid_map.lookup_file_id(newpath_decoded)
 
736
            file_id = new_mapping.generate_file_id(newpath_decoded)
721
737
            ret.kind_changed.append(
722
738
                (newpath_decoded, file_id, mode_kind(oldmode),
723
739
                 mode_kind(newmode)))
724
740
        elif oldsha != newsha or oldmode != newmode:
725
741
            if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
726
742
                continue
727
 
            file_id = new_fileid_map.lookup_file_id(newpath_decoded)
 
743
            file_id = new_mapping.generate_file_id(newpath_decoded)
728
744
            ret.modified.append(
729
745
                (newpath_decoded, file_id, mode_kind(newmode),
730
746
                 (oldsha != newsha), (oldmode != newmode)))
731
747
        else:
732
 
            file_id = new_fileid_map.lookup_file_id(newpath_decoded)
 
748
            file_id = new_mapping.generate_file_id(newpath_decoded)
733
749
            ret.unchanged.append(
734
750
                (newpath_decoded, file_id, mode_kind(newmode)))
735
751
 
746
762
        if path in target_extras:
747
763
            ret.unversioned.append((path_decoded, None, kind))
748
764
        else:
749
 
            file_id = new_fileid_map.lookup_file_id(path_decoded)
 
765
            file_id = new_mapping.generate_file_id(path_decoded)
750
766
            ret.added.append((path_decoded, file_id, kind))
751
767
 
752
768
    return ret
857
873
                specific_files=specific_files,
858
874
                extra_trees=extra_trees,
859
875
                want_unversioned=want_unversioned)
860
 
            source_fileid_map = self.source._fileid_map
861
 
            target_fileid_map = self.target._fileid_map
862
876
            return tree_delta_from_git_changes(
863
 
                changes, self.target.mapping,
864
 
                (source_fileid_map, target_fileid_map),
 
877
                changes, (self.source.mapping, self.target.mapping),
865
878
                specific_files=specific_files,
866
879
                include_root=include_root, target_extras=target_extras)
867
880
 
969
982
        with self.lock_read():
970
983
            path = path.rstrip('/')
971
984
            if self.is_versioned(path.rstrip('/')):
972
 
                return self._fileid_map.lookup_file_id(
 
985
                return self.mapping.generate_file_id(
973
986
                    osutils.safe_unicode(path))
974
987
            return None
975
988
 
988
1001
            raise TypeError(file_id)
989
1002
        with self.lock_read():
990
1003
            try:
991
 
                path = self._fileid_map.lookup_path(file_id)
 
1004
                path = self.mapping.parse_file_id(file_id)
992
1005
            except ValueError:
993
1006
                raise errors.NoSuchId(self, file_id)
994
1007
            if self.is_versioned(path):
998
1011
    def _set_root_id(self, file_id):
999
1012
        raise errors.UnsupportedOperation(self._set_root_id, self)
1000
1013
 
1001
 
    def get_root_id(self):
1002
 
        return self.path2id(u"")
1003
 
 
1004
1014
    def _add(self, files, ids, kinds):
1005
1015
        for (path, file_id, kind) in zip(files, ids, kinds):
1006
1016
            if file_id is not None: