/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: Robert Collins
  • Date: 2008-02-13 03:30:01 UTC
  • mfrom: (3221 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3224.
  • Revision ID: robertc@robertcollins.net-20080213033001-rw70ul0zb02ph856
Merge to fix conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2008 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
17
17
import os
18
18
import errno
19
19
from stat import S_ISREG
 
20
import tempfile
20
21
 
21
22
from bzrlib.lazy_import import lazy_import
22
23
lazy_import(globals(), """
29
30
""")
30
31
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
31
32
                           ReusingTransform, NotVersionedError, CantMoveRoot,
32
 
                           ExistingLimbo, ImmortalLimbo, NoFinalPath)
 
33
                           ExistingLimbo, ImmortalLimbo, NoFinalPath,
 
34
                           UnableCreateSymlink)
33
35
from bzrlib.inventory import InventoryEntry
34
36
from bzrlib.osutils import (file_kind, supports_executable, pathjoin, lexists,
35
 
                            delete_any)
 
37
                            delete_any, has_symlinks)
36
38
from bzrlib.progress import DummyProgress, ProgressPhase
37
39
from bzrlib.symbol_versioning import (
38
40
        deprecated_function,
61
63
        self.rename_count = rename_count
62
64
 
63
65
 
64
 
class TreeTransform(object):
65
 
    """Represent a tree transformation.
66
 
    
67
 
    This object is designed to support incremental generation of the transform,
68
 
    in any order.
69
 
 
70
 
    However, it gives optimum performance when parent directories are created
71
 
    before their contents.  The transform is then able to put child files
72
 
    directly in their parent directory, avoiding later renames.
73
 
    
74
 
    It is easy to produce malformed transforms, but they are generally
75
 
    harmless.  Attempting to apply a malformed transform will cause an
76
 
    exception to be raised before any modifications are made to the tree.  
77
 
 
78
 
    Many kinds of malformed transforms can be corrected with the 
79
 
    resolve_conflicts function.  The remaining ones indicate programming error,
80
 
    such as trying to create a file with no path.
81
 
 
82
 
    Two sets of file creation methods are supplied.  Convenience methods are:
83
 
     * new_file
84
 
     * new_directory
85
 
     * new_symlink
86
 
 
87
 
    These are composed of the low-level methods:
88
 
     * create_path
89
 
     * create_file or create_directory or create_symlink
90
 
     * version_file
91
 
     * set_executability
92
 
    """
93
 
    def __init__(self, tree, pb=DummyProgress()):
94
 
        """Note: a tree_write lock is taken on the tree.
95
 
        
96
 
        Use TreeTransform.finalize() to release the lock (can be omitted if
97
 
        TreeTransform.apply() called).
 
66
class TreeTransformBase(object):
 
67
    """The base class for TreeTransform and TreeTransformBase"""
 
68
 
 
69
    def __init__(self, tree, limbodir, pb=DummyProgress(),
 
70
                 case_sensitive=True):
 
71
        """Constructor.
 
72
 
 
73
        :param tree: The tree that will be transformed, but not necessarily
 
74
            the output tree.
 
75
        :param limbodir: A directory where new files can be stored until
 
76
            they are installed in their proper places
 
77
        :param pb: A ProgressBar indicating how much progress is being made
 
78
        :param case_sensitive: If True, the target of the transform is
 
79
            case sensitive, not just case preserving.
98
80
        """
99
81
        object.__init__(self)
100
82
        self._tree = tree
101
 
        self._tree.lock_tree_write()
102
 
        try:
103
 
            control_files = self._tree._control_files
104
 
            self._limbodir = urlutils.local_path_from_url(
105
 
                control_files.controlfilename('limbo'))
106
 
            try:
107
 
                os.mkdir(self._limbodir)
108
 
            except OSError, e:
109
 
                if e.errno == errno.EEXIST:
110
 
                    raise ExistingLimbo(self._limbodir)
111
 
            self._deletiondir = urlutils.local_path_from_url(
112
 
                control_files.controlfilename('pending-deletion'))
113
 
            try:
114
 
                os.mkdir(self._deletiondir)
115
 
            except OSError, e:
116
 
                if e.errno == errno.EEXIST:
117
 
                    raise errors.ExistingPendingDeletion(self._deletiondir)
118
 
 
119
 
        except: 
120
 
            self._tree.unlock()
121
 
            raise
122
 
 
 
83
        self._limbodir = limbodir
 
84
        self._deletiondir = None
123
85
        self._id_number = 0
 
86
        # mapping of trans_id -> new basename
124
87
        self._new_name = {}
 
88
        # mapping of trans_id -> new parent trans_id
125
89
        self._new_parent = {}
 
90
        # mapping of trans_id with new contents -> new file_kind
126
91
        self._new_contents = {}
127
92
        # A mapping of transform ids to their limbo filename
128
93
        self._limbo_files = {}
133
98
        self._limbo_children_names = {}
134
99
        # List of transform ids that need to be renamed from limbo into place
135
100
        self._needs_rename = set()
 
101
        # Set of trans_ids whose contents will be removed
136
102
        self._removed_contents = set()
 
103
        # Mapping of trans_id -> new execute-bit value
137
104
        self._new_executability = {}
 
105
        # Mapping of trans_id -> new tree-reference value
138
106
        self._new_reference_revision = {}
 
107
        # Mapping of trans_id -> new file_id
139
108
        self._new_id = {}
 
109
        # Mapping of old file-id -> trans_id
140
110
        self._non_present_ids = {}
 
111
        # Mapping of new file_id -> trans_id
141
112
        self._r_new_id = {}
 
113
        # Set of file_ids that will be removed
142
114
        self._removed_id = set()
 
115
        # Mapping of path in old tree -> trans_id
143
116
        self._tree_path_ids = {}
 
117
        # Mapping trans_id -> path in old tree
144
118
        self._tree_id_paths = {}
145
119
        # Cache of realpath results, to speed up canonical_path
146
120
        self._realpaths = {}
147
121
        # Cache of relpath results, to speed up canonical_path
148
122
        self._relpaths = {}
 
123
        # The trans_id that will be used as the tree root
149
124
        self._new_root = self.trans_id_tree_file_id(tree.get_root_id())
150
 
        self.__done = False
 
125
        # Indictor of whether the transform has been applied
 
126
        self._done = False
 
127
        # A progress bar
151
128
        self._pb = pb
 
129
        # Whether the target is case sensitive
 
130
        self._case_sensitive_target = case_sensitive
 
131
        # A counter of how many files have been renamed
152
132
        self.rename_count = 0
153
133
 
154
134
    def __get_root(self):
179
159
                # We don't especially care *why* the dir is immortal.
180
160
                raise ImmortalLimbo(self._limbodir)
181
161
            try:
182
 
                os.rmdir(self._deletiondir)
 
162
                if self._deletiondir is not None:
 
163
                    os.rmdir(self._deletiondir)
183
164
            except OSError:
184
165
                raise errors.ImmortalPendingDeletion(self._deletiondir)
185
166
        finally:
387
368
        target is a bytestring.
388
369
        See also new_symlink.
389
370
        """
390
 
        os.symlink(target, self._limbo_name(trans_id))
391
 
        unique_add(self._new_contents, trans_id, 'symlink')
 
371
        if has_symlinks():
 
372
            os.symlink(target, self._limbo_name(trans_id))
 
373
            unique_add(self._new_contents, trans_id, 'symlink')
 
374
        else:
 
375
            try:
 
376
                path = FinalPaths(self).get_path(trans_id)
 
377
            except KeyError:
 
378
                path = None
 
379
            raise UnableCreateSymlink(path=path)
392
380
 
393
381
    def cancel_creation(self, trans_id):
394
382
        """Cancel the creation of new file contents."""
569
557
 
570
558
    def find_conflicts(self):
571
559
        """Find any violations of inventory or filesystem invariants"""
572
 
        if self.__done is True:
 
560
        if self._done is True:
573
561
            raise ReusingTransform()
574
562
        conflicts = []
575
563
        # ensure all children of all existent parents are known
597
585
                        self.tree_kind(t) == 'directory'])
