~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transform.py

Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
2
 
 
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
import errno
19
19
from stat import S_ISREG
20
20
 
 
21
from bzrlib.lazy_import import lazy_import
 
22
lazy_import(globals(), """
 
23
from bzrlib import (
 
24
    bzrdir,
 
25
    delta,
 
26
    errors,
 
27
    inventory
 
28
    )
 
29
""")
21
30
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
22
31
                           ReusingTransform, NotVersionedError, CantMoveRoot,
23
 
                           ExistingLimbo, ImmortalLimbo)
 
32
                           ExistingLimbo, ImmortalLimbo, NoFinalPath)
24
33
from bzrlib.inventory import InventoryEntry
25
34
from bzrlib.osutils import (file_kind, supports_executable, pathjoin, lexists,
26
35
                            delete_any)
27
36
from bzrlib.progress import DummyProgress, ProgressPhase
 
37
from bzrlib.symbol_versioning import deprecated_function, zero_fifteen
28
38
from bzrlib.trace import mutter, warning
29
 
import bzrlib.ui 
 
39
from bzrlib import tree
 
40
import bzrlib.ui
 
41
import bzrlib.urlutils as urlutils
30
42
 
31
43
 
32
44
ROOT_PARENT = "root-parent"
70
82
     * set_executability
71
83
    """
72
84
    def __init__(self, tree, pb=DummyProgress()):
73
 
        """Note: a write lock is taken on the tree.
 
85
        """Note: a tree_write lock is taken on the tree.
74
86
        
75
87
        Use TreeTransform.finalize() to release the lock
76
88
        """
77
89
        object.__init__(self)
78
90
        self._tree = tree
79
 
        self._tree.lock_write()
 
91
        self._tree.lock_tree_write()
80
92
        try:
81
93
            control_files = self._tree._control_files
82
 
            self._limbodir = control_files.controlfilename('limbo')
 
94
            self._limbodir = urlutils.local_path_from_url(
 
95
                control_files.controlfilename('limbo'))
83
96
            try:
84
97
                os.mkdir(self._limbodir)
85
98
            except OSError, e:
95
108
        self._new_contents = {}
96
109
        self._removed_contents = set()
97
110
        self._new_executability = {}
 
111
        self._new_reference_revision = {}
98
112
        self._new_id = {}
99
113
        self._non_present_ids = {}
100
114
        self._r_new_id = {}
101
115
        self._removed_id = set()
102
116
        self._tree_path_ids = {}
103
117
        self._tree_id_paths = {}
 
118
        self._realpaths = {}
 
119
        # Cache of realpath results, to speed up canonical_path
 
120
        self._relpaths = {}
 
121
        # Cache of relpath results, to speed up canonical_path
104
122
        self._new_root = self.trans_id_tree_file_id(tree.get_root_id())
105
123
        self.__done = False
106
124
        self._pb = pb
211
229
    def canonical_path(self, path):
212
230
        """Get the canonical tree-relative path"""
213
231
        # don't follow final symlinks
214
 
        dirname, basename = os.path.split(self._tree.abspath(path))
215
 
        dirname = os.path.realpath(dirname)
216
 
        return self._tree.relpath(pathjoin(dirname, basename))
 
232
        abs = self._tree.abspath(path)
 
233
        if abs in self._relpaths:
 
234
            return self._relpaths[abs]
 
235
        dirname, basename = os.path.split(abs)
 
236
        if dirname not in self._realpaths:
 
237
            self._realpaths[dirname] = os.path.realpath(dirname)
 
238
        dirname = self._realpaths[dirname]
 
239
        abs = pathjoin(dirname, basename)
 
240
        if dirname in self._relpaths:
 
241
            relpath = pathjoin(self._relpaths[dirname], basename)
 
242
            relpath = relpath.rstrip('/\\')
 
243
        else:
 
244
            relpath = self._tree.relpath(abs)
 
245
        self._relpaths[abs] = relpath
 
246
        return relpath
217
247
 
218
248
    def trans_id_tree_path(self, path):
219
249
        """Determine (and maybe set) the transaction ID for a tree path."""
241
271
        New file takes the permissions of any existing file with that id,
242
272
        unless mode_id is specified.
