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, RenameDetector
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 (
75
from .transportgit import (
81
class GitTreeDirectory(_mod_tree.TreeDirectory):
83
__slots__ = ['file_id', 'name', 'parent_id', 'children']
85
def __init__(self, file_id, name, parent_id):
86
self.file_id = file_id
88
self.parent_id = parent_id
101
return self.__class__(
102
self.file_id, self.name, self.parent_id)
105
return "%s(file_id=%r, name=%r, parent_id=%r)" % (
106
self.__class__.__name__, self.file_id, self.name,
109
def __eq__(self, other):
110
return (self.kind == other.kind and
111
self.file_id == other.file_id and
112
self.name == other.name and
113
self.parent_id == other.parent_id)
116
class GitTreeFile(_mod_tree.TreeFile):
118
__slots__ = ['file_id', 'name', 'parent_id', 'text_size', 'text_sha1',
121
def __init__(self, file_id, name, parent_id, text_size=None,
122
text_sha1=None, executable=None):
123
self.file_id = file_id
125
self.parent_id = parent_id
126
self.text_size = text_size
127
self.text_sha1 = text_sha1
128
self.executable = executable
134
def __eq__(self, other):
135
return (self.kind == other.kind and
136
self.file_id == other.file_id and
137
self.name == other.name and
138
self.parent_id == other.parent_id and
139
self.text_sha1 == other.text_sha1 and
140
self.text_size == other.text_size and
141
self.executable == other.executable)
144
return ("%s(file_id=%r, name=%r, parent_id=%r, text_size=%r, "
145
"text_sha1=%r, executable=%r)") % (
146
type(self).__name__, self.file_id, self.name, self.parent_id,
147
self.text_size, self.text_sha1, self.executable)
150
ret = self.__class__(
151
self.file_id, self.name, self.parent_id)
152
ret.text_sha1 = self.text_sha1
153
ret.text_size = self.text_size
154
ret.executable = self.executable
158
class GitTreeSymlink(_mod_tree.TreeLink):
160
__slots__ = ['file_id', 'name', 'parent_id', 'symlink_target']
162
def __init__(self, file_id, name, parent_id,
163
symlink_target=None):
164
self.file_id = file_id
166
self.parent_id = parent_id
167
self.symlink_target = symlink_target
174
def executable(self):
182
return "%s(file_id=%r, name=%r, parent_id=%r, symlink_target=%r)" % (
183
type(self).__name__, self.file_id, self.name, self.parent_id,
186
def __eq__(self, other):
187
return (self.kind == other.kind and
188
self.file_id == other.file_id and
189
self.name == other.name and
190
self.parent_id == other.parent_id and
191
self.symlink_target == other.symlink_target)
194
return self.__class__(
195
self.file_id, self.name, self.parent_id,
199
class GitTreeSubmodule(_mod_tree.TreeReference):
201
__slots__ = ['file_id', 'name', 'parent_id', 'reference_revision']
203
def __init__(self, file_id, name, parent_id, reference_revision=None):
204
self.file_id = file_id
206
self.parent_id = parent_id
207
self.reference_revision = reference_revision
210
def executable(self):
215
return 'tree-reference'
218
return ("%s(file_id=%r, name=%r, parent_id=%r, "
219
"reference_revision=%r)") % (
220
type(self).__name__, self.file_id, self.name, self.parent_id,
221
self.reference_revision)
223
def __eq__(self, other):
224
return (self.kind == other.kind and
225
self.file_id == other.file_id and
226
self.name == other.name and
227
self.parent_id == other.parent_id and
228
self.reference_revision == other.reference_revision)
231
return self.__class__(
232
self.file_id, self.name, self.parent_id,
233
self.reference_revision)
237
'directory': GitTreeDirectory,
239
'symlink': GitTreeSymlink,
240
'tree-reference': GitTreeSubmodule,
244
def ensure_normalized_path(path):
245
"""Check whether path is normalized.
247
:raises InvalidNormalization: When path is not normalized, and cannot be
248
accessed on this platform by the normalized path.
249
:return: The NFC normalised version of path.
251
norm_path, can_access = osutils.normalized_filename(path)
252
if norm_path != path:
256
raise errors.InvalidNormalization(path)
260
class GitRevisionTree(revisiontree.RevisionTree):
261
"""Revision tree implementation based on Git objects."""
263
def __init__(self, repository, revision_id):
264
self._revision_id = revision_id
265
self._repository = repository
266
self._submodules = None
267
self.store = repository._git.object_store
268
if not isinstance(revision_id, bytes):
269
raise TypeError(revision_id)
270
self.commit_id, self.mapping = repository.lookup_bzr_revision_id(
272
if revision_id == NULL_REVISION:
274
self.mapping = default_mapping
277
commit = self.store[self.commit_id]
279
raise errors.NoSuchRevision(repository, revision_id)
280
self.tree = commit.tree
282
def _submodule_info(self):
283
if self._submodules is None:
285
with self.get_file('.gitmodules') as f:
286
config = GitConfigFile.from_file(f)
289
for path, url, section in parse_submodules(config)}
290
except errors.NoSuchFile:
291
self._submodules = {}
292
return self._submodules
294
def _get_submodule_repository(self, relpath):
295
if not isinstance(relpath, bytes):
296
raise TypeError(relpath)
298
info = self._submodule_info()[relpath]
300
nested_repo_transport = self._repository.controldir.user_transport.clone(
301
decode_git_path(relpath))
303
nested_repo_transport = self._repository.controldir.control_transport.clone(
304
posixpath.join('modules', decode_git_path(info[1])))
305
nested_controldir = _mod_controldir.ControlDir.open_from_transport(
306
nested_repo_transport)
307
return nested_controldir.find_repository()
309
def _get_submodule_store(self, relpath):
310
return self._get_submodule_repository(relpath)._git.object_store
312
def get_nested_tree(self, path):
313
encoded_path = encode_git_path(path)
314
nested_repo = self._get_submodule_repository(encoded_path)
315
ref_rev = self.get_reference_revision(path)
316
return nested_repo.revision_tree(ref_rev)
318
def supports_rename_tracking(self):
321
def get_file_revision(self, path):
322
change_scanner = self._repository._file_change_scanner
323
if self.commit_id == ZERO_SHA:
325
(unused_path, commit_id) = change_scanner.find_last_change_revision(
326
encode_git_path(path), self.commit_id)
327
return self._repository.lookup_foreign_revision_id(
328
commit_id, self.mapping)
330
def get_file_mtime(self, path):
332
revid = self.get_file_revision(path)
334
raise errors.NoSuchFile(path)
336
rev = self._repository.get_revision(revid)
337
except errors.NoSuchRevision:
338
raise _mod_tree.FileTimestampUnavailable(path)
341
def id2path(self, file_id, recurse='down'):
343
path = self.mapping.parse_file_id(file_id)
345
raise errors.NoSuchId(self, file_id)
346
if self.is_versioned(path):
348
raise errors.NoSuchId(self, file_id)
350
def is_versioned(self, path):
351
return self.has_filename(path)
353
def path2id(self, path):
354
if self.mapping.is_special_file(path):
356
if not self.is_versioned(path):
358
return self.mapping.generate_file_id(osutils.safe_unicode(path))
360
def all_file_ids(self):
361
raise errors.UnsupportedOperation(self.all_file_ids, self)
363
def all_versioned_paths(self):
365
todo = [(self.store, b'', self.tree)]
367
(store, path, tree_id) = todo.pop()
370
tree = store[tree_id]
371
for name, mode, hexsha in tree.items():
372
subpath = posixpath.join(path, name)
373
ret.add(decode_git_path(subpath))
374
if stat.S_ISDIR(mode):
375
todo.append((store, subpath, hexsha))
378
def _lookup_path(self, path):
379
if self.tree is None:
380
raise errors.NoSuchFile(path)
382
encoded_path = encode_git_path(path)
383
parts = encoded_path.split(b'/')
387
for i, p in enumerate(parts):
391
if not isinstance(obj, Tree):
392
raise NotTreeError(hexsha)
394
mode, hexsha = obj[p]
396
raise errors.NoSuchFile(path)
397
if S_ISGITLINK(mode) and i != len(parts) - 1:
398
store = self._get_submodule_store(b'/'.join(parts[:i + 1]))
399
hexsha = store[hexsha].tree
400
return (store, mode, hexsha)
402
def is_executable(self, path):
403
(store, mode, hexsha) = self._lookup_path(path)
405
# the tree root is a directory
407
return mode_is_executable(mode)
409
def kind(self, path):
410
(store, mode, hexsha) = self._lookup_path(path)
412
# the tree root is a directory
414
return mode_kind(mode)
416
def has_filename(self, path):
418
self._lookup_path(path)
419
except errors.NoSuchFile:
424
def _submodule_info(self):
425
if self._submodules is None:
427
with self.get_file('.gitmodules') as f:
428
config = GitConfigFile.from_file(f)
431
for path, url, section in parse_submodules(config)}
432
except errors.NoSuchFile:
433
self._submodules = {}
434
return self._submodules
436
def list_files(self, include_root=False, from_dir=None, recursive=True,
437
recurse_nested=False):
438
if self.tree is None:
440
if from_dir is None or from_dir == '.':
442
(store, mode, hexsha) = self._lookup_path(from_dir)
443
if mode is None: # Root
444
root_ie = self._get_dir_ie(b"", None)
446
parent_path = posixpath.dirname(from_dir)
447
parent_id = self.mapping.generate_file_id(parent_path)
448
if mode_kind(mode) == 'directory':
449
root_ie = self._get_dir_ie(encode_git_path(from_dir), parent_id)
451
root_ie = self._get_file_ie(
452
store, encode_git_path(from_dir),
453
posixpath.basename(from_dir), mode, hexsha)
455
yield (from_dir, "V", root_ie.kind, root_ie)
457
if root_ie.kind == 'directory':
458
todo.append((store, encode_git_path(from_dir),
459
b"", hexsha, root_ie.file_id))
461
(store, path, relpath, hexsha, parent_id) = todo.pop()
463
for name, mode, hexsha in tree.iteritems():
464
if self.mapping.is_special_file(name):
466
child_path = posixpath.join(path, name)
467
child_relpath = posixpath.join(relpath, name)
468
if S_ISGITLINK(mode) and recurse_nested:
470
store = self._get_submodule_store(child_relpath)
471
hexsha = store[hexsha].tree
472
if stat.S_ISDIR(mode):
473
ie = self._get_dir_ie(child_path, parent_id)
476
(store, child_path, child_relpath, hexsha,
479
ie = self._get_file_ie(
480
store, child_path, name, mode, hexsha, parent_id)
481
yield (decode_git_path(child_relpath), "V", ie.kind, ie)
483
def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
484
if not isinstance(path, bytes):
485
raise TypeError(path)
486
if not isinstance(name, bytes):
487
raise TypeError(name)
488
kind = mode_kind(mode)
489
path = decode_git_path(path)
490
name = decode_git_path(name)
491
file_id = self.mapping.generate_file_id(path)
492
ie = entry_factory[kind](file_id, name, parent_id)
493
if kind == 'symlink':
494
ie.symlink_target = decode_git_path(store[hexsha].data)
495
elif kind == 'tree-reference':
496
ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(
499
data = store[hexsha].data
500
ie.text_sha1 = osutils.sha_string(data)
501
ie.text_size = len(data)
502
ie.executable = mode_is_executable(mode)
505
def _get_dir_ie(self, path, parent_id):
506
path = decode_git_path(path)
507
file_id = self.mapping.generate_file_id(path)
508
return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
510
def iter_child_entries(self, path):
511
(store, mode, tree_sha) = self._lookup_path(path)
513
if mode is not None and not stat.S_ISDIR(mode):
516
encoded_path = encode_git_path(path)
517
file_id = self.path2id(path)
518
tree = store[tree_sha]
519
for name, mode, hexsha in tree.iteritems():
520
if self.mapping.is_special_file(name):
522
child_path = posixpath.join(encoded_path, name)
523
if stat.S_ISDIR(mode):
524
yield self._get_dir_ie(child_path, file_id)
526
yield self._get_file_ie(store, child_path, name, mode, hexsha,
529
def iter_entries_by_dir(self, specific_files=None,
530
recurse_nested=False):
531
if self.tree is None:
533
if specific_files is not None:
534
if specific_files in ([""], []):
535
specific_files = None
537
specific_files = set([encode_git_path(p)
538
for p in specific_files])
539
todo = deque([(self.store, b"", self.tree, self.path2id(''))])
540
if specific_files is None or u"" in specific_files:
541
yield u"", self._get_dir_ie(b"", None)
543
store, path, tree_sha, parent_id = todo.popleft()
544
tree = store[tree_sha]
546
for name, mode, hexsha in tree.iteritems():
547
if self.mapping.is_special_file(name):
549
child_path = posixpath.join(path, name)
550
child_path_decoded = decode_git_path(child_path)
551
if recurse_nested and S_ISGITLINK(mode):
553
store = self._get_submodule_store(child_path)
554
hexsha = store[hexsha].tree
555
if stat.S_ISDIR(mode):
556
if (specific_files is None or
557
any([p for p in specific_files if p.startswith(
560
(store, child_path, hexsha,
561
self.path2id(child_path_decoded)))
562
if specific_files is None or child_path in specific_files:
563
if stat.S_ISDIR(mode):
564
yield (child_path_decoded,
565
self._get_dir_ie(child_path, parent_id))
567
yield (child_path_decoded,
568
self._get_file_ie(store, child_path, name, mode,
570
todo.extendleft(reversed(extradirs))
572
def iter_references(self):
573
if self.supports_tree_reference():
574
for path, entry in self.iter_entries_by_dir():
575
if entry.kind == 'tree-reference':
578
def get_revision_id(self):
579
"""See RevisionTree.get_revision_id."""
580
return self._revision_id
582
def get_file_sha1(self, path, stat_value=None):
583
if self.tree is None:
584
raise errors.NoSuchFile(path)
585
return osutils.sha_string(self.get_file_text(path))
587
def get_file_verifier(self, path, stat_value=None):
588
(store, mode, hexsha) = self._lookup_path(path)
589
return ("GIT", hexsha)
591
def get_file_size(self, path):
592
(store, mode, hexsha) = self._lookup_path(path)
593
if stat.S_ISREG(mode):
594
return len(store[hexsha].data)
597
def get_file_text(self, path):
598
"""See RevisionTree.get_file_text."""
599
(store, mode, hexsha) = self._lookup_path(path)
600
if stat.S_ISREG(mode):
601
return store[hexsha].data
605
def get_symlink_target(self, path):
606
"""See RevisionTree.get_symlink_target."""
607
(store, mode, hexsha) = self._lookup_path(path)
608
if stat.S_ISLNK(mode):
609
return decode_git_path(store[hexsha].data)
613
def get_reference_revision(self, path):
614
"""See RevisionTree.get_symlink_target."""
615
(store, mode, hexsha) = self._lookup_path(path)
616
if S_ISGITLINK(mode):
618
nested_repo = self._get_submodule_repository(encode_git_path(path))
619
except errors.NotBranchError:
620
return self.mapping.revision_id_foreign_to_bzr(hexsha)
622
return nested_repo.lookup_foreign_revision_id(hexsha)
626
def _comparison_data(self, entry, path):
628
return None, False, None
629
return entry.kind, entry.executable, None
631
def path_content_summary(self, path):
632
"""See Tree.path_content_summary."""
634
(store, mode, hexsha) = self._lookup_path(path)
635
except errors.NoSuchFile:
636
return ('missing', None, None, None)
637
kind = mode_kind(mode)
639
executable = mode_is_executable(mode)
640
contents = store[hexsha].data
641
return (kind, len(contents), executable,
642
osutils.sha_string(contents))
643
elif kind == 'symlink':
644
return (kind, None, None, decode_git_path(store[hexsha].data))
645
elif kind == 'tree-reference':
646
nested_repo = self._get_submodule_repository(encode_git_path(path))
647
return (kind, None, None,
648
nested_repo.lookup_foreign_revision_id(hexsha))
650
return (kind, None, None, None)
652
def find_related_paths_across_trees(self, paths, trees=[],
653
require_versioned=True):
656
if require_versioned:
657
trees = [self] + (trees if trees is not None else [])
661
if t.is_versioned(p):
666
raise errors.PathsNotVersionedError(unversioned)
667
return filter(self.is_versioned, paths)
669
def _iter_tree_contents(self, include_trees=False):
670
if self.tree is None:
672
return self.store.iter_tree_contents(
673
self.tree, include_trees=include_trees)
675
def annotate_iter(self, path, default_revision=CURRENT_REVISION):
676
"""Return an iterator of revision_id, line tuples.
678
For working trees (and mutable trees in general), the special
679
revision_id 'current:' will be used for lines that are new in this
680
tree, e.g. uncommitted changes.
681
:param default_revision: For lines that don't match a basis, mark them
682
with this revision id. Not all implementations will make use of
685
with self.lock_read():
686
# Now we have the parents of this content
687
from breezy.annotate import Annotator
688
from .annotate import AnnotateProvider
689
annotator = Annotator(AnnotateProvider(
690
self._repository._file_change_scanner))
691
this_key = (path, self.get_file_revision(path))
692
annotations = [(key[-1], line)
693
for key, line in annotator.annotate_flat(this_key)]
696
def _get_rules_searcher(self, default_searcher):
697
return default_searcher
699
def walkdirs(self, prefix=u""):
700
(store, mode, hexsha) = self._lookup_path(prefix)
702
[(store, encode_git_path(prefix), hexsha, self.path2id(prefix))])
704
store, path, tree_sha, parent_id = todo.popleft()
705
path_decoded = decode_git_path(path)
706
tree = store[tree_sha]
708
for name, mode, hexsha in tree.iteritems():
709
if self.mapping.is_special_file(name):
711
child_path = posixpath.join(path, name)
712
file_id = self.path2id(decode_git_path(child_path))
713
if stat.S_ISDIR(mode):
714
todo.append((store, child_path, hexsha, file_id))
716
(decode_git_path(child_path), decode_git_path(name),
717
mode_kind(mode), None,
718
file_id, mode_kind(mode)))
719
yield (path_decoded, parent_id), children
721
def preview_transform(self, pb=None):
722
from .transform import GitTransformPreview
723
return GitTransformPreview(self, pb=pb)
726
def tree_delta_from_git_changes(changes, mappings,
728
require_versioned=False, include_root=False,
729
source_extras=None, target_extras=None):
730
"""Create a TreeDelta from two git trees.
732
source and target are iterators over tuples with:
733
(filename, sha, mode)
735
(old_mapping, new_mapping) = mappings
736
if target_extras is None:
737
target_extras = set()
738
if source_extras is None:
739
source_extras = set()
740
ret = delta.TreeDelta()
742
for (change_type, old, new) in changes:
743
(oldpath, oldmode, oldsha) = old
744
(newpath, newmode, newsha) = new
745
if newpath == b'' and not include_root:
747
copied = (change_type == 'copy')
748
if oldpath is not None:
749
oldpath_decoded = decode_git_path(oldpath)
751
oldpath_decoded = None
752
if newpath is not None:
753
newpath_decoded = decode_git_path(newpath)
755
newpath_decoded = None
756
if not (specific_files is None or
757
(oldpath is not None and
758
osutils.is_inside_or_parent_of_any(
759
specific_files, oldpath_decoded)) or
760
(newpath is not None and
761
osutils.is_inside_or_parent_of_any(
762
specific_files, newpath_decoded))):
772
oldversioned = (oldpath not in source_extras)
774
oldexe = mode_is_executable(oldmode)
775
oldkind = mode_kind(oldmode)
783
(oldparentpath, oldname) = osutils.split(oldpath_decoded)
784
oldparent = old_mapping.generate_file_id(oldparentpath)
792
newversioned = (newpath not in target_extras)
794
newexe = mode_is_executable(newmode)
795
newkind = mode_kind(newmode)
799
if newpath_decoded == u'':
803
newparentpath, newname = osutils.split(newpath_decoded)
804
newparent = new_mapping.generate_file_id(newparentpath)
805
if oldversioned and not copied:
806
fileid = old_mapping.generate_file_id(oldpath_decoded)
808
fileid = new_mapping.generate_file_id(newpath_decoded)
811
if old_mapping.is_special_file(oldpath):
813
if new_mapping.is_special_file(newpath):
815
if oldpath is None and newpath is None:
817
change = _mod_tree.TreeChange(
818
fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
819
(oldversioned, newversioned),
820
(oldparent, newparent), (oldname, newname),
821
(oldkind, newkind), (oldexe, newexe),
823
if newpath is not None and not newversioned and newkind != 'directory':
824
change.file_id = None
825
ret.unversioned.append(change)
826
elif change_type == 'add':
827
added.append((newpath, newkind))
828
elif newpath is None or newmode == 0:
829
ret.removed.append(change)
830
elif change_type == 'delete':
831
ret.removed.append(change)
832
elif change_type == 'copy':
833
if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
835
ret.copied.append(change)
836
elif change_type == 'rename':
837
if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
839
ret.renamed.append(change)
840
elif mode_kind(oldmode) != mode_kind(newmode):
841
ret.kind_changed.append(change)
842
elif oldsha != newsha or oldmode != newmode:
843
if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
845
ret.modified.append(change)
847
ret.unchanged.append(change)
849
implicit_dirs = {b''}
850
for path, kind in added:
851
if kind == 'directory' or path in target_extras:
853
implicit_dirs.update(osutils.parent_directories(path))
855
for path, kind in added:
856
if kind == 'directory' and path not in implicit_dirs:
858
path_decoded = decode_git_path(path)
859
parent_path, basename = osutils.split(path_decoded)
860
parent_id = new_mapping.generate_file_id(parent_path)
861
file_id = new_mapping.generate_file_id(path_decoded)
863
_mod_tree.TreeChange(
864
file_id, (None, path_decoded), True,
867
(None, basename), (None, kind), (None, False)))
872
def changes_from_git_changes(changes, mapping, specific_files=None,
873
include_unchanged=False, source_extras=None,
875
"""Create a iter_changes-like generator from a git stream.
877
source and target are iterators over tuples with:
878
(filename, sha, mode)
880
if target_extras is None:
881
target_extras = set()
882
if source_extras is None:
883
source_extras = set()
884
for (change_type, old, new) in changes:
885
if change_type == 'unchanged' and not include_unchanged:
887
(oldpath, oldmode, oldsha) = old
888
(newpath, newmode, newsha) = new
889
if oldpath is not None:
890
oldpath_decoded = decode_git_path(oldpath)
892
oldpath_decoded = None
893
if newpath is not None:
894
newpath_decoded = decode_git_path(newpath)
896
newpath_decoded = None
897
if not (specific_files is None or
898
(oldpath_decoded is not None and
899
osutils.is_inside_or_parent_of_any(
900
specific_files, oldpath_decoded)) or
901
(newpath_decoded is not None and
902
osutils.is_inside_or_parent_of_any(
903
specific_files, newpath_decoded))):
905
if oldpath is not None and mapping.is_special_file(oldpath):
907
if newpath is not None and mapping.is_special_file(newpath):
916
oldversioned = (oldpath not in source_extras)
918
oldexe = mode_is_executable(oldmode)
919
oldkind = mode_kind(oldmode)
923
if oldpath_decoded == u'':
927
(oldparentpath, oldname) = osutils.split(oldpath_decoded)
928
oldparent = mapping.generate_file_id(oldparentpath)
936
newversioned = (newpath not in target_extras)
938
newexe = mode_is_executable(newmode)
939
newkind = mode_kind(newmode)
943
if newpath_decoded == u'':
947
newparentpath, newname = osutils.split(newpath_decoded)
948
newparent = mapping.generate_file_id(newparentpath)
949
if (not include_unchanged and
950
oldkind == 'directory' and newkind == 'directory' and
951
oldpath_decoded == newpath_decoded):
953
if oldversioned and change_type != 'copy':
954
fileid = mapping.generate_file_id(oldpath_decoded)
956
fileid = mapping.generate_file_id(newpath_decoded)
959
yield _mod_tree.TreeChange(
960
fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
961
(oldversioned, newversioned),
962
(oldparent, newparent), (oldname, newname),
963
(oldkind, newkind), (oldexe, newexe),
964
copied=(change_type == 'copy'))
967
class InterGitTrees(_mod_tree.InterTree):
968
"""InterTree that works between two git trees."""
970
_matching_from_tree_format = None
971
_matching_to_tree_format = None
972
_test_mutable_trees_to_test_trees = None
975
def is_compatible(cls, source, target):
976
return (isinstance(source, GitRevisionTree) and
977
isinstance(target, GitRevisionTree))
979
def compare(self, want_unchanged=False, specific_files=None,
980
extra_trees=None, require_versioned=False, include_root=False,
981
want_unversioned=False):
982
with self.lock_read():
983
changes, source_extras, target_extras = self._iter_git_changes(
984
want_unchanged=want_unchanged,
985
require_versioned=require_versioned,
986
specific_files=specific_files,
987
extra_trees=extra_trees,
988
want_unversioned=want_unversioned)
989
return tree_delta_from_git_changes(
990
changes, (self.source.mapping, self.target.mapping),
991
specific_files=specific_files,
992
include_root=include_root,
993
source_extras=source_extras, target_extras=target_extras)
995
def iter_changes(self, include_unchanged=False, specific_files=None,
996
pb=None, extra_trees=[], require_versioned=True,
997
want_unversioned=False):
998
with self.lock_read():
999
changes, source_extras, target_extras = self._iter_git_changes(
1000
want_unchanged=include_unchanged,
1001
require_versioned=require_versioned,
1002
specific_files=specific_files,
1003
extra_trees=extra_trees,
1004
want_unversioned=want_unversioned)
1005
return changes_from_git_changes(
1006
changes, self.target.mapping,
1007
specific_files=specific_files,
1008
include_unchanged=include_unchanged,
1009
source_extras=source_extras,
1010
target_extras=target_extras)
1012
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1013
require_versioned=False, extra_trees=None,
1014
want_unversioned=False, include_trees=True):
1015
raise NotImplementedError(self._iter_git_changes)
1017
def find_target_path(self, path, recurse='none'):
1018
ret = self.find_target_paths([path], recurse=recurse)
1021
def find_source_path(self, path, recurse='none'):
1022
ret = self.find_source_paths([path], recurse=recurse)
1025
def find_target_paths(self, paths, recurse='none'):
1028
changes = self._iter_git_changes(
1029
specific_files=paths, include_trees=False)[0]
1030
for (change_type, old, new) in changes:
1033
oldpath = decode_git_path(old[0])
1034
if oldpath in paths:
1035
ret[oldpath] = decode_git_path(new[0]) if new[0] else None
1038
if self.source.has_filename(path):
1039
if self.target.has_filename(path):
1044
raise errors.NoSuchFile(path)
1047
def find_source_paths(self, paths, recurse='none'):
1050
changes = self._iter_git_changes(
1051
specific_files=paths, include_trees=False)[0]
1052
for (change_type, old, new) in changes:
1055
newpath = decode_git_path(new[0])
1056
if newpath in paths:
1057
ret[newpath] = decode_git_path(old[0]) if old[0] else None
1060
if self.target.has_filename(path):
1061
if self.source.has_filename(path):
1066
raise errors.NoSuchFile(path)
1070
class InterGitRevisionTrees(InterGitTrees):
1071
"""InterTree that works between two git revision trees."""
1073
_matching_from_tree_format = None
1074
_matching_to_tree_format = None
1075
_test_mutable_trees_to_test_trees = None
1078
def is_compatible(cls, source, target):
1079
return (isinstance(source, GitRevisionTree) and
1080
isinstance(target, GitRevisionTree))
1082
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1083
require_versioned=True, extra_trees=None,
1084
want_unversioned=False, include_trees=True):
1085
trees = [self.source]
1086
if extra_trees is not None:
1087
trees.extend(extra_trees)
1088
if specific_files is not None:
1089
specific_files = self.target.find_related_paths_across_trees(
1090
specific_files, trees,
1091
require_versioned=require_versioned)
1093
if (self.source._repository._git.object_store !=
1094
self.target._repository._git.object_store):
1095
store = OverlayObjectStore(
1096
[self.source._repository._git.object_store,
1097
self.target._repository._git.object_store])
1099
store = self.source._repository._git.object_store
1100
rename_detector = RenameDetector(store)
1101
changes = tree_changes(
1102
store, self.source.tree, self.target.tree,
1103
want_unchanged=want_unchanged, include_trees=include_trees,
1104
change_type_same=True, rename_detector=rename_detector)
1105
return changes, set(), set()
1108
_mod_tree.InterTree.register_optimiser(InterGitRevisionTrees)
1111
class MutableGitIndexTree(mutabletree.MutableTree):
1114
self._lock_mode = None
1115
self._lock_count = 0
1116
self._versioned_dirs = None
1117
self._index_dirty = False
1118
self._submodules = None
1120
def is_versioned(self, path):
1121
with self.lock_read():
1122
path = encode_git_path(path.rstrip('/'))
1123
(index, subpath) = self._lookup_index(path)
1124
return (subpath in index or self._has_dir(path))
1126
def _has_dir(self, path):
1127
if not isinstance(path, bytes):
1128
raise TypeError(path)
1131
if self._versioned_dirs is None:
1133
return path in self._versioned_dirs
1135
def _load_dirs(self):
1136
if self._lock_mode is None:
1137
raise errors.ObjectNotLocked(self)
1138
self._versioned_dirs = set()
1139
for p, i in self._recurse_index_entries():
1140
self._ensure_versioned_dir(posixpath.dirname(p))
1142
def _ensure_versioned_dir(self, dirname):
1143
if not isinstance(dirname, bytes):
1144
raise TypeError(dirname)
1145
if dirname in self._versioned_dirs:
1148
self._ensure_versioned_dir(posixpath.dirname(dirname))
1149
self._versioned_dirs.add(dirname)
1151
def path2id(self, path):
1152
with self.lock_read():
1153
path = path.rstrip('/')
1154
if self.is_versioned(path.rstrip('/')):
1155
return self.mapping.generate_file_id(
1156
osutils.safe_unicode(path))
1159
def id2path(self, file_id, recurse='down'):
1162
if type(file_id) is not bytes:
1163
raise TypeError(file_id)
1164
with self.lock_read():
1166
path = self.mapping.parse_file_id(file_id)
1168
raise errors.NoSuchId(self, file_id)
1169
if self.is_versioned(path):
1171
raise errors.NoSuchId(self, file_id)
1173
def _set_root_id(self, file_id):
1174
raise errors.UnsupportedOperation(self._set_root_id, self)
1176
def _add(self, files, ids, kinds):
1177
for (path, file_id, kind) in zip(files, ids, kinds):
1178
if file_id is not None:
1179
raise workingtree.SettingFileIdUnsupported()
1180
path, can_access = osutils.normalized_filename(path)
1182
raise errors.InvalidNormalization(path)
1183
self._index_add_entry(path, kind)
1185
def _read_submodule_head(self, path):
1186
raise NotImplementedError(self._read_submodule_head)
1188
def _submodule_info(self):
1189
if self._submodules is None:
1191
with self.get_file('.gitmodules') as f:
1192
config = GitConfigFile.from_file(f)
1193
self._submodules = {
1194
path: (url, section)
1195
for path, url, section in parse_submodules(config)}
1196
except errors.NoSuchFile:
1197
self._submodules = {}
1198
return self._submodules
1200
def _lookup_index(self, encoded_path):
1201
if not isinstance(encoded_path, bytes):
1202
raise TypeError(encoded_path)
1204
if encoded_path in self.index:
1205
return self.index, encoded_path
1206
# TODO(jelmer): Perhaps have a cache with paths under which some
1209
remaining_path = encoded_path
1211
parts = remaining_path.split(b'/')
1212
for i in range(1, len(parts)):
1213
basepath = b'/'.join(parts[:i])
1215
(ctime, mtime, dev, ino, mode, uid, gid, size, sha,
1216
flags) = index[basepath]
1220
if S_ISGITLINK(mode):
1221
index = self._get_submodule_index(basepath)
1222
remaining_path = b'/'.join(parts[i:])
1225
return index, remaining_path
1227
return index, remaining_path
1228
return index, remaining_path
1230
def _index_del_entry(self, index, path):
1232
# TODO(jelmer): Keep track of dirty per index
1233
self._index_dirty = True
1235
def _index_add_entry(self, path, kind, flags=0, reference_revision=None):
1236
if kind == "directory":
1237
# Git indexes don't contain directories
1242
file, stat_val = self.get_file_with_stat(path)
1243
except (errors.NoSuchFile, IOError):
1244
# TODO: Rather than come up with something here, use the old
1247
stat_val = os.stat_result(
1248
(stat.S_IFREG | 0o644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1250
blob.set_raw_string(file.read())
1251
# Add object to the repository if it didn't exist yet
1252
if blob.id not in self.store:
1253
self.store.add_object(blob)
1255
elif kind == "symlink":
1258
stat_val = self._lstat(path)
1259
except EnvironmentError:
1260
# TODO: Rather than come up with something here, use the
1262
stat_val = os.stat_result(
1263
(stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1264
blob.set_raw_string(encode_git_path(self.get_symlink_target(path)))
1265
# Add object to the repository if it didn't exist yet
1266
if blob.id not in self.store:
1267
self.store.add_object(blob)
1269
elif kind == "tree-reference":
1270
if reference_revision is not None:
1271
hexsha = self.branch.lookup_bzr_revision_id(
1272
reference_revision)[0]
1274
hexsha = self._read_submodule_head(path)
1276
raise errors.NoCommits(path)
1278
stat_val = self._lstat(path)
1279
except EnvironmentError:
1280
stat_val = os.stat_result(
1281
(S_IFGITLINK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
1282
stat_val = os.stat_result((S_IFGITLINK, ) + stat_val[1:])
1284
raise AssertionError("unknown kind '%s'" % kind)
1285
# Add an entry to the index or update the existing entry
1286
ensure_normalized_path(path)
1287
encoded_path = encode_git_path(path)
1288
if b'\r' in encoded_path or b'\n' in encoded_path:
1289
# TODO(jelmer): Why do we need to do this?
1290
trace.mutter('ignoring path with invalid newline in it: %r', path)
1292
(index, index_path) = self._lookup_index(encoded_path)
1293
index[index_path] = index_entry_from_stat(stat_val, hexsha, flags)
1294
self._index_dirty = True
1295
if self._versioned_dirs is not None:
1296
self._ensure_versioned_dir(index_path)
1298
def _recurse_index_entries(self, index=None, basepath=b"",
1299
recurse_nested=False):
1300
# Iterate over all index entries
1301
with self.lock_read():
1304
for path, value in index.items():
1305
(ctime, mtime, dev, ino, mode, uid, gid, size, sha,
1307
if S_ISGITLINK(mode) and recurse_nested:
1308
subindex = self._get_submodule_index(path)
1309
for entry in self._recurse_index_entries(
1310
index=subindex, basepath=path,
1311
recurse_nested=recurse_nested):
1314
yield (posixpath.join(basepath, path), value)
1316
def iter_entries_by_dir(self, specific_files=None,
1317
recurse_nested=False):
1318
with self.lock_read():
1319
if specific_files is not None:
1320
specific_files = set(specific_files)
1322
specific_files = None
1323
root_ie = self._get_dir_ie(u"", None)
1325
if specific_files is None or u"" in specific_files:
1326
ret[(u"", u"")] = root_ie
1327
dir_ids = {u"": root_ie.file_id}
1328
for path, value in self._recurse_index_entries(
1329
recurse_nested=recurse_nested):
1330
if self.mapping.is_special_file(path):
1332
path = decode_git_path(path)
1333
if specific_files is not None and path not in specific_files:
1335
(parent, name) = posixpath.split(path)
1337
file_ie = self._get_file_ie(name, path, value, None)
1338
except errors.NoSuchFile:
1340
if specific_files is None:
1341
for (dir_path, dir_ie) in self._add_missing_parent_ids(
1343
ret[(posixpath.dirname(dir_path), dir_path)] = dir_ie
1344
file_ie.parent_id = self.path2id(parent)
1345
ret[(posixpath.dirname(path), path)] = file_ie
1346
# Special casing for directories
1348
for path in specific_files:
1349
key = (posixpath.dirname(path), path)
1350
if key not in ret and self.is_versioned(path):
1351
ret[key] = self._get_dir_ie(path, self.path2id(key[0]))
1352
return ((path, ie) for ((_, path), ie) in sorted(ret.items()))
1354
def iter_references(self):
1355
if self.supports_tree_reference():
1356
# TODO(jelmer): Implement a more efficient version of this
1357
for path, entry in self.iter_entries_by_dir():
1358
if entry.kind == 'tree-reference':
1361
def _get_dir_ie(self, path, parent_id):
1362
file_id = self.path2id(path)
1363
return GitTreeDirectory(file_id,
1364
posixpath.basename(path).strip("/"), parent_id)
1366
def _get_file_ie(self, name, path, value, parent_id):
1367
if not isinstance(name, str):
1368
raise TypeError(name)
1369
if not isinstance(path, str):
1370
raise TypeError(path)
1371
if not isinstance(value, tuple) or len(value) != 10:
1372
raise TypeError(value)
1373
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
1374
file_id = self.path2id(path)
1375
if not isinstance(file_id, bytes):
1376
raise TypeError(file_id)
1377
kind = mode_kind(mode)
1378
ie = entry_factory[kind](file_id, name, parent_id)
1379
if kind == 'symlink':
1380
ie.symlink_target = self.get_symlink_target(path)
1381
elif kind == 'tree-reference':
1382
ie.reference_revision = self.get_reference_revision(path)
1385
data = self.get_file_text(path)
1386
except errors.NoSuchFile:
1388
except IOError as e:
1389
if e.errno != errno.ENOENT:
1393
data = self.branch.repository._git.object_store[sha].data
1394
ie.text_sha1 = osutils.sha_string(data)
1395
ie.text_size = len(data)
1396
ie.executable = bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
1399
def _add_missing_parent_ids(self, path, dir_ids):
1402
parent = posixpath.dirname(path).strip("/")
1403
ret = self._add_missing_parent_ids(parent, dir_ids)
1404
parent_id = dir_ids[parent]
1405
ie = self._get_dir_ie(path, parent_id)
1406
dir_ids[path] = ie.file_id
1407
ret.append((path, ie))
1410
def _comparison_data(self, entry, path):
1412
return None, False, None
1413
return entry.kind, entry.executable, None
1415
def _unversion_path(self, path):
1416
if self._lock_mode is None:
1417
raise errors.ObjectNotLocked(self)
1418
encoded_path = encode_git_path(path)
1420
(index, subpath) = self._lookup_index(encoded_path)
1422
self._index_del_entry(index, encoded_path)
1424
# A directory, perhaps?
1425
# TODO(jelmer): Deletes that involve submodules?
1426
for p in list(index):
1427
if p.startswith(subpath + b"/"):
1429
self._index_del_entry(index, p)
1432
self._versioned_dirs = None
1435
def unversion(self, paths):
1436
with self.lock_tree_write():
1438
if self._unversion_path(path) == 0:
1439
raise errors.NoSuchFile(path)
1440
self._versioned_dirs = None
1446
def update_basis_by_delta(self, revid, delta):
1447
# TODO(jelmer): This shouldn't be called, it's inventory specific.
1448
for (old_path, new_path, file_id, ie) in delta:
1449
if old_path is not None:
1450
(index, old_subpath) = self._lookup_index(
1451
encode_git_path(old_path))
1452
if old_subpath in index:
1453
self._index_del_entry(index, old_subpath)
1454
self._versioned_dirs = None
1455
if new_path is not None and ie.kind != 'directory':
1456
self._index_add_entry(new_path, ie.kind)
1458
self._set_merges_from_parent_ids([])
1460
def move(self, from_paths, to_dir=None, after=None):
1462
with self.lock_tree_write():
1463
to_abs = self.abspath(to_dir)
1464
if not os.path.isdir(to_abs):
1465
raise errors.BzrMoveFailedError('', to_dir,
1466
errors.NotADirectory(to_abs))
1468
for from_rel in from_paths:
1469
from_tail = os.path.split(from_rel)[-1]
1470
to_rel = os.path.join(to_dir, from_tail)
1471
self.rename_one(from_rel, to_rel, after=after)
1472
rename_tuples.append((from_rel, to_rel))
1474
return rename_tuples
1476
def rename_one(self, from_rel, to_rel, after=None):
1477
from_path = encode_git_path(from_rel)
1478
to_rel, can_access = osutils.normalized_filename(to_rel)
1480
raise errors.InvalidNormalization(to_rel)
1481
to_path = encode_git_path(to_rel)
1482
with self.lock_tree_write():
1484
# Perhaps it's already moved?
1486
not self.has_filename(from_rel) and
1487
self.has_filename(to_rel) and
1488
not self.is_versioned(to_rel))
1490
if not self.has_filename(to_rel):
1491
raise errors.BzrMoveFailedError(
1492
from_rel, to_rel, errors.NoSuchFile(to_rel))
1493
if self.basis_tree().is_versioned(to_rel):
1494
raise errors.BzrMoveFailedError(
1495
from_rel, to_rel, errors.AlreadyVersionedError(to_rel))
1497
kind = self.kind(to_rel)
1500
to_kind = self.kind(to_rel)
1501
except errors.NoSuchFile:
1502
exc_type = errors.BzrRenameFailedError
1505
exc_type = errors.BzrMoveFailedError
1506
if self.is_versioned(to_rel):
1507
raise exc_type(from_rel, to_rel,
1508
errors.AlreadyVersionedError(to_rel))
1509
if not self.has_filename(from_rel):
1510
raise errors.BzrMoveFailedError(
1511
from_rel, to_rel, errors.NoSuchFile(from_rel))
1512
kind = self.kind(from_rel)
1513
if not self.is_versioned(from_rel) and kind != 'directory':
1514
raise exc_type(from_rel, to_rel,
1515
errors.NotVersionedError(from_rel))
1516
if self.has_filename(to_rel):
1517
raise errors.RenameFailedFilesExist(
1518
from_rel, to_rel, errors.FileExists(to_rel))
1520
kind = self.kind(from_rel)
1522
if not after and kind != 'directory':
1523
(index, from_subpath) = self._lookup_index(from_path)
1524
if from_subpath not in index:
1526
raise errors.BzrMoveFailedError(
1528
errors.NotVersionedError(path=from_rel))
1532
self._rename_one(from_rel, to_rel)
1533
except OSError as e:
1534
if e.errno == errno.ENOENT:
1535
raise errors.BzrMoveFailedError(
1536
from_rel, to_rel, errors.NoSuchFile(to_rel))
1538
if kind != 'directory':
1539
(index, from_index_path) = self._lookup_index(from_path)
1541
self._index_del_entry(index, from_path)
1544
self._index_add_entry(to_rel, kind)
1546
todo = [(p, i) for (p, i) in self._recurse_index_entries()
1547
if p.startswith(from_path + b'/')]
1548
for child_path, child_value in todo:
1549
(child_to_index, child_to_index_path) = self._lookup_index(
1550
posixpath.join(to_path, posixpath.relpath(child_path, from_path)))
1551
child_to_index[child_to_index_path] = child_value
1552
# TODO(jelmer): Mark individual index as dirty
1553
self._index_dirty = True
1554
(child_from_index, child_from_index_path) = self._lookup_index(
1556
self._index_del_entry(
1557
child_from_index, child_from_index_path)
1559
self._versioned_dirs = None
1562
def find_related_paths_across_trees(self, paths, trees=[],
1563
require_versioned=True):
1567
if require_versioned:
1568
trees = [self] + (trees if trees is not None else [])
1572
if t.is_versioned(p):
1577
raise errors.PathsNotVersionedError(unversioned)
1579
return filter(self.is_versioned, paths)
1581
def path_content_summary(self, path):
1582
"""See Tree.path_content_summary."""
1584
stat_result = self._lstat(path)
1585
except OSError as e:
1586
if getattr(e, 'errno', None) == errno.ENOENT:
1588
return ('missing', None, None, None)
1589
# propagate other errors
1591
kind = mode_kind(stat_result.st_mode)
1593
return self._file_content_summary(path, stat_result)
1594
elif kind == 'directory':
1595
# perhaps it looks like a plain directory, but it's really a
1597
if self._directory_is_tree_reference(path):
1598
kind = 'tree-reference'
1599
return kind, None, None, None
1600
elif kind == 'symlink':
1601
target = osutils.readlink(self.abspath(path))
1602
return ('symlink', None, None, target)
1604
return (kind, None, None, None)
1606
def stored_kind(self, relpath):
1607
(index, index_path) = self._lookup_index(encode_git_path(relpath))
1611
mode = index[index_path].mode
1615
if S_ISGITLINK(mode):
1616
return 'tree-reference'
1619
def kind(self, relpath):
1620
kind = osutils.file_kind(self.abspath(relpath))
1621
if kind == 'directory':
1622
if self._directory_is_tree_reference(relpath):
1623
return 'tree-reference'
1628
def _live_entry(self, relpath):
1629
raise NotImplementedError(self._live_entry)
1631
def transform(self, pb=None):
1632
from .transform import GitTreeTransform
1633
return GitTreeTransform(self, pb=pb)
1635
def preview_transform(self, pb=None):
1636
from .transform import GitTransformPreview
1637
return GitTransformPreview(self, pb=pb)
1640
class InterToIndexGitTree(InterGitTrees):
1641
"""InterTree that works between a Git revision tree and an index."""
1643
def __init__(self, source, target):
1644
super(InterToIndexGitTree, self).__init__(source, target)
1645
if self.source.store == self.target.store:
1646
self.store = self.source.store
1648
self.store = OverlayObjectStore(
1649
[self.source.store, self.target.store])
1650
self.rename_detector = RenameDetector(self.store)
1653
def is_compatible(cls, source, target):
1654
return (isinstance(source, GitRevisionTree) and
1655
isinstance(target, MutableGitIndexTree))
1657
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1658
require_versioned=False, extra_trees=None,
1659
want_unversioned=False, include_trees=True):
1660
trees = [self.source]
1661
if extra_trees is not None:
1662
trees.extend(extra_trees)
1663
if specific_files is not None:
1664
specific_files = self.target.find_related_paths_across_trees(
1665
specific_files, trees,
1666
require_versioned=require_versioned)
1667
# TODO(jelmer): Restrict to specific_files, for performance reasons.
1668
with self.lock_read():
1669
changes, target_extras = changes_between_git_tree_and_working_copy(
1670
self.source.store, self.source.tree,
1671
self.target, want_unchanged=want_unchanged,
1672
want_unversioned=want_unversioned,
1673
rename_detector=self.rename_detector,
1674
include_trees=include_trees)
1675
return changes, set(), target_extras
1678
_mod_tree.InterTree.register_optimiser(InterToIndexGitTree)
1681
class InterFromIndexGitTree(InterGitTrees):
1682
"""InterTree that works between a Git revision tree and an index."""
1684
def __init__(self, source, target):
1685
super(InterFromIndexGitTree, self).__init__(source, target)
1686
if self.source.store == self.target.store:
1687
self.store = self.source.store
1689
self.store = OverlayObjectStore(
1690
[self.source.store, self.target.store])
1691
self.rename_detector = RenameDetector(self.store)
1694
def is_compatible(cls, source, target):
1695
return (isinstance(target, GitRevisionTree) and
1696
isinstance(source, MutableGitIndexTree))
1698
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1699
require_versioned=False, extra_trees=None,
1700
want_unversioned=False, include_trees=True):
1701
trees = [self.source]
1702
if extra_trees is not None:
1703
trees.extend(extra_trees)
1704
if specific_files is not None:
1705
specific_files = self.target.find_related_paths_across_trees(
1706
specific_files, trees,
1707
require_versioned=require_versioned)
1708
# TODO(jelmer): Restrict to specific_files, for performance reasons.
1709
with self.lock_read():
1710
from_tree_sha, extras = snapshot_workingtree(self.source, want_unversioned=want_unversioned)
1711
return tree_changes(
1712
self.store, from_tree_sha, self.target.tree,
1713
include_trees=include_trees,
1714
rename_detector=self.rename_detector,
1715
want_unchanged=want_unchanged, change_type_same=True), extras
1718
_mod_tree.InterTree.register_optimiser(InterFromIndexGitTree)
1721
class InterIndexGitTree(InterGitTrees):
1722
"""InterTree that works between a Git revision tree and an index."""
1724
def __init__(self, source, target):
1725
super(InterIndexGitTree, self).__init__(source, target)
1726
if self.source.store == self.target.store:
1727
self.store = self.source.store
1729
self.store = OverlayObjectStore(
1730
[self.source.store, self.target.store])
1731
self.rename_detector = RenameDetector(self.store)
1734
def is_compatible(cls, source, target):
1735
return (isinstance(target, MutableGitIndexTree) and
1736
isinstance(source, MutableGitIndexTree))
1738
def _iter_git_changes(self, want_unchanged=False, specific_files=None,
1739
require_versioned=False, extra_trees=None,
1740
want_unversioned=False, include_trees=True):
1741
trees = [self.source]
1742
if extra_trees is not None:
1743
trees.extend(extra_trees)
1744
if specific_files is not None:
1745
specific_files = self.target.find_related_paths_across_trees(
1746
specific_files, trees,
1747
require_versioned=require_versioned)
1748
# TODO(jelmer): Restrict to specific_files, for performance reasons.
1749
with self.lock_read():
1750
from_tree_sha, from_extras = snapshot_workingtree(
1751
self.source, want_unversioned=want_unversioned)
1752
to_tree_sha, to_extras = snapshot_workingtree(
1753
self.target, want_unversioned=want_unversioned)
1754
changes = tree_changes(
1755
self.store, from_tree_sha, to_tree_sha,
1756
include_trees=include_trees,
1757
rename_detector=self.rename_detector,
1758
want_unchanged=want_unchanged, change_type_same=True)
1759
return changes, from_extras, to_extras
1762
_mod_tree.InterTree.register_optimiser(InterIndexGitTree)
1765
def snapshot_workingtree(target, want_unversioned=False):
1768
# Report dirified directories to commit_tree first, so that they can be
1769
# replaced with non-empty directories if they have contents.
1771
trust_executable = target._supports_executable()
1772
for path, index_entry in target._recurse_index_entries():
1774
live_entry = target._live_entry(path)
1775
except EnvironmentError as e:
1776
if e.errno == errno.ENOENT:
1777
# Entry was removed; keep it listed, but mark it as gone.
1778
blobs[path] = (ZERO_SHA, 0)
1782
if live_entry is None:
1783
# Entry was turned into a directory.
1784
# Maybe it's just a submodule that's not checked out?
1785
if S_ISGITLINK(index_entry.mode):
1786
blobs[path] = (index_entry.sha, index_entry.mode)
1788
dirified.append((path, Tree().id, stat.S_IFDIR))
1789
target.store.add_object(Tree())
1791
mode = live_entry.mode
1792
if not trust_executable:
1793
if mode_is_executable(index_entry.mode):
1797
if live_entry.sha != index_entry.sha:
1798
rp = decode_git_path(path)
1799
if stat.S_ISREG(live_entry.mode):
1801
with target.get_file(rp) as f:
1802
blob.data = f.read()
1803
elif stat.S_ISLNK(live_entry.mode):
1805
blob.data = target.get_symlink_target(rp).encode(osutils._fs_enc)
1808
if blob is not None:
1809
target.store.add_object(blob)
1810
blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
1811
if want_unversioned:
1812
for e in target._iter_files_recursive(include_dirs=False):
1814
e, accessible = osutils.normalized_filename(e)
1815
except UnicodeDecodeError:
1816
raise errors.BadFilenameEncoding(
1818
np = encode_git_path(e)
1821
st = target._lstat(e)
1822
if stat.S_ISDIR(st.st_mode):
1824
elif stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode):
1825
blob = blob_from_path_and_stat(
1826
target.abspath(e).encode(osutils._fs_enc), st)
1829
target.store.add_object(blob)
1830
blobs[np] = (blob.id, cleanup_mode(st.st_mode))
1833
target.store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()]), extras
1836
def changes_between_git_tree_and_working_copy(source_store, from_tree_sha, target,
1837
want_unchanged=False,
1838
want_unversioned=False,
1839
rename_detector=None,
1840
include_trees=True):
1841
"""Determine the changes between a git tree and a working tree with index.
1844
to_tree_sha, extras = snapshot_workingtree(target, want_unversioned=want_unversioned)
1845
store = OverlayObjectStore([source_store, target.store])
1846
return tree_changes(
1847
store, from_tree_sha, to_tree_sha, include_trees=include_trees,
1848
rename_detector=rename_detector,
1849
want_unchanged=want_unchanged, change_type_same=True), extras