/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: 2008-02-24 16:42:13 UTC
  • mfrom: (3234 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3235.
  • Revision ID: aaron@aaronbentley.com-20080224164213-eza1lzru5bwuwmmj
Merge with bzr.dev

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(), """
62
63
        self.rename_count = rename_count
63
64
 
64
65
 
65
 
class TreeTransform(object):
66
 
    """Represent a tree transformation.
67
 
    
68
 
    This object is designed to support incremental generation of the transform,
69
 
    in any order.
70
 
 
71
 
    However, it gives optimum performance when parent directories are created
72
 
    before their contents.  The transform is then able to put child files
73
 
    directly in their parent directory, avoiding later renames.
74
 
    
75
 
    It is easy to produce malformed transforms, but they are generally
76
 
    harmless.  Attempting to apply a malformed transform will cause an
77
 
    exception to be raised before any modifications are made to the tree.  
78
 
 
79
 
    Many kinds of malformed transforms can be corrected with the 
80
 
    resolve_conflicts function.  The remaining ones indicate programming error,
81
 
    such as trying to create a file with no path.
82
 
 
83
 
    Two sets of file creation methods are supplied.  Convenience methods are:
84
 
     * new_file
85
 
     * new_directory
86
 
     * new_symlink
87
 
 
88
 
    These are composed of the low-level methods:
89
 
     * create_path
90
 
     * create_file or create_directory or create_symlink
91
 
     * version_file
92
 
     * set_executability
93
 
 
94
 
    Transform/Transaction ids
95
 
    -------------------------
96
 
    trans_ids are temporary ids assigned to all files involved in a transform.
97
 
    It's possible, even common, that not all files in the Tree have trans_ids.
98
 
 
99
 
    trans_ids are used because filenames and file_ids are not good enough
100
 
    identifiers; filenames change, and not all files have file_ids.  File-ids
101
 
    are also associated with trans-ids, so that moving a file moves its
102
 
    file-id.
103
 
 
104
 
    trans_ids are only valid for the TreeTransform that generated them.
105
 
 
106
 
    Limbo
107
 
    -----
108
 
    Limbo is a temporary directory use to hold new versions of files.
109
 
    Files are added to limbo by create_file, create_directory, create_symlink,
110
 
    and their convenience variants (new_*).  Files may be removed from limbo
111
 
    using cancel_creation.  Files are renamed from limbo into their final
112
 
    location as part of TreeTransform.apply
113
 
 
114
 
    Limbo must be cleaned up, by either calling TreeTransform.apply or
115
 
    calling TreeTransform.finalize.
116
 
 
117
 
    Files are placed into limbo inside their parent directories, where
118
 
    possible.  This reduces subsequent renames, and makes operations involving
119
 
    lots of files faster.  This optimization is only possible if the parent
120
 
    directory is created *before* creating any of its children, so avoid
121
 
    creating children before parents, where possible.
122
 
 
123
 
    Pending-deletion
124
 
    ----------------
125
 
    This temporary directory is used by _FileMover for storing files that are
126
 
    about to be deleted.  In case of rollback, the files will be restored.
127
 
    FileMover does not delete files until it is sure that a rollback will not
128
 
    happen.  
129
 
    """
130
 
    def __init__(self, tree, pb=DummyProgress()):
131
 
        """Note: a tree_write lock is taken on the tree.
132
 
        
133
 
        Use TreeTransform.finalize() to release the lock (can be omitted if
134
 
        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.
135
80
        """
136
81
        object.__init__(self)
137
82
        self._tree = tree
138
 
        self._tree.lock_tree_write()
139
 
        try:
140
 
            control_files = self._tree._control_files
141
 
            self._limbodir = urlutils.local_path_from_url(
142
 
                control_files.controlfilename('limbo'))
143
 
            try:
144
 
                os.mkdir(self._limbodir)
145
 
            except OSError, e:
146
 
                if e.errno == errno.EEXIST:
147
 
                    raise ExistingLimbo(self._limbodir)
148
 
            self._deletiondir = urlutils.local_path_from_url(
149
 
                control_files.controlfilename('pending-deletion'))
150
 
            try:
151
 
                os.mkdir(self._deletiondir)
152
 
            except OSError, e:
153
 
                if e.errno == errno.EEXIST:
154
 
                    raise errors.ExistingPendingDeletion(self._deletiondir)
155
 
 
156
 
        except: 
157
 
            self._tree.unlock()
158
 
            raise
159
 
 
160
 
        # counter used to generate trans-ids (which are locally unique)
 
83
        self._limbodir = limbodir
 
84
        self._deletiondir = None
161
85
        self._id_number = 0
162
86
        # mapping of trans_id -> new basename
163
87
        self._new_name = {}
199
123
        # The trans_id that will be used as the tree root
200
124
        self._new_root = self.trans_id_tree_file_id(tree.get_root_id())
201
125
        # Indictor of whether the transform has been applied
202
 
        self.__done = False
 
126
        self._done = False
203
127
        # A progress bar
204
128
        self._pb = pb
 
129
        # Whether the target is case sensitive
 
130
        self._case_sensitive_target = case_sensitive
205
131
        # A counter of how many files have been renamed
206
132
        self.rename_count = 0
207
133
 
233
159
                # We don't especially care *why* the dir is immortal.
234
160
                raise ImmortalLimbo(self._limbodir)
235
161
            try:
236
 
                os.rmdir(self._deletiondir)
 
162
                if self._deletiondir is not None:
 
163
                    os.rmdir(self._deletiondir)
237
164
            except OSError:
238
165
                raise errors.ImmortalPendingDeletion(self._deletiondir)
239
166
        finally:
642
569
 
643
570
    def find_conflicts(self):
644
571
        """Find any violations of inventory or filesystem invariants"""
645
 
        if self.__done is True:
 
572
        if self._done is True:
646
573
            raise ReusingTransform()
647
574
        conflicts = []
648
575
        # ensure all children of all existent parents are known
670
597
                        self.tree_kind(t) == 'directory'])
671
598
        for trans_id in self._removed_id:
672
599
            file_id = self.tree_file_id(trans_id)
673
 
            if self._tree.inventory[file_id].kind == 'directory':
 
600
            if file_id is not None:
 
601
                if self._tree.inventory[file_id].kind == 'directory':
 
602
                    parents.append(trans_id)
 
603
            elif self.tree_kind(trans_id) == 'directory':
674
604
                parents.append(trans_id)
675
605
 
676
606
        for parent_id in parents:
686
616
        try:
687
617
            children = os.listdir(self._tree.abspath(path))
688
618
        except OSError, e:
689
 
            if e.errno != errno.ENOENT and e.errno != errno.ESRCH:
 
619
            if e.errno not in (errno.ENOENT, errno.ESRCH, errno.ENOTDIR):
690
620
                raise
691
621
            return
692
622
            
810
740
            return conflicts
811
741
        for children in by_parent.itervalues():
812
742
            name_ids = [(self.final_name(t), t) for t in children]
813
 
            if not self._tree.case_sensitive:
 
743
            if not self._case_sensitive_target:
814
744
                name_ids = [(n.lower(), t) for n, t in name_ids]
815
745
            name_ids.sort()
816
746
            last_name = None
875
805
                continue
876
806
            return True
877
807
        return False
878
 
            
879
 
    def apply(self, no_conflicts=False, _mover=None):
880
 
        """Apply all changes to the inventory and filesystem.
881
 
        
882
 
        If filesystem or inventory conflicts are present, MalformedTransform
883
 
        will be thrown.
884
 
 
885
 
        If apply succeeds, finalize is not necessary.
886
 
 
887
 
        :param no_conflicts: if True, the caller guarantees there are no
888
 
            conflicts, so no check is made.
889
 
        :param _mover: Supply an alternate FileMover, for testing
890
 
        """
891
 
        if not no_conflicts:
892
 
            conflicts = self.find_conflicts()
893
 
            if len(conflicts) != 0:
894
 
                raise MalformedTransform(conflicts=conflicts)
895
 
        inv = self._tree.inventory
896
 
        inventory_delta = []
897
 
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
898
 
        try:
899
 
            if _mover is None:
900
 
                mover = _FileMover()
901
 
            else:
902
 
                mover = _mover
903
 
            try:
904
 
                child_pb.update('Apply phase', 0, 2)
905
 
                self._apply_removals(inv, inventory_delta, mover)
906
 
                child_pb.update('Apply phase', 1, 2)
907
 
                modified_paths = self._apply_insertions(inv, inventory_delta,
908
 
                                                        mover)
909
 
            except:
910
 
                mover.rollback()
911
 
                raise
912
 
            else:
913
 
                mover.apply_deletions()
914
 
        finally:
915
 
            child_pb.finished()
916
 
        self._tree.apply_inventory_delta(inventory_delta)
917
 
        self.__done = True
918
 
        self.finalize()
919
 
        return _TransformResults(modified_paths, self.rename_count)
920
808
 
921
809
    def _limbo_name(self, trans_id):
922
810
        """Generate the limbo name of a file"""
938
826
                # the direct path can only be used if no other file has
939
827
                # already taken this pathname, i.e. if the name is unused, or
940
828
                # if it is already associated with this trans_id.
941
 
                elif self._tree.case_sensitive:
 
829
                elif self._case_sensitive_target:
942
830
                    if (self._limbo_children_names[parent].get(filename)
943
831
                        in (trans_id, None)):
944
832
                        use_direct_path = True
962
850
        self._limbo_files[trans_id] = limbo_name
963
851
        return limbo_name
964
852
 
965
 
    def _apply_removals(self, inv, inventory_delta, mover):
966
 
        """Perform tree operations that remove directory/inventory names.
967
 
        
968
 
        That is, delete files that are to be deleted, and put any files that
969
 
        need renaming into limbo.  This must be done in strict child-to-parent
970
 
        order.
971
 
        """
972
 
        tree_paths = list(self._tree_path_ids.iteritems())
973
 
        tree_paths.sort(reverse=True)
974
 
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
975
 
        try:
976
 
            for num, data in enumerate(tree_paths):
977
 
                path, trans_id = data
978
 
                child_pb.update('removing file', num, len(tree_paths))
979
 
                full_path = self._tree.abspath(path)
980
 
                if trans_id in self._removed_contents:
981
 
                    mover.pre_delete(full_path, os.path.join(self._deletiondir,
982
 
                                     trans_id))
983
 
                elif trans_id in self._new_name or trans_id in \
984
 
                    self._new_parent:
985
 
                    try:
986
 
                        mover.rename(full_path, self._limbo_name(trans_id))
987
 
                    except OSError, e:
988
 
                        if e.errno != errno.ENOENT:
989
 
                            raise
990
 
                    else:
991
 
                        self.rename_count += 1
992
 
                if trans_id in self._removed_id:
993
 
                    if trans_id == self._new_root:
994
 
                        file_id = self._tree.get_root_id()
995
 
                    else:
996
 
                        file_id = self.tree_file_id(trans_id)
997
 
                    assert file_id is not None
998
 
                    inventory_delta.append((path, None, file_id, None))
999
 
        finally:
1000
 
            child_pb.finished()
1001
 
 
1002
 
    def _apply_insertions(self, inv, inventory_delta, mover):
1003
 
        """Perform tree operations that insert directory/inventory names.
1004
 
        
1005
 
        That is, create any files that need to be created, and restore from
1006
 
        limbo any files that needed renaming.  This must be done in strict
1007
 
        parent-to-child order.
1008
 
        """
1009
 
        new_paths = self.new_paths()
1010
 
        modified_paths = []
1011
 
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1012
 
        completed_new = []
1013
 
        try:
1014
 
            for num, (path, trans_id) in enumerate(new_paths):
1015
 
                new_entry = None
1016
 
                child_pb.update('adding file', num, len(new_paths))
1017
 
                try:
1018
 
                    kind = self._new_contents[trans_id]
1019
 
                except KeyError:
1020
 
                    kind = contents = None
1021
 
                if trans_id in self._new_contents or \
1022
 
                    self.path_changed(trans_id):
1023
 
                    full_path = self._tree.abspath(path)
1024
 
                    if trans_id in self._needs_rename:
1025
 
                        try:
1026
 
                            mover.rename(self._limbo_name(trans_id), full_path)
1027
 
                        except OSError, e:
1028
 
                            # We may be renaming a dangling inventory id
1029
 
                            if e.errno != errno.ENOENT:
1030
 
                                raise
1031
 
                        else:
1032
 
                            self.rename_count += 1
1033
 
                    if trans_id in self._new_contents:
1034
 
                        modified_paths.append(full_path)
1035
 
                        completed_new.append(trans_id)
1036
 
 
1037
 
                if trans_id in self._new_id:
1038
 
                    if kind is None:
1039
 
                        kind = file_kind(self._tree.abspath(path))
1040
 
                    if trans_id in self._new_reference_revision:
1041
 
                        new_entry = inventory.TreeReference(
1042
 
                            self._new_id[trans_id],
1043
 
                            self._new_name[trans_id], 
1044
 
                            self.final_file_id(self._new_parent[trans_id]),
1045
 
                            None, self._new_reference_revision[trans_id])
1046
 
                    else:
1047
 
                        new_entry = inventory.make_entry(kind,
1048
 
                            self.final_name(trans_id),
1049
 
                            self.final_file_id(self.final_parent(trans_id)),
1050
 
                            self._new_id[trans_id])
1051
 
                else:
1052
 
                    if trans_id in self._new_name or trans_id in\
1053
 
                        self._new_parent or\
1054
 
                        trans_id in self._new_executability:
1055
 
                        file_id = self.final_file_id(trans_id)
1056
 
                        if file_id is not None:
1057
 
                            entry = inv[file_id]
1058
 
                            new_entry = entry.copy()
1059
 
 
1060
 
                    if trans_id in self._new_name or trans_id in\
1061
 
                        self._new_parent:
1062
 
                            if new_entry is not None:
1063
 
                                new_entry.name = self.final_name(trans_id)
1064
 
                                parent = self.final_parent(trans_id)
1065
 
                                parent_id = self.final_file_id(parent)
1066
 
                                new_entry.parent_id = parent_id
1067
 
 
1068
 
                if trans_id in self._new_executability:
1069
 
                    self._set_executability(path, new_entry, trans_id)
1070
 
                if new_entry is not None:
1071
 
                    if new_entry.file_id in inv:
1072
 
                        old_path = inv.id2path(new_entry.file_id)
1073
 
                    else:
1074
 
                        old_path = None
1075
 
                    inventory_delta.append((old_path, path,
1076
 
                                            new_entry.file_id,
1077
 
                                            new_entry))
1078
 
        finally:
1079
 
            child_pb.finished()
1080
 
        for trans_id in completed_new:
1081
 
            del self._new_contents[trans_id]
1082
 
        return modified_paths
1083
 
 
1084
853
    def _set_executability(self, path, entry, trans_id):
1085
854
        """Set the executability of versioned files """
1086
855
        new_executability = self._new_executability[trans_id]
1290
1059
                   (from_executable, to_executable)))
1291
1060
        return iter(sorted(results, key=lambda x:x[1]))
1292
1061
 
 
1062
    def get_preview_tree(self):
 
1063
        """Return a tree representing the result of the transform.
 
1064
 
 
1065
        This tree only supports the subset of Tree functionality required
 
1066
        by show_diff_trees.  It must only be compared to tt._tree.
 
1067
        """
 
1068
        return _PreviewTree(self)
 
1069
 
 
1070
 
 
1071
class TreeTransform(TreeTransformBase):
 
1072
    """Represent a tree transformation.
 
1073
 
 
1074
    This object is designed to support incremental generation of the transform,
 
1075
    in any order.
 
1076
 
 
1077
    However, it gives optimum performance when parent directories are created
 
1078
    before their contents.  The transform is then able to put child files
 
1079
    directly in their parent directory, avoiding later renames.
 
1080
 
 
1081
    It is easy to produce malformed transforms, but they are generally
 
1082
    harmless.  Attempting to apply a malformed transform will cause an
 
1083
    exception to be raised before any modifications are made to the tree.
 
1084
 
 
1085
    Many kinds of malformed transforms can be corrected with the
 
1086
    resolve_conflicts function.  The remaining ones indicate programming error,
 
1087
    such as trying to create a file with no path.
 
1088
 
 
1089
    Two sets of file creation methods are supplied.  Convenience methods are:
 
1090
     * new_file
 
1091
     * new_directory
 
1092
     * new_symlink
 
1093
 
 
1094
    These are composed of the low-level methods:
 
1095
     * create_path
 
1096
     * create_file or create_directory or create_symlink
 
1097
     * version_file
 
1098
     * set_executability
 
1099
 
 
1100
    Transform/Transaction ids
 
1101
    -------------------------
 
1102
    trans_ids are temporary ids assigned to all files involved in a transform.
 
1103
    It's possible, even common, that not all files in the Tree have trans_ids.
 
1104
 
 
1105
    trans_ids are used because filenames and file_ids are not good enough
 
1106
    identifiers; filenames change, and not all files have file_ids.  File-ids
 
1107
    are also associated with trans-ids, so that moving a file moves its
 
1108
    file-id.
 
1109
 
 
1110
    trans_ids are only valid for the TreeTransform that generated them.
 
1111
 
 
1112
    Limbo
 
1113
    -----
 
1114
    Limbo is a temporary directory use to hold new versions of files.
 
1115
    Files are added to limbo by create_file, create_directory, create_symlink,
 
1116
    and their convenience variants (new_*).  Files may be removed from limbo
 
1117
    using cancel_creation.  Files are renamed from limbo into their final
 
1118
    location as part of TreeTransform.apply
 
1119
 
 
1120
    Limbo must be cleaned up, by either calling TreeTransform.apply or
 
1121
    calling TreeTransform.finalize.
 
1122
 
 
1123
    Files are placed into limbo inside their parent directories, where
 
1124
    possible.  This reduces subsequent renames, and makes operations involving
 
1125
    lots of files faster.  This optimization is only possible if the parent
 
1126
    directory is created *before* creating any of its children, so avoid
 
1127
    creating children before parents, where possible.
 
1128
 
 
1129
    Pending-deletion
 
1130
    ----------------
 
1131
    This temporary directory is used by _FileMover for storing files that are
 
1132
    about to be deleted.  In case of rollback, the files will be restored.
 
1133
    FileMover does not delete files until it is sure that a rollback will not
 
1134
    happen.
 
1135
    """
 
1136
    def __init__(self, tree, pb=DummyProgress()):
 
1137
        """Note: a tree_write lock is taken on the tree.
 
1138
 
 
1139
        Use TreeTransform.finalize() to release the lock (can be omitted if
 
1140
        TreeTransform.apply() called).
 
1141
        """
 
1142
        tree.lock_tree_write()
 
1143
 
 
1144
        try:
 
1145
            control_files = tree._control_files
 
1146
            limbodir = urlutils.local_path_from_url(
 
1147
                control_files.controlfilename('limbo'))
 
1148
            try:
 
1149
                os.mkdir(limbodir)
 
1150
            except OSError, e:
 
1151
                if e.errno == errno.EEXIST:
 
1152
                    raise ExistingLimbo(limbodir)
 
1153
            deletiondir = urlutils.local_path_from_url(
 
1154
                control_files.controlfilename('pending-deletion'))
 
1155
            try:
 
1156
                os.mkdir(deletiondir)
 
1157
            except OSError, e:
 
1158
                if e.errno == errno.EEXIST:
 
1159
                    raise errors.ExistingPendingDeletion(deletiondir)
 
1160
        except:
 
1161
            tree.unlock()
 
1162
            raise
 
1163
 
 
1164
        TreeTransformBase.__init__(self, tree, limbodir, pb,
 
1165
                                   tree.case_sensitive)
 
1166
        self._deletiondir = deletiondir
 
1167
 
 
1168
    def apply(self, no_conflicts=False, _mover=None):
 
1169
        """Apply all changes to the inventory and filesystem.
 
1170
 
 
1171
        If filesystem or inventory conflicts are present, MalformedTransform
 
1172
        will be thrown.
 
1173
 
 
1174
        If apply succeeds, finalize is not necessary.
 
1175
 
 
1176
        :param no_conflicts: if True, the caller guarantees there are no
 
1177
            conflicts, so no check is made.
 
1178
        :param _mover: Supply an alternate FileMover, for testing
 
1179
        """
 
1180
        if not no_conflicts:
 
1181
            conflicts = self.find_conflicts()
 
1182
            if len(conflicts) != 0:
 
1183
                raise MalformedTransform(conflicts=conflicts)
 
1184
        inv = self._tree.inventory
 
1185
        inventory_delta = []
 
1186
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1187
        try:
 
1188
            if _mover is None:
 
1189
                mover = _FileMover()
 
1190
            else:
 
1191
                mover = _mover
 
1192
            try:
 
1193
                child_pb.update('Apply phase', 0, 2)
 
1194
                self._apply_removals(inv, inventory_delta, mover)
 
1195
                child_pb.update('Apply phase', 1, 2)
 
1196
                modified_paths = self._apply_insertions(inv, inventory_delta,
 
1197
                                                        mover)
 
1198
            except:
 
1199
                mover.rollback()
 
1200
                raise
 
1201
            else:
 
1202
                mover.apply_deletions()
 
1203
        finally:
 
1204
            child_pb.finished()
 
1205
        self._tree.apply_inventory_delta(inventory_delta)
 
1206
        self._done = True
 
1207
        self.finalize()
 
1208
        return _TransformResults(modified_paths, self.rename_count)
 
1209
 
 
1210
    def _apply_removals(self, inv, inventory_delta, mover):
 
1211
        """Perform tree operations that remove directory/inventory names.
 
1212
 
 
1213
        That is, delete files that are to be deleted, and put any files that
 
1214
        need renaming into limbo.  This must be done in strict child-to-parent
 
1215
        order.
 
1216
        """
 
1217
        tree_paths = list(self._tree_path_ids.iteritems())
 
1218
        tree_paths.sort(reverse=True)
 
1219
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1220
        try:
 
1221
            for num, data in enumerate(tree_paths):
 
1222
                path, trans_id = data
 
1223
                child_pb.update('removing file', num, len(tree_paths))
 
1224
                full_path = self._tree.abspath(path)
 
1225
                if trans_id in self._removed_contents:
 
1226
                    mover.pre_delete(full_path, os.path.join(self._deletiondir,
 
1227
                                     trans_id))
 
1228
                elif trans_id in self._new_name or trans_id in \
 
1229
                    self._new_parent:
 
1230
                    try:
 
1231
                        mover.rename(full_path, self._limbo_name(trans_id))
 
1232
                    except OSError, e:
 
1233
                        if e.errno != errno.ENOENT:
 
1234
                            raise
 
1235
                    else:
 
1236
                        self.rename_count += 1
 
1237
                if trans_id in self._removed_id:
 
1238
                    if trans_id == self._new_root:
 
1239
                        file_id = self._tree.get_root_id()
 
1240
                    else:
 
1241
                        file_id = self.tree_file_id(trans_id)
 
1242
                    if file_id is not None:
 
1243
                        inventory_delta.append((path, None, file_id, None))
 
1244
        finally:
 
1245
            child_pb.finished()
 
1246
 
 
1247
    def _apply_insertions(self, inv, inventory_delta, mover):
 
1248
        """Perform tree operations that insert directory/inventory names.
 
1249
 
 
1250
        That is, create any files that need to be created, and restore from
 
1251
        limbo any files that needed renaming.  This must be done in strict
 
1252
        parent-to-child order.
 
1253
        """
 
1254
        new_paths = self.new_paths()
 
1255
        modified_paths = []
 
1256
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1257
        completed_new = []
 
1258
        try:
 
1259
            for num, (path, trans_id) in enumerate(new_paths):
 
1260
                new_entry = None
 
1261
                child_pb.update('adding file', num, len(new_paths))
 
1262
                try:
 
1263
                    kind = self._new_contents[trans_id]
 
1264
                except KeyError:
 
1265
                    kind = contents = None
 
1266
                if trans_id in self._new_contents or \
 
1267
                    self.path_changed(trans_id):
 
1268
                    full_path = self._tree.abspath(path)
 
1269
                    if trans_id in self._needs_rename:
 
1270
                        try:
 
1271
                            mover.rename(self._limbo_name(trans_id), full_path)
 
1272
                        except OSError, e:
 
1273
                            # We may be renaming a dangling inventory id
 
1274
                            if e.errno != errno.ENOENT:
 
1275
                                raise
 
1276
                        else:
 
1277
                            self.rename_count += 1
 
1278
                    if trans_id in self._new_contents:
 
1279
                        modified_paths.append(full_path)
 
1280
                        completed_new.append(trans_id)
 
1281
 
 
1282
                if trans_id in self._new_id:
 
1283
                    if kind is None:
 
1284
                        kind = file_kind(self._tree.abspath(path))
 
1285
                    if trans_id in self._new_reference_revision:
 
1286
                        new_entry = inventory.TreeReference(
 
1287
                            self._new_id[trans_id],
 
1288
                            self._new_name[trans_id],
 
1289
                            self.final_file_id(self._new_parent[trans_id]),
 
1290
                            None, self._new_reference_revision[trans_id])
 
1291
                    else:
 
1292
                        new_entry = inventory.make_entry(kind,
 
1293
                            self.final_name(trans_id),
 
1294
                            self.final_file_id(self.final_parent(trans_id)),
 
1295
                            self._new_id[trans_id])
 
1296
                else:
 
1297
                    if trans_id in self._new_name or trans_id in\
 
1298
                        self._new_parent or\
 
1299
                        trans_id in self._new_executability:
 
1300
                        file_id = self.final_file_id(trans_id)
 
1301
                        if file_id is not None:
 
1302
                            entry = inv[file_id]
 
1303
                            new_entry = entry.copy()
 
1304
 
 
1305
                    if trans_id in self._new_name or trans_id in\
 
1306
                        self._new_parent:
 
1307
                            if new_entry is not None:
 
1308
                                new_entry.name = self.final_name(trans_id)
 
1309
                                parent = self.final_parent(trans_id)
 
1310
                                parent_id = self.final_file_id(parent)
 
1311
                                new_entry.parent_id = parent_id
 
1312
 
 
1313
                if trans_id in self._new_executability:
 
1314
                    self._set_executability(path, new_entry, trans_id)
 
1315
                if new_entry is not None:
 
1316
                    if new_entry.file_id in inv:
 
1317
                        old_path = inv.id2path(new_entry.file_id)
 
1318
                    else:
 
1319
                        old_path = None
 
1320
                    inventory_delta.append((old_path, path,
 
1321
                                            new_entry.file_id,
 
1322
                                            new_entry))
 
1323
        finally:
 
1324
            child_pb.finished()
 
1325
        for trans_id in completed_new:
 
1326
            del self._new_contents[trans_id]
 
1327
        return modified_paths
 
1328
 
 
1329
 
 
1330
class TransformPreview(TreeTransformBase):
 
1331
    """A TreeTransform for generating preview trees.
 
1332
 
 
1333
    Unlike TreeTransform, this version works when the input tree is a
 
1334
    RevisionTree, rather than a WorkingTree.  As a result, it tends to ignore
 
1335
    unversioned files in the input tree.
 
1336
    """
 
1337
 
 
1338
    def __init__(self, tree, pb=DummyProgress(), case_sensitive=True):
 
1339
        tree.lock_read()
 
1340
        limbodir = tempfile.mkdtemp(prefix='bzr-limbo-')
 
1341
        TreeTransformBase.__init__(self, tree, limbodir, pb, case_sensitive)
 
1342
 
 
1343
    def canonical_path(self, path):
 
1344
        return path
 
1345
 
 
1346
    def tree_kind(self, trans_id):
 
1347
        path = self._tree_id_paths.get(trans_id)
 
1348
        if path is None:
 
1349
            raise NoSuchFile(None)
 
1350
        file_id = self._tree.path2id(path)
 
1351
        return self._tree.kind(file_id)
 
1352
 
 
1353
    def _set_mode(self, trans_id, mode_id, typefunc):
 
1354
        """Set the mode of new file contents.
 
1355
        The mode_id is the existing file to get the mode from (often the same
 
1356
        as trans_id).  The operation is only performed if there's a mode match
 
1357
        according to typefunc.
 
1358
        """
 
1359
        # is it ok to ignore this?  probably
 
1360
        pass
 
1361
 
 
1362
    def iter_tree_children(self, parent_id):
 
1363
        """Iterate through the entry's tree children, if any"""
 
1364
        try:
 
1365
            path = self._tree_id_paths[parent_id]
 
1366
        except KeyError:
 
1367
            return
 
1368
        file_id = self.tree_file_id(parent_id)
 
1369
        for child in self._tree.inventory[file_id].children.iterkeys():
 
1370
            childpath = joinpath(path, child)
 
1371
            yield self.trans_id_tree_path(childpath)
 
1372
 
 
1373
 
 
1374
class _PreviewTree(object):
 
1375
    """Partial implementation of Tree to support show_diff_trees"""
 
1376
 
 
1377
    def __init__(self, transform):
 
1378
        self._transform = transform
 
1379
 
 
1380
    def lock_read(self):
 
1381
        # Perhaps in theory, this should lock the TreeTransform?
 
1382
        pass
 
1383
 
 
1384
    def unlock(self):
 
1385
        pass
 
1386
 
 
1387
    def _iter_changes(self, from_tree, include_unchanged=False,
 
1388
                      specific_files=None, pb=None, extra_trees=None,
 
1389
                      require_versioned=True, want_unversioned=False):
 
1390
        """See InterTree._iter_changes.
 
1391
 
 
1392
        This implementation does not support include_unchanged, specific_files,
 
1393
        or want_unversioned.  extra_trees, require_versioned, and pb are
 
1394
        ignored.
 
1395
        """
 
1396
        if from_tree is not self._transform._tree:
 
1397
            raise ValueError('from_tree must be transform source tree.')
 
1398
        if include_unchanged:
 
1399
            raise ValueError('include_unchanged is not supported')
 
1400
        if specific_files is not None:
 
1401
            raise ValueError('specific_files is not supported')
 
1402
        if want_unversioned:
 
1403
            raise ValueError('want_unversioned is not supported')
 
1404
        return self._transform._iter_changes()
 
1405
 
 
1406
    def kind(self, file_id):
 
1407
        trans_id = self._transform.trans_id_file_id(file_id)
 
1408
        return self._transform.final_kind(trans_id)
 
1409
 
 
1410
    def get_file_mtime(self, file_id, path=None):
 
1411
        """See Tree.get_file_mtime"""
 
1412
        trans_id = self._transform.trans_id_file_id(file_id)
 
1413
        name = self._transform._limbo_name(trans_id)
 
1414
        return os.stat(name).st_mtime
 
1415
 
 
1416
    def get_file(self, file_id):
 
1417
        """See Tree.get_file"""
 
1418
        trans_id = self._transform.trans_id_file_id(file_id)
 
1419
        name = self._transform._limbo_name(trans_id)
 
1420
        return open(name, 'rb')
 
1421
 
 
1422
    def get_symlink_target(self, file_id):
 
1423
        """See Tree.get_symlink_target"""
 
1424
        trans_id = self._transform.trans_id_file_id(file_id)
 
1425
        name = self._transform._limbo_name(trans_id)
 
1426
        return os.readlink(name)
 
1427
 
 
1428
    def paths2ids(self, specific_files, trees=None, require_versioned=False):
 
1429
        """See Tree.paths2ids"""
 
1430
        return 'not_empty'
 
1431
 
1293
1432
 
1294
1433
def joinpath(parent, child):
1295
1434
    """Join tree-relative paths, handling the tree root specially"""
1513
1652
    for child in tt.iter_tree_children(old_parent):
1514
1653
        tt.adjust_path(tt.final_name(child), new_parent, child)
1515
1654
 
 
1655
def _reparent_transform_children(tt, old_parent, new_parent):
 
1656
    by_parent = tt.by_parent()
 
1657
    for child in by_parent[old_parent]:
 
1658
        tt.adjust_path(tt.final_name(child), new_parent, child)
1516
1659
 
1517
1660
def _content_match(tree, entry, file_id, kind, target_path):
1518
1661
    if entry.kind != kind:
1901
2044
        elif c_type == 'unversioned parent':
1902
2045
            tt.version_file(tt.inactive_file_id(conflict[1]), conflict[1])
1903
2046
            new_conflicts.add((c_type, 'Versioned directory', conflict[1]))
 
2047
        elif c_type == 'non-directory parent':
 
2048
            parent_id = conflict[1]
 
2049
            parent_parent = tt.final_parent(parent_id)
 
2050
            parent_name = tt.final_name(parent_id)
 
2051
            parent_file_id = tt.final_file_id(parent_id)
 
2052
            new_parent_id = tt.new_directory(parent_name + '.new',
 
2053
                parent_parent, parent_file_id)
 
2054
            _reparent_transform_children(tt, parent_id, new_parent_id)
 
2055
            tt.unversion_file(parent_id)
 
2056
            new_conflicts.add((c_type, 'Created directory', new_parent_id))
1904
2057
    return new_conflicts
1905
2058
 
1906
2059