/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/git/tree.py

  • Committer: Gustav Hartvigsson
  • Date: 2021-01-11 20:19:38 UTC
  • mfrom: (7526.3.2 work)
  • Revision ID: gustav.hartvigsson@gmail.com-20210111201938-omr9wjz3qdgyxe8k
MergedĀ lp:brz

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
    parse_submodules,
27
27
    ConfigFile as GitConfigFile,
28
28
    )
29
 
from dulwich.diff_tree import tree_changes
 
29
from dulwich.diff_tree import tree_changes, RenameDetector
30
30
from dulwich.errors import NotTreeError
31
31
from dulwich.index import (
32
32
    blob_from_path_and_stat,
66
66
    )
67
67
 
68
68
from .mapping import (
 
69
    encode_git_path,
 
70
    decode_git_path,
69
71
    mode_is_executable,
70
72
    mode_kind,
71
73
    default_mapping,
74
76
    TransportObjectStore,
75
77
    TransportRepo,
76
78
    )
 
79
from ..bzr.inventorytree import InventoryTreeChange
77
80
 
78
81
 
79
82
class GitTreeDirectory(_mod_tree.TreeDirectory):
80
83
 
81
 
    __slots__ = ['file_id', 'name', 'parent_id', 'children']
 
84
    __slots__ = ['file_id', 'name', 'parent_id']
82
85
 
83
86
    def __init__(self, file_id, name, parent_id):
84
87
        self.file_id = file_id
85
88
        self.name = name
86
89
        self.parent_id = parent_id
87
 
        # TODO(jelmer)
88
 
        self.children = {}
89
90
 
90
91
    @property
91
92
    def kind(self):
113
114
 
114
115
class GitTreeFile(_mod_tree.TreeFile):
115
116
 
116
 
    __slots__ = ['file_id', 'name', 'parent_id', 'text_size', 'text_sha1',
117
 
                 'executable']
 
117
    __slots__ = ['file_id', 'name', 'parent_id', 'text_size',
 
118
                 'executable', 'git_sha1']
118
119
 
119
120
    def __init__(self, file_id, name, parent_id, text_size=None,
120
 
                 text_sha1=None, executable=None):
 
121
                 git_sha1=None, executable=None):
121
122
        self.file_id = file_id
122
123
        self.name = name
123
124
        self.parent_id = parent_id
124
125
        self.text_size = text_size
125
 
        self.text_sha1 = text_sha1
 
126
        self.git_sha1 = git_sha1
126
127
        self.executable = executable
127
128
 
128
129
    @property
134
135
                self.file_id == other.file_id and
135
136
                self.name == other.name and
136
137
                self.parent_id == other.parent_id and
137
 
                self.text_sha1 == other.text_sha1 and
 
138
                self.git_sha1 == other.git_sha1 and
138
139
                self.text_size == other.text_size and
139
140
                self.executable == other.executable)
140
141
 
141
142
    def __repr__(self):
142
143
        return ("%s(file_id=%r, name=%r, parent_id=%r, text_size=%r, "
143
 
                "text_sha1=%r, executable=%r)") % (
 
144
                "git_sha1=%r, executable=%r)") % (
144
145
            type(self).__name__, self.file_id, self.name, self.parent_id,
145
 
            self.text_size, self.text_sha1, self.executable)
 
146
            self.text_size, self.git_sha1, self.executable)
146
147
 
147
148
    def copy(self):
148
149
        ret = self.__class__(
149
150
            self.file_id, self.name, self.parent_id)
150
 
        ret.text_sha1 = self.text_sha1
 
151
        ret.git_sha1 = self.git_sha1
151
152
        ret.text_size = self.text_size
152
153
        ret.executable = self.executable
153
154
        return ret
255
256
    return path
256
257
 
257
258
 
258
 
class GitRevisionTree(revisiontree.RevisionTree):
 
259
class GitTree(_mod_tree.Tree):
 
260
 
 
261
    def iter_git_objects(self):
 
262
        """Iterate over all the objects in the tree.
 
263
 
 
264
        :return :Yields tuples with (path, sha, mode)
 
265
        """
 
266
        raise NotImplementedError(self.iter_git_objects)
 
267
 
 
268
    def git_snapshot(self, want_unversioned=False):
 
269
        """Snapshot a tree, and return tree object.
 
270
 
 
271
        :return: Tree sha and set of extras
 
272
        """
 
273
        raise NotImplementedError(self.snapshot)
 
274
 
 
275
    def preview_transform(self, pb=None):
 
276
        from .transform import GitTransformPreview
 
277
        return GitTransformPreview(self, pb=pb)
 
278
 
 
279
    def find_related_paths_across_trees(self, paths, trees=[],
 
280
                                        require_versioned=True):
 
281
        if paths is None:
 
282
            return None
 
283
        if require_versioned:
 
284
            trees = [self] + (trees if trees is not None else [])
 
285
            unversioned = set()
 
286
            for p in paths:
 
287
                for t in trees:
 
288
                    if t.is_versioned(p):
 
289
                        break
 
290
                else:
 
291
                    unversioned.add(p)
 
292
            if unversioned:
 
293
                raise errors.PathsNotVersionedError(unversioned)
 
294
        return filter(self.is_versioned, paths)
 
295
 
 
296
    def _submodule_info(self):
 
297
        if self._submodules is None:
 
298
            try:
 
299
                with self.get_file('.gitmodules') as f:
 
300
                    config = GitConfigFile.from_file(f)
 
301
                    self._submodules = {
 
302
                        path: (url, section)
 
303
                        for path, url, section in parse_submodules(config)}
 
304
            except errors.NoSuchFile:
 
305
                self._submodules = {}
 
306
        return self._submodules
 
307
 
 
308
 
 
309
class GitRevisionTree(revisiontree.RevisionTree, GitTree):
259
310
    """Revision tree implementation based on Git objects."""
260
311
 
261
312
    def __init__(self, repository, revision_id):
277
328
                raise errors.NoSuchRevision(repository, revision_id)
278
329
            self.tree = commit.tree
279
330
 
280
 
    def _submodule_info(self):
281
 
        if self._submodules is None:
282
 
            try:
283
 
                with self.get_file('.gitmodules') as f:
284
 
                    config = GitConfigFile.from_file(f)
285
 
                    self._submodules = {
286
 
                        path: (url, section)
287
 
                        for path, url, section in parse_submodules(config)}