598
586
        for trans_id in self._removed_id:
599
587
            file_id = self.tree_file_id(trans_id)
600
 
            if self._tree.inventory[file_id].kind == 'directory':
 
588
            if file_id is not None:
 
589
                if self._tree.inventory[file_id].kind == 'directory':
 
590
                    parents.append(trans_id)
 
591
            elif self.tree_kind(trans_id) == 'directory':
601
592
                parents.append(trans_id)
602
593
 
603
594
        for parent_id in parents:
613
604
        try:
614
605
            children = os.listdir(self._tree.abspath(path))
615
606
        except OSError, e:
616
 
            if e.errno != errno.ENOENT and e.errno != errno.ESRCH:
 
607
            if e.errno not in (errno.ENOENT, errno.ESRCH, errno.ENOTDIR):
617
608
                raise
618
609
            return
619
610
            
737
728
            return conflicts
738
729
        for children in by_parent.itervalues():
739
730
            name_ids = [(self.final_name(t), t) for t in children]
 
731
            if not self._case_sensitive_target:
 
732
                name_ids = [(n.lower(), t) for n, t in name_ids]
740
733
            name_ids.sort()
741
734
            last_name = None
742
735
            last_trans_id = None
800
793
                continue
801
794
            return True
802
795
        return False
803
 
            
804
 
    def apply(self, no_conflicts=False, _mover=None):
805
 
        """Apply all changes to the inventory and filesystem.
806
 
        
807
 
        If filesystem or inventory conflicts are present, MalformedTransform
808
 
        will be thrown.
809
 
 
810
 
        If apply succeeds, finalize is not necessary.
811
 
 
812
 
        :param no_conflicts: if True, the caller guarantees there are no
813
 
            conflicts, so no check is made.
814
 
        :param _mover: Supply an alternate FileMover, for testing
815
 
        """
816
 
        if not no_conflicts:
817
 
            conflicts = self.find_conflicts()
818
 
            if len(conflicts) != 0:
819
 
                raise MalformedTransform(conflicts=conflicts)
820
 
        inv = self._tree.inventory
821
 
        inventory_delta = []
822
 
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
823
 
        try:
824
 
            if _mover is None:
825
 
                mover = _FileMover()
826
 
            else:
827
 
                mover = _mover
828
 
            try:
829
 
                child_pb.update('Apply phase', 0, 2)
830
 
                self._apply_removals(inv, inventory_delta, mover)
831
 
                child_pb.update('Apply phase', 1, 2)
832
 
                modified_paths = self._apply_insertions(inv, inventory_delta,
833
 
                                                        mover)
834
 
            except:
835
 
                mover.rollback()
836
 
                raise
837
 
            else:
838
 
                mover.apply_deletions()
839
 
        finally:
840
 
            child_pb.finished()
841
 
        self._tree.apply_inventory_delta(inventory_delta)
842
 
        self.__done = True
843
 
        self.finalize()
844
 
        return _TransformResults(modified_paths, self.rename_count)
