115
110
class GitTreeFile(_mod_tree.TreeFile):
117
__slots__ = ['file_id', 'name', 'parent_id', 'text_size',
118
'executable', 'git_sha1']
112
__slots__ = ['file_id', 'name', 'parent_id', 'text_size', 'text_sha1',
120
115
def __init__(self, file_id, name, parent_id, text_size=None,
121
git_sha1=None, executable=None):
116
text_sha1=None, executable=None):
122
117
self.file_id = file_id
124
119
self.parent_id = parent_id
125
120
self.text_size = text_size
126
self.git_sha1 = git_sha1
121
self.text_sha1 = text_sha1
127
122
self.executable = executable
135
130
self.file_id == other.file_id and
136
131
self.name == other.name and
137
132
self.parent_id == other.parent_id and
138
self.git_sha1 == other.git_sha1 and
133
self.text_sha1 == other.text_sha1 and
139
134
self.text_size == other.text_size and
140
135
self.executable == other.executable)
142
137
def __repr__(self):
143
138
return ("%s(file_id=%r, name=%r, parent_id=%r, text_size=%r, "
144
"git_sha1=%r, executable=%r)") % (
139
"text_sha1=%r, executable=%r)") % (
145
140
type(self).__name__, self.file_id, self.name, self.parent_id,
146
self.text_size, self.git_sha1, self.executable)
141
self.text_size, self.text_sha1, self.executable)
149
144
ret = self.__class__(
150
145
self.file_id, self.name, self.parent_id)
151
ret.git_sha1 = self.git_sha1
146
ret.text_sha1 = self.text_sha1
152
147
ret.text_size = self.text_size
153
148
ret.executable = self.executable
259
class GitTree(_mod_tree.Tree):
261
def iter_git_objects(self):
262
"""Iterate over all the objects in the tree.
264
:return :Yields tuples with (path, sha, mode)
266
raise NotImplementedError(self.iter_git_objects)
268
def git_snapshot(self, want_unversioned=False):
269
"""Snapshot a tree, and return tree object.
271
:return: Tree sha and set of extras
273
raise NotImplementedError(self.snapshot)
275
def preview_transform(self, pb=None):
276
from .transform import GitTransformPreview
277
return GitTransformPreview(self, pb=pb)
279
def find_related_paths_across_trees(self, paths, trees=[],
280
require_versioned=True):
283
if require_versioned:
284
trees = [self] + (trees if trees is not None else [])
288
if t.is_versioned(p):
293
raise errors.PathsNotVersionedError(unversioned)
294
return filter(self.is_versioned, paths)
296
def _submodule_info(self):
297
if self._submodules is None:
299
with self.get_file('.gitmodules') as f:
300
config = GitConfigFile.from_file(f)
303
for path, url, section in parse_submodules(config)}
304
except errors.NoSuchFile:
305
self._submodules = {}
306
return self._submodules
309
class GitRevisionTree(revisiontree.RevisionTree, GitTree):
250
class GitRevisionTree(revisiontree.RevisionTree):
310
251
"""Revision tree implementation based on Git objects."""
312
253
def __init__(self, repository, revision_id):
313
254
self._revision_id = revision_id
314
255
self._repository = repository
315
self._submodules = None
316
256
self.store = repository._git.object_store
317
257
if not isinstance(revision_id, bytes):
318
258
raise TypeError(revision_id)
321
261
if revision_id == NULL_REVISION:
323
263
self.mapping = default_mapping
264
self._fileid_map = GitFileIdMap(
326
269
commit = self.store[self.commit_id]
328
271
raise errors.NoSuchRevision(repository, revision_id)
329
272
self.tree = commit.tree
331
def git_snapshot(self, want_unversioned=False):
332
return self.tree, set()
334
def _get_submodule_repository(self, relpath):
335
if not isinstance(relpath, bytes):
336
raise TypeError(relpath)
338
info = self._submodule_info()[relpath]
340
nested_repo_transport = self._repository.controldir.user_transport.clone(
341
decode_git_path(relpath))
343
nested_repo_transport = self._repository.controldir.control_transport.clone(
344
posixpath.join('modules', decode_git_path(info[1])))
273
self._fileid_map = self.mapping.get_fileid_map(
274
self.store.__getitem__, self.tree)
276
def _get_nested_repository(self, path):
277
nested_repo_transport = self._repository.user_transport.clone(path)
345
278
nested_controldir = _mod_controldir.ControlDir.open_from_transport(
346
279
nested_repo_transport)
347
280
return nested_controldir.find_repository()
349
def _get_submodule_store(self, relpath):
350
return self._get_submodule_repository(relpath)._git.object_store
352
def get_nested_tree(self, path):
353
encoded_path = encode_git_path(path)
354
nested_repo = self._get_submodule_repository(encoded_path)
355
ref_rev = self.get_reference_revision(path)
356
return nested_repo.revision_tree(ref_rev)
358
282
def supports_rename_tracking(self):
410
334
tree = store[tree_id]
411
335
for name, mode, hexsha in tree.items():
412
336
subpath = posixpath.join(path, name)
413
ret.add(decode_git_path(subpath))
337
ret.add(subpath.decode('utf-8'))
414
338
if stat.S_ISDIR(mode):
415
339
todo.append((store, subpath, hexsha))
342
def get_root_id(self):
343
if self.tree is None:
345
return self.path2id("")
347
def has_or_had_id(self, file_id):
349
self.id2path(file_id)
350
except errors.NoSuchId:
354
def has_id(self, file_id):
356
path = self.id2path(file_id)
357
except errors.NoSuchId:
359
return self.has_filename(path)
418
361
def _lookup_path(self, path):
419
362
if self.tree is None:
420
363
raise errors.NoSuchFile(path)
422
encoded_path = encode_git_path(path)
423
parts = encoded_path.split(b'/')
427
for i, p in enumerate(parts):
431
if not isinstance(obj, Tree):
432
raise NotTreeError(hexsha)
434
mode, hexsha = obj[p]
436
raise errors.NoSuchFile(path)
437
if S_ISGITLINK(mode) and i != len(parts) - 1:
438
store = self._get_submodule_store(b'/'.join(parts[:i + 1]))
439
hexsha = store[hexsha].tree
440
return (store, mode, hexsha)
365
(mode, hexsha) = tree_lookup_path(
366
self.store.__getitem__, self.tree, path.encode('utf-8'))
368
raise errors.NoSuchFile(self, path)
370
return (self.store, mode, hexsha)
442
372
def is_executable(self, path):
443
373
(store, mode, hexsha) = self._lookup_path(path)
472
401
root_ie = self._get_dir_ie(b"", None)
474
403
parent_path = posixpath.dirname(from_dir)
475
parent_id = self.mapping.generate_file_id(parent_path)
404
parent_id = self._fileid_map.lookup_file_id(parent_path)
476
405
if mode_kind(mode) == 'directory':
477
root_ie = self._get_dir_ie(encode_git_path(from_dir), parent_id)
406
root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
479
408
root_ie = self._get_file_ie(
480
store, encode_git_path(from_dir),
409
store, from_dir.encode("utf-8"),
481
410
posixpath.basename(from_dir), mode, hexsha)
483
412
yield (from_dir, "V", root_ie.kind, root_ie)
485
414
if root_ie.kind == 'directory':
486
todo.append((store, encode_git_path(from_dir),
415
todo.append((store, from_dir.encode("utf-8"),
487
416
b"", hexsha, root_ie.file_id))
489
418
(store, path, relpath, hexsha, parent_id) = todo.pop()
514
439
if not isinstance(name, bytes):
515
440
raise TypeError(name)
516
441
kind = mode_kind(mode)
517
path = decode_git_path(path)
518
name = decode_git_path(name)
519
file_id = self.mapping.generate_file_id(path)
442
path = path.decode('utf-8')
443
name = name.decode("utf-8")
444
file_id = self._fileid_map.lookup_file_id(path)
520
445
ie = entry_factory[kind](file_id, name, parent_id)
521
446
if kind == 'symlink':
522
ie.symlink_target = decode_git_path(store[hexsha].data)
447
ie.symlink_target = store[hexsha].data.decode('utf-8')
523
448
elif kind == 'tree-reference':
524
449
ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(
452
data = store[hexsha].data
453
ie.text_sha1 = osutils.sha_string(data)
454
ie.text_size = len(data)
529
455
ie.executable = mode_is_executable(mode)
532
458
def _get_dir_ie(self, path, parent_id):
533
path = decode_git_path(path)
534
file_id = self.mapping.generate_file_id(path)
459
path = path.decode('utf-8')
460
file_id = self._fileid_map.lookup_file_id(path)
535
461
return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
537
463
def iter_child_entries(self, path):
553
479
yield self._get_file_ie(store, child_path, name, mode, hexsha,
556
def iter_entries_by_dir(self, specific_files=None,
557
recurse_nested=False):
482
def iter_entries_by_dir(self, specific_files=None, yield_parents=False):
558
483
if self.tree is None:
486
# TODO(jelmer): Support yield parents
487
raise NotImplementedError
560
488
if specific_files is not None:
561
489
if specific_files in ([""], []):
562
490
specific_files = None
564
specific_files = set([encode_git_path(p)
492
specific_files = set([p.encode('utf-8')
565
493
for p in specific_files])
566
todo = deque([(self.store, b"", self.tree, self.path2id(''))])
494
todo = deque([(self.store, b"", self.tree, self.get_root_id())])
567
495
if specific_files is None or u"" in specific_files:
568
496
yield u"", self._get_dir_ie(b"", None)
668
588
return (kind, len(contents), executable,
669
589
osutils.sha_string(contents))
670
590
elif kind == 'symlink':
671
return (kind, None, None, decode_git_path(store[hexsha].data))
591
return (kind, None, None, store[hexsha].data.decode('utf-8'))
672
592
elif kind == 'tree-reference':
673
nested_repo = self._get_submodule_repository(encode_git_path(path))
593
nested_repo = self._get_nested_repository(path)
674
594
return (kind, None, None,
675
595
nested_repo.lookup_foreign_revision_id(hexsha))
677
597
return (kind, None, None, None)
599
def find_related_paths_across_trees(self, paths, trees=[],
600
require_versioned=True):
603
if require_versioned:
604
trees = [self] + (trees if trees is not None else [])
608
if t.is_versioned(p):
613
raise errors.PathsNotVersionedError(unversioned)
614
return filter(self.is_versioned, paths)
679
616
def _iter_tree_contents(self, include_trees=False):
680
617
if self.tree is None:
709
646
def walkdirs(self, prefix=u""):
710
647
(store, mode, hexsha) = self._lookup_path(prefix)
712
[(store, encode_git_path(prefix), hexsha)])
649
[(store, prefix.encode('utf-8'), hexsha, self.path2id(prefix))])
714
store, path, tree_sha = todo.popleft()
715
path_decoded = decode_git_path(path)
651
store, path, tree_sha, parent_id = todo.popleft()
652
path_decoded = path.decode('utf-8')
716
653
tree = store[tree_sha]
718
655
for name, mode, hexsha in tree.iteritems():
719
656
if self.mapping.is_special_file(name):
721
658
child_path = posixpath.join(path, name)
659
file_id = self.path2id(child_path.decode('utf-8'))
722
660
if stat.S_ISDIR(mode):
723
todo.append((store, child_path, hexsha))
661
todo.append((store, child_path, hexsha, file_id))
725
(decode_git_path(child_path), decode_git_path(name),
663
(child_path.decode('utf-8'), name.decode('utf-8'),
726
664
mode_kind(mode), None,
728
yield path_decoded, children
731
def tree_delta_from_git_changes(changes, mappings,
665
file_id, mode_kind(mode)))
666
yield (path_decoded, parent_id), children
669
def tree_delta_from_git_changes(changes, mapping,
670
fileid_maps, specific_files=None,
733
671
require_versioned=False, include_root=False,
734
source_extras=None, target_extras=None):
735
673
"""Create a TreeDelta from two git trees.
737
675
source and target are iterators over tuples with:
738
676
(filename, sha, mode)
740
(old_mapping, new_mapping) = mappings
678
(old_fileid_map, new_fileid_map) = fileid_maps
741
679
if target_extras is None:
742
680
target_extras = set()
743
if source_extras is None:
744
source_extras = set()
745
681
ret = delta.TreeDelta()
747
for (change_type, old, new) in changes:
748
(oldpath, oldmode, oldsha) = old
749
(newpath, newmode, newsha) = new
683
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
750
684
if newpath == b'' and not include_root:
752
copied = (change_type == 'copy')
753
if oldpath is not None:
754
oldpath_decoded = decode_git_path(oldpath)
756
687
oldpath_decoded = None
757
if newpath is not None:
758
newpath_decoded = decode_git_path(newpath)
689
oldpath_decoded = oldpath.decode('utf-8')
760
691
newpath_decoded = None
693
newpath_decoded = newpath.decode('utf-8')
761
694
if not (specific_files is None or
762
695
(oldpath is not None and
763
696
osutils.is_inside_or_parent_of_any(
766
699
osutils.is_inside_or_parent_of_any(
767
700
specific_files, newpath_decoded))):
777
oldversioned = (oldpath not in source_extras)
779
oldexe = mode_is_executable(oldmode)
780
oldkind = mode_kind(oldmode)
788
(oldparentpath, oldname) = osutils.split(oldpath_decoded)
789
oldparent = old_mapping.generate_file_id(oldparentpath)
797
newversioned = (newpath not in target_extras)
799
newexe = mode_is_executable(newmode)
800
newkind = mode_kind(newmode)
804
if newpath_decoded == u'':
808
newparentpath, newname = osutils.split(newpath_decoded)
809
newparent = new_mapping.generate_file_id(newparentpath)
810
if oldversioned and not copied:
811
fileid = old_mapping.generate_file_id(oldpath_decoded)
813
fileid = new_mapping.generate_file_id(newpath_decoded)
816
if old_mapping.is_special_file(oldpath):
702
if mapping.is_special_file(oldpath):
818
if new_mapping.is_special_file(newpath):
704
if mapping.is_special_file(newpath):
820
706
if oldpath is None and newpath is None:
822
change = InventoryTreeChange(
823
fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
824
(oldversioned, newversioned),
825
(oldparent, newparent), (oldname, newname),
826
(oldkind, newkind), (oldexe, newexe),
828
if newpath is not None and not newversioned and newkind != 'directory':
829
change.file_id = None
830
ret.unversioned.append(change)
831
elif change_type == 'add':
832
added.append((newpath, newkind))
709
added.append((newpath, mode_kind(newmode)))
833
710
elif newpath is None or newmode == 0:
834
ret.removed.append(change)
835
elif change_type == 'delete':
836
ret.removed.append(change)
837
elif change_type == 'copy':
838
if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
840
ret.copied.append(change)
841
elif change_type == 'rename':
842
if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
844
ret.renamed.append(change)
711
file_id = old_fileid_map.lookup_file_id(oldpath_decoded)
712
ret.removed.append((oldpath_decoded, file_id, mode_kind(oldmode)))
713
elif oldpath != newpath:
714
file_id = old_fileid_map.lookup_file_id(oldpath_decoded)
716
(oldpath_decoded, newpath.decode('utf-8'), file_id,
717
mode_kind(newmode), (oldsha != newsha),
718
(oldmode != newmode)))
845
719
elif mode_kind(oldmode) != mode_kind(newmode):
846
ret.kind_changed.append(change)
720
file_id = new_fileid_map.lookup_file_id(newpath_decoded)
721
ret.kind_changed.append(
722
(newpath_decoded, file_id, mode_kind(oldmode),
847
724
elif oldsha != newsha or oldmode != newmode:
848
725
if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
850
ret.modified.append(change)
727
file_id = new_fileid_map.lookup_file_id(newpath_decoded)
729
(newpath_decoded, file_id, mode_kind(newmode),
730
(oldsha != newsha), (oldmode != newmode)))
852
ret.unchanged.append(change)
732
file_id = new_fileid_map.lookup_file_id(newpath_decoded)
733
ret.unchanged.append(
734
(newpath_decoded, file_id, mode_kind(newmode)))
854
736
implicit_dirs = {b''}
855
737
for path, kind in added:
860
742
for path, kind in added:
861
743
if kind == 'directory' and path not in implicit_dirs:
863
path_decoded = decode_git_path(path)
864
parent_path, basename = osutils.split(path_decoded)
865
parent_id = new_mapping.generate_file_id(parent_path)
866
file_id = new_mapping.generate_file_id(path_decoded)
869
file_id, (None, path_decoded), True,
872
(None, basename), (None, kind), (None, False)))
745
path_decoded = osutils.normalized_filename(path)[0]
746
if path in target_extras:
747
ret.unversioned.append((path_decoded, None, kind))
749
file_id = new_fileid_map.lookup_file_id(path_decoded)
750
ret.added.append((path_decoded, file_id, kind))
877
755
def changes_from_git_changes(changes, mapping, specific_files=None,
878
include_unchanged=False, source_extras=None,
756
include_unchanged=False, target_extras=None):
880
757
"""Create a iter_changes-like generator from a git stream.
882
759
source and target are iterators over tuples with:
885
762
if target_extras is None:
886
763
target_extras = set()
887
if source_extras is None:
888
source_extras = set()
889
for (change_type, old, new) in changes:
890
if change_type == 'unchanged' and not include_unchanged:
892
(oldpath, oldmode, oldsha) = old
893
(newpath, newmode, newsha) = new
764
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
894
765
if oldpath is not None:
895
oldpath_decoded = decode_git_path(oldpath)
766
oldpath_decoded = oldpath.decode('utf-8')
897
768
oldpath_decoded = None
898
769
if newpath is not None:
899
newpath_decoded = decode_git_path(newpath)
770
newpath_decoded = newpath.decode('utf-8')
901
772
newpath_decoded = None
902
773
if not (specific_files is None or
952
825
newparentpath, newname = osutils.split(newpath_decoded)
953
826
newparent = mapping.generate_file_id(newparentpath)
954
827
if (not include_unchanged and
955
oldkind == 'directory' and newkind == 'directory' and
828
oldkind == 'directory' and newkind == 'directory' and
956
829
oldpath_decoded == newpath_decoded):
958
if oldversioned and change_type != 'copy':
959
fileid = mapping.generate_file_id(oldpath_decoded)
961
fileid = mapping.generate_file_id(newpath_decoded)
964
if oldkind == 'directory' and newkind == 'directory':
967
modified = (oldsha != newsha) or (oldmode != newmode)
968
yield InventoryTreeChange(
969
fileid, (oldpath_decoded, newpath_decoded),
971
(oldversioned, newversioned),
972
(oldparent, newparent), (oldname, newname),
973
(oldkind, newkind), (oldexe, newexe),
974
copied=(change_type == 'copy'))
831
yield (fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
832
(oldversioned, newversioned),
833
(oldparent, newparent), (oldname, newname),
834
(oldkind, newkind), (oldexe, newexe))
977
837
class InterGitTrees(_mod_tree.InterTree):
981
841
_matching_to_tree_format = None
982
842
_test_mutable_trees_to_test_trees = None
984
def __init__(self, source, target):
985
super(InterGitTrees, self).__init__(source, target)
986
if self.source.store == self.target.store:
987
self.store = self.source.store
989
self.store = OverlayObjectStore(
990
[self.source.store, self.target.store])
991
self.rename_detector = RenameDetector(self.store)
994
845
def is_compatible(cls, source, target):
995
return isinstance(source, GitTree) and isinstance(target, GitTree)
846
return (isinstance(source, GitRevisionTree) and
847
isinstance(target, GitRevisionTree))
997
849
def compare(self, want_unchanged=False, specific_files=None,
998
850
extra_trees=None, require_versioned=False, include_root=False,
999
851
want_unversioned=False):
1000
852
with self.lock_read():
1001
changes, source_extras, target_extras = self._iter_git_changes(
853
changes, target_extras = self._iter_git_changes(
1002
854
want_unchanged=want_unchanged,
1003
855
require_versioned=require_versioned,
1004
856
specific_files=specific_files,
1005
857
extra_trees=extra_trees,
1006
858
want_unversioned=want_unversioned)
859
source_fileid_map = self.source._fileid_map
860
target_fileid_map = self.target._fileid_map
1007
861
return tree_delta_from_git_changes(
1008
changes, (self.source.mapping, self.target.mapping),
862
changes, self.target.mapping,
863
(source_fileid_map, target_fileid_map),
1009
864
specific_files=specific_files,
1010
include_root=include_root,
1011
source_extras=source_extras, target_extras=target_extras)
865
include_root=include_root, target_extras=target_extras)
1013
867
def iter_changes(self, include_unchanged=False, specific_files=None,
1014
868
pb=None, extra_trees=[], require_versioned=True,
1015
869
want_unversioned=False):
1016
870
with self.lock_read():
1017
changes, source_extras, target_extras = self._iter_git_changes(
871
changes, target_extras = self._iter_git_changes(
1018
872
want_unchanged=include_unchanged,
1019
873
require_versioned=require_versioned,
1020
874
specific_files=specific_files,
1024
878
changes, self.target.mapping,
1025
879
specific_files=specific_files,
1026
880
include_unchanged=include_unchanged,
1027
source_extras=source_extras,
1028
881
target_extras=target_extras)
1030
883
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1031
884
require_versioned=False, extra_trees=None,
1032
want_unversioned=False, include_trees=True):
885
want_unversioned=False):
886
raise NotImplementedError(self._iter_git_changes)
889
class InterGitRevisionTrees(InterGitTrees):
890
"""InterTree that works between two git revision trees."""
892
_matching_from_tree_format = None
893
_matching_to_tree_format = None
894
_test_mutable_trees_to_test_trees = None
897
def is_compatible(cls, source, target):
898
return (isinstance(source, GitRevisionTree) and
899
isinstance(target, GitRevisionTree))
901
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
902
require_versioned=True, extra_trees=None,
903
want_unversioned=False):
1033
904
trees = [self.source]
1034
905
if extra_trees is not None:
1035
906
trees.extend(extra_trees)
1037
908
specific_files = self.target.find_related_paths_across_trees(
1038
909
specific_files, trees,
1039
910
require_versioned=require_versioned)
1040
# TODO(jelmer): Restrict to specific_files, for performance reasons.
1041
with self.lock_read():
1042
from_tree_sha, from_extras = self.source.git_snapshot(
1043
want_unversioned=want_unversioned)
1044
to_tree_sha, to_extras = self.target.git_snapshot(
1045
want_unversioned=want_unversioned)
1046
changes = tree_changes(
1047
self.store, from_tree_sha, to_tree_sha,
1048
include_trees=include_trees,
1049
rename_detector=self.rename_detector,
1050
want_unchanged=want_unchanged, change_type_same=True)
1051
return changes, from_extras, to_extras
1053
def find_target_path(self, path, recurse='none'):
1054
ret = self.find_target_paths([path], recurse=recurse)
1057
def find_source_path(self, path, recurse='none'):
1058
ret = self.find_source_paths([path], recurse=recurse)
1061
def find_target_paths(self, paths, recurse='none'):
1064
changes = self._iter_git_changes(
1065
specific_files=paths, include_trees=False)[0]
1066
for (change_type, old, new) in changes:
1069
oldpath = decode_git_path(old[0])
1070
if oldpath in paths:
1071
ret[oldpath] = decode_git_path(new[0]) if new[0] else None
1074
if self.source.has_filename(path):
1075
if self.target.has_filename(path):
1080
raise errors.NoSuchFile(path)
1083
def find_source_paths(self, paths, recurse='none'):
1086
changes = self._iter_git_changes(
1087
specific_files=paths, include_trees=False)[0]
1088
for (change_type, old, new) in changes:
1091
newpath = decode_git_path(new[0])
1092
if newpath in paths:
1093
ret[newpath] = decode_git_path(old[0]) if old[0] else None
1096
if self.target.has_filename(path):
1097
if self.source.has_filename(path):
1102
raise errors.NoSuchFile(path)
1106
_mod_tree.InterTree.register_optimiser(InterGitTrees)
1109
class MutableGitIndexTree(mutabletree.MutableTree, GitTree):
912
if (self.source._repository._git.object_store !=
913
self.target._repository._git.object_store):
914
store = OverlayObjectStore(
915
[self.source._repository._git.object_store,
916
self.target._repository._git.object_store])
918
store = self.source._repository._git.object_store
919
return store.tree_changes(
920
self.source.tree, self.target.tree, want_unchanged=want_unchanged,
921
include_trees=True, change_type_same=True), set()
924
_mod_tree.InterTree.register_optimiser(InterGitRevisionTrees)
927
class MutableGitIndexTree(mutabletree.MutableTree):
1111
929
def __init__(self):
1112
930
self._lock_mode = None
1113
931
self._lock_count = 0
1114
932
self._versioned_dirs = None
1115
933
self._index_dirty = False
1116
self._submodules = None
1118
def git_snapshot(self, want_unversioned=False):
1119
return snapshot_workingtree(self, want_unversioned=want_unversioned)
1121
935
def is_versioned(self, path):
1122
936
with self.lock_read():
1123
path = encode_git_path(path.rstrip('/'))
937
path = path.rstrip('/').encode('utf-8')
1124
938
(index, subpath) = self._lookup_index(path)
1125
939
return (subpath in index or self._has_dir(path))
1153
968
with self.lock_read():
1154
969
path = path.rstrip('/')
1155
970
if self.is_versioned(path.rstrip('/')):
1156
return self.mapping.generate_file_id(
971
return self._fileid_map.lookup_file_id(
1157
972
osutils.safe_unicode(path))
1160
def id2path(self, file_id, recurse='down'):
975
def has_id(self, file_id):
977
self.id2path(file_id)
978
except errors.NoSuchId:
983
def id2path(self, file_id):
1161
984
if file_id is None:
1163
986
if type(file_id) is not bytes:
1164
987
raise TypeError(file_id)
1165
988
with self.lock_read():
1167
path = self.mapping.parse_file_id(file_id)
990
path = self._fileid_map.lookup_path(file_id)
1168
991
except ValueError:
1169
992
raise errors.NoSuchId(self, file_id)
1170
993
if self.is_versioned(path):
1189
1015
def _lookup_index(self, encoded_path):
1190
1016
if not isinstance(encoded_path, bytes):
1191
1017
raise TypeError(encoded_path)
1193
if encoded_path in self.index:
1194
return self.index, encoded_path
1195
# TODO(jelmer): Perhaps have a cache with paths under which some
1198
remaining_path = encoded_path
1200
parts = remaining_path.split(b'/')
1201
for i in range(1, len(parts)):
1202
basepath = b'/'.join(parts[:i])
1204
(ctime, mtime, dev, ino, mode, uid, gid, size, sha,
1205
flags) = index[basepath]
1209
if S_ISGITLINK(mode):
1210
index = self._get_submodule_index(basepath)
1211
remaining_path = b'/'.join(parts[i:])
1214
return index, remaining_path
1216
return index, remaining_path
1217
return index, remaining_path
1018
# TODO(jelmer): Look in other indexes
1019
return self.index, encoded_path
1219
1021
def _index_del_entry(self, index, path):
1220
1022
del index[path]
1221
1023
# TODO(jelmer): Keep track of dirty per index
1222
1024
self._index_dirty = True
1224
def _apply_index_changes(self, changes):
1225
for (path, kind, executability, reference_revision,
1226
symlink_target) in changes:
1227
if kind is None or kind == 'directory':
1228
(index, subpath) = self._lookup_index(
1229
encode_git_path(path))
1231
self._index_del_entry(index, subpath)
1235
self._versioned_dirs = None
1237
self._index_add_entry(
1239
reference_revision=reference_revision,
1240
symlink_target=symlink_target)
1243
def _index_add_entry(
1244
self, path, kind, flags=0, reference_revision=None,
1245
symlink_target=None):
1026
def _index_add_entry(self, path, kind, flags=0, reference_revision=None):
1246
1027
if kind == "directory":
1247
1028
# Git indexes don't contain directories
1249
elif kind == "file":
1252
1033
file, stat_val = self.get_file_with_stat(path)
1307
1087
if self._versioned_dirs is not None:
1308
1088
self._ensure_versioned_dir(index_path)
1310
def iter_git_objects(self):
1311
for p, entry in self._recurse_index_entries():
1312
yield p, entry.sha, entry.mode
1314
def _recurse_index_entries(self, index=None, basepath=b"",
1315
recurse_nested=False):
1090
def _recurse_index_entries(self, index=None, basepath=b""):
1316
1091
# Iterate over all index entries
1317
1092
with self.lock_read():
1318
1093
if index is None:
1319
1094
index = self.index
1320
1095
for path, value in index.items():
1096
yield (posixpath.join(basepath, path), value)
1321
1097
(ctime, mtime, dev, ino, mode, uid, gid, size, sha,
1323
if S_ISGITLINK(mode) and recurse_nested:
1324
subindex = self._get_submodule_index(path)
1325
for entry in self._recurse_index_entries(
1326
index=subindex, basepath=path,
1327
recurse_nested=recurse_nested):
1330
yield (posixpath.join(basepath, path), value)
1099
if S_ISGITLINK(mode):
1100
pass # TODO(jelmer): dive into submodule
1332
def iter_entries_by_dir(self, specific_files=None,
1333
recurse_nested=False):
1102
def iter_entries_by_dir(self, specific_files=None, yield_parents=False):
1104
raise NotImplementedError(self.iter_entries_by_dir)
1334
1105
with self.lock_read():
1335
1106
if specific_files is not None:
1336
1107
specific_files = set(specific_files)
1353
1123
file_ie = self._get_file_ie(name, path, value, None)
1354
1124
except errors.NoSuchFile:
1356
if specific_files is None:
1126
if yield_parents or specific_files is None:
1357
1127
for (dir_path, dir_ie) in self._add_missing_parent_ids(
1358
1128
parent, dir_ids):
1359
1129
ret[(posixpath.dirname(dir_path), dir_path)] = dir_ie
1360
1130
file_ie.parent_id = self.path2id(parent)
1361
1131
ret[(posixpath.dirname(path), path)] = file_ie
1362
# Special casing for directories
1364
for path in specific_files:
1365
key = (posixpath.dirname(path), path)
1366
if key not in ret and self.is_versioned(path):
1367
ret[key] = self._get_dir_ie(path, self.path2id(key[0]))
1368
return ((path, ie) for ((_, path), ie) in sorted(ret.items()))
1132
return ((path, ie) for ((_, path), ie) in sorted(viewitems(ret)))
1370
1134
def iter_references(self):
1371
if self.supports_tree_reference():
1372
# TODO(jelmer): Implement a more efficient version of this
1373
for path, entry in self.iter_entries_by_dir():
1374
if entry.kind == 'tree-reference':
1135
# TODO(jelmer): Implement a more efficient version of this
1136
for path, entry in self.iter_entries_by_dir():
1137
if entry.kind == 'tree-reference':
1138
yield path, self.mapping.generate_file_id(b'')
1377
1140
def _get_dir_ie(self, path, parent_id):
1378
1141
file_id = self.path2id(path)
1591
1383
return (kind, None, None, None)
1593
def stored_kind(self, relpath):
1596
(index, index_path) = self._lookup_index(encode_git_path(relpath))
1600
mode = index[index_path].mode
1603
if osutils.is_inside(
1604
decode_git_path(index_path), decode_git_path(p)):
1608
return mode_kind(mode)
1610
1385
def kind(self, relpath):
1611
1386
kind = osutils.file_kind(self.abspath(relpath))
1612
1387
if kind == 'directory':
1613
if self._directory_is_tree_reference(relpath):
1614
return 'tree-reference'
1388
(index, index_path) = self._lookup_index(relpath.encode('utf-8'))
1392
mode = index[index_path].mode
1396
if S_ISGITLINK(mode):
1397
return 'tree-reference'
1619
1402
def _live_entry(self, relpath):
1620
1403
raise NotImplementedError(self._live_entry)
1622
def transform(self, pb=None):
1623
from .transform import GitTreeTransform
1624
return GitTreeTransform(self, pb=pb)
1626
def has_changes(self, _from_tree=None):
1627
"""Quickly check that the tree contains at least one commitable change.
1629
:param _from_tree: tree to compare against to find changes (default to
1630
the basis tree and is intended to be used by tests).
1632
:return: True if a change is found. False otherwise
1406
class InterIndexGitTree(InterGitTrees):
1407
"""InterTree that works between a Git revision tree and an index."""
1409
def __init__(self, source, target):
1410
super(InterIndexGitTree, self).__init__(source, target)
1411
self._index = target.index
1414
def is_compatible(cls, source, target):
1415
return (isinstance(source, GitRevisionTree) and
1416
isinstance(target, MutableGitIndexTree))
1418
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1419
require_versioned=False, extra_trees=None,
1420
want_unversioned=False):
1421
trees = [self.source]
1422
if extra_trees is not None:
1423
trees.extend(extra_trees)
1424
if specific_files is not None:
1425
specific_files = self.target.find_related_paths_across_trees(
1426
specific_files, trees,
1427
require_versioned=require_versioned)
1428
# TODO(jelmer): Restrict to specific_files, for performance reasons.
1634
1429
with self.lock_read():
1635
# Check pending merges
1636
if len(self.get_parent_ids()) > 1:
1638
if _from_tree is None:
1639
_from_tree = self.basis_tree()
1640
changes = self.iter_changes(_from_tree)
1641
if self.supports_symlinks():
1642
# Fast path for has_changes.
1644
change = next(changes)
1645
if change.path[1] == '':
1648
except StopIteration:
1652
# Slow path for has_changes.
1653
# Handle platforms that do not support symlinks in the
1654
# conditional below. This is slower than the try/except
1655
# approach below that but we don't have a choice as we
1656
# need to be sure that all symlinks are removed from the
1657
# entire changeset. This is because in platforms that
1658
# do not support symlinks, they show up as None in the
1659
# working copy as compared to the repository.
1660
# Also, exclude root as mention in the above fast path.
1662
lambda c: c[6][0] != 'symlink' and c[4] != (None, None),
1666
except StopIteration:
1671
def snapshot_workingtree(target, want_unversioned=False):
1430
return changes_between_git_tree_and_working_copy(
1431
self.source.store, self.source.tree,
1432
self.target, want_unchanged=want_unchanged,
1433
want_unversioned=want_unversioned)
1436
_mod_tree.InterTree.register_optimiser(InterIndexGitTree)
1439
def changes_between_git_tree_and_working_copy(store, from_tree_sha, target,
1440
want_unchanged=False,
1441
want_unversioned=False):
1442
"""Determine the changes between a git tree and a working tree with index.
1674
1447
# Report dirified directories to commit_tree first, so that they can be
1675
1448
# replaced with non-empty directories if they have contents.
1677
trust_executable = target._supports_executable()
1678
1450
for path, index_entry in target._recurse_index_entries():
1680
1452
live_entry = target._live_entry(path)
1682
1454
if e.errno == errno.ENOENT:
1683
1455
# Entry was removed; keep it listed, but mark it as gone.
1684
1456
blobs[path] = (ZERO_SHA, 0)
1688
if live_entry is None:
1689
# Entry was turned into a directory.
1690
# Maybe it's just a submodule that's not checked out?
1457
elif e.errno == errno.EISDIR:
1458
# TODO(jelmer): Only do this if 'path' appears in .gitmodules?
1691
1459
if S_ISGITLINK(index_entry.mode):
1692
1460
blobs[path] = (index_entry.sha, index_entry.mode)
1462
# Entry was turned into a directory
1694
1463
dirified.append((path, Tree().id, stat.S_IFDIR))
1695
target.store.add_object(Tree())
1464
store.add_object(Tree())
1697
mode = live_entry.mode
1698
if not trust_executable:
1699
if mode_is_executable(index_entry.mode):
1703
if live_entry.sha != index_entry.sha:
1704
rp = decode_git_path(path)
1705
if stat.S_ISREG(live_entry.mode):
1707
with target.get_file(rp) as f:
1708
blob.data = f.read()
1709
elif stat.S_ISLNK(live_entry.mode):
1711
blob.data = target.get_symlink_target(rp).encode(osutils._fs_enc)
1714
if blob is not None:
1715
target.store.add_object(blob)
1716
blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
1468
blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
1717
1469
if want_unversioned:
1718
for extra in target._iter_files_recursive(include_dirs=False):
1470
for e in target.extras():
1471
st = target._lstat(e)
1720
extra, accessible = osutils.normalized_filename(extra)
1473
np, accessible = osutils.normalized_filename(e)
1721
1474
except UnicodeDecodeError:
1722
1475
raise errors.BadFilenameEncoding(
1723
extra, osutils._fs_enc)
1724
np = encode_git_path(extra)
1727
st = target._lstat(extra)
1728
1477
if stat.S_ISDIR(st.st_mode):
1730
elif stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode):
1731
1480
blob = blob_from_path_and_stat(
1732
target.abspath(extra).encode(osutils._fs_enc), st)
1735
target.store.add_object(blob)
1481
target.abspath(e).encode(osutils._fs_enc), st)
1482
store.add_object(blob)
1483
np = np.encode('utf-8')
1736
1484
blobs[np] = (blob.id, cleanup_mode(st.st_mode))
1739
target.store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()]), extras
1486
to_tree_sha = commit_tree(
1487
store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()])
1488
return store.tree_changes(
1489
from_tree_sha, to_tree_sha, include_trees=True,
1490
want_unchanged=want_unchanged, change_type_same=True), extras