795
882
self.create_symlink(target, trans_id)
798
def new_orphan(self, trans_id, parent_id):
799
"""Schedule an item to be orphaned.
801
When a directory is about to be removed, its children, if they are not
802
versioned are moved out of the way: they don't have a parent anymore.
804
:param trans_id: The trans_id of the existing item.
805
:param parent_id: The parent trans_id of the item.
807
raise NotImplementedError(self.new_orphan)
809
def _get_potential_orphans(self, dir_id):
810
"""Find the potential orphans in a directory.
812
A directory can't be safely deleted if there are versioned files in it.
813
If all the contained files are unversioned then they can be orphaned.
815
The 'None' return value means that the directory contains at least one
816
versioned file and should not be deleted.
818
:param dir_id: The directory trans id.
820
:return: A list of the orphan trans ids or None if at least one
821
versioned file is present.
824
# Find the potential orphans, stop if one item should be kept
825
for child_tid in self.by_parent()[dir_id]:
826
if child_tid in self._removed_contents:
827
# The child is removed as part of the transform. Since it was
828
# versioned before, it's not an orphan
830
elif self.final_file_id(child_tid) is None:
831
# The child is not versioned
832
orphans.append(child_tid)
834
# We have a versioned file here, searching for orphans is
840
def _affected_ids(self):
841
"""Return the set of transform ids affected by the transform"""
842
trans_ids = set(self._removed_id)
843
trans_ids.update(self._new_id.keys())
844
trans_ids.update(self._removed_contents)
845
trans_ids.update(self._new_contents.keys())
846
trans_ids.update(self._new_executability.keys())
847
trans_ids.update(self._new_name.keys())
848
trans_ids.update(self._new_parent.keys())
851
def _get_file_id_maps(self):
852
"""Return mapping of file_ids to trans_ids in the to and from states"""
853
trans_ids = self._affected_ids()
856
# Build up two dicts: trans_ids associated with file ids in the
857
# FROM state, vs the TO state.
858
for trans_id in trans_ids:
859
from_file_id = self.tree_file_id(trans_id)
860
if from_file_id is not None:
861
from_trans_ids[from_file_id] = trans_id
862
to_file_id = self.final_file_id(trans_id)
863
if to_file_id is not None:
864
to_trans_ids[to_file_id] = trans_id
865
return from_trans_ids, to_trans_ids
867
def _from_file_data(self, from_trans_id, from_versioned, file_id):
868
"""Get data about a file in the from (tree) state
870
Return a (name, parent, kind, executable) tuple
872
from_path = self._tree_id_paths.get(from_trans_id)
874
# get data from working tree if versioned
875
from_entry = self._tree.iter_entries_by_dir([file_id]).next()[1]
876
from_name = from_entry.name
877
from_parent = from_entry.parent_id
880
if from_path is None:
881
# File does not exist in FROM state
885
# File exists, but is not versioned. Have to use path-
887
from_name = os.path.basename(from_path)
888
tree_parent = self.get_tree_parent(from_trans_id)
889
from_parent = self.tree_file_id(tree_parent)
890
if from_path is not None:
891
from_kind, from_executable, from_stats = \
892
self._tree._comparison_data(from_entry, from_path)
895
from_executable = False
896
return from_name, from_parent, from_kind, from_executable
898
def _to_file_data(self, to_trans_id, from_trans_id, from_executable):
899
"""Get data about a file in the to (target) state
901
Return a (name, parent, kind, executable) tuple
903
to_name = self.final_name(to_trans_id)
904
to_kind = self.final_kind(to_trans_id)
905
to_parent = self.final_file_id(self.final_parent(to_trans_id))
906
if to_trans_id in self._new_executability:
907
to_executable = self._new_executability[to_trans_id]
908
elif to_trans_id == from_trans_id:
909
to_executable = from_executable
911
to_executable = False
912
return to_name, to_parent, to_kind, to_executable
914
def iter_changes(self):
915
"""Produce output in the same format as Tree.iter_changes.
917
Will produce nonsensical results if invoked while inventory/filesystem
918
conflicts (as reported by TreeTransform.find_conflicts()) are present.
920
This reads the Transform, but only reproduces changes involving a
921
file_id. Files that are not versioned in either of the FROM or TO
922
states are not reflected.
924
final_paths = FinalPaths(self)
925
from_trans_ids, to_trans_ids = self._get_file_id_maps()
927
# Now iterate through all active file_ids
928
for file_id in set(from_trans_ids.keys() + to_trans_ids.keys()):
930
from_trans_id = from_trans_ids.get(file_id)
931
# find file ids, and determine versioning state
932
if from_trans_id is None:
933
from_versioned = False
934
from_trans_id = to_trans_ids[file_id]
936
from_versioned = True
937
to_trans_id = to_trans_ids.get(file_id)
938
if to_trans_id is None:
940
to_trans_id = from_trans_id
944
from_name, from_parent, from_kind, from_executable = \
945
self._from_file_data(from_trans_id, from_versioned, file_id)
947
to_name, to_parent, to_kind, to_executable = \
948
self._to_file_data(to_trans_id, from_trans_id, from_executable)
950
if not from_versioned:
953
from_path = self._tree_id_paths.get(from_trans_id)
957
to_path = final_paths.get_path(to_trans_id)
958
if from_kind != to_kind:
960
elif to_kind in ('file', 'symlink') and (
961
to_trans_id != from_trans_id or
962
to_trans_id in self._new_contents):
964
if (not modified and from_versioned == to_versioned and
965
from_parent==to_parent and from_name == to_name and
966
from_executable == to_executable):
968
results.append((file_id, (from_path, to_path), modified,
969
(from_versioned, to_versioned),
970
(from_parent, to_parent),
971
(from_name, to_name),
972
(from_kind, to_kind),
973
(from_executable, to_executable)))
974
return iter(sorted(results, key=lambda x:x[1]))
976
def get_preview_tree(self):
977
"""Return a tree representing the result of the transform.
979
The tree is a snapshot, and altering the TreeTransform will invalidate
982
return _PreviewTree(self)
984
def commit(self, branch, message, merge_parents=None, strict=False,
985
timestamp=None, timezone=None, committer=None, authors=None,
986
revprops=None, revision_id=None):
987
"""Commit the result of this TreeTransform to a branch.
989
:param branch: The branch to commit to.
990
:param message: The message to attach to the commit.
991
:param merge_parents: Additional parent revision-ids specified by
993
:param strict: If True, abort the commit if there are unversioned
995
:param timestamp: if not None, seconds-since-epoch for the time and
996
date. (May be a float.)
997
:param timezone: Optional timezone for timestamp, as an offset in
999
:param committer: Optional committer in email-id format.
1000
(e.g. "J Random Hacker <jrandom@example.com>")
1001
:param authors: Optional list of authors in email-id format.
1002
:param revprops: Optional dictionary of revision properties.
1003
:param revision_id: Optional revision id. (Specifying a revision-id
1004
may reduce performance for some non-native formats.)
1005
:return: The revision_id of the revision committed.
1007
self._check_malformed()
1009
unversioned = set(self._new_contents).difference(set(self._new_id))
1010
for trans_id in unversioned:
1011
if self.final_file_id(trans_id) is None:
1012
raise errors.StrictCommitFailed()
1014
revno, last_rev_id = branch.last_revision_info()
1015
if last_rev_id == _mod_revision.NULL_REVISION:
1016
if merge_parents is not None:
1017
raise ValueError('Cannot supply merge parents for first'
1021
parent_ids = [last_rev_id]
1022
if merge_parents is not None:
1023
parent_ids.extend(merge_parents)
1024
if self._tree.get_revision_id() != last_rev_id:
1025
raise ValueError('TreeTransform not based on branch basis: %s' %
1026
self._tree.get_revision_id())
1027
revprops = commit.Commit.update_revprops(revprops, branch, authors)
1028
builder = branch.get_commit_builder(parent_ids,
1029
timestamp=timestamp,
1031
committer=committer,
1033
revision_id=revision_id)
1034
preview = self.get_preview_tree()
1035
list(builder.record_iter_changes(preview, last_rev_id,
1036
self.iter_changes()))
1037
builder.finish_inventory()
1038
revision_id = builder.commit(message)
1039
branch.set_last_revision_info(revno + 1, revision_id)
1042
def _text_parent(self, trans_id):
1043
file_id = self.tree_file_id(trans_id)
1045
if file_id is None or self._tree.kind(file_id) != 'file':
1047
except errors.NoSuchFile:
1051
def _get_parents_texts(self, trans_id):
1052
"""Get texts for compression parents of this file."""
1053
file_id = self._text_parent(trans_id)
1056
return (self._tree.get_file_text(file_id),)
1058
def _get_parents_lines(self, trans_id):
1059
"""Get lines for compression parents of this file."""
1060
file_id = self._text_parent(trans_id)
1063
return (self._tree.get_file_lines(file_id),)
1065
def serialize(self, serializer):
1066
"""Serialize this TreeTransform.
1068
:param serializer: A Serialiser like pack.ContainerSerializer.
1070
new_name = dict((k, v.encode('utf-8')) for k, v in
1071
self._new_name.items())
1072
new_executability = dict((k, int(v)) for k, v in
1073
self._new_executability.items())
1074
tree_path_ids = dict((k.encode('utf-8'), v)
1075
for k, v in self._tree_path_ids.items())
1077
'_id_number': self._id_number,
1078
'_new_name': new_name,
1079
'_new_parent': self._new_parent,
1080
'_new_executability': new_executability,
1081
'_new_id': self._new_id,
1082
'_tree_path_ids': tree_path_ids,
1083
'_removed_id': list(self._removed_id),
1084
'_removed_contents': list(self._removed_contents),
1085
'_non_present_ids': self._non_present_ids,
1087
yield serializer.bytes_record(bencode.bencode(attribs),
1089
for trans_id, kind in self._new_contents.items():
1091
lines = osutils.chunks_to_lines(
1092
self._read_file_chunks(trans_id))
1093
parents = self._get_parents_lines(trans_id)
1094
mpdiff = multiparent.MultiParent.from_lines(lines, parents)
1095
content = ''.join(mpdiff.to_patch())
1096
if kind == 'directory':
1098
if kind == 'symlink':
1099
content = self._read_symlink_target(trans_id)
1100
yield serializer.bytes_record(content, ((trans_id, kind),))
1102
def deserialize(self, records):
1103
"""Deserialize a stored TreeTransform.
1105
:param records: An iterable of (names, content) tuples, as per
1106
pack.ContainerPushParser.
1108
names, content = records.next()
1109
attribs = bencode.bdecode(content)
1110
self._id_number = attribs['_id_number']
1111
self._new_name = dict((k, v.decode('utf-8'))
1112
for k, v in attribs['_new_name'].items())
1113
self._new_parent = attribs['_new_parent']
1114
self._new_executability = dict((k, bool(v)) for k, v in
1115
attribs['_new_executability'].items())
1116
self._new_id = attribs['_new_id']
1117
self._r_new_id = dict((v, k) for k, v in self._new_id.items())
1118
self._tree_path_ids = {}
1119
self._tree_id_paths = {}
1120
for bytepath, trans_id in attribs['_tree_path_ids'].items():
1121
path = bytepath.decode('utf-8')
1122
self._tree_path_ids[path] = trans_id
1123
self._tree_id_paths[trans_id] = path
1124
self._removed_id = set(attribs['_removed_id'])
1125
self._removed_contents = set(attribs['_removed_contents'])
1126
self._non_present_ids = attribs['_non_present_ids']
1127
for ((trans_id, kind),), content in records:
1129
mpdiff = multiparent.MultiParent.from_patch(content)
1130
lines = mpdiff.to_lines(self._get_parents_texts(trans_id))
1131
self.create_file(lines, trans_id)
1132
if kind == 'directory':
1133
self.create_directory(trans_id)
1134
if kind == 'symlink':
1135
self.create_symlink(content.decode('utf-8'), trans_id)
1138
class DiskTreeTransform(TreeTransformBase):
1139
"""Tree transform storing its contents on disk."""
1141
def __init__(self, tree, limbodir, pb=None,
1142
case_sensitive=True):
1144
:param tree: The tree that will be transformed, but not necessarily
1146
:param limbodir: A directory where new files can be stored until
1147
they are installed in their proper places
1149
:param case_sensitive: If True, the target of the transform is
1150
case sensitive, not just case preserving.
1152
TreeTransformBase.__init__(self, tree, pb, case_sensitive)
1153
self._limbodir = limbodir
1154
self._deletiondir = None
1155
# A mapping of transform ids to their limbo filename
1156
self._limbo_files = {}
1157
# A mapping of transform ids to a set of the transform ids of children
1158
# that their limbo directory has
1159
self._limbo_children = {}
1160
# Map transform ids to maps of child filename to child transform id
1161
self._limbo_children_names = {}
1162
# List of transform ids that need to be renamed from limbo into place
1163
self._needs_rename = set()
1164
self._creation_mtime = None
1167
"""Release the working tree lock, if held, clean up limbo dir.
1169
This is required if apply has not been invoked, but can be invoked
1172
if self._tree is None:
1175
entries = [(self._limbo_name(t), t, k) for t, k in
1176
self._new_contents.iteritems()]
1177
entries.sort(reverse=True)
1178
for path, trans_id, kind in entries:
1181
delete_any(self._limbodir)
1183
# We don't especially care *why* the dir is immortal.
1184
raise ImmortalLimbo(self._limbodir)
1186
if self._deletiondir is not None:
1187
delete_any(self._deletiondir)
1189
raise errors.ImmortalPendingDeletion(self._deletiondir)
1191
TreeTransformBase.finalize(self)
1193
def _limbo_name(self, trans_id):
1194
"""Generate the limbo name of a file"""
1195
limbo_name = self._limbo_files.get(trans_id)
1196
if limbo_name is None:
1197
limbo_name = self._generate_limbo_path(trans_id)
1198
self._limbo_files[trans_id] = limbo_name
1201
def _generate_limbo_path(self, trans_id):
1202
"""Generate a limbo path using the trans_id as the relative path.
1204
This is suitable as a fallback, and when the transform should not be
1205
sensitive to the path encoding of the limbo directory.
1207
self._needs_rename.add(trans_id)
1208
return pathjoin(self._limbodir, trans_id)
1210
def adjust_path(self, name, parent, trans_id):
1211
previous_parent = self._new_parent.get(trans_id)
1212
previous_name = self._new_name.get(trans_id)
1213
TreeTransformBase.adjust_path(self, name, parent, trans_id)
1214
if (trans_id in self._limbo_files and
1215
trans_id not in self._needs_rename):
1216
self._rename_in_limbo([trans_id])
1217
if previous_parent != parent:
1218
self._limbo_children[previous_parent].remove(trans_id)
1219
if previous_parent != parent or previous_name != name:
1220
del self._limbo_children_names[previous_parent][previous_name]
1222
def _rename_in_limbo(self, trans_ids):
1223
"""Fix limbo names so that the right final path is produced.
1225
This means we outsmarted ourselves-- we tried to avoid renaming
1226
these files later by creating them with their final names in their
1227
final parents. But now the previous name or parent is no longer
1228
suitable, so we have to rename them.
1230
Even for trans_ids that have no new contents, we must remove their
1231
entries from _limbo_files, because they are now stale.
1233
for trans_id in trans_ids:
1234
old_path = self._limbo_files.pop(trans_id)
1235
if trans_id not in self._new_contents:
1237
new_path = self._limbo_name(trans_id)
1238
os.rename(old_path, new_path)
1239
for descendant in self._limbo_descendants(trans_id):
1240
desc_path = self._limbo_files[descendant]
1241
desc_path = new_path + desc_path[len(old_path):]
1242
self._limbo_files[descendant] = desc_path
1244
def _limbo_descendants(self, trans_id):
1245
"""Return the set of trans_ids whose limbo paths descend from this."""
1246
descendants = set(self._limbo_children.get(trans_id, []))
1247
for descendant in list(descendants):
1248
descendants.update(self._limbo_descendants(descendant))
1251
def create_file(self, contents, trans_id, mode_id=None, sha1=None):
1252
"""Schedule creation of a new file.
1256
:param contents: an iterator of strings, all of which will be written
1257
to the target destination.
1258
:param trans_id: TreeTransform handle
1259
:param mode_id: If not None, force the mode of the target file to match
1260
the mode of the object referenced by mode_id.
1261
Otherwise, we will try to preserve mode bits of an existing file.
1262
:param sha1: If the sha1 of this content is already known, pass it in.
1263
We can use it to prevent future sha1 computations.
1265
name = self._limbo_name(trans_id)
1266
f = open(name, 'wb')
1269
unique_add(self._new_contents, trans_id, 'file')
1271
# Clean up the file, it never got registered so
1272
# TreeTransform.finalize() won't clean it up.
1276
f.writelines(contents)
1279
self._set_mtime(name)
1280
self._set_mode(trans_id, mode_id, S_ISREG)
1281
# It is unfortunate we have to use lstat instead of fstat, but we just
1282
# used utime and chmod on the file, so we need the accurate final
1284
if sha1 is not None:
1285
self._observed_sha1s[trans_id] = (sha1, osutils.lstat(name))
1287
def _read_file_chunks(self, trans_id):
1288
cur_file = open(self._limbo_name(trans_id), 'rb')
1290
return cur_file.readlines()
1294
def _read_symlink_target(self, trans_id):
1295
return os.readlink(self._limbo_name(trans_id))
1297
def _set_mtime(self, path):
1298
"""All files that are created get the same mtime.
1300
This time is set by the first object to be created.
1302
if self._creation_mtime is None:
1303
self._creation_mtime = time.time()
1304
os.utime(path, (self._creation_mtime, self._creation_mtime))
1306
def create_hardlink(self, path, trans_id):
1307
"""Schedule creation of a hard link"""
1308
name = self._limbo_name(trans_id)
1312
if e.errno != errno.EPERM:
1314
raise errors.HardLinkNotSupported(path)
1316
unique_add(self._new_contents, trans_id, 'file')
1318
# Clean up the file, it never got registered so
1319
# TreeTransform.finalize() won't clean it up.
1323
def create_directory(self, trans_id):
1324
"""Schedule creation of a new directory.
1326
See also new_directory.
1328
os.mkdir(self._limbo_name(trans_id))
1329
unique_add(self._new_contents, trans_id, 'directory')
1331
def create_symlink(self, target, trans_id):
1332
"""Schedule creation of a new symbolic link.
1334
target is a bytestring.
1335
See also new_symlink.
1338
os.symlink(target, self._limbo_name(trans_id))
1339
unique_add(self._new_contents, trans_id, 'symlink')
1342
path = FinalPaths(self).get_path(trans_id)
1345
raise UnableCreateSymlink(path=path)
1347
def cancel_creation(self, trans_id):
1348
"""Cancel the creation of new file contents."""
1349
del self._new_contents[trans_id]
1350
if trans_id in self._observed_sha1s:
1351
del self._observed_sha1s[trans_id]
1352
children = self._limbo_children.get(trans_id)
1353
# if this is a limbo directory with children, move them before removing
1355
if children is not None:
1356
self._rename_in_limbo(children)
1357
del self._limbo_children[trans_id]
1358
del self._limbo_children_names[trans_id]
1359
delete_any(self._limbo_name(trans_id))
1361
def new_orphan(self, trans_id, parent_id):
1362
# FIXME: There is no tree config, so we use the branch one (it's weird
1363
# to define it this way as orphaning can only occur in a working tree,
1364
# but that's all we have (for now). It will find the option in
1365
# locations.conf or bazaar.conf though) -- vila 20100916
1366
conf = self._tree.branch.get_config()
1367
conf_var_name = 'bzr.transform.orphan_policy'
1368
orphan_policy = conf.get_user_option(conf_var_name)
1369
default_policy = orphaning_registry.default_key
1370
if orphan_policy is None:
1371
orphan_policy = default_policy
1372
if orphan_policy not in orphaning_registry:
1373
trace.warning('%s (from %s) is not a known policy, defaulting '
1374
'to %s' % (orphan_policy, conf_var_name, default_policy))
1375
orphan_policy = default_policy
1376
handle_orphan = orphaning_registry.get(orphan_policy)
1377
handle_orphan(self, trans_id, parent_id)
1380
class OrphaningError(errors.BzrError):
1382
# Only bugs could lead to such exception being seen by the user
1383
internal_error = True
1384
_fmt = "Error while orphaning %s in %s directory"
1386
def __init__(self, orphan, parent):
1387
errors.BzrError.__init__(self)
1388
self.orphan = orphan
1389
self.parent = parent
1392
class OrphaningForbidden(OrphaningError):
1394
_fmt = "Policy: %s doesn't allow creating orphans."
1396
def __init__(self, policy):
1397
errors.BzrError.__init__(self)
1398
self.policy = policy
1401
def move_orphan(tt, orphan_id, parent_id):
1402
"""See TreeTransformBase.new_orphan.
1404
This creates a new orphan in the `bzr-orphans` dir at the root of the
1407
:param tt: The TreeTransform orphaning `trans_id`.
1409
:param orphan_id: The trans id that should be orphaned.
1411
:param parent_id: The orphan parent trans id.
1413
# Add the orphan dir if it doesn't exist
1414
orphan_dir_basename = 'bzr-orphans'
1415
od_id = tt.trans_id_tree_path(orphan_dir_basename)
1416
if tt.final_kind(od_id) is None:
1417
tt.create_directory(od_id)
1418
parent_path = tt._tree_id_paths[parent_id]
1419
# Find a name that doesn't exist yet in the orphan dir
1420
actual_name = tt.final_name(orphan_id)
1421
new_name = tt._available_backup_name(actual_name, od_id)
1422
tt.adjust_path(new_name, od_id, orphan_id)
1423
trace.warning('%s has been orphaned in %s'
1424
% (joinpath(parent_path, actual_name), orphan_dir_basename))
1427
def refuse_orphan(tt, orphan_id, parent_id):
1428
"""See TreeTransformBase.new_orphan.
1430
This refuses to create orphan, letting the caller handle the conflict.
1432
raise OrphaningForbidden('never')
1435
orphaning_registry = registry.Registry()
1436
orphaning_registry.register(
1437
'conflict', refuse_orphan,
1438
'Leave orphans in place and create a conflict on the directory.')
1439
orphaning_registry.register(
1440
'move', move_orphan,
1441
'Move orphans into the bzr-orphans directory.')
1442
orphaning_registry._set_default_key('conflict')
1445
class TreeTransform(DiskTreeTransform):
1446
"""Represent a tree transformation.
1448
This object is designed to support incremental generation of the transform,
1451
However, it gives optimum performance when parent directories are created
1452
before their contents. The transform is then able to put child files
1453
directly in their parent directory, avoiding later renames.
1455
It is easy to produce malformed transforms, but they are generally
1456
harmless. Attempting to apply a malformed transform will cause an
1457
exception to be raised before any modifications are made to the tree.
1459
Many kinds of malformed transforms can be corrected with the
1460
resolve_conflicts function. The remaining ones indicate programming error,
1461
such as trying to create a file with no path.
1463
Two sets of file creation methods are supplied. Convenience methods are:
1468
These are composed of the low-level methods:
1470
* create_file or create_directory or create_symlink
1474
Transform/Transaction ids
1475
-------------------------
1476
trans_ids are temporary ids assigned to all files involved in a transform.
1477
It's possible, even common, that not all files in the Tree have trans_ids.
1479
trans_ids are used because filenames and file_ids are not good enough
1480
identifiers; filenames change, and not all files have file_ids. File-ids
1481
are also associated with trans-ids, so that moving a file moves its
1484
trans_ids are only valid for the TreeTransform that generated them.
1488
Limbo is a temporary directory use to hold new versions of files.
1489
Files are added to limbo by create_file, create_directory, create_symlink,
1490
and their convenience variants (new_*). Files may be removed from limbo
1491
using cancel_creation. Files are renamed from limbo into their final
1492
location as part of TreeTransform.apply
1494
Limbo must be cleaned up, by either calling TreeTransform.apply or
1495
calling TreeTransform.finalize.
1497
Files are placed into limbo inside their parent directories, where
1498
possible. This reduces subsequent renames, and makes operations involving
1499
lots of files faster. This optimization is only possible if the parent
1500
directory is created *before* creating any of its children, so avoid
1501
creating children before parents, where possible.
1505
This temporary directory is used by _FileMover for storing files that are
1506
about to be deleted. In case of rollback, the files will be restored.
1507
FileMover does not delete files until it is sure that a rollback will not
1510
def __init__(self, tree, pb=None):
1511
"""Note: a tree_write lock is taken on the tree.
1513
Use TreeTransform.finalize() to release the lock (can be omitted if
1514
TreeTransform.apply() called).
1516
tree.lock_tree_write()
1519
limbodir = urlutils.local_path_from_url(
1520
tree._transport.abspath('limbo'))
1524
if e.errno == errno.EEXIST:
1525
raise ExistingLimbo(limbodir)
1526
deletiondir = urlutils.local_path_from_url(
1527
tree._transport.abspath('pending-deletion'))
1529
os.mkdir(deletiondir)
1531
if e.errno == errno.EEXIST:
1532
raise errors.ExistingPendingDeletion(deletiondir)
1537
# Cache of realpath results, to speed up canonical_path
1538
self._realpaths = {}
1539
# Cache of relpath results, to speed up canonical_path
1541
DiskTreeTransform.__init__(self, tree, limbodir, pb,
1542
tree.case_sensitive)
1543
self._deletiondir = deletiondir
1545
def canonical_path(self, path):
1546
"""Get the canonical tree-relative path"""
1547
# don't follow final symlinks
1548
abs = self._tree.abspath(path)
1549
if abs in self._relpaths:
1550
return self._relpaths[abs]
1551
dirname, basename = os.path.split(abs)
1552
if dirname not in self._realpaths:
1553
self._realpaths[dirname] = os.path.realpath(dirname)
1554
dirname = self._realpaths[dirname]
1555
abs = pathjoin(dirname, basename)
1556
if dirname in self._relpaths:
1557
relpath = pathjoin(self._relpaths[dirname], basename)
1558
relpath = relpath.rstrip('/\\')
1560
relpath = self._tree.relpath(abs)
1561
self._relpaths[abs] = relpath
1564
def tree_kind(self, trans_id):
1565
"""Determine the file kind in the working tree.
1567
:returns: The file kind or None if the file does not exist
1569
path = self._tree_id_paths.get(trans_id)
1573
return file_kind(self._tree.abspath(path))
1574
except errors.NoSuchFile:
1577
def _set_mode(self, trans_id, mode_id, typefunc):
1578
"""Set the mode of new file contents.
1579
The mode_id is the existing file to get the mode from (often the same
1580
as trans_id). The operation is only performed if there's a mode match
1581
according to typefunc.
1586
old_path = self._tree_id_paths[mode_id]
1590
mode = os.stat(self._tree.abspath(old_path)).st_mode
1592
if e.errno in (errno.ENOENT, errno.ENOTDIR):
1593
# Either old_path doesn't exist, or the parent of the
1594
# target is not a directory (but will be one eventually)
1595
# Either way, we know it doesn't exist *right now*
1596
# See also bug #248448
1601
os.chmod(self._limbo_name(trans_id), mode)
1603
def iter_tree_children(self, parent_id):
1604
"""Iterate through the entry's tree children, if any"""
1606
path = self._tree_id_paths[parent_id]
1610
children = os.listdir(self._tree.abspath(path))
1612
if not (osutils._is_error_enotdir(e)
1613
or e.errno in (errno.ENOENT, errno.ESRCH)):
1617
for child in children:
1618
childpath = joinpath(path, child)
1619
if self._tree.is_control_filename(childpath):
1621
yield self.trans_id_tree_path(childpath)
1623
def _generate_limbo_path(self, trans_id):
1624
"""Generate a limbo path using the final path if possible.
1626
This optimizes the performance of applying the tree transform by
1627
avoiding renames. These renames can be avoided only when the parent
1628
directory is already scheduled for creation.
1630
If the final path cannot be used, falls back to using the trans_id as
1633
parent = self._new_parent.get(trans_id)
1634
# if the parent directory is already in limbo (e.g. when building a
1635
# tree), choose a limbo name inside the parent, to reduce further
1637
use_direct_path = False
1638
if self._new_contents.get(parent) == 'directory':
1639
filename = self._new_name.get(trans_id)
1640
if filename is not None:
1641
if parent not in self._limbo_children:
1642
self._limbo_children[parent] = set()
1643
self._limbo_children_names[parent] = {}
1644
use_direct_path = True
1645
# the direct path can only be used if no other file has
1646
# already taken this pathname, i.e. if the name is unused, or
1647
# if it is already associated with this trans_id.
1648
elif self._case_sensitive_target:
1649
if (self._limbo_children_names[parent].get(filename)
1650
in (trans_id, None)):
1651
use_direct_path = True
1653
for l_filename, l_trans_id in\
1654
self._limbo_children_names[parent].iteritems():
1655
if l_trans_id == trans_id:
1657
if l_filename.lower() == filename.lower():
1660
use_direct_path = True
1662
if not use_direct_path:
1663
return DiskTreeTransform._generate_limbo_path(self, trans_id)
1665
limbo_name = pathjoin(self._limbo_files[parent], filename)
1666
self._limbo_children[parent].add(trans_id)
1667
self._limbo_children_names[parent][filename] = trans_id
1671
def apply(self, no_conflicts=False, precomputed_delta=None, _mover=None):
1672
"""Apply all changes to the inventory and filesystem.
1674
If filesystem or inventory conflicts are present, MalformedTransform
1677
If apply succeeds, finalize is not necessary.
1679
:param no_conflicts: if True, the caller guarantees there are no
1680
conflicts, so no check is made.
1681
:param precomputed_delta: An inventory delta to use instead of
1683
:param _mover: Supply an alternate FileMover, for testing
1685
if not no_conflicts:
1686
self._check_malformed()
1687
child_pb = ui.ui_factory.nested_progress_bar()
1689
if precomputed_delta is None:
1690
child_pb.update('Apply phase', 0, 2)
1691
inventory_delta = self._generate_inventory_delta()
1694
inventory_delta = precomputed_delta
1697
mover = _FileMover()
1701
child_pb.update('Apply phase', 0 + offset, 2 + offset)
1702
self._apply_removals(mover)
1703
child_pb.update('Apply phase', 1 + offset, 2 + offset)
1704
modified_paths = self._apply_insertions(mover)
1709
mover.apply_deletions()
1712
self._tree.apply_inventory_delta(inventory_delta)
1713
self._apply_observed_sha1s()
1716
return _TransformResults(modified_paths, self.rename_count)
1718
def _generate_inventory_delta(self):
1719
"""Generate an inventory delta for the current transform."""
1720
inventory_delta = []
1721
child_pb = ui.ui_factory.nested_progress_bar()
1722
new_paths = self._inventory_altered()
1723
total_entries = len(new_paths) + len(self._removed_id)
1725
for num, trans_id in enumerate(self._removed_id):
1727
child_pb.update('removing file', num, total_entries)
1728
if trans_id == self._new_root:
1729
file_id = self._tree.get_root_id()
1731
file_id = self.tree_file_id(trans_id)
1732
# File-id isn't really being deleted, just moved
1733
if file_id in self._r_new_id:
1735
path = self._tree_id_paths[trans_id]
1736
inventory_delta.append((path, None, file_id, None))
1737
new_path_file_ids = dict((t, self.final_file_id(t)) for p, t in
1739
entries = self._tree.iter_entries_by_dir(
1740
new_path_file_ids.values())
1741
old_paths = dict((e.file_id, p) for p, e in entries)
1743
for num, (path, trans_id) in enumerate(new_paths):
1745
child_pb.update('adding file',
1746
num + len(self._removed_id), total_entries)
1747
file_id = new_path_file_ids[trans_id]
1751
kind = self.final_kind(trans_id)
1753
kind = self._tree.stored_kind(file_id)
1754
parent_trans_id = self.final_parent(trans_id)
1755
parent_file_id = new_path_file_ids.get(parent_trans_id)
1756
if parent_file_id is None:
1757
parent_file_id = self.final_file_id(parent_trans_id)
1758
if trans_id in self._new_reference_revision:
1759
new_entry = inventory.TreeReference(
1761
self._new_name[trans_id],
1762
self.final_file_id(self._new_parent[trans_id]),
1763
None, self._new_reference_revision[trans_id])
1765
new_entry = inventory.make_entry(kind,
1766
self.final_name(trans_id),
1767
parent_file_id, file_id)
1768
old_path = old_paths.get(new_entry.file_id)
1769
new_executability = self._new_executability.get(trans_id)
1770
if new_executability is not None:
1771
new_entry.executable = new_executability
1772
inventory_delta.append(
1773
(old_path, path, new_entry.file_id, new_entry))
1776
return inventory_delta
1778
def _apply_removals(self, mover):
1779
"""Perform tree operations that remove directory/inventory names.
1781
That is, delete files that are to be deleted, and put any files that
1782
need renaming into limbo. This must be done in strict child-to-parent
1785
If inventory_delta is None, no inventory delta generation is performed.
1787
tree_paths = list(self._tree_path_ids.iteritems())
1788
tree_paths.sort(reverse=True)
1789
child_pb = ui.ui_factory.nested_progress_bar()
1791
for num, data in enumerate(tree_paths):
1792
path, trans_id = data
1793
child_pb.update('removing file', num, len(tree_paths))
1794
full_path = self._tree.abspath(path)
1795
if trans_id in self._removed_contents:
1796
delete_path = os.path.join(self._deletiondir, trans_id)
1797
mover.pre_delete(full_path, delete_path)
1798
elif (trans_id in self._new_name
1799
or trans_id in self._new_parent):
1801
mover.rename(full_path, self._limbo_name(trans_id))
1802
except errors.TransformRenameFailed, e:
1803
if e.errno != errno.ENOENT:
1806
self.rename_count += 1
1810
def _apply_insertions(self, mover):
1811
"""Perform tree operations that insert directory/inventory names.
1813
That is, create any files that need to be created, and restore from
1814
limbo any files that needed renaming. This must be done in strict
1815
parent-to-child order.
1817
If inventory_delta is None, no inventory delta is calculated, and
1818
no list of modified paths is returned.
1820
new_paths = self.new_paths(filesystem_only=True)
1822
new_path_file_ids = dict((t, self.final_file_id(t)) for p, t in
1824
child_pb = ui.ui_factory.nested_progress_bar()
1826
for num, (path, trans_id) in enumerate(new_paths):
1828
child_pb.update('adding file', num, len(new_paths))
1829
full_path = self._tree.abspath(path)
1830
if trans_id in self._needs_rename:
1832
mover.rename(self._limbo_name(trans_id), full_path)
1833
except errors.TransformRenameFailed, e:
1834
# We may be renaming a dangling inventory id
1835
if e.errno != errno.ENOENT:
1838
self.rename_count += 1
1839
# TODO: if trans_id in self._observed_sha1s, we should
1840
# re-stat the final target, since ctime will be
1841
# updated by the change.
1842
if (trans_id in self._new_contents or
1843
self.path_changed(trans_id)):
1844
if trans_id in self._new_contents:
1845
modified_paths.append(full_path)
1846
if trans_id in self._new_executability:
1847
self._set_executability(path, trans_id)
1848
if trans_id in self._observed_sha1s:
1849
o_sha1, o_st_val = self._observed_sha1s[trans_id]
1850
st = osutils.lstat(full_path)
1851
self._observed_sha1s[trans_id] = (o_sha1, st)
1854
self._new_contents.clear()
1855
return modified_paths
1857
def _apply_observed_sha1s(self):
1858
"""After we have finished renaming everything, update observed sha1s
1860
This has to be done after self._tree.apply_inventory_delta, otherwise
1861
it doesn't know anything about the files we are updating. Also, we want
1862
to do this as late as possible, so that most entries end up cached.
1864
# TODO: this doesn't update the stat information for directories. So
1865
# the first 'bzr status' will still need to rewrite
1866
# .bzr/checkout/dirstate. However, we at least don't need to
1867
# re-read all of the files.
1868
# TODO: If the operation took a while, we could do a time.sleep(3) here
1869
# to allow the clock to tick over and ensure we won't have any
1870
# problems. (we could observe start time, and finish time, and if
1871
# it is less than eg 10% overhead, add a sleep call.)
1872
paths = FinalPaths(self)
1873
for trans_id, observed in self._observed_sha1s.iteritems():
1874
path = paths.get_path(trans_id)
1875
# We could get the file_id, but dirstate prefers to use the path
1876
# anyway, and it is 'cheaper' to determine.
1877
# file_id = self._new_id[trans_id]
1878
self._tree._observed_sha1(None, path, observed)
1881
class TransformPreview(DiskTreeTransform):
1882
"""A TreeTransform for generating preview trees.
1884
Unlike TreeTransform, this version works when the input tree is a
1885
RevisionTree, rather than a WorkingTree. As a result, it tends to ignore
1886
unversioned files in the input tree.
1889
def __init__(self, tree, pb=None, case_sensitive=True):
1891
limbodir = osutils.mkdtemp(prefix='bzr-limbo-')
1892
DiskTreeTransform.__init__(self, tree, limbodir, pb, case_sensitive)
1894
def canonical_path(self, path):
1897
def tree_kind(self, trans_id):
1898
path = self._tree_id_paths.get(trans_id)
1901
file_id = self._tree.path2id(path)
1903
return self._tree.kind(file_id)
1904
except errors.NoSuchFile:
1907
def _set_mode(self, trans_id, mode_id, typefunc):
1908
"""Set the mode of new file contents.
1909
The mode_id is the existing file to get the mode from (often the same
1910
as trans_id). The operation is only performed if there's a mode match
1911
according to typefunc.
1913
# is it ok to ignore this? probably
1916
def iter_tree_children(self, parent_id):
1917
"""Iterate through the entry's tree children, if any"""
1919
path = self._tree_id_paths[parent_id]
1922
file_id = self.tree_file_id(parent_id)
1925
entry = self._tree.iter_entries_by_dir([file_id]).next()[1]
1926
children = getattr(entry, 'children', {})
1927
for child in children:
1928
childpath = joinpath(path, child)
1929
yield self.trans_id_tree_path(childpath)
1931
def new_orphan(self, trans_id, parent_id):
1932
raise NotImplementedError(self.new_orphan)
1935
class _PreviewTree(tree.InventoryTree):
1936
"""Partial implementation of Tree to support show_diff_trees"""
1938
def __init__(self, transform):
1939
self._transform = transform
1940
self._final_paths = FinalPaths(transform)
1941
self.__by_parent = None
1942
self._parent_ids = []
1943
self._all_children_cache = {}
1944
self._path2trans_id_cache = {}
1945
self._final_name_cache = {}
1946
self._iter_changes_cache = dict((c[0], c) for c in
1947
self._transform.iter_changes())
1949
def _content_change(self, file_id):
1950
"""Return True if the content of this file changed"""
1951
changes = self._iter_changes_cache.get(file_id)
1952
# changes[2] is true if the file content changed. See
1953
# InterTree.iter_changes.
1954
return (changes is not None and changes[2])
1956
def _get_repository(self):
1957
repo = getattr(self._transform._tree, '_repository', None)
1959
repo = self._transform._tree.branch.repository
1962
def _iter_parent_trees(self):
1963
for revision_id in self.get_parent_ids():
1965
yield self.revision_tree(revision_id)
1966
except errors.NoSuchRevisionInTree:
1967
yield self._get_repository().revision_tree(revision_id)
1969
def _get_file_revision(self, file_id, vf, tree_revision):
1970
parent_keys = [(file_id, t.get_file_revision(file_id)) for t in
1971
self._iter_parent_trees()]
1972
vf.add_lines((file_id, tree_revision), parent_keys,
1973
self.get_file_lines(file_id))
1974
repo = self._get_repository()
1975
base_vf = repo.texts
1976
if base_vf not in vf.fallback_versionedfiles:
1977
vf.fallback_versionedfiles.append(base_vf)
1978
return tree_revision
1980
def _stat_limbo_file(self, file_id):
1981
trans_id = self._transform.trans_id_file_id(file_id)
1982
name = self._transform._limbo_name(trans_id)
1983
return os.lstat(name)
1986
def _by_parent(self):
1987
if self.__by_parent is None:
1988
self.__by_parent = self._transform.by_parent()
1989
return self.__by_parent
1991
def _comparison_data(self, entry, path):
1992
kind, size, executable, link_or_sha1 = self.path_content_summary(path)
1993
if kind == 'missing':
1997
file_id = self._transform.final_file_id(self._path2trans_id(path))
1998
executable = self.is_executable(file_id, path)
1999
return kind, executable, None
2001
def is_locked(self):
2004
def lock_read(self):
2005
# Perhaps in theory, this should lock the TreeTransform?
2012
def inventory(self):
2013
"""This Tree does not use inventory as its backing data."""
2014
raise NotImplementedError(_PreviewTree.inventory)
2016
def get_root_id(self):
2017
return self._transform.final_file_id(self._transform.root)
2019
def all_file_ids(self):
2020
tree_ids = set(self._transform._tree.all_file_ids())
2021
tree_ids.difference_update(self._transform.tree_file_id(t)
2022
for t in self._transform._removed_id)
2023
tree_ids.update(self._transform._new_id.values())
2027
return iter(self.all_file_ids())
2029
def _has_id(self, file_id, fallback_check):
2030
if file_id in self._transform._r_new_id:
2032
elif file_id in set([self._transform.tree_file_id(trans_id) for
2033
trans_id in self._transform._removed_id]):
2036
return fallback_check(file_id)
2038
def has_id(self, file_id):
2039
return self._has_id(file_id, self._transform._tree.has_id)
2041
def has_or_had_id(self, file_id):
2042
return self._has_id(file_id, self._transform._tree.has_or_had_id)
2044
def _path2trans_id(self, path):
2045
# We must not use None here, because that is a valid value to store.
2046
trans_id = self._path2trans_id_cache.get(path, object)
2047
if trans_id is not object:
2049
segments = splitpath(path)
2050
cur_parent = self._transform.root
2051
for cur_segment in segments:
2052
for child in self._all_children(cur_parent):
2053
final_name = self._final_name_cache.get(child)
2054
if final_name is None:
2055
final_name = self._transform.final_name(child)
2056
self._final_name_cache[child] = final_name
2057
if final_name == cur_segment:
2061
self._path2trans_id_cache[path] = None
2063
self._path2trans_id_cache[path] = cur_parent
2066
def path2id(self, path):
2067
return self._transform.final_file_id(self._path2trans_id(path))
2069
def id2path(self, file_id):
2070
trans_id = self._transform.trans_id_file_id(file_id)
2072
return self._final_paths._determine_path(trans_id)
2074
raise errors.NoSuchId(self, file_id)
2076
def _all_children(self, trans_id):
2077
children = self._all_children_cache.get(trans_id)
2078
if children is not None:
2080
children = set(self._transform.iter_tree_children(trans_id))
2081
# children in the _new_parent set are provided by _by_parent.
2082
children.difference_update(self._transform._new_parent.keys())
2083
children.update(self._by_parent.get(trans_id, []))
2084
self._all_children_cache[trans_id] = children
2087
def iter_children(self, file_id):
2088
trans_id = self._transform.trans_id_file_id(file_id)
2089
for child_trans_id in self._all_children(trans_id):
2090
yield self._transform.final_file_id(child_trans_id)
2093
possible_extras = set(self._transform.trans_id_tree_path(p) for p
2094
in self._transform._tree.extras())
2095
possible_extras.update(self._transform._new_contents)
2096
possible_extras.update(self._transform._removed_id)
2097
for trans_id in possible_extras:
2098
if self._transform.final_file_id(trans_id) is None:
2099
yield self._final_paths._determine_path(trans_id)
2101
def _make_inv_entries(self, ordered_entries, specific_file_ids=None,
2102
yield_parents=False):
2103
for trans_id, parent_file_id in ordered_entries:
2104
file_id = self._transform.final_file_id(trans_id)
2107
if (specific_file_ids is not None
2108
and file_id not in specific_file_ids):
2110
kind = self._transform.final_kind(trans_id)
2112
kind = self._transform._tree.stored_kind(file_id)
2113
new_entry = inventory.make_entry(
2115
self._transform.final_name(trans_id),
2116
parent_file_id, file_id)
2117
yield new_entry, trans_id
2119
def _list_files_by_dir(self):
2120
todo = [ROOT_PARENT]
2122
while len(todo) > 0:
2124
parent_file_id = self._transform.final_file_id(parent)
2125
children = list(self._all_children(parent))
2126
paths = dict(zip(children, self._final_paths.get_paths(children)))
2127
children.sort(key=paths.get)
2128
todo.extend(reversed(children))
2129
for trans_id in children:
2130
ordered_ids.append((trans_id, parent_file_id))
2133
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
2134
# This may not be a maximally efficient implementation, but it is
2135
# reasonably straightforward. An implementation that grafts the
2136
# TreeTransform changes onto the tree's iter_entries_by_dir results
2137
# might be more efficient, but requires tricky inferences about stack
2139
ordered_ids = self._list_files_by_dir()
2140
for entry, trans_id in self._make_inv_entries(ordered_ids,
2141
specific_file_ids, yield_parents=yield_parents):
2142
yield unicode(self._final_paths.get_path(trans_id)), entry
2144
def _iter_entries_for_dir(self, dir_path):
2145
"""Return path, entry for items in a directory without recursing down."""
2146
dir_file_id = self.path2id(dir_path)
2148
for file_id in self.iter_children(dir_file_id):
2149
trans_id = self._transform.trans_id_file_id(file_id)
2150
ordered_ids.append((trans_id, file_id))
2151
for entry, trans_id in self._make_inv_entries(ordered_ids):
2152
yield unicode(self._final_paths.get_path(trans_id)), entry
2154
def list_files(self, include_root=False, from_dir=None, recursive=True):
2155
"""See WorkingTree.list_files."""
2156
# XXX This should behave like WorkingTree.list_files, but is really
2157
# more like RevisionTree.list_files.
2161
prefix = from_dir + '/'
2162
entries = self.iter_entries_by_dir()
2163
for path, entry in entries:
2164
if entry.name == '' and not include_root:
2167
if not path.startswith(prefix):
2169
path = path[len(prefix):]
2170
yield path, 'V', entry.kind, entry.file_id, entry
2172
if from_dir is None and include_root is True:
2173
root_entry = inventory.make_entry('directory', '',
2174
ROOT_PARENT, self.get_root_id())
2175
yield '', 'V', 'directory', root_entry.file_id, root_entry
2176
entries = self._iter_entries_for_dir(from_dir or '')
2177
for path, entry in entries:
2178
yield path, 'V', entry.kind, entry.file_id, entry
2180
def kind(self, file_id):
2181
trans_id = self._transform.trans_id_file_id(file_id)
2182
return self._transform.final_kind(trans_id)
2184
def stored_kind(self, file_id):
2185
trans_id = self._transform.trans_id_file_id(file_id)
2187
return self._transform._new_contents[trans_id]
2189
return self._transform._tree.stored_kind(file_id)
2191
def get_file_mtime(self, file_id, path=None):
2192
"""See Tree.get_file_mtime"""
2193
if not self._content_change(file_id):
2194
return self._transform._tree.get_file_mtime(file_id)
2195
return self._stat_limbo_file(file_id).st_mtime
2197
def _file_size(self, entry, stat_value):
2198
return self.get_file_size(entry.file_id)
2200
def get_file_size(self, file_id):
2201
"""See Tree.get_file_size"""
2202
if self.kind(file_id) == 'file':
2203
return self._transform._tree.get_file_size(file_id)
2207
def get_file_sha1(self, file_id, path=None, stat_value=None):
2208
trans_id = self._transform.trans_id_file_id(file_id)
2209
kind = self._transform._new_contents.get(trans_id)
2211
return self._transform._tree.get_file_sha1(file_id)
2213
fileobj = self.get_file(file_id)
2215
return sha_file(fileobj)
2219
def is_executable(self, file_id, path=None):
2222
trans_id = self._transform.trans_id_file_id(file_id)
2224
return self._transform._new_executability[trans_id]
2227
return self._transform._tree.is_executable(file_id, path)
2229
if e.errno == errno.ENOENT:
2232
except errors.NoSuchId:
2235
def path_content_summary(self, path):
2236
trans_id = self._path2trans_id(path)
2237
tt = self._transform
2238
tree_path = tt._tree_id_paths.get(trans_id)
2239
kind = tt._new_contents.get(trans_id)
2241
if tree_path is None or trans_id in tt._removed_contents:
2242
return 'missing', None, None, None
2243
summary = tt._tree.path_content_summary(tree_path)
2244
kind, size, executable, link_or_sha1 = summary
2247
limbo_name = tt._limbo_name(trans_id)
2248
if trans_id in tt._new_reference_revision:
2249
kind = 'tree-reference'
2251
statval = os.lstat(limbo_name)
2252
size = statval.st_size
2253
if not supports_executable():
2256
executable = statval.st_mode & S_IEXEC
2260
if kind == 'symlink':
2261
link_or_sha1 = os.readlink(limbo_name).decode(osutils._fs_enc)
2262
executable = tt._new_executability.get(trans_id, executable)
2263
return kind, size, executable, link_or_sha1
2265
def iter_changes(self, from_tree, include_unchanged=False,
2266
specific_files=None, pb=None, extra_trees=None,
2267
require_versioned=True, want_unversioned=False):
2268
"""See InterTree.iter_changes.
2270
This has a fast path that is only used when the from_tree matches
2271
the transform tree, and no fancy options are supplied.
2273
if (from_tree is not self._transform._tree or include_unchanged or
2274
specific_files or want_unversioned):
2275
return tree.InterTree(from_tree, self).iter_changes(
2276
include_unchanged=include_unchanged,
2277
specific_files=specific_files,
2279
extra_trees=extra_trees,
2280
require_versioned=require_versioned,
2281
want_unversioned=want_unversioned)
2282
if want_unversioned:
2283
raise ValueError('want_unversioned is not supported')
2284
return self._transform.iter_changes()
2286
def get_file(self, file_id, path=None):
2287
"""See Tree.get_file"""
2288
if not self._content_change(file_id):
2289
return self._transform._tree.get_file(file_id, path)
2290
trans_id = self._transform.trans_id_file_id(file_id)
2291
name = self._transform._limbo_name(trans_id)
2292
return open(name, 'rb')
2294
def get_file_with_stat(self, file_id, path=None):
2295
return self.get_file(file_id, path), None
2297
def annotate_iter(self, file_id,
2298
default_revision=_mod_revision.CURRENT_REVISION):
2299
changes = self._iter_changes_cache.get(file_id)
2303
changed_content, versioned, kind = (changes[2], changes[3],
2307
get_old = (kind[0] == 'file' and versioned[0])
2309
old_annotation = self._transform._tree.annotate_iter(file_id,
2310
default_revision=default_revision)
2314
return old_annotation
2315
if not changed_content:
2316
return old_annotation
2317
# TODO: This is doing something similar to what WT.annotate_iter is
2318
# doing, however it fails slightly because it doesn't know what
2319
# the *other* revision_id is, so it doesn't know how to give the
2320
# other as the origin for some lines, they all get
2321
# 'default_revision'
2322
# It would be nice to be able to use the new Annotator based
2323
# approach, as well.
2324
return annotate.reannotate([old_annotation],
2325
self.get_file(file_id).readlines(),
2328
def get_symlink_target(self, file_id):
2329
"""See Tree.get_symlink_target"""
2330
if not self._content_change(file_id):
2331
return self._transform._tree.get_symlink_target(file_id)
2332
trans_id = self._transform.trans_id_file_id(file_id)
2333
name = self._transform._limbo_name(trans_id)
2334
return osutils.readlink(name)
2336
def walkdirs(self, prefix=''):
2337
pending = [self._transform.root]
2338
while len(pending) > 0:
2339
parent_id = pending.pop()
2342
prefix = prefix.rstrip('/')
2343
parent_path = self._final_paths.get_path(parent_id)
2344
parent_file_id = self._transform.final_file_id(parent_id)
2345
for child_id in self._all_children(parent_id):
2346
path_from_root = self._final_paths.get_path(child_id)
2347
basename = self._transform.final_name(child_id)
2348
file_id = self._transform.final_file_id(child_id)
2349
kind = self._transform.final_kind(child_id)
2350
if kind is not None:
2351
versioned_kind = kind
2354
versioned_kind = self._transform._tree.stored_kind(file_id)
2355
if versioned_kind == 'directory':
2356
subdirs.append(child_id)
2357
children.append((path_from_root, basename, kind, None,
2358
file_id, versioned_kind))
2360
if parent_path.startswith(prefix):
2361
yield (parent_path, parent_file_id), children
2362
pending.extend(sorted(subdirs, key=self._final_paths.get_path,
2365
def get_parent_ids(self):
2366
return self._parent_ids
2368
def set_parent_ids(self, parent_ids):
2369
self._parent_ids = parent_ids
2371
def get_revision_tree(self, revision_id):
2372
return self._transform._tree.get_revision_tree(revision_id)
2375
885
def joinpath(parent, child):
2376
886
"""Join tree-relative paths, handling the tree root specially"""
2377
887
if parent is None or parent == "":
2407
917
self._known_paths[trans_id] = self._determine_path(trans_id)
2408
918
return self._known_paths[trans_id]
2410
def get_paths(self, trans_ids):
2411
return [(self.get_path(t), t) for t in trans_ids]
2415
920
def topology_sorted_ids(tree):
2416
921
"""Determine the topological order of the ids in a tree"""
2417
922
file_ids = list(tree)
2418
923
file_ids.sort(key=tree.id2path)
2422
def build_tree(tree, wt, accelerator_tree=None, hardlink=False,
2423
delta_from_tree=False):
2424
"""Create working tree for a branch, using a TreeTransform.
2426
This function should be used on empty trees, having a tree root at most.
2427
(see merge and revert functionality for working with existing trees)
2429
Existing files are handled like so:
2431
- Existing bzrdirs take precedence over creating new items. They are
2432
created as '%s.diverted' % name.
2433
- Otherwise, if the content on disk matches the content we are building,
2434
it is silently replaced.
2435
- Otherwise, conflict resolution will move the old file to 'oldname.moved'.
2437
:param tree: The tree to convert wt into a copy of
2438
:param wt: The working tree that files will be placed into
2439
:param accelerator_tree: A tree which can be used for retrieving file
2440
contents more quickly than tree itself, i.e. a workingtree. tree
2441
will be used for cases where accelerator_tree's content is different.
2442
:param hardlink: If true, hard-link files to accelerator_tree, where
2443
possible. accelerator_tree must implement abspath, i.e. be a
2445
:param delta_from_tree: If true, build_tree may use the input Tree to
2446
generate the inventory delta.
2448
wt.lock_tree_write()
2452
if accelerator_tree is not None:
2453
accelerator_tree.lock_read()
2455
return _build_tree(tree, wt, accelerator_tree, hardlink,
2458
if accelerator_tree is not None:
2459
accelerator_tree.unlock()
2466
def _build_tree(tree, wt, accelerator_tree, hardlink, delta_from_tree):
2467
"""See build_tree."""
2468
for num, _unused in enumerate(wt.all_file_ids()):
2469
if num > 0: # more than just a root
2470
raise errors.WorkingTreeAlreadyPopulated(base=wt.basedir)
926
def build_tree(tree, wt):
927
"""Create working tree for a branch, using a Transaction."""
2471
928
file_trans_id = {}
2472
top_pb = ui.ui_factory.nested_progress_bar()
929
top_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2473
930
pp = ProgressPhase("Build phase", 2, top_pb)
2474
if tree.inventory.root is not None:
2475
# This is kind of a hack: we should be altering the root
2476
# as part of the regular tree shape diff logic.
2477
# The conditional test here is to avoid doing an
2478
# expensive operation (flush) every time the root id
2479
# is set within the tree, nor setting the root and thus
2480
# marking the tree as dirty, because we use two different
2481
# idioms here: tree interfaces and inventory interfaces.
2482
if wt.get_root_id() != tree.get_root_id():
2483
wt.set_root_id(tree.get_root_id())
2485
931
tt = TreeTransform(wt)
2489
file_trans_id[wt.get_root_id()] = \
2490
tt.trans_id_tree_file_id(wt.get_root_id())
2491
pb = ui.ui_factory.nested_progress_bar()
934
file_trans_id[wt.get_root_id()] = tt.trans_id_tree_file_id(wt.get_root_id())
935
file_ids = topology_sorted_ids(tree)
936
pb = bzrlib.ui.ui_factory.nested_progress_bar()
2493
deferred_contents = []
2495
total = len(tree.inventory)
2497
precomputed_delta = []
2499
precomputed_delta = None
2500
# Check if tree inventory has content. If so, we populate
2501
# existing_files with the directory content. If there are no
2502
# entries we skip populating existing_files as its not used.
2503
# This improves performance and unncessary work on large
2504
# directory trees. (#501307)
2506
existing_files = set()
2507
for dir, files in wt.walkdirs():
2508
existing_files.update(f[0] for f in files)
2509
for num, (tree_path, entry) in \
2510
enumerate(tree.inventory.iter_entries_by_dir()):
2511
pb.update("Building tree", num - len(deferred_contents), total)
938
for num, file_id in enumerate(file_ids):
939
pb.update("Building tree", num, len(file_ids))
940
entry = tree.inventory[file_id]
2512
941
if entry.parent_id is None:
2515
file_id = entry.file_id
2517
precomputed_delta.append((None, tree_path, file_id, entry))
2518
if tree_path in existing_files:
2519
target_path = wt.abspath(tree_path)
2520
kind = file_kind(target_path)
2521
if kind == "directory":
2523
bzrdir.BzrDir.open(target_path)
2524
except errors.NotBranchError:
2528
if (file_id not in divert and
2529
_content_match(tree, entry, file_id, kind,
2531
tt.delete_contents(tt.trans_id_tree_path(tree_path))
2532
if kind == 'directory':
943
if entry.parent_id not in file_trans_id:
944
raise repr(entry.parent_id)
2534
945
parent_id = file_trans_id[entry.parent_id]
2535
if entry.kind == 'file':
2536
# We *almost* replicate new_by_entry, so that we can defer
2537
# getting the file text, and get them all at once.
2538
trans_id = tt.create_path(entry.name, parent_id)
2539
file_trans_id[file_id] = trans_id
2540
tt.version_file(file_id, trans_id)
2541
executable = tree.is_executable(file_id, tree_path)
2543
tt.set_executability(executable, trans_id)
2544
trans_data = (trans_id, tree_path, entry.text_sha1)
2545
deferred_contents.append((file_id, trans_data))
2547
file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
2550
new_trans_id = file_trans_id[file_id]
2551
old_parent = tt.trans_id_tree_path(tree_path)
2552
_reparent_children(tt, old_parent, new_trans_id)
2553
offset = num + 1 - len(deferred_contents)
2554
_create_files(tt, tree, deferred_contents, pb, offset,
2555
accelerator_tree, hardlink)
946
file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
2559
divert_trans = set(file_trans_id[f] for f in divert)
2560
resolver = lambda t, c: resolve_checkout(t, c, divert_trans)
2561
raw_conflicts = resolve_conflicts(tt, pass_func=resolver)
2562
if len(raw_conflicts) > 0:
2563
precomputed_delta = None
2564
conflicts = cook_conflicts(raw_conflicts, tt)
2565
for conflict in conflicts:
2566
trace.warning(conflict)
2568
wt.add_conflicts(conflicts)
2569
except errors.UnsupportedOperation:
2571
result = tt.apply(no_conflicts=True,
2572
precomputed_delta=precomputed_delta)
2575
954
top_pb.finished()
2579
def _create_files(tt, tree, desired_files, pb, offset, accelerator_tree,
2581
total = len(desired_files) + offset
2583
if accelerator_tree is None:
2584
new_desired_files = desired_files
2586
iter = accelerator_tree.iter_changes(tree, include_unchanged=True)
2587
unchanged = [(f, p[1]) for (f, p, c, v, d, n, k, e)
2588
in iter if not (c or e[0] != e[1])]
2589
if accelerator_tree.supports_content_filtering():
2590
unchanged = [(f, p) for (f, p) in unchanged
2591
if not accelerator_tree.iter_search_rules([p]).next()]
2592
unchanged = dict(unchanged)
2593
new_desired_files = []
2595
for file_id, (trans_id, tree_path, text_sha1) in desired_files:
2596
accelerator_path = unchanged.get(file_id)
2597
if accelerator_path is None:
2598
new_desired_files.append((file_id,
2599
(trans_id, tree_path, text_sha1)))
2601
pb.update('Adding file contents', count + offset, total)
2603
tt.create_hardlink(accelerator_tree.abspath(accelerator_path),
2606
contents = accelerator_tree.get_file(file_id, accelerator_path)
2607
if wt.supports_content_filtering():
2608
filters = wt._content_filter_stack(tree_path)
2609
contents = filtered_output_bytes(contents, filters,
2610
ContentFilterContext(tree_path, tree))
2612
tt.create_file(contents, trans_id, sha1=text_sha1)
2616
except AttributeError:
2617
# after filtering, contents may no longer be file-like
2621
for count, ((trans_id, tree_path, text_sha1), contents) in enumerate(
2622
tree.iter_files_bytes(new_desired_files)):
2623
if wt.supports_content_filtering():
2624
filters = wt._content_filter_stack(tree_path)
2625
contents = filtered_output_bytes(contents, filters,
2626
ContentFilterContext(tree_path, tree))
2627
tt.create_file(contents, trans_id, sha1=text_sha1)
2628
pb.update('Adding file contents', count + offset, total)
2631
def _reparent_children(tt, old_parent, new_parent):
2632
for child in tt.iter_tree_children(old_parent):
2633
tt.adjust_path(tt.final_name(child), new_parent, child)
2636
def _reparent_transform_children(tt, old_parent, new_parent):
2637
by_parent = tt.by_parent()
2638
for child in by_parent[old_parent]:
2639
tt.adjust_path(tt.final_name(child), new_parent, child)
2640
return by_parent[old_parent]
2643
def _content_match(tree, entry, file_id, kind, target_path):
2644
if entry.kind != kind:
2646
if entry.kind == "directory":
2648
if entry.kind == "file":
2649
f = file(target_path, 'rb')
2651
if tree.get_file_text(file_id) == f.read():
2655
elif entry.kind == "symlink":
2656
if tree.get_symlink_target(file_id) == os.readlink(target_path):
2661
def resolve_checkout(tt, conflicts, divert):
2662
new_conflicts = set()
2663
for c_type, conflict in ((c[0], c) for c in conflicts):
2664
# Anything but a 'duplicate' would indicate programmer error
2665
if c_type != 'duplicate':
2666
raise AssertionError(c_type)
2667
# Now figure out which is new and which is old
2668
if tt.new_contents(conflict[1]):
2669
new_file = conflict[1]
2670
old_file = conflict[2]
2672
new_file = conflict[2]
2673
old_file = conflict[1]
2675
# We should only get here if the conflict wasn't completely
2677
final_parent = tt.final_parent(old_file)
2678
if new_file in divert:
2679
new_name = tt.final_name(old_file)+'.diverted'
2680
tt.adjust_path(new_name, final_parent, new_file)
2681
new_conflicts.add((c_type, 'Diverted to',
2682
new_file, old_file))
2684
new_name = tt.final_name(old_file)+'.moved'
2685
tt.adjust_path(new_name, final_parent, old_file)
2686
new_conflicts.add((c_type, 'Moved existing file to',
2687
old_file, new_file))
2688
return new_conflicts
2691
956
def new_by_entry(tt, entry, parent_id, tree):
2692
957
"""Create a new file according to its inventory entry"""