845
796
 
846
797
    def _limbo_name(self, trans_id):
847
798
        """Generate the limbo name of a file"""
863
814
                # the direct path can only be used if no other file has
864
815
                # already taken this pathname, i.e. if the name is unused, or
865
816
                # if it is already associated with this trans_id.
866
 
                elif (self._limbo_children_names[parent].get(filename)
867
 
                      in (trans_id, None)):
868
 
                    use_direct_path = True
 
817
                elif self._case_sensitive_target:
 
818
                    if (self._limbo_children_names[parent].get(filename)
 
819
                        in (trans_id, None)):
 
820
                        use_direct_path = True
 
821
                else:
 
822
                    for l_filename, l_trans_id in\
 
823
                        self._limbo_children_names[parent].iteritems():
 
824
                        if l_trans_id == trans_id:
 
825
                            continue
 
826
                        if l_filename.lower() == filename.lower():
 
827
                            break
 
828
                    else:
 
829
                        use_direct_path = True
 
830
 
869
831
        if use_direct_path:
870
832
            limbo_name = pathjoin(self._limbo_files[parent], filename)
871
833
            self._limbo_children[parent].add(trans_id)
876
838
        self._limbo_files[trans_id] = limbo_name
877
839
        return limbo_name
878
840
 
879
 
    def _apply_removals(self, inv, inventory_delta, mover):
880
 
        """Perform tree operations that remove directory/inventory names.
881
 
        
882
 
        That is, delete files that are to be deleted, and put any files that
883
 
        need renaming into limbo.  This must be done in strict child-to-parent
884
 
        order.
885
 
        """
886
 
        tree_paths = list(self._tree_path_ids.iteritems())
887
 
        tree_paths.sort(reverse=True)
888
 
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
889
 
        try:
890
 
            for num, data in enumerate(tree_paths):
891
 
                path, trans_id = data
892
 
                child_pb.update('removing file', num, len(tree_paths))
893
 
                full_path = self._tree.abspath(path)
894
 
                if trans_id in self._removed_contents:
895
 
                    mover.pre_delete(full_path, os.path.join(self._deletiondir,
896
 
                                     trans_id))
897
 
                elif trans_id in self._new_name or trans_id in \
898
 
                    self._new_parent:
899
 
                    try:
900
 
                        mover.rename(full_path, self._limbo_name(trans_id))
901
 
                    except OSError, e:
902
 
                        if e.errno != errno.ENOENT:
903
 
                            raise
904
 
                    else:
905
 
                        self.rename_count += 1
906
 
                if trans_id in self._removed_id:
907
 
                    if trans_id == self._new_root:
908
 
                        file_id = self._tree.get_root_id()
909
 
                    else:
910
 
                        file_id = self.tree_file_id(trans_id)
911
 
                    assert file_id is not None
912
 
                    inventory_delta.append((path, None, file_id, None))
913
 
        finally:
914
 
            child_pb.finished()
915
 
 
916
 
    def _apply_insertions(self, inv, inventory_delta, mover):
917
 
        """Perform tree operations that insert directory/inventory names.
918
 
        
919
 
        That is, create any files that need to be created, and restore from
920
 
        limbo any files that needed renaming.  This must be done in strict
921
 
        parent-to-child order.
922
 
        """
923
 
        new_paths = self.new_paths()
924
 
        modified_paths = []
925
 
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
926
 
        try:
927
 
            for num, (path, trans_id) in enumerate(new_paths):
928
 
                new_entry = None
929
 
                child_pb.update('adding file', num, len(new_paths))
930
 
                try:
931
 
                    kind = self._new_contents[trans_id]
932
 
                except KeyError:
933
 
                    kind = contents = None
934
 
                if trans_id in self._new_contents or \
935
 
                    self.path_changed(trans_id):
936
 
                    full_path = self._tree.abspath(path)
937
 
                    if trans_id in self._needs_rename:
938
 
                        try:
939
 
                            mover.rename(self._limbo_name(trans_id), full_path)
940
 
                        except OSError, e:
941
 
                            # We may be renaming a dangling inventory id
942
 
                            if e.errno != errno.ENOENT:
943
 
                                raise
944
 
                        else:
945
 
                            self.rename_count += 1
946
 
                    if trans_id in self._new_contents:
947
 
                        modified_paths.append(full_path)
948
 
                        del self._new_contents[trans_id]
949
 
 
950
 
                if trans_id in self._new_id:
951
 
                    if kind is None:
952
 
                        kind = file_kind(self._tree.abspath(path))
953
 
                    if trans_id in self._new_reference_revision:
954
 
                        new_entry = inventory.TreeReference(
955
 
                            self._new_id[trans_id],
956
 
                            self._new_name[trans_id], 
957
 
                            self.final_file_id(self._new_parent[trans_id]),
958
 
                            None, self._new_reference_revision[trans_id])
959
 
                    else:
960
 
                        new_entry = inventory.make_entry(kind,
961
 
                            self.final_name(trans_id),
962
 
                            self.final_file_id(self.final_parent(trans_id)),
963
 
                            self._new_id[trans_id])
964
 
                else:
965
 
                    if trans_id in self._new_name or trans_id in\
966
 
                        self._new_parent or\
967
 
                        trans_id in self._new_executability:
968
 
                        file_id = self.final_file_id(trans_id)
969
 
                        if file_id is not None:
970
 
                            entry = inv[file_id]
971
 
                            new_entry = entry.copy()
972
 
 
973
 
                    if trans_id in self._new_name or trans_id in\
974
 
                        self._new_parent:
