45
47
ExistingLimbo, ImmortalLimbo, NoFinalPath,
46
48
UnableCreateSymlink)
47
49
from bzrlib.filters import filtered_output_bytes, ContentFilterContext
48
from bzrlib.inventory import InventoryEntry
49
50
from bzrlib.osutils import (
57
57
supports_executable,
59
59
from bzrlib.progress import ProgressPhase
60
60
from bzrlib.symbol_versioning import (
65
from bzrlib.trace import mutter, warning
66
from bzrlib import tree
68
import bzrlib.urlutils as urlutils
71
67
ROOT_PARENT = "root-parent"
106
102
self._new_parent = {}
107
103
# mapping of trans_id with new contents -> new file_kind
108
104
self._new_contents = {}
105
# mapping of trans_id => (sha1 of content, stat_value)
106
self._observed_sha1s = {}
109
107
# Set of trans_ids whose contents will be removed
110
108
self._removed_contents = set()
111
109
# Mapping of trans_id -> new execute-bit value
629
629
conflicts.append(('versioning no contents', trans_id))
631
if not InventoryEntry.versionable_kind(kind):
631
if not inventory.InventoryEntry.versionable_kind(kind):
632
632
conflicts.append(('versioning bad kind', trans_id, kind))
666
666
if (self._new_name, self._new_parent) == ({}, {}):
668
668
for children in by_parent.itervalues():
669
name_ids = [(self.final_name(t), t) for t in children]
670
if not self._case_sensitive_target:
671
name_ids = [(n.lower(), t) for n, t in name_ids]
670
for child_tid in children:
671
name = self.final_name(child_tid)
673
# Keep children only if they still exist in the end
674
if not self._case_sensitive_target:
676
name_ids.append((name, child_tid))
674
679
last_trans_id = None
700
705
def _parent_type_conflicts(self, by_parent):
701
"""parents must have directory 'contents'."""
706
"""Children must have a directory parent"""
703
708
for parent_id, children in by_parent.iteritems():
704
709
if parent_id is ROOT_PARENT:
706
if not self._any_contents(children):
712
for child_id in children:
713
if self.final_kind(child_id) is not None:
718
# There is at least a child, so we need an existing directory to
708
720
kind = self.final_kind(parent_id)
722
# The directory will be deleted
710
723
conflicts.append(('missing parent', parent_id))
711
724
elif kind != "directory":
725
# Meh, we need a *directory* to put something in it
712
726
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
729
def _set_executability(self, path, trans_id):
723
730
"""Set the executability of versioned files """
724
731
if supports_executable():
748
755
def new_file(self, name, parent_id, contents, file_id=None,
756
executable=None, sha1=None):
750
757
"""Convenience method to create files.
752
759
name is the name of the file to create.
759
766
trans_id = self._new_entry(name, parent_id, file_id)
760
767
# TODO: rather than scheduling a set_executable call,
761
768
# have create_file create the file with the right mode.
762
self.create_file(contents, trans_id)
769
self.create_file(contents, trans_id, sha1=sha1)
763
770
if executable is not None:
764
771
self.set_executability(executable, trans_id)
817
824
# Find the potential orphans, stop if one item should be kept
818
for c in self.by_parent()[dir_id]:
819
if self.final_file_id(c) is None:
825
for child_tid in self.by_parent()[dir_id]:
826
if child_tid in self._removed_contents:
827
# The child is removed as part of the transform. Since it was
828
# versioned before, it's not an orphan
830
elif self.final_file_id(child_tid) is None:
831
# The child is not versioned
832
orphans.append(child_tid)
822
834
# We have a versioned file here, searching for orphans is
1236
1248
descendants.update(self._limbo_descendants(descendant))
1237
1249
return descendants
1239
def create_file(self, contents, trans_id, mode_id=None):
1251
def create_file(self, contents, trans_id, mode_id=None, sha1=None):
1240
1252
"""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.
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.
1250
1265
name = self._limbo_name(trans_id)
1251
1266
f = open(name, 'wb')
1259
1274
os.unlink(name)
1262
1276
f.writelines(contents)
1265
1279
self._set_mtime(name)
1266
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))
1268
1287
def _read_file_chunks(self, trans_id):
1269
1288
cur_file = open(self._limbo_name(trans_id), 'rb')
1328
1347
def cancel_creation(self, trans_id):
1329
1348
"""Cancel the creation of new file contents."""
1330
1349
del self._new_contents[trans_id]
1350
if trans_id in self._observed_sha1s:
1351
del self._observed_sha1s[trans_id]
1331
1352
children = self._limbo_children.get(trans_id)
1332
1353
# if this is a limbo directory with children, move them before removing
1333
1354
# the directory
1349
1370
if orphan_policy is None:
1350
1371
orphan_policy = default_policy
1351
1372
if orphan_policy not in orphaning_registry:
1352
trace.warning('%s (from %s) is not a known policy, defaulting to %s'
1353
% (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))
1354
1375
orphan_policy = default_policy
1355
1376
handle_orphan = orphaning_registry.get(orphan_policy)
1356
1377
handle_orphan(self, trans_id, parent_id)
1696
1718
def _generate_inventory_delta(self):
1697
1719
"""Generate an inventory delta for the current transform."""
1698
1720
inventory_delta = []
1699
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1721
child_pb = ui.ui_factory.nested_progress_bar()
1700
1722
new_paths = self._inventory_altered()
1701
1723
total_entries = len(new_paths) + len(self._removed_id)
1765
1787
tree_paths = list(self._tree_path_ids.iteritems())
1766
1788
tree_paths.sort(reverse=True)
1767
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1789
child_pb = ui.ui_factory.nested_progress_bar()
1769
1791
for num, data in enumerate(tree_paths):
1770
1792
path, trans_id = data
1799
1821
modified_paths = []
1800
1822
new_path_file_ids = dict((t, self.final_file_id(t)) for p, t in
1802
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1824
child_pb = ui.ui_factory.nested_progress_bar()
1804
1826
for num, (path, trans_id) in enumerate(new_paths):
1805
1827
if (num % 10) == 0:
1816
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.
1817
1842
if (trans_id in self._new_contents or
1818
1843
self.path_changed(trans_id)):
1819
1844
if trans_id in self._new_contents:
1820
1845
modified_paths.append(full_path)
1821
1846
if trans_id in self._new_executability:
1822
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)
1824
1853
child_pb.finished()
1825
1854
self._new_contents.clear()
1826
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)
1829
1881
class TransformPreview(DiskTreeTransform):
1830
1882
"""A TreeTransform for generating preview trees.
1880
1932
raise NotImplementedError(self.new_orphan)
1883
class _PreviewTree(tree.Tree):
1935
class _PreviewTree(tree.InventoryTree):
1884
1936
"""Partial implementation of Tree to support show_diff_trees"""
1886
1938
def __init__(self, transform):
2417
2469
if num > 0: # more than just a root
2418
2470
raise errors.WorkingTreeAlreadyPopulated(base=wt.basedir)
2419
2471
file_trans_id = {}
2420
top_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2472
top_pb = ui.ui_factory.nested_progress_bar()
2421
2473
pp = ProgressPhase("Build phase", 2, top_pb)
2422
2474
if tree.inventory.root is not None:
2423
2475
# This is kind of a hack: we should be altering the root
2489
2541
executable = tree.is_executable(file_id, tree_path)
2491
2543
tt.set_executability(executable, trans_id)
2492
trans_data = (trans_id, tree_path)
2544
trans_data = (trans_id, tree_path, entry.text_sha1)
2493
2545
deferred_contents.append((file_id, trans_data))
2495
2547
file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
2540
2592
unchanged = dict(unchanged)
2541
2593
new_desired_files = []
2543
for file_id, (trans_id, tree_path) in desired_files:
2595
for file_id, (trans_id, tree_path, text_sha1) in desired_files:
2544
2596
accelerator_path = unchanged.get(file_id)
2545
2597
if accelerator_path is None:
2546
new_desired_files.append((file_id, (trans_id, tree_path)))
2598
new_desired_files.append((file_id,
2599
(trans_id, tree_path, text_sha1)))
2548
2601
pb.update('Adding file contents', count + offset, total)
2556
2609
contents = filtered_output_bytes(contents, filters,
2557
2610
ContentFilterContext(tree_path, tree))
2559
tt.create_file(contents, trans_id)
2612
tt.create_file(contents, trans_id, sha1=text_sha1)
2562
2615
contents.close()
2567
2620
offset += count
2568
for count, ((trans_id, tree_path), contents) in enumerate(
2621
for count, ((trans_id, tree_path, text_sha1), contents) in enumerate(
2569
2622
tree.iter_files_bytes(new_desired_files)):
2570
2623
if wt.supports_content_filtering():
2571
2624
filters = wt._content_filter_stack(tree_path)
2572
2625
contents = filtered_output_bytes(contents, filters,
2573
2626
ContentFilterContext(tree_path, tree))
2574
tt.create_file(contents, trans_id)
2627
tt.create_file(contents, trans_id, sha1=text_sha1)
2575
2628
pb.update('Adding file contents', count + offset, total)
2766
2819
def _prepare_revert_transform(working_tree, target_tree, tt, filenames,
2767
2820
backups, pp, basis_tree=None,
2768
2821
merge_modified=None):
2769
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2822
child_pb = ui.ui_factory.nested_progress_bar()
2771
2824
if merge_modified is None:
2772
2825
merge_modified = working_tree.merge_modified()
2775
2828
merge_modified, basis_tree)
2777
2830
child_pb.finished()
2778
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2831
child_pb = ui.ui_factory.nested_progress_bar()
2780
2833
raw_conflicts = resolve_conflicts(tt, child_pb,
2781
2834
lambda t, c: conflict_pass(t, c, target_tree))
2986
3039
file_id = tt.final_file_id(trans_id)
2987
3040
if file_id is None:
2988
3041
file_id = tt.inactive_file_id(trans_id)
2989
entry = path_tree.inventory[file_id]
3042
_, entry = path_tree.iter_entries_by_dir(
2990
3044
# special-case the other tree root (move its
2991
3045
# children to current root)
2992
3046
if entry.parent_id is None:
3008
3062
file_id = tt.inactive_file_id(conflict[1])
3009
3063
# special-case the other tree root (move its children instead)
3010
3064
if path_tree and file_id in path_tree:
3011
if path_tree.inventory[file_id].parent_id is None:
3065
if path_tree.path2id('') == file_id:
3066
# This is the root entry, skip it
3013
3068
tt.version_file(file_id, conflict[1])
3014
3069
new_conflicts.add((c_type, 'Versioned directory', conflict[1]))