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.config import (
29
ConfigFile as GitConfigFile,
31
from dulwich.diff_tree import tree_changes
32
from dulwich.errors import NotTreeError
33
from dulwich.index import (
34
blob_from_path_and_stat,
37
index_entry_from_stat,
40
from dulwich.object_store import (
44
from dulwich.objects import (
55
controldir as _mod_controldir,
65
from ..revision import (
69
from ..sixish import (
74
from .mapping import (
79
from .transportgit import (
85
class GitTreeDirectory(_mod_tree.TreeDirectory):
87
__slots__ = ['file_id', 'name', 'parent_id', 'children']
89
def __init__(self, file_id, name, parent_id):
90
self.file_id = file_id
92
self.parent_id = parent_id
101
def executable(self):
105
return self.__class__(
106
self.file_id, self.name, self.parent_id)
109
return "%s(file_id=%r, name=%r, parent_id=%r)" % (
110
self.__class__.__name__, self.file_id, self.name,
113
def __eq__(self, other):
114
return (self.kind == other.kind and
115
self.file_id == other.file_id and
116
self.name == other.name and
117
self.parent_id == other.parent_id)
120
class GitTreeFile(_mod_tree.TreeFile):
122
__slots__ = ['file_id', 'name', 'parent_id', 'text_size', 'text_sha1',
125
def __init__(self, file_id, name, parent_id, text_size=None,
126
text_sha1=None, executable=None):
127
self.file_id = file_id
129
self.parent_id = parent_id
130
self.text_size = text_size
131
self.text_sha1 = text_sha1
132
self.executable = executable
138
def __eq__(self, other):
139
return (self.kind == other.kind and
140
self.file_id == other.file_id and
141
self.name == other.name and
142
self.parent_id == other.parent_id and
143
self.text_sha1 == other.text_sha1 and
144
self.text_size == other.text_size and
145
self.executable == other.executable)
148
return ("%s(file_id=%r, name=%r, parent_id=%r, text_size=%r, "
149
"text_sha1=%r, executable=%r)") % (
150
type(self).__name__, self.file_id, self.name, self.parent_id,
151
self.text_size, self.text_sha1, self.executable)
154
ret = self.__class__(
155
self.file_id, self.name, self.parent_id)
156
ret.text_sha1 = self.text_sha1
157
ret.text_size = self.text_size
158
ret.executable = self.executable
162
class GitTreeSymlink(_mod_tree.TreeLink):
164
__slots__ = ['file_id', 'name', 'parent_id', 'symlink_target']
166
def __init__(self, file_id, name, parent_id,
167
symlink_target=None):
168
self.file_id = file_id
170
self.parent_id = parent_id
171
self.symlink_target = symlink_target
178
def executable(self):
186
return "%s(file_id=%r, name=%r, parent_id=%r, symlink_target=%r)" % (
187
type(self).__name__, self.file_id, self.name, self.parent_id,
190
def __eq__(self, other):
191
return (self.kind == other.kind and
192
self.file_id == other.file_id and
193
self.name == other.name and
194
self.parent_id == other.parent_id and
195
self.symlink_target == other.symlink_target)
198
return self.__class__(
199
self.file_id, self.name, self.parent_id,
203
class GitTreeSubmodule(_mod_tree.TreeReference):
205
__slots__ = ['file_id', 'name', 'parent_id', 'reference_revision']
207
def __init__(self, file_id, name, parent_id, reference_revision=None):
208
self.file_id = file_id
210
self.parent_id = parent_id
211
self.reference_revision = reference_revision
215
return 'tree-reference'
218
return ("%s(file_id=%r, name=%r, parent_id=%r, "
219
"reference_revision=%r)") % (
220
type(self).__name__, self.file_id, self.name, self.parent_id,
221
self.reference_revision)
223
def __eq__(self, other):
224
return (self.kind == other.kind and
225
self.file_id == other.file_id and
226
self.name == other.name and
227
self.parent_id == other.parent_id and
228
self.reference_revision == other.reference_revision)
231
return self.__class__(
232
self.file_id, self.name, self.parent_id,
233
self.reference_revision)
237
'directory': GitTreeDirectory,
239
'symlink': GitTreeSymlink,
240
'tree-reference': GitTreeSubmodule,
244
def ensure_normalized_path(path):
245
"""Check whether path is normalized.
247
:raises InvalidNormalization: When path is not normalized, and cannot be
248
accessed on this platform by the normalized path.
249
:return: The NFC normalised version of path.
251
norm_path, can_access = osutils.normalized_filename(path)
252
if norm_path != path:
256
raise errors.InvalidNormalization(path)
260
class GitRevisionTree(revisiontree.RevisionTree):
261
"""Revision tree implementation based on Git objects."""
263
def __init__(self, repository, revision_id):
264
self._revision_id = revision_id
265
self._repository = repository
266
self._submodules = None
267
self.store = repository._git.object_store
268
if not isinstance(revision_id, bytes):
269
raise TypeError(revision_id)
270
self.commit_id, self.mapping = repository.lookup_bzr_revision_id(
272
if revision_id == NULL_REVISION:
274
self.mapping = default_mapping
277
commit = self.store[self.commit_id]
279
raise errors.NoSuchRevision(repository, revision_id)
280
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
relpath.decode('utf-8'))
303
nested_repo_transport = self._repository.controldir.control_transport.clone(
304
posixpath.join('modules', info[1]))
305
nested_controldir = _mod_controldir.ControlDir.open_from_transport(
306
nested_repo_transport)
307
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 = path.encode('utf-8')
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
def supports_rename_tracking(self):
321
def get_file_revision(self, path):
322
change_scanner = self._repository._file_change_scanner
323
if self.commit_id == ZERO_SHA:
325
(unused_path, commit_id) = change_scanner.find_last_change_revision(
326
path.encode('utf-8'), self.commit_id)
327
return self._repository.lookup_foreign_revision_id(
328
commit_id, self.mapping)
330
def get_file_mtime(self, path):
332
revid = self.get_file_revision(path)
334
raise errors.NoSuchFile(path)
336
rev = self._repository.get_revision(revid)
337
except errors.NoSuchRevision:
338
raise _mod_tree.FileTimestampUnavailable(path)
341
def id2path(self, file_id):
343
path = self.mapping.parse_file_id(file_id)
345
raise errors.NoSuchId(self, file_id)
346
if self.is_versioned(path):
348
raise errors.NoSuchId(self, file_id)
350
def is_versioned(self, path):
351
return self.has_filename(path)
353
def path2id(self, path):
354
if self.mapping.is_special_file(path):
356
if not self.is_versioned(path):
358
return self.mapping.generate_file_id(osutils.safe_unicode(path))
360
def all_file_ids(self):
361
raise errors.UnsupportedOperation(self.all_file_ids, self)
363
def all_versioned_paths(self):
365
todo = [(self.store, b'', self.tree)]
367
(store, path, tree_id) = todo.pop()
370
tree = store[tree_id]
371
for name, mode, hexsha in tree.items():
372
subpath = posixpath.join(path, name)
373
ret.add(subpath.decode('utf-8'))
374
if stat.S_ISDIR(mode):
375
todo.append((store, subpath, hexsha))
378
def _lookup_path(self, path):
379
if self.tree is None:
380
raise errors.NoSuchFile(path)
382
encoded_path = path.encode('utf-8')
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):
403
(store, mode, hexsha) = self._lookup_path(path)
405
# the tree root is a directory
407
return mode_is_executable(mode)
409
def kind(self, path):
410
(store, mode, hexsha) = self._lookup_path(path)
412
# the tree root is a directory
414
return mode_kind(mode)
416
def has_filename(self, path):
418
self._lookup_path(path)
419
except errors.NoSuchFile:
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):
438
if self.tree is None:
440
if from_dir is None or from_dir == '.':
442
(store, mode, hexsha) = self._lookup_path(from_dir)
443
if mode is None: # Root
444
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)
448
if mode_kind(mode) == 'directory':
449
root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
451
root_ie = self._get_file_ie(
452
store, from_dir.encode("utf-8"),
453
posixpath.basename(from_dir), mode, hexsha)
455
yield (from_dir, "V", root_ie.kind, root_ie)
457
if root_ie.kind == 'directory':
458
todo.append((store, from_dir.encode("utf-8"),
459
b"", hexsha, root_ie.file_id))
461
(store, path, relpath, hexsha, parent_id) = todo.pop()
463
for name, mode, hexsha in tree.iteritems():
464
if self.mapping.is_special_file(name):
466
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
if stat.S_ISDIR(mode):
473
ie = self._get_dir_ie(child_path, parent_id)
476
(store, child_path, child_relpath, hexsha,
479
ie = self._get_file_ie(
480
store, child_path, name, mode, hexsha, parent_id)
481
yield (child_relpath.decode('utf-8'), "V", ie.kind, ie)
483
def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
484
if not isinstance(path, bytes):
485
raise TypeError(path)
486
if not isinstance(name, bytes):
487
raise TypeError(name)
488
kind = mode_kind(mode)
489
path = path.decode('utf-8')
490
name = name.decode("utf-8")
491
file_id = self.mapping.generate_file_id(path)
492
ie = entry_factory[kind](file_id, name, parent_id)
493
if kind == 'symlink':
494
ie.symlink_target = store[hexsha].data.decode('utf-8')
495
elif kind == 'tree-reference':
496
ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(
499
data = store[hexsha].data
500
ie.text_sha1 = osutils.sha_string(data)
501
ie.text_size = len(data)
502
ie.executable = mode_is_executable(mode)
505
def _get_dir_ie(self, path, parent_id):
506
path = path.decode('utf-8')
507
file_id = self.mapping.generate_file_id(path)
508
return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
510
def iter_child_entries(self, path):
511
(store, mode, tree_sha) = self._lookup_path(path)
513
if mode is not None and not stat.S_ISDIR(mode):
516
encoded_path = path.encode('utf-8')
517
file_id = self.path2id(path)
518
tree = store[tree_sha]
519
for name, mode, hexsha in tree.iteritems():
520
if self.mapping.is_special_file(name):
522
child_path = posixpath.join(encoded_path, name)
523
if stat.S_ISDIR(mode):
524
yield self._get_dir_ie(child_path, file_id)
526
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):
531
if self.tree is None:
533
if specific_files is not None:
534
if specific_files in ([""], []):
535
specific_files = None
537
specific_files = set([p.encode('utf-8')
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)
543
store, path, tree_sha, parent_id = todo.popleft()
544
tree = store[tree_sha]
546
for name, mode, hexsha in tree.iteritems():
547
if self.mapping.is_special_file(name):
549
child_path = posixpath.join(path, name)
550
child_path_decoded = child_path.decode('utf-8')
551
if recurse_nested and S_ISGITLINK(mode):
553
store = self._get_submodule_store(child_path)
554
hexsha = store[hexsha].tree
555
if stat.S_ISDIR(mode):
556
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':
578
def get_revision_id(self):
579
"""See RevisionTree.get_revision_id."""
580
return self._revision_id
582
def get_file_sha1(self, path, stat_value=None):
583
if self.tree is None:
584
raise errors.NoSuchFile(path)
585
return osutils.sha_string(self.get_file_text(path))
587
def get_file_verifier(self, path, stat_value=None):
588
(store, mode, hexsha) = self._lookup_path(path)
589
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):
598
"""See RevisionTree.get_file_text."""
599
(store, mode, hexsha) = self._lookup_path(path)
600
if stat.S_ISREG(mode):
601
return store[hexsha].data
605
def get_symlink_target(self, path):
606
"""See RevisionTree.get_symlink_target."""
607
(store, mode, hexsha) = self._lookup_path(path)
608
if stat.S_ISLNK(mode):
609
return store[hexsha].data.decode('utf-8')
613
def get_reference_revision(self, path):
614
"""See RevisionTree.get_symlink_target."""
615
(store, mode, hexsha) = self._lookup_path(path)
616
if S_ISGITLINK(mode):
617
nested_repo = self._get_submodule_repository(path.encode('utf-8'))
618
return nested_repo.lookup_foreign_revision_id(hexsha)
622
def _comparison_data(self, entry, path):
624
return None, False, None
625
return entry.kind, entry.executable, None
627
def path_content_summary(self, path):
628
"""See Tree.path_content_summary."""
630
(store, mode, hexsha) = self._lookup_path(path)
631
except errors.NoSuchFile:
632
return ('missing', None, None, None)
633
kind = mode_kind(mode)
635
executable = mode_is_executable(mode)
636
contents = store[hexsha].data
637
return (kind, len(contents), executable,
638
osutils.sha_string(contents))
639
elif kind == 'symlink':
640
return (kind, None, None, store[hexsha].data.decode('utf-8'))
641
elif kind == 'tree-reference':
642
nested_repo = self._get_submodule_repository(path.encode('utf-8'))
643
return (kind, None, None,
644
nested_repo.lookup_foreign_revision_id(hexsha))
646
return (kind, None, None, None)
648
def find_related_paths_across_trees(self, paths, trees=[],
649
require_versioned=True):
652
if require_versioned:
653
trees = [self] + (trees if trees is not None else [])
657
if t.is_versioned(p):
662
raise errors.PathsNotVersionedError(unversioned)
663
return filter(self.is_versioned, paths)
665
def _iter_tree_contents(self, include_trees=False):
666
if self.tree is None:
668
return self.store.iter_tree_contents(
669
self.tree, include_trees=include_trees)
671
def annotate_iter(self, path, default_revision=CURRENT_REVISION):
672
"""Return an iterator of revision_id, line tuples.
674
For working trees (and mutable trees in general), the special
675
revision_id 'current:' will be used for lines that are new in this
676
tree, e.g. uncommitted changes.
677
:param default_revision: For lines that don't match a basis, mark them
678
with this revision id. Not all implementations will make use of
681
with self.lock_read():
682
# Now we have the parents of this content
683
from breezy.annotate import Annotator
684
from .annotate import AnnotateProvider
685
annotator = Annotator(AnnotateProvider(
686
self._repository._file_change_scanner))
687
this_key = (path, self.get_file_revision(path))
688
annotations = [(key[-1], line)
689
for key, line in annotator.annotate_flat(this_key)]
692
def _get_rules_searcher(self, default_searcher):
693
return default_searcher
695
def walkdirs(self, prefix=u""):
696
(store, mode, hexsha) = self._lookup_path(prefix)
698
[(store, prefix.encode('utf-8'), hexsha, self.path2id(prefix))])
700
store, path, tree_sha, parent_id = todo.popleft()
701
path_decoded = path.decode('utf-8')
702
tree = store[tree_sha]
704
for name, mode, hexsha in tree.iteritems():
705
if self.mapping.is_special_file(name):
707
child_path = posixpath.join(path, name)
708
file_id = self.path2id(child_path.decode('utf-8'))
709
if stat.S_ISDIR(mode):
710
todo.append((store, child_path, hexsha, file_id))
712
(child_path.decode('utf-8'), name.decode('utf-8'),
713
mode_kind(mode), None,
714
file_id, mode_kind(mode)))
715
yield (path_decoded, parent_id), children
718
def tree_delta_from_git_changes(changes, mappings,
720
require_versioned=False, include_root=False,
722
"""Create a TreeDelta from two git trees.
724
source and target are iterators over tuples with:
725
(filename, sha, mode)
727
(old_mapping, new_mapping) = mappings
728
if target_extras is None:
729
target_extras = set()
730
ret = delta.TreeDelta()
732
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
733
if newpath == b'' and not include_root:
735
if oldpath is not None:
736
oldpath_decoded = oldpath.decode('utf-8')
738
oldpath_decoded = None
739
if newpath is not None:
740
newpath_decoded = newpath.decode('utf-8')
742
newpath_decoded = None
743
if not (specific_files is None or
744
(oldpath is not None and
745
osutils.is_inside_or_parent_of_any(
746
specific_files, oldpath_decoded)) or
747
(newpath is not None and
748
osutils.is_inside_or_parent_of_any(
749
specific_files, newpath_decoded))):
752
if oldpath_decoded is None:
753
fileid = new_mapping.generate_file_id(newpath_decoded)
762
oldexe = mode_is_executable(oldmode)
763
oldkind = mode_kind(oldmode)
767
if oldpath_decoded == u'':
771
(oldparentpath, oldname) = osutils.split(oldpath_decoded)
772
oldparent = old_mapping.generate_file_id(oldparentpath)
773
fileid = old_mapping.generate_file_id(oldpath_decoded)
774
if newpath_decoded is None:
781
newversioned = (newpath_decoded not in target_extras)
783
newexe = mode_is_executable(newmode)
784
newkind = mode_kind(newmode)
788
if newpath_decoded == u'':
792
newparentpath, newname = osutils.split(newpath_decoded)
793
newparent = new_mapping.generate_file_id(newparentpath)
794
if old_mapping.is_special_file(oldpath):
796
if new_mapping.is_special_file(newpath):
798
if oldpath is None and newpath is None:
800
change = _mod_tree.TreeChange(
801
fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
802
(oldversioned, newversioned),
803
(oldparent, newparent), (oldname, newname),
804
(oldkind, newkind), (oldexe, newexe))
806
added.append((newpath, newkind))
807
elif newpath is None or newmode == 0:
808
ret.removed.append(change)
809
elif oldpath != newpath:
810
ret.renamed.append(change)
811
elif mode_kind(oldmode) != mode_kind(newmode):
812
ret.kind_changed.append(change)
813
elif oldsha != newsha or oldmode != newmode:
814
if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
816
ret.modified.append(change)
818
ret.unchanged.append(change)
820
implicit_dirs = {b''}
821
for path, kind in added:
822
if kind == 'directory' or path in target_extras:
824
implicit_dirs.update(osutils.parent_directories(path))
826
for path, kind in added:
827
if kind == 'directory' and path not in implicit_dirs:
829
path_decoded = osutils.normalized_filename(path)[0]
830
parent_path, basename = osutils.split(path_decoded)
831
parent_id = new_mapping.generate_file_id(parent_path)
832
if path in target_extras:
833
ret.unversioned.append(_mod_tree.TreeChange(
834
None, (None, path_decoded),
835
True, (False, False), (None, parent_id),
836
(None, basename), (None, kind), (None, False)))
838
file_id = new_mapping.generate_file_id(path_decoded)
840
_mod_tree.TreeChange(
841
file_id, (None, path_decoded), True,
844
(None, basename), (None, kind), (None, False)))
849
def changes_from_git_changes(changes, mapping, specific_files=None,
850
include_unchanged=False, target_extras=None):
851
"""Create a iter_changes-like generator from a git stream.
853
source and target are iterators over tuples with:
854
(filename, sha, mode)
856
if target_extras is None:
857
target_extras = set()
858
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
859
if oldpath is not None:
860
oldpath_decoded = oldpath.decode('utf-8')
862
oldpath_decoded = None
863
if newpath is not None:
864
newpath_decoded = newpath.decode('utf-8')
866
newpath_decoded = None
867
if not (specific_files is None or
868
(oldpath_decoded is not None and
869
osutils.is_inside_or_parent_of_any(
870
specific_files, oldpath_decoded)) or
871
(newpath_decoded is not None and
872
osutils.is_inside_or_parent_of_any(
873
specific_files, newpath_decoded))):
875
if oldpath is not None and mapping.is_special_file(oldpath):
877
if newpath is not None and mapping.is_special_file(newpath):
879
if oldpath_decoded is None:
880
fileid = mapping.generate_file_id(newpath_decoded)
889
oldexe = mode_is_executable(oldmode)
890
oldkind = mode_kind(oldmode)
894
if oldpath_decoded == u'':
898
(oldparentpath, oldname) = osutils.split(oldpath_decoded)
899
oldparent = mapping.generate_file_id(oldparentpath)
900
fileid = mapping.generate_file_id(oldpath_decoded)
901
if newpath_decoded is None:
908
newversioned = (newpath_decoded not in target_extras)
910
newexe = mode_is_executable(newmode)
911
newkind = mode_kind(newmode)
915
if newpath_decoded == u'':
919
newparentpath, newname = osutils.split(newpath_decoded)
920
newparent = mapping.generate_file_id(newparentpath)
921
if (not include_unchanged and
922
oldkind == 'directory' and newkind == 'directory' and
923
oldpath_decoded == newpath_decoded):
925
yield _mod_tree.TreeChange(
926
fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
927
(oldversioned, newversioned),
928
(oldparent, newparent), (oldname, newname),
929
(oldkind, newkind), (oldexe, newexe))
932
class InterGitTrees(_mod_tree.InterTree):
933
"""InterTree that works between two git trees."""
935
_matching_from_tree_format = None
936
_matching_to_tree_format = None
937
_test_mutable_trees_to_test_trees = None
940
def is_compatible(cls, source, target):
941
return (isinstance(source, GitRevisionTree) and
942
isinstance(target, GitRevisionTree))
944
def compare(self, want_unchanged=False, specific_files=None,
945
extra_trees=None, require_versioned=False, include_root=False,
946
want_unversioned=False):
947
with self.lock_read():
948
changes, target_extras = self._iter_git_changes(
949
want_unchanged=want_unchanged,
950
require_versioned=require_versioned,
951
specific_files=specific_files,
952
extra_trees=extra_trees,
953
want_unversioned=want_unversioned)
954
return tree_delta_from_git_changes(
955
changes, (self.source.mapping, self.target.mapping),
956
specific_files=specific_files,
957
include_root=include_root, target_extras=target_extras)
959
def iter_changes(self, include_unchanged=False, specific_files=None,
960
pb=None, extra_trees=[], require_versioned=True,
961
want_unversioned=False):
962
with self.lock_read():
963
changes, target_extras = self._iter_git_changes(
964
want_unchanged=include_unchanged,
965
require_versioned=require_versioned,
966
specific_files=specific_files,
967
extra_trees=extra_trees,
968
want_unversioned=want_unversioned)
969
return changes_from_git_changes(
970
changes, self.target.mapping,
971
specific_files=specific_files,
972
include_unchanged=include_unchanged,
973
target_extras=target_extras)
975
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
976
require_versioned=False, extra_trees=None,
977
want_unversioned=False):
978
raise NotImplementedError(self._iter_git_changes)
981
class InterGitRevisionTrees(InterGitTrees):
982
"""InterTree that works between two git revision trees."""
984
_matching_from_tree_format = None
985
_matching_to_tree_format = None
986
_test_mutable_trees_to_test_trees = None
989
def is_compatible(cls, source, target):
990
return (isinstance(source, GitRevisionTree) and
991
isinstance(target, GitRevisionTree))
993
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
994
require_versioned=True, extra_trees=None,
995
want_unversioned=False):
996
trees = [self.source]
997
if extra_trees is not None:
998
trees.extend(extra_trees)
999
if specific_files is not None:
1000
specific_files = self.target.find_related_paths_across_trees(
1001
specific_files, trees,
1002
require_versioned=require_versioned)
1004
if (self.source._repository._git.object_store !=
1005
self.target._repository._git.object_store):
1006
store = OverlayObjectStore(
1007
[self.source._repository._git.object_store,
1008
self.target._repository._git.object_store])
1010
store = self.source._repository._git.object_store
1011
return store.tree_changes(
1012
self.source.tree, self.target.tree, want_unchanged=want_unchanged,
1013
include_trees=True, change_type_same=True), set()
1016
_mod_tree.InterTree.register_optimiser(InterGitRevisionTrees)
1019
class MutableGitIndexTree(mutabletree.MutableTree):
1022
self._lock_mode = None
1023
self._lock_count = 0
1024
self._versioned_dirs = None
1025
self._index_dirty = False
1026
self._submodules = None
1028
def is_versioned(self, path):
1029
with self.lock_read():
1030
path = path.rstrip('/').encode('utf-8')
1031
(index, subpath) = self._lookup_index(path)
1032
return (subpath in index or self._has_dir(path))
1034
def _has_dir(self, path):
1035
if not isinstance(path, bytes):
1036
raise TypeError(path)
1039
if self._versioned_dirs is None:
1041
return path in self._versioned_dirs
1043
def _load_dirs(self):
1044
if self._lock_mode is None:
1045
raise errors.ObjectNotLocked(self)
1046
self._versioned_dirs = set()
1047
for p, i in self._recurse_index_entries():
1048
self._ensure_versioned_dir(posixpath.dirname(p))
1050
def _ensure_versioned_dir(self, dirname):
1051
if not isinstance(dirname, bytes):
1052
raise TypeError(dirname)
1053
if dirname in self._versioned_dirs:
1056
self._ensure_versioned_dir(posixpath.dirname(dirname))
1057
self._versioned_dirs.add(dirname)
1059
def path2id(self, path):
1060
with self.lock_read():
1061
path = path.rstrip('/')
1062
if self.is_versioned(path.rstrip('/')):
1063
return self.mapping.generate_file_id(
1064
osutils.safe_unicode(path))
1067
def id2path(self, file_id):
1070
if type(file_id) is not bytes:
1071
raise TypeError(file_id)
1072
with self.lock_read():
1074
path = self.mapping.parse_file_id(file_id)
1076
raise errors.NoSuchId(self, file_id)
1077
if self.is_versioned(path):
1079
raise errors.NoSuchId(self, file_id)
1081
def _set_root_id(self, file_id):
1082
raise errors.UnsupportedOperation(self._set_root_id, self)
1084
def _add(self, files, ids, kinds):
1085
for (path, file_id, kind) in zip(files, ids, kinds):
1086
if file_id is not None:
1087
raise workingtree.SettingFileIdUnsupported()
1088
path, can_access = osutils.normalized_filename(path)
1090
raise errors.InvalidNormalization(path)
1091
self._index_add_entry(path, kind)
1093
def _read_submodule_head(self, path):
1094
raise NotImplementedError(self._read_submodule_head)
1096
def _submodule_info(self):
1097
if self._submodules is None:
1099
with self.get_file('.gitmodules') as f:
1100
config = GitConfigFile.from_file(f)
1101
self._submodules = {
1102
path: (url, section)
1103
for path, url, section in parse_submodules(config)}
1104
except errors.NoSuchFile:
1105
self._submodules = {}
1106
return self._submodules
1108
def _lookup_index(self, encoded_path):
1109
if not isinstance(encoded_path, bytes):
1110
raise TypeError(encoded_path)
1112
if encoded_path in self.index:
1113
return self.index, encoded_path
1114
# TODO(jelmer): Perhaps have a cache with paths under which some
1117
remaining_path = encoded_path
1119
parts = remaining_path.split(b'/')
1120
for i in range(1, len(parts)):
1121
basepath = b'/'.join(parts[:i])
1123
(ctime, mtime, dev, ino, mode, uid, gid, size, sha,
1124
flags) = index[basepath]
1128
if S_ISGITLINK(mode):
1129
index = self._get_submodule_index(basepath)
1130
remaining_path = b'/'.join(parts[i:])
1133
return index, remaining_path
1135
return index, remaining_path
1136
return index, remaining_path
1138
def _index_del_entry(self, index, path):
1140
# TODO(jelmer): Keep track of dirty per index
1141
self._index_dirty = True
1143
def _index_add_entry(self, path, kind, flags=0, reference_revision=None):
1144
if kind == "directory":
1145
# Git indexes don't contain directories
1150
file, stat_val = self.get_file_with_stat(path)
1151
except (errors.NoSuchFile, IOError):
1152
# TODO: Rather than come up with something here, use the old
1155
stat_val = os.stat_result(
1156
(stat.S_IFREG | 0o644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1158
blob.set_raw_string(file.read())
1159
# Add object to the repository if it didn't exist yet
1160
if blob.id not in self.store:
1161
self.store.add_object(blob)
1163
elif kind == "symlink":
1166
stat_val = self._lstat(path)
1167
except EnvironmentError:
1168
# TODO: Rather than come up with something here, use the
1170
stat_val = os.stat_result(
1171
(stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1172
blob.set_raw_string(
1173
self.get_symlink_target(path).encode("utf-8"))
1174
# Add object to the repository if it didn't exist yet
1175
if blob.id not in self.store:
1176
self.store.add_object(blob)
1178
elif kind == "tree-reference":
1179
if reference_revision is not None:
1180
hexsha = self.branch.lookup_bzr_revision_id(
1181
reference_revision)[0]
1183
hexsha = self._read_submodule_head(path)
1185
raise errors.NoCommits(path)
1187
stat_val = self._lstat(path)
1188
except EnvironmentError:
1189
stat_val = os.stat_result(
1190
(S_IFGITLINK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1191
stat_val = os.stat_result((S_IFGITLINK, ) + stat_val[1:])
1193
raise AssertionError("unknown kind '%s'" % kind)
1194
# Add an entry to the index or update the existing entry
1195
ensure_normalized_path(path)
1196
encoded_path = path.encode("utf-8")
1197
if b'\r' in encoded_path or b'\n' in encoded_path:
1198
# TODO(jelmer): Why do we need to do this?
1199
trace.mutter('ignoring path with invalid newline in it: %r', path)
1201
(index, index_path) = self._lookup_index(encoded_path)
1202
index[index_path] = index_entry_from_stat(stat_val, hexsha, flags)
1203
self._index_dirty = True
1204
if self._versioned_dirs is not None:
1205
self._ensure_versioned_dir(index_path)
1207
def _recurse_index_entries(self, index=None, basepath=b"",
1208
recurse_nested=False):
1209
# Iterate over all index entries
1210
with self.lock_read():
1213
for path, value in index.items():
1214
(ctime, mtime, dev, ino, mode, uid, gid, size, sha,
1216
if S_ISGITLINK(mode) and recurse_nested:
1217
subindex = self._get_submodule_index(path)
1218
for entry in self._recurse_index_entries(
1219
index=subindex, basepath=path,
1220
recurse_nested=recurse_nested):
1223
yield (posixpath.join(basepath, path), value)
1225
def iter_entries_by_dir(self, specific_files=None,
1226
recurse_nested=False):
1227
with self.lock_read():
1228
if specific_files is not None:
1229
specific_files = set(specific_files)
1231
specific_files = None
1232
root_ie = self._get_dir_ie(u"", None)
1234
if specific_files is None or u"" in specific_files:
1235
ret[(u"", u"")] = root_ie
1236
dir_ids = {u"": root_ie.file_id}
1237
for path, value in self._recurse_index_entries(
1238
recurse_nested=recurse_nested):
1239
if self.mapping.is_special_file(path):
1241
path = path.decode("utf-8")
1242
if specific_files is not None and path not in specific_files:
1244
(parent, name) = posixpath.split(path)
1246
file_ie = self._get_file_ie(name, path, value, None)
1247
except errors.NoSuchFile:
1249
if specific_files is None:
1250
for (dir_path, dir_ie) in self._add_missing_parent_ids(
1252
ret[(posixpath.dirname(dir_path), dir_path)] = dir_ie
1253
file_ie.parent_id = self.path2id(parent)
1254
ret[(posixpath.dirname(path), path)] = file_ie
1255
# Special casing for directories
1257
for path in specific_files:
1258
key = (posixpath.dirname(path), path)
1259
if key not in ret and self.is_versioned(path):
1260
ret[key] = self._get_dir_ie(path, self.path2id(key[0]))
1261
return ((path, ie) for ((_, path), ie) in sorted(viewitems(ret)))
1263
def iter_references(self):
1264
if self.supports_tree_reference():
1265
# TODO(jelmer): Implement a more efficient version of this
1266
for path, entry in self.iter_entries_by_dir():
1267
if entry.kind == 'tree-reference':
1270
def _get_dir_ie(self, path, parent_id):
1271
file_id = self.path2id(path)
1272
return GitTreeDirectory(file_id,
1273
posixpath.basename(path).strip("/"), parent_id)
1275
def _get_file_ie(self, name, path, value, parent_id):
1276
if not isinstance(name, text_type):
1277
raise TypeError(name)
1278
if not isinstance(path, text_type):
1279
raise TypeError(path)
1280
if not isinstance(value, tuple) or len(value) != 10:
1281
raise TypeError(value)
1282
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
1283
file_id = self.path2id(path)
1284
if not isinstance(file_id, bytes):
1285
raise TypeError(file_id)
1286
kind = mode_kind(mode)
1287
ie = entry_factory[kind](file_id, name, parent_id)
1288
if kind == 'symlink':
1289
ie.symlink_target = self.get_symlink_target(path)
1290
elif kind == 'tree-reference':
1291
ie.reference_revision = self.get_reference_revision(path)
1294
data = self.get_file_text(path)
1295
except errors.NoSuchFile:
1297
except IOError as e:
1298
if e.errno != errno.ENOENT:
1302
data = self.branch.repository._git.object_store[sha].data
1303
ie.text_sha1 = osutils.sha_string(data)
1304
ie.text_size = len(data)
1305
ie.executable = bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
1308
def _add_missing_parent_ids(self, path, dir_ids):
1311
parent = posixpath.dirname(path).strip("/")
1312
ret = self._add_missing_parent_ids(parent, dir_ids)
1313
parent_id = dir_ids[parent]
1314
ie = self._get_dir_ie(path, parent_id)
1315
dir_ids[path] = ie.file_id
1316
ret.append((path, ie))
1319
def _comparison_data(self, entry, path):
1321
return None, False, None
1322
return entry.kind, entry.executable, None
1324
def _unversion_path(self, path):
1325
if self._lock_mode is None:
1326
raise errors.ObjectNotLocked(self)
1327
encoded_path = path.encode("utf-8")
1329
(index, subpath) = self._lookup_index(encoded_path)
1331
self._index_del_entry(index, encoded_path)
1333
# A directory, perhaps?
1334
# TODO(jelmer): Deletes that involve submodules?
1335
for p in list(index):
1336
if p.startswith(subpath + b"/"):
1338
self._index_del_entry(index, p)
1341
self._versioned_dirs = None
1344
def unversion(self, paths):
1345
with self.lock_tree_write():
1347
if self._unversion_path(path) == 0:
1348
raise errors.NoSuchFile(path)
1349
self._versioned_dirs = None
1355
def update_basis_by_delta(self, revid, delta):
1356
# TODO(jelmer): This shouldn't be called, it's inventory specific.
1357
for (old_path, new_path, file_id, ie) in delta:
1358
if old_path is not None:
1359
(index, old_subpath) = self._lookup_index(
1360
old_path.encode('utf-8'))
1361
if old_subpath in index:
1362
self._index_del_entry(index, old_subpath)
1363
self._versioned_dirs = None
1364
if new_path is not None and ie.kind != 'directory':
1365
self._index_add_entry(new_path, ie.kind)
1367
self._set_merges_from_parent_ids([])
1369
def move(self, from_paths, to_dir=None, after=None):
1371
with self.lock_tree_write():
1372
to_abs = self.abspath(to_dir)
1373
if not os.path.isdir(to_abs):
1374
raise errors.BzrMoveFailedError('', to_dir,
1375
errors.NotADirectory(to_abs))
1377
for from_rel in from_paths:
1378
from_tail = os.path.split(from_rel)[-1]
1379
to_rel = os.path.join(to_dir, from_tail)
1380
self.rename_one(from_rel, to_rel, after=after)
1381
rename_tuples.append((from_rel, to_rel))
1383
return rename_tuples
1385
def rename_one(self, from_rel, to_rel, after=None):
1386
from_path = from_rel.encode("utf-8")
1387
to_rel, can_access = osutils.normalized_filename(to_rel)
1389
raise errors.InvalidNormalization(to_rel)
1390
to_path = to_rel.encode("utf-8")
1391
with self.lock_tree_write():
1393
# Perhaps it's already moved?
1395
not self.has_filename(from_rel) and
1396
self.has_filename(to_rel) and
1397
not self.is_versioned(to_rel))
1399
if not self.has_filename(to_rel):
1400
raise errors.BzrMoveFailedError(
1401
from_rel, to_rel, errors.NoSuchFile(to_rel))
1402
if self.basis_tree().is_versioned(to_rel):
1403
raise errors.BzrMoveFailedError(
1404
from_rel, to_rel, errors.AlreadyVersionedError(to_rel))
1406
kind = self.kind(to_rel)
1409
to_kind = self.kind(to_rel)
1410
except errors.NoSuchFile:
1411
exc_type = errors.BzrRenameFailedError
1414
exc_type = errors.BzrMoveFailedError
1415
if self.is_versioned(to_rel):
1416
raise exc_type(from_rel, to_rel,
1417
errors.AlreadyVersionedError(to_rel))
1418
if not self.has_filename(from_rel):
1419
raise errors.BzrMoveFailedError(
1420
from_rel, to_rel, errors.NoSuchFile(from_rel))
1421
kind = self.kind(from_rel)
1422
if not self.is_versioned(from_rel) and kind != 'directory':
1423
raise exc_type(from_rel, to_rel,
1424
errors.NotVersionedError(from_rel))
1425
if self.has_filename(to_rel):
1426
raise errors.RenameFailedFilesExist(
1427
from_rel, to_rel, errors.FileExists(to_rel))
1429
kind = self.kind(from_rel)
1431
if not after and kind != 'directory':
1432
(index, from_subpath) = self._lookup_index(from_path)
1433
if from_subpath not in index:
1435
raise errors.BzrMoveFailedError(
1437
errors.NotVersionedError(path=from_rel))
1441
self._rename_one(from_rel, to_rel)
1442
except OSError as e:
1443
if e.errno == errno.ENOENT:
1444
raise errors.BzrMoveFailedError(
1445
from_rel, to_rel, errors.NoSuchFile(to_rel))
1447
if kind != 'directory':
1448
(index, from_index_path) = self._lookup_index(from_path)
1450
self._index_del_entry(index, from_path)
1453
self._index_add_entry(to_rel, kind)
1455
todo = [(p, i) for (p, i) in self._recurse_index_entries()
1456
if p.startswith(from_path + b'/')]
1457
for child_path, child_value in todo:
1458
(child_to_index, child_to_index_path) = self._lookup_index(
1459
posixpath.join(to_path, posixpath.relpath(child_path, from_path)))
1460
child_to_index[child_to_index_path] = child_value
1461
# TODO(jelmer): Mark individual index as dirty
1462
self._index_dirty = True
1463
(child_from_index, child_from_index_path) = self._lookup_index(
1465
self._index_del_entry(
1466
child_from_index, child_from_index_path)
1468
self._versioned_dirs = None
1471
def find_related_paths_across_trees(self, paths, trees=[],
1472
require_versioned=True):
1476
if require_versioned:
1477
trees = [self] + (trees if trees is not None else [])
1481
if t.is_versioned(p):
1486
raise errors.PathsNotVersionedError(unversioned)
1488
return filter(self.is_versioned, paths)
1490
def path_content_summary(self, path):
1491
"""See Tree.path_content_summary."""
1493
stat_result = self._lstat(path)
1494
except OSError as e:
1495
if getattr(e, 'errno', None) == errno.ENOENT:
1497
return ('missing', None, None, None)
1498
# propagate other errors
1500
kind = mode_kind(stat_result.st_mode)
1502
return self._file_content_summary(path, stat_result)
1503
elif kind == 'directory':
1504
# perhaps it looks like a plain directory, but it's really a
1506
if self._directory_is_tree_reference(path):
1507
kind = 'tree-reference'
1508
return kind, None, None, None
1509
elif kind == 'symlink':
1510
target = osutils.readlink(self.abspath(path))
1511
return ('symlink', None, None, target)
1513
return (kind, None, None, None)
1515
def stored_kind(self, relpath):
1516
(index, index_path) = self._lookup_index(relpath.encode('utf-8'))
1520
mode = index[index_path].mode
1524
if S_ISGITLINK(mode):
1525
return 'tree-reference'
1528
def kind(self, relpath):
1529
kind = osutils.file_kind(self.abspath(relpath))
1530
if kind == 'directory':
1531
if self._directory_is_tree_reference(relpath):
1532
return 'tree-reference'
1537
def _live_entry(self, relpath):
1538
raise NotImplementedError(self._live_entry)
1540
def get_transform(self, pb=None):
1541
from ..transform import TreeTransform
1542
return TreeTransform(self, pb=pb)
1546
class InterIndexGitTree(InterGitTrees):
1547
"""InterTree that works between a Git revision tree and an index."""
1549
def __init__(self, source, target):
1550
super(InterIndexGitTree, self).__init__(source, target)
1551
self._index = target.index
1554
def is_compatible(cls, source, target):
1555
return (isinstance(source, GitRevisionTree) and
1556
isinstance(target, MutableGitIndexTree))
1558
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1559
require_versioned=False, extra_trees=None,
1560
want_unversioned=False):
1561
trees = [self.source]
1562
if extra_trees is not None:
1563
trees.extend(extra_trees)
1564
if specific_files is not None:
1565
specific_files = self.target.find_related_paths_across_trees(
1566
specific_files, trees,
1567
require_versioned=require_versioned)
1568
# TODO(jelmer): Restrict to specific_files, for performance reasons.
1569
with self.lock_read():
1570
return changes_between_git_tree_and_working_copy(
1571
self.source.store, self.source.tree,
1572
self.target, want_unchanged=want_unchanged,
1573
want_unversioned=want_unversioned)
1576
_mod_tree.InterTree.register_optimiser(InterIndexGitTree)
1579
def changes_between_git_tree_and_working_copy(store, from_tree_sha, target,
1580
want_unchanged=False,
1581
want_unversioned=False):
1582
"""Determine the changes between a git tree and a working tree with index.
1587
# Report dirified directories to commit_tree first, so that they can be
1588
# replaced with non-empty directories if they have contents.
1590
trust_executable = target._supports_executable()
1591
for path, index_entry in target._recurse_index_entries():
1593
live_entry = target._live_entry(path)
1594
except EnvironmentError as e:
1595
if e.errno == errno.ENOENT:
1596
# Entry was removed; keep it listed, but mark it as gone.
1597
blobs[path] = (ZERO_SHA, 0)
1598
elif e.errno == errno.EISDIR:
1599
# Backwards compatibility with Dulwich < 0.19.12;
1600
# newer versions of Dulwich return either an entry for the
1601
# submodule or None for directories.
1602
if S_ISGITLINK(index_entry.mode):
1603
blobs[path] = (index_entry.sha, index_entry.mode)
1605
# Entry was turned into a directory
1606
dirified.append((path, Tree().id, stat.S_IFDIR))
1607
store.add_object(Tree())
1611
if live_entry is None:
1612
# Entry was turned into a directory
1613
dirified.append((path, Tree().id, stat.S_IFDIR))
1614
store.add_object(Tree())
1616
mode = live_entry.mode
1617
if not trust_executable:
1618
if mode_is_executable(index_entry.mode):
1622
blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
1623
if want_unversioned:
1624
for e in target.extras():
1625
st = target._lstat(e)
1627
np, accessible = osutils.normalized_filename(e)
1628
except UnicodeDecodeError:
1629
raise errors.BadFilenameEncoding(
1631
if stat.S_ISDIR(st.st_mode):
1634
blob = blob_from_path_and_stat(
1635
target.abspath(e).encode(osutils._fs_enc), st)
1636
store.add_object(blob)
1637
np = np.encode('utf-8')
1638
blobs[np] = (blob.id, cleanup_mode(st.st_mode))
1640
to_tree_sha = commit_tree(
1641
store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()])
1642
return store.tree_changes(
1643
from_tree_sha, to_tree_sha, include_trees=True,
1644
want_unchanged=want_unchanged, change_type_same=True), extras