243
273
        """
244
 
        f = file(self._limbo_name(trans_id), 'wb')
245
 
        unique_add(self._new_contents, trans_id, 'file')
246
 
        for segment in contents:
247
 
            f.write(segment)
248
 
        f.close()
 
274
        name = self._limbo_name(trans_id)
 
275
        f = open(name, 'wb')
 
276
        try:
 
277
            try:
 
278
                unique_add(self._new_contents, trans_id, 'file')
 
279
            except:
 
280
                # Clean up the file, it never got registered so
 
281
                # TreeTransform.finalize() won't clean it up.
 
282
                f.close()
 
283
                os.unlink(name)
 
284
                raise
 
285
 
 
286
            f.writelines(contents)
 
287
        finally:
 
288
            f.close()
249
289
        self._set_mode(trans_id, mode_id, S_ISREG)
250
290
 
251
291
    def _set_mode(self, trans_id, mode_id, typefunc):
261
301
        except KeyError:
262
302
            return
263
303
        try:
264
 
            mode = os.stat(old_path).st_mode
 
304
            mode = os.stat(self._tree.abspath(old_path)).st_mode
265
305
        except OSError, e:
266
306
            if e.errno == errno.ENOENT:
267
307
                return
319
359
        else:
320
360
            unique_add(self._new_executability, trans_id, executability)
321
361
 
 
362
    def set_tree_reference(self, revision_id, trans_id):
 
363
        """Set the reference associated with a directory"""
 
364
        unique_add(self._new_reference_revision, trans_id, revision_id)
 
365
 
322
366
    def version_file(self, file_id, trans_id):
323
367
        """Schedule a file to become versioned."""
324
368
        assert file_id is not None
426
470
        try:
427
471
            return self._new_name[trans_id]
428
472
        except KeyError:
429
 
            return os.path.basename(self._tree_id_paths[trans_id])
 
473
            try:
 
474
                return os.path.basename(self._tree_id_paths[trans_id])
 
475
            except KeyError:
 
476
                raise NoFinalPath(trans_id, self)
430
477
 
431
478
    def by_parent(self):
432
479
        """Return a map of parent: children for known parents.
445
492
 
446
493
    def path_changed(self, trans_id):
447
494
        """Return True if a trans_id's path has changed."""
448
 
        return trans_id in self._new_name or trans_id in self._new_parent
 
495
        return (trans_id in self._new_name) or (trans_id in self._new_parent)
 
496
 
 
497
    def new_contents(self, trans_id):
 
498
        return (trans_id in self._new_contents)
449
499
 
450
500
    def find_conflicts(self):
451
501
        """Find any violations of inventory or filesystem invariants"""
477
527
                        self.tree_kind(t) == 'directory'])
478
528
        for trans_id in self._removed_id:
479
529
            file_id = self.tree_file_id(trans_id)
480
 
            if self._tree.inventory[file_id].kind in ('directory', 
481
 
                                                      'root_directory'):
 
530
            if self._tree.inventory[file_id].kind == 'directory':
482
531
                parents.append(trans_id)
483
532
 
484
533
        for parent_id in parents:
521
570
        if child_id is None:
522
571
            return lexists(self._tree.abspath(childpath))
523
572
        else:
524
 
            if tt.final_parent(child_id) != parent_id:
 
573
            if self.final_parent(child_id) != parent_id:
525
574
                return False
526
 
            if child_id in tt._removed_contents:
 
575
            if child_id in self._removed_contents:
527
576
                # XXX What about dangling file-ids?
528
577
                return False
529
578
            else:
537
586
            parent_id = trans_id
538
587
            while parent_id is not ROOT_PARENT:
539
588
                seen.add(parent_id)
540
 
                parent_id = self.final_parent(parent_id)
 
589
                try:
 
590
                    parent_id = self.final_parent(parent_id)
 
591
                except KeyError:
 
592
                    break
541
593
                if parent_id == trans_id:
542
594
                    conflicts.append(('parent loop', trans_id))
543
595
                if parent_id in seen:
617
669
            last_name = None
618
670
            last_trans_id = None
619
671
            for name, trans_id in name_ids:
 
672
                try:
 
673
                    kind = self.final_kind(trans_id)
 
674
                except NoSuchFile:
 
675
                    kind = None
 
676
                file_id = self.final_file_id(trans_id)
 
677
                if kind is None and file_id is None:
 
678
                    continue
620
679
                if name == last_name:
621
680
                    conflicts.append(('duplicate', last_trans_id, trans_id,
622
681
                    name))
623
 
                try:
624
 
                    kind = self.final_kind(trans_id)
625
 
                except NoSuchFile:
626
 
                    kind = None
627
 
                file_id = self.final_file_id(trans_id)
628
 
                if kind is not None or file_id is not None:
629
 
                    last_name = name
630
 
                    last_trans_id = trans_id
 
682
                last_name = name
 
683
                last_trans_id = trans_id
631
684
        return conflicts
632
685
 
633
686
    def _duplicate_ids(self):
738
791
                    file_id = self.tree_file_id(trans_id)
739
792
                    if file_id is not None:
740
793
                        limbo_inv[trans_id] = inv[file_id]
741
 
                        del inv[file_id]
 
794
                        inv.remove_recursive_id(file_id)
742
795
        finally:
743
796
            child_pb.finished()
744
797
 
775
828
                if trans_id in self._new_id:
776
829
                    if kind is None:
777
830
                        kind = file_kind(self._tree.abspath(path))
778
 
                    inv.add_path(path, kind, self._new_id[trans_id])
 
831
                    if trans_id in self._new_reference_revision:
 
832
                        entry = inventory.TreeReference(self._new_id[trans_id], 
 
833
                            self._new_name[trans_id], 
 
834
                            self.final_file_id(self._new_parent[trans_id]),
 
835
                            None, self._new_reference_revision[trans_id])
 
836
                        inv.add(entry)
 
837
                    else:
 
838
                        inv.add_path(path, kind, self._new_id[trans_id])
779
839
                elif trans_id in self._new_name or trans_id in\
780
840
                    self._new_parent:
781
841
                    entry = limbo_inv.get(trans_id)
829
889
        parent_id is the transaction id of the parent directory of the file.
830
890
        contents is an iterator of bytestrings, which will be used to produce
831
891
        the file.
832
 
        file_id is the inventory ID of the file, if it is to be versioned.
 
892
        :param file_id: The inventory ID of the file, if it is to be versioned.
 
893
        :param executable: Only valid when a file_id has been supplied.
833
894
        """
834
895
        trans_id = self._new_entry(name, parent_id, file_id)
 
896
        # TODO: rather than scheduling a set_executable call,
 
897
        # have create_file create the file with the right mode.
835
898
        self.create_file(contents, trans_id)
836
899
        if executable is not None:
837
900
            self.set_executability(executable, trans_id)
861
924
        self.create_symlink(target, trans_id)
862
925
        return trans_id
863
926
 
 
927
    def _affected_ids(self):
 
928
        """Return the set of transform ids affected by the transform"""
 
929
        trans_ids = set(self._removed_id)
 
930
        trans_ids.update(self._new_id.keys())
 
931
        trans_ids.update(self._removed_contents)
 
932
        trans_ids.update(self._new_contents.keys())
 
933
        trans_ids.update(self._new_executability.keys())
 
934
        trans_ids.update(self._new_name.keys())
 
935
        trans_ids.update(self._new_parent.keys())
 
936
        return trans_ids
 
937
 
 
938
    def _get_file_id_maps(self):
 
939
        """Return mapping of file_ids to trans_ids in the to and from states"""
 
940
        trans_ids = self._affected_ids()
 
941
        from_trans_ids = {}
 
942
        to_trans_ids = {}
 
943
        # Build up two dicts: trans_ids associated with file ids in the
 
944
        # FROM state, vs the TO state.
 
945
        for trans_id in trans_ids:
 
946
            from_file_id = self.tree_file_id(trans_id)
 
947
            if from_file_id is not None:
 
948
                from_trans_ids[from_file_id] = trans_id
 
949
            to_file_id = self.final_file_id(trans_id)
 
950
            if to_file_id is not None:
 
951
                to_trans_ids[to_file_id] = trans_id
 
952
        return from_trans_ids, to_trans_ids
 
953
 
 
954
    def _from_file_data(self, from_trans_id, from_versioned, file_id):
 
955
        """Get data about a file in the from (tree) state
 
956
 
 
957
        Return a (name, parent, kind, executable) tuple
 
958
        """
 
959
        from_path = self._tree_id_paths.get(from_trans_id)
 
960
        if from_versioned:
 
961
            # get data from working tree if versioned
 
962
            from_entry = self._tree.inventory[file_id]
 
963
            from_name = from_entry.name
 
964
            from_parent = from_entry.parent_id
 
965
        else:
 
966
            from_entry = None
 
967
            if from_path is None:
 
968
                # File does not exist in FROM state
 
969
                from_name = None
 
970
                from_parent = None
 
971
            else:
 
972
                # File exists, but is not versioned.  Have to use path-
 
973
                # splitting stuff
 
974
                from_name = os.path.basename(from_path)
 
975
                tree_parent = self.get_tree_parent(from_trans_id)
 
976
                from_parent = self.tree_file_id(tree_parent)
 
977
        if from_path is not None:
 
978
            from_kind, from_executable, from_stats = \
 
979
                self._tree._comparison_data(from_entry, from_path)
 
980
        else:
 
981
            from_kind = None
 
982
            from_executable = False
 
983
        return from_name, from_parent, from_kind, from_executable
 
984
 
 
985
    def _to_file_data(self, to_trans_id, from_trans_id, from_executable):
 
986
        """Get data about a file in the to (target) state
 
987
 
 
988
        Return a (name, parent, kind, executable) tuple
 
989
        """
 
990
        to_name = self.final_name(to_trans_id)
 
991
        try:
 
992
            to_kind = self.final_kind(to_trans_id)
 
993
        except NoSuchFile:
 
994
            to_kind = None
 
995
        to_parent = self.final_file_id(self.final_parent(to_trans_id))
 
996
        if to_trans_id in self._new_executability:
 
997
            to_executable = self._new_executability[to_trans_id]
 
998
        elif to_trans_id == from_trans_id:
 
999
            to_executable = from_executable
 
1000
        else:
 
1001
            to_executable = False
 
1002
        return to_name, to_parent, to_kind, to_executable
 
1003
 
 
1004
    def _iter_changes(self):
 
1005
        """Produce output in the same format as Tree._iter_changes.
 
1006
 
 
1007
        Will produce nonsensical results if invoked while inventory/filesystem
 
1008
        conflicts (as reported by TreeTransform.find_conflicts()) are present.
 
1009
 
 
1010
        This reads the Transform, but only reproduces changes involving a
 
1011
        file_id.  Files that are not versioned in either of the FROM or TO
 
1012
        states are not reflected.
 