288
 
            except errors.NoSuchFile:
289
 
                self._submodules = {}
290
 
        return self._submodules
 
331
    def git_snapshot(self, want_unversioned=False):
 
332
        return self.tree, set()
291
333
 
292
334
    def _get_submodule_repository(self, relpath):
293
335
        if not isinstance(relpath, bytes):
296
338
            info = self._submodule_info()[relpath]
297
339
        except KeyError:
298
340
            nested_repo_transport = self._repository.controldir.user_transport.clone(
299
 
                relpath.decode('utf-8'))
 
341
                decode_git_path(relpath))
300
342
        else:
301
343
            nested_repo_transport = self._repository.controldir.control_transport.clone(
302
 
                posixpath.join('modules', info[1].decode('utf-8')))
 
344
                posixpath.join('modules', decode_git_path(info[1])))
303
345
        nested_controldir = _mod_controldir.ControlDir.open_from_transport(
304
346
            nested_repo_transport)
305
347
        return nested_controldir.find_repository()
308
350
        return self._get_submodule_repository(relpath)._git.object_store
309
351
 
310
352
    def get_nested_tree(self, path):
311
 
        encoded_path = path.encode('utf-8')
 
353
        encoded_path = encode_git_path(path)
312
354
        nested_repo = self._get_submodule_repository(encoded_path)
313
355
        ref_rev = self.get_reference_revision(path)
314
356
        return nested_repo.revision_tree(ref_rev)
321
363
        if self.commit_id == ZERO_SHA:
322
364
            return NULL_REVISION
323
365
        (unused_path, commit_id) = change_scanner.find_last_change_revision(
324
 
            path.encode('utf-8'), self.commit_id)
 
366
            encode_git_path(path), self.commit_id)
325
367
        return self._repository.lookup_foreign_revision_id(
326
368
            commit_id, self.mapping)
327
369
 
368
410
            tree = store[tree_id]
369
411
            for name, mode, hexsha in tree.items():
370
412
                subpath = posixpath.join(path, name)
371
 
                ret.add(subpath.decode('utf-8'))
 
413
                ret.add(decode_git_path(subpath))
372
414
                if stat.S_ISDIR(mode):
373
415
                    todo.append((store, subpath, hexsha))
374
416
        return ret
377
419
        if self.tree is None:
378
420
            raise errors.NoSuchFile(path)
379
421
 
380
 
        encoded_path = path.encode('utf-8')
 
422
        encoded_path = encode_git_path(path)
381
423
        parts = encoded_path.split(b'/')
382
424
        hexsha = self.tree
383
425
        store = self.store
419
461
        else:
420
462
            return True
421
463
 
422
 
    def _submodule_info(self):
423
 
        if self._submodules is None:
424
 
            try:
425
 
                with self.get_file('.gitmodules') as f:
426
 
                    config = GitConfigFile.from_file(f)
427
 
                    self._submodules = {
428
 
                        path: (url, section)
429
 
                        for path, url, section in parse_submodules(config)}
430
 
            except errors.NoSuchFile:
431
 
                self._submodules = {}
432
 
        return self._submodules
433
 
 
434
464
    def list_files(self, include_root=False, from_dir=None, recursive=True,
435
465
                   recurse_nested=False):
436
466
        if self.tree is None:
444
474
            parent_path = posixpath.dirname(from_dir)
445
475
            parent_id = self.mapping.generate_file_id(parent_path)
446
476
            if mode_kind(mode) == 'directory':
447
 
                root_ie = self._get_dir_ie(from_dir.encode("utf-8"), parent_id)
 
477
                root_ie = self._get_dir_ie(encode_git_path(from_dir), parent_id)
448
478
            else:
449
479
                root_ie = self._get_file_ie(
450
 
                    store, from_dir.encode("utf-8"),
 
480
                    store, encode_git_path(from_dir),
451
481
                    posixpath.basename(from_dir), mode, hexsha)
452
482
        if include_root:
453
483
            yield (from_dir, "V", root_ie.kind, root_ie)
454
484
        todo = []
455
485
        if root_ie.kind == 'directory':
