34
32
revision as _mod_revision,
38
35
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
39
ReusingTransform, CantMoveRoot,
36
ReusingTransform, NotVersionedError, CantMoveRoot,
40
37
ExistingLimbo, ImmortalLimbo, NoFinalPath,
41
38
UnableCreateSymlink)
42
39
from bzrlib.filters import filtered_output_bytes, ContentFilterContext
81
78
class TreeTransformBase(object):
82
79
"""The base class for TreeTransform and its kin."""
84
def __init__(self, tree, pb=None,
81
def __init__(self, tree, pb=DummyProgress(),
85
82
case_sensitive=True):
88
85
:param tree: The tree that will be transformed, but not necessarily
87
:param pb: A ProgressBar indicating how much progress is being made
91
88
:param case_sensitive: If True, the target of the transform is
92
89
case sensitive, not just case preserving.
165
162
def adjust_path(self, name, parent, trans_id):
166
163
"""Change the path that is assigned to a transaction id."""
168
raise ValueError("Parent trans-id may not be None")
169
164
if trans_id == self._new_root:
170
165
raise CantMoveRoot
171
166
self._new_name[trans_id] = name
172
167
self._new_parent[trans_id] = parent
168
if parent == ROOT_PARENT:
169
if self._new_root is not None:
170
raise ValueError("Cannot have multiple roots.")
171
self._new_root = trans_id
174
173
def adjust_root_path(self, name, parent):
175
174
"""Emulate moving the root by moving all children, instead.
203
202
self.version_file(old_root_file_id, old_root)
204
203
self.unversion_file(self._new_root)
206
def fixup_new_roots(self):
207
"""Reinterpret requests to change the root directory
209
Instead of creating a root directory, or moving an existing directory,
210
all the attributes and children of the new root are applied to the
211
existing root directory.
213
This means that the old root trans-id becomes obsolete, so it is
214
recommended only to invoke this after the root trans-id has become
217
new_roots = [k for k, v in self._new_parent.iteritems() if v is
219
if len(new_roots) < 1:
221
if len(new_roots) != 1:
222
raise ValueError('A tree cannot have two roots!')
223
if self._new_root is None:
224
self._new_root = new_roots[0]
226
old_new_root = new_roots[0]
227
# TODO: What to do if a old_new_root is present, but self._new_root is
228
# not listed as being removed? This code explicitly unversions
229
# the old root and versions it with the new file_id. Though that
230
# seems like an incomplete delta
232
# unversion the new root's directory.
233
file_id = self.final_file_id(old_new_root)
234
if old_new_root in self._new_id:
235
self.cancel_versioning(old_new_root)
237
self.unversion_file(old_new_root)
238
# if, at this stage, root still has an old file_id, zap it so we can
239
# stick a new one in.
240
if (self.tree_file_id(self._new_root) is not None and
241
self._new_root not in self._removed_id):
242
self.unversion_file(self._new_root)
243
self.version_file(file_id, self._new_root)
245
# Now move children of new root into old root directory.
246
# Ensure all children are registered with the transaction, but don't
247
# use directly-- some tree children have new parents
248
list(self.iter_tree_children(old_new_root))
249
# Move all children of new root into old root directory.
250
for child in self.by_parent().get(old_new_root, []):
251
self.adjust_path(self.final_name(child), self._new_root, child)
253
# Ensure old_new_root has no directory.
254
if old_new_root in self._new_contents:
255
self.cancel_creation(old_new_root)
257
self.delete_contents(old_new_root)
259
# prevent deletion of root directory.
260
if self._new_root in self._removed_contents:
261
self.cancel_deletion(self._new_root)
263
# destroy path info for old_new_root.
264
del self._new_parent[old_new_root]
265
del self._new_name[old_new_root]
267
205
def trans_id_tree_file_id(self, inventory_id):
268
206
"""Determine the transaction id of a working tree file.
923
854
def get_preview_tree(self):
924
855
"""Return a tree representing the result of the transform.
926
The tree is a snapshot, and altering the TreeTransform will invalidate
857
This tree only supports the subset of Tree functionality required
858
by show_diff_trees. It must only be compared to tt._tree.
929
860
return _PreviewTree(self)
931
def commit(self, branch, message, merge_parents=None, strict=False,
932
timestamp=None, timezone=None, committer=None, authors=None,
933
revprops=None, revision_id=None):
934
"""Commit the result of this TreeTransform to a branch.
936
:param branch: The branch to commit to.
937
:param message: The message to attach to the commit.
938
:param merge_parents: Additional parent revision-ids specified by
940
:param strict: If True, abort the commit if there are unversioned
942
:param timestamp: if not None, seconds-since-epoch for the time and
943
date. (May be a float.)
944
:param timezone: Optional timezone for timestamp, as an offset in
946
:param committer: Optional committer in email-id format.
947
(e.g. "J Random Hacker <jrandom@example.com>")
948
:param authors: Optional list of authors in email-id format.
949
:param revprops: Optional dictionary of revision properties.
950
:param revision_id: Optional revision id. (Specifying a revision-id
951
may reduce performance for some non-native formats.)
952
:return: The revision_id of the revision committed.
954
self._check_malformed()
956
unversioned = set(self._new_contents).difference(set(self._new_id))
957
for trans_id in unversioned:
958
if self.final_file_id(trans_id) is None:
959
raise errors.StrictCommitFailed()
961
revno, last_rev_id = branch.last_revision_info()
962
if last_rev_id == _mod_revision.NULL_REVISION:
963
if merge_parents is not None:
964
raise ValueError('Cannot supply merge parents for first'
968
parent_ids = [last_rev_id]
969
if merge_parents is not None:
970
parent_ids.extend(merge_parents)
971
if self._tree.get_revision_id() != last_rev_id:
972
raise ValueError('TreeTransform not based on branch basis: %s' %
973
self._tree.get_revision_id())
974
revprops = commit.Commit.update_revprops(revprops, branch, authors)
975
builder = branch.get_commit_builder(parent_ids,
980
revision_id=revision_id)
981
preview = self.get_preview_tree()
982
list(builder.record_iter_changes(preview, last_rev_id,
983
self.iter_changes()))
984
builder.finish_inventory()
985
revision_id = builder.commit(message)
986
branch.set_last_revision_info(revno + 1, revision_id)
989
862
def _text_parent(self, trans_id):
990
863
file_id = self.tree_file_id(trans_id)
1085
958
class DiskTreeTransform(TreeTransformBase):
1086
959
"""Tree transform storing its contents on disk."""
1088
def __init__(self, tree, limbodir, pb=None,
961
def __init__(self, tree, limbodir, pb=DummyProgress(),
1089
962
case_sensitive=True):
1091
964
:param tree: The tree that will be transformed, but not necessarily
1092
965
the output tree.
1093
966
:param limbodir: A directory where new files can be stored until
1094
967
they are installed in their proper places
968
:param pb: A ProgressBar indicating how much progress is being made
1096
969
:param case_sensitive: If True, the target of the transform is
1097
970
case sensitive, not just case preserving.
1123
995
self._new_contents.iteritems()]
1124
996
entries.sort(reverse=True)
1125
997
for path, trans_id, kind in entries:
998
if kind == "directory":
1128
delete_any(self._limbodir)
1003
os.rmdir(self._limbodir)
1129
1004
except OSError:
1130
1005
# We don't especially care *why* the dir is immortal.
1131
1006
raise ImmortalLimbo(self._limbodir)
1133
1008
if self._deletiondir is not None:
1134
delete_any(self._deletiondir)
1009
os.rmdir(self._deletiondir)
1135
1010
except OSError:
1136
1011
raise errors.ImmortalPendingDeletion(self._deletiondir)
1140
1015
def _limbo_name(self, trans_id):
1141
1016
"""Generate the limbo name of a file"""
1142
1017
limbo_name = self._limbo_files.get(trans_id)
1143
if limbo_name is None:
1144
limbo_name = self._generate_limbo_path(trans_id)
1145
self._limbo_files[trans_id] = limbo_name
1018
if limbo_name is not None:
1020
parent = self._new_parent.get(trans_id)
1021
# if the parent directory is already in limbo (e.g. when building a
1022
# tree), choose a limbo name inside the parent, to reduce further
1024
use_direct_path = False
1025
if self._new_contents.get(parent) == 'directory':
1026
filename = self._new_name.get(trans_id)
1027
if filename is not None:
1028
if parent not in self._limbo_children:
1029
self._limbo_children[parent] = set()
1030
self._limbo_children_names[parent] = {}
1031
use_direct_path = True
1032
# the direct path can only be used if no other file has
1033
# already taken this pathname, i.e. if the name is unused, or
1034
# if it is already associated with this trans_id.
1035
elif self._case_sensitive_target:
1036
if (self._limbo_children_names[parent].get(filename)
1037
in (trans_id, None)):
1038
use_direct_path = True
1040
for l_filename, l_trans_id in\
1041
self._limbo_children_names[parent].iteritems():
1042
if l_trans_id == trans_id:
1044
if l_filename.lower() == filename.lower():
1047
use_direct_path = True
1050
limbo_name = pathjoin(self._limbo_files[parent], filename)
1051
self._limbo_children[parent].add(trans_id)
1052
self._limbo_children_names[parent][filename] = trans_id
1054
limbo_name = pathjoin(self._limbodir, trans_id)
1055
self._needs_rename.add(trans_id)
1056
self._limbo_files[trans_id] = limbo_name
1146
1057
return limbo_name
1148
def _generate_limbo_path(self, trans_id):
1149
"""Generate a limbo path using the trans_id as the relative path.
1151
This is suitable as a fallback, and when the transform should not be
1152
sensitive to the path encoding of the limbo directory.
1154
self._needs_rename.add(trans_id)
1155
return pathjoin(self._limbodir, trans_id)
1157
1059
def adjust_path(self, name, parent, trans_id):
1158
1060
previous_parent = self._new_parent.get(trans_id)
1159
1061
previous_name = self._new_name.get(trans_id)
1161
1063
if (trans_id in self._limbo_files and
1162
1064
trans_id not in self._needs_rename):
1163
1065
self._rename_in_limbo([trans_id])
1164
if previous_parent != parent:
1165
self._limbo_children[previous_parent].remove(trans_id)
1166
if previous_parent != parent or previous_name != name:
1167
del self._limbo_children_names[previous_parent][previous_name]
1066
self._limbo_children[previous_parent].remove(trans_id)
1067
del self._limbo_children_names[previous_parent][previous_name]
1169
1069
def _rename_in_limbo(self, trans_ids):
1170
1070
"""Fix limbo names so that the right final path is produced.
1182
1082
if trans_id not in self._new_contents:
1184
1084
new_path = self._limbo_name(trans_id)
1185
osutils.rename(old_path, new_path)
1186
for descendant in self._limbo_descendants(trans_id):
1187
desc_path = self._limbo_files[descendant]
1188
desc_path = new_path + desc_path[len(old_path):]
1189
self._limbo_files[descendant] = desc_path
1191
def _limbo_descendants(self, trans_id):
1192
"""Return the set of trans_ids whose limbo paths descend from this."""
1193
descendants = set(self._limbo_children.get(trans_id, []))
1194
for descendant in list(descendants):
1195
descendants.update(self._limbo_descendants(descendant))
1085
os.rename(old_path, new_path)
1198
1087
def create_file(self, contents, trans_id, mode_id=None):
1199
1088
"""Schedule creation of a new file.
1234
1122
def _read_symlink_target(self, trans_id):
1235
1123
return os.readlink(self._limbo_name(trans_id))
1237
def _set_mtime(self, path):
1238
"""All files that are created get the same mtime.
1240
This time is set by the first object to be created.
1242
if self._creation_mtime is None:
1243
self._creation_mtime = time.time()
1244
os.utime(path, (self._creation_mtime, self._creation_mtime))
1246
1125
def create_hardlink(self, path, trans_id):
1247
1126
"""Schedule creation of a hard link"""
1248
1127
name = self._limbo_name(trans_id)
1479
1358
yield self.trans_id_tree_path(childpath)
1481
def _generate_limbo_path(self, trans_id):
1482
"""Generate a limbo path using the final path if possible.
1484
This optimizes the performance of applying the tree transform by
1485
avoiding renames. These renames can be avoided only when the parent
1486
directory is already scheduled for creation.
1488
If the final path cannot be used, falls back to using the trans_id as
1491
parent = self._new_parent.get(trans_id)
1492
# if the parent directory is already in limbo (e.g. when building a
1493
# tree), choose a limbo name inside the parent, to reduce further
1495
use_direct_path = False
1496
if self._new_contents.get(parent) == 'directory':
1497
filename = self._new_name.get(trans_id)
1498
if filename is not None:
1499
if parent not in self._limbo_children:
1500
self._limbo_children[parent] = set()
1501
self._limbo_children_names[parent] = {}
1502
use_direct_path = True
1503
# the direct path can only be used if no other file has
1504
# already taken this pathname, i.e. if the name is unused, or
1505
# if it is already associated with this trans_id.
1506
elif self._case_sensitive_target:
1507
if (self._limbo_children_names[parent].get(filename)
1508
in (trans_id, None)):
1509
use_direct_path = True
1511
for l_filename, l_trans_id in\
1512
self._limbo_children_names[parent].iteritems():
1513
if l_trans_id == trans_id:
1515
if l_filename.lower() == filename.lower():
1518
use_direct_path = True
1520
if not use_direct_path:
1521
return DiskTreeTransform._generate_limbo_path(self, trans_id)
1523
limbo_name = pathjoin(self._limbo_files[parent], filename)
1524
self._limbo_children[parent].add(trans_id)
1525
self._limbo_children_names[parent][filename] = trans_id
1529
1361
def apply(self, no_conflicts=False, precomputed_delta=None, _mover=None):
1530
1362
"""Apply all changes to the inventory and filesystem.
1651
1485
child_pb.update('removing file', num, len(tree_paths))
1652
1486
full_path = self._tree.abspath(path)
1653
1487
if trans_id in self._removed_contents:
1654
delete_path = os.path.join(self._deletiondir, trans_id)
1655
mover.pre_delete(full_path, delete_path)
1656
elif (trans_id in self._new_name
1657
or trans_id in self._new_parent):
1488
mover.pre_delete(full_path, os.path.join(self._deletiondir,
1490
elif trans_id in self._new_name or trans_id in \
1659
1493
mover.rename(full_path, self._limbo_name(trans_id))
1660
1494
except OSError, e:
1714
1548
unversioned files in the input tree.
1717
def __init__(self, tree, pb=None, case_sensitive=True):
1551
def __init__(self, tree, pb=DummyProgress(), case_sensitive=True):
1718
1552
tree.lock_read()
1719
1553
limbodir = osutils.mkdtemp(prefix='bzr-limbo-')
1720
1554
DiskTreeTransform.__init__(self, tree, limbodir, pb, case_sensitive)
1765
1599
self._all_children_cache = {}
1766
1600
self._path2trans_id_cache = {}
1767
1601
self._final_name_cache = {}
1768
self._iter_changes_cache = dict((c[0], c) for c in
1769
self._transform.iter_changes())
1603
def _changes(self, file_id):
1604
for changes in self._transform.iter_changes():
1605
if changes[0] == file_id:
1771
1608
def _content_change(self, file_id):
1772
1609
"""Return True if the content of this file changed"""
1773
changes = self._iter_changes_cache.get(file_id)
1610
changes = self._changes(file_id)
1774
1611
# changes[2] is true if the file content changed. See
1775
1612
# InterTree.iter_changes.
1776
1613
return (changes is not None and changes[2])
1848
1682
def __iter__(self):
1849
1683
return iter(self.all_file_ids())
1851
def _has_id(self, file_id, fallback_check):
1685
def has_id(self, file_id):
1852
1686
if file_id in self._transform._r_new_id:
1854
1688
elif file_id in set([self._transform.tree_file_id(trans_id) for
1855
1689
trans_id in self._transform._removed_id]):
1858
return fallback_check(file_id)
1860
def has_id(self, file_id):
1861
return self._has_id(file_id, self._transform._tree.has_id)
1863
def has_or_had_id(self, file_id):
1864
return self._has_id(file_id, self._transform._tree.has_or_had_id)
1692
return self._transform._tree.has_id(file_id)
1866
1694
def _path2trans_id(self, path):
1867
1695
# We must not use None here, because that is a valid value to store.
1920
1748
if self._transform.final_file_id(trans_id) is None:
1921
1749
yield self._final_paths._determine_path(trans_id)
1923
def _make_inv_entries(self, ordered_entries, specific_file_ids=None,
1924
yield_parents=False):
1751
def _make_inv_entries(self, ordered_entries, specific_file_ids=None):
1925
1752
for trans_id, parent_file_id in ordered_entries:
1926
1753
file_id = self._transform.final_file_id(trans_id)
1927
1754
if file_id is None:
1953
1780
ordered_ids.append((trans_id, parent_file_id))
1954
1781
return ordered_ids
1956
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
1783
def iter_entries_by_dir(self, specific_file_ids=None):
1957
1784
# This may not be a maximally efficient implementation, but it is
1958
1785
# reasonably straightforward. An implementation that grafts the
1959
1786
# TreeTransform changes onto the tree's iter_entries_by_dir results
2014
1841
def get_file_mtime(self, file_id, path=None):
2015
1842
"""See Tree.get_file_mtime"""
2016
1843
if not self._content_change(file_id):
2017
return self._transform._tree.get_file_mtime(file_id)
1844
return self._transform._tree.get_file_mtime(file_id, path)
2018
1845
return self._stat_limbo_file(file_id).st_mtime
2020
1847
def _file_size(self, entry, stat_value):
2082
1909
executable = None
2083
1910
if kind == 'symlink':
2084
1911
link_or_sha1 = os.readlink(limbo_name).decode(osutils._fs_enc)
2085
executable = tt._new_executability.get(trans_id, executable)
1912
if supports_executable():
1913
executable = tt._new_executability.get(trans_id, executable)
2086
1914
return kind, size, executable, link_or_sha1
2088
1916
def iter_changes(self, from_tree, include_unchanged=False,
2137
1965
return old_annotation
2138
1966
if not changed_content:
2139
1967
return old_annotation
2140
# TODO: This is doing something similar to what WT.annotate_iter is
2141
# doing, however it fails slightly because it doesn't know what
2142
# the *other* revision_id is, so it doesn't know how to give the
2143
# other as the origin for some lines, they all get
2144
# 'default_revision'
2145
# It would be nice to be able to use the new Annotator based
2146
# approach, as well.
2147
1968
return annotate.reannotate([old_annotation],
2148
1969
self.get_file(file_id).readlines(),
2149
1970
default_revision)
2401
2222
new_desired_files = desired_files
2403
2224
iter = accelerator_tree.iter_changes(tree, include_unchanged=True)
2404
unchanged = [(f, p[1]) for (f, p, c, v, d, n, k, e)
2405
in iter if not (c or e[0] != e[1])]
2406
if accelerator_tree.supports_content_filtering():
2407
unchanged = [(f, p) for (f, p) in unchanged
2408
if not accelerator_tree.iter_search_rules([p]).next()]
2409
unchanged = dict(unchanged)
2225
unchanged = dict((f, p[1]) for (f, p, c, v, d, n, k, e)
2226
in iter if not (c or e[0] != e[1]))
2410
2227
new_desired_files = []
2412
2229
for file_id, (trans_id, tree_path) in desired_files:
2535
2352
tt.create_directory(trans_id)
2538
def create_from_tree(tt, trans_id, tree, file_id, bytes=None,
2539
filter_tree_path=None):
2540
"""Create new file contents according to tree contents.
2542
:param filter_tree_path: the tree path to use to lookup
2543
content filters to apply to the bytes output in the working tree.
2544
This only applies if the working tree supports content filtering.
2355
def create_from_tree(tt, trans_id, tree, file_id, bytes=None):
2356
"""Create new file contents according to tree contents."""
2546
2357
kind = tree.kind(file_id)
2547
2358
if kind == 'directory':
2548
2359
tt.create_directory(trans_id)
2553
2364
bytes = tree_file.readlines()
2555
2366
tree_file.close()
2557
if wt.supports_content_filtering() and filter_tree_path is not None:
2558
filters = wt._content_filter_stack(filter_tree_path)
2559
bytes = filtered_output_bytes(bytes, filters,
2560
ContentFilterContext(filter_tree_path, tree))
2561
2367
tt.create_file(bytes, trans_id)
2562
2368
elif kind == "symlink":
2563
2369
tt.create_symlink(tree.get_symlink_target(file_id), trans_id)
2617
2423
def revert(working_tree, target_tree, filenames, backups=False,
2618
pb=None, change_reporter=None):
2424
pb=DummyProgress(), change_reporter=None):
2619
2425
"""Revert a working tree's contents to those of a target tree."""
2620
2426
target_tree.lock_read()
2621
pb = ui.ui_factory.nested_progress_bar()
2622
2427
tt = TreeTransform(working_tree, pb)
2624
2429
pp = ProgressPhase("Revert phase", 3, pb)
2750
2557
parent_trans = ROOT_PARENT
2752
2559
parent_trans = tt.trans_id_file_id(parent[1])
2753
if parent[0] is None and versioned[0]:
2754
tt.adjust_root_path(name[1], parent_trans)
2756
tt.adjust_path(name[1], parent_trans, trans_id)
2560
tt.adjust_path(name[1], parent_trans, trans_id)
2757
2561
if executable[0] != executable[1] and kind[1] == "file":
2758
2562
tt.set_executability(executable[1], trans_id)
2759
if working_tree.supports_content_filtering():
2760
for index, ((trans_id, mode_id), bytes) in enumerate(
2761
target_tree.iter_files_bytes(deferred_files)):
2762
file_id = deferred_files[index][0]
2763
# We're reverting a tree to the target tree so using the
2764
# target tree to find the file path seems the best choice
2765
# here IMO - Ian C 27/Oct/2009
2766
filter_tree_path = target_tree.id2path(file_id)
2767
filters = working_tree._content_filter_stack(filter_tree_path)
2768
bytes = filtered_output_bytes(bytes, filters,
2769
ContentFilterContext(filter_tree_path, working_tree))
2770
tt.create_file(bytes, trans_id, mode_id)
2772
for (trans_id, mode_id), bytes in target_tree.iter_files_bytes(
2774
tt.create_file(bytes, trans_id, mode_id)
2775
tt.fixup_new_roots()
2563
for (trans_id, mode_id), bytes in target_tree.iter_files_bytes(
2565
tt.create_file(bytes, trans_id, mode_id)
2777
2567
if basis_tree is not None:
2778
2568
basis_tree.unlock()
2779
2569
return merge_modified
2782
def resolve_conflicts(tt, pb=None, pass_func=None):
2572
def resolve_conflicts(tt, pb=DummyProgress(), pass_func=None):
2783
2573
"""Make many conflict-resolution attempts, but die if they fail"""
2784
2574
if pass_func is None:
2785
2575
pass_func = conflict_pass
2786
2576
new_conflicts = set()
2787
pb = ui.ui_factory.nested_progress_bar()
2789
2578
for n in range(10):
2790
2579
pb.update('Resolution pass', n+1, 10)
2923
2712
self.pending_deletions = []
2925
2714
def rename(self, from_, to):
2926
"""Rename a file from one path to another."""
2715
"""Rename a file from one path to another. Functions like os.rename"""
2928
osutils.rename(from_, to)
2717
os.rename(from_, to)
2929
2718
except OSError, e:
2930
2719
if e.errno in (errno.EEXIST, errno.ENOTEMPTY):
2931
2720
raise errors.FileExists(to, str(e))