975
 
                            if new_entry is not None:
976
 
                                new_entry.name = self.final_name(trans_id)
977
 
                                parent = self.final_parent(trans_id)
978
 
                                parent_id = self.final_file_id(parent)
979
 
                                new_entry.parent_id = parent_id
980
 
 
981
 
                if trans_id in self._new_executability:
982
 
                    self._set_executability(path, new_entry, trans_id)
983
 
                if new_entry is not None:
984
 
                    if new_entry.file_id in inv:
985
 
                        old_path = inv.id2path(new_entry.file_id)
986
 
                    else:
987
 
                        old_path = None
988
 
                    inventory_delta.append((old_path, path,
989
 
                                            new_entry.file_id,
990
 
                                            new_entry))
991
 
        finally:
992
 
            child_pb.finished()
993
 
        return modified_paths
994
 
 
995
841
    def _set_executability(self, path, entry, trans_id):
996
842
        """Set the executability of versioned files """
997
843
        new_executability = self._new_executability[trans_id]
1201
1047
                   (from_executable, to_executable)))
1202
1048
        return iter(sorted(results, key=lambda x:x[1]))
1203
1049
 
 
1050
    def get_preview_tree(self):
 
1051
        """Return a tree representing the result of the transform.
 
1052
 
 
1053
        This tree only supports the subset of Tree functionality required
 
1054
        by show_diff_trees.  It must only be compared to tt._tree.
 
1055
        """
 
1056
        return _PreviewTree(self)
 
1057
 
 
1058
 
 
1059
class TreeTransform(TreeTransformBase):
 
1060
    """Represent a tree transformation.
 
1061
 
 
1062
    This object is designed to support incremental generation of the transform,
 
1063
    in any order.
 
1064
 
 
1065
    However, it gives optimum performance when parent directories are created
 
1066
    before their contents.  The transform is then able to put child files
 
1067
    directly in their parent directory, avoiding later renames.
 
1068
 
 
1069
    It is easy to produce malformed transforms, but they are generally
 
1070
    harmless.  Attempting to apply a malformed transform will cause an
 
1071
    exception to be raised before any modifications are made to the tree.
 
1072
 
 
1073
    Many kinds of malformed transforms can be corrected with the
 
1074
    resolve_conflicts function.  The remaining ones indicate programming error,
 
1075
    such as trying to create a file with no path.
 
1076
 
 
1077
    Two sets of file creation methods are supplied.  Convenience methods are:
 
1078
     * new_file
 
1079
     * new_directory
 
1080
     * new_symlink
 
1081
 
 
1082
    These are composed of the low-level methods:
 
1083
     * create_path
 
1084
     * create_file or create_directory or create_symlink
 
1085
     * version_file
 
1086
     * set_executability
 
1087
 
 
1088
    Transform/Transaction ids
 
1089
    -------------------------
 
1090
    trans_ids are temporary ids assigned to all files involved in a transform.
 
1091
    It's possible, even common, that not all files in the Tree have trans_ids.
 
1092
 
 
1093
    trans_ids are used because filenames and file_ids are not good enough
 
1094
    identifiers; filenames change, and not all files have file_ids.  File-ids
 
1095
    are also associated with trans-ids, so that moving a file moves its
 
1096
    file-id.
 
1097
 
 
1098
    trans_ids are only valid for the TreeTransform that generated them.
 
1099
 
 
1100
    Limbo
 
1101
    -----
 
1102
    Limbo is a temporary directory use to hold new versions of files.
 
1103
    Files are added to limbo by create_file, create_directory, create_symlink,
 
1104
    and their convenience variants (new_*).  Files may be removed from limbo
 
1105
    using cancel_creation.  Files are renamed from limbo into their final
 
1106
    location as part of TreeTransform.apply
 
1107
 
 
1108
    Limbo must be cleaned up, by either calling TreeTransform.apply or
 
1109
    calling TreeTransform.finalize.
 
1110
 
 
1111
    Files are placed into limbo inside their parent directories, where
 
1112
    possible.  This reduces subsequent renames, and makes operations involving
 
1113
    lots of files faster.  This optimization is only possible if the parent
 
1114
    directory is created *before* creating any of its children, so avoid
 
1115
    creating children before parents, where possible.
 
1116
 
 
1117
    Pending-deletion
 
1118
    ----------------
 
1119
    This temporary directory is used by _FileMover for storing files that are
 
1120
    about to be deleted.  In case of rollback, the files will be restored.
 
1121
    FileMover does not delete files until it is sure that a rollback will not
 
1122
    happen.
 
1123
    """
 
1124
    def __init__(self, tree, pb=DummyProgress()):
 
1125
        """Note: a tree_write lock is taken on the tree.
 
1126
 
 
1127
        Use TreeTransform.finalize() to release the lock (can be omitted if
 
1128
        TreeTransform.apply() called).
 
1129
        """
 
1130
        tree.lock_tree_write()
 
1131
 
 
1132
        try:
 
1133
            control_files = tree._control_files
 
1134
            limbodir = urlutils.local_path_from_url(
 
1135
                control_files.controlfilename('limbo'))
 
1136
            try:
 
1137
                os.mkdir(limbodir)
 
1138
            except OSError, e:
 
1139
                if e.errno == errno.EEXIST:
 
1140
                    raise ExistingLimbo(limbodir)
 
1141
            deletiondir = urlutils.local_path_from_url(
 
1142
                control_files.controlfilename('pending-deletion'))
 
1143
            try:
 
1144
                os.mkdir(deletiondir)
 
1145
            except OSError, e:
 
