47
48
ExistingLimbo, ImmortalLimbo, NoFinalPath,
48
49
UnableCreateSymlink)
49
50
from bzrlib.filters import filtered_output_bytes, ContentFilterContext
51
from bzrlib.inventory import InventoryEntry
50
52
from bzrlib.osutils import (
102
105
self._new_parent = {}
103
106
# mapping of trans_id with new contents -> new file_kind
104
107
self._new_contents = {}
105
# mapping of trans_id => (sha1 of content, stat_value)
106
self._observed_sha1s = {}
107
108
# Set of trans_ids whose contents will be removed
108
109
self._removed_contents = set()
109
110
# Mapping of trans_id -> new execute-bit value
629
630
conflicts.append(('versioning no contents', trans_id))
631
if not inventory.InventoryEntry.versionable_kind(kind):
632
if not InventoryEntry.versionable_kind(kind):
632
633
conflicts.append(('versioning bad kind', trans_id, kind))
755
756
def new_file(self, name, parent_id, contents, file_id=None,
756
executable=None, sha1=None):
757
758
"""Convenience method to create files.
759
760
name is the name of the file to create.
766
767
trans_id = self._new_entry(name, parent_id, file_id)
767
768
# TODO: rather than scheduling a set_executable call,
768
769
# have create_file create the file with the right mode.
769
self.create_file(contents, trans_id, sha1=sha1)
770
self.create_file(contents, trans_id)
770
771
if executable is not None:
771
772
self.set_executability(executable, trans_id)
1248
1249
descendants.update(self._limbo_descendants(descendant))
1249
1250
return descendants
1251
def create_file(self, contents, trans_id, mode_id=None, sha1=None):
1252
def create_file(self, contents, trans_id, mode_id=None):
1252
1253
"""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.
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.
1265
1263
name = self._limbo_name(trans_id)
1266
1264
f = open(name, 'wb')
1274
1272
os.unlink(name)
1276
1275
f.writelines(contents)
1279
1278
self._set_mtime(name)
1280
1279
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
1281
def _read_file_chunks(self, trans_id):
1288
1282
cur_file = open(self._limbo_name(trans_id), 'rb')
1347
1341
def cancel_creation(self, trans_id):
1348
1342
"""Cancel the creation of new file contents."""
1349
1343
del self._new_contents[trans_id]
1350
if trans_id in self._observed_sha1s:
1351
del self._observed_sha1s[trans_id]
1352
1344
children = self._limbo_children.get(trans_id)
1353
1345
# if this is a limbo directory with children, move them before removing
1354
1346
# the directory
1370
1362
if orphan_policy is None:
1371
1363
orphan_policy = default_policy
1372
1364
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))
1365
trace.warning('%s (from %s) is not a known policy, defaulting to %s'
1366
% (orphan_policy, conf_var_name, default_policy))
1375
1367
orphan_policy = default_policy
1376
1368
handle_orphan = orphaning_registry.get(orphan_policy)
1377
1369
handle_orphan(self, trans_id, parent_id)
1838
1829
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
1830
if (trans_id in self._new_contents or
1843
1831
self.path_changed(trans_id)):
1844
1832
if trans_id in self._new_contents:
1845
1833
modified_paths.append(full_path)
1846
1834
if trans_id in self._new_executability:
1847
1835
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)
1853
1837
child_pb.finished()
1854
1838
self._new_contents.clear()
1855
1839
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
1842
class TransformPreview(DiskTreeTransform):
1882
1843
"""A TreeTransform for generating preview trees.
1932
1893
raise NotImplementedError(self.new_orphan)
1935
class _PreviewTree(tree.InventoryTree):
1896
class _PreviewTree(tree.Tree):
1936
1897
"""Partial implementation of Tree to support show_diff_trees"""
1938
1899
def __init__(self, transform):
1967
1928
yield self._get_repository().revision_tree(revision_id)
1969
1930
def _get_file_revision(self, file_id, vf, tree_revision):
1970
parent_keys = [(file_id, t.get_file_revision(file_id)) for t in
1931
parent_keys = [(file_id, self._file_revision(t, file_id)) for t in
1971
1932
self._iter_parent_trees()]
1972
1933
vf.add_lines((file_id, tree_revision), parent_keys,
1973
1934
self.get_file_lines(file_id))
2541
2502
executable = tree.is_executable(file_id, tree_path)
2543
2504
tt.set_executability(executable, trans_id)
2544
trans_data = (trans_id, tree_path, entry.text_sha1)
2505
trans_data = (trans_id, tree_path)
2545
2506
deferred_contents.append((file_id, trans_data))
2547
2508
file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
2592
2553
unchanged = dict(unchanged)
2593
2554
new_desired_files = []
2595
for file_id, (trans_id, tree_path, text_sha1) in desired_files:
2556
for file_id, (trans_id, tree_path) in desired_files:
2596
2557
accelerator_path = unchanged.get(file_id)
2597
2558
if accelerator_path is None:
2598
new_desired_files.append((file_id,
2599
(trans_id, tree_path, text_sha1)))
2559
new_desired_files.append((file_id, (trans_id, tree_path)))
2601
2561
pb.update('Adding file contents', count + offset, total)
2609
2569
contents = filtered_output_bytes(contents, filters,
2610
2570
ContentFilterContext(tree_path, tree))
2612
tt.create_file(contents, trans_id, sha1=text_sha1)
2572
tt.create_file(contents, trans_id)
2615
2575
contents.close()
2620
2580
offset += count
2621
for count, ((trans_id, tree_path, text_sha1), contents) in enumerate(
2581
for count, ((trans_id, tree_path), contents) in enumerate(
2622
2582
tree.iter_files_bytes(new_desired_files)):
2623
2583
if wt.supports_content_filtering():
2624
2584
filters = wt._content_filter_stack(tree_path)
2625
2585
contents = filtered_output_bytes(contents, filters,
2626
2586
ContentFilterContext(tree_path, tree))
2627
tt.create_file(contents, trans_id, sha1=text_sha1)
2587
tt.create_file(contents, trans_id)
2628
2588
pb.update('Adding file contents', count + offset, total)
3039
2999
file_id = tt.final_file_id(trans_id)
3040
3000
if file_id is None:
3041
3001
file_id = tt.inactive_file_id(trans_id)
3042
_, entry = path_tree.iter_entries_by_dir(
3002
entry = path_tree.inventory[file_id]
3044
3003
# special-case the other tree root (move its
3045
3004
# children to current root)
3046
3005
if entry.parent_id is None: