1
# Copyright (C) 2006-2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
from stat import S_ISREG, S_IEXEC
22
from bzrlib.lazy_import import lazy_import
23
lazy_import(globals(), """
34
revision as _mod_revision,
38
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
39
ReusingTransform, CantMoveRoot,
40
ExistingLimbo, ImmortalLimbo, NoFinalPath,
42
from bzrlib.filters import filtered_output_bytes, ContentFilterContext
43
from bzrlib.inventory import InventoryEntry
44
from bzrlib.osutils import (
54
from bzrlib.progress import ProgressPhase
55
from bzrlib.symbol_versioning import (
59
from bzrlib.trace import mutter, warning
60
from bzrlib import tree
62
import bzrlib.urlutils as urlutils
65
ROOT_PARENT = "root-parent"
68
def unique_add(map, key, value):
70
raise DuplicateKey(key=key)
74
class _TransformResults(object):
75
def __init__(self, modified_paths, rename_count):
77
self.modified_paths = modified_paths
78
self.rename_count = rename_count
81
class TreeTransformBase(object):
82
"""The base class for TreeTransform and its kin."""
84
def __init__(self, tree, pb=None,
88
:param tree: The tree that will be transformed, but not necessarily
91
:param case_sensitive: If True, the target of the transform is
92
case sensitive, not just case preserving.
97
# mapping of trans_id -> new basename
99
# mapping of trans_id -> new parent trans_id
100
self._new_parent = {}
101
# mapping of trans_id with new contents -> new file_kind
102
self._new_contents = {}
103
# Set of trans_ids whose contents will be removed
104
self._removed_contents = set()
105
# Mapping of trans_id -> new execute-bit value
106
self._new_executability = {}
107
# Mapping of trans_id -> new tree-reference value
108
self._new_reference_revision = {}
109
# Mapping of trans_id -> new file_id
111
# Mapping of old file-id -> trans_id
112
self._non_present_ids = {}
113
# Mapping of new file_id -> trans_id
115
# Set of trans_ids that will be removed
116
self._removed_id = set()
117
# Mapping of path in old tree -> trans_id
118
self._tree_path_ids = {}
119
# Mapping trans_id -> path in old tree
120
self._tree_id_paths = {}
121
# The trans_id that will be used as the tree root
122
root_id = tree.get_root_id()
123
if root_id is not None:
124
self._new_root = self.trans_id_tree_file_id(root_id)
126
self._new_root = None
127
# Indictor of whether the transform has been applied
131
# Whether the target is case sensitive
132
self._case_sensitive_target = case_sensitive
133
# A counter of how many files have been renamed
134
self.rename_count = 0
137
"""Release the working tree lock, if held.
139
This is required if apply has not been invoked, but can be invoked
142
if self._tree is None:
147
def __get_root(self):
148
return self._new_root
150
root = property(__get_root)
152
def _assign_id(self):
153
"""Produce a new tranform id"""
154
new_id = "new-%s" % self._id_number
158
def create_path(self, name, parent):
159
"""Assign a transaction id to a new path"""
160
trans_id = self._assign_id()
161
unique_add(self._new_name, trans_id, name)
162
unique_add(self._new_parent, trans_id, parent)
165
def adjust_path(self, name, parent, trans_id):
166
"""Change the path that is assigned to a transaction id."""
168
raise ValueError("Parent trans-id may not be None")
169
if trans_id == self._new_root:
171
self._new_name[trans_id] = name
172
self._new_parent[trans_id] = parent
174
def adjust_root_path(self, name, parent):
175
"""Emulate moving the root by moving all children, instead.
177
We do this by undoing the association of root's transaction id with the
178
current tree. This allows us to create a new directory with that
179
transaction id. We unversion the root directory and version the
180
physically new directory, and hope someone versions the tree root
183
old_root = self._new_root
184
old_root_file_id = self.final_file_id(old_root)
185
# force moving all children of root
186
for child_id in self.iter_tree_children(old_root):
187
if child_id != parent:
188
self.adjust_path(self.final_name(child_id),
189
self.final_parent(child_id), child_id)
190
file_id = self.final_file_id(child_id)
191
if file_id is not None:
192
self.unversion_file(child_id)
193
self.version_file(file_id, child_id)
195
# the physical root needs a new transaction id
196
self._tree_path_ids.pop("")
197
self._tree_id_paths.pop(old_root)
198
self._new_root = self.trans_id_tree_file_id(self._tree.get_root_id())
199
if parent == old_root:
200
parent = self._new_root
201
self.adjust_path(name, parent, old_root)
202
self.create_directory(old_root)
203
self.version_file(old_root_file_id, old_root)
204
self.unversion_file(self._new_root)
206
def fixup_new_roots(self):
207
"""Reinterpret requests to change the root directory
209
Instead of creating a root directory, or moving an existing directory,
210
all the attributes and children of the new root are applied to the
211
existing root directory.
213
This means that the old root trans-id becomes obsolete, so it is
214
recommended only to invoke this after the root trans-id has become
217
new_roots = [k for k, v in self._new_parent.iteritems() if v is
219
if len(new_roots) < 1:
221
if len(new_roots) != 1:
222
raise ValueError('A tree cannot have two roots!')
223
if self._new_root is None:
224
self._new_root = new_roots[0]
226
old_new_root = new_roots[0]
227
# TODO: What to do if a old_new_root is present, but self._new_root is
228
# not listed as being removed? This code explicitly unversions
229
# the old root and versions it with the new file_id. Though that
230
# seems like an incomplete delta
232
# unversion the new root's directory.
233
file_id = self.final_file_id(old_new_root)
234
if old_new_root in self._new_id:
235
self.cancel_versioning(old_new_root)
237
self.unversion_file(old_new_root)
238
# if, at this stage, root still has an old file_id, zap it so we can
239
# stick a new one in.
240
if (self.tree_file_id(self._new_root) is not None and
241
self._new_root not in self._removed_id):
242
self.unversion_file(self._new_root)
243
self.version_file(file_id, self._new_root)
245
# Now move children of new root into old root directory.
246
# Ensure all children are registered with the transaction, but don't
247
# use directly-- some tree children have new parents
248
list(self.iter_tree_children(old_new_root))
249
# Move all children of new root into old root directory.
250
for child in self.by_parent().get(old_new_root, []):
251
self.adjust_path(self.final_name(child), self._new_root, child)
253
# Ensure old_new_root has no directory.
254
if old_new_root in self._new_contents:
255
self.cancel_creation(old_new_root)
257
self.delete_contents(old_new_root)
259
# prevent deletion of root directory.
260
if self._new_root in self._removed_contents:
261
self.cancel_deletion(self._new_root)
263
# destroy path info for old_new_root.
264
del self._new_parent[old_new_root]
265
del self._new_name[old_new_root]
267
def trans_id_tree_file_id(self, inventory_id):
268
"""Determine the transaction id of a working tree file.
270
This reflects only files that already exist, not ones that will be
271
added by transactions.
273
if inventory_id is None:
274
raise ValueError('None is not a valid file id')
275
path = self._tree.id2path(inventory_id)
276
return self.trans_id_tree_path(path)
278
def trans_id_file_id(self, file_id):
279
"""Determine or set the transaction id associated with a file ID.
280
A new id is only created for file_ids that were never present. If
281
a transaction has been unversioned, it is deliberately still returned.
282
(this will likely lead to an unversioned parent conflict.)
285
raise ValueError('None is not a valid file id')
286
if file_id in self._r_new_id and self._r_new_id[file_id] is not None:
287
return self._r_new_id[file_id]
290
self._tree.iter_entries_by_dir([file_id]).next()
291
except StopIteration:
292
if file_id in self._non_present_ids:
293
return self._non_present_ids[file_id]
295
trans_id = self._assign_id()
296
self._non_present_ids[file_id] = trans_id
299
return self.trans_id_tree_file_id(file_id)
301
def trans_id_tree_path(self, path):
302
"""Determine (and maybe set) the transaction ID for a tree path."""
303
path = self.canonical_path(path)
304
if path not in self._tree_path_ids:
305
self._tree_path_ids[path] = self._assign_id()
306
self._tree_id_paths[self._tree_path_ids[path]] = path
307
return self._tree_path_ids[path]
309
def get_tree_parent(self, trans_id):
310
"""Determine id of the parent in the tree."""
311
path = self._tree_id_paths[trans_id]
314
return self.trans_id_tree_path(os.path.dirname(path))
316
def delete_contents(self, trans_id):
317
"""Schedule the contents of a path entry for deletion"""
318
# Ensure that the object exists in the WorkingTree, this will raise an
319
# exception if there is a problem
320
self.tree_kind(trans_id)
321
self._removed_contents.add(trans_id)
323
def cancel_deletion(self, trans_id):
324
"""Cancel a scheduled deletion"""
325
self._removed_contents.remove(trans_id)
327
def unversion_file(self, trans_id):
328
"""Schedule a path entry to become unversioned"""
329
self._removed_id.add(trans_id)
331
def delete_versioned(self, trans_id):
332
"""Delete and unversion a versioned file"""
333
self.delete_contents(trans_id)
334
self.unversion_file(trans_id)
336
def set_executability(self, executability, trans_id):
337
"""Schedule setting of the 'execute' bit
338
To unschedule, set to None
340
if executability is None:
341
del self._new_executability[trans_id]
343
unique_add(self._new_executability, trans_id, executability)
345
def set_tree_reference(self, revision_id, trans_id):
346
"""Set the reference associated with a directory"""
347
unique_add(self._new_reference_revision, trans_id, revision_id)
349
def version_file(self, file_id, trans_id):
350
"""Schedule a file to become versioned."""
353
unique_add(self._new_id, trans_id, file_id)
354
unique_add(self._r_new_id, file_id, trans_id)
356
def cancel_versioning(self, trans_id):
357
"""Undo a previous versioning of a file"""
358
file_id = self._new_id[trans_id]
359
del self._new_id[trans_id]
360
del self._r_new_id[file_id]
362
def new_paths(self, filesystem_only=False):
363
"""Determine the paths of all new and changed files.
365
:param filesystem_only: if True, only calculate values for files
366
that require renames or execute bit changes.
370
stale_ids = self._needs_rename.difference(self._new_name)
371
stale_ids.difference_update(self._new_parent)
372
stale_ids.difference_update(self._new_contents)
373
stale_ids.difference_update(self._new_id)
374
needs_rename = self._needs_rename.difference(stale_ids)
375
id_sets = (needs_rename, self._new_executability)
377
id_sets = (self._new_name, self._new_parent, self._new_contents,
378
self._new_id, self._new_executability)
379
for id_set in id_sets:
380
new_ids.update(id_set)
381
return sorted(FinalPaths(self).get_paths(new_ids))
383
def _inventory_altered(self):
384
"""Get the trans_ids and paths of files needing new inv entries."""
386
for id_set in [self._new_name, self._new_parent, self._new_id,
387
self._new_executability]:
388
new_ids.update(id_set)
389
changed_kind = set(self._removed_contents)
390
changed_kind.intersection_update(self._new_contents)
391
changed_kind.difference_update(new_ids)
392
changed_kind = (t for t in changed_kind if self.tree_kind(t) !=
394
new_ids.update(changed_kind)
395
return sorted(FinalPaths(self).get_paths(new_ids))
397
def final_kind(self, trans_id):
398
"""Determine the final file kind, after any changes applied.
400
Raises NoSuchFile if the file does not exist/has no contents.
401
(It is conceivable that a path would be created without the
402
corresponding contents insertion command)
404
if trans_id in self._new_contents:
405
return self._new_contents[trans_id]
406
elif trans_id in self._removed_contents:
407
raise NoSuchFile(None)
409
return self.tree_kind(trans_id)
411
def tree_file_id(self, trans_id):
412
"""Determine the file id associated with the trans_id in the tree"""
414
path = self._tree_id_paths[trans_id]
416
# the file is a new, unversioned file, or invalid trans_id
418
# the file is old; the old id is still valid
419
if self._new_root == trans_id:
420
return self._tree.get_root_id()
421
return self._tree.path2id(path)
423
def final_file_id(self, trans_id):
424
"""Determine the file id after any changes are applied, or None.
426
None indicates that the file will not be versioned after changes are
430
return self._new_id[trans_id]
432
if trans_id in self._removed_id:
434
return self.tree_file_id(trans_id)
436
def inactive_file_id(self, trans_id):
437
"""Return the inactive file_id associated with a transaction id.
438
That is, the one in the tree or in non_present_ids.
439
The file_id may actually be active, too.
441
file_id = self.tree_file_id(trans_id)
442
if file_id is not None:
444
for key, value in self._non_present_ids.iteritems():
445
if value == trans_id:
448
def final_parent(self, trans_id):
449
"""Determine the parent file_id, after any changes are applied.
451
ROOT_PARENT is returned for the tree root.
454
return self._new_parent[trans_id]
456
return self.get_tree_parent(trans_id)
458
def final_name(self, trans_id):
459
"""Determine the final filename, after all changes are applied."""
461
return self._new_name[trans_id]
464
return os.path.basename(self._tree_id_paths[trans_id])
466
raise NoFinalPath(trans_id, self)
469
"""Return a map of parent: children for known parents.
471
Only new paths and parents of tree files with assigned ids are used.
474
items = list(self._new_parent.iteritems())
475
items.extend((t, self.final_parent(t)) for t in
476
self._tree_id_paths.keys())
477
for trans_id, parent_id in items:
478
if parent_id not in by_parent:
479
by_parent[parent_id] = set()
480
by_parent[parent_id].add(trans_id)
483
def path_changed(self, trans_id):
484
"""Return True if a trans_id's path has changed."""
485
return (trans_id in self._new_name) or (trans_id in self._new_parent)
487
def new_contents(self, trans_id):
488
return (trans_id in self._new_contents)
490
def find_conflicts(self):
491
"""Find any violations of inventory or filesystem invariants"""
492
if self._done is True:
493
raise ReusingTransform()
495
# ensure all children of all existent parents are known
496
# all children of non-existent parents are known, by definition.
497
self._add_tree_children()
498
by_parent = self.by_parent()
499
conflicts.extend(self._unversioned_parents(by_parent))
500
conflicts.extend(self._parent_loops())
501
conflicts.extend(self._duplicate_entries(by_parent))
502
conflicts.extend(self._duplicate_ids())
503
conflicts.extend(self._parent_type_conflicts(by_parent))
504
conflicts.extend(self._improper_versioning())
505
conflicts.extend(self._executability_conflicts())
506
conflicts.extend(self._overwrite_conflicts())
509
def _check_malformed(self):
510
conflicts = self.find_conflicts()
511
if len(conflicts) != 0:
512
raise MalformedTransform(conflicts=conflicts)
514
def _add_tree_children(self):
515
"""Add all the children of all active parents to the known paths.
517
Active parents are those which gain children, and those which are
518
removed. This is a necessary first step in detecting conflicts.
520
parents = self.by_parent().keys()
521
parents.extend([t for t in self._removed_contents if
522
self.tree_kind(t) == 'directory'])
523
for trans_id in self._removed_id:
524
file_id = self.tree_file_id(trans_id)
525
if file_id is not None:
526
if self._tree.inventory[file_id].kind == 'directory':
527
parents.append(trans_id)
528
elif self.tree_kind(trans_id) == 'directory':
529
parents.append(trans_id)
531
for parent_id in parents:
532
# ensure that all children are registered with the transaction
533
list(self.iter_tree_children(parent_id))
535
def has_named_child(self, by_parent, parent_id, name):
537
children = by_parent[parent_id]
540
for child in children:
541
if self.final_name(child) == name:
544
path = self._tree_id_paths[parent_id]
547
childpath = joinpath(path, name)
548
child_id = self._tree_path_ids.get(childpath)
550
return lexists(self._tree.abspath(childpath))
552
if self.final_parent(child_id) != parent_id:
554
if child_id in self._removed_contents:
555
# XXX What about dangling file-ids?
560
def _parent_loops(self):
561
"""No entry should be its own ancestor"""
563
for trans_id in self._new_parent:
566
while parent_id is not ROOT_PARENT:
569
parent_id = self.final_parent(parent_id)
572
if parent_id == trans_id:
573
conflicts.append(('parent loop', trans_id))
574
if parent_id in seen:
578
def _unversioned_parents(self, by_parent):
579
"""If parent directories are versioned, children must be versioned."""
581
for parent_id, children in by_parent.iteritems():
582
if parent_id is ROOT_PARENT:
584
if self.final_file_id(parent_id) is not None:
586
for child_id in children:
587
if self.final_file_id(child_id) is not None:
588
conflicts.append(('unversioned parent', parent_id))
592
def _improper_versioning(self):
593
"""Cannot version a file with no contents, or a bad type.
595
However, existing entries with no contents are okay.
598
for trans_id in self._new_id.iterkeys():
600
kind = self.final_kind(trans_id)
602
conflicts.append(('versioning no contents', trans_id))
604
if not InventoryEntry.versionable_kind(kind):
605
conflicts.append(('versioning bad kind', trans_id, kind))
608
def _executability_conflicts(self):
609
"""Check for bad executability changes.
611
Only versioned files may have their executability set, because
612
1. only versioned entries can have executability under windows
613
2. only files can be executable. (The execute bit on a directory
614
does not indicate searchability)
617
for trans_id in self._new_executability:
618
if self.final_file_id(trans_id) is None:
619
conflicts.append(('unversioned executability', trans_id))
622
non_file = self.final_kind(trans_id) != "file"
626
conflicts.append(('non-file executability', trans_id))
629
def _overwrite_conflicts(self):
630
"""Check for overwrites (not permitted on Win32)"""
632
for trans_id in self._new_contents:
634
self.tree_kind(trans_id)
637
if trans_id not in self._removed_contents:
638
conflicts.append(('overwrite', trans_id,
639
self.final_name(trans_id)))
642
def _duplicate_entries(self, by_parent):
643
"""No directory may have two entries with the same name."""
645
if (self._new_name, self._new_parent) == ({}, {}):
647
for children in by_parent.itervalues():
648
name_ids = [(self.final_name(t), t) for t in children]
649
if not self._case_sensitive_target:
650
name_ids = [(n.lower(), t) for n, t in name_ids]
654
for name, trans_id in name_ids:
656
kind = self.final_kind(trans_id)
659
file_id = self.final_file_id(trans_id)
660
if kind is None and file_id is None:
662
if name == last_name:
663
conflicts.append(('duplicate', last_trans_id, trans_id,
666
last_trans_id = trans_id
669
def _duplicate_ids(self):
670
"""Each inventory id may only be used once"""
672
removed_tree_ids = set((self.tree_file_id(trans_id) for trans_id in
674
all_ids = self._tree.all_file_ids()
675
active_tree_ids = all_ids.difference(removed_tree_ids)
676
for trans_id, file_id in self._new_id.iteritems():
677
if file_id in active_tree_ids:
678
old_trans_id = self.trans_id_tree_file_id(file_id)
679
conflicts.append(('duplicate id', old_trans_id, trans_id))
682
def _parent_type_conflicts(self, by_parent):
683
"""parents must have directory 'contents'."""
685
for parent_id, children in by_parent.iteritems():
686
if parent_id is ROOT_PARENT:
688
if not self._any_contents(children):
690
for child in children:
692
self.final_kind(child)
696
kind = self.final_kind(parent_id)
700
conflicts.append(('missing parent', parent_id))
701
elif kind != "directory":
702
conflicts.append(('non-directory parent', parent_id))
705
def _any_contents(self, trans_ids):
706
"""Return true if any of the trans_ids, will have contents."""
707
for trans_id in trans_ids:
709
kind = self.final_kind(trans_id)
715
def _set_executability(self, path, trans_id):
716
"""Set the executability of versioned files """
717
if supports_executable():
718
new_executability = self._new_executability[trans_id]
719
abspath = self._tree.abspath(path)
720
current_mode = os.stat(abspath).st_mode
721
if new_executability:
724
to_mode = current_mode | (0100 & ~umask)
725
# Enable x-bit for others only if they can read it.
726
if current_mode & 0004:
727
to_mode |= 0001 & ~umask
728
if current_mode & 0040:
729
to_mode |= 0010 & ~umask
731
to_mode = current_mode & ~0111
732
os.chmod(abspath, to_mode)
734
def _new_entry(self, name, parent_id, file_id):
735
"""Helper function to create a new filesystem entry."""
736
trans_id = self.create_path(name, parent_id)
737
if file_id is not None:
738
self.version_file(file_id, trans_id)
741
def new_file(self, name, parent_id, contents, file_id=None,
743
"""Convenience method to create files.
745
name is the name of the file to create.
746
parent_id is the transaction id of the parent directory of the file.
747
contents is an iterator of bytestrings, which will be used to produce
749
:param file_id: The inventory ID of the file, if it is to be versioned.
750
:param executable: Only valid when a file_id has been supplied.
752
trans_id = self._new_entry(name, parent_id, file_id)
753
# TODO: rather than scheduling a set_executable call,
754
# have create_file create the file with the right mode.
755
self.create_file(contents, trans_id)
756
if executable is not None:
757
self.set_executability(executable, trans_id)
760
def new_directory(self, name, parent_id, file_id=None):
761
"""Convenience method to create directories.
763
name is the name of the directory to create.
764
parent_id is the transaction id of the parent directory of the
766
file_id is the inventory ID of the directory, if it is to be versioned.
768
trans_id = self._new_entry(name, parent_id, file_id)
769
self.create_directory(trans_id)
772
def new_symlink(self, name, parent_id, target, file_id=None):
773
"""Convenience method to create symbolic link.
775
name is the name of the symlink to create.
776
parent_id is the transaction id of the parent directory of the symlink.
777
target is a bytestring of the target of the symlink.
778
file_id is the inventory ID of the file, if it is to be versioned.
780
trans_id = self._new_entry(name, parent_id, file_id)
781
self.create_symlink(target, trans_id)
784
def _affected_ids(self):
785
"""Return the set of transform ids affected by the transform"""
786
trans_ids = set(self._removed_id)
787
trans_ids.update(self._new_id.keys())
788
trans_ids.update(self._removed_contents)
789
trans_ids.update(self._new_contents.keys())
790
trans_ids.update(self._new_executability.keys())
791
trans_ids.update(self._new_name.keys())
792
trans_ids.update(self._new_parent.keys())
795
def _get_file_id_maps(self):
796
"""Return mapping of file_ids to trans_ids in the to and from states"""
797
trans_ids = self._affected_ids()
800
# Build up two dicts: trans_ids associated with file ids in the
801
# FROM state, vs the TO state.
802
for trans_id in trans_ids:
803
from_file_id = self.tree_file_id(trans_id)
804
if from_file_id is not None:
805
from_trans_ids[from_file_id] = trans_id
806
to_file_id = self.final_file_id(trans_id)
807
if to_file_id is not None:
808
to_trans_ids[to_file_id] = trans_id
809
return from_trans_ids, to_trans_ids
811
def _from_file_data(self, from_trans_id, from_versioned, file_id):
812
"""Get data about a file in the from (tree) state
814
Return a (name, parent, kind, executable) tuple
816
from_path = self._tree_id_paths.get(from_trans_id)
818
# get data from working tree if versioned
819
from_entry = self._tree.iter_entries_by_dir([file_id]).next()[1]
820
from_name = from_entry.name
821
from_parent = from_entry.parent_id
824
if from_path is None:
825
# File does not exist in FROM state
829
# File exists, but is not versioned. Have to use path-
831
from_name = os.path.basename(from_path)
832
tree_parent = self.get_tree_parent(from_trans_id)
833
from_parent = self.tree_file_id(tree_parent)
834
if from_path is not None:
835
from_kind, from_executable, from_stats = \
836
self._tree._comparison_data(from_entry, from_path)
839
from_executable = False
840
return from_name, from_parent, from_kind, from_executable
842
def _to_file_data(self, to_trans_id, from_trans_id, from_executable):
843
"""Get data about a file in the to (target) state
845
Return a (name, parent, kind, executable) tuple
847
to_name = self.final_name(to_trans_id)
849
to_kind = self.final_kind(to_trans_id)
852
to_parent = self.final_file_id(self.final_parent(to_trans_id))
853
if to_trans_id in self._new_executability:
854
to_executable = self._new_executability[to_trans_id]
855
elif to_trans_id == from_trans_id:
856
to_executable = from_executable
858
to_executable = False
859
return to_name, to_parent, to_kind, to_executable
861
def iter_changes(self):
862
"""Produce output in the same format as Tree.iter_changes.
864
Will produce nonsensical results if invoked while inventory/filesystem
865
conflicts (as reported by TreeTransform.find_conflicts()) are present.
867
This reads the Transform, but only reproduces changes involving a
868
file_id. Files that are not versioned in either of the FROM or TO
869
states are not reflected.
871
final_paths = FinalPaths(self)
872
from_trans_ids, to_trans_ids = self._get_file_id_maps()
874
# Now iterate through all active file_ids
875
for file_id in set(from_trans_ids.keys() + to_trans_ids.keys()):
877
from_trans_id = from_trans_ids.get(file_id)
878
# find file ids, and determine versioning state
879
if from_trans_id is None:
880
from_versioned = False
881
from_trans_id = to_trans_ids[file_id]
883
from_versioned = True
884
to_trans_id = to_trans_ids.get(file_id)
885
if to_trans_id is None:
887
to_trans_id = from_trans_id
891
from_name, from_parent, from_kind, from_executable = \
892
self._from_file_data(from_trans_id, from_versioned, file_id)
894
to_name, to_parent, to_kind, to_executable = \
895
self._to_file_data(to_trans_id, from_trans_id, from_executable)
897
if not from_versioned:
900
from_path = self._tree_id_paths.get(from_trans_id)
904
to_path = final_paths.get_path(to_trans_id)
905
if from_kind != to_kind:
907
elif to_kind in ('file', 'symlink') and (
908
to_trans_id != from_trans_id or
909
to_trans_id in self._new_contents):
911
if (not modified and from_versioned == to_versioned and
912
from_parent==to_parent and from_name == to_name and
913
from_executable == to_executable):
915
results.append((file_id, (from_path, to_path), modified,
916
(from_versioned, to_versioned),
917
(from_parent, to_parent),
918
(from_name, to_name),
919
(from_kind, to_kind),
920
(from_executable, to_executable)))
921
return iter(sorted(results, key=lambda x:x[1]))
923
def get_preview_tree(self):
924
"""Return a tree representing the result of the transform.
926
The tree is a snapshot, and altering the TreeTransform will invalidate
929
return _PreviewTree(self)
931
def commit(self, branch, message, merge_parents=None, strict=False,
932
timestamp=None, timezone=None, committer=None, authors=None,
933
revprops=None, revision_id=None):
934
"""Commit the result of this TreeTransform to a branch.
936
:param branch: The branch to commit to.
937
:param message: The message to attach to the commit.
938
:param merge_parents: Additional parent revision-ids specified by
940
:param strict: If True, abort the commit if there are unversioned
942
:param timestamp: if not None, seconds-since-epoch for the time and
943
date. (May be a float.)
944
:param timezone: Optional timezone for timestamp, as an offset in
946
:param committer: Optional committer in email-id format.
947
(e.g. "J Random Hacker <jrandom@example.com>")
948
:param authors: Optional list of authors in email-id format.
949
:param revprops: Optional dictionary of revision properties.
950
:param revision_id: Optional revision id. (Specifying a revision-id
951
may reduce performance for some non-native formats.)
952
:return: The revision_id of the revision committed.
954
self._check_malformed()
956
unversioned = set(self._new_contents).difference(set(self._new_id))
957
for trans_id in unversioned:
958
if self.final_file_id(trans_id) is None:
959
raise errors.StrictCommitFailed()
961
revno, last_rev_id = branch.last_revision_info()
962
if last_rev_id == _mod_revision.NULL_REVISION:
963
if merge_parents is not None:
964
raise ValueError('Cannot supply merge parents for first'
968
parent_ids = [last_rev_id]
969
if merge_parents is not None:
970
parent_ids.extend(merge_parents)
971
if self._tree.get_revision_id() != last_rev_id:
972
raise ValueError('TreeTransform not based on branch basis: %s' %
973
self._tree.get_revision_id())
974
revprops = commit.Commit.update_revprops(revprops, branch, authors)
975
builder = branch.get_commit_builder(parent_ids,
980
revision_id=revision_id)
981
preview = self.get_preview_tree()
982
list(builder.record_iter_changes(preview, last_rev_id,
983
self.iter_changes()))
984
builder.finish_inventory()
985
revision_id = builder.commit(message)
986
branch.set_last_revision_info(revno + 1, revision_id)
989
def _text_parent(self, trans_id):
990
file_id = self.tree_file_id(trans_id)
992
if file_id is None or self._tree.kind(file_id) != 'file':
994
except errors.NoSuchFile:
998
def _get_parents_texts(self, trans_id):
999
"""Get texts for compression parents of this file."""
1000
file_id = self._text_parent(trans_id)
1003
return (self._tree.get_file_text(file_id),)
1005
def _get_parents_lines(self, trans_id):
1006
"""Get lines for compression parents of this file."""
1007
file_id = self._text_parent(trans_id)
1010
return (self._tree.get_file_lines(file_id),)
1012
def serialize(self, serializer):
1013
"""Serialize this TreeTransform.
1015
:param serializer: A Serialiser like pack.ContainerSerializer.
1017
new_name = dict((k, v.encode('utf-8')) for k, v in
1018
self._new_name.items())
1019
new_executability = dict((k, int(v)) for k, v in
1020
self._new_executability.items())
1021
tree_path_ids = dict((k.encode('utf-8'), v)
1022
for k, v in self._tree_path_ids.items())
1024
'_id_number': self._id_number,
1025
'_new_name': new_name,
1026
'_new_parent': self._new_parent,
1027
'_new_executability': new_executability,
1028
'_new_id': self._new_id,
1029
'_tree_path_ids': tree_path_ids,
1030
'_removed_id': list(self._removed_id),
1031
'_removed_contents': list(self._removed_contents),
1032
'_non_present_ids': self._non_present_ids,
1034
yield serializer.bytes_record(bencode.bencode(attribs),
1036
for trans_id, kind in self._new_contents.items():
1038
lines = osutils.chunks_to_lines(
1039
self._read_file_chunks(trans_id))
1040
parents = self._get_parents_lines(trans_id)
1041
mpdiff = multiparent.MultiParent.from_lines(lines, parents)
1042
content = ''.join(mpdiff.to_patch())
1043
if kind == 'directory':
1045
if kind == 'symlink':
1046
content = self._read_symlink_target(trans_id)
1047
yield serializer.bytes_record(content, ((trans_id, kind),))
1049
def deserialize(self, records):
1050
"""Deserialize a stored TreeTransform.
1052
:param records: An iterable of (names, content) tuples, as per
1053
pack.ContainerPushParser.
1055
names, content = records.next()
1056
attribs = bencode.bdecode(content)
1057
self._id_number = attribs['_id_number']
1058
self._new_name = dict((k, v.decode('utf-8'))
1059
for k, v in attribs['_new_name'].items())
1060
self._new_parent = attribs['_new_parent']
1061
self._new_executability = dict((k, bool(v)) for k, v in
1062
attribs['_new_executability'].items())
1063
self._new_id = attribs['_new_id']
1064
self._r_new_id = dict((v, k) for k, v in self._new_id.items())
1065
self._tree_path_ids = {}
1066
self._tree_id_paths = {}
1067
for bytepath, trans_id in attribs['_tree_path_ids'].items():
1068
path = bytepath.decode('utf-8')
1069
self._tree_path_ids[path] = trans_id
1070
self._tree_id_paths[trans_id] = path
1071
self._removed_id = set(attribs['_removed_id'])
1072
self._removed_contents = set(attribs['_removed_contents'])
1073
self._non_present_ids = attribs['_non_present_ids']
1074
for ((trans_id, kind),), content in records:
1076
mpdiff = multiparent.MultiParent.from_patch(content)
1077
lines = mpdiff.to_lines(self._get_parents_texts(trans_id))
1078
self.create_file(lines, trans_id)
1079
if kind == 'directory':
1080
self.create_directory(trans_id)
1081
if kind == 'symlink':
1082
self.create_symlink(content.decode('utf-8'), trans_id)
1085
class DiskTreeTransform(TreeTransformBase):
1086
"""Tree transform storing its contents on disk."""
1088
def __init__(self, tree, limbodir, pb=None,
1089
case_sensitive=True):
1091
:param tree: The tree that will be transformed, but not necessarily
1093
:param limbodir: A directory where new files can be stored until
1094
they are installed in their proper places
1096
:param case_sensitive: If True, the target of the transform is
1097
case sensitive, not just case preserving.
1099
TreeTransformBase.__init__(self, tree, pb, case_sensitive)
1100
self._limbodir = limbodir
1101
self._deletiondir = None
1102
# A mapping of transform ids to their limbo filename
1103
self._limbo_files = {}
1104
# A mapping of transform ids to a set of the transform ids of children
1105
# that their limbo directory has
1106
self._limbo_children = {}
1107
# Map transform ids to maps of child filename to child transform id
1108
self._limbo_children_names = {}
1109
# List of transform ids that need to be renamed from limbo into place
1110
self._needs_rename = set()
1111
self._creation_mtime = None
1114
"""Release the working tree lock, if held, clean up limbo dir.
1116
This is required if apply has not been invoked, but can be invoked
1119
if self._tree is None:
1122
entries = [(self._limbo_name(t), t, k) for t, k in
1123
self._new_contents.iteritems()]
1124
entries.sort(reverse=True)
1125
for path, trans_id, kind in entries:
1128
delete_any(self._limbodir)
1130
# We don't especially care *why* the dir is immortal.
1131
raise ImmortalLimbo(self._limbodir)
1133
if self._deletiondir is not None:
1134
delete_any(self._deletiondir)
1136
raise errors.ImmortalPendingDeletion(self._deletiondir)
1138
TreeTransformBase.finalize(self)
1140
def _limbo_name(self, trans_id):
1141
"""Generate the limbo name of a file"""
1142
limbo_name = self._limbo_files.get(trans_id)
1143
if limbo_name is None:
1144
limbo_name = self._generate_limbo_path(trans_id)
1145
self._limbo_files[trans_id] = limbo_name
1148
def _generate_limbo_path(self, trans_id):
1149
"""Generate a limbo path using the trans_id as the relative path.
1151
This is suitable as a fallback, and when the transform should not be
1152
sensitive to the path encoding of the limbo directory.
1154
self._needs_rename.add(trans_id)
1155
return pathjoin(self._limbodir, trans_id)
1157
def adjust_path(self, name, parent, trans_id):
1158
previous_parent = self._new_parent.get(trans_id)
1159
previous_name = self._new_name.get(trans_id)
1160
TreeTransformBase.adjust_path(self, name, parent, trans_id)
1161
if (trans_id in self._limbo_files and
1162
trans_id not in self._needs_rename):
1163
self._rename_in_limbo([trans_id])
1164
if previous_parent != parent:
1165
self._limbo_children[previous_parent].remove(trans_id)
1166
if previous_parent != parent or previous_name != name:
1167
del self._limbo_children_names[previous_parent][previous_name]
1169
def _rename_in_limbo(self, trans_ids):
1170
"""Fix limbo names so that the right final path is produced.
1172
This means we outsmarted ourselves-- we tried to avoid renaming
1173
these files later by creating them with their final names in their
1174
final parents. But now the previous name or parent is no longer
1175
suitable, so we have to rename them.
1177
Even for trans_ids that have no new contents, we must remove their
1178
entries from _limbo_files, because they are now stale.
1180
for trans_id in trans_ids:
1181
old_path = self._limbo_files.pop(trans_id)
1182
if trans_id not in self._new_contents:
1184
new_path = self._limbo_name(trans_id)
1185
os.rename(old_path, new_path)
1186
for descendant in self._limbo_descendants(trans_id):
1187
desc_path = self._limbo_files[descendant]
1188
desc_path = new_path + desc_path[len(old_path):]
1189
self._limbo_files[descendant] = desc_path
1191
def _limbo_descendants(self, trans_id):
1192
"""Return the set of trans_ids whose limbo paths descend from this."""
1193
descendants = set(self._limbo_children.get(trans_id, []))
1194
for descendant in list(descendants):
1195
descendants.update(self._limbo_descendants(descendant))
1198
def create_file(self, contents, trans_id, mode_id=None):
1199
"""Schedule creation of a new file.
1203
Contents is an iterator of strings, all of which will be written
1204
to the target destination.
1206
New file takes the permissions of any existing file with that id,
1207
unless mode_id is specified.
1209
name = self._limbo_name(trans_id)
1210
f = open(name, 'wb')
1213
unique_add(self._new_contents, trans_id, 'file')
1215
# Clean up the file, it never got registered so
1216
# TreeTransform.finalize() won't clean it up.
1221
f.writelines(contents)
1224
self._set_mtime(name)
1225
self._set_mode(trans_id, mode_id, S_ISREG)
1227
def _read_file_chunks(self, trans_id):
1228
cur_file = open(self._limbo_name(trans_id), 'rb')
1230
return cur_file.readlines()
1234
def _read_symlink_target(self, trans_id):
1235
return os.readlink(self._limbo_name(trans_id))
1237
def _set_mtime(self, path):
1238
"""All files that are created get the same mtime.
1240
This time is set by the first object to be created.
1242
if self._creation_mtime is None:
1243
self._creation_mtime = time.time()
1244
os.utime(path, (self._creation_mtime, self._creation_mtime))
1246
def create_hardlink(self, path, trans_id):
1247
"""Schedule creation of a hard link"""
1248
name = self._limbo_name(trans_id)
1252
if e.errno != errno.EPERM:
1254
raise errors.HardLinkNotSupported(path)
1256
unique_add(self._new_contents, trans_id, 'file')
1258
# Clean up the file, it never got registered so
1259
# TreeTransform.finalize() won't clean it up.
1263
def create_directory(self, trans_id):
1264
"""Schedule creation of a new directory.
1266
See also new_directory.
1268
os.mkdir(self._limbo_name(trans_id))
1269
unique_add(self._new_contents, trans_id, 'directory')
1271
def create_symlink(self, target, trans_id):
1272
"""Schedule creation of a new symbolic link.
1274
target is a bytestring.
1275
See also new_symlink.
1278
os.symlink(target, self._limbo_name(trans_id))
1279
unique_add(self._new_contents, trans_id, 'symlink')
1282
path = FinalPaths(self).get_path(trans_id)
1285
raise UnableCreateSymlink(path=path)
1287
def cancel_creation(self, trans_id):
1288
"""Cancel the creation of new file contents."""
1289
del self._new_contents[trans_id]
1290
children = self._limbo_children.get(trans_id)
1291
# if this is a limbo directory with children, move them before removing
1293
if children is not None:
1294
self._rename_in_limbo(children)
1295
del self._limbo_children[trans_id]
1296
del self._limbo_children_names[trans_id]
1297
delete_any(self._limbo_name(trans_id))
1300
class TreeTransform(DiskTreeTransform):
1301
"""Represent a tree transformation.
1303
This object is designed to support incremental generation of the transform,
1306
However, it gives optimum performance when parent directories are created
1307
before their contents. The transform is then able to put child files
1308
directly in their parent directory, avoiding later renames.
1310
It is easy to produce malformed transforms, but they are generally
1311
harmless. Attempting to apply a malformed transform will cause an
1312
exception to be raised before any modifications are made to the tree.
1314
Many kinds of malformed transforms can be corrected with the
1315
resolve_conflicts function. The remaining ones indicate programming error,
1316
such as trying to create a file with no path.
1318
Two sets of file creation methods are supplied. Convenience methods are:
1323
These are composed of the low-level methods:
1325
* create_file or create_directory or create_symlink
1329
Transform/Transaction ids
1330
-------------------------
1331
trans_ids are temporary ids assigned to all files involved in a transform.
1332
It's possible, even common, that not all files in the Tree have trans_ids.
1334
trans_ids are used because filenames and file_ids are not good enough
1335
identifiers; filenames change, and not all files have file_ids. File-ids
1336
are also associated with trans-ids, so that moving a file moves its
1339
trans_ids are only valid for the TreeTransform that generated them.
1343
Limbo is a temporary directory use to hold new versions of files.
1344
Files are added to limbo by create_file, create_directory, create_symlink,
1345
and their convenience variants (new_*). Files may be removed from limbo
1346
using cancel_creation. Files are renamed from limbo into their final
1347
location as part of TreeTransform.apply
1349
Limbo must be cleaned up, by either calling TreeTransform.apply or
1350
calling TreeTransform.finalize.
1352
Files are placed into limbo inside their parent directories, where
1353
possible. This reduces subsequent renames, and makes operations involving
1354
lots of files faster. This optimization is only possible if the parent
1355
directory is created *before* creating any of its children, so avoid
1356
creating children before parents, where possible.
1360
This temporary directory is used by _FileMover for storing files that are
1361
about to be deleted. In case of rollback, the files will be restored.
1362
FileMover does not delete files until it is sure that a rollback will not
1365
def __init__(self, tree, pb=None):
1366
"""Note: a tree_write lock is taken on the tree.
1368
Use TreeTransform.finalize() to release the lock (can be omitted if
1369
TreeTransform.apply() called).
1371
tree.lock_tree_write()
1374
limbodir = urlutils.local_path_from_url(
1375
tree._transport.abspath('limbo'))
1379
if e.errno == errno.EEXIST:
1380
raise ExistingLimbo(limbodir)
1381
deletiondir = urlutils.local_path_from_url(
1382
tree._transport.abspath('pending-deletion'))
1384
os.mkdir(deletiondir)
1386
if e.errno == errno.EEXIST:
1387
raise errors.ExistingPendingDeletion(deletiondir)
1392
# Cache of realpath results, to speed up canonical_path
1393
self._realpaths = {}
1394
# Cache of relpath results, to speed up canonical_path
1396
DiskTreeTransform.__init__(self, tree, limbodir, pb,
1397
tree.case_sensitive)
1398
self._deletiondir = deletiondir
1400
def canonical_path(self, path):
1401
"""Get the canonical tree-relative path"""
1402
# don't follow final symlinks
1403
abs = self._tree.abspath(path)
1404
if abs in self._relpaths:
1405
return self._relpaths[abs]
1406
dirname, basename = os.path.split(abs)
1407
if dirname not in self._realpaths:
1408
self._realpaths[dirname] = os.path.realpath(dirname)
1409
dirname = self._realpaths[dirname]
1410
abs = pathjoin(dirname, basename)
1411
if dirname in self._relpaths:
1412
relpath = pathjoin(self._relpaths[dirname], basename)
1413
relpath = relpath.rstrip('/\\')
1415
relpath = self._tree.relpath(abs)
1416
self._relpaths[abs] = relpath
1419
def tree_kind(self, trans_id):
1420
"""Determine the file kind in the working tree.
1422
Raises NoSuchFile if the file does not exist
1424
path = self._tree_id_paths.get(trans_id)
1426
raise NoSuchFile(None)
1428
return file_kind(self._tree.abspath(path))
1430
if e.errno != errno.ENOENT:
1433
raise NoSuchFile(path)
1435
def _set_mode(self, trans_id, mode_id, typefunc):
1436
"""Set the mode of new file contents.
1437
The mode_id is the existing file to get the mode from (often the same
1438
as trans_id). The operation is only performed if there's a mode match
1439
according to typefunc.
1444
old_path = self._tree_id_paths[mode_id]
1448
mode = os.stat(self._tree.abspath(old_path)).st_mode
1450
if e.errno in (errno.ENOENT, errno.ENOTDIR):
1451
# Either old_path doesn't exist, or the parent of the
1452
# target is not a directory (but will be one eventually)
1453
# Either way, we know it doesn't exist *right now*
1454
# See also bug #248448
1459
os.chmod(self._limbo_name(trans_id), mode)
1461
def iter_tree_children(self, parent_id):
1462
"""Iterate through the entry's tree children, if any"""
1464
path = self._tree_id_paths[parent_id]
1468
children = os.listdir(self._tree.abspath(path))
1470
if not (osutils._is_error_enotdir(e)
1471
or e.errno in (errno.ENOENT, errno.ESRCH)):
1475
for child in children:
1476
childpath = joinpath(path, child)
1477
if self._tree.is_control_filename(childpath):
1479
yield self.trans_id_tree_path(childpath)
1481
def _generate_limbo_path(self, trans_id):
1482
"""Generate a limbo path using the final path if possible.
1484
This optimizes the performance of applying the tree transform by
1485
avoiding renames. These renames can be avoided only when the parent
1486
directory is already scheduled for creation.
1488
If the final path cannot be used, falls back to using the trans_id as
1491
parent = self._new_parent.get(trans_id)
1492
# if the parent directory is already in limbo (e.g. when building a
1493
# tree), choose a limbo name inside the parent, to reduce further
1495
use_direct_path = False
1496
if self._new_contents.get(parent) == 'directory':
1497
filename = self._new_name.get(trans_id)
1498
if filename is not None:
1499
if parent not in self._limbo_children:
1500
self._limbo_children[parent] = set()
1501
self._limbo_children_names[parent] = {}
1502
use_direct_path = True
1503
# the direct path can only be used if no other file has
1504
# already taken this pathname, i.e. if the name is unused, or
1505
# if it is already associated with this trans_id.
1506
elif self._case_sensitive_target:
1507
if (self._limbo_children_names[parent].get(filename)
1508
in (trans_id, None)):
1509
use_direct_path = True
1511
for l_filename, l_trans_id in\
1512
self._limbo_children_names[parent].iteritems():
1513
if l_trans_id == trans_id:
1515
if l_filename.lower() == filename.lower():
1518
use_direct_path = True
1520
if not use_direct_path:
1521
return DiskTreeTransform._generate_limbo_path(self, trans_id)
1523
limbo_name = pathjoin(self._limbo_files[parent], filename)
1524
self._limbo_children[parent].add(trans_id)
1525
self._limbo_children_names[parent][filename] = trans_id
1529
def apply(self, no_conflicts=False, precomputed_delta=None, _mover=None):
1530
"""Apply all changes to the inventory and filesystem.
1532
If filesystem or inventory conflicts are present, MalformedTransform
1535
If apply succeeds, finalize is not necessary.
1537
:param no_conflicts: if True, the caller guarantees there are no
1538
conflicts, so no check is made.
1539
:param precomputed_delta: An inventory delta to use instead of
1541
:param _mover: Supply an alternate FileMover, for testing
1543
if not no_conflicts:
1544
self._check_malformed()
1545
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1547
if precomputed_delta is None:
1548
child_pb.update('Apply phase', 0, 2)
1549
inventory_delta = self._generate_inventory_delta()
1552
inventory_delta = precomputed_delta
1555
mover = _FileMover()
1559
child_pb.update('Apply phase', 0 + offset, 2 + offset)
1560
self._apply_removals(mover)
1561
child_pb.update('Apply phase', 1 + offset, 2 + offset)
1562
modified_paths = self._apply_insertions(mover)
1567
mover.apply_deletions()
1570
self._tree.apply_inventory_delta(inventory_delta)
1573
return _TransformResults(modified_paths, self.rename_count)
1575
def _generate_inventory_delta(self):
1576
"""Generate an inventory delta for the current transform."""
1577
inventory_delta = []
1578
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1579
new_paths = self._inventory_altered()
1580
total_entries = len(new_paths) + len(self._removed_id)
1582
for num, trans_id in enumerate(self._removed_id):
1584
child_pb.update('removing file', num, total_entries)
1585
if trans_id == self._new_root:
1586
file_id = self._tree.get_root_id()
1588
file_id = self.tree_file_id(trans_id)
1589
# File-id isn't really being deleted, just moved
1590
if file_id in self._r_new_id:
1592
path = self._tree_id_paths[trans_id]
1593
inventory_delta.append((path, None, file_id, None))
1594
new_path_file_ids = dict((t, self.final_file_id(t)) for p, t in
1596
entries = self._tree.iter_entries_by_dir(
1597
new_path_file_ids.values())
1598
old_paths = dict((e.file_id, p) for p, e in entries)
1600
for num, (path, trans_id) in enumerate(new_paths):
1602
child_pb.update('adding file',
1603
num + len(self._removed_id), total_entries)
1604
file_id = new_path_file_ids[trans_id]
1609
kind = self.final_kind(trans_id)
1611
kind = self._tree.stored_kind(file_id)
1612
parent_trans_id = self.final_parent(trans_id)
1613
parent_file_id = new_path_file_ids.get(parent_trans_id)
1614
if parent_file_id is None:
1615
parent_file_id = self.final_file_id(parent_trans_id)
1616
if trans_id in self._new_reference_revision:
1617
new_entry = inventory.TreeReference(
1619
self._new_name[trans_id],
1620
self.final_file_id(self._new_parent[trans_id]),
1621
None, self._new_reference_revision[trans_id])
1623
new_entry = inventory.make_entry(kind,
1624
self.final_name(trans_id),
1625
parent_file_id, file_id)
1626
old_path = old_paths.get(new_entry.file_id)
1627
new_executability = self._new_executability.get(trans_id)
1628
if new_executability is not None:
1629
new_entry.executable = new_executability
1630
inventory_delta.append(
1631
(old_path, path, new_entry.file_id, new_entry))
1634
return inventory_delta
1636
def _apply_removals(self, mover):
1637
"""Perform tree operations that remove directory/inventory names.
1639
That is, delete files that are to be deleted, and put any files that
1640
need renaming into limbo. This must be done in strict child-to-parent
1643
If inventory_delta is None, no inventory delta generation is performed.
1645
tree_paths = list(self._tree_path_ids.iteritems())
1646
tree_paths.sort(reverse=True)
1647
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1649
for num, data in enumerate(tree_paths):
1650
path, trans_id = data
1651
child_pb.update('removing file', num, len(tree_paths))
1652
full_path = self._tree.abspath(path)
1653
if trans_id in self._removed_contents:
1654
delete_path = os.path.join(self._deletiondir, trans_id)
1655
mover.pre_delete(full_path, delete_path)
1656
elif (trans_id in self._new_name
1657
or trans_id in self._new_parent):
1659
mover.rename(full_path, self._limbo_name(trans_id))
1660
except errors.TransformRenameFailed, e:
1661
if e.errno != errno.ENOENT:
1664
self.rename_count += 1
1668
def _apply_insertions(self, mover):
1669
"""Perform tree operations that insert directory/inventory names.
1671
That is, create any files that need to be created, and restore from
1672
limbo any files that needed renaming. This must be done in strict
1673
parent-to-child order.
1675
If inventory_delta is None, no inventory delta is calculated, and
1676
no list of modified paths is returned.
1678
new_paths = self.new_paths(filesystem_only=True)
1680
new_path_file_ids = dict((t, self.final_file_id(t)) for p, t in
1682
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1684
for num, (path, trans_id) in enumerate(new_paths):
1686
child_pb.update('adding file', num, len(new_paths))
1687
full_path = self._tree.abspath(path)
1688
if trans_id in self._needs_rename:
1690
mover.rename(self._limbo_name(trans_id), full_path)
1691
except errors.TransformRenameFailed, e:
1692
# We may be renaming a dangling inventory id
1693
if e.errno != errno.ENOENT:
1696
self.rename_count += 1
1697
if (trans_id in self._new_contents or
1698
self.path_changed(trans_id)):
1699
if trans_id in self._new_contents:
1700
modified_paths.append(full_path)
1701
if trans_id in self._new_executability:
1702
self._set_executability(path, trans_id)
1705
self._new_contents.clear()
1706
return modified_paths
1709
class TransformPreview(DiskTreeTransform):
1710
"""A TreeTransform for generating preview trees.
1712
Unlike TreeTransform, this version works when the input tree is a
1713
RevisionTree, rather than a WorkingTree. As a result, it tends to ignore
1714
unversioned files in the input tree.
1717
def __init__(self, tree, pb=None, case_sensitive=True):
1719
limbodir = osutils.mkdtemp(prefix='bzr-limbo-')
1720
DiskTreeTransform.__init__(self, tree, limbodir, pb, case_sensitive)
1722
def canonical_path(self, path):
1725
def tree_kind(self, trans_id):
1726
path = self._tree_id_paths.get(trans_id)
1728
raise NoSuchFile(None)
1729
file_id = self._tree.path2id(path)
1730
return self._tree.kind(file_id)
1732
def _set_mode(self, trans_id, mode_id, typefunc):
1733
"""Set the mode of new file contents.
1734
The mode_id is the existing file to get the mode from (often the same
1735
as trans_id). The operation is only performed if there's a mode match
1736
according to typefunc.
1738
# is it ok to ignore this? probably
1741
def iter_tree_children(self, parent_id):
1742
"""Iterate through the entry's tree children, if any"""
1744
path = self._tree_id_paths[parent_id]
1747
file_id = self.tree_file_id(parent_id)
1750
entry = self._tree.iter_entries_by_dir([file_id]).next()[1]
1751
children = getattr(entry, 'children', {})
1752
for child in children:
1753
childpath = joinpath(path, child)
1754
yield self.trans_id_tree_path(childpath)
1757
class _PreviewTree(tree.Tree):
1758
"""Partial implementation of Tree to support show_diff_trees"""
1760
def __init__(self, transform):
1761
self._transform = transform
1762
self._final_paths = FinalPaths(transform)
1763
self.__by_parent = None
1764
self._parent_ids = []
1765
self._all_children_cache = {}
1766
self._path2trans_id_cache = {}
1767
self._final_name_cache = {}
1768
self._iter_changes_cache = dict((c[0], c) for c in
1769
self._transform.iter_changes())
1771
def _content_change(self, file_id):
1772
"""Return True if the content of this file changed"""
1773
changes = self._iter_changes_cache.get(file_id)
1774
# changes[2] is true if the file content changed. See
1775
# InterTree.iter_changes.
1776
return (changes is not None and changes[2])
1778
def _get_repository(self):
1779
repo = getattr(self._transform._tree, '_repository', None)
1781
repo = self._transform._tree.branch.repository
1784
def _iter_parent_trees(self):
1785
for revision_id in self.get_parent_ids():
1787
yield self.revision_tree(revision_id)
1788
except errors.NoSuchRevisionInTree:
1789
yield self._get_repository().revision_tree(revision_id)
1791
def _get_file_revision(self, file_id, vf, tree_revision):
1792
parent_keys = [(file_id, self._file_revision(t, file_id)) for t in
1793
self._iter_parent_trees()]
1794
vf.add_lines((file_id, tree_revision), parent_keys,
1795
self.get_file_lines(file_id))
1796
repo = self._get_repository()
1797
base_vf = repo.texts
1798
if base_vf not in vf.fallback_versionedfiles:
1799
vf.fallback_versionedfiles.append(base_vf)
1800
return tree_revision
1802
def _stat_limbo_file(self, file_id):
1803
trans_id = self._transform.trans_id_file_id(file_id)
1804
name = self._transform._limbo_name(trans_id)
1805
return os.lstat(name)
1808
def _by_parent(self):
1809
if self.__by_parent is None:
1810
self.__by_parent = self._transform.by_parent()
1811
return self.__by_parent
1813
def _comparison_data(self, entry, path):
1814
kind, size, executable, link_or_sha1 = self.path_content_summary(path)
1815
if kind == 'missing':
1819
file_id = self._transform.final_file_id(self._path2trans_id(path))
1820
executable = self.is_executable(file_id, path)
1821
return kind, executable, None
1823
def is_locked(self):
1826
def lock_read(self):
1827
# Perhaps in theory, this should lock the TreeTransform?
1834
def inventory(self):
1835
"""This Tree does not use inventory as its backing data."""
1836
raise NotImplementedError(_PreviewTree.inventory)
1838
def get_root_id(self):
1839
return self._transform.final_file_id(self._transform.root)
1841
def all_file_ids(self):
1842
tree_ids = set(self._transform._tree.all_file_ids())
1843
tree_ids.difference_update(self._transform.tree_file_id(t)
1844
for t in self._transform._removed_id)
1845
tree_ids.update(self._transform._new_id.values())
1849
return iter(self.all_file_ids())
1851
def _has_id(self, file_id, fallback_check):
1852
if file_id in self._transform._r_new_id:
1854
elif file_id in set([self._transform.tree_file_id(trans_id) for
1855
trans_id in self._transform._removed_id]):
1858
return fallback_check(file_id)
1860
def has_id(self, file_id):
1861
return self._has_id(file_id, self._transform._tree.has_id)
1863
def has_or_had_id(self, file_id):
1864
return self._has_id(file_id, self._transform._tree.has_or_had_id)
1866
def _path2trans_id(self, path):
1867
# We must not use None here, because that is a valid value to store.
1868
trans_id = self._path2trans_id_cache.get(path, object)
1869
if trans_id is not object:
1871
segments = splitpath(path)
1872
cur_parent = self._transform.root
1873
for cur_segment in segments:
1874
for child in self._all_children(cur_parent):
1875
final_name = self._final_name_cache.get(child)
1876
if final_name is None:
1877
final_name = self._transform.final_name(child)
1878
self._final_name_cache[child] = final_name
1879
if final_name == cur_segment:
1883
self._path2trans_id_cache[path] = None
1885
self._path2trans_id_cache[path] = cur_parent
1888
def path2id(self, path):
1889
return self._transform.final_file_id(self._path2trans_id(path))
1891
def id2path(self, file_id):
1892
trans_id = self._transform.trans_id_file_id(file_id)
1894
return self._final_paths._determine_path(trans_id)
1896
raise errors.NoSuchId(self, file_id)
1898
def _all_children(self, trans_id):
1899
children = self._all_children_cache.get(trans_id)
1900
if children is not None:
1902
children = set(self._transform.iter_tree_children(trans_id))
1903
# children in the _new_parent set are provided by _by_parent.
1904
children.difference_update(self._transform._new_parent.keys())
1905
children.update(self._by_parent.get(trans_id, []))
1906
self._all_children_cache[trans_id] = children
1909
def iter_children(self, file_id):
1910
trans_id = self._transform.trans_id_file_id(file_id)
1911
for child_trans_id in self._all_children(trans_id):
1912
yield self._transform.final_file_id(child_trans_id)
1915
possible_extras = set(self._transform.trans_id_tree_path(p) for p
1916
in self._transform._tree.extras())
1917
possible_extras.update(self._transform._new_contents)
1918
possible_extras.update(self._transform._removed_id)
1919
for trans_id in possible_extras:
1920
if self._transform.final_file_id(trans_id) is None:
1921
yield self._final_paths._determine_path(trans_id)
1923
def _make_inv_entries(self, ordered_entries, specific_file_ids=None,
1924
yield_parents=False):
1925
for trans_id, parent_file_id in ordered_entries:
1926
file_id = self._transform.final_file_id(trans_id)
1929
if (specific_file_ids is not None
1930
and file_id not in specific_file_ids):
1933
kind = self._transform.final_kind(trans_id)
1935
kind = self._transform._tree.stored_kind(file_id)
1936
new_entry = inventory.make_entry(
1938
self._transform.final_name(trans_id),
1939
parent_file_id, file_id)
1940
yield new_entry, trans_id
1942
def _list_files_by_dir(self):
1943
todo = [ROOT_PARENT]
1945
while len(todo) > 0:
1947
parent_file_id = self._transform.final_file_id(parent)
1948
children = list(self._all_children(parent))
1949
paths = dict(zip(children, self._final_paths.get_paths(children)))
1950
children.sort(key=paths.get)
1951
todo.extend(reversed(children))
1952
for trans_id in children:
1953
ordered_ids.append((trans_id, parent_file_id))
1956
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
1957
# This may not be a maximally efficient implementation, but it is
1958
# reasonably straightforward. An implementation that grafts the
1959
# TreeTransform changes onto the tree's iter_entries_by_dir results
1960
# might be more efficient, but requires tricky inferences about stack
1962
ordered_ids = self._list_files_by_dir()
1963
for entry, trans_id in self._make_inv_entries(ordered_ids,
1964
specific_file_ids, yield_parents=yield_parents):
1965
yield unicode(self._final_paths.get_path(trans_id)), entry
1967
def _iter_entries_for_dir(self, dir_path):
1968
"""Return path, entry for items in a directory without recursing down."""
1969
dir_file_id = self.path2id(dir_path)
1971
for file_id in self.iter_children(dir_file_id):
1972
trans_id = self._transform.trans_id_file_id(file_id)
1973
ordered_ids.append((trans_id, file_id))
1974
for entry, trans_id in self._make_inv_entries(ordered_ids):
1975
yield unicode(self._final_paths.get_path(trans_id)), entry
1977
def list_files(self, include_root=False, from_dir=None, recursive=True):
1978
"""See WorkingTree.list_files."""
1979
# XXX This should behave like WorkingTree.list_files, but is really
1980
# more like RevisionTree.list_files.
1984
prefix = from_dir + '/'
1985
entries = self.iter_entries_by_dir()
1986
for path, entry in entries:
1987
if entry.name == '' and not include_root:
1990
if not path.startswith(prefix):
1992
path = path[len(prefix):]
1993
yield path, 'V', entry.kind, entry.file_id, entry
1995
if from_dir is None and include_root is True:
1996
root_entry = inventory.make_entry('directory', '',
1997
ROOT_PARENT, self.get_root_id())
1998
yield '', 'V', 'directory', root_entry.file_id, root_entry
1999
entries = self._iter_entries_for_dir(from_dir or '')
2000
for path, entry in entries:
2001
yield path, 'V', entry.kind, entry.file_id, entry
2003
def kind(self, file_id):
2004
trans_id = self._transform.trans_id_file_id(file_id)
2005
return self._transform.final_kind(trans_id)
2007
def stored_kind(self, file_id):
2008
trans_id = self._transform.trans_id_file_id(file_id)
2010
return self._transform._new_contents[trans_id]
2012
return self._transform._tree.stored_kind(file_id)
2014
def get_file_mtime(self, file_id, path=None):
2015
"""See Tree.get_file_mtime"""
2016
if not self._content_change(file_id):
2017
return self._transform._tree.get_file_mtime(file_id)
2018
return self._stat_limbo_file(file_id).st_mtime
2020
def _file_size(self, entry, stat_value):
2021
return self.get_file_size(entry.file_id)
2023
def get_file_size(self, file_id):
2024
"""See Tree.get_file_size"""
2025
if self.kind(file_id) == 'file':
2026
return self._transform._tree.get_file_size(file_id)
2030
def get_file_sha1(self, file_id, path=None, stat_value=None):
2031
trans_id = self._transform.trans_id_file_id(file_id)
2032
kind = self._transform._new_contents.get(trans_id)
2034
return self._transform._tree.get_file_sha1(file_id)
2036
fileobj = self.get_file(file_id)
2038
return sha_file(fileobj)
2042
def is_executable(self, file_id, path=None):
2045
trans_id = self._transform.trans_id_file_id(file_id)
2047
return self._transform._new_executability[trans_id]
2050
return self._transform._tree.is_executable(file_id, path)
2052
if e.errno == errno.ENOENT:
2055
except errors.NoSuchId:
2058
def path_content_summary(self, path):
2059
trans_id = self._path2trans_id(path)
2060
tt = self._transform
2061
tree_path = tt._tree_id_paths.get(trans_id)
2062
kind = tt._new_contents.get(trans_id)
2064
if tree_path is None or trans_id in tt._removed_contents:
2065
return 'missing', None, None, None
2066
summary = tt._tree.path_content_summary(tree_path)
2067
kind, size, executable, link_or_sha1 = summary
2070
limbo_name = tt._limbo_name(trans_id)
2071
if trans_id in tt._new_reference_revision:
2072
kind = 'tree-reference'
2074
statval = os.lstat(limbo_name)
2075
size = statval.st_size
2076
if not supports_executable():
2079
executable = statval.st_mode & S_IEXEC
2083
if kind == 'symlink':
2084
link_or_sha1 = os.readlink(limbo_name).decode(osutils._fs_enc)
2085
executable = tt._new_executability.get(trans_id, executable)
2086
return kind, size, executable, link_or_sha1
2088
def iter_changes(self, from_tree, include_unchanged=False,
2089
specific_files=None, pb=None, extra_trees=None,
2090
require_versioned=True, want_unversioned=False):
2091
"""See InterTree.iter_changes.
2093
This has a fast path that is only used when the from_tree matches
2094
the transform tree, and no fancy options are supplied.
2096
if (from_tree is not self._transform._tree or include_unchanged or
2097
specific_files or want_unversioned):
2098
return tree.InterTree(from_tree, self).iter_changes(
2099
include_unchanged=include_unchanged,
2100
specific_files=specific_files,
2102
extra_trees=extra_trees,
2103
require_versioned=require_versioned,
2104
want_unversioned=want_unversioned)
2105
if want_unversioned:
2106
raise ValueError('want_unversioned is not supported')
2107
return self._transform.iter_changes()
2109
def get_file(self, file_id, path=None):
2110
"""See Tree.get_file"""
2111
if not self._content_change(file_id):
2112
return self._transform._tree.get_file(file_id, path)
2113
trans_id = self._transform.trans_id_file_id(file_id)
2114
name = self._transform._limbo_name(trans_id)
2115
return open(name, 'rb')
2117
def get_file_with_stat(self, file_id, path=None):
2118
return self.get_file(file_id, path), None
2120
def annotate_iter(self, file_id,
2121
default_revision=_mod_revision.CURRENT_REVISION):
2122
changes = self._iter_changes_cache.get(file_id)
2126
changed_content, versioned, kind = (changes[2], changes[3],
2130
get_old = (kind[0] == 'file' and versioned[0])
2132
old_annotation = self._transform._tree.annotate_iter(file_id,
2133
default_revision=default_revision)
2137
return old_annotation
2138
if not changed_content:
2139
return old_annotation
2140
# TODO: This is doing something similar to what WT.annotate_iter is
2141
# doing, however it fails slightly because it doesn't know what
2142
# the *other* revision_id is, so it doesn't know how to give the
2143
# other as the origin for some lines, they all get
2144
# 'default_revision'
2145
# It would be nice to be able to use the new Annotator based
2146
# approach, as well.
2147
return annotate.reannotate([old_annotation],
2148
self.get_file(file_id).readlines(),
2151
def get_symlink_target(self, file_id):
2152
"""See Tree.get_symlink_target"""
2153
if not self._content_change(file_id):
2154
return self._transform._tree.get_symlink_target(file_id)
2155
trans_id = self._transform.trans_id_file_id(file_id)
2156
name = self._transform._limbo_name(trans_id)
2157
return osutils.readlink(name)
2159
def walkdirs(self, prefix=''):
2160
pending = [self._transform.root]
2161
while len(pending) > 0:
2162
parent_id = pending.pop()
2165
prefix = prefix.rstrip('/')
2166
parent_path = self._final_paths.get_path(parent_id)
2167
parent_file_id = self._transform.final_file_id(parent_id)
2168
for child_id in self._all_children(parent_id):
2169
path_from_root = self._final_paths.get_path(child_id)
2170
basename = self._transform.final_name(child_id)
2171
file_id = self._transform.final_file_id(child_id)
2173
kind = self._transform.final_kind(child_id)
2174
versioned_kind = kind
2177
versioned_kind = self._transform._tree.stored_kind(file_id)
2178
if versioned_kind == 'directory':
2179
subdirs.append(child_id)
2180
children.append((path_from_root, basename, kind, None,
2181
file_id, versioned_kind))
2183
if parent_path.startswith(prefix):
2184
yield (parent_path, parent_file_id), children
2185
pending.extend(sorted(subdirs, key=self._final_paths.get_path,
2188
def get_parent_ids(self):
2189
return self._parent_ids
2191
def set_parent_ids(self, parent_ids):
2192
self._parent_ids = parent_ids
2194
def get_revision_tree(self, revision_id):
2195
return self._transform._tree.get_revision_tree(revision_id)
2198
def joinpath(parent, child):
2199
"""Join tree-relative paths, handling the tree root specially"""
2200
if parent is None or parent == "":
2203
return pathjoin(parent, child)
2206
class FinalPaths(object):
2207
"""Make path calculation cheap by memoizing paths.
2209
The underlying tree must not be manipulated between calls, or else
2210
the results will likely be incorrect.
2212
def __init__(self, transform):
2213
object.__init__(self)
2214
self._known_paths = {}
2215
self.transform = transform
2217
def _determine_path(self, trans_id):
2218
if (trans_id == self.transform.root or trans_id == ROOT_PARENT):
2220
name = self.transform.final_name(trans_id)
2221
parent_id = self.transform.final_parent(trans_id)
2222
if parent_id == self.transform.root:
2225
return pathjoin(self.get_path(parent_id), name)
2227
def get_path(self, trans_id):
2228
"""Find the final path associated with a trans_id"""
2229
if trans_id not in self._known_paths:
2230
self._known_paths[trans_id] = self._determine_path(trans_id)
2231
return self._known_paths[trans_id]
2233
def get_paths(self, trans_ids):
2234
return [(self.get_path(t), t) for t in trans_ids]
2238
def topology_sorted_ids(tree):
2239
"""Determine the topological order of the ids in a tree"""
2240
file_ids = list(tree)
2241
file_ids.sort(key=tree.id2path)
2245
def build_tree(tree, wt, accelerator_tree=None, hardlink=False,
2246
delta_from_tree=False):
2247
"""Create working tree for a branch, using a TreeTransform.
2249
This function should be used on empty trees, having a tree root at most.
2250
(see merge and revert functionality for working with existing trees)
2252
Existing files are handled like so:
2254
- Existing bzrdirs take precedence over creating new items. They are
2255
created as '%s.diverted' % name.
2256
- Otherwise, if the content on disk matches the content we are building,
2257
it is silently replaced.
2258
- Otherwise, conflict resolution will move the old file to 'oldname.moved'.
2260
:param tree: The tree to convert wt into a copy of
2261
:param wt: The working tree that files will be placed into
2262
:param accelerator_tree: A tree which can be used for retrieving file
2263
contents more quickly than tree itself, i.e. a workingtree. tree
2264
will be used for cases where accelerator_tree's content is different.
2265
:param hardlink: If true, hard-link files to accelerator_tree, where
2266
possible. accelerator_tree must implement abspath, i.e. be a
2268
:param delta_from_tree: If true, build_tree may use the input Tree to
2269
generate the inventory delta.
2271
wt.lock_tree_write()
2275
if accelerator_tree is not None:
2276
accelerator_tree.lock_read()
2278
return _build_tree(tree, wt, accelerator_tree, hardlink,
2281
if accelerator_tree is not None:
2282
accelerator_tree.unlock()
2289
def _build_tree(tree, wt, accelerator_tree, hardlink, delta_from_tree):
2290
"""See build_tree."""
2291
for num, _unused in enumerate(wt.all_file_ids()):
2292
if num > 0: # more than just a root
2293
raise errors.WorkingTreeAlreadyPopulated(base=wt.basedir)
2295
top_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2296
pp = ProgressPhase("Build phase", 2, top_pb)
2297
if tree.inventory.root is not None:
2298
# This is kind of a hack: we should be altering the root
2299
# as part of the regular tree shape diff logic.
2300
# The conditional test here is to avoid doing an
2301
# expensive operation (flush) every time the root id
2302
# is set within the tree, nor setting the root and thus
2303
# marking the tree as dirty, because we use two different
2304
# idioms here: tree interfaces and inventory interfaces.
2305
if wt.get_root_id() != tree.get_root_id():
2306
wt.set_root_id(tree.get_root_id())
2308
tt = TreeTransform(wt)
2312
file_trans_id[wt.get_root_id()] = \
2313
tt.trans_id_tree_file_id(wt.get_root_id())
2314
pb = bzrlib.ui.ui_factory.nested_progress_bar()
2316
deferred_contents = []
2318
total = len(tree.inventory)
2320
precomputed_delta = []
2322
precomputed_delta = None
2323
# Check if tree inventory has content. If so, we populate
2324
# existing_files with the directory content. If there are no
2325
# entries we skip populating existing_files as its not used.
2326
# This improves performance and unncessary work on large
2327
# directory trees. (#501307)
2329
existing_files = set()
2330
for dir, files in wt.walkdirs():
2331
existing_files.update(f[0] for f in files)
2332
for num, (tree_path, entry) in \
2333
enumerate(tree.inventory.iter_entries_by_dir()):
2334
pb.update("Building tree", num - len(deferred_contents), total)
2335
if entry.parent_id is None:
2338
file_id = entry.file_id
2340
precomputed_delta.append((None, tree_path, file_id, entry))
2341
if tree_path in existing_files:
2342
target_path = wt.abspath(tree_path)
2343
kind = file_kind(target_path)
2344
if kind == "directory":
2346
bzrdir.BzrDir.open(target_path)
2347
except errors.NotBranchError:
2351
if (file_id not in divert and
2352
_content_match(tree, entry, file_id, kind,
2354
tt.delete_contents(tt.trans_id_tree_path(tree_path))
2355
if kind == 'directory':
2357
parent_id = file_trans_id[entry.parent_id]
2358
if entry.kind == 'file':
2359
# We *almost* replicate new_by_entry, so that we can defer
2360
# getting the file text, and get them all at once.
2361
trans_id = tt.create_path(entry.name, parent_id)
2362
file_trans_id[file_id] = trans_id
2363
tt.version_file(file_id, trans_id)
2364
executable = tree.is_executable(file_id, tree_path)
2366
tt.set_executability(executable, trans_id)
2367
trans_data = (trans_id, tree_path)
2368
deferred_contents.append((file_id, trans_data))
2370
file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
2373
new_trans_id = file_trans_id[file_id]
2374
old_parent = tt.trans_id_tree_path(tree_path)
2375
_reparent_children(tt, old_parent, new_trans_id)
2376
offset = num + 1 - len(deferred_contents)
2377
_create_files(tt, tree, deferred_contents, pb, offset,
2378
accelerator_tree, hardlink)
2382
divert_trans = set(file_trans_id[f] for f in divert)
2383
resolver = lambda t, c: resolve_checkout(t, c, divert_trans)
2384
raw_conflicts = resolve_conflicts(tt, pass_func=resolver)
2385
if len(raw_conflicts) > 0:
2386
precomputed_delta = None
2387
conflicts = cook_conflicts(raw_conflicts, tt)
2388
for conflict in conflicts:
2391
wt.add_conflicts(conflicts)
2392
except errors.UnsupportedOperation:
2394
result = tt.apply(no_conflicts=True,
2395
precomputed_delta=precomputed_delta)
2402
def _create_files(tt, tree, desired_files, pb, offset, accelerator_tree,
2404
total = len(desired_files) + offset
2406
if accelerator_tree is None:
2407
new_desired_files = desired_files
2409
iter = accelerator_tree.iter_changes(tree, include_unchanged=True)
2410
unchanged = [(f, p[1]) for (f, p, c, v, d, n, k, e)
2411
in iter if not (c or e[0] != e[1])]
2412
if accelerator_tree.supports_content_filtering():
2413
unchanged = [(f, p) for (f, p) in unchanged
2414
if not accelerator_tree.iter_search_rules([p]).next()]
2415
unchanged = dict(unchanged)
2416
new_desired_files = []
2418
for file_id, (trans_id, tree_path) in desired_files:
2419
accelerator_path = unchanged.get(file_id)
2420
if accelerator_path is None:
2421
new_desired_files.append((file_id, (trans_id, tree_path)))
2423
pb.update('Adding file contents', count + offset, total)
2425
tt.create_hardlink(accelerator_tree.abspath(accelerator_path),
2428
contents = accelerator_tree.get_file(file_id, accelerator_path)
2429
if wt.supports_content_filtering():
2430
filters = wt._content_filter_stack(tree_path)
2431
contents = filtered_output_bytes(contents, filters,
2432
ContentFilterContext(tree_path, tree))
2434
tt.create_file(contents, trans_id)
2438
except AttributeError:
2439
# after filtering, contents may no longer be file-like
2443
for count, ((trans_id, tree_path), contents) in enumerate(
2444
tree.iter_files_bytes(new_desired_files)):
2445
if wt.supports_content_filtering():
2446
filters = wt._content_filter_stack(tree_path)
2447
contents = filtered_output_bytes(contents, filters,
2448
ContentFilterContext(tree_path, tree))
2449
tt.create_file(contents, trans_id)
2450
pb.update('Adding file contents', count + offset, total)
2453
def _reparent_children(tt, old_parent, new_parent):
2454
for child in tt.iter_tree_children(old_parent):
2455
tt.adjust_path(tt.final_name(child), new_parent, child)
2457
def _reparent_transform_children(tt, old_parent, new_parent):
2458
by_parent = tt.by_parent()
2459
for child in by_parent[old_parent]:
2460
tt.adjust_path(tt.final_name(child), new_parent, child)
2461
return by_parent[old_parent]
2463
def _content_match(tree, entry, file_id, kind, target_path):
2464
if entry.kind != kind:
2466
if entry.kind == "directory":
2468
if entry.kind == "file":
2469
f = file(target_path, 'rb')
2471
if tree.get_file_text(file_id) == f.read():
2475
elif entry.kind == "symlink":
2476
if tree.get_symlink_target(file_id) == os.readlink(target_path):
2481
def resolve_checkout(tt, conflicts, divert):
2482
new_conflicts = set()
2483
for c_type, conflict in ((c[0], c) for c in conflicts):
2484
# Anything but a 'duplicate' would indicate programmer error
2485
if c_type != 'duplicate':
2486
raise AssertionError(c_type)
2487
# Now figure out which is new and which is old
2488
if tt.new_contents(conflict[1]):
2489
new_file = conflict[1]
2490
old_file = conflict[2]
2492
new_file = conflict[2]
2493
old_file = conflict[1]
2495
# We should only get here if the conflict wasn't completely
2497
final_parent = tt.final_parent(old_file)
2498
if new_file in divert:
2499
new_name = tt.final_name(old_file)+'.diverted'
2500
tt.adjust_path(new_name, final_parent, new_file)
2501
new_conflicts.add((c_type, 'Diverted to',
2502
new_file, old_file))
2504
new_name = tt.final_name(old_file)+'.moved'
2505
tt.adjust_path(new_name, final_parent, old_file)
2506
new_conflicts.add((c_type, 'Moved existing file to',
2507
old_file, new_file))
2508
return new_conflicts
2511
def new_by_entry(tt, entry, parent_id, tree):
2512
"""Create a new file according to its inventory entry"""
2516
contents = tree.get_file(entry.file_id).readlines()
2517
executable = tree.is_executable(entry.file_id)
2518
return tt.new_file(name, parent_id, contents, entry.file_id,
2520
elif kind in ('directory', 'tree-reference'):
2521
trans_id = tt.new_directory(name, parent_id, entry.file_id)
2522
if kind == 'tree-reference':
2523
tt.set_tree_reference(entry.reference_revision, trans_id)
2525
elif kind == 'symlink':
2526
target = tree.get_symlink_target(entry.file_id)
2527
return tt.new_symlink(name, parent_id, target, entry.file_id)
2529
raise errors.BadFileKindError(name, kind)
2532
def create_from_tree(tt, trans_id, tree, file_id, bytes=None,
2533
filter_tree_path=None):
2534
"""Create new file contents according to tree contents.
2536
:param filter_tree_path: the tree path to use to lookup
2537
content filters to apply to the bytes output in the working tree.
2538
This only applies if the working tree supports content filtering.
2540
kind = tree.kind(file_id)
2541
if kind == 'directory':
2542
tt.create_directory(trans_id)
2543
elif kind == "file":
2545
tree_file = tree.get_file(file_id)
2547
bytes = tree_file.readlines()
2551
if wt.supports_content_filtering() and filter_tree_path is not None:
2552
filters = wt._content_filter_stack(filter_tree_path)
2553
bytes = filtered_output_bytes(bytes, filters,
2554
ContentFilterContext(filter_tree_path, tree))
2555
tt.create_file(bytes, trans_id)
2556
elif kind == "symlink":
2557
tt.create_symlink(tree.get_symlink_target(file_id), trans_id)
2559
raise AssertionError('Unknown kind %r' % kind)
2562
def create_entry_executability(tt, entry, trans_id):
2563
"""Set the executability of a trans_id according to an inventory entry"""
2564
if entry.kind == "file":
2565
tt.set_executability(entry.executable, trans_id)
2568
def get_backup_name(entry, by_parent, parent_trans_id, tt):
2569
return _get_backup_name(entry.name, by_parent, parent_trans_id, tt)
2572
def _get_backup_name(name, by_parent, parent_trans_id, tt):
2573
"""Produce a backup-style name that appears to be available"""
2577
yield "%s.~%d~" % (name, counter)
2579
for new_name in name_gen():
2580
if not tt.has_named_child(by_parent, parent_trans_id, new_name):
2584
def _entry_changes(file_id, entry, working_tree):
2585
"""Determine in which ways the inventory entry has changed.
2587
Returns booleans: has_contents, content_mod, meta_mod
2588
has_contents means there are currently contents, but they differ
2589
contents_mod means contents need to be modified
2590
meta_mod means the metadata needs to be modified
2592
cur_entry = working_tree.inventory[file_id]
2594
working_kind = working_tree.kind(file_id)
2597
has_contents = False
2600
if has_contents is True:
2601
if entry.kind != working_kind:
2602
contents_mod, meta_mod = True, False
2604
cur_entry._read_tree_state(working_tree.id2path(file_id),
2606
contents_mod, meta_mod = entry.detect_changes(cur_entry)
2607
cur_entry._forget_tree_state()
2608
return has_contents, contents_mod, meta_mod
2611
def revert(working_tree, target_tree, filenames, backups=False,
2612
pb=None, change_reporter=None):
2613
"""Revert a working tree's contents to those of a target tree."""
2614
target_tree.lock_read()
2615
pb = ui.ui_factory.nested_progress_bar()
2616
tt = TreeTransform(working_tree, pb)
2618
pp = ProgressPhase("Revert phase", 3, pb)
2619
conflicts, merge_modified = _prepare_revert_transform(
2620
working_tree, target_tree, tt, filenames, backups, pp)
2622
change_reporter = delta._ChangeReporter(
2623
unversioned_filter=working_tree.is_ignored)
2624
delta.report_changes(tt.iter_changes(), change_reporter)
2625
for conflict in conflicts:
2629
working_tree.set_merge_modified(merge_modified)
2631
target_tree.unlock()
2637
def _prepare_revert_transform(working_tree, target_tree, tt, filenames,
2638
backups, pp, basis_tree=None,
2639
merge_modified=None):
2640
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2642
if merge_modified is None:
2643
merge_modified = working_tree.merge_modified()
2644
merge_modified = _alter_files(working_tree, target_tree, tt,
2645
child_pb, filenames, backups,
2646
merge_modified, basis_tree)
2649
child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2651
raw_conflicts = resolve_conflicts(tt, child_pb,
2652
lambda t, c: conflict_pass(t, c, target_tree))
2655
conflicts = cook_conflicts(raw_conflicts, tt)
2656
return conflicts, merge_modified
2659
def _alter_files(working_tree, target_tree, tt, pb, specific_files,
2660
backups, merge_modified, basis_tree=None):
2661
if basis_tree is not None:
2662
basis_tree.lock_read()
2663
change_list = target_tree.iter_changes(working_tree,
2664
specific_files=specific_files, pb=pb)
2665
if target_tree.get_root_id() is None:
2671
for id_num, (file_id, path, changed_content, versioned, parent, name,
2672
kind, executable) in enumerate(change_list):
2673
if skip_root and file_id[0] is not None and parent[0] is None:
2675
trans_id = tt.trans_id_file_id(file_id)
2678
keep_content = False
2679
if kind[0] == 'file' and (backups or kind[1] is None):
2680
wt_sha1 = working_tree.get_file_sha1(file_id)
2681
if merge_modified.get(file_id) != wt_sha1:
2682
# acquire the basis tree lazily to prevent the
2683
# expense of accessing it when it's not needed ?
2684
# (Guessing, RBC, 200702)
2685
if basis_tree is None:
2686
basis_tree = working_tree.basis_tree()
2687
basis_tree.lock_read()
2688
if file_id in basis_tree:
2689
if wt_sha1 != basis_tree.get_file_sha1(file_id):
2691
elif kind[1] is None and not versioned[1]:
2693
if kind[0] is not None:
2694
if not keep_content:
2695
tt.delete_contents(trans_id)
2696
elif kind[1] is not None:
2697
parent_trans_id = tt.trans_id_file_id(parent[0])
2698
by_parent = tt.by_parent()
2699
backup_name = _get_backup_name(name[0], by_parent,
2700
parent_trans_id, tt)
2701
tt.adjust_path(backup_name, parent_trans_id, trans_id)
2702
new_trans_id = tt.create_path(name[0], parent_trans_id)
2703
if versioned == (True, True):
2704
tt.unversion_file(trans_id)
2705
tt.version_file(file_id, new_trans_id)
2706
# New contents should have the same unix perms as old
2709
trans_id = new_trans_id
2710
if kind[1] in ('directory', 'tree-reference'):
2711
tt.create_directory(trans_id)
2712
if kind[1] == 'tree-reference':
2713
revision = target_tree.get_reference_revision(file_id,
2715
tt.set_tree_reference(revision, trans_id)
2716
elif kind[1] == 'symlink':
2717
tt.create_symlink(target_tree.get_symlink_target(file_id),
2719
elif kind[1] == 'file':
2720
deferred_files.append((file_id, (trans_id, mode_id)))
2721
if basis_tree is None:
2722
basis_tree = working_tree.basis_tree()
2723
basis_tree.lock_read()
2724
new_sha1 = target_tree.get_file_sha1(file_id)
2725
if (file_id in basis_tree and new_sha1 ==
2726
basis_tree.get_file_sha1(file_id)):
2727
if file_id in merge_modified:
2728
del merge_modified[file_id]
2730
merge_modified[file_id] = new_sha1
2732
# preserve the execute bit when backing up
2733
if keep_content and executable[0] == executable[1]:
2734
tt.set_executability(executable[1], trans_id)
2735
elif kind[1] is not None:
2736
raise AssertionError(kind[1])
2737
if versioned == (False, True):
2738
tt.version_file(file_id, trans_id)
2739
if versioned == (True, False):
2740
tt.unversion_file(trans_id)
2741
if (name[1] is not None and
2742
(name[0] != name[1] or parent[0] != parent[1])):
2743
if name[1] == '' and parent[1] is None:
2744
parent_trans = ROOT_PARENT
2746
parent_trans = tt.trans_id_file_id(parent[1])
2747
if parent[0] is None and versioned[0]:
2748
tt.adjust_root_path(name[1], parent_trans)
2750
tt.adjust_path(name[1], parent_trans, trans_id)
2751
if executable[0] != executable[1] and kind[1] == "file":
2752
tt.set_executability(executable[1], trans_id)
2753
if working_tree.supports_content_filtering():
2754
for index, ((trans_id, mode_id), bytes) in enumerate(
2755
target_tree.iter_files_bytes(deferred_files)):
2756
file_id = deferred_files[index][0]
2757
# We're reverting a tree to the target tree so using the
2758
# target tree to find the file path seems the best choice
2759
# here IMO - Ian C 27/Oct/2009
2760
filter_tree_path = target_tree.id2path(file_id)
2761
filters = working_tree._content_filter_stack(filter_tree_path)
2762
bytes = filtered_output_bytes(bytes, filters,
2763
ContentFilterContext(filter_tree_path, working_tree))
2764
tt.create_file(bytes, trans_id, mode_id)
2766
for (trans_id, mode_id), bytes in target_tree.iter_files_bytes(
2768
tt.create_file(bytes, trans_id, mode_id)
2769
tt.fixup_new_roots()
2771
if basis_tree is not None:
2773
return merge_modified
2776
def resolve_conflicts(tt, pb=None, pass_func=None):
2777
"""Make many conflict-resolution attempts, but die if they fail"""
2778
if pass_func is None:
2779
pass_func = conflict_pass
2780
new_conflicts = set()
2781
pb = ui.ui_factory.nested_progress_bar()
2784
pb.update('Resolution pass', n+1, 10)
2785
conflicts = tt.find_conflicts()
2786
if len(conflicts) == 0:
2787
return new_conflicts
2788
new_conflicts.update(pass_func(tt, conflicts))
2789
raise MalformedTransform(conflicts=conflicts)
2794
def conflict_pass(tt, conflicts, path_tree=None):
2795
"""Resolve some classes of conflicts.
2797
:param tt: The transform to resolve conflicts in
2798
:param conflicts: The conflicts to resolve
2799
:param path_tree: A Tree to get supplemental paths from
2801
new_conflicts = set()
2802
for c_type, conflict in ((c[0], c) for c in conflicts):
2803
if c_type == 'duplicate id':
2804
tt.unversion_file(conflict[1])
2805
new_conflicts.add((c_type, 'Unversioned existing file',
2806
conflict[1], conflict[2], ))
2807
elif c_type == 'duplicate':
2808
# files that were renamed take precedence
2809
final_parent = tt.final_parent(conflict[1])
2810
if tt.path_changed(conflict[1]):
2811
existing_file, new_file = conflict[2], conflict[1]
2813
existing_file, new_file = conflict[1], conflict[2]
2814
new_name = tt.final_name(existing_file)+'.moved'
2815
tt.adjust_path(new_name, final_parent, existing_file)
2816
new_conflicts.add((c_type, 'Moved existing file to',
2817
existing_file, new_file))
2818
elif c_type == 'parent loop':
2819
# break the loop by undoing one of the ops that caused the loop
2821
while not tt.path_changed(cur):
2822
cur = tt.final_parent(cur)
2823
new_conflicts.add((c_type, 'Cancelled move', cur,
2824
tt.final_parent(cur),))
2825
tt.adjust_path(tt.final_name(cur), tt.get_tree_parent(cur), cur)
2827
elif c_type == 'missing parent':
2828
trans_id = conflict[1]
2830
tt.cancel_deletion(trans_id)
2831
new_conflicts.add(('deleting parent', 'Not deleting',
2836
tt.final_name(trans_id)
2838
if path_tree is not None:
2839
file_id = tt.final_file_id(trans_id)
2841
file_id = tt.inactive_file_id(trans_id)
2842
entry = path_tree.inventory[file_id]
2843
# special-case the other tree root (move its
2844
# children to current root)
2845
if entry.parent_id is None:
2847
moved = _reparent_transform_children(
2848
tt, trans_id, tt.root)
2850
new_conflicts.add((c_type, 'Moved to root',
2853
parent_trans_id = tt.trans_id_file_id(
2855
tt.adjust_path(entry.name, parent_trans_id,
2858
tt.create_directory(trans_id)
2859
new_conflicts.add((c_type, 'Created directory', trans_id))
2860
elif c_type == 'unversioned parent':
2861
file_id = tt.inactive_file_id(conflict[1])
2862
# special-case the other tree root (move its children instead)
2863
if path_tree and file_id in path_tree:
2864
if path_tree.inventory[file_id].parent_id is None:
2866
tt.version_file(file_id, conflict[1])
2867
new_conflicts.add((c_type, 'Versioned directory', conflict[1]))
2868
elif c_type == 'non-directory parent':
2869
parent_id = conflict[1]
2870
parent_parent = tt.final_parent(parent_id)
2871
parent_name = tt.final_name(parent_id)
2872
parent_file_id = tt.final_file_id(parent_id)
2873
new_parent_id = tt.new_directory(parent_name + '.new',
2874
parent_parent, parent_file_id)
2875
_reparent_transform_children(tt, parent_id, new_parent_id)
2876
if parent_file_id is not None:
2877
tt.unversion_file(parent_id)
2878
new_conflicts.add((c_type, 'Created directory', new_parent_id))
2879
elif c_type == 'versioning no contents':
2880
tt.cancel_versioning(conflict[1])
2881
return new_conflicts
2884
def cook_conflicts(raw_conflicts, tt):
2885
"""Generate a list of cooked conflicts, sorted by file path"""
2886
from bzrlib.conflicts import Conflict
2887
conflict_iter = iter_cook_conflicts(raw_conflicts, tt)
2888
return sorted(conflict_iter, key=Conflict.sort_key)
2891
def iter_cook_conflicts(raw_conflicts, tt):
2892
from bzrlib.conflicts import Conflict
2894
for conflict in raw_conflicts:
2895
c_type = conflict[0]
2896
action = conflict[1]
2897
modified_path = fp.get_path(conflict[2])
2898
modified_id = tt.final_file_id(conflict[2])
2899
if len(conflict) == 3:
2900
yield Conflict.factory(c_type, action=action, path=modified_path,
2901
file_id=modified_id)
2904
conflicting_path = fp.get_path(conflict[3])
2905
conflicting_id = tt.final_file_id(conflict[3])
2906
yield Conflict.factory(c_type, action=action, path=modified_path,
2907
file_id=modified_id,
2908
conflict_path=conflicting_path,
2909
conflict_file_id=conflicting_id)
2912
class _FileMover(object):
2913
"""Moves and deletes files for TreeTransform, tracking operations"""
2916
self.past_renames = []
2917
self.pending_deletions = []
2919
def rename(self, from_, to):
2920
"""Rename a file from one path to another."""
2922
os.rename(from_, to)
2924
if e.errno in (errno.EEXIST, errno.ENOTEMPTY):
2925
raise errors.FileExists(to, str(e))
2926
# normal OSError doesn't include filenames so it's hard to see where
2927
# the problem is, see https://bugs.launchpad.net/bzr/+bug/491763
2928
raise errors.TransformRenameFailed(from_, to, str(e), e.errno)
2929
self.past_renames.append((from_, to))
2931
def pre_delete(self, from_, to):
2932
"""Rename a file out of the way and mark it for deletion.
2934
Unlike os.unlink, this works equally well for files and directories.
2935
:param from_: The current file path
2936
:param to: A temporary path for the file
2938
self.rename(from_, to)
2939
self.pending_deletions.append(to)
2942
"""Reverse all renames that have been performed"""
2943
for from_, to in reversed(self.past_renames):
2945
os.rename(to, from_)
2947
raise errors.TransformRenameFailed(to, from_, str(e), e.errno)
2948
# after rollback, don't reuse _FileMover
2950
pending_deletions = None
2952
def apply_deletions(self):
2953
"""Apply all marked deletions"""
2954
for path in self.pending_deletions:
2956
# after apply_deletions, don't reuse _FileMover
2958
pending_deletions = None