398
386
return sorted(FinalPaths(self).get_paths(new_ids))
400
388
def _inventory_altered(self):
401
"""Determine which trans_ids need new Inventory entries.
403
An new entry is needed when anything that would be reflected by an
404
inventory entry changes, including file name, file_id, parent file_id,
405
file kind, and the execute bit.
407
Some care is taken to return entries with real changes, not cases
408
where the value is deleted and then restored to its original value,
409
but some actually unchanged values may be returned.
411
:returns: A list of (path, trans_id) for all items requiring an
412
inventory change. Ordered by path.
415
# Find entries whose file_ids are new (or changed).
416
new_file_id = set(t for t in self._new_id
417
if self._new_id[t] != self.tree_file_id(t))
418
for id_set in [self._new_name, self._new_parent, new_file_id,
389
"""Get the trans_ids and paths of files needing new inv entries."""
391
for id_set in [self._new_name, self._new_parent, self._new_id,
419
392
self._new_executability]:
420
changed_ids.update(id_set)
421
# removing implies a kind change
393
new_ids.update(id_set)
422
394
changed_kind = set(self._removed_contents)
424
395
changed_kind.intersection_update(self._new_contents)
425
# Ignore entries that are already known to have changed.
426
changed_kind.difference_update(changed_ids)
427
# to keep only the truly changed ones
396
changed_kind.difference_update(new_ids)
428
397
changed_kind = (t for t in changed_kind
429
398
if self.tree_kind(t) != self.final_kind(t))
430
# all kind changes will alter the inventory
431
changed_ids.update(changed_kind)
432
# To find entries with changed parent_ids, find parents which existed,
433
# but changed file_id.
434
changed_file_id = set(t for t in new_file_id if t in self._removed_id)
435
# Now add all their children to the set.
436
for parent_trans_id in new_file_id:
437
changed_ids.update(self.iter_tree_children(parent_trans_id))
438
return sorted(FinalPaths(self).get_paths(changed_ids))
399
new_ids.update(changed_kind)
400
return sorted(FinalPaths(self).get_paths(new_ids))
440
402
def final_kind(self, trans_id):
441
403
"""Determine the final file kind, after any changes applied.
745
705
def _parent_type_conflicts(self, by_parent):
746
"""Children must have a directory parent"""
706
"""parents must have directory 'contents'."""
748
708
for parent_id, children in by_parent.iteritems():
749
709
if parent_id is ROOT_PARENT:
752
for child_id in children:
753
if self.final_kind(child_id) is not None:
711
if not self._any_contents(children):
758
# There is at least a child, so we need an existing directory to
760
713
kind = self.final_kind(parent_id)
762
# The directory will be deleted
763
715
conflicts.append(('missing parent', parent_id))
764
716
elif kind != "directory":
765
# Meh, we need a *directory* to put something in it
766
717
conflicts.append(('non-directory parent', parent_id))
720
def _any_contents(self, trans_ids):
721
"""Return true if any of the trans_ids, will have contents."""
722
for trans_id in trans_ids:
723
if self.final_kind(trans_id) is not None:
769
727
def _set_executability(self, path, trans_id):
770
728
"""Set the executability of versioned files """
771
729
if supports_executable():
1299
1241
descendants.update(self._limbo_descendants(descendant))
1300
1242
return descendants
1302
def create_file(self, contents, trans_id, mode_id=None, sha1=None):
1244
def create_file(self, contents, trans_id, mode_id=None):
1303
1245
"""Schedule creation of a new file.
1307
:param contents: an iterator of strings, all of which will be written
1308
to the target destination.
1309
:param trans_id: TreeTransform handle
1310
:param mode_id: If not None, force the mode of the target file to match
1311
the mode of the object referenced by mode_id.
1312
Otherwise, we will try to preserve mode bits of an existing file.
1313
:param sha1: If the sha1 of this content is already known, pass it in.
1314
We can use it to prevent future sha1 computations.
1249
Contents is an iterator of strings, all of which will be written
1250
to the target destination.
1252
New file takes the permissions of any existing file with that id,
1253
unless mode_id is specified.
1316
1255
name = self._limbo_name(trans_id)
1317
1256
f = open(name, 'wb')
1319
unique_add(self._new_contents, trans_id, 'file')
1259
unique_add(self._new_contents, trans_id, 'file')
1261
# Clean up the file, it never got registered so
1262
# TreeTransform.finalize() won't clean it up.
1320
1267
f.writelines(contents)
1323
1270
self._set_mtime(name)
1324
1271
self._set_mode(trans_id, mode_id, S_ISREG)
1325
# It is unfortunate we have to use lstat instead of fstat, but we just
1326
# used utime and chmod on the file, so we need the accurate final
1328
if sha1 is not None:
1329
self._observed_sha1s[trans_id] = (sha1, osutils.lstat(name))
1331
1273
def _read_file_chunks(self, trans_id):
1332
1274
cur_file = open(self._limbo_name(trans_id), 'rb')
1884
1821
self.rename_count += 1
1885
# TODO: if trans_id in self._observed_sha1s, we should
1886
# re-stat the final target, since ctime will be
1887
# updated by the change.
1888
1822
if (trans_id in self._new_contents or
1889
1823
self.path_changed(trans_id)):
1890
1824
if trans_id in self._new_contents:
1891
1825
modified_paths.append(full_path)
1892
1826
if trans_id in self._new_executability:
1893
1827
self._set_executability(path, trans_id)
1894
if trans_id in self._observed_sha1s:
1895
o_sha1, o_st_val = self._observed_sha1s[trans_id]
1896
st = osutils.lstat(full_path)
1897
self._observed_sha1s[trans_id] = (o_sha1, st)
1899
1829
child_pb.finished()
1900
for path, trans_id in new_paths:
1901
# new_paths includes stuff like workingtree conflicts. Only the
1902
# stuff in new_contents actually comes from limbo.
1903
if trans_id in self._limbo_files:
1904
del self._limbo_files[trans_id]
1905
1830
self._new_contents.clear()
1906
1831
return modified_paths
1908
def _apply_observed_sha1s(self):
1909
"""After we have finished renaming everything, update observed sha1s
1911
This has to be done after self._tree.apply_inventory_delta, otherwise
1912
it doesn't know anything about the files we are updating. Also, we want
1913
to do this as late as possible, so that most entries end up cached.
1915
# TODO: this doesn't update the stat information for directories. So
1916
# the first 'bzr status' will still need to rewrite
1917
# .bzr/checkout/dirstate. However, we at least don't need to
1918
# re-read all of the files.
1919
# TODO: If the operation took a while, we could do a time.sleep(3) here
1920
# to allow the clock to tick over and ensure we won't have any
1921
# problems. (we could observe start time, and finish time, and if
1922
# it is less than eg 10% overhead, add a sleep call.)
1923
paths = FinalPaths(self)
1924
for trans_id, observed in self._observed_sha1s.iteritems():
1925
path = paths.get_path(trans_id)
1926
# We could get the file_id, but dirstate prefers to use the path
1927
# anyway, and it is 'cheaper' to determine.
1928
# file_id = self._new_id[trans_id]
1929
self._tree._observed_sha1(None, path, observed)
1932
1834
class TransformPreview(DiskTreeTransform):
1933
1835
"""A TreeTransform for generating preview trees.
2686
2572
offset += count
2687
for count, ((trans_id, tree_path, text_sha1), contents) in enumerate(
2573
for count, ((trans_id, tree_path), contents) in enumerate(
2688
2574
tree.iter_files_bytes(new_desired_files)):
2689
2575
if wt.supports_content_filtering():
2690
2576
filters = wt._content_filter_stack(tree_path)
2691
2577
contents = filtered_output_bytes(contents, filters,
2692
2578
ContentFilterContext(tree_path, tree))
2693
tt.create_file(contents, trans_id, sha1=text_sha1)
2579
tt.create_file(contents, trans_id)
2694
2580
pb.update('Adding file contents', count + offset, total)
2922
2804
deferred_files = []
2923
2805
for id_num, (file_id, path, changed_content, versioned, parent, name,
2924
2806
kind, executable) in enumerate(change_list):
2925
target_path, wt_path = path
2926
target_versioned, wt_versioned = versioned
2927
target_parent, wt_parent = parent
2928
target_name, wt_name = name
2929
target_kind, wt_kind = kind
2930
target_executable, wt_executable = executable
2931
if skip_root and wt_parent is None:
2807
if skip_root and file_id[0] is not None and parent[0] is None:
2933
2809
trans_id = tt.trans_id_file_id(file_id)
2935
2811
if changed_content:
2936
2812
keep_content = False
2937
if wt_kind == 'file' and (backups or target_kind is None):
2813
if kind[0] == 'file' and (backups or kind[1] is None):
2938
2814
wt_sha1 = working_tree.get_file_sha1(file_id)
2939
2815
if merge_modified.get(file_id) != wt_sha1:
2940
2816
# acquire the basis tree lazily to prevent the
2943
2819
if basis_tree is None:
2944
2820
basis_tree = working_tree.basis_tree()
2945
2821
basis_tree.lock_read()
2946
if basis_tree.has_id(file_id):
2822
if file_id in basis_tree:
2947
2823
if wt_sha1 != basis_tree.get_file_sha1(file_id):
2948
2824
keep_content = True
2949
elif target_kind is None and not target_versioned:
2825
elif kind[1] is None and not versioned[1]:
2950
2826
keep_content = True
2951
if wt_kind is not None:
2827
if kind[0] is not None:
2952
2828
if not keep_content:
2953
2829
tt.delete_contents(trans_id)
2954
elif target_kind is not None:
2955
parent_trans_id = tt.trans_id_file_id(wt_parent)
2830
elif kind[1] is not None:
2831
parent_trans_id = tt.trans_id_file_id(parent[0])
2956
2832
backup_name = tt._available_backup_name(
2957
wt_name, parent_trans_id)
2833
name[0], parent_trans_id)
2958
2834
tt.adjust_path(backup_name, parent_trans_id, trans_id)
2959
new_trans_id = tt.create_path(wt_name, parent_trans_id)
2960
if wt_versioned and target_versioned:
2835
new_trans_id = tt.create_path(name[0], parent_trans_id)
2836
if versioned == (True, True):
2961
2837
tt.unversion_file(trans_id)
2962
2838
tt.version_file(file_id, new_trans_id)
2963
2839
# New contents should have the same unix perms as old
2965
2841
mode_id = trans_id
2966
2842
trans_id = new_trans_id
2967
if target_kind in ('directory', 'tree-reference'):
2843
if kind[1] in ('directory', 'tree-reference'):
2968
2844
tt.create_directory(trans_id)
2969
if target_kind == 'tree-reference':
2845
if kind[1] == 'tree-reference':
2970
2846
revision = target_tree.get_reference_revision(file_id,
2972
2848
tt.set_tree_reference(revision, trans_id)
2973
elif target_kind == 'symlink':
2849
elif kind[1] == 'symlink':
2974
2850
tt.create_symlink(target_tree.get_symlink_target(file_id),
2976
elif target_kind == 'file':
2852
elif kind[1] == 'file':
2977
2853
deferred_files.append((file_id, (trans_id, mode_id)))
2978
2854
if basis_tree is None:
2979
2855
basis_tree = working_tree.basis_tree()
2980
2856
basis_tree.lock_read()
2981
2857
new_sha1 = target_tree.get_file_sha1(file_id)
2982
if (basis_tree.has_id(file_id) and
2983
new_sha1 == basis_tree.get_file_sha1(file_id)):
2858
if (file_id in basis_tree and new_sha1 ==
2859
basis_tree.get_file_sha1(file_id)):
2984
2860
if file_id in merge_modified:
2985
2861
del merge_modified[file_id]
2987
2863
merge_modified[file_id] = new_sha1
2989
2865
# preserve the execute bit when backing up
2990
if keep_content and wt_executable == target_executable:
2991
tt.set_executability(target_executable, trans_id)
2992
elif target_kind is not None:
2993
raise AssertionError(target_kind)
2994
if not wt_versioned and target_versioned:
2866
if keep_content and executable[0] == executable[1]:
2867
tt.set_executability(executable[1], trans_id)
2868
elif kind[1] is not None:
2869
raise AssertionError(kind[1])
2870
if versioned == (False, True):
2995
2871
tt.version_file(file_id, trans_id)
2996
if wt_versioned and not target_versioned:
2872
if versioned == (True, False):
2997
2873
tt.unversion_file(trans_id)
2998
if (target_name is not None and
2999
(wt_name != target_name or wt_parent != target_parent)):
3000
if target_name == '' and target_parent is None:
2874
if (name[1] is not None and
2875
(name[0] != name[1] or parent[0] != parent[1])):
2876
if name[1] == '' and parent[1] is None:
3001
2877
parent_trans = ROOT_PARENT
3003
parent_trans = tt.trans_id_file_id(target_parent)
3004
if wt_parent is None and wt_versioned:
3005
tt.adjust_root_path(target_name, parent_trans)
2879
parent_trans = tt.trans_id_file_id(parent[1])
2880
if parent[0] is None and versioned[0]:
2881
tt.adjust_root_path(name[1], parent_trans)
3007
tt.adjust_path(target_name, parent_trans, trans_id)
3008
if wt_executable != target_executable and target_kind == "file":
3009
tt.set_executability(target_executable, trans_id)
2883
tt.adjust_path(name[1], parent_trans, trans_id)
2884
if executable[0] != executable[1] and kind[1] == "file":
2885
tt.set_executability(executable[1], trans_id)
3010
2886
if working_tree.supports_content_filtering():
3011
2887
for index, ((trans_id, mode_id), bytes) in enumerate(
3012
2888
target_tree.iter_files_bytes(deferred_files)):
3173
3049
modified_path = fp.get_path(conflict[2])
3174
3050
modified_id = tt.final_file_id(conflict[2])
3175
3051
if len(conflict) == 3:
3176
yield conflicts.Conflict.factory(
3177
c_type, action=action, path=modified_path, file_id=modified_id)
3052
yield Conflict.factory(c_type, action=action, path=modified_path,
3053
file_id=modified_id)
3180
3056
conflicting_path = fp.get_path(conflict[3])
3181
3057
conflicting_id = tt.final_file_id(conflict[3])
3182
yield conflicts.Conflict.factory(
3183
c_type, action=action, path=modified_path,
3184
file_id=modified_id,
3185
conflict_path=conflicting_path,
3186
conflict_file_id=conflicting_id)
3058
yield Conflict.factory(c_type, action=action, path=modified_path,
3059
file_id=modified_id,
3060
conflict_path=conflicting_path,
3061
conflict_file_id=conflicting_id)
3189
3064
class _FileMover(object):