1146
                if e.errno == errno.EEXIST:
 
1147
                    raise errors.ExistingPendingDeletion(deletiondir)
 
1148
        except:
 
1149
            tree.unlock()
 
1150
            raise
 
1151
 
 
1152
        TreeTransformBase.__init__(self, tree, limbodir, pb,
 
1153
                                   tree.case_sensitive)
 
1154
        self._deletiondir = deletiondir
 
1155
 
 
1156
    def apply(self, no_conflicts=False, _mover=None):
 
1157
        """Apply all changes to the inventory and filesystem.
 
1158
 
 
1159
        If filesystem or inventory conflicts are present, MalformedTransform
 
1160
        will be thrown.
 
1161
 
 
1162
        If apply succeeds, finalize is not necessary.
 
1163
 
 
1164
        :param no_conflicts: if True, the caller guarantees there are no
 
1165
            conflicts, so no check is made.
 
1166
        :param _mover: Supply an alternate FileMover, for testing
 
1167
        """
 
1168
        if not no_conflicts:
 
1169
            conflicts = self.find_conflicts()
 
1170
            if len(conflicts) != 0:
 
1171
                raise MalformedTransform(conflicts=conflicts)
 
1172
        inv = self._tree.inventory
 
1173
        inventory_delta = []
 
1174
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1175
        try:
 
1176
            if _mover is None:
 
1177
                mover = _FileMover()
 
1178
            else:
 
1179
                mover = _mover
 
1180
            try:
 
1181
                child_pb.update('Apply phase', 0, 2)
 
1182
                self._apply_removals(inv, inventory_delta, mover)
 
1183
                child_pb.update('Apply phase', 1, 2)
 
1184
                modified_paths = self._apply_insertions(inv, inventory_delta,
 
1185
                                                        mover)
 
1186
            except:
 
1187
                mover.rollback()
 
1188
                raise
 
1189
            else:
 
1190
                mover.apply_deletions()
 
1191
        finally:
 
1192
            child_pb.finished()
 
1193
        self._tree.apply_inventory_delta(inventory_delta)
 
1194
        self._done = True
 
1195
        self.finalize()
 
1196
        return _TransformResults(modified_paths, self.rename_count)
 
1197
 
 
1198
    def _apply_removals(self, inv, inventory_delta, mover):
 
1199
        """Perform tree operations that remove directory/inventory names.
 
1200
 
 
1201
        That is, delete files that are to be deleted, and put any files that
 
1202
        need renaming into limbo.  This must be done in strict child-to-parent
 
1203
        order.
 
1204
        """
 
1205
        tree_paths = list(self._tree_path_ids.iteritems())
 
1206
        tree_paths.sort(reverse=True)
 
1207
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1208
        try:
 
1209
            for num, data in enumerate(tree_paths):
 
1210
                path, trans_id = data
 
1211
                child_pb.update('removing file', num, len(tree_paths))
 
1212
                full_path = self._tree.abspath(path)
 
1213
                if trans_id in self._removed_contents:
 
1214
                    mover.pre_delete(full_path, os.path.join(self._deletiondir,
 
1215
                                     trans_id))
 
1216
                elif trans_id in self._new_name or trans_id in \
 
1217
                    self._new_parent:
 
1218
                    try:
 
1219
                        mover.rename(full_path, self._limbo_name(trans_id))
 
1220
                    except OSError, e:
 
1221
                        if e.errno != errno.ENOENT:
 
1222
                            raise
 
1223
                    else:
 
1224
                        self.rename_count += 1
 
1225
                if trans_id in self._removed_id:
 
1226
                    if trans_id == self._new_root:
 
1227
                        file_id = self._tree.get_root_id()
 
1228
                    else:
 
1229
                        file_id = self.tree_file_id(trans_id)
 
1230
                    if file_id is not None:
 
1231
                        inventory_delta.append((path, None, file_id, None))
 
1232
        finally:
 
1233
            child_pb.finished()
 
1234
 
 
1235
    def _apply_insertions(self, inv, inventory_delta, mover):
 
1236
        """Perform tree operations that insert directory/inventory names.
 
1237
 
 
1238
        That is, create any files that need to be created, and restore from
 
1239
        limbo any files that needed renaming.  This must be done in strict
 
1240
        parent-to-child order.
 
1241
        """
 
1242
        new_paths = self.new_paths()
 
1243
        modified_paths = []
 
1244
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1245
        completed_new = []
 
1246
        try:
 
1247
            for num, (path, trans_id) in enumerate(new_paths):
 
1248
                new_entry = None
 
1249
                child_pb.update('adding file', num, len(new_paths))
 
1250
                try:
 
1251
                    kind = self._new_contents[trans_id]
 
1252
                except KeyError:
 
1253
                    kind = contents = None
 
1254
                if trans_id in self._new_contents or \
 
1255
                    self.path_changed(trans_id):
 
1256
                    full_path = self._tree.abspath(path)
 
1257
                    if trans_id in self._needs_rename:
 
1258
                        try:
 
1259
                            mover.rename(self._limbo_name(trans_id), full_path)
 
1260
                        except OSError, e:
 
1261
                            # We may be renaming a dangling inventory id
 
1262
                            if e.errno != errno.ENOENT:
 
1263
                                raise
 
1264
                        else:
 
1265
                            self.rename_count += 1
 
1266
                    if trans_id in self._new_contents:
 
1267
                        modified_paths.append(full_path)
 
1268
                        completed_new.append(trans_id)
 
1269
 
 
1270
                if trans_id in self._new_id:
 
1271
                    if kind is None:
 