456
 
            todo.append((store, from_dir.encode("utf-8"),
 
486
            todo.append((store, encode_git_path(from_dir),
457
487
                         b"", hexsha, root_ie.file_id))
458
488
        while todo:
459
489
            (store, path, relpath, hexsha, parent_id) = todo.pop()
476
506
                else:
477
507
                    ie = self._get_file_ie(
478
508
                        store, child_path, name, mode, hexsha, parent_id)
479
 
                yield (child_relpath.decode('utf-8'), "V", ie.kind, ie)
 
509
                yield (decode_git_path(child_relpath), "V", ie.kind, ie)
480
510
 
481
511
    def _get_file_ie(self, store, path, name, mode, hexsha, parent_id):
482
512
        if not isinstance(path, bytes):
484
514
        if not isinstance(name, bytes):
485
515
            raise TypeError(name)
486
516
        kind = mode_kind(mode)
487
 
        path = path.decode('utf-8')
488
 
        name = name.decode("utf-8")
 
517
        path = decode_git_path(path)
 
518
        name = decode_git_path(name)
489
519
        file_id = self.mapping.generate_file_id(path)
490
520
        ie = entry_factory[kind](file_id, name, parent_id)
491
521
        if kind == 'symlink':
492
 
            ie.symlink_target = store[hexsha].data.decode('utf-8')
 
522
            ie.symlink_target = decode_git_path(store[hexsha].data)
493
523
        elif kind == 'tree-reference':
494
524
            ie.reference_revision = self.mapping.revision_id_foreign_to_bzr(
495
525
                hexsha)
496
526
        else:
497
 
            data = store[hexsha].data
498
 
            ie.text_sha1 = osutils.sha_string(data)
499
 
            ie.text_size = len(data)
 
527
            ie.git_sha1 = hexsha
 
528
            ie.text_size = None
500
529
            ie.executable = mode_is_executable(mode)
501
530
        return ie
502
531
 
503
532
    def _get_dir_ie(self, path, parent_id):
504
 
        path = path.decode('utf-8')
 
533
        path = decode_git_path(path)
505
534
        file_id = self.mapping.generate_file_id(path)
506
535
        return GitTreeDirectory(file_id, posixpath.basename(path), parent_id)
507
536
 
511
540
        if mode is not None and not stat.S_ISDIR(mode):
512
541
            return
513
542
 
514
 
        encoded_path = path.encode('utf-8')
 
543
        encoded_path = encode_git_path(path)
515
544
        file_id = self.path2id(path)
516
545
        tree = store[tree_sha]
517
546
        for name, mode, hexsha in tree.iteritems():
532
561
            if specific_files in ([""], []):
533
562
                specific_files = None
534
563
            else:
535
 
                specific_files = set([p.encode('utf-8')
 
564
                specific_files = set([encode_git_path(p)
536
565
                                      for p in specific_files])
537
566
        todo = deque([(self.store, b"", self.tree, self.path2id(''))])
538
567
        if specific_files is None or u"" in specific_files:
545
574
                if self.mapping.is_special_file(name):
546
575
                    continue
547
576
                child_path = posixpath.join(path, name)
548
 
                child_path_decoded = child_path.decode('utf-8')
 
577
                child_path_decoded = decode_git_path(child_path)
549
578
                if recurse_nested and S_ISGITLINK(mode):
550
579
                    mode = stat.S_IFDIR
551
580
                    store = self._get_submodule_store(child_path)
604
633
        """See RevisionTree.get_symlink_target."""
605
634
        (store, mode, hexsha) = self._lookup_path(path)
606
635
        if stat.S_ISLNK(mode):
607
 
            return store[hexsha].data.decode('utf-8')
 
636
            return decode_git_path(store[hexsha].data)
608
637
        else:
609
638
            return None
610
639
 
613
642
        (store, mode, hexsha) = self._lookup_path(path)
614
643
        if S_ISGITLINK(mode):
615
644
            try:
616
 
                nested_repo = self._get_submodule_repository(path.encode('utf-8'))
 
645
                nested_repo = self._get_submodule_repository(encode_git_path(path))
617
646
            except errors.NotBranchError:
618
647
                return self.mapping.revision_id_foreign_to_bzr(hexsha)
619
648
            else:
639
668
            return (kind, len(contents), executable,
640
669
                    osutils.sha_string(contents))
641
670
        elif kind == 'symlink':
642
 
            return (kind, None, None, store[hexsha].data.decode('utf-8'))
 
671
            return (kind, None, None, decode_git_path(store[hexsha].data))
643
672
        elif kind == 'tree-reference':
644
 
            nested_repo = self._get_submodule_repository(path.encode('utf-8'))
 
673
            nested_repo = self._get_submodule_repository(encode_git_path(path))
645
674
            return (kind, None, None,
646
675
                    nested_repo.lookup_foreign_revision_id(hexsha))
647
676
        else:
648
677
            return (kind, None, None, None)
649
678
 
650
 
    def find_related_paths_across_trees(self, paths, trees=[],
651
 
                                        require_versioned=True):
652
 
        if paths is None:
653
 
            return None
654
 
        if require_versioned:
655
 
            trees = [self] + (trees if trees is not None else [])
656
 
            unversioned = set()
657
 
            for p in paths:
658
 
                for t in trees:
659
 
                    if t.is_versioned(p):
660
 
                        break
661
 
                else:
662
 
                    unversioned.add(p)
663
 
            if unversioned:
664
 
                raise errors.PathsNotVersionedError(unversioned)
665
 
        return filter(self.is_versioned, paths)
666
 
 
667
679
    def _iter_tree_contents(self, include_trees=False):
668
680
        if self.tree is None:
669
681
            return iter([])
697
709
    def walkdirs(self, prefix=u""):
698
710
        (store, mode, hexsha) = self._lookup_path(prefix)
699
711
        todo = deque(
700
 
            [(store, prefix.encode('utf-8'), hexsha, self.path2id(prefix))])
 
712
            [(store, encode_git_path(prefix), hexsha)])
701
713
        while todo:
702
 
            store, path, tree_sha, parent_id = todo.popleft()
703
 
            path_decoded = path.decode('utf-8')
 
714
            store, path, tree_sha = todo.popleft()
 
715
            path_decoded = decode_git_path(path)
704
716
            tree = store[tree_sha]
705
717
            children = []
706
718
            for name, mode, hexsha in tree.iteritems():
707
719
                if self.mapping.is_special_file(name):
708
720
                    continue
709
721
                child_path = posixpath.join(path, name)
710
 
                file_id = self.path2id(child_path.decode('utf-8'))
711
722
                if stat.S_ISDIR(mode):
712
 
                    todo.append((store, child_path, hexsha, file_id))
 
723
                    todo.append((store, child_path, hexsha))
713
724
                children.append(
714
 
                    (child_path.decode('utf-8'), name.decode('utf-8'),
 
725
                    (decode_git_path(child_path), decode_git_path(name),
715
726
                        mode_kind(mode), None,
716
 
                        file_id, mode_kind(mode)))
717
 
            yield (path_decoded, parent_id), children
 
727
                        mode_kind(mode)))
 
728
            yield path_decoded, children
718
729
 
719
730
 
720
731
def tree_delta_from_git_changes(changes, mappings,
721
732
                                specific_files=None,
722
733
                                require_versioned=False, include_root=False,
723
 
                                target_extras=None):
 
734
                                source_extras=None, target_extras=None):
724
735
    """Create a TreeDelta from two git trees.
725
736
 
726
737
    source and target are iterators over tuples with:
729
740
    (old_mapping, new_mapping) = mappings
730
741
    if target_extras is None:
731
742
        target_extras = set()
 
743
    if source_extras is None:
 
744
        source_extras = set()
732
745
    ret = delta.TreeDelta()
733
746
    added = []
734
 
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
 
747
    for (change_type, old, new) in changes:
 
748
        (oldpath, oldmode, oldsha) = old
 
749
        (newpath, newmode, newsha) = new
735
750
        if newpath == b'' and not include_root:
736
751
            continue
 
752
        copied = (change_type == 'copy')
737
753
        if oldpath is not None:
738
 
            oldpath_decoded = oldpath.decode('utf-8')
 
754
            oldpath_decoded = decode_git_path(oldpath)
739
755
        else:
740
756
            oldpath_decoded = None
741
757
        if newpath is not None:
742
 
            newpath_decoded = newpath.decode('utf-8')
 
758
            newpath_decoded = decode_git_path(newpath)
743
759
        else:
744
760
            newpath_decoded = None
745
761
        if not (specific_files is None or
751
767
                        specific_files, newpath_decoded))):
752
768
            continue
753
769
 
754
 
        if oldpath_decoded is None:
755
 
            fileid = new_mapping.generate_file_id(newpath_decoded)
 
770
        if oldpath is None:
756
771
            oldexe = None
757
772
            oldkind = None
758
773
            oldname = None
759
774
            oldparent = None
760
775
            oldversioned = False
761
776
        else:
762
 
            oldversioned = True
 
777
            oldversioned = (oldpath not in source_extras)
763
778
            if oldmode:
764
779
                oldexe = mode_is_executable(oldmode)
765
780
                oldkind = mode_kind(oldmode)
766
781
            else:
767
782
                oldexe = False
768
783
                oldkind = None
769
 
            if oldpath_decoded == u'':
 
784
            if oldpath == b'':
770
785
                oldparent = None
771
786
                oldname = u''
772
787
            else:
773
788
                (oldparentpath, oldname) = osutils.split(oldpath_decoded)
774
789
                oldparent = old_mapping.generate_file_id(oldparentpath)
 
790
        if newpath is None:
 
791
            newexe = None
 
792
            newkind = None
 
793
            newname = None
 
794
            newparent = None
 
795
            newversioned = False
 
796
        else:
 
797
            newversioned = (newpath not in target_extras)
 
798
            if newmode:
 
799
                newexe = mode_is_executable(newmode)
 
800
                newkind = mode_kind(newmode)
 
801
            else:
 
802
                newexe = False
 
803
                newkind = None
 
804
            if newpath_decoded == u'':
 
805
                newparent = None
 
806
                newname = u''
 
807
            else:
 
808
                newparentpath, newname = osutils.split(newpath_decoded)
 
809
                newparent = new_mapping.generate_file_id(newparentpath)
 
810
        if oldversioned and not copied:
775
811
            fileid = old_mapping.generate_file_id(oldpath_decoded)
776
 
        if newpath_decoded is None:
777
 
            newexe = None
778
 
            newkind = None
779
 
            newname = None
780
 
            newparent = None
781
 
            newversioned = False
 
812
        elif newversioned:
 
813
            fileid = new_mapping.generate_file_id(newpath_decoded)
782
814
        else:
783
 
            newversioned = (newpath_decoded not in target_extras)
784
 
            if newmode:
785
 
                newexe = mode_is_executable(newmode)
786
 
                newkind = mode_kind(newmode)
787
 
            else:
788
 
                newexe = False
789
 
                newkind = None
790
 
            if newpath_decoded == u'':
791
 
                newparent = None
792
 
                newname = u''
793
 
            else:
794
 
                newparentpath, newname = osutils.split(newpath_decoded)
795
 
                newparent = new_mapping.generate_file_id(newparentpath)
 
815
            fileid = None
796
816
        if old_mapping.is_special_file(oldpath):
797
817
            oldpath = None
798
818
        if new_mapping.is_special_file(newpath):
799
819
            newpath = None
800
820
        if oldpath is None and newpath is None:
801
821
            continue
802
 
        change = _mod_tree.TreeChange(
 
822
        change = InventoryTreeChange(
803
823
            fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
804
824
            (oldversioned, newversioned),
805
825
            (oldparent, newparent), (oldname, newname),
806
 
            (oldkind, newkind), (oldexe, newexe))
807
 
        if oldpath is None:
 
826
            (oldkind, newkind), (oldexe, newexe),
 
827
            copied=copied)
 
828
        if newpath is not None and not newversioned and newkind != 'directory':
 
829
            change.file_id = None
 
830
            ret.unversioned.append(change)
 
831
        elif change_type == 'add':
808
832
            added.append((newpath, newkind))
809
833
        elif newpath is None or newmode == 0:
810
834
            ret.removed.append(change)
811
 
        elif oldpath != newpath:
 
835
        elif change_type == 'delete':
 
836
            ret.removed.append(change)
 
837
        elif change_type == 'copy':
 
838
            if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
 
839
                continue
 
840
            ret.copied.append(change)
 
841
        elif change_type == 'rename':
 
842
            if stat.S_ISDIR(oldmode) and stat.S_ISDIR(newmode):
 
843
                continue
812
844
            ret.renamed.append(change)
813
845
        elif mode_kind(oldmode) != mode_kind(newmode):
814
846
            ret.kind_changed.append(change)
828
860
    for path, kind in added:
829
861
        if kind == 'directory' and path not in implicit_dirs:
830
862
            continue
831
 
        path_decoded = osutils.normalized_filename(path)[0]
 
863
        path_decoded = decode_git_path(path)
832
864
        parent_path, basename = osutils.split(path_decoded)
833
865
        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),
 
866
        file_id = new_mapping.generate_file_id(path_decoded)
 
867
        ret.added.append(
 
868
            InventoryTreeChange(
 
869
                file_id, (None, path_decoded), True,
 
870
                (False, True),
 
871
                (None, parent_id),
838
872
                (None, basename), (None, kind), (None, False)))
839
 
        else:
840
 
            file_id = new_mapping.generate_file_id(path_decoded)
841
 
            ret.added.append(
842
 
                _mod_tree.TreeChange(
843
 
                    file_id, (None, path_decoded), True,
844
 
                    (False, True),
845
 
                    (None, parent_id),
846
 
                    (None, basename), (None, kind), (None, False)))
847
873
 
848
874
    return ret
849
875
 
850
876
 
851
877
def changes_from_git_changes(changes, mapping, specific_files=None,
852
 
                             include_unchanged=False, target_extras=None):
 
878
                             include_unchanged=False, source_extras=None,
 
879
                             target_extras=None):
853
880
    """Create a iter_changes-like generator from a git stream.
854
881
 
855
882
    source and target are iterators over tuples with:
857
884
    """
858
885
    if target_extras is None:
859
886
        target_extras = set()
860
 
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
 
887
    if source_extras is None:
 
888
        source_extras = set()
 
889
    for (change_type, old, new) in changes:
 
890
        if change_type == 'unchanged' and not include_unchanged:
 
891
            continue
 
892
        (oldpath, oldmode, oldsha) = old
 
893
        (newpath, newmode, newsha) = new
861
894
        if oldpath is not None:
862
 
            oldpath_decoded = oldpath.decode('utf-8')
 
895
            oldpath_decoded = decode_git_path(oldpath)
863
896
        else:
864
897
            oldpath_decoded = None
865
898
        if newpath is not None:
866
 
            newpath_decoded = newpath.decode('utf-8')
 
899
            newpath_decoded = decode_git_path(newpath)
867
900
        else:
868
901
            newpath_decoded = None
869
902
        if not (specific_files is None or
878
911
            continue
879
912
        if newpath is not None and mapping.is_special_file(newpath):
880
913
            continue
881
 
        if oldpath_decoded is None:
882
 
            fileid = mapping.generate_file_id(newpath_decoded)
 
914
        if oldpath is None:
883
915
            oldexe = None
884
916
            oldkind = None
885
917
            oldname = None
886
918
            oldparent = None
887
919
            oldversioned = False
888
920
        else:
889
 
            oldversioned = True
 
921
            oldversioned = (oldpath not in source_extras)
890
922
            if oldmode:
891
923
                oldexe = mode_is_executable(oldmode)
892
924
                oldkind = mode_kind(oldmode)
899
931
            else:
900
932
                (oldparentpath, oldname) = osutils.split(oldpath_decoded)
901
933
                oldparent = mapping.generate_file_id(oldparentpath)
902
 
            fileid = mapping.generate_file_id(oldpath_decoded)
903
 
        if newpath_decoded is None:
 
934
        if newpath is None:
904
935
            newexe = None
905
936
            newkind = None
906
937
            newname = None
907
938
            newparent = None
908
939
            newversioned = False
909
940
        else:
910
 
            newversioned = (newpath_decoded not in target_extras)
 
941
            newversioned = (newpath not in target_extras)
911
942
            if newmode:
912
943
                newexe = mode_is_executable(newmode)
913
944
                newkind = mode_kind(newmode)
921
952
                newparentpath, newname = osutils.split(newpath_decoded)
922
953
                newparent = mapping.generate_file_id(newparentpath)
923
954
        if (not include_unchanged and
924
 
            oldkind == 'directory' and newkind == 'directory' and
 
955
                oldkind == 'directory' and newkind == 'directory' and
925
956
                oldpath_decoded == newpath_decoded):
926
957
            continue
927
 
        yield _mod_tree.TreeChange(
928
 
            fileid, (oldpath_decoded, newpath_decoded), (oldsha != newsha),
 
958
        if oldversioned and change_type != 'copy':
 
959
            fileid = mapping.generate_file_id(oldpath_decoded)
 
960
        elif newversioned:
 
961
            fileid = mapping.generate_file_id(newpath_decoded)
 
962
        else:
 
963
            fileid = None
 
964
        if oldkind == 'directory' and newkind == 'directory':
 
965
            modified = False
 
966
        else:
 
967
            modified = (oldsha != newsha) or (oldmode != newmode)
 
968
        yield InventoryTreeChange(
 
969
            fileid, (oldpath_decoded, newpath_decoded),
 
970
            modified,
929
971
            (oldversioned, newversioned),
930
972
            (oldparent, newparent), (oldname, newname),
931
 
            (oldkind, newkind), (oldexe, newexe))
 
973
            (oldkind, newkind), (oldexe, newexe),
 
974
            copied=(change_type == 'copy'))
932
975
 
933
976
 
934
977
class InterGitTrees(_mod_tree.InterTree):
938
981
    _matching_to_tree_format = None
939
982
    _test_mutable_trees_to_test_trees = None
940
983
 
 
984
    def __init__(self, source, target):
 
985
        super(InterGitTrees, self).__init__(source, target)
 
986
        if self.source.store == self.target.store:
 
987
            self.store = self.source.store
 
988
        else:
 
989
            self.store = OverlayObjectStore(
 
990
                [self.source.store, self.target.store])
 
991
        self.rename_detector = RenameDetector(self.store)
 
992
 
941
993
    @classmethod
942
994
    def is_compatible(cls, source, target):
943
 
        return (isinstance(source, GitRevisionTree) and
944
 
                isinstance(target, GitRevisionTree))
 
995
        return isinstance(source, GitTree) and isinstance(target, GitTree)
945
996
 
946
997
    def compare(self, want_unchanged=False, specific_files=None,
947
998
                extra_trees=None, require_versioned=False, include_root=False,
948
999
                want_unversioned=False):
949
1000
        with self.lock_read():
950
 
            changes, target_extras = self._iter_git_changes(
 
1001
            changes, source_extras, target_extras = self._iter_git_changes(
951
1002
                want_unchanged=want_unchanged,
952
1003
                require_versioned=require_versioned,
953
1004
                specific_files=specific_files,
956
1007
            return tree_delta_from_git_changes(
957
1008
                changes, (self.source.mapping, self.target.mapping),
958
1009
                specific_files=specific_files,
959
 
                include_root=include_root, target_extras=target_extras)
 
1010
                include_root=include_root,
 
1011
                source_extras=source_extras, target_extras=target_extras)
960
1012
 
961
1013
    def iter_changes(self, include_unchanged=False, specific_files=None,
962
1014
                     pb=None, extra_trees=[], require_versioned=True,
963
1015
                     want_unversioned=False):
964
1016
        with self.lock_read():
965
 
            changes, target_extras = self._iter_git_changes(
 
1017
            changes, source_extras, target_extras = self._iter_git_changes(
966
1018
                want_unchanged=include_unchanged,
967
1019
                require_versioned=require_versioned,
968
1020
                specific_files=specific_files,
972
1024
                changes, self.target.mapping,
973
1025
                specific_files=specific_files,
974
1026
                include_unchanged=include_unchanged,
 
1027
                source_extras=source_extras,
975
1028
                target_extras=target_extras)
976
1029
 
977
1030
    def _iter_git_changes(self, want_unchanged=False, specific_files=None,
978
1031
                          require_versioned=False, extra_trees=None,
979
 
                          want_unversioned=False):
980
 
        raise NotImplementedError(self._iter_git_changes)
 
1032
                          want_unversioned=False, include_trees=True):
 
1033
        trees = [self.source]
 
1034
        if extra_trees is not None:
 
1035
            trees.extend(extra_trees)
 
1036
        if specific_files is not None:
 
1037
            specific_files = self.target.find_related_paths_across_trees(
 
1038
                specific_files, trees,
 
1039
                require_versioned=require_versioned)
 
1040
        # TODO(jelmer): Restrict to specific_files, for performance reasons.
 
1041
        with self.lock_read():
 
1042
            from_tree_sha, from_extras = self.source.git_snapshot(
 
1043
                want_unversioned=want_unversioned)
 
1044
            to_tree_sha, to_extras = self.target.git_snapshot(
 
1045
                want_unversioned=want_unversioned)
 
1046
            changes = tree_changes(
 
1047
                self.store, from_tree_sha, to_tree_sha,
 
1048
                include_trees=include_trees,
 
1049
                rename_detector=self.rename_detector,
 
1050
                want_unchanged=want_unchanged, change_type_same=True)
 
1051
            return changes, from_extras, to_extras
981
1052
 
982
1053
    def find_target_path(self, path, recurse='none'):
983
1054
        ret = self.find_target_paths([path], recurse=recurse)
990
1061
    def find_target_paths(self, paths, recurse='none'):
991
1062
        paths = set(paths)
992
1063
        ret = {}
993
 
        changes = self._iter_git_changes(specific_files=paths)[0]
994
 
        for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
 
1064
        changes = self._iter_git_changes(
 
1065
            specific_files=paths, include_trees=False)[0]
 
1066
        for (change_type, old, new) in changes:
 
1067
            if old[0] is None:
 
1068
                continue
 
1069
            oldpath = decode_git_path(old[0])
995
1070
            if oldpath in paths:
996
 
                ret[oldpath] = newpath
 
1071
                ret[oldpath] = decode_git_path(new[0]) if new[0] else None
997
1072
        for path in paths:
998
1073
            if path not in ret:
999
1074
                if self.source.has_filename(path):
1008
1083
    def find_source_paths(self, paths, recurse='none'):
1009
1084
        paths = set(paths)
1010
1085
        ret = {}
1011
 
        changes = self._iter_git_changes(specific_files=paths)[0]
1012
 
        for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
 
1086
        changes = self._iter_git_changes(
 
1087
            specific_files=paths, include_trees=False)[0]
 
1088
        for (change_type, old, new) in changes:
 
1089
            if new[0] is None:
 
1090
                continue
 
1091
            newpath = decode_git_path(new[0])
1013
1092
            if newpath in paths:
1014
 
                ret[newpath] = oldpath
 
1093
                ret[newpath] = decode_git_path(old[0]) if old[0] else None
1015
1094
        for path in paths:
1016
1095
            if path not in ret:
1017
1096
                if self.target.has_filename(path):
1024
1103
        return ret
1025
1104
 
1026
1105
 
1027
 
class InterGitRevisionTrees(InterGitTrees):
1028
 
    """InterTree that works between two git revision trees."""
1029
 
 
1030
 
    _matching_from_tree_format = None
1031
 
    _matching_to_tree_format = None
1032
 
    _test_mutable_trees_to_test_trees = None
1033
 
 
1034
 
    @classmethod
1035
 
    def is_compatible(cls, source, target):
1036
 
        return (isinstance(source, GitRevisionTree) and
1037
 
                isinstance(target, GitRevisionTree))
1038
 
 
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)
1049
 
 
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])
1055
 
        else:
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()
1060
 
 
1061
 
 
1062
 
_mod_tree.InterTree.register_optimiser(InterGitRevisionTrees)
1063
 
 
1064
 
 
1065
 
class MutableGitIndexTree(mutabletree.MutableTree):
 
1106
_mod_tree.InterTree.register_optimiser(InterGitTrees)
 
1107
 
 
1108
 
 
1109
class MutableGitIndexTree(mutabletree.MutableTree, GitTree):
1066
1110
 
1067
1111
    def __init__(self):
1068
1112
        self._lock_mode = None
1071
1115
        self._index_dirty = False
1072
1116
        self._submodules = None
1073
1117
 
 
1118
    def git_snapshot(self, want_unversioned=False):
 
1119
        return snapshot_workingtree(self, want_unversioned=want_unversioned)
 
1120
 
1074
1121
    def is_versioned(self, path):
1075
1122
        with self.lock_read():
1076
 
            path = path.rstrip('/').encode('utf-8')
 
1123
            path = encode_git_path(path.rstrip('/'))
1077
1124
            (index, subpath) = self._lookup_index(path)
1078
1125
            return (subpath in index or self._has_dir(path))
1079
1126
 
1090
1137
        if self._lock_mode is None:
1091
1138
            raise errors.ObjectNotLocked(self)
1092
1139
        self._versioned_dirs = set()
1093
 
        for p, i in self._recurse_index_entries():
 
1140
        for p, sha, mode in self.iter_git_objects():
1094
1141
            self._ensure_versioned_dir(posixpath.dirname(p))
1095
1142
 
1096
1143
    def _ensure_versioned_dir(self, dirname):
1139
1186
    def _read_submodule_head(self, path):
1140
1187
        raise NotImplementedError(self._read_submodule_head)
1141
1188
 
1142
 
    def _submodule_info(self):
1143
 
        if self._submodules is None:
1144
 
            try:
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
1153
 
 
1154
1189
    def _lookup_index(self, encoded_path):
1155
1190
        if not isinstance(encoded_path, bytes):
1156
1191
            raise TypeError(encoded_path)
1186
1221
        # TODO(jelmer): Keep track of dirty per index
1187
1222
        self._index_dirty = True
1188
1223
 
1189
 
    def _index_add_entry(self, path, kind, flags=0, reference_revision=None):
 
1224
    def _apply_index_changes(self, changes):
 
1225
        for (path, kind, executability, reference_revision,
 
1226
             symlink_target) in changes:
 
1227
            if kind is None or kind == 'directory':
 
1228
                (index, subpath) = self._lookup_index(
 
1229
                    encode_git_path(path))
 
1230
                try:
 
1231
                    self._index_del_entry(index, subpath)
 
1232
                except KeyError:
 
1233
                    pass
 
1234
                else:
 
1235
                    self._versioned_dirs = None
 
1236
            else:
 
1237
                self._index_add_entry(
 
1238
                    path, kind,
 
1239
                    reference_revision=reference_revision,
 
1240
                    symlink_target=symlink_target)
 
1241
        self.flush()
 
1242
 
 
1243
    def _index_add_entry(
 
1244
            self, path, kind, flags=0, reference_revision=None,
 
1245
            symlink_target=None):
1190
1246
        if kind == "directory":
1191
1247
            # Git indexes don't contain directories
1192
1248
            return
1193
 
        if kind == "file":
 
1249
        elif kind == "file":
1194
1250
            blob = Blob()
1195
1251
            try:
1196
1252
                file, stat_val = self.get_file_with_stat(path)
1215
1271
                # old index
1216
1272
                stat_val = os.stat_result(
1217
1273
                    (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"))
 
1274
            if symlink_target is None:
 
1275
                symlink_target = self.get_symlink_target(path)
 
1276
            blob.set_raw_string(encode_git_path(symlink_target))
1220
1277
            # Add object to the repository if it didn't exist yet
1221
1278
            if blob.id not in self.store:
1222
1279
                self.store.add_object(blob)
1239
1296
            raise AssertionError("unknown kind '%s'" % kind)
1240
1297
        # Add an entry to the index or update the existing entry
1241
1298
        ensure_normalized_path(path)
1242
 
        encoded_path = path.encode("utf-8")
 
1299
        encoded_path = encode_git_path(path)
1243
1300
        if b'\r' in encoded_path or b'\n' in encoded_path:
1244
1301
            # TODO(jelmer): Why do we need to do this?
1245
1302
            trace.mutter('ignoring path with invalid newline in it: %r', path)
1250
1307
        if self._versioned_dirs is not None:
1251
1308
            self._ensure_versioned_dir(index_path)
1252
1309
 
 
1310
    def iter_git_objects(self):
 
1311
        for p, entry in self._recurse_index_entries():
 
1312
            yield p, entry.sha, entry.mode
 
1313
 
1253
1314
    def _recurse_index_entries(self, index=None, basepath=b"",
1254
1315
                               recurse_nested=False):
1255
1316
        # Iterate over all index entries
1284
1345
                    recurse_nested=recurse_nested):
1285
1346
                if self.mapping.is_special_file(path):
1286
1347
                    continue
1287
 
                path = path.decode("utf-8")
 
1348
                path = decode_git_path(path)
1288
1349
                if specific_files is not None and path not in specific_files:
1289
1350
                    continue
1290
1351
                (parent, name) = posixpath.split(path)
1336
1397
        elif kind == 'tree-reference':
1337
1398
            ie.reference_revision = self.get_reference_revision(path)
1338
1399
        else:
1339
 
            try:
1340
 
                data = self.get_file_text(path)
1341
 
            except errors.NoSuchFile:
1342
 
                data = None
1343
 
            except IOError as e:
1344
 
                if e.errno != errno.ENOENT:
1345
 
                    raise
1346
 
                data = None
1347
 
            if data is None:
1348
 
                data = self.branch.repository._git.object_store[sha].data
1349
 
            ie.text_sha1 = osutils.sha_string(data)
1350
 
            ie.text_size = len(data)
 
1400
            ie.git_sha1 = sha
 
1401
            ie.text_size = size
1351
1402
            ie.executable = bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
1352
1403
        return ie
1353
1404
 
1370
1421
    def _unversion_path(self, path):
1371
1422
        if self._lock_mode is None:
1372
1423
            raise errors.ObjectNotLocked(self)
1373
 
        encoded_path = path.encode("utf-8")
 
1424
        encoded_path = encode_git_path(path)
1374
1425
        count = 0
1375
1426
        (index, subpath) = self._lookup_index(encoded_path)
1376
1427
        try:
1403
1454
        for (old_path, new_path, file_id, ie) in delta:
1404
1455
            if old_path is not None:
1405
1456
                (index, old_subpath) = self._lookup_index(
1406
 
                    old_path.encode('utf-8'))
 
1457
                    encode_git_path(old_path))
1407
1458
                if old_subpath in index:
1408
1459
                    self._index_del_entry(index, old_subpath)
1409
1460
                    self._versioned_dirs = None
1429
1480
            return rename_tuples
1430
1481
 
1431
1482
    def rename_one(self, from_rel, to_rel, after=None):
1432
 
        from_path = from_rel.encode("utf-8")
 
1483
        from_path = encode_git_path(from_rel)
1433
1484
        to_rel, can_access = osutils.normalized_filename(to_rel)
1434
1485
        if not can_access:
1435
1486
            raise errors.InvalidNormalization(to_rel)
1436
 
        to_path = to_rel.encode("utf-8")
 
1487
        to_path = encode_git_path(to_rel)
1437
1488
        with self.lock_tree_write():
1438
1489
            if not after:
1439
1490
                # Perhaps it's already moved?
1514
1565
            self._versioned_dirs = None
1515
1566
            self.flush()
1516
1567
 
1517
 
    def find_related_paths_across_trees(self, paths, trees=[],
1518
 
                                        require_versioned=True):
1519
 
        if paths is None:
1520
 
            return None
1521
 
 
1522
 
        if require_versioned:
1523
 
            trees = [self] + (trees if trees is not None else [])
1524
 
            unversioned = set()
1525
 
            for p in paths:
1526
 
                for t in trees:
1527
 
                    if t.is_versioned(p):
1528
 
                        break
1529
 
                else:
1530
 
                    unversioned.add(p)
1531
 
            if unversioned:
1532
 
                raise errors.PathsNotVersionedError(unversioned)
1533
 
 
1534
 
        return filter(self.is_versioned, paths)
1535
 
 
1536
1568
    def path_content_summary(self, path):
1537
1569
        """See Tree.path_content_summary."""
1538
1570
        try:
1559
1591
            return (kind, None, None, None)
1560
1592
 
1561
1593
    def stored_kind(self, relpath):
1562
 
        (index, index_path) = self._lookup_index(relpath.encode('utf-8'))
 
1594
        if relpath == '':
 
1595
            return 'directory'
 
1596
        (index, index_path) = self._lookup_index(encode_git_path(relpath))
1563
1597
        if index is None:
1564
 
            return kind
 
1598
            return None
1565
1599
        try:
1566
1600
            mode = index[index_path].mode
1567
1601
        except KeyError:
1568
 
            return kind
 
1602
            for p in index:
 
1603
                if osutils.is_inside(
 
1604
                        decode_git_path(index_path), decode_git_path(p)):
 
1605
                    return 'directory'
 
1606
            return None
1569
1607
        else:
1570
 
            if S_ISGITLINK(mode):
1571
 
                return 'tree-reference'
1572
 
            return 'directory'
 
1608
            return mode_kind(mode)
1573
1609
 
1574
1610
    def kind(self, relpath):
1575
1611
        kind = osutils.file_kind(self.abspath(relpath))
1583
1619
    def _live_entry(self, relpath):
1584
1620
        raise NotImplementedError(self._live_entry)
1585
1621
 
1586
 
    def get_transform(self, pb=None):
1587
 
        from ..transform import TreeTransform
1588
 
        return TreeTransform(self, pb=pb)
1589
 
 
1590
 
 
1591
 
 
1592
 
class InterIndexGitTree(InterGitTrees):
1593
 
    """InterTree that works between a Git revision tree and an index."""
1594
 
 
1595
 
    def __init__(self, source, target):
1596
 
        super(InterIndexGitTree, self).__init__(source, target)
1597
 
        self._index = target.index
1598
 
 
1599
 
    @classmethod
1600
 
    def is_compatible(cls, source, target):
1601
 
        return (isinstance(source, GitRevisionTree) and
1602
 
                isinstance(target, MutableGitIndexTree))
1603
 
 
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.
 
1622
    def transform(self, pb=None):
 
1623
        from .transform import GitTreeTransform
 
1624
        return GitTreeTransform(self, pb=pb)
 
1625
 
 
1626
    def has_changes(self, _from_tree=None):
 
1627
        """Quickly check that the tree contains at least one commitable change.
 
1628
 
 
1629
        :param _from_tree: tree to compare against to find changes (default to
 
1630
            the basis tree and is intended to be used by tests).
 
1631
 
 
1632
        :return: True if a change is found. False otherwise
 
1633
        """
1615
1634
        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)
1620
 
 
1621
 
 
1622
 
_mod_tree.InterTree.register_optimiser(InterIndexGitTree)
1623
 
 
1624
 
 
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.
1629
 
 
1630
 
    """
 
1635
            # Check pending merges
 
1636
            if len(self.get_parent_ids()) > 1:
 
1637
                return True
 
1638
            if _from_tree is None:
 
1639
                _from_tree = self.basis_tree()
 
1640
            changes = self.iter_changes(_from_tree)
 
1641
            if self.supports_symlinks():
 
1642
                # Fast path for has_changes.
 
1643
                try:
 
1644
                    change = next(changes)
 
1645
                    if change.path[1] == '':
 
1646
                        next(changes)
 
1647
                    return True
 
1648
                except StopIteration:
 
1649
                    # No changes
 
1650
                    return False
 
1651
            else:
 
1652
                # Slow path for has_changes.
 
1653
                # Handle platforms that do not support symlinks in the
 
1654
                # conditional below. This is slower than the try/except
 
1655
                # approach below that but we don't have a choice as we
 
1656
                # need to be sure that all symlinks are removed from the
 
1657
                # entire changeset. This is because in platforms that
 
1658
                # do not support symlinks, they show up as None in the
 
1659
                # working copy as compared to the repository.
 
1660
                # Also, exclude root as mention in the above fast path.
 
1661
                changes = filter(
 
1662
                    lambda c: c[6][0] != 'symlink' and c[4] != (None, None),
 
1663
                    changes)
 
1664
                try:
 
1665
                    next(iter(changes))
 
1666
                except StopIteration:
 
1667
                    return False
 
1668
                return True
 
1669
 
 
1670
 
 
1671
def snapshot_workingtree(target, want_unversioned=False):
1631
1672
    extras = set()
1632
1673
    blobs = {}
1633
1674
    # Report dirified directories to commit_tree first, so that they can be
1651
1692
                    blobs[path] = (index_entry.sha, index_entry.mode)
1652
1693
                else:
1653
1694
                    dirified.append((path, Tree().id, stat.S_IFDIR))
1654
 
                    store.add_object(Tree())
 
1695
                    target.store.add_object(Tree())
1655
1696
            else:
1656
1697
                mode = live_entry.mode
1657
1698
                if not trust_executable:
1659
1700
                        mode |= 0o111
1660
1701
                    else:
1661
1702
                        mode &= ~0o111
 
1703
                if live_entry.sha != index_entry.sha:
 
1704
                    rp = decode_git_path(path)
 
1705
                    if stat.S_ISREG(live_entry.mode):
 
1706
                        blob = Blob()
 
1707
                        with target.get_file(rp) as f:
 
1708
                            blob.data = f.read()
 
1709
                    elif stat.S_ISLNK(live_entry.mode):
 
1710
                        blob = Blob()
 
1711
                        blob.data = target.get_symlink_target(rp).encode(osutils._fs_enc)
 
1712
                    else:
 
1713
                        blob = None
 
1714
                    if blob is not None:
 
1715
                        target.store.add_object(blob)
1662
1716
                blobs[path] = (live_entry.sha, cleanup_mode(live_entry.mode))
1663
1717
    if want_unversioned:
1664
 
        for e in target.extras():
1665
 
            st = target._lstat(e)
 
1718
        for extra in target._iter_files_recursive(include_dirs=False):
1666
1719
            try:
1667
 
                np, accessible = osutils.normalized_filename(e)
 
1720
                extra, accessible = osutils.normalized_filename(extra)
1668
1721
            except UnicodeDecodeError:
1669
1722
                raise errors.BadFilenameEncoding(
1670
 
                    e, osutils._fs_enc)
 
1723
                    extra, osutils._fs_enc)
 
1724
            np = encode_git_path(extra)
 
1725
            if np in blobs:
 
1726
                continue
 
1727
            st = target._lstat(extra)
1671
1728
            if stat.S_ISDIR(st.st_mode):
1672
1729
                blob = Tree()
1673
1730
            elif stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode):
1674
1731
                blob = blob_from_path_and_stat(
1675
 
                    target.abspath(e).encode(osutils._fs_enc), st)
 
1732
                    target.abspath(extra).encode(osutils._fs_enc), st)
1676
1733
            else:
1677
1734
                continue
1678
 
            store.add_object(blob)
1679
 
            np = np.encode('utf-8')
 
1735
            target.store.add_object(blob)
1680
1736
            blobs[np] = (blob.id, cleanup_mode(st.st_mode))
1681
1737
            extras.add(np)
1682
 
    to_tree_sha = commit_tree(
1683
 
        store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()])
1684
 
    return store.tree_changes(
1685
 
        from_tree_sha, to_tree_sha, include_trees=True,
1686
 
        want_unchanged=want_unchanged, change_type_same=True), extras
 
1738
    return commit_tree(
 
1739
        target.store, dirified + [(p, s, m) for (p, (s, m)) in blobs.items()]), extras