1
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
from __future__ import absolute_import
22
from collections import deque
24
from io import BytesIO
27
from dulwich.index import (
28
blob_from_path_and_stat,
31
index_entry_from_stat,
33
from dulwich.object_store import (
37
from dulwich.objects import (
48
controldir as _mod_controldir,
58
from ..revision import (
62
from ..sixish import (
67
from .mapping import (
74
class GitTreeDirectory(_mod_tree.TreeDirectory):
76
__slots__ = ['file_id', 'name', 'parent_id', 'children']
78
def __init__(self, file_id, name, parent_id):
79
self.file_id = file_id
81
self.parent_id = parent_id
94
return self.__class__(
95
self.file_id, self.name, self.parent_id)
98
return "%s(file_id=%r, name=%r, parent_id=%r)" % (
99
self.__class__.__name__, self.file_id, self.name,
102
def __eq__(self, other):
103
return (self.kind == other.kind and
104
self.file_id == other.file_id and
105
self.name == other.name and
106
self.parent_id == other.parent_id)
109
class GitTreeFile(_mod_tree.TreeFile):
111
__slots__ = ['file_id', 'name', 'parent_id', 'text_size', 'text_sha1',
114
def __init__(self, file_id, name, parent_id, text_size=None,
115
text_sha1=None, executable=None):
116
self.file_id = file_id
118
self.parent_id = parent_id
119
self.text_size = text_size
120
self.text_sha1 = text_sha1
121
self.executable = executable
127
def __eq__(self, other):
128
return (self.kind == other.kind and
129
self.file_id == other.file_id and
130
self.name == other.name and
131
self.parent_id == other.parent_id and
132
self.text_sha1 == other.text_sha1 and
133
self.text_size == other.text_size and
134
self.executable == other.executable)
137
return ("%s(file_id=%r, name=%r, parent_id=%r, text_size=%r, "
138
"text_sha1=%r, executable=%r)") % (
139
type(self).__name__, self.file_id, self.name, self.parent_id,
140
self.text_size, self.text_sha1, self.executable)
143
ret = self.__class__(
144
self.file_id, self.name, self.parent_id)
145
ret.text_sha1 = self.text_sha1
146
ret.text_size = self.text_size
147
ret.executable = self.executable
151
class GitTreeSymlink(_mod_tree.TreeLink):
153
__slots__ = ['file_id', 'name', 'parent_id', 'symlink_target']
155
def __init__(self, file_id, name, parent_id,
156
symlink_target=None):
157
self.file_id = file_id
159
self.parent_id = parent_id
160
self.symlink_target = symlink_target
167
def executable(self):
175
return "%s(file_id=%r, name=%r, parent_id=%r, symlink_target=%r)" % (
176
type(self).__name__, self.file_id, self.name, self.parent_id,
179
def __eq__(self, other):
180
return (self.kind == other.kind and
181
self.file_id == other.file_id and
182
self.name == other.name and
183
self.parent_id == other.parent_id and
184
self.symlink_target == other.symlink_target)
187
return self.__class__(
188
self.file_id, self.name, self.parent_id,
192
class GitTreeSubmodule(_mod_tree.TreeLink):
194
__slots__ = ['file_id', 'name', 'parent_id', 'reference_revision']
196
def __init__(self, file_id, name, parent_id, reference_revision=None):
197
self.file_id = file_id
199
self.parent_id = parent_id
200
self.reference_revision = reference_revision
204
return 'tree-reference'
207
return ("%s(file_id=%r, name=%r, parent_id=%r, "
208
"reference_revision=%r)") % (
209
type(self).__name__, self.file_id, self.name, self.parent_id,
210
self.reference_revision)
212
def __eq__(self, other):
213
return (self.kind == other.kind and
214
self.file_id == other.file_id and
215
self.name == other.name and
216
self.parent_id == other.parent_id and
217
self.reference_revision == other.reference_revision)
220
return self.__class__(
221
self.file_id, self.name, self.parent_id,
222
self.reference_revision)
226
'directory': GitTreeDirectory,
228
'symlink': GitTreeSymlink,
229
'tree-reference': GitTreeSubmodule,
233
def ensure_normalized_path(path):
234
"""Check whether path is normalized.
236
:raises InvalidNormalization: When path is not normalized, and cannot be
237
accessed on this platform by the normalized path.
238
:return: The NFC normalised version of path.
240
norm_path, can_access = osutils.normalized_filename(path)
241
if norm_path != path:
245
raise errors.InvalidNormalization(path)
249
class GitRevisionTree(revisiontree.RevisionTree):
250
"""Revision tree implementation based on Git objects."""
252
def __init__(self, repository, revision_id):
253
self._revision_id = revision_id
254
self._repository = repository
255
self._submodules = None
256
self.store = repository._git.object_store
257
if not isinstance(revision_id, bytes):
258
raise TypeError(revision_id)
259
self.commit_id, self.mapping = repository.lookup_bzr_revision_id(
261
if revision_id == NULL_REVISION:
263
self.mapping = default_mapping
266
commit = self.store[self.commit_id]
268
raise errors.NoSuchRevision(repository, revision_id)
269
self.tree = commit.tree
271
def _submodule_info(self):
272
if self._submodules is None:
274
with self.get_file('.gitmodules') as f:
275
config = GitConfigFile.from_file(f)
278
for path, url, section in parse_submodules(config)}
279
except errors.NoSuchFile:
280
self._submodules = {}
281
return self._submodules
283
def _get_submodule_repository(self, relpath):
284
if not isinstance(relpath, bytes):
285
raise TypeError(relpath)
287
info = self._submodule_info()[relpath]
289
nested_repo_transport = self._repository.user_transport.clone(relpath.decode('utf-8'))
291
nested_repo_transport = self._repository.control_transport.clone(
292
posixpath.join('modules', info[0]))
293
nested_controldir = _mod_controldir.ControlDir.open_from_transport(
294
nested_repo_transport)
295
return nested_controldir.find_repository()
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)
303
def supports_rename_tracking(self):
306
def get_file_revision(self, path):
307
change_scanner = self._repository._file_change_scanner
308
if self.commit_id == ZERO_SHA:
310
(unused_path, commit_id) = change_scanner.find_last_change_revision(
311
path.encode('utf-8'), self.commit_id)
312
return self._repository.lookup_foreign_revision_id(
313
commit_id, self.mapping)
315
def get_file_mtime(self, path):
317
revid = self.get_file_revision(path)
319
raise errors.NoSuchFile(path)
321
rev = self._repository.get_revision(revid)
322
except errors.NoSuchRevision:
323
raise _mod_tree.FileTimestampUnavailable(path)
326
def id2path(self, file_id):
328
path = self.mapping.parse_file_id(file_id)
330
raise errors.NoSuchId(self, file_id)
331
if self.is_versioned(path):
333
raise errors.NoSuchId(self, file_id)
335
def is_versioned(self, path):
336
return self.has_filename(path)
338
def path2id(self, path):
339
if self.mapping.is_special_file(path):
341
if not self.is_versioned(path):
343
return self.mapping.generate_file_id(osutils.safe_unicode(path))
345
def all_file_ids(self):
346
raise errors.UnsupportedOperation(self.all_file_ids, self)
348
def all_versioned_paths(self):
350
todo = [(self.store, b'', self.tree)]
352
(store, path, tree_id) = todo.pop()
355
tree = store[tree_id]
356
for name, mode, hexsha in tree.items():
357
subpath = posixpath.join(path, name)
358
ret.add(subpath.decode('utf-8'))
359
if stat.S_ISDIR(mode):
360
todo.append((store, subpath, hexsha))
363
def has_or_had_id(self, file_id):
365
self.id2path(file_id)
366
except errors.NoSuchId:
370
def has_id(self, file_id):
372
path = self.id2path(file_id)
373
except errors.NoSuchId:
375
return self.has_filename(path)
377
def _lookup_path(self, path):
378
if self.tree is None:
379
raise errors.NoSuchFile(path)
381
(mode, hexsha) = tree_lookup_path(
382
self.store.__getitem__, self.tree, path.encode('utf-8'))
384
raise errors.NoSuchFile(self, path)
386
return (self.store, mode, hexsha)
388
def is_executable(self, path):
389
(store, mode, hexsha) = self._lookup_path(path)
391
# the tree root is a directory
393
return mode_is_executable(mode)
395
def kind(self, path):
396
(store, mode, hexsha) = self._lookup_path(path)
398
# the tree root is a directory
400
return mode_kind(mode)
402
def has_filename(self, path):
404
self._lookup_path(path)
405
except errors.NoSuchFile:
410
def list_files(self, include_root=False, from_dir=None, recursive=True):
411
if self.tree is None:
413
if from_dir is None or from_dir == '.':
415
(store, mode, hexsha) = self._lookup_path(from_dir)
416
if mode is None: # Root
417
root_ie = self._get_dir_ie(b"", None)
419
parent_path = posixpath.dirname(from_dir)
420
parent_id = self.mapping.generate_file_id(parent_path)
421
if mode_kind(mode) == 'directory':
422
root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
424
root_ie = self._get_file_ie(
425
store, from_dir.encode("utf-8"),
426
posixpath.basename(from_dir), mode, hexsha)
428
yield (from_dir, "V", root_ie.kind, root_ie)
430
if root_ie.kind == 'directory':
431
todo.append((store, from_dir.encode("utf-8"),
432
b"", hexsha, root_ie.file_id))
434
(store, path, relpath, hexsha, parent_id) = todo.pop()
436
for name, mode, hexsha in tree.iteritems():
437
if self.mapping.is_special_file(name):
439
child_path = posixpath.join(path, name)
440
child_relpath = posixpath.join(relpath, name)
441
if stat.S_ISDIR(mode):
442
ie = self._get_dir_ie(child_path, parent_id)
445
(store, child_path, child_relpath, hexsha,
448
ie = self._get_file_ie(
449
store, child_path, name, mode, hexsha, parent_id)
450
yield (child_relpath.decode('utf-8'), "V", ie.kind, ie)
452
def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
453
if not isinstance(path, bytes):
454
raise TypeError(path)
455
if not isinstance(name, bytes):
456
raise TypeError(name)
457
kind = mode_kind(mode)
458
path = path.decode('utf-8')
459
name = name.decode("utf-8")
460
file_id = self.mapping.generate_file_id(path)
461
ie = entry_factory[kind](file_id, name, parent_id)
462
if kind == 'symlink':
463
ie.symlink_target = store[hexsha].data.decode('utf-8')
464
elif kind == 'tree-reference':
465
ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(
468
data = store[hexsha].data
469
ie.text_sha1 = osutils.sha_string(data)
470
ie.text_size = len(data)
471
ie.executable = mode_is_executable(mode)
474
def _get_dir_ie(self, path, parent_id):
475
path = path.decode('utf-8')
476
file_id = self.mapping.generate_file_id(path)
477
return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
479
def iter_child_entries(self, path):
480
(store, mode, tree_sha) = self._lookup_path(path)
482
if mode is not None and not stat.S_ISDIR(mode):
485
encoded_path = path.encode('utf-8')
486
file_id = self.path2id(path)
487
tree = store[tree_sha]
488
for name, mode, hexsha in tree.iteritems():
489
if self.mapping.is_special_file(name):
491
child_path = posixpath.join(encoded_path, name)
492
if stat.S_ISDIR(mode):
493
yield self._get_dir_ie(child_path, file_id)
495
yield self._get_file_ie(store, child_path, name, mode, hexsha,
498
def iter_entries_by_dir(self, specific_files=None, yield_parents=False):
499
if self.tree is None:
502
# TODO(jelmer): Support yield parents
503
raise NotImplementedError
504
if specific_files is not None:
505
if specific_files in ([""], []):
506
specific_files = None
508
specific_files = set([p.encode('utf-8')
509
for p in specific_files])
510
todo = deque([(self.store, b"", self.tree, self.path2id(''))])
511
if specific_files is None or u"" in specific_files:
512
yield u"", self._get_dir_ie(b"", None)
514
store, path, tree_sha, parent_id = todo.popleft()
515
tree = store[tree_sha]
517
for name, mode, hexsha in tree.iteritems():
518
if self.mapping.is_special_file(name):
520
child_path = posixpath.join(path, name)
521
child_path_decoded = child_path.decode('utf-8')
522
if stat.S_ISDIR(mode):
523
if (specific_files is None or
524
any([p for p in specific_files if p.startswith(
527
(store, child_path, hexsha,
528
self.path2id(child_path_decoded)))
529
if specific_files is None or child_path in specific_files:
530
if stat.S_ISDIR(mode):
531
yield (child_path_decoded,
532
self._get_dir_ie(child_path, parent_id))
534
yield (child_path_decoded,
535
self._get_file_ie(store, child_path, name, mode,
537
todo.extendleft(reversed(extradirs))
539
def iter_references(self):
540
if self.supports_tree_reference():
541
for path, entry in self.iter_entries_by_dir():
542
if entry.kind == 'tree-reference':
545
def get_revision_id(self):
546
"""See RevisionTree.get_revision_id."""
547
return self._revision_id
549
def get_file_sha1(self, path, stat_value=None):
550
if self.tree is None:
551
raise errors.NoSuchFile(path)
552
return osutils.sha_string(self.get_file_text(path))
554
def get_file_verifier(self, path, stat_value=None):
555
(store, mode, hexsha) = self._lookup_path(path)
556
return ("GIT", hexsha)
558
def get_file_size(self, path):
559
(store, mode, hexsha) = self._lookup_path(path)
560
if stat.S_ISREG(mode):
561
return len(store[hexsha].data)
564
def get_file_text(self, path):
565
"""See RevisionTree.get_file_text."""
566
(store, mode, hexsha) = self._lookup_path(path)
567
if stat.S_ISREG(mode):
568
return store[hexsha].data
572
def get_symlink_target(self, path):
573
"""See RevisionTree.get_symlink_target."""
574
(store, mode, hexsha) = self._lookup_path(path)
575
if stat.S_ISLNK(mode):
576
return store[hexsha].data.decode('utf-8')
580
def get_reference_revision(self, path):
581
"""See RevisionTree.get_symlink_target."""
582
(store, mode, hexsha) = self._lookup_path(path)
583
if S_ISGITLINK(mode):
584
nested_repo = self._get_submodule_repository(path.encode('utf-8'))
585
return nested_repo.lookup_foreign_revision_id(hexsha)
589
def _comparison_data(self, entry, path):
591
return None, False, None
592
return entry.kind, entry.executable, None
594
def path_content_summary(self, path):
595
"""See Tree.path_content_summary."""
597
(store, mode, hexsha) = self._lookup_path(path)
598
except errors.NoSuchFile:
599
return ('missing', None, None, None)
600
kind = mode_kind(mode)
602
executable = mode_is_executable(mode)
603
contents = store[hexsha].data
604
return (kind, len(contents), executable,
605
osutils.sha_string(contents))
606
elif kind == 'symlink':
607
return (kind, None, None, store[hexsha].data.decode('utf-8'))
608
elif kind == 'tree-reference':
609
nested_repo = self._get_submodule_repository(path.encode('utf-8'))
610
return (kind, None, None,
611
nested_repo.lookup_foreign_revision_id(hexsha))
613
return (kind, None, None, None)
615
def find_related_paths_across_trees(self, paths, trees=[],
616
require_versioned=True):
619
if require_versioned:
620
trees = [self] + (trees if trees is not None else [])
624
if t.is_versioned(p):
629
raise errors.PathsNotVersionedError(unversioned)
630
return filter(self.is_versioned, paths)
632
def _iter_tree_contents(self, include_trees=False):
633
if self.tree is None:
635
return self.store.iter_tree_contents(
636
self.tree, include_trees=include_trees)
638
def annotate_iter(self, path, default_revision=CURRENT_REVISION):
639
"""Return an iterator of revision_id, line tuples.
641
For working trees (and mutable trees in general), the special
642
revision_id 'current:' will be used for lines that are new in this
643
tree, e.g. uncommitted changes.
644
:param default_revision: For lines that don't match a basis, mark them
645
with this revision id. Not all implementations will make use of
648
with self.lock_read():
649
# Now we have the parents of this content
650
from breezy.annotate import Annotator
651
from .annotate import AnnotateProvider
652
annotator = Annotator(AnnotateProvider(
653
self._repository._file_change_scanner))
654
this_key = (path, self.get_file_revision(path))
655
annotations = [(key[-1], line)
656
for key, line in annotator.annotate_flat(this_key)]
659
def _get_rules_searcher(self, default_searcher):
660
return default_searcher
662
def walkdirs(self, prefix=u""):
663
(store, mode, hexsha) = self._lookup_path(prefix)
665
[(store, prefix.encode('utf-8'), hexsha, self.path2id(prefix))])
667
store, path, tree_sha, parent_id = todo.popleft()
668
path_decoded = path.decode('utf-8')
669
tree = store[tree_sha]
671
for name, mode, hexsha in tree.iteritems():
672
if self.mapping.is_special_file(name):
674
child_path = posixpath.join(path, name)
675
file_id = self.path2id(child_path.decode('utf-8'))
676
if stat.S_ISDIR(mode):
677
todo.append((store, child_path, hexsha, file_id))
679
(child_path.decode('utf-8'), name.decode('utf-8'),
680
mode_kind(mode), None,
681
file_id, mode_kind(mode)))
682
yield (path_decoded, parent_id), children
685
def tree_delta_from_git_changes(changes, mappings,
687
require_versioned=False, include_root=False,
689
"""Create a TreeDelta from two git trees.
691
source and target are iterators over tuples with:
692
(filename, sha, mode)
694
(old_mapping, new_mapping) = mappings
695
if target_extras is None:
696
target_extras = set()
697
ret = delta.TreeDelta()
699
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
700
if newpath == b'' and not include_root:
703
oldpath_decoded = None
705
oldpath_decoded = oldpath.decode('utf-8')
707
newpath_decoded = None
709
newpath_decoded = newpath.decode('utf-8')
710
if not (specific_files is None or
711
(oldpath is not None and
712
osutils.is_inside_or_parent_of_any(
713
specific_files, oldpath_decoded)) or
714
(newpath is not None and
715
osutils.is_inside_or_parent_of_any(
716
specific_files, newpath_decoded))):
718
if old_mapping.is_special_file(oldpath):
720
if new_mapping.is_special_file(newpath):
722
if oldpath is None and newpath is None:
725
added.append((newpath, mode_kind(newmode)))
726
elif newpath is None or newmode == 0:
727
file_id = old_mapping.generate_file_id(oldpath_decoded)
728
ret.removed.append((oldpath_decoded, file_id, mode_kind(oldmode)))
729
elif oldpath != newpath:
730
file_id = old_mapping.generate_file_id(oldpath_decoded)
732
(oldpath_decoded, newpath.decode('utf-8'), file_id,
733
mode_kind(newmode), (oldsha != newsha),
734
(oldmode != newmode)))
735
elif mode_kind(oldmode) != mode_kind(newmode):
736
file_id = new_mapping.generate_file_id(newpath_decoded)
737
ret.kind_changed.append(
738
(newpath_decoded, file_id, mode_kind(oldmode),
740
elif oldsha != newsha or oldmode != newmode:
741
if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
743
file_id = new_mapping.generate_file_id(newpath_decoded)
745
(newpath_decoded, file_id, mode_kind(newmode),
746
(oldsha != newsha), (oldmode != newmode)))
748
file_id = new_mapping.generate_file_id(newpath_decoded)
749
ret.unchanged.append(
750
(newpath_decoded, file_id, mode_kind(newmode)))
752
implicit_dirs = {b''}
753
for path, kind in added:
754
if kind == 'directory' or path in target_extras:
756
implicit_dirs.update(osutils.parent_directories(path))
758
for path, kind in added:
759
if kind == 'directory' and path not in implicit_dirs:
761
path_decoded = osutils.normalized_filename(path)[0]
762
if path in target_extras:
763
ret.unversioned.append((path_decoded, None, kind))
765
file_id = new_mapping.generate_file_id(path_decoded)
766
ret.added.append((path_decoded, file_id, kind))
771
def changes_from_git_changes(changes, mapping, specific_files=None,
772
include_unchanged=False, target_extras=None):
773
"""Create a iter_changes-like generator from a git stream.
775
source and target are iterators over tuples with:
776
(filename, sha, mode)
778
if target_extras is None:
779
target_extras = set()
780
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
781
if oldpath is not None:
782
oldpath_decoded = oldpath.decode('utf-8')
784
oldpath_decoded = None
785
if newpath is not None:
786
newpath_decoded = newpath.decode('utf-8')
788
newpath_decoded = None
789
if not (specific_files is None or
790
(oldpath_decoded is not None and
791
osutils.is_inside_or_parent_of_any(
792
specific_files, oldpath_decoded)) or
793
(newpath_decoded is not None and
794
osutils.is_inside_or_parent_of_any(
795
specific_files, newpath_decoded))):
797
if oldpath is not None and mapping.is_special_file(oldpath):
799
if newpath is not None and mapping.is_special_file(newpath):
801
if oldpath_decoded is None:
802
fileid = mapping.generate_file_id(newpath_decoded)
811
oldexe = mode_is_executable(oldmode)
812
oldkind = mode_kind(oldmode)
816
if oldpath_decoded == u'':
820
(oldparentpath, oldname) = osutils.split(oldpath_decoded)
821
oldparent = mapping.generate_file_id(oldparentpath)
822
fileid = mapping.generate_file_id(oldpath_decoded)
823
if newpath_decoded is None:
830
newversioned = (newpath_decoded not in target_extras)
832
newexe = mode_is_executable(newmode)
833
newkind = mode_kind(newmode)
837
if newpath_decoded == u'':
841
newparentpath, newname = osutils.split(newpath_decoded)
842
newparent = mapping.generate_file_id(newparentpath)
843
if (not include_unchanged and
844
oldkind == 'directory' and newkind == 'directory' and
845
oldpath_decoded == newpath_decoded):
847
yield _mod_tree.TreeChange(
848
fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
849
(oldversioned, newversioned),
850
(oldparent, newparent), (oldname, newname),
851
(oldkind, newkind), (oldexe, newexe))
854
class InterGitTrees(_mod_tree.InterTree):
855
"""InterTree that works between two git trees."""
857
_matching_from_tree_format = None
858
_matching_to_tree_format = None
859
_test_mutable_trees_to_test_trees = None
862
def is_compatible(cls, source, target):
863
return (isinstance(source, GitRevisionTree) and
864
isinstance(target, GitRevisionTree))
866
def compare(self, want_unchanged=False, specific_files=None,
867
extra_trees=None, require_versioned=False, include_root=False,
868
want_unversioned=False):
869
with self.lock_read():
870
changes, target_extras = self._iter_git_changes(
871
want_unchanged=want_unchanged,
872
require_versioned=require_versioned,
873
specific_files=specific_files,
874
extra_trees=extra_trees,
875
want_unversioned=want_unversioned)
876
return tree_delta_from_git_changes(
877
changes, (self.source.mapping, self.target.mapping),
878
specific_files=specific_files,
879
include_root=include_root, target_extras=target_extras)
881
def iter_changes(self, include_unchanged=False, specific_files=None,
882
pb=None, extra_trees=[], require_versioned=True,
883
want_unversioned=False):
884
with self.lock_read():
885
changes, target_extras = self._iter_git_changes(
886
want_unchanged=include_unchanged,
887
require_versioned=require_versioned,
888
specific_files=specific_files,
889
extra_trees=extra_trees,
890
want_unversioned=want_unversioned)
891
return changes_from_git_changes(
892
changes, self.target.mapping,
893
specific_files=specific_files,
894
include_unchanged=include_unchanged,
895
target_extras=target_extras)
897
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
898
require_versioned=False, extra_trees=None,
899
want_unversioned=False):
900
raise NotImplementedError(self._iter_git_changes)
903
class InterGitRevisionTrees(InterGitTrees):
904
"""InterTree that works between two git revision trees."""
906
_matching_from_tree_format = None
907
_matching_to_tree_format = None
908
_test_mutable_trees_to_test_trees = None
911
def is_compatible(cls, source, target):
912
return (isinstance(source, GitRevisionTree) and
913
isinstance(target, GitRevisionTree))
915
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
916
require_versioned=True, extra_trees=None,
917
want_unversioned=False):
918
trees = [self.source]
919
if extra_trees is not None:
920
trees.extend(extra_trees)
921
if specific_files is not None:
922
specific_files = self.target.find_related_paths_across_trees(
923
specific_files, trees,
924
require_versioned=require_versioned)
926
if (self.source._repository._git.object_store !=
927
self.target._repository._git.object_store):
928
store = OverlayObjectStore(
929
[self.source._repository._git.object_store,
930
self.target._repository._git.object_store])
932
store = self.source._repository._git.object_store
933
return store.tree_changes(
934
self.source.tree, self.target.tree, want_unchanged=want_unchanged,
935
include_trees=True, change_type_same=True), set()
938
_mod_tree.InterTree.register_optimiser(InterGitRevisionTrees)
941
class MutableGitIndexTree(mutabletree.MutableTree):
944
self._lock_mode = None
946
self._versioned_dirs = None
947
self._index_dirty = False
949
def is_versioned(self, path):
950
with self.lock_read():
951
path = path.rstrip('/').encode('utf-8')
952
(index, subpath) = self._lookup_index(path)
953
return (subpath in index or self._has_dir(path))
955
def _has_dir(self, path):
956
if not isinstance(path, bytes):
957
raise TypeError(path)
960
if self._versioned_dirs is None:
962
return path in self._versioned_dirs
964
def _load_dirs(self):
965
if self._lock_mode is None:
966
raise errors.ObjectNotLocked(self)
967
self._versioned_dirs = set()
968
# TODO(jelmer): Browse over all indexes
969
for p, i in self._recurse_index_entries():
970
self._ensure_versioned_dir(posixpath.dirname(p))
972
def _ensure_versioned_dir(self, dirname):
973
if not isinstance(dirname, bytes):
974
raise TypeError(dirname)
975
if dirname in self._versioned_dirs:
978
self._ensure_versioned_dir(posixpath.dirname(dirname))
979
self._versioned_dirs.add(dirname)
981
def path2id(self, path):
982
with self.lock_read():
983
path = path.rstrip('/')
984
if self.is_versioned(path.rstrip('/')):
985
return self.mapping.generate_file_id(
986
osutils.safe_unicode(path))
989
def has_id(self, file_id):
991
self.id2path(file_id)
992
except errors.NoSuchId:
997
def id2path(self, file_id):
1000
if type(file_id) is not bytes:
1001
raise TypeError(file_id)
1002
with self.lock_read():
1004
path = self.mapping.parse_file_id(file_id)
1006
raise errors.NoSuchId(self, file_id)
1007
if self.is_versioned(path):
1009
raise errors.NoSuchId(self, file_id)
1011
def _set_root_id(self, file_id):
1012
raise errors.UnsupportedOperation(self._set_root_id, self)
1014
def _add(self, files, ids, kinds):
1015
for (path, file_id, kind) in zip(files, ids, kinds):
1016
if file_id is not None:
1017
raise workingtree.SettingFileIdUnsupported()
1018
path, can_access = osutils.normalized_filename(path)
1020
raise errors.InvalidNormalization(path)
1021
self._index_add_entry(path, kind)
1023
def _read_submodule_head(self, path):
1024
raise NotImplementedError(self._read_submodule_head)
1026
def _lookup_index(self, encoded_path):
1027
if not isinstance(encoded_path, bytes):
1028
raise TypeError(encoded_path)
1029
# TODO(jelmer): Look in other indexes
1030
return self.index, encoded_path
1032
def _index_del_entry(self, index, path):
1034
# TODO(jelmer): Keep track of dirty per index
1035
self._index_dirty = True
1037
def _index_add_entry(self, path, kind, flags=0, reference_revision=None):
1038
if kind == "directory":
1039
# Git indexes don't contain directories
1044
file, stat_val = self.get_file_with_stat(path)
1045
except (errors.NoSuchFile, IOError):
1046
# TODO: Rather than come up with something here, use the old
1049
stat_val = os.stat_result(
1050
(stat.S_IFREG | 0o644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1052
blob.set_raw_string(file.read())
1053
# Add object to the repository if it didn't exist yet
1054
if blob.id not in self.store:
1055
self.store.add_object(blob)
1057
elif kind == "symlink":
1060
stat_val = self._lstat(path)
1061
except EnvironmentError:
1062
# TODO: Rather than come up with something here, use the
1064
stat_val = os.stat_result(
1065
(stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1066
blob.set_raw_string(
1067
self.get_symlink_target(path).encode("utf-8"))
1068
# Add object to the repository if it didn't exist yet
1069
if blob.id not in self.store:
1070
self.store.add_object(blob)
1072
elif kind == "tree-reference":
1073
if reference_revision is not None:
1074
hexsha = self.branch.lookup_bzr_revision_id(
1075
reference_revision)[0]
1077
hexsha = self._read_submodule_head(path)
1079
raise errors.NoCommits(path)
1081
stat_val = self._lstat(path)
1082
except EnvironmentError:
1083
stat_val = os.stat_result(
1084
(S_IFGITLINK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1085
stat_val = os.stat_result((S_IFGITLINK, ) + stat_val[1:])
1087
raise AssertionError("unknown kind '%s'" % kind)
1088
# Add an entry to the index or update the existing entry
1089
ensure_normalized_path(path)
1090
encoded_path = path.encode("utf-8")
1091
if b'\r' in encoded_path or b'\n' in encoded_path:
1092
# TODO(jelmer): Why do we need to do this?
1093
trace.mutter('ignoring path with invalid newline in it: %r', path)
1095
(index, index_path) = self._lookup_index(encoded_path)
1096
index[index_path] = index_entry_from_stat(stat_val, hexsha, flags)
1097
self._index_dirty = True
1098
if self._versioned_dirs is not None:
1099
self._ensure_versioned_dir(index_path)
1101
def _recurse_index_entries(self, index=None, basepath=b""):
1102
# Iterate over all index entries
1103
with self.lock_read():
1106
for path, value in index.items():
1107
yield (posixpath.join(basepath, path), value)
1108
(ctime, mtime, dev, ino, mode, uid, gid, size, sha,
1110
if S_ISGITLINK(mode):
1111
pass # TODO(jelmer): dive into submodule
1113
def iter_entries_by_dir(self, specific_files=None, yield_parents=False):
1115
raise NotImplementedError(self.iter_entries_by_dir)
1116
with self.lock_read():
1117
if specific_files is not None:
1118
specific_files = set(specific_files)
1120
specific_files = None
1121
root_ie = self._get_dir_ie(u"", None)
1123
if specific_files is None or u"" in specific_files:
1124
ret[(u"", u"")] = root_ie
1125
dir_ids = {u"": root_ie.file_id}
1126
for path, value in self._recurse_index_entries():
1127
if self.mapping.is_special_file(path):
1129
path = path.decode("utf-8")
1130
if specific_files is not None and path not in specific_files:
1132
(parent, name) = posixpath.split(path)
1134
file_ie = self._get_file_ie(name, path, value, None)
1135
except errors.NoSuchFile:
1137
if yield_parents or specific_files is None:
1138
for (dir_path, dir_ie) in self._add_missing_parent_ids(
1140
ret[(posixpath.dirname(dir_path), dir_path)] = dir_ie
1141
file_ie.parent_id = self.path2id(parent)
1142
ret[(posixpath.dirname(path), path)] = file_ie
1143
return ((path, ie) for ((_, path), ie) in sorted(viewitems(ret)))
1145
def iter_references(self):
1146
# TODO(jelmer): Implement a more efficient version of this
1147
for path, entry in self.iter_entries_by_dir():
1148
if entry.kind == 'tree-reference':
1151
def _get_dir_ie(self, path, parent_id):
1152
file_id = self.path2id(path)
1153
return GitTreeDirectory(file_id,
1154
posixpath.basename(path).strip("/"), parent_id)
1156
def _get_file_ie(self, name, path, value, parent_id):
1157
if not isinstance(name, text_type):
1158
raise TypeError(name)
1159
if not isinstance(path, text_type):
1160
raise TypeError(path)
1161
if not isinstance(value, tuple) or len(value) != 10:
1162
raise TypeError(value)
1163
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
1164
file_id = self.path2id(path)
1165
if not isinstance(file_id, bytes):
1166
raise TypeError(file_id)
1167
kind = mode_kind(mode)
1168
ie = entry_factory[kind](file_id, name, parent_id)
1169
if kind == 'symlink':
1170
ie.symlink_target = self.get_symlink_target(path)
1171
elif kind == 'tree-reference':
1172
ie.reference_revision = self.get_reference_revision(path)
1175
data = self.get_file_text(path)
1176
except errors.NoSuchFile:
1178
except IOError as e:
1179
if e.errno != errno.ENOENT:
1183
data = self.branch.repository._git.object_store[sha].data
1184
ie.text_sha1 = osutils.sha_string(data)
1185
ie.text_size = len(data)
1186
ie.executable = bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
1189
def _add_missing_parent_ids(self, path, dir_ids):
1192
parent = posixpath.dirname(path).strip("/")
1193
ret = self._add_missing_parent_ids(parent, dir_ids)
1194
parent_id = dir_ids[parent]
1195
ie = self._get_dir_ie(path, parent_id)
1196
dir_ids[path] = ie.file_id
1197
ret.append((path, ie))
1200
def _comparison_data(self, entry, path):
1202
return None, False, None
1203
return entry.kind, entry.executable, None
1205
def _unversion_path(self, path):
1206
if self._lock_mode is None:
1207
raise errors.ObjectNotLocked(self)
1208
encoded_path = path.encode("utf-8")
1210
(index, subpath) = self._lookup_index(encoded_path)
1212
self._index_del_entry(index, encoded_path)
1214
# A directory, perhaps?
1215
# TODO(jelmer): Deletes that involve submodules?
1216
for p in list(index):
1217
if p.startswith(subpath + b"/"):
1219
self._index_del_entry(index, p)
1222
self._versioned_dirs = None
1225
def unversion(self, paths):
1226
with self.lock_tree_write():
1228
if self._unversion_path(path) == 0:
1229
raise errors.NoSuchFile(path)
1230
self._versioned_dirs = None
1236
def update_basis_by_delta(self, revid, delta):
1237
# TODO(jelmer): This shouldn't be called, it's inventory specific.
1238
for (old_path, new_path, file_id, ie) in delta:
1239
if old_path is not None:
1240
(index, old_subpath) = self._lookup_index(
1241
old_path.encode('utf-8'))
1242
if old_subpath in index:
1243
self._index_del_entry(index, old_subpath)
1244
self._versioned_dirs = None
1245
if new_path is not None and ie.kind != 'directory':
1246
self._index_add_entry(new_path, ie.kind)
1248
self._set_merges_from_parent_ids([])
1250
def move(self, from_paths, to_dir=None, after=None):
1252
with self.lock_tree_write():
1253
to_abs = self.abspath(to_dir)
1254
if not os.path.isdir(to_abs):
1255
raise errors.BzrMoveFailedError('', to_dir,
1256
errors.NotADirectory(to_abs))
1258
for from_rel in from_paths:
1259
from_tail = os.path.split(from_rel)[-1]
1260
to_rel = os.path.join(to_dir, from_tail)
1261
self.rename_one(from_rel, to_rel, after=after)
1262
rename_tuples.append((from_rel, to_rel))
1264
return rename_tuples
1266
def rename_one(self, from_rel, to_rel, after=None):
1267
from_path = from_rel.encode("utf-8")
1268
to_rel, can_access = osutils.normalized_filename(to_rel)
1270
raise errors.InvalidNormalization(to_rel)
1271
to_path = to_rel.encode("utf-8")
1272
with self.lock_tree_write():
1274
# Perhaps it's already moved?
1276
not self.has_filename(from_rel) and
1277
self.has_filename(to_rel) and
1278
not self.is_versioned(to_rel))
1280
if not self.has_filename(to_rel):
1281
raise errors.BzrMoveFailedError(
1282
from_rel, to_rel, errors.NoSuchFile(to_rel))
1283
if self.basis_tree().is_versioned(to_rel):
1284
raise errors.BzrMoveFailedError(
1285
from_rel, to_rel, errors.AlreadyVersionedError(to_rel))
1287
kind = self.kind(to_rel)
1290
to_kind = self.kind(to_rel)
1291
except errors.NoSuchFile:
1292
exc_type = errors.BzrRenameFailedError
1295
exc_type = errors.BzrMoveFailedError
1296
if self.is_versioned(to_rel):
1297
raise exc_type(from_rel, to_rel,
1298
errors.AlreadyVersionedError(to_rel))
1299
if not self.has_filename(from_rel):
1300
raise errors.BzrMoveFailedError(
1301
from_rel, to_rel, errors.NoSuchFile(from_rel))
1302
kind = self.kind(from_rel)
1303
if not self.is_versioned(from_rel) and kind != 'directory':
1304
raise exc_type(from_rel, to_rel,
1305
errors.NotVersionedError(from_rel))
1306
if self.has_filename(to_rel):
1307
raise errors.RenameFailedFilesExist(
1308
from_rel, to_rel, errors.FileExists(to_rel))
1310
kind = self.kind(from_rel)
1312
if not after and kind != 'directory':
1313
(index, from_subpath) = self._lookup_index(from_path)
1314
if from_subpath not in index:
1316
raise errors.BzrMoveFailedError(
1318
errors.NotVersionedError(path=from_rel))
1322
self._rename_one(from_rel, to_rel)
1323
except OSError as e:
1324
if e.errno == errno.ENOENT:
1325
raise errors.BzrMoveFailedError(
1326
from_rel, to_rel, errors.NoSuchFile(to_rel))
1328
if kind != 'directory':
1329
(index, from_index_path) = self._lookup_index(from_path)
1331
self._index_del_entry(index, from_path)
1334
self._index_add_entry(to_rel, kind)
1336
todo = [(p, i) for (p, i) in self._recurse_index_entries()
1337
if p.startswith(from_path + b'/')]
1338
for child_path, child_value in todo:
1339
(child_to_index, child_to_index_path) = self._lookup_index(
1340
posixpath.join(to_path, posixpath.relpath(child_path, from_path)))
1341
child_to_index[child_to_index_path] = child_value
1342
# TODO(jelmer): Mark individual index as dirty
1343
self._index_dirty = True
1344
(child_from_index, child_from_index_path) = self._lookup_index(
1346
self._index_del_entry(
1347
child_from_index, child_from_index_path)
1349
self._versioned_dirs = None
1352
def find_related_paths_across_trees(self, paths, trees=[],
1353
require_versioned=True):
1357
if require_versioned:
1358
trees = [self] + (trees if trees is not None else [])
1362
if t.is_versioned(p):
1367
raise errors.PathsNotVersionedError(unversioned)
1369
return filter(self.is_versioned, paths)
1371
def path_content_summary(self, path):
1372
"""See Tree.path_content_summary."""
1374
stat_result = self._lstat(path)
1375
except OSError as e:
1376
if getattr(e, 'errno', None) == errno.ENOENT:
1378
return ('missing', None, None, None)
1379
# propagate other errors
1381
kind = mode_kind(stat_result.st_mode)
1383
return self._file_content_summary(path, stat_result)
1384
elif kind == 'directory':
1385
# perhaps it looks like a plain directory, but it's really a
1387
if self._directory_is_tree_reference(path):
1388
kind = 'tree-reference'
1389
return kind, None, None, None
1390
elif kind == 'symlink':
1391
target = osutils.readlink(self.abspath(path))
1392
return ('symlink', None, None, target)
1394
return (kind, None, None, None)
1396
def kind(self, relpath):
1397
kind = osutils.file_kind(self.abspath(relpath))
1398
if kind == 'directory':
1399
(index, index_path) = self._lookup_index(relpath.encode('utf-8'))
1403
mode = index[index_path].mode
1407
if S_ISGITLINK(mode):
1408
return 'tree-reference'
1413
def _live_entry(self, relpath):
1414
raise NotImplementedError(self._live_entry)
1416
def get_transform(self, pb=None):
1417
from ..transform import TreeTransform
1418
return TreeTransform(self, pb=pb)
1422
class InterIndexGitTree(InterGitTrees):
1423
"""InterTree that works between a Git revision tree and an index."""
1425
def __init__(self, source, target):
1426
super(InterIndexGitTree, self).__init__(source, target)
1427
self._index = target.index
1430
def is_compatible(cls, source, target):
1431
return (isinstance(source, GitRevisionTree) and
1432
isinstance(target, MutableGitIndexTree))
1434
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1435
require_versioned=False, extra_trees=None,
1436
want_unversioned=False):
1437
trees = [self.source]
1438
if extra_trees is not None:
1439
trees.extend(extra_trees)
1440
if specific_files is not None:
1441
specific_files = self.target.find_related_paths_across_trees(
1442
specific_files, trees,
1443
require_versioned=require_versioned)
1444
# TODO(jelmer): Restrict to specific_files, for performance reasons.
1445
with self.lock_read():
1446
return changes_between_git_tree_and_working_copy(
1447
self.source.store, self.source.tree,
1448
self.target, want_unchanged=want_unchanged,
1449
want_unversioned=want_unversioned)
1452
_mod_tree.InterTree.register_optimiser(InterIndexGitTree)
1455
def changes_between_git_tree_and_working_copy(store, from_tree_sha, target,
1456
want_unchanged=False,
1457
want_unversioned=False):
1458
"""Determine the changes between a git tree and a working tree with index.
1463
# Report dirified directories to commit_tree first, so that they can be
1464
# replaced with non-empty directories if they have contents.
1466
trust_executable = target._supports_executable()
1467
for path, index_entry in target._recurse_index_entries():
1469
live_entry = target._live_entry(path)
1470
except EnvironmentError as e:
1471
if e.errno == errno.ENOENT:
1472
# Entry was removed; keep it listed, but mark it as gone.
1473
blobs[path] = (ZERO_SHA, 0)
1474
elif e.errno == errno.EISDIR:
1475
# Backwards compatibility with Dulwich < 0.19.12;
1476
# newer versions of Dulwich return either an entry for the
1477
# submodule or None for directories.
1478
if S_ISGITLINK(index_entry.mode):
1479
blobs[path] = (index_entry.sha, index_entry.mode)
1481
# Entry was turned into a directory
1482
dirified.append((path, Tree().id, stat.S_IFDIR))
1483
store.add_object(Tree())
1487
if live_entry is None:
1488
# Entry was turned into a directory
1489
dirified.append((path, Tree().id, stat.S_IFDIR))
1490
store.add_object(Tree())
1492
mode = live_entry.mode
1493
if not trust_executable:
1494
if mode_is_executable(index_entry.mode):
1498
blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
1499
if want_unversioned:
1500
for e in target.extras():
1501
st = target._lstat(e)
1503
np, accessible = osutils.normalized_filename(e)
1504
except UnicodeDecodeError:
1505
raise errors.BadFilenameEncoding(
1507
if stat.S_ISDIR(st.st_mode):
1510
blob = blob_from_path_and_stat(
1511
target.abspath(e).encode(osutils._fs_enc), st)
1512
store.add_object(blob)
1513
np = np.encode('utf-8')
1514
blobs[np] = (blob.id, cleanup_mode(st.st_mode))
1516
to_tree_sha = commit_tree(
1517
store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()])
1518
return store.tree_changes(
1519
from_tree_sha, to_tree_sha, include_trees=True,
1520
want_unchanged=want_unchanged, change_type_same=True), extras