/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 bzrlib/transform.py

  • Committer: Aaron Bentley
  • Date: 2007-01-17 15:39:21 UTC
  • mfrom: (1551.9.35 Aaron's mergeable stuff)
  • mto: This revision was merged to the branch mainline in revision 2239.
  • Revision ID: abentley@panoramicfeedback.com-20070117153921-6pp9ssa2r8n5izoo
Merge bzr.ab, to avoid conflicts submitting

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2006 Canonical Ltd
2
 
 
 
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 import bzrdir, errors
21
22
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
22
23
                           ReusingTransform, NotVersionedError, CantMoveRoot,
23
 
                           ExistingLimbo, ImmortalLimbo)
 
24
                           ExistingLimbo, ImmortalLimbo, NoFinalPath)
24
25
from bzrlib.inventory import InventoryEntry
25
26
from bzrlib.osutils import (file_kind, supports_executable, pathjoin, lexists,
26
27
                            delete_any)
27
28
from bzrlib.progress import DummyProgress, ProgressPhase
28
29
from bzrlib.trace import mutter, warning
 
30
from bzrlib import tree
29
31
import bzrlib.ui 
 
32
import bzrlib.urlutils as urlutils
30
33
 
31
34
 
32
35
ROOT_PARENT = "root-parent"
70
73
     * set_executability
71
74
    """
72
75
    def __init__(self, tree, pb=DummyProgress()):
73
 
        """Note: a write lock is taken on the tree.
 
76
        """Note: a tree_write lock is taken on the tree.
74
77
        
75
78
        Use TreeTransform.finalize() to release the lock
76
79
        """
77
80
        object.__init__(self)
78
81
        self._tree = tree
79
 
        self._tree.lock_write()
 
82
        self._tree.lock_tree_write()
80
83
        try:
81
84
            control_files = self._tree._control_files
82
 
            self._limbodir = control_files.controlfilename('limbo')
 
85
            self._limbodir = urlutils.local_path_from_url(
 
86
                control_files.controlfilename('limbo'))
83
87
            try:
84
88
                os.mkdir(self._limbodir)
85
89
            except OSError, e:
101
105
        self._removed_id = set()
102
106
        self._tree_path_ids = {}
103
107
        self._tree_id_paths = {}
 
108
        self._realpaths = {}
 
109
        # Cache of realpath results, to speed up canonical_path
 
110
        self._relpaths = {}
 
111
        # Cache of relpath results, to speed up canonical_path
104
112
        self._new_root = self.trans_id_tree_file_id(tree.get_root_id())
105
113
        self.__done = False
106
114
        self._pb = pb
211
219
    def canonical_path(self, path):
212
220
        """Get the canonical tree-relative path"""
213
221
        # 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))
 
222
        abs = self._tree.abspath(path)
 
223
        if abs in self._relpaths:
 
224
            return self._relpaths[abs]
 
225
        dirname, basename = os.path.split(abs)
 
226
        if dirname not in self._realpaths:
 
227
            self._realpaths[dirname] = os.path.realpath(dirname)
 
228
        dirname = self._realpaths[dirname]
 
229
        abs = pathjoin(dirname, basename)
 
230
        if dirname in self._relpaths:
 
231
            relpath = pathjoin(self._relpaths[dirname], basename)
 
232
            relpath = relpath.rstrip('/\\')
 
233
        else:
 
234
            relpath = self._tree.relpath(abs)
 
235
        self._relpaths[abs] = relpath
 
236
        return relpath
217
237
 
218
238
    def trans_id_tree_path(self, path):
219
239
        """Determine (and maybe set) the transaction ID for a tree path."""
241
261
        New file takes the permissions of any existing file with that id,
242
262
        unless mode_id is specified.