1272
                        kind = file_kind(self._tree.abspath(path))
 
1273
                    if trans_id in self._new_reference_revision:
 
1274
                        new_entry = inventory.TreeReference(
 
1275
                            self._new_id[trans_id],
 
1276
                            self._new_name[trans_id],
 
1277
                            self.final_file_id(self._new_parent[trans_id]),
 
1278
                            None, self._new_reference_revision[trans_id])
 
1279
                    else:
 
1280
                        new_entry = inventory.make_entry(kind,
 
1281
                            self.final_name(trans_id),
 
1282
                            self.final_file_id(self.final_parent(trans_id)),
 
1283
                            self._new_id[trans_id])
 
1284
                else:
 
1285
                    if trans_id in self._new_name or trans_id in\
 
1286
                        self._new_parent or\
 
1287
                        trans_id in self._new_executability:
 
1288
                        file_id = self.final_file_id(trans_id)
 
1289
                        if file_id is not None:
 
1290
                            entry = inv[file_id]
 
1291
                            new_entry = entry.copy()
 
1292
 
 
1293
                    if trans_id in self._new_name or trans_id in\
 
1294
                        self._new_parent:
 
1295
                            if new_entry is not None:
 
1296
                                new_entry.name = self.final_name(trans_id)
 
1297
                                parent = self.final_parent(trans_id)
 
1298
                                parent_id = self.final_file_id(parent)
 
1299
                                new_entry.parent_id = parent_id
 
1300
 
 
1301
                if trans_id in self._new_executability:
 
1302
                    self._set_executability(path, new_entry, trans_id)
 
1303
                if new_entry is not None:
 
1304
                    if new_entry.file_id in inv:
 
1305
                        old_path = inv.id2path(new_entry.file_id)
 
1306
                    else:
 
1307
                        old_path = None
 
1308
                    inventory_delta.append((old_path, path,
 
1309
                                            new_entry.file_id,
 
1310
                                            new_entry))
 
1311
        finally:
 
1312
            child_pb.finished()
 
1313
        for trans_id in completed_new:
 
1314
            del self._new_contents[trans_id]
 
1315
        return modified_paths
 
1316
 
 
1317
 
 
1318
class TransformPreview(TreeTransformBase):
 
1319
    """A TreeTransform for generating preview trees.
 
1320
 
 
1321
    Unlike TreeTransform, this version works when the input tree is a
 
1322
    RevisionTree, rather than a WorkingTree.  As a result, it tends to ignore
 
1323
    unversioned files in the input tree.
 
1324
    """
 
1325
 
 
1326
    def __init__(self, tree, pb=DummyProgress(), case_sensitive=True):
 
1327
        tree.lock_read()
 
1328
        limbodir = tempfile.mkdtemp(prefix='bzr-limbo-')
 
1329
        TreeTransformBase.__init__(self, tree, limbodir, pb, case_sensitive)
 
1330
 
 
1331
    def canonical_path(self, path):
 
1332
        return path
 
1333
 
 
1334
    def tree_kind(self, trans_id):
 
1335
        path = self._tree_id_paths.get(trans_id)
 
1336
        if path is None:
 
1337
            raise NoSuchFile(None)
 
1338
        file_id = self._tree.path2id(path)
 
1339
        return self._tree.kind(file_id)
 
1340
 
 
1341
    def _set_mode(self, trans_id, mode_id, typefunc):
 
1342
        """Set the mode of new file contents.
 
1343
        The mode_id is the existing file to get the mode from (often the same
 
1344
        as trans_id).  The operation is only performed if there's a mode match
 
1345
        according to typefunc.
 
1346
        """
 
1347
        # is it ok to ignore this?  probably
 
1348
        pass
 
1349
 
 
1350
    def iter_tree_children(self, parent_id):
 
1351
        """Iterate through the entry's tree children, if any"""
 
1352
        try:
 
1353
            path = self._tree_id_paths[parent_id]
 
1354
        except KeyError:
 
1355
            return
 
1356
        file_id = self.tree_file_id(parent_id)
 
1357
        for child in self._tree.inventory[file_id].children.iterkeys():
 
1358
            childpath = joinpath(path, child)
 
1359
            yield self.trans_id_tree_path(childpath)
 
1360
 
 
1361
 
 
1362
class _PreviewTree(object):
 
1363
    """Partial implementation of Tree to support show_diff_trees"""
 
1364
 
 
1365
    def __init__(self, transform):
 
1366
        self._transform = transform
 
1367
 
 
1368
    def lock_read(self):
 
1369
        # Perhaps in theory, this should lock the TreeTransform?
 
1370
        pass
 
1371
 
 
1372
    def unlock(self):
 
1373
        pass
 
1374
 
 
1375
    def _iter_changes(self, from_tree, include_unchanged=False,
 
1376
                      specific_files=None, pb=None, extra_trees=None,
 
1377
                      require_versioned=True, want_unversioned=False):
 
1378
        """See InterTree._iter_changes.
 
1379
 
 
1380
        This implementation does not support include_unchanged, specific_files,
 
1381
        or want_unversioned.  extra_trees, require_versioned, and pb are
 
1382
        ignored.
 
1383
        """
 
1384
        if from_tree is not self._transform._tree:
 
1385
            raise ValueError('from_tree must be transform source tree.')
 
1386
        if include_unchanged:
 
1387
            raise ValueError('include_unchanged is not supported')
 
1388
        if specific_files is not None:
 
1389
            raise ValueError('specific_files is not supported')
 
1390
        if want_unversioned:
 
