263
247
def __init__(self, repository, revision_id):
264
248
self._revision_id = revision_id
265
249
self._repository = repository
266
self._submodules = None
267
250
self.store = repository._git.object_store
268
251
if not isinstance(revision_id, bytes):
269
252
raise TypeError(revision_id)
270
self.commit_id, self.mapping = repository.lookup_bzr_revision_id(
253
self.commit_id, self.mapping = repository.lookup_bzr_revision_id(revision_id)
272
254
if revision_id == NULL_REVISION:
274
256
self.mapping = default_mapping
257
self._fileid_map = GitFileIdMap(
277
262
commit = self.store[self.commit_id]
279
264
raise errors.NoSuchRevision(repository, revision_id)
280
265
self.tree = commit.tree
282
def _submodule_info(self):
283
if self._submodules is None:
285
with self.get_file('.gitmodules') as f:
286
config = GitConfigFile.from_file(f)
289
for path, url, section in parse_submodules(config)}
290
except errors.NoSuchFile:
291
self._submodules = {}
292
return self._submodules
294
def _get_submodule_repository(self, relpath):
295
if not isinstance(relpath, bytes):
296
raise TypeError(relpath)
298
info = self._submodule_info()[relpath]
300
nested_repo_transport = self._repository.controldir.user_transport.clone(
301
decode_git_path(relpath))
303
nested_repo_transport = self._repository.controldir.control_transport.clone(
304
posixpath.join('modules', decode_git_path(info[1])))
305
nested_controldir = _mod_controldir.ControlDir.open_from_transport(
306
nested_repo_transport)
266
self._fileid_map = self.mapping.get_fileid_map(self.store.__getitem__, self.tree)
268
def _get_nested_repository(self, path):
269
nested_repo_transport = self._repository.user_transport.clone(path)
270
nested_controldir = _mod_controldir.ControlDir.open_from_transport(nested_repo_transport)
307
271
return nested_controldir.find_repository()
309
def _get_submodule_store(self, relpath):
310
return self._get_submodule_repository(relpath)._git.object_store
312
def get_nested_tree(self, path):
313
encoded_path = encode_git_path(path)
314
nested_repo = self._get_submodule_repository(encoded_path)
315
ref_rev = self.get_reference_revision(path)
316
return nested_repo.revision_tree(ref_rev)
318
273
def supports_rename_tracking(self):
321
def get_file_revision(self, path):
276
def get_file_revision(self, path, file_id=None):
322
277
change_scanner = self._repository._file_change_scanner
323
278
if self.commit_id == ZERO_SHA:
324
279
return NULL_REVISION
325
(unused_path, commit_id) = change_scanner.find_last_change_revision(
326
encode_git_path(path), self.commit_id)
327
return self._repository.lookup_foreign_revision_id(
328
commit_id, self.mapping)
280
(path, commit_id) = change_scanner.find_last_change_revision(
281
path.encode('utf-8'), self.commit_id)
282
return self._repository.lookup_foreign_revision_id(commit_id, self.mapping)
330
def get_file_mtime(self, path):
284
def get_file_mtime(self, path, file_id=None):
332
revid = self.get_file_revision(path)
286
revid = self.get_file_revision(path, file_id)
334
raise errors.NoSuchFile(path)
288
raise _mod_tree.FileTimestampUnavailable(path)
336
290
rev = self._repository.get_revision(revid)
337
291
except errors.NoSuchRevision:
338
292
raise _mod_tree.FileTimestampUnavailable(path)
339
293
return rev.timestamp
341
def id2path(self, file_id, recurse='down'):
295
def id2path(self, file_id):
343
path = self.mapping.parse_file_id(file_id)
297
path = self._fileid_map.lookup_path(file_id)
344
298
except ValueError:
345
299
raise errors.NoSuchId(self, file_id)
300
path = path.decode('utf-8')
346
301
if self.is_versioned(path):
348
303
raise errors.NoSuchId(self, file_id)
370
323
tree = store[tree_id]
371
324
for name, mode, hexsha in tree.items():
372
325
subpath = posixpath.join(path, name)
373
ret.add(decode_git_path(subpath))
374
326
if stat.S_ISDIR(mode):
375
todo.append((store, subpath, hexsha))
327
todo.add((store, subpath, hexsha))
332
def get_root_id(self):
333
if self.tree is None:
335
return self.path2id("")
337
def has_or_had_id(self, file_id):
339
path = self.id2path(file_id)
340
except errors.NoSuchId:
344
def has_id(self, file_id):
346
path = self.id2path(file_id)
347
except errors.NoSuchId:
349
return self.has_filename(path)
378
351
def _lookup_path(self, path):
379
352
if self.tree is None:
380
353
raise errors.NoSuchFile(path)
382
encoded_path = encode_git_path(path)
383
parts = encoded_path.split(b'/')
387
for i, p in enumerate(parts):
391
if not isinstance(obj, Tree):
392
raise NotTreeError(hexsha)
394
mode, hexsha = obj[p]
396
raise errors.NoSuchFile(path)
397
if S_ISGITLINK(mode) and i != len(parts) - 1:
398
store = self._get_submodule_store(b'/'.join(parts[:i + 1]))
399
hexsha = store[hexsha].tree
400
return (store, mode, hexsha)
402
def is_executable(self, path):
355
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
356
path.encode('utf-8'))
358
raise errors.NoSuchFile(self, path)
360
return (self.store, mode, hexsha)
362
def is_executable(self, path, file_id=None):
403
363
(store, mode, hexsha) = self._lookup_path(path)
405
365
# the tree root is a directory
407
367
return mode_is_executable(mode)
409
def kind(self, path):
369
def kind(self, path, file_id=None):
410
370
(store, mode, hexsha) = self._lookup_path(path)
412
372
# the tree root is a directory
424
def _submodule_info(self):
425
if self._submodules is None:
427
with self.get_file('.gitmodules') as f:
428
config = GitConfigFile.from_file(f)
431
for path, url, section in parse_submodules(config)}
432
except errors.NoSuchFile:
433
self._submodules = {}
434
return self._submodules
436
def list_files(self, include_root=False, from_dir=None, recursive=True,
437
recurse_nested=False):
384
def list_files(self, include_root=False, from_dir=None, recursive=True):
438
385
if self.tree is None:
440
if from_dir is None or from_dir == '.':
442
389
(store, mode, hexsha) = self._lookup_path(from_dir)
443
if mode is None: # Root
390
if mode is None: # Root
444
391
root_ie = self._get_dir_ie(b"", None)
446
parent_path = posixpath.dirname(from_dir)
447
parent_id = self.mapping.generate_file_id(parent_path)
393
parent_path = posixpath.dirname(from_dir.encode("utf-8"))
394
parent_id = self._fileid_map.lookup_file_id(parent_path)
448
395
if mode_kind(mode) == 'directory':
449
root_ie = self._get_dir_ie(encode_git_path(from_dir), parent_id)
396
root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
451
root_ie = self._get_file_ie(
452
store, encode_git_path(from_dir),
398
root_ie = self._get_file_ie(store, from_dir.encode("utf-8"),
453
399
posixpath.basename(from_dir), mode, hexsha)
455
yield (from_dir, "V", root_ie.kind, root_ie)
400
if from_dir != "" or include_root:
401
yield (from_dir, "V", root_ie.kind, root_ie.file_id, root_ie)
457
403
if root_ie.kind == 'directory':
458
todo.append((store, encode_git_path(from_dir),
459
b"", hexsha, root_ie.file_id))
404
todo.add((store, from_dir.encode("utf-8"), hexsha, root_ie.file_id))
461
(store, path, relpath, hexsha, parent_id) = todo.pop()
406
(store, path, hexsha, parent_id) = todo.pop()
462
407
tree = store[hexsha]
463
408
for name, mode, hexsha in tree.iteritems():
464
409
if self.mapping.is_special_file(name):
466
411
child_path = posixpath.join(path, name)
467
child_relpath = posixpath.join(relpath, name)
468
if S_ISGITLINK(mode) and recurse_nested:
470
store = self._get_submodule_store(child_relpath)
471
hexsha = store[hexsha].tree
472
412
if stat.S_ISDIR(mode):
473
413
ie = self._get_dir_ie(child_path, parent_id)
476
(store, child_path, child_relpath, hexsha,
415
todo.add((store, child_path, hexsha, ie.file_id))
479
ie = self._get_file_ie(
480
store, child_path, name, mode, hexsha, parent_id)
481
yield (decode_git_path(child_relpath), "V", ie.kind, ie)
417
ie = self._get_file_ie(store, child_path, name, mode, hexsha, parent_id)
418
yield child_path.decode('utf-8'), "V", ie.kind, ie.file_id, ie
483
420
def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
484
if not isinstance(path, bytes):
421
if type(path) is not bytes:
485
422
raise TypeError(path)
486
if not isinstance(name, bytes):
423
if type(name) is not bytes:
487
424
raise TypeError(name)
488
425
kind = mode_kind(mode)
489
path = decode_git_path(path)
490
name = decode_git_path(name)
491
file_id = self.mapping.generate_file_id(path)
492
ie = entry_factory[kind](file_id, name, parent_id)
426
file_id = self._fileid_map.lookup_file_id(path)
427
ie = entry_factory[kind](file_id, name.decode("utf-8"), parent_id)
493
428
if kind == 'symlink':
494
ie.symlink_target = decode_git_path(store[hexsha].data)
429
ie.symlink_target = store[hexsha].data.decode('utf-8')
495
430
elif kind == 'tree-reference':
496
ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(
431
ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(hexsha)
499
433
data = store[hexsha].data
500
434
ie.text_sha1 = osutils.sha_string(data)
526
460
yield self._get_file_ie(store, child_path, name, mode, hexsha,
529
def iter_entries_by_dir(self, specific_files=None,
530
recurse_nested=False):
463
def iter_entries_by_dir(self, specific_files=None, yield_parents=False):
531
464
if self.tree is None:
467
# TODO(jelmer): Support yield parents
468
raise NotImplementedError
533
469
if specific_files is not None:
534
470
if specific_files in ([""], []):
535
471
specific_files = None
537
specific_files = set([encode_git_path(p)
538
for p in specific_files])
539
todo = deque([(self.store, b"", self.tree, self.path2id(''))])
540
if specific_files is None or u"" in specific_files:
541
yield u"", self._get_dir_ie(b"", None)
473
specific_files = set([p.encode('utf-8') for p in specific_files])
474
todo = set([(self.store, "", self.tree, None)])
543
store, path, tree_sha, parent_id = todo.popleft()
476
store, path, tree_sha, parent_id = todo.pop()
477
ie = self._get_dir_ie(path, parent_id)
478
if specific_files is None or path in specific_files:
479
yield path.decode("utf-8"), ie
544
480
tree = store[tree_sha]
546
481
for name, mode, hexsha in tree.iteritems():
547
482
if self.mapping.is_special_file(name):
549
484
child_path = posixpath.join(path, name)
550
child_path_decoded = decode_git_path(child_path)
551
if recurse_nested and S_ISGITLINK(mode):
553
store = self._get_submodule_store(child_path)
554
hexsha = store[hexsha].tree
555
485
if stat.S_ISDIR(mode):
556
486
if (specific_files is None or
557
any([p for p in specific_files if p.startswith(
560
(store, child_path, hexsha,
561
self.path2id(child_path_decoded)))
562
if specific_files is None or child_path in specific_files:
563
if stat.S_ISDIR(mode):
564
yield (child_path_decoded,
565
self._get_dir_ie(child_path, parent_id))
567
yield (child_path_decoded,
568
self._get_file_ie(store, child_path, name, mode,
570
todo.extendleft(reversed(extradirs))
572
def iter_references(self):
573
if self.supports_tree_reference():
574
for path, entry in self.iter_entries_by_dir():
575
if entry.kind == 'tree-reference':
487
any(filter(lambda p: p.startswith(child_path), specific_files))):
488
todo.add((store, child_path, hexsha, ie.file_id))
489
elif specific_files is None or child_path in specific_files:
490
yield (child_path.decode("utf-8"),
491
self._get_file_ie(store, child_path, name, mode, hexsha,
578
494
def get_revision_id(self):
579
495
"""See RevisionTree.get_revision_id."""
580
496
return self._revision_id
582
def get_file_sha1(self, path, stat_value=None):
498
def get_file_sha1(self, path, file_id=None, stat_value=None):
583
499
if self.tree is None:
584
500
raise errors.NoSuchFile(path)
585
return osutils.sha_string(self.get_file_text(path))
501
return osutils.sha_string(self.get_file_text(path, file_id))
587
def get_file_verifier(self, path, stat_value=None):
503
def get_file_verifier(self, path, file_id=None, stat_value=None):
588
504
(store, mode, hexsha) = self._lookup_path(path)
589
505
return ("GIT", hexsha)
591
def get_file_size(self, path):
592
(store, mode, hexsha) = self._lookup_path(path)
593
if stat.S_ISREG(mode):
594
return len(store[hexsha].data)
597
def get_file_text(self, path):
507
def get_file_text(self, path, file_id=None):
598
508
"""See RevisionTree.get_file_text."""
599
509
(store, mode, hexsha) = self._lookup_path(path)
600
510
if stat.S_ISREG(mode):
693
600
for key, line in annotator.annotate_flat(this_key)]
694
601
return annotations
696
def _get_rules_searcher(self, default_searcher):
697
return default_searcher
699
def walkdirs(self, prefix=u""):
700
(store, mode, hexsha) = self._lookup_path(prefix)
702
[(store, encode_git_path(prefix), hexsha, self.path2id(prefix))])
704
store, path, tree_sha, parent_id = todo.popleft()
705
path_decoded = decode_git_path(path)
706
tree = store[tree_sha]
708
for name, mode, hexsha in tree.iteritems():
709
if self.mapping.is_special_file(name):
711
child_path = posixpath.join(path, name)
712
file_id = self.path2id(decode_git_path(child_path))
713
if stat.S_ISDIR(mode):
714
todo.append((store, child_path, hexsha, file_id))
716
(decode_git_path(child_path), decode_git_path(name),
717
mode_kind(mode), None,
718
file_id, mode_kind(mode)))
719
yield (path_decoded, parent_id), children
721
def preview_transform(self, pb=None):
722
from .transform import GitTransformPreview
723
return GitTransformPreview(self, pb=pb)
726
def tree_delta_from_git_changes(changes, mappings,
728
require_versioned=False, include_root=False,
729
source_extras=None, target_extras=None):
604
def tree_delta_from_git_changes(changes, mapping,
605
fileid_maps, specific_files=None,
606
require_versioned=False, include_root=False,
730
608
"""Create a TreeDelta from two git trees.
732
610
source and target are iterators over tuples with:
733
611
(filename, sha, mode)
735
(old_mapping, new_mapping) = mappings
613
(old_fileid_map, new_fileid_map) = fileid_maps
736
614
if target_extras is None:
737
615
target_extras = set()
738
if source_extras is None:
739
source_extras = set()
740
616
ret = delta.TreeDelta()
742
for (change_type, old, new) in changes:
743
(oldpath, oldmode, oldsha) = old
744
(newpath, newmode, newsha) = new
745
if newpath == b'' and not include_root:
617
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
618
if newpath == u'' and not include_root:
747
copied = (change_type == 'copy')
748
if oldpath is not None:
749
oldpath_decoded = decode_git_path(oldpath)
751
oldpath_decoded = None
752
if newpath is not None:
753
newpath_decoded = decode_git_path(newpath)
755
newpath_decoded = None
756
620
if not (specific_files is None or
757
(oldpath is not None and
758
osutils.is_inside_or_parent_of_any(
759
specific_files, oldpath_decoded)) or
760
(newpath is not None and
761
osutils.is_inside_or_parent_of_any(
762
specific_files, newpath_decoded))):
621
(oldpath is not None and osutils.is_inside_or_parent_of_any(specific_files, oldpath)) or
622
(newpath is not None and osutils.is_inside_or_parent_of_any(specific_files, newpath))):
772
oldversioned = (oldpath not in source_extras)
774
oldexe = mode_is_executable(oldmode)
775
oldkind = mode_kind(oldmode)
783
(oldparentpath, oldname) = osutils.split(oldpath_decoded)
784
oldparent = old_mapping.generate_file_id(oldparentpath)
792
newversioned = (newpath not in target_extras)
794
newexe = mode_is_executable(newmode)
795
newkind = mode_kind(newmode)
799
if newpath_decoded == u'':
803
newparentpath, newname = osutils.split(newpath_decoded)
804
newparent = new_mapping.generate_file_id(newparentpath)
805
if oldversioned and not copied:
806
fileid = old_mapping.generate_file_id(oldpath_decoded)
808
fileid = new_mapping.generate_file_id(newpath_decoded)
811
if old_mapping.is_special_file(oldpath):
624
if mapping.is_special_file(oldpath):
813
if new_mapping.is_special_file(newpath):
626
if mapping.is_special_file(newpath):
815
628
if oldpath is None and newpath is None:
817
change = _mod_tree.TreeChange(
818
fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
819
(oldversioned, newversioned),
820
(oldparent, newparent), (oldname, newname),
821
(oldkind, newkind), (oldexe, newexe),
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':
827
added.append((newpath, newkind))
631
if newpath in target_extras:
632
ret.unversioned.append(
633
(osutils.normalized_filename(newpath)[0], None, mode_kind(newmode)))
635
file_id = new_fileid_map.lookup_file_id(newpath)
636
ret.added.append((newpath.decode('utf-8'), file_id, mode_kind(newmode)))
828
637
elif newpath is None or newmode == 0:
829
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):
835
ret.copied.append(change)
836
elif change_type == 'rename':
837
if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
839
ret.renamed.append(change)
638
file_id = old_fileid_map.lookup_file_id(oldpath)
639
ret.removed.append((oldpath.decode('utf-8'), file_id, mode_kind(oldmode)))
640
elif oldpath != newpath:
641
file_id = old_fileid_map.lookup_file_id(oldpath)
643
(oldpath.decode('utf-8'), newpath.decode('utf-8'), file_id,
644
mode_kind(newmode), (oldsha != newsha),
645
(oldmode != newmode)))
840
646
elif mode_kind(oldmode) != mode_kind(newmode):
841
ret.kind_changed.append(change)
647
file_id = new_fileid_map.lookup_file_id(newpath)
648
ret.kind_changed.append(
649
(newpath.decode('utf-8'), file_id, mode_kind(oldmode),
842
651
elif oldsha != newsha or oldmode != newmode:
843
652
if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
845
ret.modified.append(change)
654
file_id = new_fileid_map.lookup_file_id(newpath)
656
(newpath.decode('utf-8'), file_id, mode_kind(newmode),
657
(oldsha != newsha), (oldmode != newmode)))
847
ret.unchanged.append(change)
849
implicit_dirs = {b''}
850
for path, kind in added:
851
if kind == 'directory' or path in target_extras:
853
implicit_dirs.update(osutils.parent_directories(path))
855
for path, kind in added:
856
if kind == 'directory' and path not in implicit_dirs:
858
path_decoded = decode_git_path(path)
859
parent_path, basename = osutils.split(path_decoded)
860
parent_id = new_mapping.generate_file_id(parent_path)
861
file_id = new_mapping.generate_file_id(path_decoded)
863
_mod_tree.TreeChange(
864
file_id, (None, path_decoded), True,
867
(None, basename), (None, kind), (None, False)))
659
file_id = new_fileid_map.lookup_file_id(newpath)
660
ret.unchanged.append((newpath.decode('utf-8'), file_id, mode_kind(newmode)))
872
def changes_from_git_changes(changes, mapping, specific_files=None,
873
include_unchanged=False, source_extras=None,
665
def changes_from_git_changes(changes, mapping, specific_files=None, include_unchanged=False,
874
666
target_extras=None):
875
667
"""Create a iter_changes-like generator from a git stream.
880
672
if target_extras is None:
881
673
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:
887
(oldpath, oldmode, oldsha) = old
888
(newpath, newmode, newsha) = new
889
if oldpath is not None:
890
oldpath_decoded = decode_git_path(oldpath)
892
oldpath_decoded = None
893
if newpath is not None:
894
newpath_decoded = decode_git_path(newpath)
896
newpath_decoded = None
674
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
897
675
if not (specific_files is None or
898
(oldpath_decoded is not None and
899
osutils.is_inside_or_parent_of_any(
900
specific_files, oldpath_decoded)) or
901
(newpath_decoded is not None and
902
osutils.is_inside_or_parent_of_any(
903
specific_files, newpath_decoded))):
676
(oldpath is not None and osutils.is_inside_or_parent_of_any(specific_files, oldpath)) or
677
(newpath is not None and osutils.is_inside_or_parent_of_any(specific_files, newpath))):
679
path = (oldpath, newpath)
905
680
if oldpath is not None and mapping.is_special_file(oldpath):
907
682
if newpath is not None and mapping.is_special_file(newpath):
909
684
if oldpath is None:
685
fileid = mapping.generate_file_id(newpath)
914
690
oldversioned = False
916
oldversioned = (oldpath not in source_extras)
693
oldpath = oldpath.decode("utf-8")
918
695
oldexe = mode_is_executable(oldmode)
919
696
oldkind = mode_kind(oldmode)
923
if oldpath_decoded == u'':
927
(oldparentpath, oldname) = osutils.split(oldpath_decoded)
704
(oldparentpath, oldname) = osutils.split(oldpath)
928
705
oldparent = mapping.generate_file_id(oldparentpath)
706
fileid = mapping.generate_file_id(oldpath)
929
707
if newpath is None:
943
if newpath_decoded == u'':
721
newpath = newpath.decode("utf-8")
947
newparentpath, newname = osutils.split(newpath_decoded)
726
newparentpath, newname = osutils.split(newpath)
948
727
newparent = mapping.generate_file_id(newparentpath)
949
728
if (not include_unchanged and
950
oldkind == 'directory' and newkind == 'directory' and
951
oldpath_decoded == newpath_decoded):
729
oldkind == 'directory' and newkind == 'directory' and
953
if oldversioned and change_type != 'copy':
954
fileid = mapping.generate_file_id(oldpath_decoded)
956
fileid = mapping.generate_file_id(newpath_decoded)
959
yield _mod_tree.TreeChange(
960
fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
961
(oldversioned, newversioned),
962
(oldparent, newparent), (oldname, newname),
963
(oldkind, newkind), (oldexe, newexe),
964
copied=(change_type == 'copy'))
732
yield (fileid, (oldpath, newpath), (oldsha != newsha),
733
(oldversioned, newversioned),
734
(oldparent, newparent), (oldname, newname),
735
(oldkind, newkind), (oldexe, newexe))
967
738
class InterGitTrees(_mod_tree.InterTree):
980
751
extra_trees=None, require_versioned=False, include_root=False,
981
752
want_unversioned=False):
982
753
with self.lock_read():
983
changes, source_extras, target_extras = self._iter_git_changes(
984
want_unchanged=want_unchanged,
985
require_versioned=require_versioned,
986
specific_files=specific_files,
987
extra_trees=extra_trees,
988
want_unversioned=want_unversioned)
989
return tree_delta_from_git_changes(
990
changes, (self.source.mapping, self.target.mapping),
991
specific_files=specific_files,
992
include_root=include_root,
993
source_extras=source_extras, target_extras=target_extras)
754
changes, target_extras = self._iter_git_changes(
755
want_unchanged=want_unchanged,
756
require_versioned=require_versioned,
757
specific_files=specific_files,
758
extra_trees=extra_trees,
759
want_unversioned=want_unversioned)
760
source_fileid_map = self.source._fileid_map
761
target_fileid_map = self.target._fileid_map
762
return tree_delta_from_git_changes(changes, self.target.mapping,
763
(source_fileid_map, target_fileid_map),
764
specific_files=specific_files, include_root=include_root,
765
target_extras=target_extras)
995
767
def iter_changes(self, include_unchanged=False, specific_files=None,
996
768
pb=None, extra_trees=[], require_versioned=True,
997
769
want_unversioned=False):
998
770
with self.lock_read():
999
changes, source_extras, target_extras = self._iter_git_changes(
1000
want_unchanged=include_unchanged,
1001
require_versioned=require_versioned,
1002
specific_files=specific_files,
1003
extra_trees=extra_trees,
1004
want_unversioned=want_unversioned)
771
changes, target_extras = self._iter_git_changes(
772
want_unchanged=include_unchanged,
773
require_versioned=require_versioned,
774
specific_files=specific_files,
775
extra_trees=extra_trees,
776
want_unversioned=want_unversioned)
1005
777
return changes_from_git_changes(
1006
changes, self.target.mapping,
1007
specific_files=specific_files,
1008
include_unchanged=include_unchanged,
1009
source_extras=source_extras,
1010
target_extras=target_extras)
778
changes, self.target.mapping,
779
specific_files=specific_files,
780
include_unchanged=include_unchanged,
781
target_extras=target_extras)
1012
783
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1013
require_versioned=False, extra_trees=None,
1014
want_unversioned=False, include_trees=True):
784
require_versioned=False, extra_trees=None,
785
want_unversioned=False):
1015
786
raise NotImplementedError(self._iter_git_changes)
1017
def find_target_path(self, path, recurse='none'):
1018
ret = self.find_target_paths([path], recurse=recurse)
1021
def find_source_path(self, path, recurse='none'):
1022
ret = self.find_source_paths([path], recurse=recurse)
1025
def find_target_paths(self, paths, recurse='none'):
1028
changes = self._iter_git_changes(
1029
specific_files=paths, include_trees=False)[0]
1030
for (change_type, old, new) in changes:
1033
oldpath = decode_git_path(old[0])
1034
if oldpath in paths:
1035
ret[oldpath] = decode_git_path(new[0]) if new[0] else None
1038
if self.source.has_filename(path):
1039
if self.target.has_filename(path):
1044
raise errors.NoSuchFile(path)
1047
def find_source_paths(self, paths, recurse='none'):
1050
changes = self._iter_git_changes(
1051
specific_files=paths, include_trees=False)[0]
1052
for (change_type, old, new) in changes:
1055
newpath = decode_git_path(new[0])
1056
if newpath in paths:
1057
ret[newpath] = decode_git_path(old[0]) if old[0] else None
1060
if self.target.has_filename(path):
1061
if self.source.has_filename(path):
1066
raise errors.NoSuchFile(path)
1070
789
class InterGitRevisionTrees(InterGitTrees):
1071
790
"""InterTree that works between two git revision trees."""
1080
799
isinstance(target, GitRevisionTree))
1082
801
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1083
require_versioned=True, extra_trees=None,
1084
want_unversioned=False, include_trees=True):
802
require_versioned=True, extra_trees=None,
803
want_unversioned=False):
1085
804
trees = [self.source]
1086
805
if extra_trees is not None:
1087
806
trees.extend(extra_trees)
1088
807
if specific_files is not None:
1089
808
specific_files = self.target.find_related_paths_across_trees(
1090
specific_files, trees,
1091
require_versioned=require_versioned)
809
specific_files, trees,
810
require_versioned=require_versioned)
1093
if (self.source._repository._git.object_store !=
1094
self.target._repository._git.object_store):
1095
store = OverlayObjectStore(
1096
[self.source._repository._git.object_store,
1097
self.target._repository._git.object_store])
812
if self.source._repository._git.object_store != self.target._repository._git.object_store:
813
store = OverlayObjectStore([self.source._repository._git.object_store,
814
self.target._repository._git.object_store])
1099
816
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()
817
return self.source._repository._git.object_store.tree_changes(
818
self.source.tree, self.target.tree, want_unchanged=want_unchanged,
819
include_trees=True, change_type_same=True), set()
1108
822
_mod_tree.InterTree.register_optimiser(InterGitRevisionTrees)
1185
906
def _read_submodule_head(self, path):
1186
907
raise NotImplementedError(self._read_submodule_head)
1188
def _submodule_info(self):
1189
if self._submodules is None:
1191
with self.get_file('.gitmodules') as f:
1192
config = GitConfigFile.from_file(f)
1193
self._submodules = {
1194
path: (url, section)
1195
for path, url, section in parse_submodules(config)}
1196
except errors.NoSuchFile:
1197
self._submodules = {}
1198
return self._submodules
1200
909
def _lookup_index(self, encoded_path):
1201
910
if not isinstance(encoded_path, bytes):
1202
911
raise TypeError(encoded_path)
1204
if encoded_path in self.index:
1205
return self.index, encoded_path
1206
# TODO(jelmer): Perhaps have a cache with paths under which some
1209
remaining_path = encoded_path
1211
parts = remaining_path.split(b'/')
1212
for i in range(1, len(parts)):
1213
basepath = b'/'.join(parts[:i])
1215
(ctime, mtime, dev, ino, mode, uid, gid, size, sha,
1216
flags) = index[basepath]
1220
if S_ISGITLINK(mode):
1221
index = self._get_submodule_index(basepath)
1222
remaining_path = b'/'.join(parts[i:])
1225
return index, remaining_path
1227
return index, remaining_path
1228
return index, remaining_path
912
# TODO(jelmer): Look in other indexes
913
return self.index, encoded_path
1230
915
def _index_del_entry(self, index, path):
1295
978
if self._versioned_dirs is not None:
1296
979
self._ensure_versioned_dir(index_path)
1298
def _recurse_index_entries(self, index=None, basepath=b"",
1299
recurse_nested=False):
981
def _recurse_index_entries(self, index=None, basepath=""):
1300
982
# Iterate over all index entries
1301
983
with self.lock_read():
1302
984
if index is None:
1303
985
index = self.index
1304
for path, value in index.items():
1305
(ctime, mtime, dev, ino, mode, uid, gid, size, sha,
1307
if S_ISGITLINK(mode) and recurse_nested:
1308
subindex = self._get_submodule_index(path)
1309
for entry in self._recurse_index_entries(
1310
index=subindex, basepath=path,
1311
recurse_nested=recurse_nested):
1314
yield (posixpath.join(basepath, path), value)
1316
def iter_entries_by_dir(self, specific_files=None,
1317
recurse_nested=False):
986
for path, value in index.iteritems():
987
yield (posixpath.join(basepath, path), value)
988
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
989
if S_ISGITLINK(mode):
990
pass # TODO(jelmer): dive into submodule
993
def iter_entries_by_dir(self, specific_files=None, yield_parents=False):
995
raise NotImplementedError(self.iter_entries_by_dir)
1318
996
with self.lock_read():
1319
997
if specific_files is not None:
1320
998
specific_files = set(specific_files)
1323
1001
root_ie = self._get_dir_ie(u"", None)
1325
1003
if specific_files is None or u"" in specific_files:
1326
ret[(u"", u"")] = root_ie
1004
ret[(None, u"")] = root_ie
1327
1005
dir_ids = {u"": root_ie.file_id}
1328
for path, value in self._recurse_index_entries(
1329
recurse_nested=recurse_nested):
1006
for path, value in self._recurse_index_entries():
1330
1007
if self.mapping.is_special_file(path):
1332
path = decode_git_path(path)
1333
if specific_files is not None and path not in specific_files:
1009
path = path.decode("utf-8")
1010
if specific_files is not None and not path in specific_files:
1335
1012
(parent, name) = posixpath.split(path)
1337
1014
file_ie = self._get_file_ie(name, path, value, None)
1338
1015
except errors.NoSuchFile:
1340
if specific_files is None:
1341
for (dir_path, dir_ie) in self._add_missing_parent_ids(
1017
if yield_parents or specific_files is None:
1018
for (dir_path, dir_ie) in self._add_missing_parent_ids(parent,
1343
1020
ret[(posixpath.dirname(dir_path), dir_path)] = dir_ie
1344
1021
file_ie.parent_id = self.path2id(parent)
1345
1022
ret[(posixpath.dirname(path), path)] = file_ie
1346
# Special casing for directories
1348
for path in specific_files:
1349
key = (posixpath.dirname(path), path)
1350
if key not in ret and self.is_versioned(path):
1351
ret[key] = self._get_dir_ie(path, self.path2id(key[0]))
1352
1023
return ((path, ie) for ((_, path), ie) in sorted(ret.items()))
1354
1025
def iter_references(self):
1355
if self.supports_tree_reference():
1356
# TODO(jelmer): Implement a more efficient version of this
1357
for path, entry in self.iter_entries_by_dir():
1358
if entry.kind == 'tree-reference':
1026
# TODO(jelmer): Implement a more efficient version of this
1027
for path, entry in self.iter_entries_by_dir():
1028
if entry.kind == 'tree-reference':
1029
yield path, self.mapping.generate_file_id(b'')
1361
1031
def _get_dir_ie(self, path, parent_id):
1362
1032
file_id = self.path2id(path)
1363
1033
return GitTreeDirectory(file_id,
1364
posixpath.basename(path).strip("/"), parent_id)
1034
posixpath.basename(path).strip("/"), parent_id)
1366
1036
def _get_file_ie(self, name, path, value, parent_id):
1367
if not isinstance(name, str):
1037
if type(name) is not unicode:
1368
1038
raise TypeError(name)
1369
if not isinstance(path, str):
1039
if type(path) is not unicode:
1370
1040
raise TypeError(path)
1371
1041
if not isinstance(value, tuple) or len(value) != 10:
1372
1042
raise TypeError(value)
1373
1043
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
1374
1044
file_id = self.path2id(path)
1375
if not isinstance(file_id, bytes):
1376
raise TypeError(file_id)
1045
if type(file_id) != str:
1046
raise AssertionError
1377
1047
kind = mode_kind(mode)
1378
1048
ie = entry_factory[kind](file_id, name, parent_id)
1379
1049
if kind == 'symlink':
1380
ie.symlink_target = self.get_symlink_target(path)
1050
ie.symlink_target = self.get_symlink_target(path, file_id)
1381
1051
elif kind == 'tree-reference':
1382
ie.reference_revision = self.get_reference_revision(path)
1052
ie.reference_revision = self.get_reference_revision(path, file_id)
1385
data = self.get_file_text(path)
1055
data = self.get_file_text(path, file_id)
1386
1056
except errors.NoSuchFile:
1388
1058
except IOError as e:
1604
1269
return (kind, None, None, None)
1606
def stored_kind(self, relpath):
1607
(index, index_path) = self._lookup_index(encode_git_path(relpath))
1611
mode = index[index_path].mode
1615
if S_ISGITLINK(mode):
1616
return 'tree-reference'
1619
def kind(self, relpath):
1271
def kind(self, relpath, file_id=None):
1620
1272
kind = osutils.file_kind(self.abspath(relpath))
1621
1273
if kind == 'directory':
1622
if self._directory_is_tree_reference(relpath):
1623
return 'tree-reference'
1274
(index, index_path) = self._lookup_index(relpath.encode('utf-8'))
1276
mode = index[index_path].mode
1280
if S_ISGITLINK(mode):
1281
return 'tree-reference'
1628
1286
def _live_entry(self, relpath):
1629
1287
raise NotImplementedError(self._live_entry)
1631
def transform(self, pb=None):
1632
from .transform import GitTreeTransform
1633
return GitTreeTransform(self, pb=pb)
1635
def preview_transform(self, pb=None):
1636
from .transform import GitTransformPreview
1637
return GitTransformPreview(self, pb=pb)
1640
class InterToIndexGitTree(InterGitTrees):
1290
class InterIndexGitTree(InterGitTrees):
1641
1291
"""InterTree that works between a Git revision tree and an index."""
1643
1293
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
1648
self.store = OverlayObjectStore(
1649
[self.source.store, self.target.store])
1650
self.rename_detector = RenameDetector(self.store)
1294
super(InterIndexGitTree, self).__init__(source, target)
1295
self._index = target.index
1653
1298
def is_compatible(cls, source, target):
1655
1300
isinstance(target, MutableGitIndexTree))
1657
1302
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1658
require_versioned=False, extra_trees=None,
1659
want_unversioned=False, include_trees=True):
1303
require_versioned=False, extra_trees=None,
1304
want_unversioned=False):
1660
1305
trees = [self.source]
1661
1306
if extra_trees is not None:
1662
1307
trees.extend(extra_trees)
1663
1308
if specific_files is not None:
1664
1309
specific_files = self.target.find_related_paths_across_trees(
1665
specific_files, trees,
1666
require_versioned=require_versioned)
1310
specific_files, trees,
1311
require_versioned=require_versioned)
1667
1312
# TODO(jelmer): Restrict to specific_files, for performance reasons.
1668
1313
with self.lock_read():
1669
changes, target_extras = changes_between_git_tree_and_working_copy(
1314
return changes_between_git_tree_and_working_copy(
1670
1315
self.source.store, self.source.tree,
1671
1316
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
1678
_mod_tree.InterTree.register_optimiser(InterToIndexGitTree)
1681
class InterFromIndexGitTree(InterGitTrees):
1682
"""InterTree that works between a Git revision tree and an index."""
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
1689
self.store = OverlayObjectStore(
1690
[self.source.store, self.target.store])
1691
self.rename_detector = RenameDetector(self.store)
1694
def is_compatible(cls, source, target):
1695
return (isinstance(target, GitRevisionTree) and
1696
isinstance(source, MutableGitIndexTree))
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
1718
_mod_tree.InterTree.register_optimiser(InterFromIndexGitTree)
1721
class InterIndexGitTree(InterGitTrees):
1722
"""InterTree that works between a Git revision tree and an index."""
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
1729
self.store = OverlayObjectStore(
1730
[self.source.store, self.target.store])
1731
self.rename_detector = RenameDetector(self.store)
1734
def is_compatible(cls, source, target):
1735
return (isinstance(target, MutableGitIndexTree) and
1736
isinstance(source, MutableGitIndexTree))
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
1317
want_unversioned=want_unversioned)
1762
1320
_mod_tree.InterTree.register_optimiser(InterIndexGitTree)
1765
def snapshot_workingtree(target, want_unversioned=False):
1323
def changes_between_git_tree_and_working_copy(store, from_tree_sha, target,
1324
want_unchanged=False, want_unversioned=False):
1325
"""Determine the changes between a git tree and a working tree with index.
1768
1330
# Report dirified directories to commit_tree first, so that they can be
1769
1331
# replaced with non-empty directories if they have contents.
1771
trust_executable = target._supports_executable()
1772
1333
for path, index_entry in target._recurse_index_entries():
1774
1335
live_entry = target._live_entry(path)
1776
1337
if e.errno == errno.ENOENT:
1777
1338
# Entry was removed; keep it listed, but mark it as gone.
1778
1339
blobs[path] = (ZERO_SHA, 0)
1340
elif e.errno == errno.EISDIR:
1341
# Entry was turned into a directory
1342
dirified.append((path, Tree().id, stat.S_IFDIR))
1343
store.add_object(Tree())
1782
if live_entry is None:
1783
# Entry was turned into a directory.
1784
# Maybe it's just a submodule that's not checked out?
1785
if S_ISGITLINK(index_entry.mode):
1786
blobs[path] = (index_entry.sha, index_entry.mode)
1788
dirified.append((path, Tree().id, stat.S_IFDIR))
1789
target.store.add_object(Tree())
1791
mode = live_entry.mode
1792
if not trust_executable:
1793
if mode_is_executable(index_entry.mode):
1797
if live_entry.sha != index_entry.sha:
1798
rp = decode_git_path(path)
1799
if stat.S_ISREG(live_entry.mode):
1801
with target.get_file(rp) as f:
1802
blob.data = f.read()
1803
elif stat.S_ISLNK(live_entry.mode):
1805
blob.data = target.get_symlink_target(rp).encode(osutils._fs_enc)
1808
if blob is not None:
1809
target.store.add_object(blob)
1810
blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
1347
blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
1811
1348
if want_unversioned:
1812
for e in target._iter_files_recursive(include_dirs=False):
1349
for e in target.extras():
1350
st = target._lstat(e)
1814
e, accessible = osutils.normalized_filename(e)
1352
np, accessible = osutils.normalized_filename(e)
1815
1353
except UnicodeDecodeError:
1816
1354
raise errors.BadFilenameEncoding(
1817
1355
e, osutils._fs_enc)
1818
np = encode_git_path(e)
1821
st = target._lstat(e)
1822
1356
if stat.S_ISDIR(st.st_mode):
1824
elif stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode):
1825
blob = blob_from_path_and_stat(
1826
target.abspath(e).encode(osutils._fs_enc), st)
1829
target.store.add_object(blob)
1359
blob = blob_from_path_and_stat(target.abspath(e).encode(osutils._fs_enc), st)
1360
store.add_object(blob)
1361
np = np.encode('utf-8')
1830
1362
blobs[np] = (blob.id, cleanup_mode(st.st_mode))
1833
target.store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()]), extras
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.
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,
1364
to_tree_sha = commit_tree(store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()])
1365
return store.tree_changes(
1366
from_tree_sha, to_tree_sha, include_trees=True,
1849
1367
want_unchanged=want_unchanged, change_type_same=True), extras