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 collections import deque
22
from io import BytesIO
25
from dulwich.config import (
27
ConfigFile as GitConfigFile,
29
from dulwich.diff_tree import tree_changes
30
from dulwich.errors import NotTreeError
31
from dulwich.index import (
32
blob_from_path_and_stat,
35
index_entry_from_stat,
38
from dulwich.object_store import (
42
from dulwich.objects import (
53
controldir as _mod_controldir,
63
from ..revision import (
68
from .mapping import (
73
from .transportgit import (
79
class GitTreeDirectory(_mod_tree.TreeDirectory):
81
__slots__ = ['file_id', 'name', 'parent_id', 'children']
83
def __init__(self, file_id, name, parent_id):
84
self.file_id = file_id
86
self.parent_id = parent_id
99
return self.__class__(
100
self.file_id, self.name, self.parent_id)
103
return "%s(file_id=%r, name=%r, parent_id=%r)" % (
104
self.__class__.__name__, self.file_id, self.name,
107
def __eq__(self, other):
108
return (self.kind == other.kind and
109
self.file_id == other.file_id and
110
self.name == other.name and
111
self.parent_id == other.parent_id)
114
class GitTreeFile(_mod_tree.TreeFile):
116
__slots__ = ['file_id', 'name', 'parent_id', 'text_size', 'text_sha1',
119
def __init__(self, file_id, name, parent_id, text_size=None,
120
text_sha1=None, executable=None):
121
self.file_id = file_id
123
self.parent_id = parent_id
124
self.text_size = text_size
125
self.text_sha1 = text_sha1
126
self.executable = executable
132
def __eq__(self, other):
133
return (self.kind == other.kind and
134
self.file_id == other.file_id and
135
self.name == other.name and
136
self.parent_id == other.parent_id and
137
self.text_sha1 == other.text_sha1 and
138
self.text_size == other.text_size and
139
self.executable == other.executable)
142
return ("%s(file_id=%r, name=%r, parent_id=%r, text_size=%r, "
143
"text_sha1=%r, executable=%r)") % (
144
type(self).__name__, self.file_id, self.name, self.parent_id,
145
self.text_size, self.text_sha1, self.executable)
148
ret = self.__class__(
149
self.file_id, self.name, self.parent_id)
150
ret.text_sha1 = self.text_sha1
151
ret.text_size = self.text_size
152
ret.executable = self.executable
156
class GitTreeSymlink(_mod_tree.TreeLink):
158
__slots__ = ['file_id', 'name', 'parent_id', 'symlink_target']
160
def __init__(self, file_id, name, parent_id,
161
symlink_target=None):
162
self.file_id = file_id
164
self.parent_id = parent_id
165
self.symlink_target = symlink_target
172
def executable(self):
180
return "%s(file_id=%r, name=%r, parent_id=%r, symlink_target=%r)" % (
181
type(self).__name__, self.file_id, self.name, self.parent_id,
184
def __eq__(self, other):
185
return (self.kind == other.kind and
186
self.file_id == other.file_id and
187
self.name == other.name and
188
self.parent_id == other.parent_id and
189
self.symlink_target == other.symlink_target)
192
return self.__class__(
193
self.file_id, self.name, self.parent_id,
197
class GitTreeSubmodule(_mod_tree.TreeReference):
199
__slots__ = ['file_id', 'name', 'parent_id', 'reference_revision']
201
def __init__(self, file_id, name, parent_id, reference_revision=None):
202
self.file_id = file_id
204
self.parent_id = parent_id
205
self.reference_revision = reference_revision
208
def executable(self):
213
return 'tree-reference'
216
return ("%s(file_id=%r, name=%r, parent_id=%r, "
217
"reference_revision=%r)") % (
218
type(self).__name__, self.file_id, self.name, self.parent_id,
219
self.reference_revision)
221
def __eq__(self, other):
222
return (self.kind == other.kind and
223
self.file_id == other.file_id and
224
self.name == other.name and
225
self.parent_id == other.parent_id and
226
self.reference_revision == other.reference_revision)
229
return self.__class__(
230
self.file_id, self.name, self.parent_id,
231
self.reference_revision)
235
'directory': GitTreeDirectory,
237
'symlink': GitTreeSymlink,
238
'tree-reference': GitTreeSubmodule,
242
def ensure_normalized_path(path):
243
"""Check whether path is normalized.
245
:raises InvalidNormalization: When path is not normalized, and cannot be
246
accessed on this platform by the normalized path.
247
:return: The NFC normalised version of path.
249
norm_path, can_access = osutils.normalized_filename(path)
250
if norm_path != path:
254
raise errors.InvalidNormalization(path)
258
class GitRevisionTree(revisiontree.RevisionTree):
259
"""Revision tree implementation based on Git objects."""
261
def __init__(self, repository, revision_id):
262
self._revision_id = revision_id
263
self._repository = repository
264
self._submodules = None
265
self.store = repository._git.object_store
266
if not isinstance(revision_id, bytes):
267
raise TypeError(revision_id)
268
self.commit_id, self.mapping = repository.lookup_bzr_revision_id(
270
if revision_id == NULL_REVISION:
272
self.mapping = default_mapping
275
commit = self.store[self.commit_id]
277
raise errors.NoSuchRevision(repository, revision_id)
278
self.tree = commit.tree
280
def _submodule_info(self):
281
if self._submodules is None:
283
with self.get_file('.gitmodules') as f:
284
config = GitConfigFile.from_file(f)
287
for path, url, section in parse_submodules(config)}
288
except errors.NoSuchFile:
289
self._submodules = {}
290
return self._submodules
292
def _get_submodule_repository(self, relpath):
293
if not isinstance(relpath, bytes):
294
raise TypeError(relpath)
296
info = self._submodule_info()[relpath]
298
nested_repo_transport = self._repository.controldir.user_transport.clone(
299
relpath.decode('utf-8'))
301
nested_repo_transport = self._repository.controldir.control_transport.clone(
302
posixpath.join('modules', info[1].decode('utf-8')))
303
nested_controldir = _mod_controldir.ControlDir.open_from_transport(
304
nested_repo_transport)
305
return nested_controldir.find_repository()
307
def _get_submodule_store(self, relpath):
308
return self._get_submodule_repository(relpath)._git.object_store
310
def get_nested_tree(self, path):
311
encoded_path = path.encode('utf-8')
312
nested_repo = self._get_submodule_repository(encoded_path)
313
ref_rev = self.get_reference_revision(path)
314
return nested_repo.revision_tree(ref_rev)
316
def supports_rename_tracking(self):
319
def get_file_revision(self, path):
320
change_scanner = self._repository._file_change_scanner
321
if self.commit_id == ZERO_SHA:
323
(unused_path, commit_id) = change_scanner.find_last_change_revision(
324
path.encode('utf-8'), self.commit_id)
325
return self._repository.lookup_foreign_revision_id(
326
commit_id, self.mapping)
328
def get_file_mtime(self, path):
330
revid = self.get_file_revision(path)
332
raise errors.NoSuchFile(path)
334
rev = self._repository.get_revision(revid)
335
except errors.NoSuchRevision:
336
raise _mod_tree.FileTimestampUnavailable(path)
339
def id2path(self, file_id, recurse='down'):
341
path = self.mapping.parse_file_id(file_id)
343
raise errors.NoSuchId(self, file_id)
344
if self.is_versioned(path):
346
raise errors.NoSuchId(self, file_id)
348
def is_versioned(self, path):
349
return self.has_filename(path)
351
def path2id(self, path):
352
if self.mapping.is_special_file(path):
354
if not self.is_versioned(path):
356
return self.mapping.generate_file_id(osutils.safe_unicode(path))
358
def all_file_ids(self):
359
raise errors.UnsupportedOperation(self.all_file_ids, self)
361
def all_versioned_paths(self):
363
todo = [(self.store, b'', self.tree)]
365
(store, path, tree_id) = todo.pop()
368
tree = store[tree_id]
369
for name, mode, hexsha in tree.items():
370
subpath = posixpath.join(path, name)
371
ret.add(subpath.decode('utf-8'))
372
if stat.S_ISDIR(mode):
373
todo.append((store, subpath, hexsha))
376
def _lookup_path(self, path):
377
if self.tree is None:
378
raise errors.NoSuchFile(path)
380
encoded_path = path.encode('utf-8')
381
parts = encoded_path.split(b'/')
385
for i, p in enumerate(parts):
389
if not isinstance(obj, Tree):
390
raise NotTreeError(hexsha)
392
mode, hexsha = obj[p]
394
raise errors.NoSuchFile(path)
395
if S_ISGITLINK(mode) and i != len(parts) - 1:
396
store = self._get_submodule_store(b'/'.join(parts[:i + 1]))
397
hexsha = store[hexsha].tree
398
return (store, mode, hexsha)
400
def is_executable(self, path):
401
(store, mode, hexsha) = self._lookup_path(path)
403
# the tree root is a directory
405
return mode_is_executable(mode)
407
def kind(self, path):
408
(store, mode, hexsha) = self._lookup_path(path)
410
# the tree root is a directory
412
return mode_kind(mode)
414
def has_filename(self, path):
416
self._lookup_path(path)
417
except errors.NoSuchFile:
422
def _submodule_info(self):
423
if self._submodules is None:
425
with self.get_file('.gitmodules') as f:
426
config = GitConfigFile.from_file(f)
429
for path, url, section in parse_submodules(config)}
430
except errors.NoSuchFile:
431
self._submodules = {}
432
return self._submodules
434
def list_files(self, include_root=False, from_dir=None, recursive=True,
435
recurse_nested=False):
436
if self.tree is None:
438
if from_dir is None or from_dir == '.':
440
(store, mode, hexsha) = self._lookup_path(from_dir)
441
if mode is None: # Root
442
root_ie = self._get_dir_ie(b"", None)
444
parent_path = posixpath.dirname(from_dir)
445
parent_id = self.mapping.generate_file_id(parent_path)
446
if mode_kind(mode) == 'directory':
447
root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
449
root_ie = self._get_file_ie(
450
store, from_dir.encode("utf-8"),
451
posixpath.basename(from_dir), mode, hexsha)
453
yield (from_dir, "V", root_ie.kind, root_ie)
455
if root_ie.kind == 'directory':
456
todo.append((store, from_dir.encode("utf-8"),
457
b"", hexsha, root_ie.file_id))
459
(store, path, relpath, hexsha, parent_id) = todo.pop()
461
for name, mode, hexsha in tree.iteritems():
462
if self.mapping.is_special_file(name):
464
child_path = posixpath.join(path, name)
465
child_relpath = posixpath.join(relpath, name)
466
if S_ISGITLINK(mode) and recurse_nested:
468
store = self._get_submodule_store(child_relpath)
469
hexsha = store[hexsha].tree
470
if stat.S_ISDIR(mode):
471
ie = self._get_dir_ie(child_path, parent_id)
474
(store, child_path, child_relpath, hexsha,
477
ie = self._get_file_ie(
478
store, child_path, name, mode, hexsha, parent_id)
479
yield (child_relpath.decode('utf-8'), "V", ie.kind, ie)
481
def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
482
if not isinstance(path, bytes):
483
raise TypeError(path)
484
if not isinstance(name, bytes):
485
raise TypeError(name)
486
kind = mode_kind(mode)
487
path = path.decode('utf-8')
488
name = name.decode("utf-8")
489
file_id = self.mapping.generate_file_id(path)
490
ie = entry_factory[kind](file_id, name, parent_id)
491
if kind == 'symlink':
492
ie.symlink_target = store[hexsha].data.decode('utf-8')
493
elif kind == 'tree-reference':
494
ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(
497
data = store[hexsha].data
498
ie.text_sha1 = osutils.sha_string(data)
499
ie.text_size = len(data)
500
ie.executable = mode_is_executable(mode)
503
def _get_dir_ie(self, path, parent_id):
504
path = path.decode('utf-8')
505
file_id = self.mapping.generate_file_id(path)
506
return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
508
def iter_child_entries(self, path):
509
(store, mode, tree_sha) = self._lookup_path(path)
511
if mode is not None and not stat.S_ISDIR(mode):
514
encoded_path = path.encode('utf-8')
515
file_id = self.path2id(path)
516
tree = store[tree_sha]
517
for name, mode, hexsha in tree.iteritems():
518
if self.mapping.is_special_file(name):
520
child_path = posixpath.join(encoded_path, name)
521
if stat.S_ISDIR(mode):
522
yield self._get_dir_ie(child_path, file_id)
524
yield self._get_file_ie(store, child_path, name, mode, hexsha,
527
def iter_entries_by_dir(self, specific_files=None,
528
recurse_nested=False):
529
if self.tree is None:
531
if specific_files is not None:
532
if specific_files in ([""], []):
533
specific_files = None
535
specific_files = set([p.encode('utf-8')
536
for p in specific_files])
537
todo = deque([(self.store, b"", self.tree, self.path2id(''))])
538
if specific_files is None or u"" in specific_files:
539
yield u"", self._get_dir_ie(b"", None)
541
store, path, tree_sha, parent_id = todo.popleft()
542
tree = store[tree_sha]
544
for name, mode, hexsha in tree.iteritems():
545
if self.mapping.is_special_file(name):
547
child_path = posixpath.join(path, name)
548
child_path_decoded = child_path.decode('utf-8')
549
if recurse_nested and S_ISGITLINK(mode):
551
store = self._get_submodule_store(child_path)
552
hexsha = store[hexsha].tree
553
if stat.S_ISDIR(mode):
554
if (specific_files is None or
555
any([p for p in specific_files if p.startswith(
558
(store, child_path, hexsha,
559
self.path2id(child_path_decoded)))
560
if specific_files is None or child_path in specific_files:
561
if stat.S_ISDIR(mode):
562
yield (child_path_decoded,
563
self._get_dir_ie(child_path, parent_id))
565
yield (child_path_decoded,
566
self._get_file_ie(store, child_path, name, mode,
568
todo.extendleft(reversed(extradirs))
570
def iter_references(self):
571
if self.supports_tree_reference():
572
for path, entry in self.iter_entries_by_dir():
573
if entry.kind == 'tree-reference':
576
def get_revision_id(self):
577
"""See RevisionTree.get_revision_id."""
578
return self._revision_id
580
def get_file_sha1(self, path, stat_value=None):
581
if self.tree is None:
582
raise errors.NoSuchFile(path)
583
return osutils.sha_string(self.get_file_text(path))
585
def get_file_verifier(self, path, stat_value=None):
586
(store, mode, hexsha) = self._lookup_path(path)
587
return ("GIT", hexsha)
589
def get_file_size(self, path):
590
(store, mode, hexsha) = self._lookup_path(path)
591
if stat.S_ISREG(mode):
592
return len(store[hexsha].data)
595
def get_file_text(self, path):
596
"""See RevisionTree.get_file_text."""
597
(store, mode, hexsha) = self._lookup_path(path)
598
if stat.S_ISREG(mode):
599
return store[hexsha].data
603
def get_symlink_target(self, path):
604
"""See RevisionTree.get_symlink_target."""
605
(store, mode, hexsha) = self._lookup_path(path)
606
if stat.S_ISLNK(mode):
607
return store[hexsha].data.decode('utf-8')
611
def get_reference_revision(self, path):
612
"""See RevisionTree.get_symlink_target."""
613
(store, mode, hexsha) = self._lookup_path(path)
614
if S_ISGITLINK(mode):
616
nested_repo = self._get_submodule_repository(path.encode('utf-8'))
617
except errors.NotBranchError:
618
return self.mapping.revision_id_foreign_to_bzr(hexsha)
620
return nested_repo.lookup_foreign_revision_id(hexsha)
624
def _comparison_data(self, entry, path):
626
return None, False, None
627
return entry.kind, entry.executable, None
629
def path_content_summary(self, path):
630
"""See Tree.path_content_summary."""
632
(store, mode, hexsha) = self._lookup_path(path)
633
except errors.NoSuchFile:
634
return ('missing', None, None, None)
635
kind = mode_kind(mode)
637
executable = mode_is_executable(mode)
638
contents = store[hexsha].data
639
return (kind, len(contents), executable,
640
osutils.sha_string(contents))
641
elif kind == 'symlink':
642
return (kind, None, None, store[hexsha].data.decode('utf-8'))
643
elif kind == 'tree-reference':
644
nested_repo = self._get_submodule_repository(path.encode('utf-8'))
645
return (kind, None, None,
646
nested_repo.lookup_foreign_revision_id(hexsha))
648
return (kind, None, None, None)
650
def find_related_paths_across_trees(self, paths, trees=[],
651
require_versioned=True):
654
if require_versioned:
655
trees = [self] + (trees if trees is not None else [])
659
if t.is_versioned(p):
664
raise errors.PathsNotVersionedError(unversioned)
665
return filter(self.is_versioned, paths)
667
def _iter_tree_contents(self, include_trees=False):
668
if self.tree is None:
670
return self.store.iter_tree_contents(
671
self.tree, include_trees=include_trees)
673
def annotate_iter(self, path, default_revision=CURRENT_REVISION):
674
"""Return an iterator of revision_id, line tuples.
676
For working trees (and mutable trees in general), the special
677
revision_id 'current:' will be used for lines that are new in this
678
tree, e.g. uncommitted changes.
679
:param default_revision: For lines that don't match a basis, mark them
680
with this revision id. Not all implementations will make use of
683
with self.lock_read():
684
# Now we have the parents of this content
685
from breezy.annotate import Annotator
686
from .annotate import AnnotateProvider
687
annotator = Annotator(AnnotateProvider(
688
self._repository._file_change_scanner))
689
this_key = (path, self.get_file_revision(path))
690
annotations = [(key[-1], line)
691
for key, line in annotator.annotate_flat(this_key)]
694
def _get_rules_searcher(self, default_searcher):
695
return default_searcher
697
def walkdirs(self, prefix=u""):
698
(store, mode, hexsha) = self._lookup_path(prefix)
700
[(store, prefix.encode('utf-8'), hexsha, self.path2id(prefix))])
702
store, path, tree_sha, parent_id = todo.popleft()
703
path_decoded = path.decode('utf-8')
704
tree = store[tree_sha]
706
for name, mode, hexsha in tree.iteritems():
707
if self.mapping.is_special_file(name):
709
child_path = posixpath.join(path, name)
710
file_id = self.path2id(child_path.decode('utf-8'))
711
if stat.S_ISDIR(mode):
712
todo.append((store, child_path, hexsha, file_id))
714
(child_path.decode('utf-8'), name.decode('utf-8'),
715
mode_kind(mode), None,
716
file_id, mode_kind(mode)))
717
yield (path_decoded, parent_id), children
720
def tree_delta_from_git_changes(changes, mappings,
722
require_versioned=False, include_root=False,
724
"""Create a TreeDelta from two git trees.
726
source and target are iterators over tuples with:
727
(filename, sha, mode)
729
(old_mapping, new_mapping) = mappings
730
if target_extras is None:
731
target_extras = set()
732
ret = delta.TreeDelta()
734
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
735
if newpath == b'' and not include_root:
737
if oldpath is not None:
738
oldpath_decoded = oldpath.decode('utf-8')
740
oldpath_decoded = None
741
if newpath is not None:
742
newpath_decoded = newpath.decode('utf-8')
744
newpath_decoded = None
745
if not (specific_files is None or
746
(oldpath is not None and
747
osutils.is_inside_or_parent_of_any(
748
specific_files, oldpath_decoded)) or
749
(newpath is not None and
750
osutils.is_inside_or_parent_of_any(
751
specific_files, newpath_decoded))):
754
if oldpath_decoded is None:
755
fileid = new_mapping.generate_file_id(newpath_decoded)
764
oldexe = mode_is_executable(oldmode)
765
oldkind = mode_kind(oldmode)
769
if oldpath_decoded == u'':
773
(oldparentpath, oldname) = osutils.split(oldpath_decoded)
774
oldparent = old_mapping.generate_file_id(oldparentpath)
775
fileid = old_mapping.generate_file_id(oldpath_decoded)
776
if newpath_decoded is None:
783
newversioned = (newpath_decoded not in target_extras)
785
newexe = mode_is_executable(newmode)
786
newkind = mode_kind(newmode)
790
if newpath_decoded == u'':
794
newparentpath, newname = osutils.split(newpath_decoded)
795
newparent = new_mapping.generate_file_id(newparentpath)
796
if old_mapping.is_special_file(oldpath):
798
if new_mapping.is_special_file(newpath):
800
if oldpath is None and newpath is None:
802
change = _mod_tree.TreeChange(
803
fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
804
(oldversioned, newversioned),
805
(oldparent, newparent), (oldname, newname),
806
(oldkind, newkind), (oldexe, newexe))
808
added.append((newpath, newkind))
809
elif newpath is None or newmode == 0:
810
ret.removed.append(change)
811
elif oldpath != newpath:
812
ret.renamed.append(change)
813
elif mode_kind(oldmode) != mode_kind(newmode):
814
ret.kind_changed.append(change)
815
elif oldsha != newsha or oldmode != newmode:
816
if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
818
ret.modified.append(change)
820
ret.unchanged.append(change)
822
implicit_dirs = {b''}
823
for path, kind in added:
824
if kind == 'directory' or path in target_extras:
826
implicit_dirs.update(osutils.parent_directories(path))
828
for path, kind in added:
829
if kind == 'directory' and path not in implicit_dirs:
831
path_decoded = osutils.normalized_filename(path)[0]
832
parent_path, basename = osutils.split(path_decoded)
833
parent_id = new_mapping.generate_file_id(parent_path)
834
if path in target_extras:
835
ret.unversioned.append(_mod_tree.TreeChange(
836
None, (None, path_decoded),
837
True, (False, False), (None, parent_id),
838
(None, basename), (None, kind), (None, False)))
840
file_id = new_mapping.generate_file_id(path_decoded)
842
_mod_tree.TreeChange(
843
file_id, (None, path_decoded), True,
846
(None, basename), (None, kind), (None, False)))
851
def changes_from_git_changes(changes, mapping, specific_files=None,
852
include_unchanged=False, target_extras=None):
853
"""Create a iter_changes-like generator from a git stream.
855
source and target are iterators over tuples with:
856
(filename, sha, mode)
858
if target_extras is None:
859
target_extras = set()
860
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
861
if oldpath is not None:
862
oldpath_decoded = oldpath.decode('utf-8')
864
oldpath_decoded = None
865
if newpath is not None:
866
newpath_decoded = newpath.decode('utf-8')
868
newpath_decoded = None
869
if not (specific_files is None or
870
(oldpath_decoded is not None and
871
osutils.is_inside_or_parent_of_any(
872
specific_files, oldpath_decoded)) or
873
(newpath_decoded is not None and
874
osutils.is_inside_or_parent_of_any(
875
specific_files, newpath_decoded))):
877
if oldpath is not None and mapping.is_special_file(oldpath):
879
if newpath is not None and mapping.is_special_file(newpath):
881
if oldpath_decoded is None:
882
fileid = mapping.generate_file_id(newpath_decoded)
891
oldexe = mode_is_executable(oldmode)
892
oldkind = mode_kind(oldmode)
896
if oldpath_decoded == u'':
900
(oldparentpath, oldname) = osutils.split(oldpath_decoded)
901
oldparent = mapping.generate_file_id(oldparentpath)
902
fileid = mapping.generate_file_id(oldpath_decoded)
903
if newpath_decoded is None:
910
newversioned = (newpath_decoded not in target_extras)
912
newexe = mode_is_executable(newmode)
913
newkind = mode_kind(newmode)
917
if newpath_decoded == u'':
921
newparentpath, newname = osutils.split(newpath_decoded)
922
newparent = mapping.generate_file_id(newparentpath)
923
if (not include_unchanged and
924
oldkind == 'directory' and newkind == 'directory' and
925
oldpath_decoded == newpath_decoded):
927
yield _mod_tree.TreeChange(
928
fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
929
(oldversioned, newversioned),
930
(oldparent, newparent), (oldname, newname),
931
(oldkind, newkind), (oldexe, newexe))
934
class InterGitTrees(_mod_tree.InterTree):
935
"""InterTree that works between two git trees."""
937
_matching_from_tree_format = None
938
_matching_to_tree_format = None
939
_test_mutable_trees_to_test_trees = None
942
def is_compatible(cls, source, target):
943
return (isinstance(source, GitRevisionTree) and
944
isinstance(target, GitRevisionTree))
946
def compare(self, want_unchanged=False, specific_files=None,
947
extra_trees=None, require_versioned=False, include_root=False,
948
want_unversioned=False):
949
with self.lock_read():
950
changes, target_extras = self._iter_git_changes(
951
want_unchanged=want_unchanged,
952
require_versioned=require_versioned,
953
specific_files=specific_files,
954
extra_trees=extra_trees,
955
want_unversioned=want_unversioned)
956
return tree_delta_from_git_changes(
957
changes, (self.source.mapping, self.target.mapping),
958
specific_files=specific_files,
959
include_root=include_root, target_extras=target_extras)
961
def iter_changes(self, include_unchanged=False, specific_files=None,
962
pb=None, extra_trees=[], require_versioned=True,
963
want_unversioned=False):
964
with self.lock_read():
965
changes, target_extras = self._iter_git_changes(
966
want_unchanged=include_unchanged,
967
require_versioned=require_versioned,
968
specific_files=specific_files,
969
extra_trees=extra_trees,
970
want_unversioned=want_unversioned)
971
return changes_from_git_changes(
972
changes, self.target.mapping,
973
specific_files=specific_files,
974
include_unchanged=include_unchanged,
975
target_extras=target_extras)
977
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
978
require_versioned=False, extra_trees=None,
979
want_unversioned=False):
980
raise NotImplementedError(self._iter_git_changes)
982
def find_target_path(self, path, recurse='none'):
983
ret = self.find_target_paths([path], recurse=recurse)
986
def find_source_path(self, path, recurse='none'):
987
ret = self.find_source_paths([path], recurse=recurse)
990
def find_target_paths(self, paths, recurse='none'):
993
changes = self._iter_git_changes(specific_files=paths)[0]
994
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
996
ret[oldpath] = newpath
999
if self.source.has_filename(path):
1000
if self.target.has_filename(path):
1005
raise errors.NoSuchFile(path)
1008
def find_source_paths(self, paths, recurse='none'):
1011
changes = self._iter_git_changes(specific_files=paths)[0]
1012
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
1013
if newpath in paths:
1014
ret[newpath] = oldpath
1017
if self.target.has_filename(path):
1018
if self.source.has_filename(path):
1023
raise errors.NoSuchFile(path)
1027
class InterGitRevisionTrees(InterGitTrees):
1028
"""InterTree that works between two git revision trees."""
1030
_matching_from_tree_format = None
1031
_matching_to_tree_format = None
1032
_test_mutable_trees_to_test_trees = None
1035
def is_compatible(cls, source, target):
1036
return (isinstance(source, GitRevisionTree) and
1037
isinstance(target, GitRevisionTree))
1039
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1040
require_versioned=True, extra_trees=None,
1041
want_unversioned=False):
1042
trees = [self.source]
1043
if extra_trees is not None:
1044
trees.extend(extra_trees)
1045
if specific_files is not None:
1046
specific_files = self.target.find_related_paths_across_trees(
1047
specific_files, trees,
1048
require_versioned=require_versioned)
1050
if (self.source._repository._git.object_store !=
1051
self.target._repository._git.object_store):
1052
store = OverlayObjectStore(
1053
[self.source._repository._git.object_store,
1054
self.target._repository._git.object_store])
1056
store = self.source._repository._git.object_store
1057
return store.tree_changes(
1058
self.source.tree, self.target.tree, want_unchanged=want_unchanged,
1059
include_trees=True, change_type_same=True), set()
1062
_mod_tree.InterTree.register_optimiser(InterGitRevisionTrees)
1065
class MutableGitIndexTree(mutabletree.MutableTree):
1068
self._lock_mode = None
1069
self._lock_count = 0
1070
self._versioned_dirs = None
1071
self._index_dirty = False
1072
self._submodules = None
1074
def is_versioned(self, path):
1075
with self.lock_read():
1076
path = path.rstrip('/').encode('utf-8')
1077
(index, subpath) = self._lookup_index(path)
1078
return (subpath in index or self._has_dir(path))
1080
def _has_dir(self, path):
1081
if not isinstance(path, bytes):
1082
raise TypeError(path)
1085
if self._versioned_dirs is None:
1087
return path in self._versioned_dirs
1089
def _load_dirs(self):
1090
if self._lock_mode is None:
1091
raise errors.ObjectNotLocked(self)
1092
self._versioned_dirs = set()
1093
for p, i in self._recurse_index_entries():
1094
self._ensure_versioned_dir(posixpath.dirname(p))
1096
def _ensure_versioned_dir(self, dirname):
1097
if not isinstance(dirname, bytes):
1098
raise TypeError(dirname)
1099
if dirname in self._versioned_dirs:
1102
self._ensure_versioned_dir(posixpath.dirname(dirname))
1103
self._versioned_dirs.add(dirname)
1105
def path2id(self, path):
1106
with self.lock_read():
1107
path = path.rstrip('/')
1108
if self.is_versioned(path.rstrip('/')):
1109
return self.mapping.generate_file_id(
1110
osutils.safe_unicode(path))
1113
def id2path(self, file_id, recurse='down'):
1116
if type(file_id) is not bytes:
1117
raise TypeError(file_id)
1118
with self.lock_read():
1120
path = self.mapping.parse_file_id(file_id)
1122
raise errors.NoSuchId(self, file_id)
1123
if self.is_versioned(path):
1125
raise errors.NoSuchId(self, file_id)
1127
def _set_root_id(self, file_id):
1128
raise errors.UnsupportedOperation(self._set_root_id, self)
1130
def _add(self, files, ids, kinds):
1131
for (path, file_id, kind) in zip(files, ids, kinds):
1132
if file_id is not None:
1133
raise workingtree.SettingFileIdUnsupported()
1134
path, can_access = osutils.normalized_filename(path)
1136
raise errors.InvalidNormalization(path)
1137
self._index_add_entry(path, kind)
1139
def _read_submodule_head(self, path):
1140
raise NotImplementedError(self._read_submodule_head)
1142
def _submodule_info(self):
1143
if self._submodules is None:
1145
with self.get_file('.gitmodules') as f:
1146
config = GitConfigFile.from_file(f)
1147
self._submodules = {
1148
path: (url, section)
1149
for path, url, section in parse_submodules(config)}
1150
except errors.NoSuchFile:
1151
self._submodules = {}
1152
return self._submodules
1154
def _lookup_index(self, encoded_path):
1155
if not isinstance(encoded_path, bytes):
1156
raise TypeError(encoded_path)
1158
if encoded_path in self.index:
1159
return self.index, encoded_path
1160
# TODO(jelmer): Perhaps have a cache with paths under which some
1163
remaining_path = encoded_path
1165
parts = remaining_path.split(b'/')
1166
for i in range(1, len(parts)):
1167
basepath = b'/'.join(parts[:i])
1169
(ctime, mtime, dev, ino, mode, uid, gid, size, sha,
1170
flags) = index[basepath]
1174
if S_ISGITLINK(mode):
1175
index = self._get_submodule_index(basepath)
1176
remaining_path = b'/'.join(parts[i:])
1179
return index, remaining_path
1181
return index, remaining_path
1182
return index, remaining_path
1184
def _index_del_entry(self, index, path):
1186
# TODO(jelmer): Keep track of dirty per index
1187
self._index_dirty = True
1189
def _index_add_entry(self, path, kind, flags=0, reference_revision=None):
1190
if kind == "directory":
1191
# Git indexes don't contain directories
1196
file, stat_val = self.get_file_with_stat(path)
1197
except (errors.NoSuchFile, IOError):
1198
# TODO: Rather than come up with something here, use the old
1201
stat_val = os.stat_result(
1202
(stat.S_IFREG | 0o644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1204
blob.set_raw_string(file.read())
1205
# Add object to the repository if it didn't exist yet
1206
if blob.id not in self.store:
1207
self.store.add_object(blob)
1209
elif kind == "symlink":
1212
stat_val = self._lstat(path)
1213
except EnvironmentError:
1214
# TODO: Rather than come up with something here, use the
1216
stat_val = os.stat_result(
1217
(stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1218
blob.set_raw_string(
1219
self.get_symlink_target(path).encode("utf-8"))
1220
# Add object to the repository if it didn't exist yet
1221
if blob.id not in self.store:
1222
self.store.add_object(blob)
1224
elif kind == "tree-reference":
1225
if reference_revision is not None:
1226
hexsha = self.branch.lookup_bzr_revision_id(
1227
reference_revision)[0]
1229
hexsha = self._read_submodule_head(path)
1231
raise errors.NoCommits(path)
1233
stat_val = self._lstat(path)
1234
except EnvironmentError:
1235
stat_val = os.stat_result(
1236
(S_IFGITLINK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1237
stat_val = os.stat_result((S_IFGITLINK, ) + stat_val[1:])
1239
raise AssertionError("unknown kind '%s'" % kind)
1240
# Add an entry to the index or update the existing entry
1241
ensure_normalized_path(path)
1242
encoded_path = path.encode("utf-8")
1243
if b'\r' in encoded_path or b'\n' in encoded_path:
1244
# TODO(jelmer): Why do we need to do this?
1245
trace.mutter('ignoring path with invalid newline in it: %r', path)
1247
(index, index_path) = self._lookup_index(encoded_path)
1248
index[index_path] = index_entry_from_stat(stat_val, hexsha, flags)
1249
self._index_dirty = True
1250
if self._versioned_dirs is not None:
1251
self._ensure_versioned_dir(index_path)
1253
def _recurse_index_entries(self, index=None, basepath=b"",
1254
recurse_nested=False):
1255
# Iterate over all index entries
1256
with self.lock_read():
1259
for path, value in index.items():
1260
(ctime, mtime, dev, ino, mode, uid, gid, size, sha,
1262
if S_ISGITLINK(mode) and recurse_nested:
1263
subindex = self._get_submodule_index(path)
1264
for entry in self._recurse_index_entries(
1265
index=subindex, basepath=path,
1266
recurse_nested=recurse_nested):
1269
yield (posixpath.join(basepath, path), value)
1271
def iter_entries_by_dir(self, specific_files=None,
1272
recurse_nested=False):
1273
with self.lock_read():
1274
if specific_files is not None:
1275
specific_files = set(specific_files)
1277
specific_files = None
1278
root_ie = self._get_dir_ie(u"", None)
1280
if specific_files is None or u"" in specific_files:
1281
ret[(u"", u"")] = root_ie
1282
dir_ids = {u"": root_ie.file_id}
1283
for path, value in self._recurse_index_entries(
1284
recurse_nested=recurse_nested):
1285
if self.mapping.is_special_file(path):
1287
path = path.decode("utf-8")
1288
if specific_files is not None and path not in specific_files:
1290
(parent, name) = posixpath.split(path)
1292
file_ie = self._get_file_ie(name, path, value, None)
1293
except errors.NoSuchFile:
1295
if specific_files is None:
1296
for (dir_path, dir_ie) in self._add_missing_parent_ids(
1298
ret[(posixpath.dirname(dir_path), dir_path)] = dir_ie
1299
file_ie.parent_id = self.path2id(parent)
1300
ret[(posixpath.dirname(path), path)] = file_ie
1301
# Special casing for directories
1303
for path in specific_files:
1304
key = (posixpath.dirname(path), path)
1305
if key not in ret and self.is_versioned(path):
1306
ret[key] = self._get_dir_ie(path, self.path2id(key[0]))
1307
return ((path, ie) for ((_, path), ie) in sorted(ret.items()))
1309
def iter_references(self):
1310
if self.supports_tree_reference():
1311
# TODO(jelmer): Implement a more efficient version of this
1312
for path, entry in self.iter_entries_by_dir():
1313
if entry.kind == 'tree-reference':
1316
def _get_dir_ie(self, path, parent_id):
1317
file_id = self.path2id(path)
1318
return GitTreeDirectory(file_id,
1319
posixpath.basename(path).strip("/"), parent_id)
1321
def _get_file_ie(self, name, path, value, parent_id):
1322
if not isinstance(name, str):
1323
raise TypeError(name)
1324
if not isinstance(path, str):
1325
raise TypeError(path)
1326
if not isinstance(value, tuple) or len(value) != 10:
1327
raise TypeError(value)
1328
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
1329
file_id = self.path2id(path)
1330
if not isinstance(file_id, bytes):
1331
raise TypeError(file_id)
1332
kind = mode_kind(mode)
1333
ie = entry_factory[kind](file_id, name, parent_id)
1334
if kind == 'symlink':
1335
ie.symlink_target = self.get_symlink_target(path)
1336
elif kind == 'tree-reference':
1337
ie.reference_revision = self.get_reference_revision(path)
1340
data = self.get_file_text(path)
1341
except errors.NoSuchFile:
1343
except IOError as e:
1344
if e.errno != errno.ENOENT:
1348
data = self.branch.repository._git.object_store[sha].data
1349
ie.text_sha1 = osutils.sha_string(data)
1350
ie.text_size = len(data)
1351
ie.executable = bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
1354
def _add_missing_parent_ids(self, path, dir_ids):
1357
parent = posixpath.dirname(path).strip("/")
1358
ret = self._add_missing_parent_ids(parent, dir_ids)
1359
parent_id = dir_ids[parent]
1360
ie = self._get_dir_ie(path, parent_id)
1361
dir_ids[path] = ie.file_id
1362
ret.append((path, ie))
1365
def _comparison_data(self, entry, path):
1367
return None, False, None
1368
return entry.kind, entry.executable, None
1370
def _unversion_path(self, path):
1371
if self._lock_mode is None:
1372
raise errors.ObjectNotLocked(self)
1373
encoded_path = path.encode("utf-8")
1375
(index, subpath) = self._lookup_index(encoded_path)
1377
self._index_del_entry(index, encoded_path)
1379
# A directory, perhaps?
1380
# TODO(jelmer): Deletes that involve submodules?
1381
for p in list(index):
1382
if p.startswith(subpath + b"/"):
1384
self._index_del_entry(index, p)
1387
self._versioned_dirs = None
1390
def unversion(self, paths):
1391
with self.lock_tree_write():
1393
if self._unversion_path(path) == 0:
1394
raise errors.NoSuchFile(path)
1395
self._versioned_dirs = None
1401
def update_basis_by_delta(self, revid, delta):
1402
# TODO(jelmer): This shouldn't be called, it's inventory specific.
1403
for (old_path, new_path, file_id, ie) in delta:
1404
if old_path is not None:
1405
(index, old_subpath) = self._lookup_index(
1406
old_path.encode('utf-8'))
1407
if old_subpath in index:
1408
self._index_del_entry(index, old_subpath)
1409
self._versioned_dirs = None
1410
if new_path is not None and ie.kind != 'directory':
1411
self._index_add_entry(new_path, ie.kind)
1413
self._set_merges_from_parent_ids([])
1415
def move(self, from_paths, to_dir=None, after=None):
1417
with self.lock_tree_write():
1418
to_abs = self.abspath(to_dir)
1419
if not os.path.isdir(to_abs):
1420
raise errors.BzrMoveFailedError('', to_dir,
1421
errors.NotADirectory(to_abs))
1423
for from_rel in from_paths:
1424
from_tail = os.path.split(from_rel)[-1]
1425
to_rel = os.path.join(to_dir, from_tail)
1426
self.rename_one(from_rel, to_rel, after=after)
1427
rename_tuples.append((from_rel, to_rel))
1429
return rename_tuples
1431
def rename_one(self, from_rel, to_rel, after=None):
1432
from_path = from_rel.encode("utf-8")
1433
to_rel, can_access = osutils.normalized_filename(to_rel)
1435
raise errors.InvalidNormalization(to_rel)
1436
to_path = to_rel.encode("utf-8")
1437
with self.lock_tree_write():
1439
# Perhaps it's already moved?
1441
not self.has_filename(from_rel) and
1442
self.has_filename(to_rel) and
1443
not self.is_versioned(to_rel))
1445
if not self.has_filename(to_rel):
1446
raise errors.BzrMoveFailedError(
1447
from_rel, to_rel, errors.NoSuchFile(to_rel))
1448
if self.basis_tree().is_versioned(to_rel):
1449
raise errors.BzrMoveFailedError(
1450
from_rel, to_rel, errors.AlreadyVersionedError(to_rel))
1452
kind = self.kind(to_rel)
1455
to_kind = self.kind(to_rel)
1456
except errors.NoSuchFile:
1457
exc_type = errors.BzrRenameFailedError
1460
exc_type = errors.BzrMoveFailedError
1461
if self.is_versioned(to_rel):
1462
raise exc_type(from_rel, to_rel,
1463
errors.AlreadyVersionedError(to_rel))
1464
if not self.has_filename(from_rel):
1465
raise errors.BzrMoveFailedError(
1466
from_rel, to_rel, errors.NoSuchFile(from_rel))
1467
kind = self.kind(from_rel)
1468
if not self.is_versioned(from_rel) and kind != 'directory':
1469
raise exc_type(from_rel, to_rel,
1470
errors.NotVersionedError(from_rel))
1471
if self.has_filename(to_rel):
1472
raise errors.RenameFailedFilesExist(
1473
from_rel, to_rel, errors.FileExists(to_rel))
1475
kind = self.kind(from_rel)
1477
if not after and kind != 'directory':
1478
(index, from_subpath) = self._lookup_index(from_path)
1479
if from_subpath not in index:
1481
raise errors.BzrMoveFailedError(
1483
errors.NotVersionedError(path=from_rel))
1487
self._rename_one(from_rel, to_rel)
1488
except OSError as e:
1489
if e.errno == errno.ENOENT:
1490
raise errors.BzrMoveFailedError(
1491
from_rel, to_rel, errors.NoSuchFile(to_rel))
1493
if kind != 'directory':
1494
(index, from_index_path) = self._lookup_index(from_path)
1496
self._index_del_entry(index, from_path)
1499
self._index_add_entry(to_rel, kind)
1501
todo = [(p, i) for (p, i) in self._recurse_index_entries()
1502
if p.startswith(from_path + b'/')]
1503
for child_path, child_value in todo:
1504
(child_to_index, child_to_index_path) = self._lookup_index(
1505
posixpath.join(to_path, posixpath.relpath(child_path, from_path)))
1506
child_to_index[child_to_index_path] = child_value
1507
# TODO(jelmer): Mark individual index as dirty
1508
self._index_dirty = True
1509
(child_from_index, child_from_index_path) = self._lookup_index(
1511
self._index_del_entry(
1512
child_from_index, child_from_index_path)
1514
self._versioned_dirs = None
1517
def find_related_paths_across_trees(self, paths, trees=[],
1518
require_versioned=True):
1522
if require_versioned:
1523
trees = [self] + (trees if trees is not None else [])
1527
if t.is_versioned(p):
1532
raise errors.PathsNotVersionedError(unversioned)
1534
return filter(self.is_versioned, paths)
1536
def path_content_summary(self, path):
1537
"""See Tree.path_content_summary."""
1539
stat_result = self._lstat(path)
1540
except OSError as e:
1541
if getattr(e, 'errno', None) == errno.ENOENT:
1543
return ('missing', None, None, None)
1544
# propagate other errors
1546
kind = mode_kind(stat_result.st_mode)
1548
return self._file_content_summary(path, stat_result)
1549
elif kind == 'directory':
1550
# perhaps it looks like a plain directory, but it's really a
1552
if self._directory_is_tree_reference(path):
1553
kind = 'tree-reference'
1554
return kind, None, None, None
1555
elif kind == 'symlink':
1556
target = osutils.readlink(self.abspath(path))
1557
return ('symlink', None, None, target)
1559
return (kind, None, None, None)
1561
def stored_kind(self, relpath):
1562
(index, index_path) = self._lookup_index(relpath.encode('utf-8'))
1566
mode = index[index_path].mode
1570
if S_ISGITLINK(mode):
1571
return 'tree-reference'
1574
def kind(self, relpath):
1575
kind = osutils.file_kind(self.abspath(relpath))
1576
if kind == 'directory':
1577
if self._directory_is_tree_reference(relpath):
1578
return 'tree-reference'
1583
def _live_entry(self, relpath):
1584
raise NotImplementedError(self._live_entry)
1586
def get_transform(self, pb=None):
1587
from ..transform import TreeTransform
1588
return TreeTransform(self, pb=pb)
1592
class InterIndexGitTree(InterGitTrees):
1593
"""InterTree that works between a Git revision tree and an index."""
1595
def __init__(self, source, target):
1596
super(InterIndexGitTree, self).__init__(source, target)
1597
self._index = target.index
1600
def is_compatible(cls, source, target):
1601
return (isinstance(source, GitRevisionTree) and
1602
isinstance(target, MutableGitIndexTree))
1604
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1605
require_versioned=False, extra_trees=None,
1606
want_unversioned=False):
1607
trees = [self.source]
1608
if extra_trees is not None:
1609
trees.extend(extra_trees)
1610
if specific_files is not None:
1611
specific_files = self.target.find_related_paths_across_trees(
1612
specific_files, trees,
1613
require_versioned=require_versioned)
1614
# TODO(jelmer): Restrict to specific_files, for performance reasons.
1615
with self.lock_read():
1616
return changes_between_git_tree_and_working_copy(
1617
self.source.store, self.source.tree,
1618
self.target, want_unchanged=want_unchanged,
1619
want_unversioned=want_unversioned)
1622
_mod_tree.InterTree.register_optimiser(InterIndexGitTree)
1625
def changes_between_git_tree_and_working_copy(store, from_tree_sha, target,
1626
want_unchanged=False,
1627
want_unversioned=False):
1628
"""Determine the changes between a git tree and a working tree with index.
1633
# Report dirified directories to commit_tree first, so that they can be
1634
# replaced with non-empty directories if they have contents.
1636
trust_executable = target._supports_executable()
1637
for path, index_entry in target._recurse_index_entries():
1639
live_entry = target._live_entry(path)
1640
except EnvironmentError as e:
1641
if e.errno == errno.ENOENT:
1642
# Entry was removed; keep it listed, but mark it as gone.
1643
blobs[path] = (ZERO_SHA, 0)
1647
if live_entry is None:
1648
# Entry was turned into a directory.
1649
# Maybe it's just a submodule that's not checked out?
1650
if S_ISGITLINK(index_entry.mode):
1651
blobs[path] = (index_entry.sha, index_entry.mode)
1653
dirified.append((path, Tree().id, stat.S_IFDIR))
1654
store.add_object(Tree())
1656
mode = live_entry.mode
1657
if not trust_executable:
1658
if mode_is_executable(index_entry.mode):
1662
blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
1663
if want_unversioned:
1664
for e in target.extras():
1665
st = target._lstat(e)
1667
np, accessible = osutils.normalized_filename(e)
1668
except UnicodeDecodeError:
1669
raise errors.BadFilenameEncoding(
1671
if stat.S_ISDIR(st.st_mode):
1674
blob = blob_from_path_and_stat(
1675
target.abspath(e).encode(osutils._fs_enc), st)
1676
store.add_object(blob)
1677
np = np.encode('utf-8')
1678
blobs[np] = (blob.id, cleanup_mode(st.st_mode))
1680
to_tree_sha = commit_tree(
1681
store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()])
1682
return store.tree_changes(
1683
from_tree_sha, to_tree_sha, include_trees=True,
1684
want_unchanged=want_unchanged, change_type_same=True), extras