243
263
        """
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()
 
264
        name = self._limbo_name(trans_id)
 
265
        f = open(name, 'wb')
 
266
        try:
 
267
            try:
 
268
                unique_add(self._new_contents, trans_id, 'file')
 
269
            except:
 
270
                # Clean up the file, it never got registered so
 
271
                # TreeTransform.finalize() won't clean it up.
 
272
                f.close()
 
273
                os.unlink(name)
 
274
                raise
 
275
 
 
276
            for segment in contents:
 
277
                f.write(segment)
 
278
        finally:
 
279
            f.close()
249
280
        self._set_mode(trans_id, mode_id, S_ISREG)
250
281
 
251
282
    def _set_mode(self, trans_id, mode_id, typefunc):
261
292
        except KeyError:
262
293
            return
263
294
        try:
264
 
            mode = os.stat(old_path).st_mode
 
295
            mode = os.stat(self._tree.abspath(old_path)).st_mode
265
296
        except OSError, e:
266
297
            if e.errno == errno.ENOENT:
267
298
                return
426
457
        try:
427
458
            return self._new_name[trans_id]
428
459
        except KeyError:
429
 
            return os.path.basename(self._tree_id_paths[trans_id])
 
460
            try:
 
461
                return os.path.basename(self._tree_id_paths[trans_id])
 
462
            except KeyError:
 
463
                raise NoFinalPath(trans_id, self)
430
464
 
431
465
    def by_parent(self):
432
466
        """Return a map of parent: children for known parents.
445
479
 
446
480
    def path_changed(self, trans_id):
447
481
        """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
 
482
        return (trans_id in self._new_name) or (trans_id in self._new_parent)
 
483
 
 
484
    def new_contents(self, trans_id):
 
485
        return (trans_id in self._new_contents)
449
486
 
450
487
    def find_conflicts(self):
451
488
        """Find any violations of inventory or filesystem invariants"""
477
514
                        self.tree_kind(t) == 'directory'])
478
515
        for trans_id in self._removed_id:
479
516
            file_id = self.tree_file_id(trans_id)
480
 
            if self._tree.inventory[file_id].kind in ('directory', 
481
 
                                                      'root_directory'):
 
517
            if self._tree.inventory[file_id].kind == 'directory':
482
518
                parents.append(trans_id)
483
519
 
484
520
        for parent_id in parents:
521
557
        if child_id is None:
522
558
            return lexists(self._tree.abspath(childpath))
523
559
        else:
524
 
            if tt.final_parent(child_id) != parent_id:
 
560
            if self.final_parent(child_id) != parent_id:
525
561
                return False
526
 
            if child_id in tt._removed_contents:
 
562
            if child_id in self._removed_contents:
527
563
                # XXX What about dangling file-ids?
528
564
                return False
529
565
            else:
537
573
            parent_id = trans_id
538
574
            while parent_id is not ROOT_PARENT:
539
575
                seen.add(parent_id)
540
 
                parent_id = self.final_parent(parent_id)
 
576
                try:
 
577
                    parent_id = self.final_parent(parent_id)
 
578
                except KeyError:
 
579
                    break
541
580
                if parent_id == trans_id:
542
581
                    conflicts.append(('parent loop', trans_id))
543
582
                if parent_id in seen:
617
656
            last_name = None
618
657
            last_trans_id = None
619
658
            for name, trans_id in name_ids:
 
659
                try:
 
660
                    kind = self.final_kind(trans_id)
 
661
                except NoSuchFile:
 
662
                    kind = None
 
663
                file_id = self.final_file_id(trans_id)
 
664
                if kind is None and file_id is None:
 
665
                    continue
620
666
                if name == last_name:
621
667
                    conflicts.append(('duplicate', last_trans_id, trans_id,
622
668
                    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
 
669
                last_name = name
 
670
                last_trans_id = trans_id
631
671
        return conflicts
632
672
 
633
673
    def _duplicate_ids(self):
829
869
        parent_id is the transaction id of the parent directory of the file.
830
870
        contents is an iterator of bytestrings, which will be used to produce
831
871
        the file.
832
 
        file_id is the inventory ID of the file, if it is to be versioned.
 
872
        :param file_id: The inventory ID of the file, if it is to be versioned.
 
873
        :param executable: Only valid when a file_id has been supplied.
833
874
        """
834
875
        trans_id = self._new_entry(name, parent_id, file_id)
 
876
        # TODO: rather than scheduling a set_executable call,
 
877
        # have create_file create the file with the right mode.
