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 (
75
class GitTreeDirectory(_mod_tree.TreeDirectory):
77
__slots__ = ['file_id', 'name', 'parent_id', 'children']
79
def __init__(self, file_id, name, parent_id):
80
self.file_id = file_id
82
self.parent_id = parent_id
95
return self.__class__(
96
self.file_id, self.name, self.parent_id)
99
return "%s(file_id=%r, name=%r, parent_id=%r)" % (
100
self.__class__.__name__, self.file_id, self.name,
103
def __eq__(self, other):
104
return (self.kind == other.kind and
105
self.file_id == other.file_id and
106
self.name == other.name and
107
self.parent_id == other.parent_id)
110
class GitTreeFile(_mod_tree.TreeFile):
112
__slots__ = ['file_id', 'name', 'parent_id', 'text_size', 'text_sha1',
115
def __init__(self, file_id, name, parent_id, text_size=None,
116
text_sha1=None, executable=None):
117
self.file_id = file_id
119
self.parent_id = parent_id
120
self.text_size = text_size
121
self.text_sha1 = text_sha1
122
self.executable = executable
128
def __eq__(self, other):
129
return (self.kind == other.kind and
130
self.file_id == other.file_id and
131
self.name == other.name and
132
self.parent_id == other.parent_id and
133
self.text_sha1 == other.text_sha1 and
134
self.text_size == other.text_size and
135
self.executable == other.executable)
138
return "%s(file_id=%r, name=%r, parent_id=%r, text_size=%r, 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, reference_revision=%r)" % (
208
type(self).__name__, self.file_id, self.name, self.parent_id,
209
self.reference_revision)
211
def __eq__(self, other):
212
return (self.kind == other.kind and
213
self.file_id == other.file_id and
214
self.name == other.name and
215
self.parent_id == other.parent_id and
216
self.reference_revision == other.reference_revision)
219
return self.__class__(
220
self.file_id, self.name, self.parent_id,
221
self.reference_revision)
225
'directory': GitTreeDirectory,
227
'symlink': GitTreeSymlink,
228
'tree-reference': GitTreeSubmodule,
232
def ensure_normalized_path(path):
233
"""Check whether path is normalized.
235
:raises InvalidNormalization: When path is not normalized, and cannot be
236
accessed on this platform by the normalized path.
237
:return: The NFC normalised version of path.
239
norm_path, can_access = osutils.normalized_filename(path)
240
if norm_path != path:
244
raise errors.InvalidNormalization(path)
248
class GitRevisionTree(revisiontree.RevisionTree):
249
"""Revision tree implementation based on Git objects."""
251
def __init__(self, repository, revision_id):
252
self._revision_id = revision_id
253
self._repository = repository
254
self.store = repository._git.object_store
255
if not isinstance(revision_id, bytes):
256
raise TypeError(revision_id)
257
self.commit_id, self.mapping = repository.lookup_bzr_revision_id(revision_id)
258
if revision_id == NULL_REVISION:
260
self.mapping = default_mapping
261
self._fileid_map = GitFileIdMap(
266
commit = self.store[self.commit_id]
268
raise errors.NoSuchRevision(repository, revision_id)
269
self.tree = commit.tree
270
self._fileid_map = self.mapping.get_fileid_map(self.store.__getitem__, self.tree)
272
def _get_nested_repository(self, path):
273
nested_repo_transport = self._repository.user_transport.clone(path)
274
nested_controldir = _mod_controldir.ControlDir.open_from_transport(nested_repo_transport)
275
return nested_controldir.find_repository()
277
def supports_rename_tracking(self):
280
def get_file_revision(self, path, file_id=None):
281
change_scanner = self._repository._file_change_scanner
282
if self.commit_id == ZERO_SHA:
284
(unused_path, commit_id) = change_scanner.find_last_change_revision(
285
path.encode('utf-8'), self.commit_id)
286
return self._repository.lookup_foreign_revision_id(commit_id, self.mapping)
288
def get_file_mtime(self, path, file_id=None):
290
revid = self.get_file_revision(path, file_id)
292
raise errors.NoSuchFile(path)
294
rev = self._repository.get_revision(revid)
295
except errors.NoSuchRevision:
296
raise _mod_tree.FileTimestampUnavailable(path)
299
def id2path(self, file_id):
301
path = self._fileid_map.lookup_path(file_id)
303
raise errors.NoSuchId(self, file_id)
304
if self.is_versioned(path):
306
raise errors.NoSuchId(self, file_id)
308
def is_versioned(self, path):
309
return self.has_filename(path)
311
def path2id(self, path):
312
if self.mapping.is_special_file(path):
314
if not self.is_versioned(path):
316
return self._fileid_map.lookup_file_id(osutils.safe_unicode(path))
318
def all_file_ids(self):
319
return {self.path2id(path) for path in self.all_versioned_paths()}
321
def all_versioned_paths(self):
323
todo = [(self.store, b'', self.tree)]
325
(store, path, tree_id) = todo.pop()
328
tree = store[tree_id]
329
for name, mode, hexsha in tree.items():
330
subpath = posixpath.join(path, name)
331
ret.add(subpath.decode('utf-8'))
332
if stat.S_ISDIR(mode):
333
todo.append((store, subpath, hexsha))
336
def get_root_id(self):
337
if self.tree is None:
339
return self.path2id("")
341
def has_or_had_id(self, file_id):
343
path = self.id2path(file_id)
344
except errors.NoSuchId:
348
def has_id(self, file_id):
350
path = self.id2path(file_id)
351
except errors.NoSuchId:
353
return self.has_filename(path)
355
def _lookup_path(self, path):
356
if self.tree is None:
357
raise errors.NoSuchFile(path)
359
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, self.tree,
360
path.encode('utf-8'))
362
raise errors.NoSuchFile(self, path)
364
return (self.store, mode, hexsha)
366
def is_executable(self, path, file_id=None):
367
(store, mode, hexsha) = self._lookup_path(path)
369
# the tree root is a directory
371
return mode_is_executable(mode)
373
def kind(self, path, file_id=None):
374
(store, mode, hexsha) = self._lookup_path(path)
376
# the tree root is a directory
378
return mode_kind(mode)
380
def has_filename(self, path):
382
self._lookup_path(path)
383
except errors.NoSuchFile:
388
def list_files(self, include_root=False, from_dir=None, recursive=True):
389
if self.tree is None:
393
(store, mode, hexsha) = self._lookup_path(from_dir)
394
if mode is None: # Root
395
root_ie = self._get_dir_ie(b"", None)
397
parent_path = posixpath.dirname(from_dir)
398
parent_id = self._fileid_map.lookup_file_id(parent_path)
399
if mode_kind(mode) == 'directory':
400
root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
402
root_ie = self._get_file_ie(store, from_dir.encode("utf-8"),
403
posixpath.basename(from_dir), mode, hexsha)
405
yield (from_dir, "V", root_ie.kind, root_ie.file_id, root_ie)
407
if root_ie.kind == 'directory':
408
todo.append((store, from_dir.encode("utf-8"), b"", hexsha, root_ie.file_id))
410
(store, path, relpath, hexsha, parent_id) = todo.pop()
412
for name, mode, hexsha in tree.iteritems():
413
if self.mapping.is_special_file(name):
415
child_path = posixpath.join(path, name)
416
child_relpath = posixpath.join(relpath, name)
417
if stat.S_ISDIR(mode):
418
ie = self._get_dir_ie(child_path, parent_id)
420
todo.append((store, child_path, child_relpath, hexsha, ie.file_id))
422
ie = self._get_file_ie(store, child_path, name, mode, hexsha, parent_id)
423
yield child_relpath.decode('utf-8'), "V", ie.kind, ie.file_id, ie
425
def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
426
if not isinstance(path, bytes):
427
raise TypeError(path)
428
if not isinstance(name, bytes):
429
raise TypeError(name)
430
kind = mode_kind(mode)
431
path = path.decode('utf-8')
432
name = name.decode("utf-8")
433
file_id = self._fileid_map.lookup_file_id(path)
434
ie = entry_factory[kind](file_id, name, parent_id)
435
if kind == 'symlink':
436
ie.symlink_target = store[hexsha].data.decode('utf-8')
437
elif kind == 'tree-reference':
438
ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(hexsha)
440
data = store[hexsha].data
441
ie.text_sha1 = osutils.sha_string(data)
442
ie.text_size = len(data)
443
ie.executable = mode_is_executable(mode)
446
def _get_dir_ie(self, path, parent_id):
447
path = path.decode('utf-8')
448
file_id = self._fileid_map.lookup_file_id(path)
449
return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
451
def iter_child_entries(self, path, file_id=None):
452
(store, mode, tree_sha) = self._lookup_path(path)
454
if mode is not None and not stat.S_ISDIR(mode):
457
encoded_path = path.encode('utf-8')
458
file_id = self.path2id(path)
459
tree = store[tree_sha]
460
for name, mode, hexsha in tree.iteritems():
461
if self.mapping.is_special_file(name):
463
child_path = posixpath.join(encoded_path, name)
464
if stat.S_ISDIR(mode):
465
yield self._get_dir_ie(child_path, file_id)
467
yield self._get_file_ie(store, child_path, name, mode, hexsha,
470
def iter_entries_by_dir(self, specific_files=None, yield_parents=False):
471
if self.tree is None:
474
# TODO(jelmer): Support yield parents
475
raise NotImplementedError
476
if specific_files is not None:
477
if specific_files in ([""], []):
478
specific_files = None
480
specific_files = set([p.encode('utf-8') for p in specific_files])
481
todo = deque([(self.store, b"", self.tree, self.get_root_id())])
482
if specific_files is None or u"" in specific_files:
483
yield u"", self._get_dir_ie(b"", None)
485
store, path, tree_sha, parent_id = todo.popleft()
486
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(path, name)
492
child_path_decoded = child_path.decode('utf-8')
493
if stat.S_ISDIR(mode):
494
if (specific_files is None or
495
any(filter(lambda p: p.startswith(child_path), specific_files))):
497
(store, child_path, hexsha, self.path2id(child_path_decoded)))
498
if specific_files is None or child_path in specific_files:
499
if stat.S_ISDIR(mode):
500
yield (child_path_decoded,
501
self._get_dir_ie(child_path, parent_id))
503
yield (child_path_decoded,
504
self._get_file_ie(store, child_path, name, mode,
506
todo.extendleft(reversed(extradirs))
508
def iter_references(self):
509
if self.supports_tree_reference():
510
for path, entry in self.iter_entries_by_dir():
511
if entry.kind == 'tree-reference':
512
yield path, self.mapping.generate_file_id(b'')
514
def get_revision_id(self):
515
"""See RevisionTree.get_revision_id."""
516
return self._revision_id
518
def get_file_sha1(self, path, file_id=None, stat_value=None):
519
if self.tree is None:
520
raise errors.NoSuchFile(path)
521
return osutils.sha_string(self.get_file_text(path, file_id))
523
def get_file_verifier(self, path, file_id=None, stat_value=None):
524
(store, mode, hexsha) = self._lookup_path(path)
525
return ("GIT", hexsha)
527
def get_file_size(self, path, file_id=None):
528
(store, mode, hexsha) = self._lookup_path(path)
529
if stat.S_ISREG(mode):
530
return len(store[hexsha].data)
533
def get_file_text(self, path, file_id=None):
534
"""See RevisionTree.get_file_text."""
535
(store, mode, hexsha) = self._lookup_path(path)
536
if stat.S_ISREG(mode):
537
return store[hexsha].data
541
def get_symlink_target(self, path, file_id=None):
542
"""See RevisionTree.get_symlink_target."""
543
(store, mode, hexsha) = self._lookup_path(path)
544
if stat.S_ISLNK(mode):
545
return store[hexsha].data.decode('utf-8')
549
def get_reference_revision(self, path, file_id=None):
550
"""See RevisionTree.get_symlink_target."""
551
(store, mode, hexsha) = self._lookup_path(path)
552
if S_ISGITLINK(mode):
553
nested_repo = self._get_nested_repository(path)
554
return nested_repo.lookup_foreign_revision_id(hexsha)
558
def _comparison_data(self, entry, path):
560
return None, False, None
561
return entry.kind, entry.executable, None
563
def path_content_summary(self, path):
564
"""See Tree.path_content_summary."""
566
(store, mode, hexsha) = self._lookup_path(path)
567
except errors.NoSuchFile:
568
return ('missing', None, None, None)
569
kind = mode_kind(mode)
571
executable = mode_is_executable(mode)
572
contents = store[hexsha].data
573
return (kind, len(contents), executable, osutils.sha_string(contents))
574
elif kind == 'symlink':
575
return (kind, None, None, store[hexsha].data.decode('utf-8'))
576
elif kind == 'tree-reference':
577
nested_repo = self._get_nested_repository(path)
578
return (kind, None, None,
579
nested_repo.lookup_foreign_revision_id(hexsha))
581
return (kind, None, None, None)
583
def find_related_paths_across_trees(self, paths, trees=[],
584
require_versioned=True):
587
if require_versioned:
588
trees = [self] + (trees if trees is not None else [])
592
if t.is_versioned(p):
597
raise errors.PathsNotVersionedError(unversioned)
598
return filter(self.is_versioned, paths)
600
def _iter_tree_contents(self, include_trees=False):
601
if self.tree is None:
603
return self.store.iter_tree_contents(
604
self.tree, include_trees=include_trees)
606
def annotate_iter(self, path, file_id=None,
607
default_revision=CURRENT_REVISION):
608
"""Return an iterator of revision_id, line tuples.
610
For working trees (and mutable trees in general), the special
611
revision_id 'current:' will be used for lines that are new in this
612
tree, e.g. uncommitted changes.
613
:param file_id: The file to produce an annotated version from
614
:param default_revision: For lines that don't match a basis, mark them
615
with this revision id. Not all implementations will make use of
618
with self.lock_read():
619
# Now we have the parents of this content
620
from breezy.annotate import Annotator
621
from .annotate import AnnotateProvider
622
annotator = Annotator(AnnotateProvider(
623
self._repository._file_change_scanner))
624
this_key = (path, self.get_file_revision(path))
625
annotations = [(key[-1], line)
626
for key, line in annotator.annotate_flat(this_key)]
629
def _get_rules_searcher(self, default_searcher):
630
return default_searcher
632
def walkdirs(self, prefix=u""):
633
(store, mode, hexsha) = self._lookup_path(prefix)
634
todo = deque([(store, prefix.encode('utf-8'), hexsha, self.path2id(prefix))])
636
store, path, tree_sha, parent_id = todo.popleft()
637
path_decoded = path.decode('utf-8')
638
tree = store[tree_sha]
640
for name, mode, hexsha in tree.iteritems():
641
if self.mapping.is_special_file(name):
643
child_path = posixpath.join(path, name)
644
file_id = self.path2id(child_path.decode('utf-8'))
645
if stat.S_ISDIR(mode):
646
todo.append((store, child_path, hexsha, file_id))
648
(child_path.decode('utf-8'), name.decode('utf-8'),
649
mode_kind(mode), None,
650
file_id, mode_kind(mode)))
651
yield (path_decoded, parent_id), children
654
def tree_delta_from_git_changes(changes, mapping,
655
fileid_maps, specific_files=None,
656
require_versioned=False, include_root=False,
658
"""Create a TreeDelta from two git trees.
660
source and target are iterators over tuples with:
661
(filename, sha, mode)
663
(old_fileid_map, new_fileid_map) = fileid_maps
664
if target_extras is None:
665
target_extras = set()
666
ret = delta.TreeDelta()
667
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
668
if newpath == b'' and not include_root:
671
oldpath_encoded = None
673
oldpath_decoded = oldpath.decode('utf-8')
675
newpath_decoded = None
677
newpath_decoded = newpath.decode('utf-8')
678
if not (specific_files is None or
679
(oldpath is not None and osutils.is_inside_or_parent_of_any(specific_files, oldpath_decoded)) or
680
(newpath is not None and osutils.is_inside_or_parent_of_any(specific_files, newpath_decoded))):
682
if mapping.is_special_file(oldpath):
684
if mapping.is_special_file(newpath):
686
if oldpath is None and newpath is None:
689
if newpath in target_extras:
690
ret.unversioned.append(
691
(osutils.normalized_filename(newpath)[0], None, mode_kind(newmode)))
693
file_id = new_fileid_map.lookup_file_id(newpath_decoded)
694
ret.added.append((newpath_decoded, file_id, mode_kind(newmode)))
695
elif newpath is None or newmode == 0:
696
file_id = old_fileid_map.lookup_file_id(oldpath_decoded)
697
ret.removed.append((oldpath_decoded, file_id, mode_kind(oldmode)))
698
elif oldpath != newpath:
699
file_id = old_fileid_map.lookup_file_id(oldpath_decoded)
701
(oldpath_decoded, newpath.decode('utf-8'), file_id,
702
mode_kind(newmode), (oldsha != newsha),
703
(oldmode != newmode)))
704
elif mode_kind(oldmode) != mode_kind(newmode):
705
file_id = new_fileid_map.lookup_file_id(newpath_decoded)
706
ret.kind_changed.append(
707
(newpath_decoded, file_id, mode_kind(oldmode),
709
elif oldsha != newsha or oldmode != newmode:
710
if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
712
file_id = new_fileid_map.lookup_file_id(newpath_decoded)
714
(newpath_decoded, file_id, mode_kind(newmode),
715
(oldsha != newsha), (oldmode != newmode)))
717
file_id = new_fileid_map.lookup_file_id(newpath_decoded)
718
ret.unchanged.append((newpath_decoded, file_id, mode_kind(newmode)))
723
def changes_from_git_changes(changes, mapping, specific_files=None, include_unchanged=False,
725
"""Create a iter_changes-like generator from a git stream.
727
source and target are iterators over tuples with:
728
(filename, sha, mode)
730
if target_extras is None:
731
target_extras = set()
732
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
733
if oldpath is not None:
734
oldpath_decoded = oldpath.decode('utf-8')
736
oldpath_decoded = None
737
if newpath is not None:
738
newpath_decoded = newpath.decode('utf-8')
740
newpath_decoded = None
741
if not (specific_files is None or
742
(oldpath_decoded is not None and osutils.is_inside_or_parent_of_any(specific_files, oldpath_decoded)) or
743
(newpath_decoded is not None and osutils.is_inside_or_parent_of_any(specific_files, newpath_decoded))):
745
if oldpath is not None and mapping.is_special_file(oldpath):
747
if newpath is not None and mapping.is_special_file(newpath):
749
if oldpath_decoded is None:
750
fileid = mapping.generate_file_id(newpath_decoded)
759
oldexe = mode_is_executable(oldmode)
760
oldkind = mode_kind(oldmode)
764
if oldpath_decoded == u'':
768
(oldparentpath, oldname) = osutils.split(oldpath_decoded)
769
oldparent = mapping.generate_file_id(oldparentpath)
770
fileid = mapping.generate_file_id(oldpath_decoded)
771
if newpath_decoded is None:
778
newversioned = (newpath_decoded not in target_extras)
780
newexe = mode_is_executable(newmode)
781
newkind = mode_kind(newmode)
785
if newpath_decoded == u'':
789
newparentpath, newname = osutils.split(newpath_decoded)
790
newparent = mapping.generate_file_id(newparentpath)
791
if (not include_unchanged and
792
oldkind == 'directory' and newkind == 'directory' and
793
oldpath_decoded == newpath_decoded):
795
yield (fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
796
(oldversioned, newversioned),
797
(oldparent, newparent), (oldname, newname),
798
(oldkind, newkind), (oldexe, newexe))
801
class InterGitTrees(_mod_tree.InterTree):
802
"""InterTree that works between two git trees."""
804
_matching_from_tree_format = None
805
_matching_to_tree_format = None
806
_test_mutable_trees_to_test_trees = None
809
def is_compatible(cls, source, target):
810
return (isinstance(source, GitRevisionTree) and
811
isinstance(target, GitRevisionTree))
813
def compare(self, want_unchanged=False, specific_files=None,
814
extra_trees=None, require_versioned=False, include_root=False,
815
want_unversioned=False):
816
with self.lock_read():
817
changes, target_extras = self._iter_git_changes(
818
want_unchanged=want_unchanged,
819
require_versioned=require_versioned,
820
specific_files=specific_files,
821
extra_trees=extra_trees,
822
want_unversioned=want_unversioned)
823
source_fileid_map = self.source._fileid_map
824
target_fileid_map = self.target._fileid_map
825
return tree_delta_from_git_changes(changes, self.target.mapping,
826
(source_fileid_map, target_fileid_map),
827
specific_files=specific_files, include_root=include_root,
828
target_extras=target_extras)
830
def iter_changes(self, include_unchanged=False, specific_files=None,
831
pb=None, extra_trees=[], require_versioned=True,
832
want_unversioned=False):
833
with self.lock_read():
834
changes, target_extras = self._iter_git_changes(
835
want_unchanged=include_unchanged,
836
require_versioned=require_versioned,
837
specific_files=specific_files,
838
extra_trees=extra_trees,
839
want_unversioned=want_unversioned)
840
return changes_from_git_changes(
841
changes, self.target.mapping,
842
specific_files=specific_files,
843
include_unchanged=include_unchanged,
844
target_extras=target_extras)
846
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
847
require_versioned=False, extra_trees=None,
848
want_unversioned=False):
849
raise NotImplementedError(self._iter_git_changes)
852
class InterGitRevisionTrees(InterGitTrees):
853
"""InterTree that works between two git revision trees."""
855
_matching_from_tree_format = None
856
_matching_to_tree_format = None
857
_test_mutable_trees_to_test_trees = None
860
def is_compatible(cls, source, target):
861
return (isinstance(source, GitRevisionTree) and
862
isinstance(target, GitRevisionTree))
864
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
865
require_versioned=True, extra_trees=None,
866
want_unversioned=False):
867
trees = [self.source]
868
if extra_trees is not None:
869
trees.extend(extra_trees)
870
if specific_files is not None:
871
specific_files = self.target.find_related_paths_across_trees(
872
specific_files, trees,
873
require_versioned=require_versioned)
875
if self.source._repository._git.object_store != self.target._repository._git.object_store:
876
store = OverlayObjectStore([self.source._repository._git.object_store,
877
self.target._repository._git.object_store])
879
store = self.source._repository._git.object_store
880
return self.source._repository._git.object_store.tree_changes(
881
self.source.tree, self.target.tree, want_unchanged=want_unchanged,
882
include_trees=True, change_type_same=True), set()
885
_mod_tree.InterTree.register_optimiser(InterGitRevisionTrees)
888
class MutableGitIndexTree(mutabletree.MutableTree):
891
self._lock_mode = None
893
self._versioned_dirs = None
894
self._index_dirty = False
896
def is_versioned(self, path):
897
with self.lock_read():
898
path = path.rstrip('/').encode('utf-8')
899
(index, subpath) = self._lookup_index(path)
900
return (subpath in index or self._has_dir(path))
902
def _has_dir(self, path):
903
if not isinstance(path, bytes):
904
raise TypeError(path)
907
if self._versioned_dirs is None:
909
return path in self._versioned_dirs
911
def _load_dirs(self):
912
if self._lock_mode is None:
913
raise errors.ObjectNotLocked(self)
914
self._versioned_dirs = set()
915
# TODO(jelmer): Browse over all indexes
916
for p, i in self._recurse_index_entries():
917
self._ensure_versioned_dir(posixpath.dirname(p))
919
def _ensure_versioned_dir(self, dirname):
920
if not isinstance(dirname, bytes):
921
raise TypeError(dirname)
922
if dirname in self._versioned_dirs:
925
self._ensure_versioned_dir(posixpath.dirname(dirname))
926
self._versioned_dirs.add(dirname)
928
def path2id(self, path):
929
with self.lock_read():
930
path = path.rstrip('/')
931
if self.is_versioned(path.rstrip('/')):
932
return self._fileid_map.lookup_file_id(osutils.safe_unicode(path))
935
def has_id(self, file_id):
937
self.id2path(file_id)
938
except errors.NoSuchId:
943
def id2path(self, file_id):
946
if type(file_id) is not bytes:
947
raise TypeError(file_id)
948
with self.lock_read():
950
path = self._fileid_map.lookup_path(file_id)
952
raise errors.NoSuchId(self, file_id)
953
if self.is_versioned(path):
955
raise errors.NoSuchId(self, file_id)
957
def _set_root_id(self, file_id):
958
self._fileid_map.set_file_id("", file_id)
960
def get_root_id(self):
961
return self.path2id(u"")
963
def _add(self, files, ids, kinds):
964
for (path, file_id, kind) in zip(files, ids, kinds):
965
if file_id is not None:
966
raise workingtree.SettingFileIdUnsupported()
967
path, can_access = osutils.normalized_filename(path)
969
raise errors.InvalidNormalization(path)
970
self._index_add_entry(path, kind)
972
def _read_submodule_head(self, path):
973
raise NotImplementedError(self._read_submodule_head)
975
def _lookup_index(self, encoded_path):
976
if not isinstance(encoded_path, bytes):
977
raise TypeError(encoded_path)
978
# TODO(jelmer): Look in other indexes
979
return self.index, encoded_path
981
def _index_del_entry(self, index, path):
983
# TODO(jelmer): Keep track of dirty per index
984
self._index_dirty = True
986
def _index_add_entry(self, path, kind, flags=0, reference_revision=None):
987
if kind == "directory":
988
# Git indexes don't contain directories
993
file, stat_val = self.get_file_with_stat(path)
994
except (errors.NoSuchFile, IOError):
995
# TODO: Rather than come up with something here, use the old index
997
stat_val = os.stat_result(
998
(stat.S_IFREG | 0o644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1000
blob.set_raw_string(file.read())
1001
# Add object to the repository if it didn't exist yet
1002
if not blob.id in self.store:
1003
self.store.add_object(blob)
1005
elif kind == "symlink":
1008
stat_val = self._lstat(path)
1009
except EnvironmentError:
1010
# TODO: Rather than come up with something here, use the
1012
stat_val = os.stat_result(
1013
(stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1014
blob.set_raw_string(
1015
self.get_symlink_target(path).encode("utf-8"))
1016
# Add object to the repository if it didn't exist yet
1017
if not blob.id in self.store:
1018
self.store.add_object(blob)
1020
elif kind == "tree-reference":
1021
if reference_revision is not None:
1022
hexsha = self.branch.lookup_bzr_revision_id(reference_revision)[0]
1024
hexsha = self._read_submodule_head(path)
1026
raise errors.NoCommits(path)
1028
stat_val = self._lstat(path)
1029
except EnvironmentError:
1030
stat_val = os.stat_result(
1031
(S_IFGITLINK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1032
stat_val = os.stat_result((S_IFGITLINK, ) + stat_val[1:])
1034
raise AssertionError("unknown kind '%s'" % kind)
1035
# Add an entry to the index or update the existing entry
1036
ensure_normalized_path(path)
1037
encoded_path = path.encode("utf-8")
1038
if b'\r' in encoded_path or b'\n' in encoded_path:
1039
# TODO(jelmer): Why do we need to do this?
1040
trace.mutter('ignoring path with invalid newline in it: %r', path)
1042
(index, index_path) = self._lookup_index(encoded_path)
1043
index[index_path] = index_entry_from_stat(stat_val, hexsha, flags)
1044
self._index_dirty = True
1045
if self._versioned_dirs is not None:
1046
self._ensure_versioned_dir(index_path)
1048
def _recurse_index_entries(self, index=None, basepath=b""):
1049
# Iterate over all index entries
1050
with self.lock_read():
1053
for path, value in index.items():
1054
yield (posixpath.join(basepath, path), value)
1055
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
1056
if S_ISGITLINK(mode):
1057
pass # TODO(jelmer): dive into submodule
1060
def iter_entries_by_dir(self, specific_files=None, yield_parents=False):
1062
raise NotImplementedError(self.iter_entries_by_dir)
1063
with self.lock_read():
1064
if specific_files is not None:
1065
specific_files = set(specific_files)
1067
specific_files = None
1068
root_ie = self._get_dir_ie(u"", None)
1070
if specific_files is None or u"" in specific_files:
1071
ret[(u"", u"")] = root_ie
1072
dir_ids = {u"": root_ie.file_id}
1073
for path, value in self._recurse_index_entries():
1074
if self.mapping.is_special_file(path):
1076
path = path.decode("utf-8")
1077
if specific_files is not None and not path in specific_files:
1079
(parent, name) = posixpath.split(path)
1081
file_ie = self._get_file_ie(name, path, value, None)
1082
except errors.NoSuchFile:
1084
if yield_parents or specific_files is None:
1085
for (dir_path, dir_ie) in self._add_missing_parent_ids(parent,
1087
ret[(posixpath.dirname(dir_path), dir_path)] = dir_ie
1088
file_ie.parent_id = self.path2id(parent)
1089
ret[(posixpath.dirname(path), path)] = file_ie
1090
return ((path, ie) for ((_, path), ie) in sorted(viewitems(ret)))
1092
def iter_references(self):
1093
# TODO(jelmer): Implement a more efficient version of this
1094
for path, entry in self.iter_entries_by_dir():
1095
if entry.kind == 'tree-reference':
1096
yield path, self.mapping.generate_file_id(b'')
1098
def _get_dir_ie(self, path, parent_id):
1099
file_id = self.path2id(path)
1100
return GitTreeDirectory(file_id,
1101
posixpath.basename(path).strip("/"), parent_id)
1103
def _get_file_ie(self, name, path, value, parent_id):
1104
if not isinstance(name, text_type):
1105
raise TypeError(name)
1106
if not isinstance(path, text_type):
1107
raise TypeError(path)
1108
if not isinstance(value, tuple) or len(value) != 10:
1109
raise TypeError(value)
1110
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
1111
file_id = self.path2id(path)
1112
if not isinstance(file_id, bytes):
1113
raise TypeError(file_id)
1114
kind = mode_kind(mode)
1115
ie = entry_factory[kind](file_id, name, parent_id)
1116
if kind == 'symlink':
1117
ie.symlink_target = self.get_symlink_target(path, file_id)
1118
elif kind == 'tree-reference':
1119
ie.reference_revision = self.get_reference_revision(path, file_id)
1122
data = self.get_file_text(path, file_id)
1123
except errors.NoSuchFile:
1125
except IOError as e:
1126
if e.errno != errno.ENOENT:
1130
data = self.branch.repository._git.object_store[sha].data
1131
ie.text_sha1 = osutils.sha_string(data)
1132
ie.text_size = len(data)
1133
ie.executable = bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
1136
def _add_missing_parent_ids(self, path, dir_ids):
1139
parent = posixpath.dirname(path).strip("/")
1140
ret = self._add_missing_parent_ids(parent, dir_ids)
1141
parent_id = dir_ids[parent]
1142
ie = self._get_dir_ie(path, parent_id)
1143
dir_ids[path] = ie.file_id
1144
ret.append((path, ie))
1147
def _comparison_data(self, entry, path):
1149
return None, False, None
1150
return entry.kind, entry.executable, None
1152
def _unversion_path(self, path):
1153
if self._lock_mode is None:
1154
raise errors.ObjectNotLocked(self)
1155
encoded_path = path.encode("utf-8")
1157
(index, subpath) = self._lookup_index(encoded_path)
1159
self._index_del_entry(index, encoded_path)
1161
# A directory, perhaps?
1162
# TODO(jelmer): Deletes that involve submodules?
1163
for p in list(index):
1164
if p.startswith(subpath+b"/"):
1166
self._index_del_entry(index, p)
1169
self._versioned_dirs = None
1172
def unversion(self, paths, file_ids=None):
1173
with self.lock_tree_write():
1175
if self._unversion_path(path) == 0:
1176
raise errors.NoSuchFile(path)
1177
self._versioned_dirs = None
1183
def update_basis_by_delta(self, revid, delta):
1184
# TODO(jelmer): This shouldn't be called, it's inventory specific.
1185
for (old_path, new_path, file_id, ie) in delta:
1186
if old_path is not None:
1187
(index, old_subpath) = self._lookup_index(old_path.encode('utf-8'))
1188
if old_subpath in index:
1189
self._index_del_entry(index, old_subpath)
1190
self._versioned_dirs = None
1191
if new_path is not None and ie.kind != 'directory':
1192
self._index_add_entry(new_path, ie.kind)
1194
self._set_merges_from_parent_ids([])
1196
def move(self, from_paths, to_dir=None, after=None):
1198
with self.lock_tree_write():
1199
to_abs = self.abspath(to_dir)
1200
if not os.path.isdir(to_abs):
1201
raise errors.BzrMoveFailedError('', to_dir,
1202
errors.NotADirectory(to_abs))
1204
for from_rel in from_paths:
1205
from_tail = os.path.split(from_rel)[-1]
1206
to_rel = os.path.join(to_dir, from_tail)
1207
self.rename_one(from_rel, to_rel, after=after)
1208
rename_tuples.append((from_rel, to_rel))
1210
return rename_tuples
1212
def rename_one(self, from_rel, to_rel, after=None):
1213
from_path = from_rel.encode("utf-8")
1214
to_rel, can_access = osutils.normalized_filename(to_rel)
1216
raise errors.InvalidNormalization(to_rel)
1217
to_path = to_rel.encode("utf-8")
1218
with self.lock_tree_write():
1220
# Perhaps it's already moved?
1222
not self.has_filename(from_rel) and
1223
self.has_filename(to_rel) and
1224
not self.is_versioned(to_rel))
1226
if not self.has_filename(to_rel):
1227
raise errors.BzrMoveFailedError(from_rel, to_rel,
1228
errors.NoSuchFile(to_rel))
1229
if self.basis_tree().is_versioned(to_rel):
1230
raise errors.BzrMoveFailedError(from_rel, to_rel,
1231
errors.AlreadyVersionedError(to_rel))
1233
kind = self.kind(to_rel)
1236
to_kind = self.kind(to_rel)
1237
except errors.NoSuchFile:
1238
exc_type = errors.BzrRenameFailedError
1241
exc_type = errors.BzrMoveFailedError
1242
if self.is_versioned(to_rel):
1243
raise exc_type(from_rel, to_rel,
1244
errors.AlreadyVersionedError(to_rel))
1245
if not self.has_filename(from_rel):
1246
raise errors.BzrMoveFailedError(from_rel, to_rel,
1247
errors.NoSuchFile(from_rel))
1248
kind = self.kind(from_rel)
1249
if not self.is_versioned(from_rel) and kind != 'directory':
1250
raise exc_type(from_rel, to_rel,
1251
errors.NotVersionedError(from_rel))
1252
if self.has_filename(to_rel):
1253
raise errors.RenameFailedFilesExist(
1254
from_rel, to_rel, errors.FileExists(to_rel))
1256
kind = self.kind(from_rel)
1258
if not after and kind != 'directory':
1259
(index, from_subpath) = self._lookup_index(from_path)
1260
if from_subpath not in index:
1262
raise errors.BzrMoveFailedError(from_rel, to_rel,
1263
errors.NotVersionedError(path=from_rel))
1267
self._rename_one(from_rel, to_rel)
1268
except OSError as e:
1269
if e.errno == errno.ENOENT:
1270
raise errors.BzrMoveFailedError(from_rel, to_rel,
1271
errors.NoSuchFile(to_rel))
1273
if kind != 'directory':
1274
(index, from_index_path) = self._lookup_index(from_path)
1276
self._index_del_entry(index, from_path)
1279
self._index_add_entry(to_rel, kind)
1281
todo = [(p, i) for (p, i) in self._recurse_index_entries() if p.startswith(from_path+b'/')]
1282
for child_path, child_value in todo:
1283
(child_to_index, child_to_index_path) = self._lookup_index(
1284
posixpath.join(to_path, posixpath.relpath(child_path, from_path)))
1285
child_to_index[child_to_index_path] = child_value
1286
# TODO(jelmer): Mark individual index as dirty
1287
self._index_dirty = True
1288
(child_from_index, child_from_index_path) = self._lookup_index(child_path)
1289
self._index_del_entry(child_from_index, child_from_index_path)
1291
self._versioned_dirs = None
1294
def find_related_paths_across_trees(self, paths, trees=[],
1295
require_versioned=True):
1299
if require_versioned:
1300
trees = [self] + (trees if trees is not None else [])
1304
if t.is_versioned(p):
1309
raise errors.PathsNotVersionedError(unversioned)
1311
return filter(self.is_versioned, paths)
1313
def path_content_summary(self, path):
1314
"""See Tree.path_content_summary."""
1316
stat_result = self._lstat(path)
1317
except OSError as e:
1318
if getattr(e, 'errno', None) == errno.ENOENT:
1320
return ('missing', None, None, None)
1321
# propagate other errors
1323
kind = mode_kind(stat_result.st_mode)
1325
return self._file_content_summary(path, stat_result)
1326
elif kind == 'directory':
1327
# perhaps it looks like a plain directory, but it's really a
1329
if self._directory_is_tree_reference(path):
1330
kind = 'tree-reference'
1331
return kind, None, None, None
1332
elif kind == 'symlink':
1333
target = osutils.readlink(self.abspath(path))
1334
return ('symlink', None, None, target)
1336
return (kind, None, None, None)
1338
def kind(self, relpath, file_id=None):
1339
kind = osutils.file_kind(self.abspath(relpath))
1340
if kind == 'directory':
1341
(index, index_path) = self._lookup_index(relpath.encode('utf-8'))
1345
mode = index[index_path].mode
1349
if S_ISGITLINK(mode):
1350
return 'tree-reference'
1355
def _live_entry(self, relpath):
1356
raise NotImplementedError(self._live_entry)
1359
class InterIndexGitTree(InterGitTrees):
1360
"""InterTree that works between a Git revision tree and an index."""
1362
def __init__(self, source, target):
1363
super(InterIndexGitTree, self).__init__(source, target)
1364
self._index = target.index
1367
def is_compatible(cls, source, target):
1368
return (isinstance(source, GitRevisionTree) and
1369
isinstance(target, MutableGitIndexTree))
1371
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1372
require_versioned=False, extra_trees=None,
1373
want_unversioned=False):
1374
trees = [self.source]
1375
if extra_trees is not None:
1376
trees.extend(extra_trees)
1377
if specific_files is not None:
1378
specific_files = self.target.find_related_paths_across_trees(
1379
specific_files, trees,
1380
require_versioned=require_versioned)
1381
# TODO(jelmer): Restrict to specific_files, for performance reasons.
1382
with self.lock_read():
1383
return changes_between_git_tree_and_working_copy(
1384
self.source.store, self.source.tree,
1385
self.target, want_unchanged=want_unchanged,
1386
want_unversioned=want_unversioned)
1389
_mod_tree.InterTree.register_optimiser(InterIndexGitTree)
1392
def changes_between_git_tree_and_working_copy(store, from_tree_sha, target,
1393
want_unchanged=False, want_unversioned=False):
1394
"""Determine the changes between a git tree and a working tree with index.
1399
# Report dirified directories to commit_tree first, so that they can be
1400
# replaced with non-empty directories if they have contents.
1402
for path, index_entry in target._recurse_index_entries():
1404
live_entry = target._live_entry(path)
1405
except EnvironmentError as e:
1406
if e.errno == errno.ENOENT:
1407
# Entry was removed; keep it listed, but mark it as gone.
1408
blobs[path] = (ZERO_SHA, 0)
1409
elif e.errno == errno.EISDIR:
1410
# Entry was turned into a directory
1411
dirified.append((path, Tree().id, stat.S_IFDIR))
1412
store.add_object(Tree())
1416
blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
1417
if want_unversioned:
1418
for e in target.extras():
1419
st = target._lstat(e)
1421
np, accessible = osutils.normalized_filename(e)
1422
except UnicodeDecodeError:
1423
raise errors.BadFilenameEncoding(
1425
if stat.S_ISDIR(st.st_mode):
1428
blob = blob_from_path_and_stat(target.abspath(e).encode(osutils._fs_enc), st)
1429
store.add_object(blob)
1430
np = np.encode('utf-8')
1431
blobs[np] = (blob.id, cleanup_mode(st.st_mode))
1433
to_tree_sha = commit_tree(store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()])
1434
return store.tree_changes(
1435
from_tree_sha, to_tree_sha, include_trees=True,
1436
want_unchanged=want_unchanged, change_type_same=True), extras