59
44
class _TransformResults(object):
60
def __init__(self, modified_paths, rename_count):
45
def __init__(self, modified_paths):
61
46
object.__init__(self)
62
47
self.modified_paths = modified_paths
63
self.rename_count = rename_count
66
class TreeTransformBase(object):
67
"""The base class for TreeTransform and TreeTransformBase"""
69
def __init__(self, tree, limbodir, pb=DummyProgress(),
73
:param tree: The tree that will be transformed, but not necessarily
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.
50
class TreeTransform(object):
51
"""Represent a tree transformation.
53
This object is designed to support incremental generation of the transform,
56
It is easy to produce malformed transforms, but they are generally
57
harmless. Attempting to apply a malformed transform will cause an
58
exception to be raised before any modifications are made to the tree.
60
Many kinds of malformed transforms can be corrected with the
61
resolve_conflicts function. The remaining ones indicate programming error,
62
such as trying to create a file with no path.
64
Two sets of file creation methods are supplied. Convenience methods are:
69
These are composed of the low-level methods:
71
* create_file or create_directory or create_symlink
75
def __init__(self, tree, pb=DummyProgress()):
76
"""Note: a tree_write lock is taken on the tree.
78
Use TreeTransform.finalize() to release the lock
81
80
object.__init__(self)
83
self._limbodir = limbodir
84
self._deletiondir = None
82
self._tree.lock_tree_write()
84
control_files = self._tree._control_files
85
self._limbodir = urlutils.local_path_from_url(
86
control_files.controlfilename('limbo'))
88
os.mkdir(self._limbodir)
90
if e.errno == errno.EEXIST:
91
raise ExistingLimbo(self._limbodir)
85
96
self._id_number = 0
86
# mapping of trans_id -> new basename
87
97
self._new_name = {}
88
# mapping of trans_id -> new parent trans_id
89
98
self._new_parent = {}
90
# mapping of trans_id with new contents -> new file_kind
91
99
self._new_contents = {}
92
# A mapping of transform ids to their limbo filename
93
self._limbo_files = {}
94
# A mapping of transform ids to a set of the transform ids of children
95
# that their limbo directory has
96
self._limbo_children = {}
97
# Map transform ids to maps of child filename to child transform id
98
self._limbo_children_names = {}
99
# List of transform ids that need to be renamed from limbo into place
100
self._needs_rename = set()
101
# Set of trans_ids whose contents will be removed
102
100
self._removed_contents = set()
103
# Mapping of trans_id -> new execute-bit value
104
101
self._new_executability = {}
105
# Mapping of trans_id -> new tree-reference value
106
self._new_reference_revision = {}
107
# Mapping of trans_id -> new file_id
108
102
self._new_id = {}
109
# Mapping of old file-id -> trans_id
110
103
self._non_present_ids = {}
111
# Mapping of new file_id -> trans_id
112
104
self._r_new_id = {}
113
# Set of file_ids that will be removed
114
105
self._removed_id = set()
115
# Mapping of path in old tree -> trans_id
116
106
self._tree_path_ids = {}
117
# Mapping trans_id -> path in old tree
118
107
self._tree_id_paths = {}
119
109
# Cache of realpath results, to speed up canonical_path
121
111
# Cache of relpath results, to speed up canonical_path
123
# The trans_id that will be used as the tree root
124
112
self._new_root = self.trans_id_tree_file_id(tree.get_root_id())
125
# Indictor of whether the transform has been applied
129
# Whether the target is case sensitive
130
self._case_sensitive_target = case_sensitive
131
# A counter of how many files have been renamed
132
self.rename_count = 0
134
116
def __get_root(self):
135
117
return self._new_root
720
"""Apply all changes to the inventory and filesystem.
722
If filesystem or inventory conflicts are present, MalformedTransform
725
conflicts = self.find_conflicts()
726
if len(conflicts) != 0:
727
raise MalformedTransform(conflicts=conflicts)
729
inv = self._tree.inventory
730
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
732
child_pb.update('Apply phase', 0, 2)
733
self._apply_removals(inv, limbo_inv)
734
child_pb.update('Apply phase', 1, 2)
735
modified_paths = self._apply_insertions(inv, limbo_inv)
738
self._tree._write_inventory(inv)
741
return _TransformResults(modified_paths)
814
743
def _limbo_name(self, trans_id):
815
744
"""Generate the limbo name of a file"""
816
limbo_name = self._limbo_files.get(trans_id)
817
if limbo_name is not None:
819
parent = self._new_parent.get(trans_id)
820
# if the parent directory is already in limbo (e.g. when building a
821
# tree), choose a limbo name inside the parent, to reduce further
823
use_direct_path = False
824
if self._new_contents.get(parent) == 'directory':
825
filename = self._new_name.get(trans_id)
826
if filename is not None:
827
if parent not in self._limbo_children:
828
self._limbo_children[parent] = set()
829
self._limbo_children_names[parent] = {}
830
use_direct_path = True
831
# the direct path can only be used if no other file has
832
# already taken this pathname, i.e. if the name is unused, or
833
# if it is already associated with this trans_id.
834
elif self._case_sensitive_target:
835
if (self._limbo_children_names[parent].get(filename)
836
in (trans_id, None)):
837
use_direct_path = True
839
for l_filename, l_trans_id in\
840
self._limbo_children_names[parent].iteritems():
841
if l_trans_id == trans_id:
843
if l_filename.lower() == filename.lower():
745
return pathjoin(self._limbodir, trans_id)
747
def _apply_removals(self, inv, limbo_inv):
748
"""Perform tree operations that remove directory/inventory names.
750
That is, delete files that are to be deleted, and put any files that
751
need renaming into limbo. This must be done in strict child-to-parent
754
tree_paths = list(self._tree_path_ids.iteritems())
755
tree_paths.sort(reverse=True)
756
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
758
for num, data in enumerate(tree_paths):
759
path, trans_id = data
760
child_pb.update('removing file', num, len(tree_paths))
761
full_path = self._tree.abspath(path)
762
if trans_id in self._removed_contents:
763
delete_any(full_path)
764
elif trans_id in self._new_name or trans_id in \
767
os.rename(full_path, self._limbo_name(trans_id))
769
if e.errno != errno.ENOENT:
771
if trans_id in self._removed_id:
772
if trans_id == self._new_root:
773
file_id = self._tree.inventory.root.file_id
846
use_direct_path = True
849
limbo_name = pathjoin(self._limbo_files[parent], filename)
850
self._limbo_children[parent].add(trans_id)
851
self._limbo_children_names[parent][filename] = trans_id
853
limbo_name = pathjoin(self._limbodir, trans_id)
854
self._needs_rename.add(trans_id)
855
self._limbo_files[trans_id] = limbo_name
858
def _set_executability(self, path, entry, trans_id):
775
file_id = self.tree_file_id(trans_id)
777
elif trans_id in self._new_name or trans_id in self._new_parent:
778
file_id = self.tree_file_id(trans_id)
779
if file_id is not None:
780
limbo_inv[trans_id] = inv[file_id]
785
def _apply_insertions(self, inv, limbo_inv):
786
"""Perform tree operations that insert directory/inventory names.
788
That is, create any files that need to be created, and restore from
789
limbo any files that needed renaming. This must be done in strict
790
parent-to-child order.
792
new_paths = self.new_paths()
794
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
796
for num, (path, trans_id) in enumerate(new_paths):
797
child_pb.update('adding file', num, len(new_paths))
799
kind = self._new_contents[trans_id]
801
kind = contents = None
802
if trans_id in self._new_contents or \
803
self.path_changed(trans_id):
804
full_path = self._tree.abspath(path)
806
os.rename(self._limbo_name(trans_id), full_path)
808
# We may be renaming a dangling inventory id
809
if e.errno != errno.ENOENT:
811
if trans_id in self._new_contents:
812
modified_paths.append(full_path)
813
del self._new_contents[trans_id]
815
if trans_id in self._new_id:
817
kind = file_kind(self._tree.abspath(path))
818
inv.add_path(path, kind, self._new_id[trans_id])
819
elif trans_id in self._new_name or trans_id in\
821
entry = limbo_inv.get(trans_id)
822
if entry is not None:
823
entry.name = self.final_name(trans_id)
824
parent_path = os.path.dirname(path)
826
self._tree.inventory.path2id(parent_path)
829
# requires files and inventory entries to be in place
830
if trans_id in self._new_executability:
831
self._set_executability(path, inv, trans_id)
834
return modified_paths
836
def _set_executability(self, path, inv, trans_id):
859
837
"""Set the executability of versioned files """
838
file_id = inv.path2id(path)
860
839
new_executability = self._new_executability[trans_id]
861
entry.executable = new_executability
840
inv[file_id].executable = new_executability
862
841
if supports_executable():
863
842
abspath = self._tree.abspath(path)
864
843
current_mode = os.stat(abspath).st_mode
925
904
self.create_symlink(target, trans_id)
928
def _affected_ids(self):
929
"""Return the set of transform ids affected by the transform"""
930
trans_ids = set(self._removed_id)
931
trans_ids.update(self._new_id.keys())
932
trans_ids.update(self._removed_contents)
933
trans_ids.update(self._new_contents.keys())
934
trans_ids.update(self._new_executability.keys())
935
trans_ids.update(self._new_name.keys())
936
trans_ids.update(self._new_parent.keys())
939
def _get_file_id_maps(self):
940
"""Return mapping of file_ids to trans_ids in the to and from states"""
941
trans_ids = self._affected_ids()
944
# Build up two dicts: trans_ids associated with file ids in the
945
# FROM state, vs the TO state.
946
for trans_id in trans_ids:
947
from_file_id = self.tree_file_id(trans_id)
948
if from_file_id is not None:
949
from_trans_ids[from_file_id] = trans_id
950
to_file_id = self.final_file_id(trans_id)
951
if to_file_id is not None:
952
to_trans_ids[to_file_id] = trans_id
953
return from_trans_ids, to_trans_ids
955
def _from_file_data(self, from_trans_id, from_versioned, file_id):
956
"""Get data about a file in the from (tree) state
958
Return a (name, parent, kind, executable) tuple
960
from_path = self._tree_id_paths.get(from_trans_id)
962
# get data from working tree if versioned
963
from_entry = self._tree.inventory[file_id]
964
from_name = from_entry.name
965
from_parent = from_entry.parent_id
968
if from_path is None:
969
# File does not exist in FROM state
973
# File exists, but is not versioned. Have to use path-
975
from_name = os.path.basename(from_path)
976
tree_parent = self.get_tree_parent(from_trans_id)
977
from_parent = self.tree_file_id(tree_parent)
978
if from_path is not None:
979
from_kind, from_executable, from_stats = \
980
self._tree._comparison_data(from_entry, from_path)
983
from_executable = False
984
return from_name, from_parent, from_kind, from_executable
986
def _to_file_data(self, to_trans_id, from_trans_id, from_executable):
987
"""Get data about a file in the to (target) state
989
Return a (name, parent, kind, executable) tuple
991
to_name = self.final_name(to_trans_id)
993
to_kind = self.final_kind(to_trans_id)
996
to_parent = self.final_file_id(self.final_parent(to_trans_id))
997
if to_trans_id in self._new_executability:
998
to_executable = self._new_executability[to_trans_id]
999
elif to_trans_id == from_trans_id:
1000
to_executable = from_executable
1002
to_executable = False
1003
return to_name, to_parent, to_kind, to_executable
1005
def iter_changes(self):
1006
"""Produce output in the same format as Tree.iter_changes.
1008
Will produce nonsensical results if invoked while inventory/filesystem
1009
conflicts (as reported by TreeTransform.find_conflicts()) are present.
1011
This reads the Transform, but only reproduces changes involving a
1012
file_id. Files that are not versioned in either of the FROM or TO
1013
states are not reflected.
1015
final_paths = FinalPaths(self)
1016
from_trans_ids, to_trans_ids = self._get_file_id_maps()
1018
# Now iterate through all active file_ids
1019
for file_id in set(from_trans_ids.keys() + to_trans_ids.keys()):
1021
from_trans_id = from_trans_ids.get(file_id)
1022
# find file ids, and determine versioning state
1023
if from_trans_id is None:
1024
from_versioned = False
1025
from_trans_id = to_trans_ids[file_id]
1027
from_versioned = True
1028
to_trans_id = to_trans_ids.get(file_id)
1029
if to_trans_id is None:
1030
to_versioned = False
1031
to_trans_id = from_trans_id
1035
from_name, from_parent, from_kind, from_executable = \
1036
self._from_file_data(from_trans_id, from_versioned, file_id)
1038
to_name, to_parent, to_kind, to_executable = \
1039
self._to_file_data(to_trans_id, from_trans_id, from_executable)
1041
if not from_versioned:
1044
from_path = self._tree_id_paths.get(from_trans_id)
1045
if not to_versioned:
1048
to_path = final_paths.get_path(to_trans_id)
1049
if from_kind != to_kind:
1051
elif to_kind in ('file', 'symlink') and (
1052
to_trans_id != from_trans_id or
1053
to_trans_id in self._new_contents):
1055
if (not modified and from_versioned == to_versioned and
1056
from_parent==to_parent and from_name == to_name and
1057
from_executable == to_executable):
1059
results.append((file_id, (from_path, to_path), modified,
1060
(from_versioned, to_versioned),
1061
(from_parent, to_parent),
1062
(from_name, to_name),
1063
(from_kind, to_kind),
1064
(from_executable, to_executable)))
1065
return iter(sorted(results, key=lambda x:x[1]))
1067
def get_preview_tree(self):
1068
"""Return a tree representing the result of the transform.
1070
This tree only supports the subset of Tree functionality required
1071
by show_diff_trees. It must only be compared to tt._tree.
1073
return _PreviewTree(self)
1076
class TreeTransform(TreeTransformBase):
1077
"""Represent a tree transformation.
1079
This object is designed to support incremental generation of the transform,
1082
However, it gives optimum performance when parent directories are created
1083
before their contents. The transform is then able to put child files
1084
directly in their parent directory, avoiding later renames.
1086
It is easy to produce malformed transforms, but they are generally
1087
harmless. Attempting to apply a malformed transform will cause an
1088
exception to be raised before any modifications are made to the tree.
1090
Many kinds of malformed transforms can be corrected with the
1091
resolve_conflicts function. The remaining ones indicate programming error,
1092
such as trying to create a file with no path.
1094
Two sets of file creation methods are supplied. Convenience methods are:
1099
These are composed of the low-level methods:
1101
* create_file or create_directory or create_symlink
1105
Transform/Transaction ids
1106
-------------------------
1107
trans_ids are temporary ids assigned to all files involved in a transform.
1108
It's possible, even common, that not all files in the Tree have trans_ids.
1110
trans_ids are used because filenames and file_ids are not good enough
1111
identifiers; filenames change, and not all files have file_ids. File-ids
1112
are also associated with trans-ids, so that moving a file moves its
1115
trans_ids are only valid for the TreeTransform that generated them.
1119
Limbo is a temporary directory use to hold new versions of files.
1120
Files are added to limbo by create_file, create_directory, create_symlink,
1121
and their convenience variants (new_*). Files may be removed from limbo
1122
using cancel_creation. Files are renamed from limbo into their final
1123
location as part of TreeTransform.apply
1125
Limbo must be cleaned up, by either calling TreeTransform.apply or
1126
calling TreeTransform.finalize.
1128
Files are placed into limbo inside their parent directories, where
1129
possible. This reduces subsequent renames, and makes operations involving
1130
lots of files faster. This optimization is only possible if the parent
1131
directory is created *before* creating any of its children, so avoid
1132
creating children before parents, where possible.
1136
This temporary directory is used by _FileMover for storing files that are
1137
about to be deleted. In case of rollback, the files will be restored.
1138
FileMover does not delete files until it is sure that a rollback will not
1141
def __init__(self, tree, pb=DummyProgress()):
1142
"""Note: a tree_write lock is taken on the tree.
1144
Use TreeTransform.finalize() to release the lock (can be omitted if
1145
TreeTransform.apply() called).
1147
tree.lock_tree_write()
1150
control_files = tree._control_files
1151
limbodir = urlutils.local_path_from_url(
1152
control_files.controlfilename('limbo'))
1156
if e.errno == errno.EEXIST:
1157
raise ExistingLimbo(limbodir)
1158
deletiondir = urlutils.local_path_from_url(
1159
control_files.controlfilename('pending-deletion'))
1161
os.mkdir(deletiondir)
1163
if e.errno == errno.EEXIST:
1164
raise errors.ExistingPendingDeletion(deletiondir)
1169
TreeTransformBase.__init__(self, tree, limbodir, pb,
1170
tree.case_sensitive)
1171
self._deletiondir = deletiondir
1173
def apply(self, no_conflicts=False, _mover=None):
1174
"""Apply all changes to the inventory and filesystem.
1176
If filesystem or inventory conflicts are present, MalformedTransform
1179
If apply succeeds, finalize is not necessary.
1181
:param no_conflicts: if True, the caller guarantees there are no
1182
conflicts, so no check is made.
1183
:param _mover: Supply an alternate FileMover, for testing
1185
if not no_conflicts:
1186
conflicts = self.find_conflicts()
1187
if len(conflicts) != 0:
1188
raise MalformedTransform(conflicts=conflicts)
1189
inventory_delta = []
1190
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1193
mover = _FileMover()
1197
child_pb.update('Apply phase', 0, 2)
1198
self._apply_removals(inventory_delta, mover)
1199
child_pb.update('Apply phase', 1, 2)
1200
modified_paths = self._apply_insertions(inventory_delta, mover)
1205
mover.apply_deletions()
1208
self._tree.apply_inventory_delta(inventory_delta)
1211
return _TransformResults(modified_paths, self.rename_count)
1213
def _apply_removals(self, inventory_delta, mover):
1214
"""Perform tree operations that remove directory/inventory names.
1216
That is, delete files that are to be deleted, and put any files that
1217
need renaming into limbo. This must be done in strict child-to-parent
1220
tree_paths = list(self._tree_path_ids.iteritems())
1221
tree_paths.sort(reverse=True)
1222
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1224
for num, data in enumerate(tree_paths):
1225
path, trans_id = data
1226
child_pb.update('removing file', num, len(tree_paths))
1227
full_path = self._tree.abspath(path)
1228
if trans_id in self._removed_contents:
1229
mover.pre_delete(full_path, os.path.join(self._deletiondir,
1231
elif trans_id in self._new_name or trans_id in \
1234
mover.rename(full_path, self._limbo_name(trans_id))
1236
if e.errno != errno.ENOENT:
1239
self.rename_count += 1
1240
if trans_id in self._removed_id:
1241
if trans_id == self._new_root:
1242
file_id = self._tree.get_root_id()
1244
file_id = self.tree_file_id(trans_id)
1245
assert file_id is not None
1246
# File-id isn't really being deleted, just moved
1247
if file_id in self._r_new_id:
1249
inventory_delta.append((path, None, file_id, None))
1253
def _apply_insertions(self, inventory_delta, mover):
1254
"""Perform tree operations that insert directory/inventory names.
1256
That is, create any files that need to be created, and restore from
1257
limbo any files that needed renaming. This must be done in strict
1258
parent-to-child order.
1260
new_paths = self.new_paths()
1262
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1265
for num, (path, trans_id) in enumerate(new_paths):
1267
child_pb.update('adding file', num, len(new_paths))
1268
if trans_id in self._new_contents or \
1269
self.path_changed(trans_id):
1270
full_path = self._tree.abspath(path)
1271
if trans_id in self._needs_rename:
1273
mover.rename(self._limbo_name(trans_id), full_path)
1275
# We may be renaming a dangling inventory id
1276
if e.errno != errno.ENOENT:
1279
self.rename_count += 1
1280
if trans_id in self._new_contents:
1281
modified_paths.append(full_path)
1282
completed_new.append(trans_id)
1283
file_id = self.final_file_id(trans_id)
1284
if file_id is not None and (trans_id in self._new_id or
1285
trans_id in self._new_name or trans_id in self._new_parent
1286
or trans_id in self._new_executability):
1288
kind = self.final_kind(trans_id)
1290
kind = self._tree.stored_kind(file_id)
1291
if trans_id in self._new_reference_revision:
1292
new_entry = inventory.TreeReference(
1293
self.final_file_id(trans_id),
1294
self._new_name[trans_id],
1295
self.final_file_id(self._new_parent[trans_id]),
1296
None, self._new_reference_revision[trans_id])
1298
new_entry = inventory.make_entry(kind,
1299
self.final_name(trans_id),
1300
self.final_file_id(self.final_parent(trans_id)),
1301
self.final_file_id(trans_id))
1303
old_path = self._tree.id2path(new_entry.file_id)
1304
except errors.NoSuchId:
1306
inventory_delta.append((old_path, path, new_entry.file_id,
1309
if trans_id in self._new_executability:
1310
self._set_executability(path, new_entry, trans_id)
1313
for trans_id in completed_new:
1314
del self._new_contents[trans_id]
1315
return modified_paths
1318
class TransformPreview(TreeTransformBase):
1319
"""A TreeTransform for generating preview trees.
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.
1326
def __init__(self, tree, pb=DummyProgress(), case_sensitive=True):
1328
limbodir = tempfile.mkdtemp(prefix='bzr-limbo-')
1329
TreeTransformBase.__init__(self, tree, limbodir, pb, case_sensitive)
1331
def canonical_path(self, path):
1334
def tree_kind(self, trans_id):
1335
path = self._tree_id_paths.get(trans_id)
1337
raise NoSuchFile(None)
1338
file_id = self._tree.path2id(path)
1339
return self._tree.kind(file_id)
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.
1347
# is it ok to ignore this? probably
1350
def iter_tree_children(self, parent_id):
1351
"""Iterate through the entry's tree children, if any"""
1353
path = self._tree_id_paths[parent_id]
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)
1362
class _PreviewTree(object):
1363
"""Partial implementation of Tree to support show_diff_trees"""
1365
def __init__(self, transform):
1366
self._transform = transform
1368
def lock_read(self):
1369
# Perhaps in theory, this should lock the TreeTransform?
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.
1380
This implementation does not support include_unchanged, specific_files,
1381
or want_unversioned. extra_trees, require_versioned, and pb are
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()
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)
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
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')
1410
def get_symlink_target(self, file_id):
1411
"""See Tree.get_symlink_target"""
1412
trans_id = self._transform.trans_id_file_id(file_id)
1413
name = self._transform._limbo_name(trans_id)
1414
return os.readlink(name)
1416
def paths2ids(self, specific_files, trees=None, require_versioned=False):
1417
"""See Tree.paths2ids"""
1421
907
def joinpath(parent, child):
1422
908
"""Join tree-relative paths, handling the tree root specially"""
1423
909
if parent is None or parent == "":
1831
1198
return has_contents, contents_mod, meta_mod
1834
def revert(working_tree, target_tree, filenames, backups=False,
1835
pb=DummyProgress(), change_reporter=None):
1201
def revert(working_tree, target_tree, filenames, backups=False,
1202
pb=DummyProgress()):
1836
1203
"""Revert a working tree's contents to those of a target tree."""
1837
target_tree.lock_read()
1204
interesting_ids = find_interesting(working_tree, target_tree, filenames)
1838
1205
tt = TreeTransform(working_tree, pb)
1840
1207
pp = ProgressPhase("Revert phase", 3, pb)
1841
1208
pp.next_phase()
1842
1209
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1844
merge_modified = _alter_files(working_tree, target_tree, tt,
1845
child_pb, filenames, backups)
1211
_alter_files(working_tree, target_tree, tt, child_pb,
1212
interesting_ids, backups)
1847
1214
child_pb.finished()
1848
1215
pp.next_phase()
1849
1216
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1851
raw_conflicts = resolve_conflicts(tt, child_pb,
1852
lambda t, c: conflict_pass(t, c, target_tree))
1218
raw_conflicts = resolve_conflicts(tt, child_pb)
1854
1220
child_pb.finished()
1855
1221
conflicts = cook_conflicts(raw_conflicts, tt)
1857
change_reporter = delta._ChangeReporter(
1858
unversioned_filter=working_tree.is_ignored)
1859
delta.report_changes(tt.iter_changes(), change_reporter)
1860
1222
for conflict in conflicts:
1861
1223
warning(conflict)
1862
1224
pp.next_phase()
1864
working_tree.set_merge_modified(merge_modified)
1226
working_tree.set_merge_modified({})
1866
target_tree.unlock()
1869
1230
return conflicts
1872
def _alter_files(working_tree, target_tree, tt, pb, specific_files,
1233
def _alter_files(working_tree, target_tree, tt, pb, interesting_ids, backups):
1874
1234
merge_modified = working_tree.merge_modified()
1875
change_list = target_tree.iter_changes(working_tree,
1876
specific_files=specific_files, pb=pb)
1235
iterator = target_tree._iter_changes(working_tree,
1236
specific_file_ids=interesting_ids,
1877
1238
if target_tree.inventory.root is None:
1878
1239
skip_root = True
1880
1241
skip_root = False
1881
1242
basis_tree = None
1884
for id_num, (file_id, path, changed_content, versioned, parent, name,
1885
kind, executable) in enumerate(change_list):
1886
if skip_root and file_id[0] is not None and parent[0] is None:
1888
trans_id = tt.trans_id_file_id(file_id)
1891
keep_content = False
1892
if kind[0] == 'file' and (backups or kind[1] is None):
1893
wt_sha1 = working_tree.get_file_sha1(file_id)
1894
if merge_modified.get(file_id) != wt_sha1:
1895
# acquire the basis tree lazily to prevent the
1896
# expense of accessing it when it's not needed ?
1897
# (Guessing, RBC, 200702)
1898
if basis_tree is None:
1899
basis_tree = working_tree.basis_tree()
1900
basis_tree.lock_read()
1901
if file_id in basis_tree:
1902
if wt_sha1 != basis_tree.get_file_sha1(file_id):
1904
elif kind[1] is None and not versioned[1]:
1906
if kind[0] is not None:
1907
if not keep_content:
1908
tt.delete_contents(trans_id)
1909
elif kind[1] is not None:
1910
parent_trans_id = tt.trans_id_file_id(parent[0])
1911
by_parent = tt.by_parent()
1912
backup_name = _get_backup_name(name[0], by_parent,
1913
parent_trans_id, tt)
1914
tt.adjust_path(backup_name, parent_trans_id, trans_id)
1915
new_trans_id = tt.create_path(name[0], parent_trans_id)
1916
if versioned == (True, True):
1917
tt.unversion_file(trans_id)
1918
tt.version_file(file_id, new_trans_id)
1919
# New contents should have the same unix perms as old
1922
trans_id = new_trans_id
1923
if kind[1] == 'directory':
1924
tt.create_directory(trans_id)
1925
elif kind[1] == 'symlink':
1926
tt.create_symlink(target_tree.get_symlink_target(file_id),
1928
elif kind[1] == 'file':
1929
deferred_files.append((file_id, (trans_id, mode_id)))
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:
1247
trans_id = tt.trans_id_file_id(file_id)
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:
1930
1254
if basis_tree is None:
1931
1255
basis_tree = working_tree.basis_tree()
1932
basis_tree.lock_read()
1933
new_sha1 = target_tree.get_file_sha1(file_id)
1934
if (file_id in basis_tree and new_sha1 ==
1935
basis_tree.get_file_sha1(file_id)):
1936
if file_id in merge_modified:
1937
del merge_modified[file_id]
1939
merge_modified[file_id] = new_sha1
1941
# preserve the execute bit when backing up
1942
if keep_content and executable[0] == executable[1]:
1943
tt.set_executability(executable[1], trans_id)
1945
assert kind[1] is None
1946
if versioned == (False, True):
1947
tt.version_file(file_id, trans_id)
1948
if versioned == (True, False):
1949
tt.unversion_file(trans_id)
1950
if (name[1] is not None and
1951
(name[0] != name[1] or parent[0] != parent[1])):
1953
name[1], tt.trans_id_file_id(parent[1]), trans_id)
1954
if executable[0] != executable[1] and kind[1] == "file":
1955
tt.set_executability(executable[1], trans_id)
1956
for (trans_id, mode_id), bytes in target_tree.iter_files_bytes(
1958
tt.create_file(bytes, trans_id, mode_id)
1960
if basis_tree is not None:
1962
return merge_modified
1256
if file_id in basis_tree:
1257
if wt_sha1 != basis_tree.get_file_sha1(file_id):
1259
elif kind[1] is None and not versioned[1]:
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
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),
1283
elif kind[1] == 'file':
1284
tt.create_file(target_tree.get_file_lines(file_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)
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)
1965
1302
def resolve_conflicts(tt, pb=DummyProgress(), pass_func=None):