835
878
        self.create_file(contents, trans_id)
836
879
        if executable is not None:
837
880
            self.set_executability(executable, trans_id)
902
945
    file_ids.sort(key=tree.id2path)
903
946
    return file_ids
904
947
 
 
948
 
905
949
def build_tree(tree, wt):
906
 
    """Create working tree for a branch, using a Transaction."""
 
950
    """Create working tree for a branch, using a TreeTransform.
 
951
    
 
952
    This function should be used on empty trees, having a tree root at most.
 
953
    (see merge and revert functionality for working with existing trees)
 
954
 
 
955
    Existing files are handled like so:
 
956
    
 
957
    - Existing bzrdirs take precedence over creating new items.  They are
 
958
      created as '%s.diverted' % name.
 
959
    - Otherwise, if the content on disk matches the content we are building,
 
960
      it is silently replaced.
 
961
    - Otherwise, conflict resolution will move the old file to 'oldname.moved'.
 
962
    """
 
963
    if len(wt.inventory) > 1:  # more than just a root
 
964
        raise errors.WorkingTreeAlreadyPopulated(base=wt.basedir)
907
965
    file_trans_id = {}
908
966
    top_pb = bzrlib.ui.ui_factory.nested_progress_bar()
909
967
    pp = ProgressPhase("Build phase", 2, top_pb)
 
968
    if tree.inventory.root is not None:
 
969
        wt.set_root_id(tree.inventory.root.file_id)
910
970
    tt = TreeTransform(wt)
 
971
    divert = set()
911
972
    try:
912
973
        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)
 
974
        file_trans_id[wt.get_root_id()] = \
 
975
            tt.trans_id_tree_file_id(wt.get_root_id())
915
976
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
916
977
        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]
 
978
            for num, (tree_path, entry) in \
 
979
                enumerate(tree.inventory.iter_entries_by_dir()):
 
980
                pb.update("Building tree", num, len(tree.inventory))
920
981
                if entry.parent_id is None:
921
982
                    continue
 
983
                reparent = False
 
984
                file_id = entry.file_id
 
985
                target_path = wt.abspath(tree_path)
 
986
                try:
 
987
                    kind = file_kind(target_path)
 
988
                except NoSuchFile:
 
989
                    pass
 
990
                else:
 
991
                    if kind == "directory":
 
992
                        try:
 
993
                            bzrdir.BzrDir.open(target_path)
 
994
                        except errors.NotBranchError:
 
995
                            pass
 
996
                        else:
 
997
                            divert.add(file_id)
 
998
                    if (file_id not in divert and
 
999
                        _content_match(tree, entry, file_id, kind,
 
1000
                        target_path)):
 
1001
                        tt.delete_contents(tt.trans_id_tree_path(tree_path))
 
1002
                        if kind == 'directory':
 
1003
                            reparent = True
922
1004
                if entry.parent_id not in file_trans_id:
923
1005
                    raise repr(entry.parent_id)
924
1006
                parent_id = file_trans_id[entry.parent_id]
925
 
                file_trans_id[file_id] = new_by_entry(tt, entry, parent_id, 
 
1007
                file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
926
1008
                                                      tree)
 
1009
                if reparent:
 
1010
                    new_trans_id = file_trans_id[file_id]
 
1011
                    old_parent = tt.trans_id_tree_path(tree_path)
 
1012
                    _reparent_children(tt, old_parent, new_trans_id)
927
1013
        finally:
928
1014
            pb.finished()
929
1015
        pp.next_phase()
 
1016
        divert_trans = set(file_trans_id[f] for f in divert)
 
1017
        resolver = lambda t, c: resolve_checkout(t, c, divert_trans)
 
1018
        raw_conflicts = resolve_conflicts(tt, pass_func=resolver)
 
1019
        conflicts = cook_conflicts(raw_conflicts, tt)
 
1020
        for conflict in conflicts:
 
1021
            warning(conflict)
 
1022
        try:
 
1023
            wt.add_conflicts(conflicts)
 
1024
        except errors.UnsupportedOperation:
 
1025
            pass
930
1026
        tt.apply()
