323
251
return ROOT_PARENT
324
252
return self.trans_id_tree_path(os.path.dirname(path))
326
def create_file(self, contents, trans_id, mode_id=None):
327
"""Schedule creation of a new file.
331
Contents is an iterator of strings, all of which will be written
332
to the target destination.
334
New file takes the permissions of any existing file with that id,
335
unless mode_id is specified.
337
name = self._limbo_name(trans_id)
341
unique_add(self._new_contents, trans_id, 'file')
343
# Clean up the file, it never got registered so
344
# TreeTransform.finalize() won't clean it up.
349
f.writelines(contents)
352
self._set_mode(trans_id, mode_id, S_ISREG)
354
def _set_mode(self, trans_id, mode_id, typefunc):
355
"""Set the mode of new file contents.
356
The mode_id is the existing file to get the mode from (often the same
357
as trans_id). The operation is only performed if there's a mode match
358
according to typefunc.
363
old_path = self._tree_id_paths[mode_id]
367
mode = os.stat(self._tree.abspath(old_path)).st_mode
369
if e.errno in (errno.ENOENT, errno.ENOTDIR):
370
# Either old_path doesn't exist, or the parent of the
371
# target is not a directory (but will be one eventually)
372
# Either way, we know it doesn't exist *right now*
373
# See also bug #248448
378
os.chmod(self._limbo_name(trans_id), mode)
380
def create_hardlink(self, path, trans_id):
381
"""Schedule creation of a hard link"""
382
name = self._limbo_name(trans_id)
386
if e.errno != errno.EPERM:
388
raise errors.HardLinkNotSupported(path)
390
unique_add(self._new_contents, trans_id, 'file')
392
# Clean up the file, it never got registered so
393
# TreeTransform.finalize() won't clean it up.
397
def create_directory(self, trans_id):
398
"""Schedule creation of a new directory.
400
See also new_directory.
402
os.mkdir(self._limbo_name(trans_id))
403
unique_add(self._new_contents, trans_id, 'directory')
405
def create_symlink(self, target, trans_id):
406
"""Schedule creation of a new symbolic link.
408
target is a bytestring.
409
See also new_symlink.
412
os.symlink(target, self._limbo_name(trans_id))
413
unique_add(self._new_contents, trans_id, 'symlink')
416
path = FinalPaths(self).get_path(trans_id)
419
raise UnableCreateSymlink(path=path)
421
def cancel_creation(self, trans_id):
422
"""Cancel the creation of new file contents."""
423
del self._new_contents[trans_id]
424
children = self._limbo_children.get(trans_id)
425
# if this is a limbo directory with children, move them before removing
427
if children is not None:
428
self._rename_in_limbo(children)
429
del self._limbo_children[trans_id]
430
del self._limbo_children_names[trans_id]
431
delete_any(self._limbo_name(trans_id))
433
254
def delete_contents(self, trans_id):
434
255
"""Schedule the contents of a path entry for deletion"""
435
256
self.tree_kind(trans_id)
1119
865
return _PreviewTree(self)
1122
class TreeTransform(TreeTransformBase):
867
def commit(self, branch, message, merge_parents=None, strict=False):
868
"""Commit the result of this TreeTransform to a branch.
870
:param branch: The branch to commit to.
871
:param message: The message to attach to the commit.
872
:param merge_parents: Additional parents specified by pending merges.
873
:return: The revision_id of the revision committed.
875
self._check_malformed()
877
unversioned = set(self._new_contents).difference(set(self._new_id))
878
for trans_id in unversioned:
879
if self.final_file_id(trans_id) is None:
880
raise errors.StrictCommitFailed()
882
revno, last_rev_id = branch.last_revision_info()
883
if last_rev_id == _mod_revision.NULL_REVISION:
884
if merge_parents is not None:
885
raise ValueError('Cannot supply merge parents for first'
889
parent_ids = [last_rev_id]
890
if merge_parents is not None:
891
parent_ids.extend(merge_parents)
892
if self._tree.get_revision_id() != last_rev_id:
893
raise ValueError('TreeTransform not based on branch basis: %s' %
894
self._tree.get_revision_id())
895
builder = branch.get_commit_builder(parent_ids)
896
preview = self.get_preview_tree()
897
list(builder.record_iter_changes(preview, last_rev_id,
898
self.iter_changes()))
899
builder.finish_inventory()
900
revision_id = builder.commit(message)
901
branch.set_last_revision_info(revno + 1, revision_id)
904
def _text_parent(self, trans_id):
905
file_id = self.tree_file_id(trans_id)
907
if file_id is None or self._tree.kind(file_id) != 'file':
909
except errors.NoSuchFile:
913
def _get_parents_texts(self, trans_id):
914
"""Get texts for compression parents of this file."""
915
file_id = self._text_parent(trans_id)
918
return (self._tree.get_file_text(file_id),)
920
def _get_parents_lines(self, trans_id):
921
"""Get lines for compression parents of this file."""
922
file_id = self._text_parent(trans_id)
925
return (self._tree.get_file_lines(file_id),)
927
def serialize(self, serializer):
928
"""Serialize this TreeTransform.
930
:param serializer: A Serialiser like pack.ContainerSerializer.
932
new_name = dict((k, v.encode('utf-8')) for k, v in
933
self._new_name.items())
934
new_executability = dict((k, int(v)) for k, v in
935
self._new_executability.items())
936
tree_path_ids = dict((k.encode('utf-8'), v)
937
for k, v in self._tree_path_ids.items())
939
'_id_number': self._id_number,
940
'_new_name': new_name,
941
'_new_parent': self._new_parent,
942
'_new_executability': new_executability,
943
'_new_id': self._new_id,
944
'_tree_path_ids': tree_path_ids,
945
'_removed_id': list(self._removed_id),
946
'_removed_contents': list(self._removed_contents),
947
'_non_present_ids': self._non_present_ids,
949
yield serializer.bytes_record(bencode.bencode(attribs),
951
for trans_id, kind in self._new_contents.items():
953
lines = osutils.chunks_to_lines(
954
self._read_file_chunks(trans_id))
955
parents = self._get_parents_lines(trans_id)
956
mpdiff = multiparent.MultiParent.from_lines(lines, parents)
957
content = ''.join(mpdiff.to_patch())
958
if kind == 'directory':
960
if kind == 'symlink':
961
content = self._read_symlink_target(trans_id)
962
yield serializer.bytes_record(content, ((trans_id, kind),))
964
def deserialize(self, records):
965
"""Deserialize a stored TreeTransform.
967
:param records: An iterable of (names, content) tuples, as per
968
pack.ContainerPushParser.
970
names, content = records.next()
971
attribs = bencode.bdecode(content)
972
self._id_number = attribs['_id_number']
973
self._new_name = dict((k, v.decode('utf-8'))
974
for k, v in attribs['_new_name'].items())
975
self._new_parent = attribs['_new_parent']
976
self._new_executability = dict((k, bool(v)) for k, v in
977
attribs['_new_executability'].items())
978
self._new_id = attribs['_new_id']
979
self._r_new_id = dict((v, k) for k, v in self._new_id.items())
980
self._tree_path_ids = {}
981
self._tree_id_paths = {}
982
for bytepath, trans_id in attribs['_tree_path_ids'].items():
983
path = bytepath.decode('utf-8')
984
self._tree_path_ids[path] = trans_id
985
self._tree_id_paths[trans_id] = path
986
self._removed_id = set(attribs['_removed_id'])
987
self._removed_contents = set(attribs['_removed_contents'])
988
self._non_present_ids = attribs['_non_present_ids']
989
for ((trans_id, kind),), content in records:
991
mpdiff = multiparent.MultiParent.from_patch(content)
992
lines = mpdiff.to_lines(self._get_parents_texts(trans_id))
993
self.create_file(lines, trans_id)
994
if kind == 'directory':
995
self.create_directory(trans_id)
996
if kind == 'symlink':
997
self.create_symlink(content.decode('utf-8'), trans_id)
1000
class DiskTreeTransform(TreeTransformBase):
1001
"""Tree transform storing its contents on disk."""
1003
def __init__(self, tree, limbodir, pb=DummyProgress(),
1004
case_sensitive=True):
1006
:param tree: The tree that will be transformed, but not necessarily
1008
:param limbodir: A directory where new files can be stored until
1009
they are installed in their proper places
1010
:param pb: A ProgressBar indicating how much progress is being made
1011
:param case_sensitive: If True, the target of the transform is
1012
case sensitive, not just case preserving.
1014
TreeTransformBase.__init__(self, tree, pb, case_sensitive)
1015
self._limbodir = limbodir
1016
self._deletiondir = None
1017
# A mapping of transform ids to their limbo filename
1018
self._limbo_files = {}
1019
# A mapping of transform ids to a set of the transform ids of children
1020
# that their limbo directory has
1021
self._limbo_children = {}
1022
# Map transform ids to maps of child filename to child transform id
1023
self._limbo_children_names = {}
1024
# List of transform ids that need to be renamed from limbo into place
1025
self._needs_rename = set()
1028
"""Release the working tree lock, if held, clean up limbo dir.
1030
This is required if apply has not been invoked, but can be invoked
1033
if self._tree is None:
1036
entries = [(self._limbo_name(t), t, k) for t, k in
1037
self._new_contents.iteritems()]
1038
entries.sort(reverse=True)
1039
for path, trans_id, kind in entries:
1042
delete_any(self._limbodir)
1044
# We don't especially care *why* the dir is immortal.
1045
raise ImmortalLimbo(self._limbodir)
1047
if self._deletiondir is not None:
1048
delete_any(self._deletiondir)
1050
raise errors.ImmortalPendingDeletion(self._deletiondir)
1052
TreeTransformBase.finalize(self)
1054
def _limbo_name(self, trans_id):
1055
"""Generate the limbo name of a file"""
1056
limbo_name = self._limbo_files.get(trans_id)
1057
if limbo_name is not None:
1059
parent = self._new_parent.get(trans_id)
1060
# if the parent directory is already in limbo (e.g. when building a
1061
# tree), choose a limbo name inside the parent, to reduce further
1063
use_direct_path = False
1064
if self._new_contents.get(parent) == 'directory':
1065
filename = self._new_name.get(trans_id)
1066
if filename is not None:
1067
if parent not in self._limbo_children:
1068
self._limbo_children[parent] = set()
1069
self._limbo_children_names[parent] = {}
1070
use_direct_path = True
1071
# the direct path can only be used if no other file has
1072
# already taken this pathname, i.e. if the name is unused, or
1073
# if it is already associated with this trans_id.
1074
elif self._case_sensitive_target:
1075
if (self._limbo_children_names[parent].get(filename)
1076
in (trans_id, None)):
1077
use_direct_path = True
1079
for l_filename, l_trans_id in\
1080
self._limbo_children_names[parent].iteritems():
1081
if l_trans_id == trans_id:
1083
if l_filename.lower() == filename.lower():
1086
use_direct_path = True
1089
limbo_name = pathjoin(self._limbo_files[parent], filename)
1090
self._limbo_children[parent].add(trans_id)
1091
self._limbo_children_names[parent][filename] = trans_id
1093
limbo_name = pathjoin(self._limbodir, trans_id)
1094
self._needs_rename.add(trans_id)
1095
self._limbo_files[trans_id] = limbo_name
1098
def adjust_path(self, name, parent, trans_id):
1099
previous_parent = self._new_parent.get(trans_id)
1100
previous_name = self._new_name.get(trans_id)
1101
TreeTransformBase.adjust_path(self, name, parent, trans_id)
1102
if (trans_id in self._limbo_files and
1103
trans_id not in self._needs_rename):
1104
self._rename_in_limbo([trans_id])
1105
self._limbo_children[previous_parent].remove(trans_id)
1106
del self._limbo_children_names[previous_parent][previous_name]
1108
def _rename_in_limbo(self, trans_ids):
1109
"""Fix limbo names so that the right final path is produced.
1111
This means we outsmarted ourselves-- we tried to avoid renaming
1112
these files later by creating them with their final names in their
1113
final parents. But now the previous name or parent is no longer
1114
suitable, so we have to rename them.
1116
Even for trans_ids that have no new contents, we must remove their
1117
entries from _limbo_files, because they are now stale.
1119
for trans_id in trans_ids:
1120
old_path = self._limbo_files.pop(trans_id)
1121
if trans_id not in self._new_contents:
1123
new_path = self._limbo_name(trans_id)
1124
os.rename(old_path, new_path)
1126
def create_file(self, contents, trans_id, mode_id=None):
1127
"""Schedule creation of a new file.
1131
Contents is an iterator of strings, all of which will be written
1132
to the target destination.
1134
New file takes the permissions of any existing file with that id,
1135
unless mode_id is specified.
1137
name = self._limbo_name(trans_id)
1138
f = open(name, 'wb')
1141
unique_add(self._new_contents, trans_id, 'file')
1143
# Clean up the file, it never got registered so
1144
# TreeTransform.finalize() won't clean it up.
1149
f.writelines(contents)
1152
self._set_mode(trans_id, mode_id, S_ISREG)
1154
def _read_file_chunks(self, trans_id):
1155
cur_file = open(self._limbo_name(trans_id), 'rb')
1157
return cur_file.readlines()
1161
def _read_symlink_target(self, trans_id):
1162
return os.readlink(self._limbo_name(trans_id))
1164
def create_hardlink(self, path, trans_id):
1165
"""Schedule creation of a hard link"""
1166
name = self._limbo_name(trans_id)
1170
if e.errno != errno.EPERM:
1172
raise errors.HardLinkNotSupported(path)
1174
unique_add(self._new_contents, trans_id, 'file')
1176
# Clean up the file, it never got registered so
1177
# TreeTransform.finalize() won't clean it up.
1181
def create_directory(self, trans_id):
1182
"""Schedule creation of a new directory.
1184
See also new_directory.
1186
os.mkdir(self._limbo_name(trans_id))
1187
unique_add(self._new_contents, trans_id, 'directory')
1189
def create_symlink(self, target, trans_id):
1190
"""Schedule creation of a new symbolic link.
1192
target is a bytestring.
1193
See also new_symlink.
1196
os.symlink(target, self._limbo_name(trans_id))
1197
unique_add(self._new_contents, trans_id, 'symlink')
1200
path = FinalPaths(self).get_path(trans_id)
1203
raise UnableCreateSymlink(path=path)
1205
def cancel_creation(self, trans_id):
1206
"""Cancel the creation of new file contents."""
1207
del self._new_contents[trans_id]
1208
children = self._limbo_children.get(trans_id)
1209
# if this is a limbo directory with children, move them before removing
1211
if children is not None:
1212
self._rename_in_limbo(children)
1213
del self._limbo_children[trans_id]
1214
del self._limbo_children_names[trans_id]
1215
delete_any(self._limbo_name(trans_id))
1218
class TreeTransform(DiskTreeTransform):
1123
1219
"""Represent a tree transformation.
1125
1221
This object is designed to support incremental generation of the transform,
1214
TreeTransformBase.__init__(self, tree, limbodir, pb,
1310
# Cache of realpath results, to speed up canonical_path
1311
self._realpaths = {}
1312
# Cache of relpath results, to speed up canonical_path
1314
DiskTreeTransform.__init__(self, tree, limbodir, pb,
1215
1315
tree.case_sensitive)
1216
1316
self._deletiondir = deletiondir
1318
def canonical_path(self, path):
1319
"""Get the canonical tree-relative path"""
1320
# don't follow final symlinks
1321
abs = self._tree.abspath(path)
1322
if abs in self._relpaths:
1323
return self._relpaths[abs]
1324
dirname, basename = os.path.split(abs)
1325
if dirname not in self._realpaths:
1326
self._realpaths[dirname] = os.path.realpath(dirname)
1327
dirname = self._realpaths[dirname]
1328
abs = pathjoin(dirname, basename)
1329
if dirname in self._relpaths:
1330
relpath = pathjoin(self._relpaths[dirname], basename)
1331
relpath = relpath.rstrip('/\\')
1333
relpath = self._tree.relpath(abs)
1334
self._relpaths[abs] = relpath
1337
def tree_kind(self, trans_id):
1338
"""Determine the file kind in the working tree.
1340
Raises NoSuchFile if the file does not exist
1342
path = self._tree_id_paths.get(trans_id)
1344
raise NoSuchFile(None)
1346
return file_kind(self._tree.abspath(path))
1348
if e.errno != errno.ENOENT:
1351
raise NoSuchFile(path)
1353
def _set_mode(self, trans_id, mode_id, typefunc):
1354
"""Set the mode of new file contents.
1355
The mode_id is the existing file to get the mode from (often the same
1356
as trans_id). The operation is only performed if there's a mode match
1357
according to typefunc.
1362
old_path = self._tree_id_paths[mode_id]
1366
mode = os.stat(self._tree.abspath(old_path)).st_mode
1368
if e.errno in (errno.ENOENT, errno.ENOTDIR):
1369
# Either old_path doesn't exist, or the parent of the
1370
# target is not a directory (but will be one eventually)
1371
# Either way, we know it doesn't exist *right now*
1372
# See also bug #248448
1377
os.chmod(self._limbo_name(trans_id), mode)
1379
def iter_tree_children(self, parent_id):
1380
"""Iterate through the entry's tree children, if any"""
1382
path = self._tree_id_paths[parent_id]
1386
children = os.listdir(self._tree.abspath(path))
1388
if not (osutils._is_error_enotdir(e)
1389
or e.errno in (errno.ENOENT, errno.ESRCH)):
1393
for child in children:
1394
childpath = joinpath(path, child)
1395
if self._tree.is_control_filename(childpath):
1397
yield self.trans_id_tree_path(childpath)
1218
1399
def apply(self, no_conflicts=False, precomputed_delta=None, _mover=None):
1219
1400
"""Apply all changes to the inventory and filesystem.