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,
59
from ..revision import (
63
from ..sixish import (
68
from .mapping import (
76
class GitTreeDirectory(_mod_tree.TreeDirectory):
78
__slots__ = ['file_id', 'name', 'parent_id', 'children']
80
def __init__(self, file_id, name, parent_id):
81
self.file_id = file_id
83
self.parent_id = parent_id
96
return self.__class__(
97
self.file_id, self.name, self.parent_id)
100
return "%s(file_id=%r, name=%r, parent_id=%r)" % (
101
self.__class__.__name__, self.file_id, self.name,
104
def __eq__(self, other):
105
return (self.kind == other.kind and
106
self.file_id == other.file_id and
107
self.name == other.name and
108
self.parent_id == other.parent_id)
111
class GitTreeFile(_mod_tree.TreeFile):
113
__slots__ = ['file_id', 'name', 'parent_id', 'text_size', 'text_sha1',
116
def __init__(self, file_id, name, parent_id, text_size=None,
117
text_sha1=None, executable=None):
118
self.file_id = file_id
120
self.parent_id = parent_id
121
self.text_size = text_size
122
self.text_sha1 = text_sha1
123
self.executable = executable
129
def __eq__(self, other):
130
return (self.kind == other.kind and
131
self.file_id == other.file_id and
132
self.name == other.name and
133
self.parent_id == other.parent_id and
134
self.text_sha1 == other.text_sha1 and
135
self.text_size == other.text_size and
136
self.executable == other.executable)
139
return "%s(file_id=%r, name=%r, parent_id=%r, text_size=%r, text_sha1=%r, executable=%r)" % (
140
type(self).__name__, self.file_id, self.name, self.parent_id,
141
self.text_size, self.text_sha1, self.executable)
144
ret = self.__class__(
145
self.file_id, self.name, self.parent_id)
146
ret.text_sha1 = self.text_sha1
147
ret.text_size = self.text_size
148
ret.executable = self.executable
152
class GitTreeSymlink(_mod_tree.TreeLink):
154
__slots__ = ['file_id', 'name', 'parent_id', 'symlink_target']
156
def __init__(self, file_id, name, parent_id,
157
symlink_target=None):
158
self.file_id = file_id
160
self.parent_id = parent_id
161
self.symlink_target = symlink_target
168
def executable(self):
176
return "%s(file_id=%r, name=%r, parent_id=%r, symlink_target=%r)" % (
177
type(self).__name__, self.file_id, self.name, self.parent_id,
180
def __eq__(self, other):
181
return (self.kind == other.kind and
182
self.file_id == other.file_id and
183
self.name == other.name and
184
self.parent_id == other.parent_id and
185
self.symlink_target == other.symlink_target)
188
return self.__class__(
189
self.file_id, self.name, self.parent_id,
193
class GitTreeSubmodule(_mod_tree.TreeLink):
195
__slots__ = ['file_id', 'name', 'parent_id', 'reference_revision']
197
def __init__(self, file_id, name, parent_id, reference_revision=None):
198
self.file_id = file_id
200
self.parent_id = parent_id
201
self.reference_revision = reference_revision
205
return 'tree-reference'
208
return "%s(file_id=%r, name=%r, parent_id=%r, 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.store = repository._git.object_store
256
if not isinstance(revision_id, bytes):
257
raise TypeError(revision_id)
258
self.commit_id, self.mapping = repository.lookup_bzr_revision_id(revision_id)
259
if revision_id == NULL_REVISION:
261
self.mapping = default_mapping
262
self._fileid_map = GitFileIdMap(
267
commit = self.store[self.commit_id]
269
raise errors.NoSuchRevision(repository, revision_id)
270
self.tree = commit.tree
271
self._fileid_map = self.mapping.get_fileid_map(self.store.__getitem__, self.tree)
273
def _get_nested_repository(self, path):
274
nested_repo_transport = self._repository.user_transport.clone(path)
275
nested_controldir = _mod_controldir.ControlDir.open_from_transport(nested_repo_transport)
276
return nested_controldir.find_repository()
278
def supports_rename_tracking(self):
281
def get_file_revision(self, path, file_id=None):
282
change_scanner = self._repository._file_change_scanner
283
if self.commit_id == ZERO_SHA:
285
(unused_path, commit_id) = change_scanner.find_last_change_revision(
286
path.encode('utf-8'), self.commit_id)
287
return self._repository.lookup_foreign_revision_id(commit_id, self.mapping)
289
def get_file_mtime(self, path, file_id=None):
291
revid = self.get_file_revision(path, file_id)
293
raise errors.NoSuchFile(path)
295
rev = self._repository.get_revision(revid)
296
except errors.NoSuchRevision:
297
raise _mod_tree.FileTimestampUnavailable(path)
300
def id2path(self, file_id):
302
path = self._fileid_map.lookup_path(file_id)
304
raise errors.NoSuchId(self, file_id)
305
if self.is_versioned(path):
307
raise errors.NoSuchId(self, file_id)
309
def is_versioned(self, path):
310
return self.has_filename(path)
312
def path2id(self, path):
313
if self.mapping.is_special_file(path):
315
if not self.is_versioned(path):
317
return self._fileid_map.lookup_file_id(osutils.safe_unicode(path))
319
def all_file_ids(self):
320
return {self.path2id(path) for path in self.all_versioned_paths()}
322
def all_versioned_paths(self):
324
todo = [(self.store, b'', self.tree)]
326
(store, path, tree_id) = todo.pop()
329
tree = store[tree_id]
330
for name, mode, hexsha in tree.items():
331
subpath = posixpath.join(path, name)
332
ret.add(subpath.decode('utf-8'))
333
if stat.S_ISDIR(mode):
334
todo.append((store, subpath, hexsha))
337
def get_root_id(self):
338
if self.tree is None:
340
return self.path2id("")
342
def has_or_had_id(self, file_id):
344
path = self.id2path(file_id)
345
except errors.NoSuchId:
349
def has_id(self, file_id):
351
path = self.id2path(file_id)
352
except errors.NoSuchId:
354
return self.has_filename(path)
356
def _lookup_path(self, path):
357
if self.tree is None:
358
raise errors.NoSuchFile(path)
360
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
361
path.encode('utf-8'))
363
raise errors.NoSuchFile(self, path)
365
return (self.store, mode, hexsha)
367
def is_executable(self, path, file_id=None):
368
(store, mode, hexsha) = self._lookup_path(path)
370
# the tree root is a directory
372
return mode_is_executable(mode)
374
def kind(self, path, file_id=None):
375
(store, mode, hexsha) = self._lookup_path(path)
377
# the tree root is a directory
379
return mode_kind(mode)
381
def has_filename(self, path):
383
self._lookup_path(path)
384
except errors.NoSuchFile:
389
def list_files(self, include_root=False, from_dir=None, recursive=True):
390
if self.tree is None:
394
(store, mode, hexsha) = self._lookup_path(from_dir)
395
if mode is None: # Root
396
root_ie = self._get_dir_ie(b"", None)
398
parent_path = posixpath.dirname(from_dir)
399
parent_id = self._fileid_map.lookup_file_id(parent_path)
400
if mode_kind(mode) == 'directory':
401
root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
403
root_ie = self._get_file_ie(store, from_dir.encode("utf-8"),
404
posixpath.basename(from_dir), mode, hexsha)
406
yield (from_dir, "V", root_ie.kind, root_ie.file_id, root_ie)
408
if root_ie.kind == 'directory':
409
todo.append((store, from_dir.encode("utf-8"), b"", hexsha, root_ie.file_id))
411
(store, path, relpath, hexsha, parent_id) = todo.pop()
413
for name, mode, hexsha in tree.iteritems():
414
if self.mapping.is_special_file(name):
416
child_path = posixpath.join(path, name)
417
child_relpath = posixpath.join(relpath, name)
418
if stat.S_ISDIR(mode):
419
ie = self._get_dir_ie(child_path, parent_id)
421
todo.append((store, child_path, child_relpath, hexsha, ie.file_id))
423
ie = self._get_file_ie(store, child_path, name, mode, hexsha, parent_id)
424
yield child_relpath.decode('utf-8'), "V", ie.kind, ie.file_id, ie
426
def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
427
if not isinstance(path, bytes):
428
raise TypeError(path)
429
if not isinstance(name, bytes):
430
raise TypeError(name)
431
kind = mode_kind(mode)
432
path = path.decode('utf-8')
433
name = name.decode("utf-8")
434
file_id = self._fileid_map.lookup_file_id(path)
435
ie = entry_factory[kind](file_id, name, parent_id)
436
if kind == 'symlink':
437
ie.symlink_target = store[hexsha].data.decode('utf-8')
438
elif kind == 'tree-reference':
439
ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(hexsha)
441
data = store[hexsha].data
442
ie.text_sha1 = osutils.sha_string(data)
443
ie.text_size = len(data)
444
ie.executable = mode_is_executable(mode)
447
def _get_dir_ie(self, path, parent_id):
448
path = path.decode('utf-8')
449
file_id = self._fileid_map.lookup_file_id(path)
450
return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
452
def iter_child_entries(self, path, file_id=None):
453
(store, mode, tree_sha) = self._lookup_path(path)
455
if mode is not None and not stat.S_ISDIR(mode):
458
encoded_path = path.encode('utf-8')
459
file_id = self.path2id(path)
460
tree = store[tree_sha]
461
for name, mode, hexsha in tree.iteritems():
462
if self.mapping.is_special_file(name):
464
child_path = posixpath.join(encoded_path, name)
465
if stat.S_ISDIR(mode):
466
yield self._get_dir_ie(child_path, file_id)
468
yield self._get_file_ie(store, child_path, name, mode, hexsha,
471
def iter_entries_by_dir(self, specific_files=None, yield_parents=False):
472
if self.tree is None:
475
# TODO(jelmer): Support yield parents
476
raise NotImplementedError
477
if specific_files is not None:
478
if specific_files in ([""], []):
479
specific_files = None
481
specific_files = set([p.encode('utf-8') for p in specific_files])
482
todo = deque([(self.store, b"", self.tree, self.get_root_id())])
483
if specific_files is None or u"" in specific_files:
484
yield u"", self._get_dir_ie(b"", None)
486
store, path, tree_sha, parent_id = todo.popleft()
487
tree = store[tree_sha]
489
for name, mode, hexsha in tree.iteritems():
490
if self.mapping.is_special_file(name):
492
child_path = posixpath.join(path, name)
493
child_path_decoded = child_path.decode('utf-8')
494
if stat.S_ISDIR(mode):
495
if (specific_files is None or
496
any(filter(lambda p: p.startswith(child_path), specific_files))):
498
(store, child_path, hexsha, self.path2id(child_path_decoded)))
499
if specific_files is None or child_path in specific_files:
500
if stat.S_ISDIR(mode):
501
yield (child_path_decoded,
502
self._get_dir_ie(child_path, parent_id))
504
yield (child_path_decoded,
505
self._get_file_ie(store, child_path, name, mode,
507
todo.extendleft(reversed(extradirs))
509
def iter_references(self):
510
if self.supports_tree_reference():
511
for path, entry in self.iter_entries_by_dir():
512
if entry.kind == 'tree-reference':
513
yield path, self.mapping.generate_file_id(b'')
515
def get_revision_id(self):
516
"""See RevisionTree.get_revision_id."""
517
return self._revision_id
519
def get_file_sha1(self, path, file_id=None, stat_value=None):
520
if self.tree is None:
521
raise errors.NoSuchFile(path)
522
return osutils.sha_string(self.get_file_text(path, file_id))
524
def get_file_verifier(self, path, file_id=None, stat_value=None):
525
(store, mode, hexsha) = self._lookup_path(path)
526
return ("GIT", hexsha)
528
def get_file_size(self, path, file_id=None):
529
(store, mode, hexsha) = self._lookup_path(path)
530
if stat.S_ISREG(mode):
531
return len(store[hexsha].data)
534
def get_file_text(self, path, file_id=None):
535
"""See RevisionTree.get_file_text."""
536
(store, mode, hexsha) = self._lookup_path(path)
537
if stat.S_ISREG(mode):
538
return store[hexsha].data
542
def get_symlink_target(self, path, file_id=None):
543
"""See RevisionTree.get_symlink_target."""
544
(store, mode, hexsha) = self._lookup_path(path)
545
if stat.S_ISLNK(mode):
546
return store[hexsha].data.decode('utf-8')
550
def get_reference_revision(self, path, file_id=None):
551
"""See RevisionTree.get_symlink_target."""
552
(store, mode, hexsha) = self._lookup_path(path)
553
if S_ISGITLINK(mode):
554
nested_repo = self._get_nested_repository(path)
555
return nested_repo.lookup_foreign_revision_id(hexsha)
559
def _comparison_data(self, entry, path):
561
return None, False, None
562
return entry.kind, entry.executable, None
564
def path_content_summary(self, path):
565
"""See Tree.path_content_summary."""
567
(store, mode, hexsha) = self._lookup_path(path)
568
except errors.NoSuchFile:
569
return ('missing', None, None, None)
570
kind = mode_kind(mode)
572
executable = mode_is_executable(mode)
573
contents = store[hexsha].data
574
return (kind, len(contents), executable, osutils.sha_string(contents))
575
elif kind == 'symlink':
576
return (kind, None, None, store[hexsha].data.decode('utf-8'))
577
elif kind == 'tree-reference':
578
nested_repo = self._get_nested_repository(path)
579
return (kind, None, None,
580
nested_repo.lookup_foreign_revision_id(hexsha))
582
return (kind, None, None, None)
584
def find_related_paths_across_trees(self, paths, trees=[],
585
require_versioned=True):
588
if require_versioned:
589
trees = [self] + (trees if trees is not None else [])
593
if t.is_versioned(p):
598
raise errors.PathsNotVersionedError(unversioned)
599
return filter(self.is_versioned, paths)
601
def _iter_tree_contents(self, include_trees=False):
602
if self.tree is None:
604
return self.store.iter_tree_contents(
605
self.tree, include_trees=include_trees)
607
def annotate_iter(self, path, file_id=None,
608
default_revision=CURRENT_REVISION):
609
"""Return an iterator of revision_id, line tuples.
611
For working trees (and mutable trees in general), the special
612
revision_id 'current:' will be used for lines that are new in this
613
tree, e.g. uncommitted changes.
614
:param file_id: The file to produce an annotated version from
615
:param default_revision: For lines that don't match a basis, mark them
616
with this revision id. Not all implementations will make use of
619
with self.lock_read():
620
# Now we have the parents of this content
621
from breezy.annotate import Annotator
622
from .annotate import AnnotateProvider
623
annotator = Annotator(AnnotateProvider(
624
self._repository._file_change_scanner))
625
this_key = (path, self.get_file_revision(path))
626
annotations = [(key[-1], line)
627
for key, line in annotator.annotate_flat(this_key)]
630
def _get_rules_searcher(self, default_searcher):
631
return default_searcher
633
def walkdirs(self, prefix=u""):
634
(store, mode, hexsha) = self._lookup_path(prefix)
635
todo = deque([(store, prefix.encode('utf-8'), hexsha, self.path2id(prefix))])
637
store, path, tree_sha, parent_id = todo.popleft()
638
path_decoded = path.decode('utf-8')
639
tree = store[tree_sha]
641
for name, mode, hexsha in tree.iteritems():
642
if self.mapping.is_special_file(name):
644
child_path = posixpath.join(path, name)
645
file_id = self.path2id(child_path.decode('utf-8'))
646
if stat.S_ISDIR(mode):
647
todo.append((store, child_path, hexsha, file_id))
649
(child_path.decode('utf-8'), name.decode('utf-8'),
650
mode_kind(mode), None,
651
file_id, mode_kind(mode)))
652
yield (path_decoded, parent_id), children
655
def tree_delta_from_git_changes(changes, mapping,
656
fileid_maps, specific_files=None,
657
require_versioned=False, include_root=False,
659
"""Create a TreeDelta from two git trees.
661
source and target are iterators over tuples with:
662
(filename, sha, mode)
664
(old_fileid_map, new_fileid_map) = fileid_maps
665
if target_extras is None:
666
target_extras = set()
667
ret = delta.TreeDelta()
668
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
669
if newpath == b'' and not include_root:
672
oldpath_encoded = None
674
oldpath_decoded = oldpath.decode('utf-8')
676
newpath_decoded = None
678
newpath_decoded = newpath.decode('utf-8')
679
if not (specific_files is None or
680
(oldpath is not None and osutils.is_inside_or_parent_of_any(specific_files, oldpath_decoded)) or
681
(newpath is not None and osutils.is_inside_or_parent_of_any(specific_files, newpath_decoded))):
683
if mapping.is_special_file(oldpath):
685
if mapping.is_special_file(newpath):
687
if oldpath is None and newpath is None:
690
if newpath in target_extras:
691
ret.unversioned.append(
692
(osutils.normalized_filename(newpath)[0], None, mode_kind(newmode)))
694
file_id = new_fileid_map.lookup_file_id(newpath_decoded)
695
ret.added.append((newpath_decoded, file_id, mode_kind(newmode)))
696
elif newpath is None or newmode == 0:
697
file_id = old_fileid_map.lookup_file_id(oldpath_decoded)
698
ret.removed.append((oldpath_decoded, file_id, mode_kind(oldmode)))
699
elif oldpath != newpath:
700
file_id = old_fileid_map.lookup_file_id(oldpath_decoded)
702
(oldpath_decoded, newpath.decode('utf-8'), file_id,
703
mode_kind(newmode), (oldsha != newsha),
704
(oldmode != newmode)))
705
elif mode_kind(oldmode) != mode_kind(newmode):
706
file_id = new_fileid_map.lookup_file_id(newpath_decoded)
707
ret.kind_changed.append(
708
(newpath_decoded, file_id, mode_kind(oldmode),
710
elif oldsha != newsha or oldmode != newmode:
711
if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
713
file_id = new_fileid_map.lookup_file_id(newpath_decoded)
715
(newpath_decoded, file_id, mode_kind(newmode),
716
(oldsha != newsha), (oldmode != newmode)))
718
file_id = new_fileid_map.lookup_file_id(newpath_decoded)
719
ret.unchanged.append((newpath_decoded, file_id, mode_kind(newmode)))
724
def changes_from_git_changes(changes, mapping, specific_files=None, include_unchanged=False,
726
"""Create a iter_changes-like generator from a git stream.
728
source and target are iterators over tuples with:
729
(filename, sha, mode)
731
if target_extras is None:
732
target_extras = set()
733
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
734
if oldpath is not None:
735
oldpath_decoded = oldpath.decode('utf-8')
737
oldpath_decoded = None
738
if newpath is not None:
739
newpath_decoded = newpath.decode('utf-8')
741
newpath_decoded = None
742
if not (specific_files is None or
743
(oldpath_decoded is not None and osutils.is_inside_or_parent_of_any(specific_files, oldpath_decoded)) or
744
(newpath_decoded is not None and osutils.is_inside_or_parent_of_any(specific_files, newpath_decoded))):
746
if oldpath is not None and mapping.is_special_file(oldpath):
748
if newpath is not None and mapping.is_special_file(newpath):
750
if oldpath_decoded is None:
751
fileid = mapping.generate_file_id(newpath_decoded)
760
oldexe = mode_is_executable(oldmode)
761
oldkind = mode_kind(oldmode)
765
if oldpath_decoded == u'':
769
(oldparentpath, oldname) = osutils.split(oldpath_decoded)
770
oldparent = mapping.generate_file_id(oldparentpath)
771
fileid = mapping.generate_file_id(oldpath_decoded)
772
if newpath_decoded is None:
779
newversioned = (newpath_decoded not in target_extras)
781
newexe = mode_is_executable(newmode)
782
newkind = mode_kind(newmode)
786
if newpath_decoded == u'':
790
newparentpath, newname = osutils.split(newpath_decoded)
791
newparent = mapping.generate_file_id(newparentpath)
792
if (not include_unchanged and
793
oldkind == 'directory' and newkind == 'directory' and
794
oldpath_decoded == newpath_decoded):
796
yield (fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
797
(oldversioned, newversioned),
798
(oldparent, newparent), (oldname, newname),
799
(oldkind, newkind), (oldexe, newexe))
802
class InterGitTrees(_mod_tree.InterTree):
803
"""InterTree that works between two git trees."""
805
_matching_from_tree_format = None
806
_matching_to_tree_format = None
807
_test_mutable_trees_to_test_trees = None
810
def is_compatible(cls, source, target):
811
return (isinstance(source, GitRevisionTree) and
812
isinstance(target, GitRevisionTree))
814
def compare(self, want_unchanged=False, specific_files=None,
815
extra_trees=None, require_versioned=False, include_root=False,
816
want_unversioned=False):
817
with self.lock_read():
818
changes, target_extras = self._iter_git_changes(
819
want_unchanged=want_unchanged,
820
require_versioned=require_versioned,
821
specific_files=specific_files,
822
extra_trees=extra_trees,
823
want_unversioned=want_unversioned)
824
source_fileid_map = self.source._fileid_map
825
target_fileid_map = self.target._fileid_map
826
return tree_delta_from_git_changes(changes, self.target.mapping,
827
(source_fileid_map, target_fileid_map),
828
specific_files=specific_files, include_root=include_root,
829
target_extras=target_extras)
831
def iter_changes(self, include_unchanged=False, specific_files=None,
832
pb=None, extra_trees=[], require_versioned=True,
833
want_unversioned=False):
834
with self.lock_read():
835
changes, target_extras = self._iter_git_changes(
836
want_unchanged=include_unchanged,
837
require_versioned=require_versioned,
838
specific_files=specific_files,
839
extra_trees=extra_trees,
840
want_unversioned=want_unversioned)
841
return changes_from_git_changes(
842
changes, self.target.mapping,
843
specific_files=specific_files,
844
include_unchanged=include_unchanged,
845
target_extras=target_extras)
847
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
848
require_versioned=False, extra_trees=None,
849
want_unversioned=False):
850
raise NotImplementedError(self._iter_git_changes)
853
class InterGitRevisionTrees(InterGitTrees):
854
"""InterTree that works between two git revision trees."""
856
_matching_from_tree_format = None
857
_matching_to_tree_format = None
858
_test_mutable_trees_to_test_trees = None
861
def is_compatible(cls, source, target):
862
return (isinstance(source, GitRevisionTree) and
863
isinstance(target, GitRevisionTree))
865
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
866
require_versioned=True, extra_trees=None,
867
want_unversioned=False):
868
trees = [self.source]
869
if extra_trees is not None:
870
trees.extend(extra_trees)
871
if specific_files is not None:
872
specific_files = self.target.find_related_paths_across_trees(
873
specific_files, trees,
874
require_versioned=require_versioned)
876
if self.source._repository._git.object_store != self.target._repository._git.object_store:
877
store = OverlayObjectStore([self.source._repository._git.object_store,
878
self.target._repository._git.object_store])
880
store = self.source._repository._git.object_store
881
return self.source._repository._git.object_store.tree_changes(
882
self.source.tree, self.target.tree, want_unchanged=want_unchanged,
883
include_trees=True, change_type_same=True), set()
886
_mod_tree.InterTree.register_optimiser(InterGitRevisionTrees)
889
class MutableGitIndexTree(mutabletree.MutableTree):
892
self._lock_mode = None
894
self._versioned_dirs = None
895
self._index_dirty = False
897
def is_versioned(self, path):
898
with self.lock_read():
899
path = path.rstrip('/').encode('utf-8')
900
(index, subpath) = self._lookup_index(path)
901
return (subpath in index or self._has_dir(path))
903
def _has_dir(self, path):
904
if not isinstance(path, bytes):
905
raise TypeError(path)
908
if self._versioned_dirs is None:
910
return path in self._versioned_dirs
912
def _load_dirs(self):
913
if self._lock_mode is None:
914
raise errors.ObjectNotLocked(self)
915
self._versioned_dirs = set()
916
# TODO(jelmer): Browse over all indexes
917
for p, i in self._recurse_index_entries():
918
self._ensure_versioned_dir(posixpath.dirname(p))
920
def _ensure_versioned_dir(self, dirname):
921
if not isinstance(dirname, bytes):
922
raise TypeError(dirname)
923
if dirname in self._versioned_dirs:
926
self._ensure_versioned_dir(posixpath.dirname(dirname))
927
self._versioned_dirs.add(dirname)
929
def path2id(self, path):
930
with self.lock_read():
931
path = path.rstrip('/')
932
if self.is_versioned(path.rstrip('/')):
933
return self._fileid_map.lookup_file_id(osutils.safe_unicode(path))
936
def has_id(self, file_id):
938
self.id2path(file_id)
939
except errors.NoSuchId:
944
def id2path(self, file_id):
947
if type(file_id) is not bytes:
948
raise TypeError(file_id)
949
with self.lock_read():
951
path = self._fileid_map.lookup_path(file_id)
953
raise errors.NoSuchId(self, file_id)
954
if self.is_versioned(path):
956
raise errors.NoSuchId(self, file_id)
958
def _set_root_id(self, file_id):
959
self._fileid_map.set_file_id("", file_id)
961
def get_root_id(self):
962
return self.path2id(u"")
964
def _add(self, files, ids, kinds):
965
for (path, file_id, kind) in zip(files, ids, kinds):
966
if file_id is not None:
967
raise workingtree.SettingFileIdUnsupported()
968
path, can_access = osutils.normalized_filename(path)
970
raise errors.InvalidNormalization(path)
971
self._index_add_entry(path, kind)
973
def _read_submodule_head(self, path):
974
raise NotImplementedError(self._read_submodule_head)
976
def _lookup_index(self, encoded_path):
977
if not isinstance(encoded_path, bytes):
978
raise TypeError(encoded_path)
979
# TODO(jelmer): Look in other indexes
980
return self.index, encoded_path
982
def _index_del_entry(self, index, path):
984
# TODO(jelmer): Keep track of dirty per index
985
self._index_dirty = True
987
def _index_add_entry(self, path, kind, flags=0, reference_revision=None):
988
if kind == "directory":
989
# Git indexes don't contain directories
994
file, stat_val = self.get_file_with_stat(path)
995
except (errors.NoSuchFile, IOError):
996
# TODO: Rather than come up with something here, use the old index
998
stat_val = os.stat_result(
999
(stat.S_IFREG | 0o644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1001
blob.set_raw_string(file.read())
1002
# Add object to the repository if it didn't exist yet
1003
if not blob.id in self.store:
1004
self.store.add_object(blob)
1006
elif kind == "symlink":
1009
stat_val = self._lstat(path)
1010
except EnvironmentError:
1011
# TODO: Rather than come up with something here, use the
1013
stat_val = os.stat_result(
1014
(stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1015
blob.set_raw_string(
1016
self.get_symlink_target(path).encode("utf-8"))
1017
# Add object to the repository if it didn't exist yet
1018
if not blob.id in self.store:
1019
self.store.add_object(blob)
1021
elif kind == "tree-reference":
1022
if reference_revision is not None:
1023
hexsha = self.branch.lookup_bzr_revision_id(reference_revision)[0]
1025
hexsha = self._read_submodule_head(path)
1027
raise errors.NoCommits(path)
1029
stat_val = self._lstat(path)
1030
except EnvironmentError:
1031
stat_val = os.stat_result(
1032
(S_IFGITLINK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1033
stat_val = os.stat_result((S_IFGITLINK, ) + stat_val[1:])
1035
raise AssertionError("unknown kind '%s'" % kind)
1036
# Add an entry to the index or update the existing entry
1037
ensure_normalized_path(path)
1038
encoded_path = path.encode("utf-8")
1039
if b'\r' in encoded_path or b'\n' in encoded_path:
1040
# TODO(jelmer): Why do we need to do this?
1041
trace.mutter('ignoring path with invalid newline in it: %r', path)
1043
(index, index_path) = self._lookup_index(encoded_path)
1044
index[index_path] = index_entry_from_stat(stat_val, hexsha, flags)
1045
self._index_dirty = True
1046
if self._versioned_dirs is not None:
1047
self._ensure_versioned_dir(index_path)
1049
def _recurse_index_entries(self, index=None, basepath=b""):
1050
# Iterate over all index entries
1051
with self.lock_read():
1054
for path, value in index.items():
1055
yield (posixpath.join(basepath, path), value)
1056
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
1057
if S_ISGITLINK(mode):
1058
pass # TODO(jelmer): dive into submodule
1061
def iter_entries_by_dir(self, specific_files=None, yield_parents=False):
1063
raise NotImplementedError(self.iter_entries_by_dir)
1064
with self.lock_read():
1065
if specific_files is not None:
1066
specific_files = set(specific_files)
1068
specific_files = None
1069
root_ie = self._get_dir_ie(u"", None)
1071
if specific_files is None or u"" in specific_files:
1072
ret[(u"", u"")] = root_ie
1073
dir_ids = {u"": root_ie.file_id}
1074
for path, value in self._recurse_index_entries():
1075
if self.mapping.is_special_file(path):
1077
path = path.decode("utf-8")
1078
if specific_files is not None and not path in specific_files:
1080
(parent, name) = posixpath.split(path)
1082
file_ie = self._get_file_ie(name, path, value, None)
1083
except errors.NoSuchFile:
1085
if yield_parents or specific_files is None:
1086
for (dir_path, dir_ie) in self._add_missing_parent_ids(parent,
1088
ret[(posixpath.dirname(dir_path), dir_path)] = dir_ie
1089
file_ie.parent_id = self.path2id(parent)
1090
ret[(posixpath.dirname(path), path)] = file_ie
1091
return ((path, ie) for ((_, path), ie) in sorted(viewitems(ret)))
1093
def iter_references(self):
1094
# TODO(jelmer): Implement a more efficient version of this
1095
for path, entry in self.iter_entries_by_dir():
1096
if entry.kind == 'tree-reference':
1097
yield path, self.mapping.generate_file_id(b'')
1099
def _get_dir_ie(self, path, parent_id):
1100
file_id = self.path2id(path)
1101
return GitTreeDirectory(file_id,
1102
posixpath.basename(path).strip("/"), parent_id)
1104
def _get_file_ie(self, name, path, value, parent_id):
1105
if not isinstance(name, text_type):
1106
raise TypeError(name)
1107
if not isinstance(path, text_type):
1108
raise TypeError(path)
1109
if not isinstance(value, tuple) or len(value) != 10:
1110
raise TypeError(value)
1111
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
1112
file_id = self.path2id(path)
1113
if not isinstance(file_id, bytes):
1114
raise TypeError(file_id)
1115
kind = mode_kind(mode)
1116
ie = entry_factory[kind](file_id, name, parent_id)
1117
if kind == 'symlink':
1118
ie.symlink_target = self.get_symlink_target(path, file_id)
1119
elif kind == 'tree-reference':
1120
ie.reference_revision = self.get_reference_revision(path, file_id)
1123
data = self.get_file_text(path, file_id)
1124
except errors.NoSuchFile:
1126
except IOError as e:
1127
if e.errno != errno.ENOENT:
1131
data = self.branch.repository._git.object_store[sha].data
1132
ie.text_sha1 = osutils.sha_string(data)
1133
ie.text_size = len(data)
1134
ie.executable = bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
1137
def _add_missing_parent_ids(self, path, dir_ids):
1140
parent = posixpath.dirname(path).strip("/")
1141
ret = self._add_missing_parent_ids(parent, dir_ids)
1142
parent_id = dir_ids[parent]
1143
ie = self._get_dir_ie(path, parent_id)
1144
dir_ids[path] = ie.file_id
1145
ret.append((path, ie))
1148
def _comparison_data(self, entry, path):
1150
return None, False, None
1151
return entry.kind, entry.executable, None
1153
def _unversion_path(self, path):
1154
if self._lock_mode is None:
1155
raise errors.ObjectNotLocked(self)
1156
encoded_path = path.encode("utf-8")
1158
(index, subpath) = self._lookup_index(encoded_path)
1160
self._index_del_entry(index, encoded_path)
1162
# A directory, perhaps?
1163
# TODO(jelmer): Deletes that involve submodules?
1164
for p in list(index):
1165
if p.startswith(subpath+b"/"):
1167
self._index_del_entry(index, p)
1170
self._versioned_dirs = None
1173
def unversion(self, paths, file_ids=None):
1174
with self.lock_tree_write():
1176
if self._unversion_path(path) == 0:
1177
raise errors.NoSuchFile(path)
1178
self._versioned_dirs = None
1184
def update_basis_by_delta(self, revid, delta):
1185
# TODO(jelmer): This shouldn't be called, it's inventory specific.
1186
for (old_path, new_path, file_id, ie) in delta:
1187
if old_path is not None:
1188
(index, old_subpath) = self._lookup_index(old_path.encode('utf-8'))
1189
if old_subpath in index:
1190
self._index_del_entry(index, old_subpath)
1191
self._versioned_dirs = None
1192
if new_path is not None and ie.kind != 'directory':
1193
self._index_add_entry(new_path, ie.kind)
1195
self._set_merges_from_parent_ids([])
1197
def move(self, from_paths, to_dir=None, after=None):
1199
with self.lock_tree_write():
1200
to_abs = self.abspath(to_dir)
1201
if not os.path.isdir(to_abs):
1202
raise errors.BzrMoveFailedError('', to_dir,
1203
errors.NotADirectory(to_abs))
1205
for from_rel in from_paths:
1206
from_tail = os.path.split(from_rel)[-1]
1207
to_rel = os.path.join(to_dir, from_tail)
1208
self.rename_one(from_rel, to_rel, after=after)
1209
rename_tuples.append((from_rel, to_rel))
1211
return rename_tuples
1213
def rename_one(self, from_rel, to_rel, after=None):
1214
from_path = from_rel.encode("utf-8")
1215
to_rel, can_access = osutils.normalized_filename(to_rel)
1217
raise errors.InvalidNormalization(to_rel)
1218
to_path = to_rel.encode("utf-8")
1219
with self.lock_tree_write():
1221
# Perhaps it's already moved?
1223
not self.has_filename(from_rel) and
1224
self.has_filename(to_rel) and
1225
not self.is_versioned(to_rel))
1227
if not self.has_filename(to_rel):
1228
raise errors.BzrMoveFailedError(from_rel, to_rel,
1229
errors.NoSuchFile(to_rel))
1230
if self.basis_tree().is_versioned(to_rel):
1231
raise errors.BzrMoveFailedError(from_rel, to_rel,
1232
errors.AlreadyVersionedError(to_rel))
1234
kind = self.kind(to_rel)
1237
to_kind = self.kind(to_rel)
1238
except errors.NoSuchFile:
1239
exc_type = errors.BzrRenameFailedError
1242
exc_type = errors.BzrMoveFailedError
1243
if self.is_versioned(to_rel):
1244
raise exc_type(from_rel, to_rel,
1245
errors.AlreadyVersionedError(to_rel))
1246
if not self.has_filename(from_rel):
1247
raise errors.BzrMoveFailedError(from_rel, to_rel,
1248
errors.NoSuchFile(from_rel))
1249
kind = self.kind(from_rel)
1250
if not self.is_versioned(from_rel) and kind != 'directory':
1251
raise exc_type(from_rel, to_rel,
1252
errors.NotVersionedError(from_rel))
1253
if self.has_filename(to_rel):
1254
raise errors.RenameFailedFilesExist(
1255
from_rel, to_rel, errors.FileExists(to_rel))
1257
kind = self.kind(from_rel)
1259
if not after and kind != 'directory':
1260
(index, from_subpath) = self._lookup_index(from_path)
1261
if from_subpath not in index:
1263
raise errors.BzrMoveFailedError(from_rel, to_rel,
1264
errors.NotVersionedError(path=from_rel))
1268
self._rename_one(from_rel, to_rel)
1269
except OSError as e:
1270
if e.errno == errno.ENOENT:
1271
raise errors.BzrMoveFailedError(from_rel, to_rel,
1272
errors.NoSuchFile(to_rel))
1274
if kind != 'directory':
1275
(index, from_index_path) = self._lookup_index(from_path)
1277
self._index_del_entry(index, from_path)
1280
self._index_add_entry(to_rel, kind)
1282
todo = [(p, i) for (p, i) in self._recurse_index_entries() if p.startswith(from_path+b'/')]
1283
for child_path, child_value in todo:
1284
(child_to_index, child_to_index_path) = self._lookup_index(
1285
posixpath.join(to_path, posixpath.relpath(child_path, from_path)))
1286
child_to_index[child_to_index_path] = child_value
1287
# TODO(jelmer): Mark individual index as dirty
1288
self._index_dirty = True
1289
(child_from_index, child_from_index_path) = self._lookup_index(child_path)
1290
self._index_del_entry(child_from_index, child_from_index_path)
1292
self._versioned_dirs = None
1295
def find_related_paths_across_trees(self, paths, trees=[],
1296
require_versioned=True):
1300
if require_versioned:
1301
trees = [self] + (trees if trees is not None else [])
1305
if t.is_versioned(p):
1310
raise errors.PathsNotVersionedError(unversioned)
1312
return filter(self.is_versioned, paths)
1314
def path_content_summary(self, path):
1315
"""See Tree.path_content_summary."""
1317
stat_result = self._lstat(path)
1318
except OSError as e:
1319
if getattr(e, 'errno', None) == errno.ENOENT:
1321
return ('missing', None, None, None)
1322
# propagate other errors
1324
kind = mode_kind(stat_result.st_mode)
1326
return self._file_content_summary(path, stat_result)
1327
elif kind == 'directory':
1328
# perhaps it looks like a plain directory, but it's really a
1330
if self._directory_is_tree_reference(path):
1331
kind = 'tree-reference'
1332
return kind, None, None, None
1333
elif kind == 'symlink':
1334
target = osutils.readlink(self.abspath(path))
1335
return ('symlink', None, None, target)
1337
return (kind, None, None, None)
1339
def kind(self, relpath, file_id=None):
1340
kind = osutils.file_kind(self.abspath(relpath))
1341
if kind == 'directory':
1342
(index, index_path) = self._lookup_index(relpath.encode('utf-8'))
1346
mode = index[index_path].mode
1350
if S_ISGITLINK(mode):
1351
return 'tree-reference'
1356
def _live_entry(self, relpath):
1357
raise NotImplementedError(self._live_entry)
1360
class InterIndexGitTree(InterGitTrees):
1361
"""InterTree that works between a Git revision tree and an index."""
1363
def __init__(self, source, target):
1364
super(InterIndexGitTree, self).__init__(source, target)
1365
self._index = target.index
1368
def is_compatible(cls, source, target):
1369
return (isinstance(source, GitRevisionTree) and
1370
isinstance(target, MutableGitIndexTree))
1372
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1373
require_versioned=False, extra_trees=None,
1374
want_unversioned=False):
1375
trees = [self.source]
1376
if extra_trees is not None:
1377
trees.extend(extra_trees)
1378
if specific_files is not None:
1379
specific_files = self.target.find_related_paths_across_trees(
1380
specific_files, trees,
1381
require_versioned=require_versioned)
1382
# TODO(jelmer): Restrict to specific_files, for performance reasons.
1383
with self.lock_read():
1384
return changes_between_git_tree_and_working_copy(
1385
self.source.store, self.source.tree,
1386
self.target, want_unchanged=want_unchanged,
1387
want_unversioned=want_unversioned)
1390
_mod_tree.InterTree.register_optimiser(InterIndexGitTree)
1393
def changes_between_git_tree_and_working_copy(store, from_tree_sha, target,
1394
want_unchanged=False, want_unversioned=False):
1395
"""Determine the changes between a git tree and a working tree with index.
1400
# Report dirified directories to commit_tree first, so that they can be
1401
# replaced with non-empty directories if they have contents.
1403
for path, index_entry in target._recurse_index_entries():
1405
live_entry = target._live_entry(path)
1406
except EnvironmentError as e:
1407
if e.errno == errno.ENOENT:
1408
# Entry was removed; keep it listed, but mark it as gone.
1409
blobs[path] = (ZERO_SHA, 0)
1410
elif e.errno == errno.EISDIR:
1411
# Entry was turned into a directory
1412
dirified.append((path, Tree().id, stat.S_IFDIR))
1413
store.add_object(Tree())
1417
blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
1418
if want_unversioned:
1419
for e in target.extras():
1420
st = target._lstat(e)
1422
np, accessible = osutils.normalized_filename(e)
1423
except UnicodeDecodeError:
1424
raise errors.BadFilenameEncoding(
1426
if stat.S_ISDIR(st.st_mode):
1429
blob = blob_from_path_and_stat(target.abspath(e).encode(osutils._fs_enc), st)
1430
store.add_object(blob)
1431
np = np.encode('utf-8')
1432
blobs[np] = (blob.id, cleanup_mode(st.st_mode))
1434
to_tree_sha = commit_tree(store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()])
1435
return store.tree_changes(
1436
from_tree_sha, to_tree_sha, include_trees=True,
1437
want_unchanged=want_unchanged, change_type_same=True), extras