1013
        """
 
1014
        final_paths = FinalPaths(self)
 
1015
        from_trans_ids, to_trans_ids = self._get_file_id_maps()
 
1016
        results = []
 
1017
        # Now iterate through all active file_ids
 
1018
        for file_id in set(from_trans_ids.keys() + to_trans_ids.keys()):
 
1019
            modified = False
 
1020
            from_trans_id = from_trans_ids.get(file_id)
 
1021
            # find file ids, and determine versioning state
 
1022
            if from_trans_id is None:
 
1023
                from_versioned = False
 
1024
                from_trans_id = to_trans_ids[file_id]
 
1025
            else:
 
1026
                from_versioned = True
 
1027
            to_trans_id = to_trans_ids.get(file_id)
 
1028
            if to_trans_id is None:
 
1029
                to_versioned = False
 
1030
                to_trans_id = from_trans_id
 
1031
            else:
 
1032
                to_versioned = True
 
1033
 
 
1034
            from_name, from_parent, from_kind, from_executable = \
 
1035
                self._from_file_data(from_trans_id, from_versioned, file_id)
 
1036
 
 
1037
            to_name, to_parent, to_kind, to_executable = \
 
1038
                self._to_file_data(to_trans_id, from_trans_id, from_executable)
 
1039
 
 
1040
            if not from_versioned:
 
1041
                from_path = None
 
1042
            else:
 
1043
                from_path = self._tree_id_paths.get(from_trans_id)
 
1044
            if not to_versioned:
 
1045
                to_path = None
 
1046
            else:
 
1047
                to_path = final_paths.get_path(to_trans_id)
 
1048
            if from_kind != to_kind:
 
1049
                modified = True
 
1050
            elif to_kind in ('file' or 'symlink') and (
 
1051
                to_trans_id != from_trans_id or
 
1052
                to_trans_id in self._new_contents):
 
1053
                modified = True
 
1054
            if (not modified and from_versioned == to_versioned and
 
1055
                from_parent==to_parent and from_name == to_name and
 
1056
                from_executable == to_executable):
 
1057
                continue
 
1058
            results.append((file_id, (from_path, to_path), modified,
 
1059
                   (from_versioned, to_versioned),
 
1060
                   (from_parent, to_parent),
 
1061
                   (from_name, to_name),
 
1062
                   (from_kind, to_kind),
 
1063
                   (from_executable, to_executable)))
 
1064
        return iter(sorted(results, key=lambda x:x[1]))
 
1065
 
 
1066
 
864
1067
def joinpath(parent, child):
865
1068
    """Join tree-relative paths, handling the tree root specially"""
866
1069
    if parent is None or parent == "":
902
1105
    file_ids.sort(key=tree.id2path)
903
1106
    return file_ids
904
1107
 
 
1108
 
905
1109
def build_tree(tree, wt):
906
 
    """Create working tree for a branch, using a Transaction."""
 
1110
    """Create working tree for a branch, using a TreeTransform.
 
1111
    
 
1112
    This function should be used on empty trees, having a tree root at most.
 
1113
    (see merge and revert functionality for working with existing trees)
 
1114
 
 
1115
    Existing files are handled like so:
 
1116
    
 
1117
    - Existing bzrdirs take precedence over creating new items.  They are
 
1118
      created as '%s.diverted' % name.
 
1119
    - Otherwise, if the content on disk matches the content we are building,
 
1120
      it is silently replaced.
 
1121
    - Otherwise, conflict resolution will move the old file to 'oldname.moved'.
 
