48
47
ExistingLimbo, ImmortalLimbo, NoFinalPath,
49
48
UnableCreateSymlink)
50
49
from bzrlib.filters import filtered_output_bytes, ContentFilterContext
51
from bzrlib.inventory import InventoryEntry
52
50
from bzrlib.osutils import (
105
102
self._new_parent = {}
106
103
# mapping of trans_id with new contents -> new file_kind
107
104
self._new_contents = {}
105
# mapping of trans_id => (sha1 of content, stat_value)
106
self._observed_sha1s = {}
108
107
# Set of trans_ids whose contents will be removed
109
108
self._removed_contents = set()
110
109
# Mapping of trans_id -> new execute-bit value
630
629
conflicts.append(('versioning no contents', trans_id))
632
if not InventoryEntry.versionable_kind(kind):
631
if not inventory.InventoryEntry.versionable_kind(kind):
633
632
conflicts.append(('versioning bad kind', trans_id, kind))
756
755
def new_file(self, name, parent_id, contents, file_id=None,
756
executable=None, sha1=None):
758
757
"""Convenience method to create files.
760
759
name is the name of the file to create.
767
766
trans_id = self._new_entry(name, parent_id, file_id)
768
767
# TODO: rather than scheduling a set_executable call,
769
768
# have create_file create the file with the right mode.
770
self.create_file(contents, trans_id)
769
self.create_file(contents, trans_id, sha1=sha1)
771
770
if executable is not None:
772
771
self.set_executability(executable, trans_id)
1249
1248
descendants.update(self._limbo_descendants(descendant))
1250
1249
return descendants
1252
def create_file(self, contents, trans_id, mode_id=None):
1251
def create_file(self, contents, trans_id, mode_id=None, sha1=None):
1253
1252
"""Schedule creation of a new file.
1257
Contents is an iterator of strings, all of which will be written
1258
to the target destination.
1260
New file takes the permissions of any existing file with that id,
1261
unless mode_id is specified.
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.
1263
1265
name = self._limbo_name(trans_id)
1264
1266
f = open(name, 'wb')
1272
1274
os.unlink(name)
1275
1276
f.writelines(contents)
1278
1279
self._set_mtime(name)
1279
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))
1281
1287
def _read_file_chunks(self, trans_id):
1282
1288
cur_file = open(self._limbo_name(trans_id), 'rb')
1341
1347
def cancel_creation(self, trans_id):
1342
1348
"""Cancel the creation of new file contents."""
1343
1349
del self._new_contents[trans_id]
1350
if trans_id in self._observed_sha1s:
1351
del self._observed_sha1s[trans_id]
1344
1352
children = self._limbo_children.get(trans_id)
1345
1353
# if this is a limbo directory with children, move them before removing
1346
1354
# the directory
1362
1370
if orphan_policy is None:
1363
1371
orphan_policy = default_policy
1364
1372
if orphan_policy not in orphaning_registry:
1365
trace.warning('%s (from %s) is not a known policy, defaulting to %s'
1366
% (orphan_policy, conf_var_name, default_policy))
1373
trace.warning('%s (from %s) is not a known policy, defaulting '
1374
'to %s' % (orphan_policy, conf_var_name, default_policy))
1367
1375
orphan_policy = default_policy
1368
1376
handle_orphan = orphaning_registry.get(orphan_policy)
1369
1377
handle_orphan(self, trans_id, parent_id)
1829
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.
1830
1842
if (trans_id in self._new_contents or
1831
1843
self.path_changed(trans_id)):
1832
1844
if trans_id in self._new_contents:
1833
1845
modified_paths.append(full_path)
1834
1846
if trans_id in self._new_executability:
1835
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)
1837
1853
child_pb.finished()
1838
1854
self._new_contents.clear()
1839
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)
1842
1881
class TransformPreview(DiskTreeTransform):
1843
1882
"""A TreeTransform for generating preview trees.
1859
1898
path = self._tree_id_paths.get(trans_id)
1860
1899
if path is None:
1862
file_id = self._tree.path2id(path)
1864
return self._tree.kind(file_id)
1865
except errors.NoSuchFile:
1901
kind = self._tree.path_content_summary(path)[0]
1902
if kind == 'missing':
1868
1906
def _set_mode(self, trans_id, mode_id, typefunc):
1869
1907
"""Set the mode of new file contents.
1893
1931
raise NotImplementedError(self.new_orphan)
1896
class _PreviewTree(tree.Tree):
1934
class _PreviewTree(tree.InventoryTree):
1897
1935
"""Partial implementation of Tree to support show_diff_trees"""
1899
1937
def __init__(self, transform):
1928
1966
yield self._get_repository().revision_tree(revision_id)
1930
1968
def _get_file_revision(self, file_id, vf, tree_revision):
1931
parent_keys = [(file_id, self._file_revision(t, file_id)) for t in
1969
parent_keys = [(file_id, t.get_file_revision(file_id)) for t in
1932
1970
self._iter_parent_trees()]
1933
1971
vf.add_lines((file_id, tree_revision), parent_keys,
1934
1972
self.get_file_lines(file_id))
1938
1976
vf.fallback_versionedfiles.append(base_vf)
1939
1977
return tree_revision
1941
def _stat_limbo_file(self, file_id):
1942
trans_id = self._transform.trans_id_file_id(file_id)
1979
def _stat_limbo_file(self, file_id=None, trans_id=None):
1980
if trans_id is None:
1981
trans_id = self._transform.trans_id_file_id(file_id)
1943
1982
name = self._transform._limbo_name(trans_id)
1944
1983
return os.lstat(name)
2161
2200
def get_file_size(self, file_id):
2162
2201
"""See Tree.get_file_size"""
2202
trans_id = self._transform.trans_id_file_id(file_id)
2203
kind = self._transform.final_kind(trans_id)
2206
if trans_id in self._transform._new_contents:
2207
return self._stat_limbo_file(trans_id=trans_id).st_size
2163
2208
if self.kind(file_id) == 'file':
2164
2209
return self._transform._tree.get_file_size(file_id)
2193
2238
except errors.NoSuchId:
2241
def has_filename(self, path):
2242
trans_id = self._path2trans_id(path)
2243
if trans_id in self._transform._new_contents:
2245
elif trans_id in self._transform._removed_contents:
2248
return self._transform._tree.has_filename(path)
2196
2250
def path_content_summary(self, path):
2197
2251
trans_id = self._path2trans_id(path)
2198
2252
tt = self._transform
2502
2556
executable = tree.is_executable(file_id, tree_path)
2504
2558
tt.set_executability(executable, trans_id)
2505
trans_data = (trans_id, tree_path)
2559
trans_data = (trans_id, tree_path, entry.text_sha1)
2506
2560
deferred_contents.append((file_id, trans_data))
2508
2562
file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
2553
2607
unchanged = dict(unchanged)
2554
2608
new_desired_files = []
2556
for file_id, (trans_id, tree_path) in desired_files:
2610
for file_id, (trans_id, tree_path, text_sha1) in desired_files:
2557
2611
accelerator_path = unchanged.get(file_id)
2558
2612
if accelerator_path is None:
2559
new_desired_files.append((file_id, (trans_id, tree_path)))
2613
new_desired_files.append((file_id,
2614
(trans_id, tree_path, text_sha1)))
2561
2616
pb.update('Adding file contents', count + offset, total)
2569
2624
contents = filtered_output_bytes(contents, filters,
2570
2625
ContentFilterContext(tree_path, tree))
2572
tt.create_file(contents, trans_id)
2627
tt.create_file(contents, trans_id, sha1=text_sha1)
2575
2630
contents.close()
2580
2635
offset += count
2581
for count, ((trans_id, tree_path), contents) in enumerate(
2636
for count, ((trans_id, tree_path, text_sha1), contents) in enumerate(
2582
2637
tree.iter_files_bytes(new_desired_files)):
2583
2638
if wt.supports_content_filtering():
2584
2639
filters = wt._content_filter_stack(tree_path)
2585
2640
contents = filtered_output_bytes(contents, filters,
2586
2641
ContentFilterContext(tree_path, tree))
2587
tt.create_file(contents, trans_id)
2642
tt.create_file(contents, trans_id, sha1=text_sha1)
2588
2643
pb.update('Adding file contents', count + offset, total)
2999
3054
file_id = tt.final_file_id(trans_id)
3000
3055
if file_id is None:
3001
3056
file_id = tt.inactive_file_id(trans_id)
3002
entry = path_tree.inventory[file_id]
3057
_, entry = path_tree.iter_entries_by_dir(
3003
3059
# special-case the other tree root (move its
3004
3060
# children to current root)
3005
3061
if entry.parent_id is None: