102
102
self._new_parent = {}
103
103
# mapping of trans_id with new contents -> new file_kind
104
104
self._new_contents = {}
105
# mapping of trans_id => (sha1 of content, stat_value)
106
self._observed_sha1s = {}
105
107
# Set of trans_ids whose contents will be removed
106
108
self._removed_contents = set()
107
109
# Mapping of trans_id -> new execute-bit value
753
755
def new_file(self, name, parent_id, contents, file_id=None,
756
executable=None, sha1=None):
755
757
"""Convenience method to create files.
757
759
name is the name of the file to create.
764
766
trans_id = self._new_entry(name, parent_id, file_id)
765
767
# TODO: rather than scheduling a set_executable call,
766
768
# have create_file create the file with the right mode.
767
self.create_file(contents, trans_id)
769
self.create_file(contents, trans_id, sha1=sha1)
768
770
if executable is not None:
769
771
self.set_executability(executable, trans_id)
1246
1248
descendants.update(self._limbo_descendants(descendant))
1247
1249
return descendants
1249
def create_file(self, contents, trans_id, mode_id=None):
1251
def create_file(self, contents, trans_id, mode_id=None, sha1=None):
1250
1252
"""Schedule creation of a new file.
1254
Contents is an iterator of strings, all of which will be written
1255
to the target destination.
1257
New file takes the permissions of any existing file with that id,
1258
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.
1260
1265
name = self._limbo_name(trans_id)
1261
1266
f = open(name, 'wb')
1269
1274
os.unlink(name)
1272
1276
f.writelines(contents)
1275
1279
self._set_mtime(name)
1276
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))
1278
1287
def _read_file_chunks(self, trans_id):
1279
1288
cur_file = open(self._limbo_name(trans_id), 'rb')
1338
1347
def cancel_creation(self, trans_id):
1339
1348
"""Cancel the creation of new file contents."""
1340
1349
del self._new_contents[trans_id]
1350
if trans_id in self._observed_sha1s:
1351
del self._observed_sha1s[trans_id]
1341
1352
children = self._limbo_children.get(trans_id)
1342
1353
# if this is a limbo directory with children, move them before removing
1343
1354
# the directory
1826
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.
1827
1842
if (trans_id in self._new_contents or
1828
1843
self.path_changed(trans_id)):
1829
1844
if trans_id in self._new_contents:
1830
1845
modified_paths.append(full_path)
1831
1846
if trans_id in self._new_executability:
1832
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)
1834
1853
child_pb.finished()
1835
1854
self._new_contents.clear()
1836
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)
1839
1881
class TransformPreview(DiskTreeTransform):
1840
1882
"""A TreeTransform for generating preview trees.