1122
    """
 
1123
    wt.lock_tree_write()
 
1124
    try:
 
1125
        tree.lock_read()
 
1126
        try:
 
1127
            return _build_tree(tree, wt)
 
1128
        finally:
 
1129
            tree.unlock()
 
1130
    finally:
 
1131
        wt.unlock()
 
1132
 
 
1133
def _build_tree(tree, wt):
 
1134
    """See build_tree."""
 
1135
    if len(wt.inventory) > 1:  # more than just a root
 
1136
        raise errors.WorkingTreeAlreadyPopulated(base=wt.basedir)
907
1137
    file_trans_id = {}
908
1138
    top_pb = bzrlib.ui.ui_factory.nested_progress_bar()
909
1139
    pp = ProgressPhase("Build phase", 2, top_pb)
 
1140
    if tree.inventory.root is not None:
 
1141
        # this is kindof a hack: we should be altering the root 
 
1142
        # as partof the regular tree shape diff logic.
 
1143
        # the conditional test hereis to avoid doing an
 
1144
        # expensive operation (flush) every time the root id
 
1145
        # is set within the tree, nor setting the root and thus
 
1146
        # marking the tree as dirty, because we use two different
 
1147
        # idioms here: tree interfaces and inventory interfaces.
 
1148
        if wt.path2id('') != tree.inventory.root.file_id:
 
1149
            wt.set_root_id(tree.inventory.root.file_id)
 
1150
            wt.flush()
910
1151
    tt = TreeTransform(wt)
 
1152
    divert = set()
911
1153
    try:
912
1154
        pp.next_phase()
913
 
        file_trans_id[wt.get_root_id()] = tt.trans_id_tree_file_id(wt.get_root_id())
914
 
        file_ids = topology_sorted_ids(tree)
 
1155
        file_trans_id[wt.get_root_id()] = \
 
1156
            tt.trans_id_tree_file_id(wt.get_root_id())
915
1157
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
916
1158
        try:
917
 
            for num, file_id in enumerate(file_ids):
918
 
                pb.update("Building tree", num, len(file_ids))
919
 
                entry = tree.inventory[file_id]
 
1159
            for num, (tree_path, entry) in \
 
1160
                enumerate(tree.inventory.iter_entries_by_dir()):
 
1161
                pb.update("Building tree", num, len(tree.inventory))
920
1162
                if entry.parent_id is None:
921
1163
                    continue
 
1164
                reparent = False
 
1165
                file_id = entry.file_id
 
1166
                target_path = wt.abspath(tree_path)
 
1167
                try:
 
1168
                    kind = file_kind(target_path)
 
1169
                except NoSuchFile:
 
1170
                    pass
 
1171
                else:
 
1172
                    if kind == "directory":
 
1173
                        try:
 
1174
                            bzrdir.BzrDir.open(target_path)
 
1175
                        except errors.NotBranchError:
 
1176
                            pass
 
1177
                        else:
 
1178
                            divert.add(file_id)
 
1179
                    if (file_id not in divert and
 
1180
                        _content_match(tree, entry, file_id, kind,
 
1181
                        target_path)):
 
1182
                        tt.delete_contents(tt.trans_id_tree_path(tree_path))
 
1183
                        if kind == 'directory':
 
1184
                            reparent = True
922
1185
                if entry.parent_id not in file_trans_id:
923
 
                    raise repr(entry.parent_id)
 
1186
                    raise AssertionError(
 
1187
                        'entry %s parent id %r is not in file_trans_id %r'
 
1188
                        % (entry, entry.parent_id, file_trans_id))
924
1189
                parent_id = file_trans_id[entry.parent_id]
925
 
                file_trans_id[file_id] = new_by_entry(tt, entry, parent_id, 
 
1190
                file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
926
1191
                                                      tree)
 
1192
                if reparent:
 
1193
                    new_trans_id = file_trans_id[file_id]
 
1194
                    old_parent = tt.trans_id_tree_path(tree_path)
 
1195
                    _reparent_children(tt, old_parent, new_trans_id)
927
1196
        finally:
928
1197
            pb.finished()
929
1198
        pp.next_phase()
 
1199
        divert_trans = set(file_trans_id[f] for f in divert)
 
1200
        resolver = lambda t, c: resolve_checkout(t, c, divert_trans)
 
1201
        raw_conflicts = resolve_conflicts(tt, pass_func=resolver)
 
1202
        conflicts = cook_conflicts(raw_conflicts, tt)
 
1203
        for conflict in conflicts:
 
1204
            warning(conflict)
 
1205
        try:
 
1206
            wt.add_conflicts(conflicts)
 
1207
        except errors.UnsupportedOperation:
 
1208
            pass
930
1209
        tt.apply()
931
1210
    finally:
932
1211
        tt.finalize()
933
1212
        top_pb.finished()
934
1213
 
 
1214
 
 
1215
def _reparent_children(tt, old_parent, new_parent):
 
1216
    for child in tt.iter_tree_children(old_parent):
 
1217
        tt.adjust_path(tt.final_name(child), new_parent, child)
 
1218
 
 
1219
 
 
1220
def _content_match(tree, entry, file_id, kind, target_path):
 
1221
    if entry.kind != kind:
 
1222
        return False
 
1223
    if entry.kind == "directory":
 
1224
        return True
 
1225
    if entry.kind == "file":
 
1226
        if tree.get_file(file_id).read() == file(target_path, 'rb').read():
 
1227
            return True
 
1228
    elif entry.kind == "symlink":
 
1229
        if tree.get_symlink_target(file_id) == os.readlink(target_path):
 
1230
            return True
 
1231
    return False
 
1232
 
 
1233
 
 
1234
def resolve_checkout(tt, conflicts, divert):
 
1235
    new_conflicts = set()
 
1236
    for c_type, conflict in ((c[0], c) for c in conflicts):
 
1237
        # Anything but a 'duplicate' would indicate programmer error
 
1238
        assert c_type == 'duplicate', c_type
 
1239
        # Now figure out which is new and which is old
 
1240
        if tt.new_contents(conflict[1]):
 
1241
            new_file = conflict[1]
 
1242
            old_file = conflict[2]
 
1243
        else:
 
1244
            new_file = conflict[2]
 
1245
            old_file = conflict[1]
 
1246
 
 
1247
        # We should only get here if the conflict wasn't completely
 
1248
        # resolved
 
1249
        final_parent = tt.final_parent(old_file)
 
1250
        if new_file in divert:
 
1251
            new_name = tt.final_name(old_file)+'.diverted'
 
1252
            tt.adjust_path(new_name, final_parent, new_file)
 
1253
            new_conflicts.add((c_type, 'Diverted to',
 
1254
                               new_file, old_file))
 
1255
        else:
 
1256
            new_name = tt.final_name(old_file)+'.moved'
 
1257
            tt.adjust_path(new_name, final_parent, old_file)
 
1258
            new_conflicts.add((c_type, 'Moved existing file to',
 
1259
                               old_file, new_file))
 
1260
    return new_conflicts
 
1261
 
 
1262
 
935
1263
def new_by_entry(tt, entry, parent_id, tree):
936
1264
    """Create a new file according to its inventory entry"""
937
1265
    name = entry.name
941
1269
        executable = tree.is_executable(entry.file_id)
942
1270
        return tt.new_file(name, parent_id, contents, entry.file_id, 
943
1271
                           executable)
944
 
    elif kind == 'directory':
945
 
        return tt.new_directory(name, parent_id, entry.file_id)
 
1272
    elif kind in ('directory', 'tree-reference'):
 
1273
        trans_id = tt.new_directory(name, parent_id, entry.file_id)
 
1274
        if kind == 'tree-reference':
 
1275
            tt.set_tree_reference(entry.reference_revision, trans_id)
 
1276
        return trans_id 
946
1277
    elif kind == 'symlink':
947
1278
        target = tree.get_symlink_target(entry.file_id)
948
1279
        return tt.new_symlink(name, parent_id, target, entry.file_id)
 
1280
    else:
 
1281
        raise errors.BadFileKindError(name, kind)
949
1282
 
950
1283
def create_by_entry(tt, entry, tree, trans_id, lines=None, mode_id=None):
951
1284
    """Create new file contents according to an inventory entry."""
952
1285
    if entry.kind == "file":
953
 
        if lines == None:
 
1286
        if lines is None:
954
1287
            lines = tree.get_file(entry.file_id).readlines()
955
1288
        tt.create_file(lines, trans_id, mode_id=mode_id)
956
1289
    elif entry.kind == "symlink":
964
1297
        tt.set_executability(entry.executable, trans_id)
965
1298
 
966
1299
 
 
1300
@deprecated_function(zero_fifteen)
967
1301
def find_interesting(working_tree, target_tree, filenames):
968
 
    """Find the ids corresponding to specified filenames."""
969
 
    if not filenames:
970
 
        interesting_ids = None
971
 
    else:
972
 
        interesting_ids = set()
973
 
        for tree_path in filenames:
974
 
            not_found = True
975
 
            for tree in (working_tree, target_tree):
976
 
                file_id = tree.inventory.path2id(tree_path)
977
 
                if file_id is not None:
978
 
                    interesting_ids.add(file_id)
979
 
                    not_found = False
980
 
            if not_found:
981
 
                raise NotVersionedError(path=tree_path)
982
 
    return interesting_ids
 
1302
    """Find the ids corresponding to specified filenames.
 
1303
    
 
1304
    Deprecated: Please use tree1.paths2ids(filenames, [tree2]).
 
1305
    """
 
1306
    working_tree.lock_read()
 
1307
    try:
 
1308
        target_tree.lock_read()
 
1309
        try:
 
1310
            return working_tree.paths2ids(filenames, [target_tree])
 
1311
        finally:
 
1312
            target_tree.unlock()
 
1313
    finally:
 
1314
        working_tree.unlock()
983
1315
 
984
1316
 
985
1317
def change_entry(tt, file_id, working_tree, target_tree, 
1023
1355
 
1024
1356
 
1025
1357
def get_backup_name(entry, by_parent, parent_trans_id, tt):
 
1358
    return _get_backup_name(entry.name, by_parent, parent_trans_id, tt)
 
1359
 
 
1360
 
 
1361
def _get_backup_name(name, by_parent, parent_trans_id, tt):
1026
1362
    """Produce a backup-style name that appears to be available"""
1027
1363
    def name_gen():
1028
1364
        counter = 1
1029
1365
        while True:
1030
 
            yield "%s.~%d~" % (entry.name, counter)
 
1366
            yield "%s.~%d~" % (name, counter)
1031
1367
            counter += 1
1032
 
    for name in name_gen():
1033
 
        if not tt.has_named_child(by_parent, parent_trans_id, name):
1034
 
            return name
 
1368
    for new_name in name_gen():
 
1369
        if not tt.has_named_child(by_parent, parent_trans_id, new_name):
 
1370
            return new_name
 
1371
 
1035
1372
 
1036
1373
def _entry_changes(file_id, entry, working_tree):
1037
1374
    """Determine in which ways the inventory entry has changed.
1045
1382
    try:
1046
1383
        working_kind = working_tree.kind(file_id)
1047
1384
        has_contents = True
1048
 
    except OSError, e:
1049
 
        if e.errno != errno.ENOENT:
1050
 
            raise
 
1385
    except NoSuchFile:
1051
1386
        has_contents = False
1052
1387
        contents_mod = True
1053
1388
        meta_mod = False
1054
1389
    if has_contents is True:
1055
 
        real_e_kind = entry.kind
1056
 
        if real_e_kind == 'root_directory':
1057
 
            real_e_kind = 'directory'
1058
 
        if real_e_kind != working_kind:
 
1390
        if entry.kind != working_kind:
1059
1391
            contents_mod, meta_mod = True, False
1060
1392
        else:
1061
1393
            cur_entry._read_tree_state(working_tree.id2path(file_id), 
1065
1397
    return has_contents, contents_mod, meta_mod
1066
1398
 
1067
1399
 
1068
 
def revert(working_tree, target_tree, filenames, backups=False, 
1069
 
           pb=DummyProgress()):
 
1400
def revert(working_tree, target_tree, filenames, backups=False,
 
1401
           pb=DummyProgress(), change_reporter=None):
1070
1402
    """Revert a working tree's contents to those of a target tree."""
1071
 
    interesting_ids = find_interesting(working_tree, target_tree, filenames)
1072
 
    def interesting(file_id):
1073
 
        return interesting_ids is None or file_id in interesting_ids
1074
 
 
 
1403
    target_tree.lock_read()
1075
1404
    tt = TreeTransform(working_tree, pb)
1076
1405
    try:
1077
 
        merge_modified = working_tree.merge_modified()
1078
 
        trans_id = {}
1079
 
        def trans_id_file_id(file_id):
1080
 
            try:
1081
 
                return trans_id[file_id]
1082
 
            except KeyError:
1083
 
                return tt.trans_id_tree_file_id(file_id)
1084
 
 
1085
 
        pp = ProgressPhase("Revert phase", 4, pb)
1086
 
        pp.next_phase()
1087
 
        sorted_interesting = [i for i in topology_sorted_ids(target_tree) if
1088
 
                              interesting(i)]
1089
 
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1090
 
        try:
1091
 
            by_parent = tt.by_parent()
1092
 
            for id_num, file_id in enumerate(sorted_interesting):
1093
 
                child_pb.update("Reverting file", id_num+1, 
1094
 
                                len(sorted_interesting))
1095
 
                if file_id not in working_tree.inventory:
1096
 
                    entry = target_tree.inventory[file_id]
1097
 
                    parent_id = trans_id_file_id(entry.parent_id)
1098
 
                    e_trans_id = new_by_entry(tt, entry, parent_id, target_tree)
1099
 
                    trans_id[file_id] = e_trans_id
1100
 
                else:
1101
 
                    backup_this = backups
1102
 
                    if file_id in merge_modified:
1103
 
                        backup_this = False
1104
 
                        del merge_modified[file_id]
1105
 
                    change_entry(tt, file_id, working_tree, target_tree, 
1106
 
                                 trans_id_file_id, backup_this, trans_id,
1107
 
                                 by_parent)
1108
 
        finally:
1109
 
            child_pb.finished()
1110
 
        pp.next_phase()
1111
 
        wt_interesting = [i for i in working_tree.inventory if interesting(i)]
1112
 
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1113
 
        try:
1114
 
            for id_num, file_id in enumerate(wt_interesting):
1115
 
                child_pb.update("New file check", id_num+1, 
1116
 
                                len(sorted_interesting))
1117
 
                if file_id not in target_tree:
1118
 
                    trans_id = tt.trans_id_tree_file_id(file_id)
1119
 
                    tt.unversion_file(trans_id)
1120
 
                    if file_id in merge_modified:
1121
 
                        tt.delete_contents(trans_id)
1122
 
                        del merge_modified[file_id]
 
1406
        pp = ProgressPhase("Revert phase", 3, pb)
 
1407
        pp.next_phase()
 
1408
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1409
        try:
 
1410
            _alter_files(working_tree, target_tree, tt, child_pb,
 
1411
                         filenames, backups)
1123
1412
        finally:
1124
1413
            child_pb.finished()
1125
1414
        pp.next_phase()
1129
1418
        finally:
1130
1419
            child_pb.finished()
1131
1420
        conflicts = cook_conflicts(raw_conflicts, tt)
 
1421
        if change_reporter:
 
1422
            change_reporter = delta.ChangeReporter(
 
1423
                unversioned_filter=working_tree.is_ignored)
 
1424
            delta.report_changes(tt._iter_changes(), change_reporter)
1132
1425
        for conflict in conflicts:
1133
1426
            warning(conflict)
1134
1427
        pp.next_phase()
1135
1428
        tt.apply()
1136
1429
        working_tree.set_merge_modified({})
1137
1430
    finally:
 
1431
        target_tree.unlock()
1138
1432
        tt.finalize()
1139
1433
        pb.clear()
1140
1434
    return conflicts
1141
1435
 
1142
1436
 
1143
 
def resolve_conflicts(tt, pb=DummyProgress()):
 
1437
def _alter_files(working_tree, target_tree, tt, pb, specific_files,
 
1438
                 backups):
 
1439
    merge_modified = working_tree.merge_modified()
 
1440
    change_list = target_tree._iter_changes(working_tree,
 
1441
        specific_files=specific_files, pb=pb)
 
1442
    if target_tree.inventory.root is None:
 
1443
        skip_root = True
 
1444
    else:
 
1445
        skip_root = False
 
1446
    basis_tree = None
 
1447
    try:
 
1448
        for id_num, (file_id, path, changed_content, versioned, parent, name,
 
1449
                kind, executable) in enumerate(change_list):
 
1450
            if skip_root and file_id[0] is not None and parent[0] is None:
 
1451
                continue
 
1452
            trans_id = tt.trans_id_file_id(file_id)
 
1453
            mode_id = None
 
1454
            if changed_content:
 
1455
                keep_content = False
 
1456
                if kind[0] == 'file' and (backups or kind[1] is None):
 
1457
                    wt_sha1 = working_tree.get_file_sha1(file_id)
 
1458
                    if merge_modified.get(file_id) != wt_sha1:
 
1459
                        # acquire the basis tree lazyily to prevent the expense
 
1460
                        # of accessing it when its not needed ? (Guessing, RBC,
 
1461
                        # 200702)
 
1462
                        if basis_tree is None:
 
1463
                            basis_tree = working_tree.basis_tree()
 
1464
                            basis_tree.lock_read()
 
1465
                        if file_id in basis_tree:
 
1466
                            if wt_sha1 != basis_tree.get_file_sha1(file_id):
 
1467
                                keep_content = True
 
1468
                        elif kind[1] is None and not versioned[1]:
 
1469
                            keep_content = True
 
1470
                if kind[0] is not None:
 
1471
                    if not keep_content:
 
1472
                        tt.delete_contents(trans_id)
 
1473
                    elif kind[1] is not None:
 
1474
                        parent_trans_id = tt.trans_id_file_id(parent[0])
 
1475
                        by_parent = tt.by_parent()
 
1476
                        backup_name = _get_backup_name(name[0], by_parent,
 
1477
                                                       parent_trans_id, tt)
 
1478
                        tt.adjust_path(backup_name, parent_trans_id, trans_id)
 
1479
                        new_trans_id = tt.create_path(name[0], parent_trans_id)
 
1480
                        if versioned == (True, True):
 
1481
                            tt.unversion_file(trans_id)
 
1482
                            tt.version_file(file_id, new_trans_id)
 
1483
                        # New contents should have the same unix perms as old
 
1484
                        # contents
 
1485
                        mode_id = trans_id
 
1486
                        trans_id = new_trans_id
 
1487
                if kind[1] == 'directory':
 
1488
                    tt.create_directory(trans_id)
 
1489
                elif kind[1] == 'symlink':
 
1490
                    tt.create_symlink(target_tree.get_symlink_target(file_id),
 
1491
                                      trans_id)
 
1492
                elif kind[1] == 'file':
 
1493
                    tt.create_file(target_tree.get_file_lines(file_id),
 
1494
                                   trans_id, mode_id)
 
1495
                    # preserve the execute bit when backing up
 
1496
                    if keep_content and executable[0] == executable[1]:
 
1497
                        tt.set_executability(executable[1], trans_id)
 
1498
                else:
 
1499
                    assert kind[1] is None
 
1500
            if versioned == (False, True):
 
1501
                tt.version_file(file_id, trans_id)
 
1502
            if versioned == (True, False):
 
1503
                tt.unversion_file(trans_id)
 
1504
            if (name[1] is not None and 
 
1505
                (name[0] != name[1] or parent[0] != parent[1])):
 
1506
                tt.adjust_path(
 
1507
                    name[1], tt.trans_id_file_id(parent[1]), trans_id)
 
1508
            if executable[0] != executable[1] and kind[1] == "file":
 
1509
                tt.set_executability(executable[1], trans_id)
 
1510
    finally:
 
1511
        if basis_tree is not None:
 
1512
            basis_tree.unlock()
 
1513
 
 
1514
 
 
1515
def resolve_conflicts(tt, pb=DummyProgress(), pass_func=None):
1144
1516
    """Make many conflict-resolution attempts, but die if they fail"""
 
1517
    if pass_func is None:
 
1518
        pass_func = conflict_pass
1145
1519
    new_conflicts = set()
1146
1520
    try:
1147
1521
        for n in range(10):
1149
1523
            conflicts = tt.find_conflicts()
1150
1524
            if len(conflicts) == 0:
1151
1525
                return new_conflicts
1152
 
            new_conflicts.update(conflict_pass(tt, conflicts))
 
1526
            new_conflicts.update(pass_func(tt, conflicts))
1153
1527
        raise MalformedTransform(conflicts=conflicts)
1154
1528
    finally:
1155
1529
        pb.clear()
1188
1562
            trans_id = conflict[1]
1189
1563
            try:
1190
1564
                tt.cancel_deletion(trans_id)
1191
 
                new_conflicts.add((c_type, 'Not deleting', trans_id))
 
1565
                new_conflicts.add(('deleting parent', 'Not deleting', 
 
1566
                                   trans_id))
1192
1567
            except KeyError:
1193
1568
                tt.create_directory(trans_id)
1194
 
                new_conflicts.add((c_type, 'Created directory.', trans_id))
 
1569
                new_conflicts.add((c_type, 'Created directory', trans_id))
1195
1570
        elif c_type == 'unversioned parent':
1196
1571
            tt.version_file(tt.inactive_file_id(conflict[1]), conflict[1])
1197
1572
            new_conflicts.add((c_type, 'Versioned directory', conflict[1]))