27
29
lazy_import.lazy_import(globals(), """
28
30
from bzrlib import (
38
40
revision as _mod_revision,
44
from bzrlib.i18n import gettext
43
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
46
from bzrlib.errors import (DuplicateKey, MalformedTransform,
44
47
ReusingTransform, CantMoveRoot,
45
48
ExistingLimbo, ImmortalLimbo, NoFinalPath,
46
49
UnableCreateSymlink)
47
50
from bzrlib.filters import filtered_output_bytes, ContentFilterContext
48
from bzrlib.inventory import InventoryEntry
49
51
from bzrlib.osutils import (
57
58
supports_executable,
59
60
from bzrlib.progress import ProgressPhase
60
61
from bzrlib.symbol_versioning import (
65
from bzrlib.trace import mutter, warning
66
from bzrlib import tree
68
import bzrlib.urlutils as urlutils
71
68
ROOT_PARENT = "root-parent"
386
393
return sorted(FinalPaths(self).get_paths(new_ids))
388
395
def _inventory_altered(self):
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,
396
"""Determine which trans_ids need new Inventory entries.
398
An new entry is needed when anything that would be reflected by an
399
inventory entry changes, including file name, file_id, parent file_id,
400
file kind, and the execute bit.
402
Some care is taken to return entries with real changes, not cases
403
where the value is deleted and then restored to its original value,
404
but some actually unchanged values may be returned.
406
:returns: A list of (path, trans_id) for all items requiring an
407
inventory change. Ordered by path.
410
# Find entries whose file_ids are new (or changed).
411
new_file_id = set(t for t in self._new_id
412
if self._new_id[t] != self.tree_file_id(t))
413
for id_set in [self._new_name, self._new_parent, new_file_id,
392
414
self._new_executability]:
393
new_ids.update(id_set)
415
changed_ids.update(id_set)
416
# removing implies a kind change
394
417
changed_kind = set(self._removed_contents)
395
419
changed_kind.intersection_update(self._new_contents)
396
changed_kind.difference_update(new_ids)
420
# Ignore entries that are already known to have changed.
421
changed_kind.difference_update(changed_ids)
422
# to keep only the truly changed ones
397
423
changed_kind = (t for t in changed_kind
398
424
if self.tree_kind(t) != self.final_kind(t))
399
new_ids.update(changed_kind)
400
return sorted(FinalPaths(self).get_paths(new_ids))
425
# all kind changes will alter the inventory
426
changed_ids.update(changed_kind)
427
# To find entries with changed parent_ids, find parents which existed,
428
# but changed file_id.
429
changed_file_id = set(t for t in new_file_id if t in self._removed_id)
430
# Now add all their children to the set.
431
for parent_trans_id in new_file_id:
432
changed_ids.update(self.iter_tree_children(parent_trans_id))
433
return sorted(FinalPaths(self).get_paths(changed_ids))
402
435
def final_kind(self, trans_id):
403
436
"""Determine the final file kind, after any changes applied.
700
738
def _parent_type_conflicts(self, by_parent):
701
"""parents must have directory 'contents'."""
739
"""Children must have a directory parent"""
703
741
for parent_id, children in by_parent.iteritems():
704
742
if parent_id is ROOT_PARENT:
706
if not self._any_contents(children):
745
for child_id in children:
746
if self.final_kind(child_id) is not None:
751
# There is at least a child, so we need an existing directory to
708
753
kind = self.final_kind(parent_id)
755
# The directory will be deleted
710
756
conflicts.append(('missing parent', parent_id))
711
757
elif kind != "directory":
758
# Meh, we need a *directory* to put something in it
712
759
conflicts.append(('non-directory parent', parent_id))
715
def _any_contents(self, trans_ids):
716
"""Return true if any of the trans_ids, will have contents."""
717
for trans_id in trans_ids:
718
if self.final_kind(trans_id) is not None:
722
762
def _set_executability(self, path, trans_id):
723
763
"""Set the executability of versioned files """
724
764
if supports_executable():
1236
1292
descendants.update(self._limbo_descendants(descendant))
1237
1293
return descendants
1239
def create_file(self, contents, trans_id, mode_id=None):
1295
def create_file(self, contents, trans_id, mode_id=None, sha1=None):
1240
1296
"""Schedule creation of a new file.
1244
Contents is an iterator of strings, all of which will be written
1245
to the target destination.
1247
New file takes the permissions of any existing file with that id,
1248
unless mode_id is specified.
1300
:param contents: an iterator of strings, all of which will be written
1301
to the target destination.
1302
:param trans_id: TreeTransform handle
1303
:param mode_id: If not None, force the mode of the target file to match
1304
the mode of the object referenced by mode_id.
1305
Otherwise, we will try to preserve mode bits of an existing file.
1306
:param sha1: If the sha1 of this content is already known, pass it in.
1307
We can use it to prevent future sha1 computations.
1250
1309
name = self._limbo_name(trans_id)
1251
1310
f = open(name, 'wb')
1254
unique_add(self._new_contents, trans_id, 'file')
1256
# Clean up the file, it never got registered so
1257
# TreeTransform.finalize() won't clean it up.
1312
unique_add(self._new_contents, trans_id, 'file')
1262
1313
f.writelines(contents)
1265
1316
self._set_mtime(name)
1266
1317
self._set_mode(trans_id, mode_id, S_ISREG)
1318
# It is unfortunate we have to use lstat instead of fstat, but we just
1319
# used utime and chmod on the file, so we need the accurate final
1321
if sha1 is not None:
1322
self._observed_sha1s[trans_id] = (sha1, osutils.lstat(name))
1268
1324
def _read_file_chunks(self, trans_id):
1269
1325
cur_file = open(self._limbo_name(trans_id), 'rb')
1765
1822
tree_paths = list(self._tree_path_ids.iteritems())
1766
1823
tree_paths.sort(reverse=True)
1767
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1824
child_pb = ui.ui_factory.nested_progress_bar()
1769
for num, data in enumerate(tree_paths):
1770
path, trans_id = data
1771
child_pb.update('removing file', num, len(tree_paths))
1826
for num, (path, trans_id) in enumerate(tree_paths):
1827
# do not attempt to move root into a subdirectory of itself.
1830
child_pb.update(gettext('removing file'), num, len(tree_paths))
1772
1831
full_path = self._tree.abspath(path)
1773
1832
if trans_id in self._removed_contents:
1774
1833
delete_path = os.path.join(self._deletiondir, trans_id)
1816
1875
self.rename_count += 1
1876
# TODO: if trans_id in self._observed_sha1s, we should
1877
# re-stat the final target, since ctime will be
1878
# updated by the change.
1817
1879
if (trans_id in self._new_contents or
1818
1880
self.path_changed(trans_id)):
1819
1881
if trans_id in self._new_contents:
1820
1882
modified_paths.append(full_path)
1821
1883
if trans_id in self._new_executability:
1822
1884
self._set_executability(path, trans_id)
1885
if trans_id in self._observed_sha1s:
1886
o_sha1, o_st_val = self._observed_sha1s[trans_id]
1887
st = osutils.lstat(full_path)
1888
self._observed_sha1s[trans_id] = (o_sha1, st)
1824
1890
child_pb.finished()
1891
for path, trans_id in new_paths:
1892
# new_paths includes stuff like workingtree conflicts. Only the
1893
# stuff in new_contents actually comes from limbo.
1894
if trans_id in self._limbo_files:
1895
del self._limbo_files[trans_id]
1825
1896
self._new_contents.clear()
1826
1897
return modified_paths
1899
def _apply_observed_sha1s(self):
1900
"""After we have finished renaming everything, update observed sha1s
1902
This has to be done after self._tree.apply_inventory_delta, otherwise
1903
it doesn't know anything about the files we are updating. Also, we want
1904
to do this as late as possible, so that most entries end up cached.
1906
# TODO: this doesn't update the stat information for directories. So
1907
# the first 'bzr status' will still need to rewrite
1908
# .bzr/checkout/dirstate. However, we at least don't need to
1909
# re-read all of the files.
1910
# TODO: If the operation took a while, we could do a time.sleep(3) here
1911
# to allow the clock to tick over and ensure we won't have any
1912
# problems. (we could observe start time, and finish time, and if
1913
# it is less than eg 10% overhead, add a sleep call.)
1914
paths = FinalPaths(self)
1915
for trans_id, observed in self._observed_sha1s.iteritems():
1916
path = paths.get_path(trans_id)
1917
# We could get the file_id, but dirstate prefers to use the path
1918
# anyway, and it is 'cheaper' to determine.
1919
# file_id = self._new_id[trans_id]
1920
self._tree._observed_sha1(None, path, observed)
1829
1923
class TransformPreview(DiskTreeTransform):
1830
1924
"""A TreeTransform for generating preview trees.
2148
2242
def get_file_size(self, file_id):
2149
2243
"""See Tree.get_file_size"""
2244
trans_id = self._transform.trans_id_file_id(file_id)
2245
kind = self._transform.final_kind(trans_id)
2248
if trans_id in self._transform._new_contents:
2249
return self._stat_limbo_file(trans_id=trans_id).st_size
2150
2250
if self.kind(file_id) == 'file':
2151
2251
return self._transform._tree.get_file_size(file_id)
2255
def get_file_verifier(self, file_id, path=None, stat_value=None):
2256
trans_id = self._transform.trans_id_file_id(file_id)
2257
kind = self._transform._new_contents.get(trans_id)
2259
return self._transform._tree.get_file_verifier(file_id)
2261
fileobj = self.get_file(file_id)
2263
return ("SHA1", sha_file(fileobj))
2155
2267
def get_file_sha1(self, file_id, path=None, stat_value=None):
2156
2268
trans_id = self._transform.trans_id_file_id(file_id)
2157
2269
kind = self._transform._new_contents.get(trans_id)
2540
2661
unchanged = dict(unchanged)
2541
2662
new_desired_files = []
2543
for file_id, (trans_id, tree_path) in desired_files:
2664
for file_id, (trans_id, tree_path, text_sha1) in desired_files:
2544
2665
accelerator_path = unchanged.get(file_id)
2545
2666
if accelerator_path is None:
2546
new_desired_files.append((file_id, (trans_id, tree_path)))
2667
new_desired_files.append((file_id,
2668
(trans_id, tree_path, text_sha1)))
2548
pb.update('Adding file contents', count + offset, total)
2670
pb.update(gettext('Adding file contents'), count + offset, total)
2550
2672
tt.create_hardlink(accelerator_tree.abspath(accelerator_path),
2567
2689
offset += count
2568
for count, ((trans_id, tree_path), contents) in enumerate(
2690
for count, ((trans_id, tree_path, text_sha1), contents) in enumerate(
2569
2691
tree.iter_files_bytes(new_desired_files)):
2570
2692
if wt.supports_content_filtering():
2571
2693
filters = wt._content_filter_stack(tree_path)
2572
2694
contents = filtered_output_bytes(contents, filters,
2573
2695
ContentFilterContext(tree_path, tree))
2574
tt.create_file(contents, trans_id)
2575
pb.update('Adding file contents', count + offset, total)
2696
tt.create_file(contents, trans_id, sha1=text_sha1)
2697
pb.update(gettext('Adding file contents'), count + offset, total)
2578
2700
def _reparent_children(tt, old_parent, new_parent):
2710
2832
return new_name
2713
def _entry_changes(file_id, entry, working_tree):
2714
"""Determine in which ways the inventory entry has changed.
2716
Returns booleans: has_contents, content_mod, meta_mod
2717
has_contents means there are currently contents, but they differ
2718
contents_mod means contents need to be modified
2719
meta_mod means the metadata needs to be modified
2721
cur_entry = working_tree.inventory[file_id]
2723
working_kind = working_tree.kind(file_id)
2726
has_contents = False
2729
if has_contents is True:
2730
if entry.kind != working_kind:
2731
contents_mod, meta_mod = True, False
2733
cur_entry._read_tree_state(working_tree.id2path(file_id),
2735
contents_mod, meta_mod = entry.detect_changes(cur_entry)
2736
cur_entry._forget_tree_state()
2737
return has_contents, contents_mod, meta_mod
2740
2835
def revert(working_tree, target_tree, filenames, backups=False,
2741
2836
pb=None, change_reporter=None):
2742
2837
"""Revert a working tree's contents to those of a target tree."""
2799
2898
deferred_files = []
2800
2899
for id_num, (file_id, path, changed_content, versioned, parent, name,
2801
2900
kind, executable) in enumerate(change_list):
2802
if skip_root and file_id[0] is not None and parent[0] is None:
2901
target_path, wt_path = path
2902
target_versioned, wt_versioned = versioned
2903
target_parent, wt_parent = parent
2904
target_name, wt_name = name
2905
target_kind, wt_kind = kind
2906
target_executable, wt_executable = executable
2907
if skip_root and wt_parent is None:
2804
2909
trans_id = tt.trans_id_file_id(file_id)
2806
2911
if changed_content:
2807
2912
keep_content = False
2808
if kind[0] == 'file' and (backups or kind[1] is None):
2913
if wt_kind == 'file' and (backups or target_kind is None):
2809
2914
wt_sha1 = working_tree.get_file_sha1(file_id)
2810
2915
if merge_modified.get(file_id) != wt_sha1:
2811
2916
# acquire the basis tree lazily to prevent the
2814
2919
if basis_tree is None:
2815
2920
basis_tree = working_tree.basis_tree()
2816
2921
basis_tree.lock_read()
2817
if file_id in basis_tree:
2922
if basis_tree.has_id(file_id):
2818
2923
if wt_sha1 != basis_tree.get_file_sha1(file_id):
2819
2924
keep_content = True
2820
elif kind[1] is None and not versioned[1]:
2925
elif target_kind is None and not target_versioned:
2821
2926
keep_content = True
2822
if kind[0] is not None:
2927
if wt_kind is not None:
2823
2928
if not keep_content:
2824
2929
tt.delete_contents(trans_id)
2825
elif kind[1] is not None:
2826
parent_trans_id = tt.trans_id_file_id(parent[0])
2930
elif target_kind is not None:
2931
parent_trans_id = tt.trans_id_file_id(wt_parent)
2827
2932
backup_name = tt._available_backup_name(
2828
name[0], parent_trans_id)
2933
wt_name, parent_trans_id)
2829
2934
tt.adjust_path(backup_name, parent_trans_id, trans_id)
2830
new_trans_id = tt.create_path(name[0], parent_trans_id)
2831
if versioned == (True, True):
2935
new_trans_id = tt.create_path(wt_name, parent_trans_id)
2936
if wt_versioned and target_versioned:
2832
2937
tt.unversion_file(trans_id)
2833
2938
tt.version_file(file_id, new_trans_id)
2834
2939
# New contents should have the same unix perms as old
2836
2941
mode_id = trans_id
2837
2942
trans_id = new_trans_id
2838
if kind[1] in ('directory', 'tree-reference'):
2943
if target_kind in ('directory', 'tree-reference'):
2839
2944
tt.create_directory(trans_id)
2840
if kind[1] == 'tree-reference':
2945
if target_kind == 'tree-reference':
2841
2946
revision = target_tree.get_reference_revision(file_id,
2843
2948
tt.set_tree_reference(revision, trans_id)
2844
elif kind[1] == 'symlink':
2949
elif target_kind == 'symlink':
2845
2950
tt.create_symlink(target_tree.get_symlink_target(file_id),
2847
elif kind[1] == 'file':
2952
elif target_kind == 'file':
2848
2953
deferred_files.append((file_id, (trans_id, mode_id)))
2849
2954
if basis_tree is None:
2850
2955
basis_tree = working_tree.basis_tree()
2851
2956
basis_tree.lock_read()
2852
2957
new_sha1 = target_tree.get_file_sha1(file_id)
2853
if (file_id in basis_tree and new_sha1 ==
2854
basis_tree.get_file_sha1(file_id)):
2958
if (basis_tree.has_id(file_id) and
2959
new_sha1 == basis_tree.get_file_sha1(file_id)):
2855
2960
if file_id in merge_modified:
2856
2961
del merge_modified[file_id]
2858
2963
merge_modified[file_id] = new_sha1
2860
2965
# preserve the execute bit when backing up
2861
if keep_content and executable[0] == executable[1]:
2862
tt.set_executability(executable[1], trans_id)
2863
elif kind[1] is not None:
2864
raise AssertionError(kind[1])
2865
if versioned == (False, True):
2966
if keep_content and wt_executable == target_executable:
2967
tt.set_executability(target_executable, trans_id)
2968
elif target_kind is not None:
2969
raise AssertionError(target_kind)
2970
if not wt_versioned and target_versioned:
2866
2971
tt.version_file(file_id, trans_id)
2867
if versioned == (True, False):
2972
if wt_versioned and not target_versioned:
2868
2973
tt.unversion_file(trans_id)
2869
if (name[1] is not None and
2870
(name[0] != name[1] or parent[0] != parent[1])):
2871
if name[1] == '' and parent[1] is None:
2974
if (target_name is not None and
2975
(wt_name != target_name or wt_parent != target_parent)):
2976
if target_name == '' and target_parent is None:
2872
2977
parent_trans = ROOT_PARENT
2874
parent_trans = tt.trans_id_file_id(parent[1])
2875
if parent[0] is None and versioned[0]:
2876
tt.adjust_root_path(name[1], parent_trans)
2979
parent_trans = tt.trans_id_file_id(target_parent)
2980
if wt_parent is None and wt_versioned:
2981
tt.adjust_root_path(target_name, parent_trans)
2878
tt.adjust_path(name[1], parent_trans, trans_id)
2879
if executable[0] != executable[1] and kind[1] == "file":
2880
tt.set_executability(executable[1], trans_id)
2983
tt.adjust_path(target_name, parent_trans, trans_id)
2984
if wt_executable != target_executable and target_kind == "file":
2985
tt.set_executability(target_executable, trans_id)
2881
2986
if working_tree.supports_content_filtering():
2882
2987
for index, ((trans_id, mode_id), bytes) in enumerate(
2883
2988
target_tree.iter_files_bytes(deferred_files)):
3044
3149
modified_path = fp.get_path(conflict[2])
3045
3150
modified_id = tt.final_file_id(conflict[2])
3046
3151
if len(conflict) == 3:
3047
yield Conflict.factory(c_type, action=action, path=modified_path,
3048
file_id=modified_id)
3152
yield conflicts.Conflict.factory(
3153
c_type, action=action, path=modified_path, file_id=modified_id)
3051
3156
conflicting_path = fp.get_path(conflict[3])
3052
3157
conflicting_id = tt.final_file_id(conflict[3])
3053
yield Conflict.factory(c_type, action=action, path=modified_path,
3054
file_id=modified_id,
3055
conflict_path=conflicting_path,
3056
conflict_file_id=conflicting_id)
3158
yield conflicts.Conflict.factory(
3159
c_type, action=action, path=modified_path,
3160
file_id=modified_id,
3161
conflict_path=conflicting_path,
3162
conflict_file_id=conflicting_id)
3059
3165
class _FileMover(object):