1391
            raise ValueError('want_unversioned is not supported')
 
1392
        return self._transform._iter_changes()
 
1393
 
 
1394
    def kind(self, file_id):
 
1395
        trans_id = self._transform.trans_id_file_id(file_id)
 
1396
        return self._transform.final_kind(trans_id)
 
1397
 
 
1398
    def get_file_mtime(self, file_id, path=None):
 
1399
        """See Tree.get_file_mtime"""
 
1400
        trans_id = self._transform.trans_id_file_id(file_id)
 
1401
        name = self._transform._limbo_name(trans_id)
 
1402
        return os.stat(name).st_mtime
 
1403
 
 
1404
    def get_file(self, file_id):
 
1405
        """See Tree.get_file"""
 
1406
        trans_id = self._transform.trans_id_file_id(file_id)
 
1407
        name = self._transform._limbo_name(trans_id)
 
1408
        return open(name, 'rb')
 
1409
 
 
1410
    def paths2ids(self, specific_files, trees=None, require_versioned=False):
 
1411
        """See Tree.paths2ids"""
 
1412
        return 'not_empty'
 
1413
 
1204
1414
 
1205
1415
def joinpath(parent, child):
1206
1416
    """Join tree-relative paths, handling the tree root specially"""
1237
1447
            self._known_paths[trans_id] = self._determine_path(trans_id)
1238
1448
        return self._known_paths[trans_id]
1239
1449
 
 
1450
 
1240
1451
def topology_sorted_ids(tree):
1241
1452
    """Determine the topological order of the ids in a tree"""
1242
1453
    file_ids = list(tree)
1244
1455
    return file_ids
1245
1456
 
1246
1457
 
1247
 
def build_tree(tree, wt):
 
1458
def build_tree(tree, wt, accelerator_tree=None):
1248
1459
    """Create working tree for a branch, using a TreeTransform.
1249
1460
    
1250
1461
    This function should be used on empty trees, having a tree root at most.
1257
1468
    - Otherwise, if the content on disk matches the content we are building,
1258
1469
      it is silently replaced.
1259
1470
    - Otherwise, conflict resolution will move the old file to 'oldname.moved'.
 
1471
 
 
1472
    :param tree: The tree to convert wt into a copy of
 
1473
    :param wt: The working tree that files will be placed into
 
1474
    :param accelerator_tree: A tree which can be used for retrieving file
 
1475
        contents more quickly than tree itself, i.e. a workingtree.  tree
 
1476
        will be used for cases where accelerator_tree's content is different.
1260
1477
    """
1261
1478
    wt.lock_tree_write()
1262
1479
    try:
1263
1480
        tree.lock_read()
1264
1481
        try:
1265
 
            return _build_tree(tree, wt)
 
1482
            if accelerator_tree is not None:
 
1483
                accelerator_tree.lock_read()
 
1484
            try:
 
1485
                return _build_tree(tree, wt, accelerator_tree)
 
1486
            finally:
 
1487
                if accelerator_tree is not None:
 
1488
                    accelerator_tree.unlock()
1266
1489
        finally:
1267
1490
            tree.unlock()
1268
1491
    finally:
1269
1492
        wt.unlock()
1270
1493
 
1271
 
def _build_tree(tree, wt):
 
1494
 
 
1495
def _build_tree(tree, wt, accelerator_tree):
1272
1496
    """See build_tree."""
1273
1497
    if len(wt.inventory) > 1:  # more than just a root
1274
1498
        raise errors.WorkingTreeAlreadyPopulated(base=wt.basedir)
1345
1569
                    old_parent = tt.trans_id_tree_path(tree_path)
1346
1570
                    _reparent_children(tt, old_parent, new_trans_id)
1347
1571
            for num, (trans_id, bytes) in enumerate(
1348
 
                tree.iter_files_bytes(deferred_contents)):
 
1572
                _iter_files_bytes_accelerated(tree, accelerator_tree,
 
1573
                                              deferred_contents)):
1349
1574
                tt.create_file(bytes, trans_id)
1350
1575
                pb.update('Adding file contents',
1351
1576
                          (num + len(tree.inventory) - len(deferred_contents)),
1363
1588
            wt.add_conflicts(conflicts)
1364
1589
        except errors.UnsupportedOperation:
1365
1590
            pass
1366
 
        result = tt.apply()
 
1591
        result = tt.apply(no_conflicts=True)
1367
1592
    finally:
1368
1593
        tt.finalize()
1369
1594
        top_pb.finished()
1370
1595
    return result
1371
1596
 
1372
1597
 
 
1598
def _iter_files_bytes_accelerated(tree, accelerator_tree, desired_files):
 
1599
    if accelerator_tree is None:
 
1600
        new_desired_files = desired_files
 
1601
    else:
 
1602
        iter = accelerator_tree._iter_changes(tree, include_unchanged=True)
 
1603
        unchanged = dict((f, p[1]) for (f, p, c, v, d, n, k, e)
 
1604
                         in iter if not c)
 
1605
        new_desired_files = []
 
1606
        for file_id, identifier in desired_files:
 
1607
            accelerator_path = unchanged.get(file_id)
 
1608
            if accelerator_path is None:
 
1609
                new_desired_files.append((file_id, identifier))
 
1610
                continue
 
1611
            contents = accelerator_tree.get_file(file_id, accelerator_path)
 
1612
            try:
 
1613
                want_new = False
 
1614
                contents_bytes = (contents.read(),)
 
1615
            finally:
 
1616
                contents.close()
 
1617
            yield identifier, contents_bytes
 
1618
    for result in tree.iter_files_bytes(new_desired_files):
 
1619
        yield result
 
1620
 
 
1621
 
1373
1622
def _reparent_children(tt, old_parent, new_parent):
1374
1623
    for child in tt.iter_tree_children(old_parent):
1375
1624
        tt.adjust_path(tt.final_name(child), new_parent, child)
1376
1625
 
 
1626
def _reparent_transform_children(tt, old_parent, new_parent):
 
1627
    by_parent = tt.by_parent()
 
1628
    for child in by_parent[old_parent]:
 
1629
        tt.adjust_path(tt.final_name(child), new_parent, child)
1377
1630
 
1378
1631
def _content_match(tree, entry, file_id, kind, target_path):
1379
1632
    if entry.kind != kind:
1438
1691
    else:
1439
1692
        raise errors.BadFileKindError(name, kind)
1440
1693
 
 
1694
 
1441
1695
def create_by_entry(tt, entry, tree, trans_id, lines=None, mode_id=None):
1442
1696
    """Create new file contents according to an inventory entry."""
1443
1697
    if entry.kind == "file":
1449
1703
    elif entry.kind == "directory":
1450
1704
        tt.create_directory(trans_id)
1451
1705
 
 
1706
 
1452
1707
def create_entry_executability(tt, entry, trans_id):
1453
1708
    """Set the executability of a trans_id according to an inventory entry"""
1454
1709
    if entry.kind == "file":
1576
1831
        pp.next_phase()
1577
1832
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1578
1833
        try:
1579
 
            raw_conflicts = resolve_conflicts(tt, child_pb)
 
1834
            raw_conflicts = resolve_conflicts(tt, child_pb,
 
1835
                lambda t, c: conflict_pass(t, c, target_tree))
1580
1836
        finally:
1581
1837
            child_pb.finished()
1582
1838
        conflicts = cook_conflicts(raw_conflicts, tt)
1721
1977
                               conflict[1], conflict[2], ))
1722
1978
        elif c_type == 'duplicate':
1723
1979
            # files that were renamed take precedence
1724
 
            new_name = tt.final_name(conflict[1])+'.moved'
1725
1980
            final_parent = tt.final_parent(conflict[1])
1726
1981
            if tt.path_changed(conflict[1]):
1727
 
                tt.adjust_path(new_name, final_parent, conflict[2])
1728
 
                new_conflicts.add((c_type, 'Moved existing file to', 
1729
 
                                   conflict[2], conflict[1]))
 
1982
                existing_file, new_file = conflict[2], conflict[1]
1730
1983
            else:
1731
 
                tt.adjust_path(new_name, final_parent, conflict[1])
1732
 
                new_conflicts.add((c_type, 'Moved existing file to', 
1733
 
                                  conflict[1], conflict[2]))
 
1984
                existing_file, new_file = conflict[1], conflict[2]
 
1985
            new_name = tt.final_name(existing_file)+'.moved'
 
1986
            tt.adjust_path(new_name, final_parent, existing_file)
 
1987
            new_conflicts.add((c_type, 'Moved existing file to', 
 
1988
                               existing_file, new_file))
1734
1989
        elif c_type == 'parent loop':
1735
1990
            # break the loop by undoing one of the ops that caused the loop
1736
1991
            cur = conflict[1]
1752
2007
                try:
1753
2008
                    tt.final_name(trans_id)
1754
2009
                except NoFinalPath:
1755
 
                    file_id = tt.final_file_id(trans_id)
1756
 
                    entry = path_tree.inventory[file_id]
1757
 
                    parent_trans_id = tt.trans_id_file_id(entry.parent_id)
1758
 
                    tt.adjust_path(entry.name, parent_trans_id, trans_id)
 
2010
                    if path_tree is not None:
 
2011
                        file_id = tt.final_file_id(trans_id)
 
2012
                        entry = path_tree.inventory[file_id]
 
2013
                        parent_trans_id = tt.trans_id_file_id(entry.parent_id)
 
2014
                        tt.adjust_path(entry.name, parent_trans_id, trans_id)
1759
2015
        elif c_type == 'unversioned parent':
1760
2016
            tt.version_file(tt.inactive_file_id(conflict[1]), conflict[1])
1761
2017
            new_conflicts.add((c_type, 'Versioned directory', conflict[1]))
 
2018
        elif c_type == 'non-directory parent':
 
2019
            parent_id = conflict[1]
 
2020
            parent_parent = tt.final_parent(parent_id)
 
2021
            parent_name = tt.final_name(parent_id)
 
2022
            parent_file_id = tt.final_file_id(parent_id)
 
2023
            new_parent_id = tt.new_directory(parent_name + '.new',
 
2024
                parent_parent, parent_file_id)
 
2025
            _reparent_transform_children(tt, parent_id, new_parent_id)
 
2026
            tt.unversion_file(parent_id)
 
2027
            new_conflicts.add((c_type, 'Created directory', new_parent_id))
1762
2028
    return new_conflicts
1763
2029
 
1764
2030
 
1799
2065
 
1800
2066
    def rename(self, from_, to):
1801
2067
        """Rename a file from one path to another.  Functions like os.rename"""
1802
 
        os.rename(from_, to)
 
2068
        try:
 
2069
            os.rename(from_, to)
 
2070
        except OSError, e:
 
2071
            if e.errno in (errno.EEXIST, errno.ENOTEMPTY):
 
2072
                raise errors.FileExists(to, str(e))
 
2073
            raise
1803
2074
        self.past_renames.append((from_, to))
1804
2075
 
1805
2076
    def pre_delete(self, from_, to):