931
1027
    finally:
932
1028
        tt.finalize()
933
1029
        top_pb.finished()
934
1030
 
 
1031
 
 
1032
def _reparent_children(tt, old_parent, new_parent):
 
1033
    for child in tt.iter_tree_children(old_parent):
 
1034
        tt.adjust_path(tt.final_name(child), new_parent, child)
 
1035
 
 
1036
 
 
1037
def _content_match(tree, entry, file_id, kind, target_path):
 
1038
    if entry.kind != kind:
 
1039
        return False
 
1040
    if entry.kind == "directory":
 
1041
        return True
 
1042
    if entry.kind == "file":
 
1043
        if tree.get_file(file_id).read() == file(target_path, 'rb').read():
 
1044
            return True
 
1045
    elif entry.kind == "symlink":
 
1046
        if tree.get_symlink_target(file_id) == os.readlink(target_path):
 
1047
            return True
 
1048
    return False
 
1049
 
 
1050
 
 
1051
def resolve_checkout(tt, conflicts, divert):
 
1052
    new_conflicts = set()
 
1053
    for c_type, conflict in ((c[0], c) for c in conflicts):
 
1054
        # Anything but a 'duplicate' would indicate programmer error
 
1055
        assert c_type == 'duplicate', c_type
 
1056
        # Now figure out which is new and which is old
 
1057
        if tt.new_contents(conflict[1]):
 
1058
            new_file = conflict[1]
 
1059
            old_file = conflict[2]
 
1060
        else:
 
1061
            new_file = conflict[2]
 
1062
            old_file = conflict[1]
 
1063
 
 
1064
        # We should only get here if the conflict wasn't completely
 
1065
        # resolved
 
1066
        final_parent = tt.final_parent(old_file)
 
1067
        if new_file in divert:
 
1068
            new_name = tt.final_name(old_file)+'.diverted'
 
1069
            tt.adjust_path(new_name, final_parent, new_file)
 
1070
            new_conflicts.add((c_type, 'Diverted to',
 
1071
                               new_file, old_file))
 
1072
        else:
 
1073
            new_name = tt.final_name(old_file)+'.moved'
 
1074
            tt.adjust_path(new_name, final_parent, old_file)
 
1075
            new_conflicts.add((c_type, 'Moved existing file to',
 
1076
                               old_file, new_file))
 
1077
    return new_conflicts
 
1078
 
 
1079
 
935
1080
def new_by_entry(tt, entry, parent_id, tree):
936
1081
    """Create a new file according to its inventory entry"""
937
1082
    name = entry.name
950
1095
def create_by_entry(tt, entry, tree, trans_id, lines=None, mode_id=None):
951
1096
    """Create new file contents according to an inventory entry."""
952
1097
    if entry.kind == "file":
953
 
        if lines == None:
 
1098
        if lines is None:
954
1099
            lines = tree.get_file(entry.file_id).readlines()
955
1100
        tt.create_file(lines, trans_id, mode_id=mode_id)
956
1101
    elif entry.kind == "symlink":
966
1111
 
967
1112
def find_interesting(working_tree, target_tree, filenames):
968
1113
    """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
 
1114
    trees = (working_tree, target_tree)
 
1115
    return tree.find_ids_across_trees(filenames, trees)
983
1116
 
984
1117
 
985
1118
def change_entry(tt, file_id, working_tree, target_tree, 
1023
1156
 
1024
1157
 
1025
1158
def get_backup_name(entry, by_parent, parent_trans_id, tt):
 
1159
    return _get_backup_name(entry.name, by_parent, parent_trans_id, tt)
 
1160
 
 
1161
 
 
1162
def _get_backup_name(name, by_parent, parent_trans_id, tt):
1026
1163
    """Produce a backup-style name that appears to be available"""
1027
1164
    def name_gen():
1028
1165
        counter = 1
1029
1166
        while True:
1030
 
            yield "%s.~%d~" % (entry.name, counter)
 
1167
            yield "%s.~%d~" % (name, counter)
1031
1168
            counter += 1
1032
 
    for name in name_gen():
1033
 
        if not tt.has_named_child(by_parent, parent_trans_id, name):
1034
 
            return name
 
1169
    for new_name in name_gen():
 
1170
        if not tt.has_named_child(by_parent, parent_trans_id, new_name):
 
1171
            return new_name
 
1172
 
1035
1173
 
1036
1174
def _entry_changes(file_id, entry, working_tree):
1037
1175
    """Determine in which ways the inventory entry has changed.
1045
1183
    try:
1046
1184
        working_kind = working_tree.kind(file_id)
1047
1185
        has_contents = True
1048
 
    except OSError, e:
1049
 
        if e.errno != errno.ENOENT:
1050
 
            raise
 
1186
    except NoSuchFile:
1051
1187
        has_contents = False
1052
1188
        contents_mod = True
1053
1189
        meta_mod = False
1054
1190
    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:
 
1191
        if entry.kind != working_kind:
1059
1192
            contents_mod, meta_mod = True, False
1060
1193
        else:
1061
1194
            cur_entry._read_tree_state(working_tree.id2path(file_id), 
1069
1202
           pb=DummyProgress()):
1070
1203
    """Revert a working tree's contents to those of a target tree."""
1071
1204
    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
 
 
1075
1205
    tt = TreeTransform(working_tree, pb)
1076
1206
    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]
 
1207
        pp = ProgressPhase("Revert phase", 3, pb)
 
1208
        pp.next_phase()
 
1209
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1210
        try:
 
1211
            _alter_files(working_tree, target_tree, tt, child_pb, 
 
1212
                         interesting_ids, backups)
1123
1213
        finally:
1124
1214
            child_pb.finished()
1125
1215
        pp.next_phase()
1140
1230
    return conflicts
1141
1231
 
1142
1232
 
1143
 
def resolve_conflicts(tt, pb=DummyProgress()):
 
1233
def _alter_files(working_tree, target_tree, tt, pb, interesting_ids, backups):
 
1234
    merge_modified = working_tree.merge_modified()
 
1235
    iterator = target_tree._iter_changes(working_tree, 
 
1236
                                         specific_file_ids=interesting_ids,
 
1237
                                         pb=pb)
 
1238
    if target_tree.inventory.root is None:
 
1239
        skip_root = True
 
1240
    else:
 
1241
        skip_root = False
 
1242
    basis_tree = None
 
1243
    for id_num, (file_id, path, changed_content, versioned, parent, name, kind,
 
1244
                 executable) in enumerate(iterator):
 
1245
        if skip_root and file_id[0] is not None and parent[0] is None:
 
1246
            continue
 
1247
        trans_id = tt.trans_id_file_id(file_id)
 
1248
        mode_id = None
 
1249
        if changed_content:
 
1250
            keep_content = False
 
1251
            if kind[0] == 'file' and (backups or kind[1] is None):
 
1252
                wt_sha1 = working_tree.get_file_sha1(file_id)
 
1253
                if merge_modified.get(file_id) != wt_sha1:
 
1254
                    if basis_tree is None:
 
1255
                        basis_tree = working_tree.basis_tree()
 
1256
                    if file_id in basis_tree:
 
1257
                        if wt_sha1 != basis_tree.get_file_sha1(file_id):
 
1258
                            keep_content = True
 
1259
                    elif kind[1] is None and not versioned[1]:
 
1260
                        keep_content = True
 
1261
            if kind[0] is not None:
 
1262
                if not keep_content:
 
1263
                    tt.delete_contents(trans_id)
 
1264
                elif kind[1] is not None:
 
1265
                    parent_trans_id = tt.trans_id_file_id(parent[0])
 
1266
                    by_parent = tt.by_parent()
 
1267
                    backup_name = _get_backup_name(name[0], by_parent,
 
1268
                                                   parent_trans_id, tt)
 
1269
                    tt.adjust_path(backup_name, parent_trans_id, trans_id)
 
1270
                    new_trans_id = tt.create_path(name[0], parent_trans_id)
 
1271
                    if versioned == (True, True):
 
1272
                        tt.unversion_file(trans_id)
 
1273
                        tt.version_file(file_id, new_trans_id)
 
1274
                    # New contents should have the same unix perms as old
 
1275
                    # contents
 
1276
                    mode_id = trans_id
 
1277
                    trans_id = new_trans_id
 
1278
            if kind[1] == 'directory':
 
1279
                tt.create_directory(trans_id)
 
1280
            elif kind[1] == 'symlink':
 
1281
                tt.create_symlink(target_tree.get_symlink_target(file_id),
 
1282
                                  trans_id)
 
1283
            elif kind[1] == 'file':
 
1284
                tt.create_file(target_tree.get_file_lines(file_id),
 
1285
                               trans_id, mode_id)
 
1286
                # preserve the execute bit when backing up
 
1287
                if keep_content and executable[0] == executable[1]:
 
1288
                    tt.set_executability(executable[1], trans_id)
 
1289
            else:
 
1290
                assert kind[1] is None
 
1291
        if versioned == (False, True):
 
1292
            tt.version_file(file_id, trans_id)
 
1293
        if versioned == (True, False):
 
1294
            tt.unversion_file(trans_id)
 
1295
        if (name[1] is not None and 
 
1296
            (name[0] != name[1] or parent[0] != parent[1])):
 
1297
            tt.adjust_path(name[1], tt.trans_id_file_id(parent[1]), trans_id)
 
1298
        if executable[0] != executable[1] and kind[1] == "file":
 
1299
            tt.set_executability(executable[1], trans_id)
 
1300
 
 
1301
 
 
1302
def resolve_conflicts(tt, pb=DummyProgress(), pass_func=None):
1144
1303
    """Make many conflict-resolution attempts, but die if they fail"""
 
1304
    if pass_func is None:
 
1305
        pass_func = conflict_pass
1145
1306
    new_conflicts = set()
1146
1307
    try:
1147
1308
        for n in range(10):
1149
1310
            conflicts = tt.find_conflicts()
1150
1311
            if len(conflicts) == 0:
1151
1312
                return new_conflicts
1152
 
            new_conflicts.update(conflict_pass(tt, conflicts))
 
1313
            new_conflicts.update(pass_func(tt, conflicts))
1153
1314
        raise MalformedTransform(conflicts=conflicts)
1154
1315
    finally:
1155
1316
        pb.clear()
1188
1349
            trans_id = conflict[1]
1189
1350
            try:
1190
1351
                tt.cancel_deletion(trans_id)
1191
 
                new_conflicts.add((c_type, 'Not deleting', trans_id))
 
1352
                new_conflicts.add(('deleting parent', 'Not deleting', 
 
1353
                                   trans_id))
1192
1354
            except KeyError:
1193
1355
                tt.create_directory(trans_id)
1194
 
                new_conflicts.add((c_type, 'Created directory.', trans_id))
 
1356
                new_conflicts.add((c_type, 'Created directory', trans_id))
1195
1357
        elif c_type == 'unversioned parent':
1196
1358
            tt.version_file(tt.inactive_file_id(conflict[1]), conflict[1])
1197
1359
            new_conflicts.add((c_type, 'Versioned directory', conflict[1]))
1198
1360
    return new_conflicts
1199
1361
 
 
1362
 
1200
1363
def cook_conflicts(raw_conflicts, tt):
1201
1364
    """Generate a list of cooked conflicts, sorted by file path"""
1202
 
    def key(conflict):
1203
 
        if conflict.path is not None:
1204
 
            return conflict.path, conflict.typestring
1205
 
        elif getattr(conflict, "conflict_path", None) is not None:
1206
 
            return conflict.conflict_path, conflict.typestring
1207
 
        else:
1208
 
            return None, conflict.typestring
 
1365
    from bzrlib.conflicts import Conflict
 
1366
    conflict_iter = iter_cook_conflicts(raw_conflicts, tt)
 
1367
    return sorted(conflict_iter, key=Conflict.sort_key)
1209
1368
 
1210
 
    return sorted(list(iter_cook_conflicts(raw_conflicts, tt)), key=key)
1211
1369
 
1212
1370
def iter_cook_conflicts(raw_conflicts, tt):
1213
1371
    from bzrlib.conflicts import Conflict