/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/transform.py

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from __future__ import absolute_import
18
 
 
19
17
import os
20
18
import errno
21
19
from stat import S_ISREG, S_IEXEC
22
20
import time
23
21
 
24
 
from . import (
25
 
    config as _mod_config,
26
 
    errors,
27
 
    lazy_import,
28
 
    registry,
29
 
    trace,
30
 
    tree,
31
 
    )
32
 
lazy_import.lazy_import(globals(), """
33
 
from breezy import (
 
22
from bzrlib.lazy_import import lazy_import
 
23
lazy_import(globals(), """
 
24
from bzrlib import (
34
25
    annotate,
35
26
    bencode,
36
 
    controldir,
37
 
    commit,
38
 
    conflicts,
 
27
    bzrdir,
39
28
    delta,
40
 
    lock,
 
29
    errors,
 
30
    inventory,
41
31
    multiparent,
42
32
    osutils,
43
33
    revision as _mod_revision,
44
34
    ui,
45
 
    urlutils,
46
 
    )
47
 
from breezy.bzr import (
48
 
    inventory,
49
 
    inventorytree,
50
 
    )
51
 
from breezy.i18n import gettext
 
35
    )
52
36
""")
53
 
from .errors import (DuplicateKey, MalformedTransform,
54
 
                     ReusingTransform, CantMoveRoot,
55
 
                     ImmortalLimbo, NoFinalPath)
56
 
from .filters import filtered_output_bytes, ContentFilterContext
57
 
from .mutabletree import MutableTree
58
 
from .osutils import (
 
37
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
 
38
                           ReusingTransform, CantMoveRoot,
 
39
                           ExistingLimbo, ImmortalLimbo, NoFinalPath,
 
40
                           UnableCreateSymlink)
 
41
from bzrlib.filters import filtered_output_bytes, ContentFilterContext
 
42
from bzrlib.inventory import InventoryEntry
 
43
from bzrlib.osutils import (
59
44
    delete_any,
60
45
    file_kind,
 
46
    has_symlinks,
 
47
    lexists,
61
48
    pathjoin,
62
49
    sha_file,
63
50
    splitpath,
64
 
    supports_symlinks,
65
 
    )
66
 
from .progress import ProgressPhase
67
 
from .sixish import (
68
 
    text_type,
69
 
    viewitems,
70
 
    viewvalues,
71
 
    )
72
 
from .tree import (
73
 
    find_previous_path,
74
 
    TreeChange,
75
 
    )
 
51
    supports_executable,
 
52
)
 
53
from bzrlib.progress import ProgressPhase
 
54
from bzrlib.symbol_versioning import (
 
55
        deprecated_function,
 
56
        deprecated_in,
 
57
        )
 
58
from bzrlib.trace import mutter, warning
 
59
from bzrlib import tree
 
60
import bzrlib.ui
 
61
import bzrlib.urlutils as urlutils
76
62
 
77
63
 
78
64
ROOT_PARENT = "root-parent"
94
80
class TreeTransformBase(object):
95
81
    """The base class for TreeTransform and its kin."""
96
82
 
97
 
    def __init__(self, tree, pb=None, case_sensitive=True):
 
83
    def __init__(self, tree, pb=None,
 
84
                 case_sensitive=True):
98
85
        """Constructor.
99
86
 
100
87
        :param tree: The tree that will be transformed, but not necessarily
112
99
        self._new_parent = {}
113
100
        # mapping of trans_id with new contents -> new file_kind
114
101
        self._new_contents = {}
115
 
        # mapping of trans_id => (sha1 of content, stat_value)
116
 
        self._observed_sha1s = {}
117
102
        # Set of trans_ids whose contents will be removed
118
103
        self._removed_contents = set()
119
104
        # Mapping of trans_id -> new execute-bit value
133
118
        # Mapping trans_id -> path in old tree
134
119
        self._tree_id_paths = {}
135
120
        # The trans_id that will be used as the tree root
136
 
        if tree.is_versioned(''):
137
 
            self._new_root = self.trans_id_tree_path('')
 
121
        root_id = tree.get_root_id()
 
122
        if root_id is not None:
 
123
            self._new_root = self.trans_id_tree_file_id(root_id)
138
124
        else:
139
125
            self._new_root = None
140
 
        # Indicator of whether the transform has been applied
 
126
        # Indictor of whether the transform has been applied
141
127
        self._done = False
142
128
        # A progress bar
143
129
        self._pb = pb
146
132
        # A counter of how many files have been renamed
147
133
        self.rename_count = 0
148
134
 
149
 
    def __enter__(self):
150
 
        """Support Context Manager API."""
151
 
        return self
152
 
 
153
 
    def __exit__(self, exc_type, exc_val, exc_tb):
154
 
        """Support Context Manager API."""
155
 
        self.finalize()
156
 
 
157
135
    def finalize(self):
158
136
        """Release the working tree lock, if held.
159
137
 
162
140
        """
163
141
        if self._tree is None:
164
142
            return
165
 
        for hook in MutableTree.hooks['post_transform']:
166
 
            hook(self._tree, self)
167
143
        self._tree.unlock()
168
144
        self._tree = None
169
145
 
175
151
    def _assign_id(self):
176
152
        """Produce a new tranform id"""
177
153
        new_id = "new-%s" % self._id_number
178
 
        self._id_number += 1
 
154
        self._id_number +=1
179
155
        return new_id
180
156
 
181
157
    def create_path(self, name, parent):
218
194
        # the physical root needs a new transaction id
219
195
        self._tree_path_ids.pop("")
220
196
        self._tree_id_paths.pop(old_root)
221
 
        self._new_root = self.trans_id_tree_path('')
 
197
        self._new_root = self.trans_id_tree_file_id(self._tree.get_root_id())
222
198
        if parent == old_root:
223
199
            parent = self._new_root
224
200
        self.adjust_path(name, parent, old_root)
236
212
        This means that the old root trans-id becomes obsolete, so it is
237
213
        recommended only to invoke this after the root trans-id has become
238
214
        irrelevant.
239
 
 
240
215
        """
241
 
        new_roots = [k for k, v in viewitems(self._new_parent)
242
 
                     if v == ROOT_PARENT]
 
216
        new_roots = [k for k, v in self._new_parent.iteritems() if v is
 
217
                     ROOT_PARENT]
243
218
        if len(new_roots) < 1:
244
219
            return
245
220
        if len(new_roots) != 1:
248
223
            self._new_root = new_roots[0]
249
224
            return
250
225
        old_new_root = new_roots[0]
 
226
        # TODO: What to do if a old_new_root is present, but self._new_root is
 
227
        #       not listed as being removed? This code explicitly unversions
 
228
        #       the old root and versions it with the new file_id. Though that
 
229
        #       seems like an incomplete delta
 
230
 
251
231
        # unversion the new root's directory.
252
 
        if self.final_kind(self._new_root) is None:
253
 
            file_id = self.final_file_id(old_new_root)
254
 
        else:
255
 
            file_id = self.final_file_id(self._new_root)
 
232
        file_id = self.final_file_id(old_new_root)
256
233
        if old_new_root in self._new_id:
257
234
            self.cancel_versioning(old_new_root)
258
235
        else:
259
236
            self.unversion_file(old_new_root)
260
237
        # if, at this stage, root still has an old file_id, zap it so we can
261
238
        # stick a new one in.
262
 
        if (self.tree_file_id(self._new_root) is not None
263
 
                and self._new_root not in self._removed_id):
 
239
        if (self.tree_file_id(self._new_root) is not None and
 
240
            self._new_root not in self._removed_id):
264
241
            self.unversion_file(self._new_root)
265
 
        if file_id is not None:
266
 
            self.version_file(file_id, self._new_root)
 
242
        self.version_file(file_id, self._new_root)
267
243
 
268
244
        # Now move children of new root into old root directory.
269
245
        # Ensure all children are registered with the transaction, but don't
287
263
        del self._new_parent[old_new_root]
288
264
        del self._new_name[old_new_root]
289
265
 
 
266
    def trans_id_tree_file_id(self, inventory_id):
 
267
        """Determine the transaction id of a working tree file.
 
268
 
 
269
        This reflects only files that already exist, not ones that will be
 
270
        added by transactions.
 
271
        """
 
272
        if inventory_id is None:
 
273
            raise ValueError('None is not a valid file id')
 
274
        path = self._tree.id2path(inventory_id)
 
275
        return self.trans_id_tree_path(path)
 
276
 
290
277
    def trans_id_file_id(self, file_id):
291
278
        """Determine or set the transaction id associated with a file ID.
292
279
        A new id is only created for file_ids that were never present.  If
299
286
            return self._r_new_id[file_id]
300
287
        else:
301
288
            try:
302
 
                path = self._tree.id2path(file_id)
303
 
            except errors.NoSuchId:
 
289
                self._tree.iter_entries_by_dir([file_id]).next()
 
290
            except StopIteration:
304
291
                if file_id in self._non_present_ids:
305
292
                    return self._non_present_ids[file_id]
306
293
                else:
308
295
                    self._non_present_ids[file_id] = trans_id
309
296
                    return trans_id
310
297
            else:
311
 
                return self.trans_id_tree_path(path)
 
298
                return self.trans_id_tree_file_id(file_id)
312
299
 
313
300
    def trans_id_tree_path(self, path):
314
301
        """Determine (and maybe set) the transaction ID for a tree path."""
327
314
 
328
315
    def delete_contents(self, trans_id):
329
316
        """Schedule the contents of a path entry for deletion"""
330
 
        kind = self.tree_kind(trans_id)
331
 
        if kind is not None:
332
 
            self._removed_contents.add(trans_id)
 
317
        # Ensure that the object exists in the WorkingTree, this will raise an
 
318
        # exception if there is a problem
 
319
        self.tree_kind(trans_id)
 
320
        self._removed_contents.add(trans_id)
333
321
 
334
322
    def cancel_deletion(self, trans_id):
335
323
        """Cancel a scheduled deletion"""
392
380
        return sorted(FinalPaths(self).get_paths(new_ids))
393
381
 
394
382
    def _inventory_altered(self):
395
 
        """Determine which trans_ids need new Inventory entries.
396
 
 
397
 
        An new entry is needed when anything that would be reflected by an
398
 
        inventory entry changes, including file name, file_id, parent file_id,
399
 
        file kind, and the execute bit.
400
 
 
401
 
        Some care is taken to return entries with real changes, not cases
402
 
        where the value is deleted and then restored to its original value,
403
 
        but some actually unchanged values may be returned.
404
 
 
405
 
        :returns: A list of (path, trans_id) for all items requiring an
406
 
            inventory change. Ordered by path.
407
 
        """
408
 
        changed_ids = set()
409
 
        # Find entries whose file_ids are new (or changed).
410
 
        new_file_id = set(t for t in self._new_id
411
 
                          if self._new_id[t] != self.tree_file_id(t))
412
 
        for id_set in [self._new_name, self._new_parent, new_file_id,
 
383
        """Get the trans_ids and paths of files needing new inv entries."""
 
384
        new_ids = set()
 
385
        for id_set in [self._new_name, self._new_parent, self._new_id,
413
386
                       self._new_executability]:
414
 
            changed_ids.update(id_set)
415
 
        # removing implies a kind change
 
387
            new_ids.update(id_set)
416
388
        changed_kind = set(self._removed_contents)
417
 
        # so does adding
418
389
        changed_kind.intersection_update(self._new_contents)
419
 
        # Ignore entries that are already known to have changed.
420
 
        changed_kind.difference_update(changed_ids)
421
 
        #  to keep only the truly changed ones
422
 
        changed_kind = (t for t in changed_kind
423
 
                        if self.tree_kind(t) != self.final_kind(t))
424
 
        # all kind changes will alter the inventory
425
 
        changed_ids.update(changed_kind)
426
 
        # To find entries with changed parent_ids, find parents which existed,
427
 
        # but changed file_id.
428
 
        # Now add all their children to the set.
429
 
        for parent_trans_id in new_file_id:
430
 
            changed_ids.update(self.iter_tree_children(parent_trans_id))
431
 
        return sorted(FinalPaths(self).get_paths(changed_ids))
 
390
        changed_kind.difference_update(new_ids)
 
391
        changed_kind = (t for t in changed_kind if self.tree_kind(t) !=
 
392
                        self.final_kind(t))
 
393
        new_ids.update(changed_kind)
 
394
        return sorted(FinalPaths(self).get_paths(new_ids))
432
395
 
433
396
    def final_kind(self, trans_id):
434
397
        """Determine the final file kind, after any changes applied.
435
398
 
436
 
        :return: None if the file does not exist/has no contents.  (It is
437
 
            conceivable that a path would be created without the corresponding
438
 
            contents insertion command)
 
399
        Raises NoSuchFile if the file does not exist/has no contents.
 
400
        (It is conceivable that a path would be created without the
 
401
        corresponding contents insertion command)
439
402
        """
440
403
        if trans_id in self._new_contents:
441
404
            return self._new_contents[trans_id]
442
405
        elif trans_id in self._removed_contents:
443
 
            return None
 
406
            raise NoSuchFile(None)
444
407
        else:
445
408
            return self.tree_kind(trans_id)
446
409
 
447
 
    def tree_path(self, trans_id):
448
 
        """Determine the tree path associated with the trans_id."""
449
 
        return self._tree_id_paths.get(trans_id)
450
 
 
451
410
    def tree_file_id(self, trans_id):
452
411
        """Determine the file id associated with the trans_id in the tree"""
453
 
        path = self.tree_path(trans_id)
454
 
        if path is None:
 
412
        try:
 
413
            path = self._tree_id_paths[trans_id]
 
414
        except KeyError:
 
415
            # the file is a new, unversioned file, or invalid trans_id
455
416
            return None
456
417
        # the file is old; the old id is still valid
457
418
        if self._new_root == trans_id:
479
440
        file_id = self.tree_file_id(trans_id)
480
441
        if file_id is not None:
481
442
            return file_id
482
 
        for key, value in viewitems(self._non_present_ids):
 
443
        for key, value in self._non_present_ids.iteritems():
483
444
            if value == trans_id:
484
445
                return key
485
446
 
509
470
        Only new paths and parents of tree files with assigned ids are used.
510
471
        """
511
472
        by_parent = {}
512
 
        items = list(viewitems(self._new_parent))
513
 
        items.extend((t, self.final_parent(t))
514
 
                     for t in list(self._tree_id_paths))
 
473
        items = list(self._new_parent.iteritems())
 
474
        items.extend((t, self.final_parent(t)) for t in
 
475
                      self._tree_id_paths.keys())
515
476
        for trans_id, parent_id in items:
516
477
            if parent_id not in by_parent:
517
478
                by_parent[parent_id] = set()
555
516
        Active parents are those which gain children, and those which are
556
517
        removed.  This is a necessary first step in detecting conflicts.
557
518
        """
558
 
        parents = list(self.by_parent())
 
519
        parents = self.by_parent().keys()
559
520
        parents.extend([t for t in self._removed_contents if
560
521
                        self.tree_kind(t) == 'directory'])
561
522
        for trans_id in self._removed_id:
562
 
            path = self.tree_path(trans_id)
563
 
            if path is not None:
564
 
                if self._tree.stored_kind(path) == 'directory':
 
523
            file_id = self.tree_file_id(trans_id)
 
524
            if file_id is not None:
 
525
                if self._tree.inventory[file_id].kind == 'directory':
565
526
                    parents.append(trans_id)
566
527
            elif self.tree_kind(trans_id) == 'directory':
567
528
                parents.append(trans_id)
570
531
            # ensure that all children are registered with the transaction
571
532
            list(self.iter_tree_children(parent_id))
572
533
 
573
 
    def _has_named_child(self, name, parent_id, known_children):
574
 
        """Does a parent already have a name child.
575
 
 
576
 
        :param name: The searched for name.
577
 
 
578
 
        :param parent_id: The parent for which the check is made.
579
 
 
580
 
        :param known_children: The already known children. This should have
581
 
            been recently obtained from `self.by_parent.get(parent_id)`
582
 
            (or will be if None is passed).
583
 
        """
584
 
        if known_children is None:
585
 
            known_children = self.by_parent().get(parent_id, [])
586
 
        for child in known_children:
 
534
    def has_named_child(self, by_parent, parent_id, name):
 
535
        try:
 
536
            children = by_parent[parent_id]
 
537
        except KeyError:
 
538
            children = []
 
539
        for child in children:
587
540
            if self.final_name(child) == name:
588
541
                return True
589
 
        parent_path = self._tree_id_paths.get(parent_id, None)
590
 
        if parent_path is None:
591
 
            # No parent... no children
 
542
        try:
 
543
            path = self._tree_id_paths[parent_id]
 
544
        except KeyError:
592
545
            return False
593
 
        child_path = joinpath(parent_path, name)
594
 
        child_id = self._tree_path_ids.get(child_path, None)
 
546
        childpath = joinpath(path, name)
 
547
        child_id = self._tree_path_ids.get(childpath)
595
548
        if child_id is None:
596
 
            # Not known by the tree transform yet, check the filesystem
597
 
            return osutils.lexists(self._tree.abspath(child_path))
 
549
            return lexists(self._tree.abspath(childpath))
598
550
        else:
599
 
            raise AssertionError('child_id is missing: %s, %s, %s'
600
 
                                 % (name, parent_id, child_id))
601
 
 
602
 
    def _available_backup_name(self, name, target_id):
603
 
        """Find an available backup name.
604
 
 
605
 
        :param name: The basename of the file.
606
 
 
607
 
        :param target_id: The directory trans_id where the backup should
608
 
            be placed.
609
 
        """
610
 
        known_children = self.by_parent().get(target_id, [])
611
 
        return osutils.available_backup_name(
612
 
            name,
613
 
            lambda base: self._has_named_child(
614
 
                base, target_id, known_children))
 
551
            if self.final_parent(child_id) != parent_id:
 
552
                return False
 
553
            if child_id in self._removed_contents:
 
554
                # XXX What about dangling file-ids?
 
555
                return False
 
556
            else:
 
557
                return True
615
558
 
616
559
    def _parent_loops(self):
617
560
        """No entry should be its own ancestor"""
619
562
        for trans_id in self._new_parent:
620
563
            seen = set()
621
564
            parent_id = trans_id
622
 
            while parent_id != ROOT_PARENT:
 
565
            while parent_id is not ROOT_PARENT:
623
566
                seen.add(parent_id)
624
567
                try:
625
568
                    parent_id = self.final_parent(parent_id)
634
577
    def _unversioned_parents(self, by_parent):
635
578
        """If parent directories are versioned, children must be versioned."""
636
579
        conflicts = []
637
 
        for parent_id, children in viewitems(by_parent):
638
 
            if parent_id == ROOT_PARENT:
 
580
        for parent_id, children in by_parent.iteritems():
 
581
            if parent_id is ROOT_PARENT:
639
582
                continue
640
583
            if self.final_file_id(parent_id) is not None:
641
584
                continue
642
585
            for child_id in children:
643
586
                if self.final_file_id(child_id) is not None:
644
587
                    conflicts.append(('unversioned parent', parent_id))
645
 
                    break
 
588
                    break;
646
589
        return conflicts
647
590
 
648
591
    def _improper_versioning(self):
651
594
        However, existing entries with no contents are okay.
652
595
        """
653
596
        conflicts = []
654
 
        for trans_id in self._new_id:
655
 
            kind = self.final_kind(trans_id)
656
 
            if kind == 'symlink' and not self._tree.supports_symlinks():
657
 
                # Ignore symlinks as they are not supported on this platform
658
 
                continue
659
 
            if kind is None:
 
597
        for trans_id in self._new_id.iterkeys():
 
598
            try:
 
599
                kind = self.final_kind(trans_id)
 
600
            except NoSuchFile:
660
601
                conflicts.append(('versioning no contents', trans_id))
661
602
                continue
662
 
            if not self._tree.versionable_kind(kind):
 
603
            if not InventoryEntry.versionable_kind(kind):
663
604
                conflicts.append(('versioning bad kind', trans_id, kind))
664
605
        return conflicts
665
606
 
676
617
            if self.final_file_id(trans_id) is None:
677
618
                conflicts.append(('unversioned executability', trans_id))
678
619
            else:
679
 
                if self.final_kind(trans_id) != "file":
 
620
                try:
 
621
                    non_file = self.final_kind(trans_id) != "file"
 
622
                except NoSuchFile:
 
623
                    non_file = True
 
624
                if non_file is True:
680
625
                    conflicts.append(('non-file executability', trans_id))
681
626
        return conflicts
682
627
 
684
629
        """Check for overwrites (not permitted on Win32)"""
685
630
        conflicts = []
686
631
        for trans_id in self._new_contents:
687
 
            if self.tree_kind(trans_id) is None:
 
632
            try:
 
633
                self.tree_kind(trans_id)
 
634
            except NoSuchFile:
688
635
                continue
689
636
            if trans_id not in self._removed_contents:
690
637
                conflicts.append(('overwrite', trans_id,
691
 
                                  self.final_name(trans_id)))
 
638
                                 self.final_name(trans_id)))
692
639
        return conflicts
693
640
 
694
641
    def _duplicate_entries(self, by_parent):
696
643
        conflicts = []
697
644
        if (self._new_name, self._new_parent) == ({}, {}):
698
645
            return conflicts
699
 
        for children in viewvalues(by_parent):
700
 
            name_ids = []
701
 
            for child_tid in children:
702
 
                name = self.final_name(child_tid)
703
 
                if name is not None:
704
 
                    # Keep children only if they still exist in the end
705
 
                    if not self._case_sensitive_target:
706
 
                        name = name.lower()
707
 
                    name_ids.append((name, child_tid))
 
646
        for children in by_parent.itervalues():
 
647
            name_ids = [(self.final_name(t), t) for t in children]
 
648
            if not self._case_sensitive_target:
 
649
                name_ids = [(n.lower(), t) for n, t in name_ids]
708
650
            name_ids.sort()
709
651
            last_name = None
710
652
            last_trans_id = None
711
653
            for name, trans_id in name_ids:
712
 
                kind = self.final_kind(trans_id)
 
654
                try:
 
655
                    kind = self.final_kind(trans_id)
 
656
                except NoSuchFile:
 
657
                    kind = None
713
658
                file_id = self.final_file_id(trans_id)
714
659
                if kind is None and file_id is None:
715
660
                    continue
716
661
                if name == last_name:
717
662
                    conflicts.append(('duplicate', last_trans_id, trans_id,
718
 
                                      name))
 
663
                    name))
719
664
                last_name = name
720
665
                last_trans_id = trans_id
721
666
        return conflicts
723
668
    def _duplicate_ids(self):
724
669
        """Each inventory id may only be used once"""
725
670
        conflicts = []
726
 
        try:
727
 
            all_ids = self._tree.all_file_ids()
728
 
        except errors.UnsupportedOperation:
729
 
            # it's okay for non-file-id trees to raise UnsupportedOperation.
730
 
            return []
731
671
        removed_tree_ids = set((self.tree_file_id(trans_id) for trans_id in
732
672
                                self._removed_id))
 
673
        all_ids = self._tree.all_file_ids()
733
674
        active_tree_ids = all_ids.difference(removed_tree_ids)
734
 
        for trans_id, file_id in viewitems(self._new_id):
 
675
        for trans_id, file_id in self._new_id.iteritems():
735
676
            if file_id in active_tree_ids:
736
 
                path = self._tree.id2path(file_id)
737
 
                old_trans_id = self.trans_id_tree_path(path)
 
677
                old_trans_id = self.trans_id_tree_file_id(file_id)
738
678
                conflicts.append(('duplicate id', old_trans_id, trans_id))
739
679
        return conflicts
740
680
 
741
681
    def _parent_type_conflicts(self, by_parent):
742
 
        """Children must have a directory parent"""
 
682
        """parents must have directory 'contents'."""
743
683
        conflicts = []
744
 
        for parent_id, children in viewitems(by_parent):
745
 
            if parent_id == ROOT_PARENT:
746
 
                continue
747
 
            no_children = True
748
 
            for child_id in children:
749
 
                if self.final_kind(child_id) is not None:
750
 
                    no_children = False
751
 
                    break
752
 
            if no_children:
753
 
                continue
754
 
            # There is at least a child, so we need an existing directory to
755
 
            # contain it.
756
 
            kind = self.final_kind(parent_id)
 
684
        for parent_id, children in by_parent.iteritems():
 
685
            if parent_id is ROOT_PARENT:
 
686
                continue
 
687
            if not self._any_contents(children):
 
688
                continue
 
689
            for child in children:
 
690
                try:
 
691
                    self.final_kind(child)
 
692
                except NoSuchFile:
 
693
                    continue
 
694
            try:
 
695
                kind = self.final_kind(parent_id)
 
696
            except NoSuchFile:
 
697
                kind = None
757
698
            if kind is None:
758
 
                # The directory will be deleted
759
699
                conflicts.append(('missing parent', parent_id))
760
700
            elif kind != "directory":
761
 
                # Meh, we need a *directory* to put something in it
762
701
                conflicts.append(('non-directory parent', parent_id))
763
702
        return conflicts
764
703
 
 
704
    def _any_contents(self, trans_ids):
 
705
        """Return true if any of the trans_ids, will have contents."""
 
706
        for trans_id in trans_ids:
 
707
            try:
 
708
                kind = self.final_kind(trans_id)
 
709
            except NoSuchFile:
 
710
                continue
 
711
            return True
 
712
        return False
 
713
 
765
714
    def _set_executability(self, path, trans_id):
766
715
        """Set the executability of versioned files """
767
 
        if self._tree._supports_executable():
 
716
        if supports_executable():
768
717
            new_executability = self._new_executability[trans_id]
769
718
            abspath = self._tree.abspath(path)
770
719
            current_mode = os.stat(abspath).st_mode
771
720
            if new_executability:
772
721
                umask = os.umask(0)
773
722
                os.umask(umask)
774
 
                to_mode = current_mode | (0o100 & ~umask)
 
723
                to_mode = current_mode | (0100 & ~umask)
775
724
                # Enable x-bit for others only if they can read it.
776
 
                if current_mode & 0o004:
777
 
                    to_mode |= 0o001 & ~umask
778
 
                if current_mode & 0o040:
779
 
                    to_mode |= 0o010 & ~umask
 
725
                if current_mode & 0004:
 
726
                    to_mode |= 0001 & ~umask
 
727
                if current_mode & 0040:
 
728
                    to_mode |= 0010 & ~umask
780
729
            else:
781
 
                to_mode = current_mode & ~0o111
782
 
            osutils.chmod_if_possible(abspath, to_mode)
 
730
                to_mode = current_mode & ~0111
 
731
            os.chmod(abspath, to_mode)
783
732
 
784
733
    def _new_entry(self, name, parent_id, file_id):
785
734
        """Helper function to create a new filesystem entry."""
789
738
        return trans_id
790
739
 
791
740
    def new_file(self, name, parent_id, contents, file_id=None,
792
 
                 executable=None, sha1=None):
 
741
                 executable=None):
793
742
        """Convenience method to create files.
794
743
 
795
744
        name is the name of the file to create.
802
751
        trans_id = self._new_entry(name, parent_id, file_id)
803
752
        # TODO: rather than scheduling a set_executable call,
804
753
        # have create_file create the file with the right mode.
805
 
        self.create_file(contents, trans_id, sha1=sha1)
 
754
        self.create_file(contents, trans_id)
806
755
        if executable is not None:
807
756
            self.set_executability(executable, trans_id)
808
757
        return trans_id
831
780
        self.create_symlink(target, trans_id)
832
781
        return trans_id
833
782
 
834
 
    def new_orphan(self, trans_id, parent_id):
835
 
        """Schedule an item to be orphaned.
836
 
 
837
 
        When a directory is about to be removed, its children, if they are not
838
 
        versioned are moved out of the way: they don't have a parent anymore.
839
 
 
840
 
        :param trans_id: The trans_id of the existing item.
841
 
        :param parent_id: The parent trans_id of the item.
842
 
        """
843
 
        raise NotImplementedError(self.new_orphan)
844
 
 
845
 
    def _get_potential_orphans(self, dir_id):
846
 
        """Find the potential orphans in a directory.
847
 
 
848
 
        A directory can't be safely deleted if there are versioned files in it.
849
 
        If all the contained files are unversioned then they can be orphaned.
850
 
 
851
 
        The 'None' return value means that the directory contains at least one
852
 
        versioned file and should not be deleted.
853
 
 
854
 
        :param dir_id: The directory trans id.
855
 
 
856
 
        :return: A list of the orphan trans ids or None if at least one
857
 
             versioned file is present.
858
 
        """
859
 
        orphans = []
860
 
        # Find the potential orphans, stop if one item should be kept
861
 
        for child_tid in self.by_parent()[dir_id]:
862
 
            if child_tid in self._removed_contents:
863
 
                # The child is removed as part of the transform. Since it was
864
 
                # versioned before, it's not an orphan
865
 
                continue
866
 
            elif self.final_file_id(child_tid) is None:
867
 
                # The child is not versioned
868
 
                orphans.append(child_tid)
869
 
            else:
870
 
                # We have a versioned file here, searching for orphans is
871
 
                # meaningless.
872
 
                orphans = None
873
 
                break
874
 
        return orphans
875
 
 
876
783
    def _affected_ids(self):
877
784
        """Return the set of transform ids affected by the transform"""
878
785
        trans_ids = set(self._removed_id)
879
 
        trans_ids.update(self._new_id)
 
786
        trans_ids.update(self._new_id.keys())
880
787
        trans_ids.update(self._removed_contents)
881
 
        trans_ids.update(self._new_contents)
882
 
        trans_ids.update(self._new_executability)
883
 
        trans_ids.update(self._new_name)
884
 
        trans_ids.update(self._new_parent)
 
788
        trans_ids.update(self._new_contents.keys())
 
789
        trans_ids.update(self._new_executability.keys())
 
790
        trans_ids.update(self._new_name.keys())
 
791
        trans_ids.update(self._new_parent.keys())
885
792
        return trans_ids
886
793
 
887
794
    def _get_file_id_maps(self):
900
807
                to_trans_ids[to_file_id] = trans_id
901
808
        return from_trans_ids, to_trans_ids
902
809
 
903
 
    def _from_file_data(self, from_trans_id, from_versioned, from_path):
 
810
    def _from_file_data(self, from_trans_id, from_versioned, file_id):
904
811
        """Get data about a file in the from (tree) state
905
812
 
906
813
        Return a (name, parent, kind, executable) tuple
908
815
        from_path = self._tree_id_paths.get(from_trans_id)
909
816
        if from_versioned:
910
817
            # get data from working tree if versioned
911
 
            from_entry = next(self._tree.iter_entries_by_dir(
912
 
                specific_files=[from_path]))[1]
 
818
            from_entry = self._tree.iter_entries_by_dir([file_id]).next()[1]
913
819
            from_name = from_entry.name
914
820
            from_parent = from_entry.parent_id
915
821
        else:
938
844
        Return a (name, parent, kind, executable) tuple
939
845
        """
940
846
        to_name = self.final_name(to_trans_id)
941
 
        to_kind = self.final_kind(to_trans_id)
 
847
        try:
 
848
            to_kind = self.final_kind(to_trans_id)
 
849
        except NoSuchFile:
 
850
            to_kind = None
942
851
        to_parent = self.final_file_id(self.final_parent(to_trans_id))
943
852
        if to_trans_id in self._new_executability:
944
853
            to_executable = self._new_executability[to_trans_id]
962
871
        from_trans_ids, to_trans_ids = self._get_file_id_maps()
963
872
        results = []
964
873
        # Now iterate through all active file_ids
965
 
        for file_id in set(from_trans_ids).union(to_trans_ids):
 
874
        for file_id in set(from_trans_ids.keys() + to_trans_ids.keys()):
966
875
            modified = False
967
876
            from_trans_id = from_trans_ids.get(file_id)
968
877
            # find file ids, and determine versioning state
978
887
            else:
979
888
                to_versioned = True
980
889
 
 
890
            from_name, from_parent, from_kind, from_executable = \
 
891
                self._from_file_data(from_trans_id, from_versioned, file_id)
 
892
 
 
893
            to_name, to_parent, to_kind, to_executable = \
 
894
                self._to_file_data(to_trans_id, from_trans_id, from_executable)
 
895
 
981
896
            if not from_versioned:
982
897
                from_path = None
983
898
            else:
986
901
                to_path = None
987
902
            else:
988
903
                to_path = final_paths.get_path(to_trans_id)
989
 
 
990
 
            from_name, from_parent, from_kind, from_executable = \
991
 
                self._from_file_data(from_trans_id, from_versioned, from_path)
992
 
 
993
 
            to_name, to_parent, to_kind, to_executable = \
994
 
                self._to_file_data(to_trans_id, from_trans_id, from_executable)
995
 
 
996
904
            if from_kind != to_kind:
997
905
                modified = True
998
906
            elif to_kind in ('file', 'symlink') and (
999
 
                    to_trans_id != from_trans_id
1000
 
                    or to_trans_id in self._new_contents):
 
907
                to_trans_id != from_trans_id or
 
908
                to_trans_id in self._new_contents):
1001
909
                modified = True
1002
 
            if (not modified and from_versioned == to_versioned
1003
 
                and from_parent == to_parent and from_name == to_name
1004
 
                    and from_executable == to_executable):
 
910
            if (not modified and from_versioned == to_versioned and
 
911
                from_parent==to_parent and from_name == to_name and
 
912
                from_executable == to_executable):
1005
913
                continue
1006
 
            results.append(
1007
 
                TreeChange(
1008
 
                    file_id, (from_path, to_path), modified,
1009
 
                    (from_versioned, to_versioned),
1010
 
                    (from_parent, to_parent),
1011
 
                    (from_name, to_name),
1012
 
                    (from_kind, to_kind),
1013
 
                    (from_executable, to_executable)))
1014
 
 
1015
 
        def path_key(c):
1016
 
            return (c.path[0] or '', c.path[1] or '')
1017
 
        return iter(sorted(results, key=path_key))
 
914
            results.append((file_id, (from_path, to_path), modified,
 
915
                   (from_versioned, to_versioned),
 
916
                   (from_parent, to_parent),
 
917
                   (from_name, to_name),
 
918
                   (from_kind, to_kind),
 
919
                   (from_executable, to_executable)))
 
920
        return iter(sorted(results, key=lambda x:x[1]))
1018
921
 
1019
922
    def get_preview_tree(self):
1020
923
        """Return a tree representing the result of the transform.
1024
927
        """
1025
928
        return _PreviewTree(self)
1026
929
 
1027
 
    def commit(self, branch, message, merge_parents=None, strict=False,
1028
 
               timestamp=None, timezone=None, committer=None, authors=None,
1029
 
               revprops=None, revision_id=None):
 
930
    def commit(self, branch, message, merge_parents=None, strict=False):
1030
931
        """Commit the result of this TreeTransform to a branch.
1031
932
 
1032
933
        :param branch: The branch to commit to.
1033
934
        :param message: The message to attach to the commit.
1034
 
        :param merge_parents: Additional parent revision-ids specified by
1035
 
            pending merges.
1036
 
        :param strict: If True, abort the commit if there are unversioned
1037
 
            files.
1038
 
        :param timestamp: if not None, seconds-since-epoch for the time and
1039
 
            date.  (May be a float.)
1040
 
        :param timezone: Optional timezone for timestamp, as an offset in
1041
 
            seconds.
1042
 
        :param committer: Optional committer in email-id format.
1043
 
            (e.g. "J Random Hacker <jrandom@example.com>")
1044
 
        :param authors: Optional list of authors in email-id format.
1045
 
        :param revprops: Optional dictionary of revision properties.
1046
 
        :param revision_id: Optional revision id.  (Specifying a revision-id
1047
 
            may reduce performance for some non-native formats.)
 
935
        :param merge_parents: Additional parents specified by pending merges.
1048
936
        :return: The revision_id of the revision committed.
1049
937
        """
1050
938
        self._check_malformed()
1066
954
                parent_ids.extend(merge_parents)
1067
955
        if self._tree.get_revision_id() != last_rev_id:
1068
956
            raise ValueError('TreeTransform not based on branch basis: %s' %
1069
 
                             self._tree.get_revision_id().decode('utf-8'))
1070
 
        revprops = commit.Commit.update_revprops(revprops, branch, authors)
1071
 
        builder = branch.get_commit_builder(parent_ids,
1072
 
                                            timestamp=timestamp,
1073
 
                                            timezone=timezone,
1074
 
                                            committer=committer,
1075
 
                                            revprops=revprops,
1076
 
                                            revision_id=revision_id)
 
957
                             self._tree.get_revision_id())
 
958
        builder = branch.get_commit_builder(parent_ids)
1077
959
        preview = self.get_preview_tree()
1078
960
        list(builder.record_iter_changes(preview, last_rev_id,
1079
961
                                         self.iter_changes()))
1083
965
        return revision_id
1084
966
 
1085
967
    def _text_parent(self, trans_id):
1086
 
        path = self.tree_path(trans_id)
 
968
        file_id = self.tree_file_id(trans_id)
1087
969
        try:
1088
 
            if path is None or self._tree.kind(path) != 'file':
 
970
            if file_id is None or self._tree.kind(file_id) != 'file':
1089
971
                return None
1090
972
        except errors.NoSuchFile:
1091
973
            return None
1092
 
        return path
 
974
        return file_id
1093
975
 
1094
976
    def _get_parents_texts(self, trans_id):
1095
977
        """Get texts for compression parents of this file."""
1096
 
        path = self._text_parent(trans_id)
1097
 
        if path is None:
 
978
        file_id = self._text_parent(trans_id)
 
979
        if file_id is None:
1098
980
            return ()
1099
 
        return (self._tree.get_file_text(path),)
 
981
        return (self._tree.get_file_text(file_id),)
1100
982
 
1101
983
    def _get_parents_lines(self, trans_id):
1102
984
        """Get lines for compression parents of this file."""
1103
 
        path = self._text_parent(trans_id)
1104
 
        if path is None:
 
985
        file_id = self._text_parent(trans_id)
 
986
        if file_id is None:
1105
987
            return ()
1106
 
        return (self._tree.get_file_lines(path),)
 
988
        return (self._tree.get_file_lines(file_id),)
1107
989
 
1108
990
    def serialize(self, serializer):
1109
991
        """Serialize this TreeTransform.
1110
992
 
1111
993
        :param serializer: A Serialiser like pack.ContainerSerializer.
1112
994
        """
1113
 
        new_name = {k.encode('utf-8'): v.encode('utf-8')
1114
 
                    for k, v in viewitems(self._new_name)}
1115
 
        new_parent = {k.encode('utf-8'): v.encode('utf-8')
1116
 
                      for k, v in viewitems(self._new_parent)}
1117
 
        new_id = {k.encode('utf-8'): v
1118
 
                  for k, v in viewitems(self._new_id)}
1119
 
        new_executability = {k.encode('utf-8'): int(v)
1120
 
                             for k, v in viewitems(self._new_executability)}
1121
 
        tree_path_ids = {k.encode('utf-8'): v.encode('utf-8')
1122
 
                         for k, v in viewitems(self._tree_path_ids)}
1123
 
        non_present_ids = {k: v.encode('utf-8')
1124
 
                           for k, v in viewitems(self._non_present_ids)}
1125
 
        removed_contents = [trans_id.encode('utf-8')
1126
 
                            for trans_id in self._removed_contents]
1127
 
        removed_id = [trans_id.encode('utf-8')
1128
 
                      for trans_id in self._removed_id]
 
995
        new_name = dict((k, v.encode('utf-8')) for k, v in
 
996
                        self._new_name.items())
 
997
        new_executability = dict((k, int(v)) for k, v in
 
998
                                 self._new_executability.items())
 
999
        tree_path_ids = dict((k.encode('utf-8'), v)
 
1000
                             for k, v in self._tree_path_ids.items())
1129
1001
        attribs = {
1130
 
            b'_id_number': self._id_number,
1131
 
            b'_new_name': new_name,
1132
 
            b'_new_parent': new_parent,
1133
 
            b'_new_executability': new_executability,
1134
 
            b'_new_id': new_id,
1135
 
            b'_tree_path_ids': tree_path_ids,
1136
 
            b'_removed_id': removed_id,
1137
 
            b'_removed_contents': removed_contents,
1138
 
            b'_non_present_ids': non_present_ids,
 
1002
            '_id_number': self._id_number,
 
1003
            '_new_name': new_name,
 
1004
            '_new_parent': self._new_parent,
 
1005
            '_new_executability': new_executability,
 
1006
            '_new_id': self._new_id,
 
1007
            '_tree_path_ids': tree_path_ids,
 
1008
            '_removed_id': list(self._removed_id),
 
1009
            '_removed_contents': list(self._removed_contents),
 
1010
            '_non_present_ids': self._non_present_ids,
1139
1011
            }
1140
1012
        yield serializer.bytes_record(bencode.bencode(attribs),
1141
 
                                      ((b'attribs',),))
1142
 
        for trans_id, kind in sorted(viewitems(self._new_contents)):
 
1013
                                      (('attribs',),))
 
1014
        for trans_id, kind in self._new_contents.items():
1143
1015
            if kind == 'file':
1144
 
                with open(self._limbo_name(trans_id), 'rb') as cur_file:
1145
 
                    lines = cur_file.readlines()
 
1016
                lines = osutils.chunks_to_lines(
 
1017
                    self._read_file_chunks(trans_id))
1146
1018
                parents = self._get_parents_lines(trans_id)
1147
1019
                mpdiff = multiparent.MultiParent.from_lines(lines, parents)
1148
 
                content = b''.join(mpdiff.to_patch())
 
1020
                content = ''.join(mpdiff.to_patch())
1149
1021
            if kind == 'directory':
1150
 
                content = b''
 
1022
                content = ''
1151
1023
            if kind == 'symlink':
1152
1024
                content = self._read_symlink_target(trans_id)
1153
 
                if not isinstance(content, bytes):
1154
 
                    content = content.encode('utf-8')
1155
 
            yield serializer.bytes_record(
1156
 
                content, ((trans_id.encode('utf-8'), kind.encode('ascii')),))
 
1025
            yield serializer.bytes_record(content, ((trans_id, kind),))
1157
1026
 
1158
1027
    def deserialize(self, records):
1159
1028
        """Deserialize a stored TreeTransform.
1161
1030
        :param records: An iterable of (names, content) tuples, as per
1162
1031
            pack.ContainerPushParser.
1163
1032
        """
1164
 
        names, content = next(records)
 
1033
        names, content = records.next()
1165
1034
        attribs = bencode.bdecode(content)
1166
 
        self._id_number = attribs[b'_id_number']
1167
 
        self._new_name = {k.decode('utf-8'): v.decode('utf-8')
1168
 
                          for k, v in viewitems(attribs[b'_new_name'])}
1169
 
        self._new_parent = {k.decode('utf-8'): v.decode('utf-8')
1170
 
                            for k, v in viewitems(attribs[b'_new_parent'])}
1171
 
        self._new_executability = {
1172
 
            k.decode('utf-8'): bool(v)
1173
 
            for k, v in viewitems(attribs[b'_new_executability'])}
1174
 
        self._new_id = {k.decode('utf-8'): v
1175
 
                        for k, v in viewitems(attribs[b'_new_id'])}
1176
 
        self._r_new_id = {v: k for k, v in viewitems(self._new_id)}
 
1035
        self._id_number = attribs['_id_number']
 
1036
        self._new_name = dict((k, v.decode('utf-8'))
 
1037
                            for k, v in attribs['_new_name'].items())
 
1038
        self._new_parent = attribs['_new_parent']
 
1039
        self._new_executability = dict((k, bool(v)) for k, v in
 
1040
            attribs['_new_executability'].items())
 
1041
        self._new_id = attribs['_new_id']
 
1042
        self._r_new_id = dict((v, k) for k, v in self._new_id.items())
1177
1043
        self._tree_path_ids = {}
1178
1044
        self._tree_id_paths = {}
1179
 
        for bytepath, trans_id in viewitems(attribs[b'_tree_path_ids']):
 
1045
        for bytepath, trans_id in attribs['_tree_path_ids'].items():
1180
1046
            path = bytepath.decode('utf-8')
1181
 
            trans_id = trans_id.decode('utf-8')
1182
1047
            self._tree_path_ids[path] = trans_id
1183
1048
            self._tree_id_paths[trans_id] = path
1184
 
        self._removed_id = {trans_id.decode('utf-8')
1185
 
                            for trans_id in attribs[b'_removed_id']}
1186
 
        self._removed_contents = set(
1187
 
            trans_id.decode('utf-8')
1188
 
            for trans_id in attribs[b'_removed_contents'])
1189
 
        self._non_present_ids = {
1190
 
            k: v.decode('utf-8')
1191
 
            for k, v in viewitems(attribs[b'_non_present_ids'])}
 
1049
        self._removed_id = set(attribs['_removed_id'])
 
1050
        self._removed_contents = set(attribs['_removed_contents'])
 
1051
        self._non_present_ids = attribs['_non_present_ids']
1192
1052
        for ((trans_id, kind),), content in records:
1193
 
            trans_id = trans_id.decode('utf-8')
1194
 
            kind = kind.decode('ascii')
1195
1053
            if kind == 'file':
1196
1054
                mpdiff = multiparent.MultiParent.from_patch(content)
1197
1055
                lines = mpdiff.to_lines(self._get_parents_texts(trans_id))
1201
1059
            if kind == 'symlink':
1202
1060
                self.create_symlink(content.decode('utf-8'), trans_id)
1203
1061
 
1204
 
    def create_file(self, contents, trans_id, mode_id=None, sha1=None):
1205
 
        """Schedule creation of a new file.
1206
 
 
1207
 
        :seealso: new_file.
1208
 
 
1209
 
        :param contents: an iterator of strings, all of which will be written
1210
 
            to the target destination.
1211
 
        :param trans_id: TreeTransform handle
1212
 
        :param mode_id: If not None, force the mode of the target file to match
1213
 
            the mode of the object referenced by mode_id.
1214
 
            Otherwise, we will try to preserve mode bits of an existing file.
1215
 
        :param sha1: If the sha1 of this content is already known, pass it in.
1216
 
            We can use it to prevent future sha1 computations.
1217
 
        """
1218
 
        raise NotImplementedError(self.create_file)
1219
 
 
1220
 
    def create_directory(self, trans_id):
1221
 
        """Schedule creation of a new directory.
1222
 
 
1223
 
        See also new_directory.
1224
 
        """
1225
 
        raise NotImplementedError(self.create_directory)
1226
 
 
1227
 
    def create_symlink(self, target, trans_id):
1228
 
        """Schedule creation of a new symbolic link.
1229
 
 
1230
 
        target is a bytestring.
1231
 
        See also new_symlink.
1232
 
        """
1233
 
        raise NotImplementedError(self.create_symlink)
1234
 
 
1235
 
    def create_hardlink(self, path, trans_id):
1236
 
        """Schedule creation of a hard link"""
1237
 
        raise NotImplementedError(self.create_hardlink)
1238
 
 
1239
 
    def cancel_creation(self, trans_id):
1240
 
        """Cancel the creation of new file contents."""
1241
 
        raise NotImplementedError(self.cancel_creation)
1242
 
 
1243
1062
 
1244
1063
class DiskTreeTransform(TreeTransformBase):
1245
1064
    """Tree transform storing its contents on disk."""
1246
1065
 
1247
 
    def __init__(self, tree, limbodir, pb=None, case_sensitive=True):
 
1066
    def __init__(self, tree, limbodir, pb=None,
 
1067
                 case_sensitive=True):
1248
1068
        """Constructor.
1249
1069
        :param tree: The tree that will be transformed, but not necessarily
1250
1070
            the output tree.
1259
1079
        self._deletiondir = None
1260
1080
        # A mapping of transform ids to their limbo filename
1261
1081
        self._limbo_files = {}
1262
 
        self._possibly_stale_limbo_files = set()
1263
1082
        # A mapping of transform ids to a set of the transform ids of children
1264
1083
        # that their limbo directory has
1265
1084
        self._limbo_children = {}
1268
1087
        # List of transform ids that need to be renamed from limbo into place
1269
1088
        self._needs_rename = set()
1270
1089
        self._creation_mtime = None
1271
 
        self._create_symlinks = osutils.supports_symlinks(self._limbodir)
1272
1090
 
1273
1091
    def finalize(self):
1274
1092
        """Release the working tree lock, if held, clean up limbo dir.
1279
1097
        if self._tree is None:
1280
1098
            return
1281
1099
        try:
1282
 
            limbo_paths = list(viewvalues(self._limbo_files))
1283
 
            limbo_paths.extend(self._possibly_stale_limbo_files)
1284
 
            limbo_paths.sort(reverse=True)
1285
 
            for path in limbo_paths:
1286
 
                try:
1287
 
                    delete_any(path)
1288
 
                except OSError as e:
1289
 
                    if e.errno != errno.ENOENT:
1290
 
                        raise
1291
 
                    # XXX: warn? perhaps we just got interrupted at an
1292
 
                    # inconvenient moment, but perhaps files are disappearing
1293
 
                    # from under us?
 
1100
            entries = [(self._limbo_name(t), t, k) for t, k in
 
1101
                       self._new_contents.iteritems()]
 
1102
            entries.sort(reverse=True)
 
1103
            for path, trans_id, kind in entries:
 
1104
                delete_any(path)
1294
1105
            try:
1295
1106
                delete_any(self._limbodir)
1296
1107
            except OSError:
1304
1115
        finally:
1305
1116
            TreeTransformBase.finalize(self)
1306
1117
 
1307
 
    def _limbo_supports_executable(self):
1308
 
        """Check if the limbo path supports the executable bit."""
1309
 
        return osutils.supports_executable(self._limbodir)
1310
 
 
1311
1118
    def _limbo_name(self, trans_id):
1312
1119
        """Generate the limbo name of a file"""
1313
1120
        limbo_name = self._limbo_files.get(trans_id)
1329
1136
        previous_parent = self._new_parent.get(trans_id)
1330
1137
        previous_name = self._new_name.get(trans_id)
1331
1138
        TreeTransformBase.adjust_path(self, name, parent, trans_id)
1332
 
        if (trans_id in self._limbo_files
1333
 
                and trans_id not in self._needs_rename):
 
1139
        if (trans_id in self._limbo_files and
 
1140
            trans_id not in self._needs_rename):
1334
1141
            self._rename_in_limbo([trans_id])
1335
1142
            if previous_parent != parent:
1336
1143
                self._limbo_children[previous_parent].remove(trans_id)
1349
1156
        entries from _limbo_files, because they are now stale.
1350
1157
        """
1351
1158
        for trans_id in trans_ids:
1352
 
            old_path = self._limbo_files[trans_id]
1353
 
            self._possibly_stale_limbo_files.add(old_path)
1354
 
            del self._limbo_files[trans_id]
 
1159
            old_path = self._limbo_files.pop(trans_id)
1355
1160
            if trans_id not in self._new_contents:
1356
1161
                continue
1357
1162
            new_path = self._limbo_name(trans_id)
1358
 
            os.rename(old_path, new_path)
1359
 
            self._possibly_stale_limbo_files.remove(old_path)
 
1163
            osutils.rename(old_path, new_path)
1360
1164
            for descendant in self._limbo_descendants(trans_id):
1361
1165
                desc_path = self._limbo_files[descendant]
1362
1166
                desc_path = new_path + desc_path[len(old_path):]
1369
1173
            descendants.update(self._limbo_descendants(descendant))
1370
1174
        return descendants
1371
1175
 
1372
 
    def create_file(self, contents, trans_id, mode_id=None, sha1=None):
 
1176
    def create_file(self, contents, trans_id, mode_id=None):
1373
1177
        """Schedule creation of a new file.
1374
1178
 
1375
 
        :seealso: new_file.
1376
 
 
1377
 
        :param contents: an iterator of strings, all of which will be written
1378
 
            to the target destination.
1379
 
        :param trans_id: TreeTransform handle
1380
 
        :param mode_id: If not None, force the mode of the target file to match
1381
 
            the mode of the object referenced by mode_id.
1382
 
            Otherwise, we will try to preserve mode bits of an existing file.
1383
 
        :param sha1: If the sha1 of this content is already known, pass it in.
1384
 
            We can use it to prevent future sha1 computations.
 
1179
        See also new_file.
 
1180
 
 
1181
        Contents is an iterator of strings, all of which will be written
 
1182
        to the target destination.
 
1183
 
 
1184
        New file takes the permissions of any existing file with that id,
 
1185
        unless mode_id is specified.
1385
1186
        """
1386
1187
        name = self._limbo_name(trans_id)
1387
 
        with open(name, 'wb') as f:
1388
 
            unique_add(self._new_contents, trans_id, 'file')
 
1188
        f = open(name, 'wb')
 
1189
        try:
 
1190
            try:
 
1191
                unique_add(self._new_contents, trans_id, 'file')
 
1192
            except:
 
1193
                # Clean up the file, it never got registered so
 
1194
                # TreeTransform.finalize() won't clean it up.
 
1195
                f.close()
 
1196
                os.unlink(name)
 
1197
                raise
 
1198
 
1389
1199
            f.writelines(contents)
 
1200
        finally:
 
1201
            f.close()
1390
1202
        self._set_mtime(name)
1391
1203
        self._set_mode(trans_id, mode_id, S_ISREG)
1392
 
        # It is unfortunate we have to use lstat instead of fstat, but we just
1393
 
        # used utime and chmod on the file, so we need the accurate final
1394
 
        # details.
1395
 
        if sha1 is not None:
1396
 
            self._observed_sha1s[trans_id] = (sha1, osutils.lstat(name))
 
1204
 
 
1205
    def _read_file_chunks(self, trans_id):
 
1206
        cur_file = open(self._limbo_name(trans_id), 'rb')
 
1207
        try:
 
1208
            return cur_file.readlines()
 
1209
        finally:
 
1210
            cur_file.close()
1397
1211
 
1398
1212
    def _read_symlink_target(self, trans_id):
1399
1213
        return os.readlink(self._limbo_name(trans_id))
1412
1226
        name = self._limbo_name(trans_id)
1413
1227
        try:
1414
1228
            os.link(path, name)
1415
 
        except OSError as e:
 
1229
        except OSError, e:
1416
1230
            if e.errno != errno.EPERM:
1417
1231
                raise
1418
1232
            raise errors.HardLinkNotSupported(path)
1419
1233
        try:
1420
1234
            unique_add(self._new_contents, trans_id, 'file')
1421
 
        except BaseException:
 
1235
        except:
1422
1236
            # Clean up the file, it never got registered so
1423
1237
            # TreeTransform.finalize() won't clean it up.
1424
1238
            os.unlink(name)
1438
1252
        target is a bytestring.
1439
1253
        See also new_symlink.
1440
1254
        """
1441
 
        if self._create_symlinks:
 
1255
        if has_symlinks():
1442
1256
            os.symlink(target, self._limbo_name(trans_id))
 
1257
            unique_add(self._new_contents, trans_id, 'symlink')
1443
1258
        else:
1444
1259
            try:
1445
1260
                path = FinalPaths(self).get_path(trans_id)
1446
1261
            except KeyError:
1447
1262
                path = None
1448
 
            trace.warning(
1449
 
                'Unable to create symlink "%s" on this filesystem.' % (path,))
1450
 
        # We add symlink to _new_contents even if they are unsupported
1451
 
        # and not created. These entries are subsequently used to avoid
1452
 
        # conflicts on platforms that don't support symlink
1453
 
        unique_add(self._new_contents, trans_id, 'symlink')
 
1263
            raise UnableCreateSymlink(path=path)
1454
1264
 
1455
1265
    def cancel_creation(self, trans_id):
1456
1266
        """Cancel the creation of new file contents."""
1457
1267
        del self._new_contents[trans_id]
1458
 
        if trans_id in self._observed_sha1s:
1459
 
            del self._observed_sha1s[trans_id]
1460
1268
        children = self._limbo_children.get(trans_id)
1461
1269
        # if this is a limbo directory with children, move them before removing
1462
1270
        # the directory
1466
1274
            del self._limbo_children_names[trans_id]
1467
1275
        delete_any(self._limbo_name(trans_id))
1468
1276
 
1469
 
    def new_orphan(self, trans_id, parent_id):
1470
 
        conf = self._tree.get_config_stack()
1471
 
        handle_orphan = conf.get('transform.orphan_policy')
1472
 
        handle_orphan(self, trans_id, parent_id)
1473
 
 
1474
 
 
1475
 
class OrphaningError(errors.BzrError):
1476
 
 
1477
 
    # Only bugs could lead to such exception being seen by the user
1478
 
    internal_error = True
1479
 
    _fmt = "Error while orphaning %s in %s directory"
1480
 
 
1481
 
    def __init__(self, orphan, parent):
1482
 
        errors.BzrError.__init__(self)
1483
 
        self.orphan = orphan
1484
 
        self.parent = parent
1485
 
 
1486
 
 
1487
 
class OrphaningForbidden(OrphaningError):
1488
 
 
1489
 
    _fmt = "Policy: %s doesn't allow creating orphans."
1490
 
 
1491
 
    def __init__(self, policy):
1492
 
        errors.BzrError.__init__(self)
1493
 
        self.policy = policy
1494
 
 
1495
 
 
1496
 
def move_orphan(tt, orphan_id, parent_id):
1497
 
    """See TreeTransformBase.new_orphan.
1498
 
 
1499
 
    This creates a new orphan in the `brz-orphans` dir at the root of the
1500
 
    `TreeTransform`.
1501
 
 
1502
 
    :param tt: The TreeTransform orphaning `trans_id`.
1503
 
 
1504
 
    :param orphan_id: The trans id that should be orphaned.
1505
 
 
1506
 
    :param parent_id: The orphan parent trans id.
1507
 
    """
1508
 
    # Add the orphan dir if it doesn't exist
1509
 
    orphan_dir_basename = 'brz-orphans'
1510
 
    od_id = tt.trans_id_tree_path(orphan_dir_basename)
1511
 
    if tt.final_kind(od_id) is None:
1512
 
        tt.create_directory(od_id)
1513
 
    parent_path = tt._tree_id_paths[parent_id]
1514
 
    # Find a name that doesn't exist yet in the orphan dir
1515
 
    actual_name = tt.final_name(orphan_id)
1516
 
    new_name = tt._available_backup_name(actual_name, od_id)
1517
 
    tt.adjust_path(new_name, od_id, orphan_id)
1518
 
    trace.warning('%s has been orphaned in %s'
1519
 
                  % (joinpath(parent_path, actual_name), orphan_dir_basename))
1520
 
 
1521
 
 
1522
 
def refuse_orphan(tt, orphan_id, parent_id):
1523
 
    """See TreeTransformBase.new_orphan.
1524
 
 
1525
 
    This refuses to create orphan, letting the caller handle the conflict.
1526
 
    """
1527
 
    raise OrphaningForbidden('never')
1528
 
 
1529
 
 
1530
 
orphaning_registry = registry.Registry()
1531
 
orphaning_registry.register(
1532
 
    u'conflict', refuse_orphan,
1533
 
    'Leave orphans in place and create a conflict on the directory.')
1534
 
orphaning_registry.register(
1535
 
    u'move', move_orphan,
1536
 
    'Move orphans into the brz-orphans directory.')
1537
 
orphaning_registry._set_default_key(u'conflict')
1538
 
 
1539
 
 
1540
 
opt_transform_orphan = _mod_config.RegistryOption(
1541
 
    'transform.orphan_policy', orphaning_registry,
1542
 
    help='Policy for orphaned files during transform operations.',
1543
 
    invalid='warning')
1544
 
 
1545
1277
 
1546
1278
class TreeTransform(DiskTreeTransform):
1547
1279
    """Represent a tree transformation.
1608
1340
    FileMover does not delete files until it is sure that a rollback will not
1609
1341
    happen.
1610
1342
    """
1611
 
 
1612
1343
    def __init__(self, tree, pb=None):
1613
1344
        """Note: a tree_write lock is taken on the tree.
1614
1345
 
1616
1347
        TreeTransform.apply() called).
1617
1348
        """
1618
1349
        tree.lock_tree_write()
 
1350
 
1619
1351
        try:
1620
1352
            limbodir = urlutils.local_path_from_url(
1621
1353
                tree._transport.abspath('limbo'))
1622
 
            osutils.ensure_empty_directory_exists(
1623
 
                limbodir,
1624
 
                errors.ExistingLimbo)
 
1354
            try:
 
1355
                os.mkdir(limbodir)
 
1356
            except OSError, e:
 
1357
                if e.errno == errno.EEXIST:
 
1358
                    raise ExistingLimbo(limbodir)
1625
1359
            deletiondir = urlutils.local_path_from_url(
1626
1360
                tree._transport.abspath('pending-deletion'))
1627
 
            osutils.ensure_empty_directory_exists(
1628
 
                deletiondir,
1629
 
                errors.ExistingPendingDeletion)
1630
 
        except BaseException:
 
1361
            try:
 
1362
                os.mkdir(deletiondir)
 
1363
            except OSError, e:
 
1364
                if e.errno == errno.EEXIST:
 
1365
                    raise errors.ExistingPendingDeletion(deletiondir)
 
1366
        except:
1631
1367
            tree.unlock()
1632
1368
            raise
1633
1369
 
1661
1397
    def tree_kind(self, trans_id):
1662
1398
        """Determine the file kind in the working tree.
1663
1399
 
1664
 
        :returns: The file kind or None if the file does not exist
 
1400
        Raises NoSuchFile if the file does not exist
1665
1401
        """
1666
1402
        path = self._tree_id_paths.get(trans_id)
1667
1403
        if path is None:
1668
 
            return None
 
1404
            raise NoSuchFile(None)
1669
1405
        try:
1670
1406
            return file_kind(self._tree.abspath(path))
1671
 
        except errors.NoSuchFile:
1672
 
            return None
 
1407
        except OSError, e:
 
1408
            if e.errno != errno.ENOENT:
 
1409
                raise
 
1410
            else:
 
1411
                raise NoSuchFile(path)
1673
1412
 
1674
1413
    def _set_mode(self, trans_id, mode_id, typefunc):
1675
1414
        """Set the mode of new file contents.
1685
1424
            return
1686
1425
        try:
1687
1426
            mode = os.stat(self._tree.abspath(old_path)).st_mode
1688
 
        except OSError as e:
 
1427
        except OSError, e:
1689
1428
            if e.errno in (errno.ENOENT, errno.ENOTDIR):
1690
1429
                # Either old_path doesn't exist, or the parent of the
1691
1430
                # target is not a directory (but will be one eventually)
1695
1434
            else:
1696
1435
                raise
1697
1436
        if typefunc(mode):
1698
 
            osutils.chmod_if_possible(self._limbo_name(trans_id), mode)
 
1437
            os.chmod(self._limbo_name(trans_id), mode)
1699
1438
 
1700
1439
    def iter_tree_children(self, parent_id):
1701
1440
        """Iterate through the entry's tree children, if any"""
1705
1444
            return
1706
1445
        try:
1707
1446
            children = os.listdir(self._tree.abspath(path))
1708
 
        except OSError as e:
1709
 
            if not (osutils._is_error_enotdir(e) or
1710
 
                    e.errno in (errno.ENOENT, errno.ESRCH)):
 
1447
        except OSError, e:
 
1448
            if not (osutils._is_error_enotdir(e)
 
1449
                    or e.errno in (errno.ENOENT, errno.ESRCH)):
1711
1450
                raise
1712
1451
            return
1713
1452
 
1744
1483
                # if it is already associated with this trans_id.
1745
1484
                elif self._case_sensitive_target:
1746
1485
                    if (self._limbo_children_names[parent].get(filename)
1747
 
                            in (trans_id, None)):
 
1486
                        in (trans_id, None)):
1748
1487
                        use_direct_path = True
1749
1488
                else:
1750
 
                    for l_filename, l_trans_id in viewitems(
1751
 
                            self._limbo_children_names[parent]):
 
1489
                    for l_filename, l_trans_id in\
 
1490
                        self._limbo_children_names[parent].iteritems():
1752
1491
                        if l_trans_id == trans_id:
1753
1492
                            continue
1754
1493
                        if l_filename.lower() == filename.lower():
1764
1503
        self._limbo_children_names[parent][filename] = trans_id
1765
1504
        return limbo_name
1766
1505
 
 
1506
 
1767
1507
    def apply(self, no_conflicts=False, precomputed_delta=None, _mover=None):
1768
1508
        """Apply all changes to the inventory and filesystem.
1769
1509
 
1778
1518
            calculating one.
1779
1519
        :param _mover: Supply an alternate FileMover, for testing
1780
1520
        """
1781
 
        for hook in MutableTree.hooks['pre_transform']:
1782
 
            hook(self._tree, self)
1783
1521
        if not no_conflicts:
1784
1522
            self._check_malformed()
1785
 
        with ui.ui_factory.nested_progress_bar() as child_pb:
 
1523
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1524
        try:
1786
1525
            if precomputed_delta is None:
1787
 
                child_pb.update(gettext('Apply phase'), 0, 2)
 
1526
                child_pb.update('Apply phase', 0, 2)
1788
1527
                inventory_delta = self._generate_inventory_delta()
1789
1528
                offset = 1
1790
1529
            else:
1795
1534
            else:
1796
1535
                mover = _mover
1797
1536
            try:
1798
 
                child_pb.update(gettext('Apply phase'), 0 + offset, 2 + offset)
 
1537
                child_pb.update('Apply phase', 0 + offset, 2 + offset)
1799
1538
                self._apply_removals(mover)
1800
 
                child_pb.update(gettext('Apply phase'), 1 + offset, 2 + offset)
 
1539
                child_pb.update('Apply phase', 1 + offset, 2 + offset)
1801
1540
                modified_paths = self._apply_insertions(mover)
1802
 
            except BaseException:
 
1541
            except:
1803
1542
                mover.rollback()
1804
1543
                raise
1805
1544
            else:
1806
1545
                mover.apply_deletions()
1807
 
        if self.final_file_id(self.root) is None:
1808
 
            inventory_delta = [e for e in inventory_delta if e[0] != '']
 
1546
        finally:
 
1547
            child_pb.finished()
1809
1548
        self._tree.apply_inventory_delta(inventory_delta)
1810
 
        self._apply_observed_sha1s()
1811
1549
        self._done = True
1812
1550
        self.finalize()
1813
1551
        return _TransformResults(modified_paths, self.rename_count)
1815
1553
    def _generate_inventory_delta(self):
1816
1554
        """Generate an inventory delta for the current transform."""
1817
1555
        inventory_delta = []
 
1556
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1818
1557
        new_paths = self._inventory_altered()
1819
1558
        total_entries = len(new_paths) + len(self._removed_id)
1820
 
        with ui.ui_factory.nested_progress_bar() as child_pb:
 
1559
        try:
1821
1560
            for num, trans_id in enumerate(self._removed_id):
1822
1561
                if (num % 10) == 0:
1823
 
                    child_pb.update(gettext('removing file'),
1824
 
                                    num, total_entries)
 
1562
                    child_pb.update('removing file', num, total_entries)
1825
1563
                if trans_id == self._new_root:
1826
1564
                    file_id = self._tree.get_root_id()
1827
1565
                else:
1833
1571
                inventory_delta.append((path, None, file_id, None))
1834
1572
            new_path_file_ids = dict((t, self.final_file_id(t)) for p, t in
1835
1573
                                     new_paths)
 
1574
            entries = self._tree.iter_entries_by_dir(
 
1575
                new_path_file_ids.values())
 
1576
            old_paths = dict((e.file_id, p) for p, e in entries)
 
1577
            final_kinds = {}
1836
1578
            for num, (path, trans_id) in enumerate(new_paths):
1837
1579
                if (num % 10) == 0:
1838
 
                    child_pb.update(gettext('adding file'),
 
1580
                    child_pb.update('adding file',
1839
1581
                                    num + len(self._removed_id), total_entries)
1840
1582
                file_id = new_path_file_ids[trans_id]
1841
1583
                if file_id is None:
1842
1584
                    continue
1843
 
                kind = self.final_kind(trans_id)
1844
 
                if kind is None:
1845
 
                    kind = self._tree.stored_kind(self._tree.id2path(file_id))
 
1585
                needs_entry = False
 
1586
                try:
 
1587
                    kind = self.final_kind(trans_id)
 
1588
                except NoSuchFile:
 
1589
                    kind = self._tree.stored_kind(file_id)
1846
1590
                parent_trans_id = self.final_parent(trans_id)
1847
1591
                parent_file_id = new_path_file_ids.get(parent_trans_id)
1848
1592
                if parent_file_id is None:
1855
1599
                        None, self._new_reference_revision[trans_id])
1856
1600
                else:
1857
1601
                    new_entry = inventory.make_entry(kind,
1858
 
                                                     self.final_name(trans_id),
1859
 
                                                     parent_file_id, file_id)
1860
 
                try:
1861
 
                    old_path = self._tree.id2path(new_entry.file_id)
1862
 
                except errors.NoSuchId:
1863
 
                    old_path = None
 
1602
                        self.final_name(trans_id),
 
1603
                        parent_file_id, file_id)
 
1604
                old_path = old_paths.get(new_entry.file_id)
1864
1605
                new_executability = self._new_executability.get(trans_id)
1865
1606
                if new_executability is not None:
1866
1607
                    new_entry.executable = new_executability
1867
1608
                inventory_delta.append(
1868
1609
                    (old_path, path, new_entry.file_id, new_entry))
 
1610
        finally:
 
1611
            child_pb.finished()
1869
1612
        return inventory_delta
1870
1613
 
1871
1614
    def _apply_removals(self, mover):
1877
1620
 
1878
1621
        If inventory_delta is None, no inventory delta generation is performed.
1879
1622
        """
1880
 
        tree_paths = sorted(viewitems(self._tree_path_ids), reverse=True)
1881
 
        with ui.ui_factory.nested_progress_bar() as child_pb:
1882
 
            for num, (path, trans_id) in enumerate(tree_paths):
1883
 
                # do not attempt to move root into a subdirectory of itself.
1884
 
                if path == '':
1885
 
                    continue
1886
 
                child_pb.update(gettext('removing file'), num, len(tree_paths))
 
1623
        tree_paths = list(self._tree_path_ids.iteritems())
 
1624
        tree_paths.sort(reverse=True)
 
1625
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1626
        try:
 
1627
            for num, data in enumerate(tree_paths):
 
1628
                path, trans_id = data
 
1629
                child_pb.update('removing file', num, len(tree_paths))
1887
1630
                full_path = self._tree.abspath(path)
1888
1631
                if trans_id in self._removed_contents:
1889
1632
                    delete_path = os.path.join(self._deletiondir, trans_id)
1890
1633
                    mover.pre_delete(full_path, delete_path)
1891
 
                elif (trans_id in self._new_name or
1892
 
                      trans_id in self._new_parent):
 
1634
                elif (trans_id in self._new_name
 
1635
                      or trans_id in self._new_parent):
1893
1636
                    try:
1894
1637
                        mover.rename(full_path, self._limbo_name(trans_id))
1895
 
                    except errors.TransformRenameFailed as e:
 
1638
                    except OSError, e:
1896
1639
                        if e.errno != errno.ENOENT:
1897
1640
                            raise
1898
1641
                    else:
1899
1642
                        self.rename_count += 1
 
1643
        finally:
 
1644
            child_pb.finished()
1900
1645
 
1901
1646
    def _apply_insertions(self, mover):
1902
1647
        """Perform tree operations that insert directory/inventory names.
1910
1655
        """
1911
1656
        new_paths = self.new_paths(filesystem_only=True)
1912
1657
        modified_paths = []
1913
 
        with ui.ui_factory.nested_progress_bar() as child_pb:
 
1658
        new_path_file_ids = dict((t, self.final_file_id(t)) for p, t in
 
1659
                                 new_paths)
 
1660
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1661
        try:
1914
1662
            for num, (path, trans_id) in enumerate(new_paths):
1915
1663
                if (num % 10) == 0:
1916
 
                    child_pb.update(gettext('adding file'),
1917
 
                                    num, len(new_paths))
 
1664
                    child_pb.update('adding file', num, len(new_paths))
1918
1665
                full_path = self._tree.abspath(path)
1919
1666
                if trans_id in self._needs_rename:
1920
1667
                    try:
1921
1668
                        mover.rename(self._limbo_name(trans_id), full_path)
1922
 
                    except errors.TransformRenameFailed as e:
 
1669
                    except OSError, e:
1923
1670
                        # We may be renaming a dangling inventory id
1924
1671
                        if e.errno != errno.ENOENT:
1925
1672
                            raise
1926
1673
                    else:
1927
1674
                        self.rename_count += 1
1928
 
                    # TODO: if trans_id in self._observed_sha1s, we should
1929
 
                    #       re-stat the final target, since ctime will be
1930
 
                    #       updated by the change.
1931
 
                if (trans_id in self._new_contents
1932
 
                        or self.path_changed(trans_id)):
 
1675
                if (trans_id in self._new_contents or
 
1676
                    self.path_changed(trans_id)):
1933
1677
                    if trans_id in self._new_contents:
1934
1678
                        modified_paths.append(full_path)
1935
1679
                if trans_id in self._new_executability:
1936
1680
                    self._set_executability(path, trans_id)
1937
 
                if trans_id in self._observed_sha1s:
1938
 
                    o_sha1, o_st_val = self._observed_sha1s[trans_id]
1939
 
                    st = osutils.lstat(full_path)
1940
 
                    self._observed_sha1s[trans_id] = (o_sha1, st)
1941
 
        for path, trans_id in new_paths:
1942
 
            # new_paths includes stuff like workingtree conflicts. Only the
1943
 
            # stuff in new_contents actually comes from limbo.
1944
 
            if trans_id in self._limbo_files:
1945
 
                del self._limbo_files[trans_id]
 
1681
        finally:
 
1682
            child_pb.finished()
1946
1683
        self._new_contents.clear()
1947
1684
        return modified_paths
1948
1685
 
1949
 
    def _apply_observed_sha1s(self):
1950
 
        """After we have finished renaming everything, update observed sha1s
1951
 
 
1952
 
        This has to be done after self._tree.apply_inventory_delta, otherwise
1953
 
        it doesn't know anything about the files we are updating. Also, we want
1954
 
        to do this as late as possible, so that most entries end up cached.
1955
 
        """
1956
 
        # TODO: this doesn't update the stat information for directories. So
1957
 
        #       the first 'bzr status' will still need to rewrite
1958
 
        #       .bzr/checkout/dirstate. However, we at least don't need to
1959
 
        #       re-read all of the files.
1960
 
        # TODO: If the operation took a while, we could do a time.sleep(3) here
1961
 
        #       to allow the clock to tick over and ensure we won't have any
1962
 
        #       problems. (we could observe start time, and finish time, and if
1963
 
        #       it is less than eg 10% overhead, add a sleep call.)
1964
 
        paths = FinalPaths(self)
1965
 
        for trans_id, observed in viewitems(self._observed_sha1s):
1966
 
            path = paths.get_path(trans_id)
1967
 
            self._tree._observed_sha1(path, observed)
1968
 
 
1969
1686
 
1970
1687
class TransformPreview(DiskTreeTransform):
1971
1688
    """A TreeTransform for generating preview trees.
1986
1703
    def tree_kind(self, trans_id):
1987
1704
        path = self._tree_id_paths.get(trans_id)
1988
1705
        if path is None:
1989
 
            return None
1990
 
        kind = self._tree.path_content_summary(path)[0]
1991
 
        if kind == 'missing':
1992
 
            kind = None
1993
 
        return kind
 
1706
            raise NoSuchFile(None)
 
1707
        file_id = self._tree.path2id(path)
 
1708
        return self._tree.kind(file_id)
1994
1709
 
1995
1710
    def _set_mode(self, trans_id, mode_id, typefunc):
1996
1711
        """Set the mode of new file contents.
2007
1722
            path = self._tree_id_paths[parent_id]
2008
1723
        except KeyError:
2009
1724
            return
2010
 
        try:
2011
 
            entry = next(self._tree.iter_entries_by_dir(
2012
 
                specific_files=[path]))[1]
2013
 
        except StopIteration:
 
1725
        file_id = self.tree_file_id(parent_id)
 
1726
        if file_id is None:
2014
1727
            return
 
1728
        entry = self._tree.iter_entries_by_dir([file_id]).next()[1]
2015
1729
        children = getattr(entry, 'children', {})
2016
1730
        for child in children:
2017
1731
            childpath = joinpath(path, child)
2018
1732
            yield self.trans_id_tree_path(childpath)
2019
1733
 
2020
 
    def new_orphan(self, trans_id, parent_id):
2021
 
        raise NotImplementedError(self.new_orphan)
2022
 
 
2023
 
 
2024
 
class _PreviewTree(inventorytree.InventoryTree):
 
1734
 
 
1735
class _PreviewTree(tree.Tree):
2025
1736
    """Partial implementation of Tree to support show_diff_trees"""
2026
1737
 
2027
1738
    def __init__(self, transform):
2032
1743
        self._all_children_cache = {}
2033
1744
        self._path2trans_id_cache = {}
2034
1745
        self._final_name_cache = {}
2035
 
        self._iter_changes_cache = dict((c.file_id, c) for c in
 
1746
        self._iter_changes_cache = dict((c[0], c) for c in
2036
1747
                                        self._transform.iter_changes())
2037
1748
 
2038
1749
    def _content_change(self, file_id):
2039
1750
        """Return True if the content of this file changed"""
2040
1751
        changes = self._iter_changes_cache.get(file_id)
2041
 
        return (changes is not None and changes.changed_content)
 
1752
        # changes[2] is true if the file content changed.  See
 
1753
        # InterTree.iter_changes.
 
1754
        return (changes is not None and changes[2])
2042
1755
 
2043
1756
    def _get_repository(self):
2044
1757
        repo = getattr(self._transform._tree, '_repository', None)
2053
1766
            except errors.NoSuchRevisionInTree:
2054
1767
                yield self._get_repository().revision_tree(revision_id)
2055
1768
 
2056
 
    def _get_file_revision(self, path, file_id, vf, tree_revision):
2057
 
        parent_keys = [
2058
 
            (file_id, t.get_file_revision(t.id2path(file_id)))
2059
 
            for t in self._iter_parent_trees()]
 
1769
    def _get_file_revision(self, file_id, vf, tree_revision):
 
1770
        parent_keys = [(file_id, self._file_revision(t, file_id)) for t in
 
1771
                       self._iter_parent_trees()]
2060
1772
        vf.add_lines((file_id, tree_revision), parent_keys,
2061
 
                     self.get_file_lines(path))
 
1773
                     self.get_file(file_id).readlines())
2062
1774
        repo = self._get_repository()
2063
1775
        base_vf = repo.texts
2064
1776
        if base_vf not in vf.fallback_versionedfiles:
2065
1777
            vf.fallback_versionedfiles.append(base_vf)
2066
1778
        return tree_revision
2067
1779
 
2068
 
    def _stat_limbo_file(self, trans_id):
 
1780
    def _stat_limbo_file(self, file_id):
 
1781
        trans_id = self._transform.trans_id_file_id(file_id)
2069
1782
        name = self._transform._limbo_name(trans_id)
2070
1783
        return os.lstat(name)
2071
1784
 
2082
1795
            executable = False
2083
1796
        else:
2084
1797
            file_id = self._transform.final_file_id(self._path2trans_id(path))
2085
 
            executable = self.is_executable(path)
 
1798
            executable = self.is_executable(file_id, path)
2086
1799
        return kind, executable, None
2087
1800
 
2088
 
    def is_locked(self):
2089
 
        return False
2090
 
 
2091
1801
    def lock_read(self):
2092
1802
        # Perhaps in theory, this should lock the TreeTransform?
2093
 
        return lock.LogicalLockResult(self.unlock)
 
1803
        pass
2094
1804
 
2095
1805
    def unlock(self):
2096
1806
        pass
2097
1807
 
2098
1808
    @property
2099
 
    def root_inventory(self):
 
1809
    def inventory(self):
2100
1810
        """This Tree does not use inventory as its backing data."""
2101
 
        raise NotImplementedError(_PreviewTree.root_inventory)
 
1811
        raise NotImplementedError(_PreviewTree.inventory)
2102
1812
 
2103
1813
    def get_root_id(self):
2104
1814
        return self._transform.final_file_id(self._transform.root)
2107
1817
        tree_ids = set(self._transform._tree.all_file_ids())
2108
1818
        tree_ids.difference_update(self._transform.tree_file_id(t)
2109
1819
                                   for t in self._transform._removed_id)
2110
 
        tree_ids.update(viewvalues(self._transform._new_id))
 
1820
        tree_ids.update(self._transform._new_id.values())
2111
1821
        return tree_ids
2112
1822
 
2113
 
    def all_versioned_paths(self):
2114
 
        tree_paths = set(self._transform._tree.all_versioned_paths())
2115
 
 
2116
 
        tree_paths.difference_update(
2117
 
            self._transform.trans_id_tree_path(t)
2118
 
            for t in self._transform._removed_id)
2119
 
 
2120
 
        tree_paths.update(
2121
 
            self._final_paths._determine_path(t)
2122
 
            for t in self._transform._new_id)
2123
 
 
2124
 
        return tree_paths
 
1823
    def __iter__(self):
 
1824
        return iter(self.all_file_ids())
2125
1825
 
2126
1826
    def _has_id(self, file_id, fallback_check):
2127
1827
        if file_id in self._transform._r_new_id:
2128
1828
            return True
2129
 
        elif file_id in {self._transform.tree_file_id(trans_id) for
2130
 
                         trans_id in self._transform._removed_id}:
 
1829
        elif file_id in set([self._transform.tree_file_id(trans_id) for
 
1830
            trans_id in self._transform._removed_id]):
2131
1831
            return False
2132
1832
        else:
2133
1833
            return fallback_check(file_id)
2161
1861
        return cur_parent
2162
1862
 
2163
1863
    def path2id(self, path):
2164
 
        if isinstance(path, list):
2165
 
            if path == []:
2166
 
                path = [""]
2167
 
            path = osutils.pathjoin(*path)
2168
1864
        return self._transform.final_file_id(self._path2trans_id(path))
2169
1865
 
2170
1866
    def id2path(self, file_id):
2180
1876
            return children
2181
1877
        children = set(self._transform.iter_tree_children(trans_id))
2182
1878
        # children in the _new_parent set are provided by _by_parent.
2183
 
        children.difference_update(self._transform._new_parent)
 
1879
        children.difference_update(self._transform._new_parent.keys())
2184
1880
        children.update(self._by_parent.get(trans_id, []))
2185
1881
        self._all_children_cache[trans_id] = children
2186
1882
        return children
2187
1883
 
2188
 
    def _iter_children(self, file_id):
 
1884
    def iter_children(self, file_id):
2189
1885
        trans_id = self._transform.trans_id_file_id(file_id)
2190
1886
        for child_trans_id in self._all_children(trans_id):
2191
1887
            yield self._transform.final_file_id(child_trans_id)
2199
1895
            if self._transform.final_file_id(trans_id) is None:
2200
1896
                yield self._final_paths._determine_path(trans_id)
2201
1897
 
2202
 
    def _make_inv_entries(self, ordered_entries, specific_files=None):
 
1898
    def _make_inv_entries(self, ordered_entries, specific_file_ids=None,
 
1899
        yield_parents=False):
2203
1900
        for trans_id, parent_file_id in ordered_entries:
2204
1901
            file_id = self._transform.final_file_id(trans_id)
2205
1902
            if file_id is None:
2206
1903
                continue
2207
 
            if (specific_files is not None
2208
 
                    and self._final_paths.get_path(trans_id) not in specific_files):
 
1904
            if (specific_file_ids is not None
 
1905
                and file_id not in specific_file_ids):
2209
1906
                continue
2210
 
            kind = self._transform.final_kind(trans_id)
2211
 
            if kind is None:
2212
 
                kind = self._transform._tree.stored_kind(
2213
 
                    self._transform._tree.id2path(file_id))
 
1907
            try:
 
1908
                kind = self._transform.final_kind(trans_id)
 
1909
            except NoSuchFile:
 
1910
                kind = self._transform._tree.stored_kind(file_id)
2214
1911
            new_entry = inventory.make_entry(
2215
1912
                kind,
2216
1913
                self._transform.final_name(trans_id),
2231
1928
                ordered_ids.append((trans_id, parent_file_id))
2232
1929
        return ordered_ids
2233
1930
 
2234
 
    def iter_child_entries(self, path):
2235
 
        trans_id = self._path2trans_id(path)
2236
 
        if trans_id is None:
2237
 
            raise errors.NoSuchFile(path)
2238
 
        todo = [(child_trans_id, trans_id) for child_trans_id in
2239
 
                self._all_children(trans_id)]
2240
 
        for entry, trans_id in self._make_inv_entries(todo):
2241
 
            yield entry
2242
 
 
2243
 
    def iter_entries_by_dir(self, specific_files=None):
 
1931
    def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
2244
1932
        # This may not be a maximally efficient implementation, but it is
2245
1933
        # reasonably straightforward.  An implementation that grafts the
2246
1934
        # TreeTransform changes onto the tree's iter_entries_by_dir results
2248
1936
        # position.
2249
1937
        ordered_ids = self._list_files_by_dir()
2250
1938
        for entry, trans_id in self._make_inv_entries(ordered_ids,
2251
 
                                                      specific_files):
2252
 
            yield self._final_paths.get_path(trans_id), entry
 
1939
            specific_file_ids, yield_parents=yield_parents):
 
1940
            yield unicode(self._final_paths.get_path(trans_id)), entry
2253
1941
 
2254
1942
    def _iter_entries_for_dir(self, dir_path):
2255
1943
        """Return path, entry for items in a directory without recursing down."""
 
1944
        dir_file_id = self.path2id(dir_path)
2256
1945
        ordered_ids = []
2257
 
        dir_trans_id = self._path2trans_id(dir_path)
2258
 
        dir_id = self._transform.final_file_id(dir_trans_id)
2259
 
        for child_trans_id in self._all_children(dir_trans_id):
2260
 
            ordered_ids.append((child_trans_id, dir_id))
2261
 
        path_entries = []
 
1946
        for file_id in self.iter_children(dir_file_id):
 
1947
            trans_id = self._transform.trans_id_file_id(file_id)
 
1948
            ordered_ids.append((trans_id, file_id))
2262
1949
        for entry, trans_id in self._make_inv_entries(ordered_ids):
2263
 
            path_entries.append((self._final_paths.get_path(trans_id), entry))
2264
 
        path_entries.sort()
2265
 
        return path_entries
 
1950
            yield unicode(self._final_paths.get_path(trans_id)), entry
2266
1951
 
2267
1952
    def list_files(self, include_root=False, from_dir=None, recursive=True):
2268
1953
        """See WorkingTree.list_files."""
2269
1954
        # XXX This should behave like WorkingTree.list_files, but is really
2270
1955
        # more like RevisionTree.list_files.
2271
 
        if from_dir == '.':
2272
 
            from_dir = None
2273
1956
        if recursive:
2274
1957
            prefix = None
2275
1958
            if from_dir:
2282
1965
                    if not path.startswith(prefix):
2283
1966
                        continue
2284
1967
                    path = path[len(prefix):]
2285
 
                yield path, 'V', entry.kind, entry
 
1968
                yield path, 'V', entry.kind, entry.file_id, entry
2286
1969
        else:
2287
1970
            if from_dir is None and include_root is True:
2288
 
                root_entry = inventory.make_entry(
2289
 
                    'directory', '', ROOT_PARENT, self.get_root_id())
2290
 
                yield '', 'V', 'directory', root_entry
 
1971
                root_entry = inventory.make_entry('directory', '',
 
1972
                    ROOT_PARENT, self.get_root_id())
 
1973
                yield '', 'V', 'directory', root_entry.file_id, root_entry
2291
1974
            entries = self._iter_entries_for_dir(from_dir or '')
2292
1975
            for path, entry in entries:
2293
 
                yield path, 'V', entry.kind, entry
 
1976
                yield path, 'V', entry.kind, entry.file_id, entry
2294
1977
 
2295
 
    def kind(self, path):
2296
 
        trans_id = self._path2trans_id(path)
2297
 
        if trans_id is None:
2298
 
            raise errors.NoSuchFile(path)
 
1978
    def kind(self, file_id):
 
1979
        trans_id = self._transform.trans_id_file_id(file_id)
2299
1980
        return self._transform.final_kind(trans_id)
2300
1981
 
2301
 
    def stored_kind(self, path):
2302
 
        trans_id = self._path2trans_id(path)
2303
 
        if trans_id is None:
2304
 
            raise errors.NoSuchFile(path)
 
1982
    def stored_kind(self, file_id):
 
1983
        trans_id = self._transform.trans_id_file_id(file_id)
2305
1984
        try:
2306
1985
            return self._transform._new_contents[trans_id]
2307
1986
        except KeyError:
2308
 
            return self._transform._tree.stored_kind(path)
 
1987
            return self._transform._tree.stored_kind(file_id)
2309
1988
 
2310
 
    def get_file_mtime(self, path):
 
1989
    def get_file_mtime(self, file_id, path=None):
2311
1990
        """See Tree.get_file_mtime"""
2312
 
        file_id = self.path2id(path)
2313
 
        if file_id is None:
2314
 
            raise errors.NoSuchFile(path)
2315
1991
        if not self._content_change(file_id):
2316
 
            return self._transform._tree.get_file_mtime(
2317
 
                self._transform._tree.id2path(file_id))
2318
 
        trans_id = self._path2trans_id(path)
2319
 
        return self._stat_limbo_file(trans_id).st_mtime
2320
 
 
2321
 
    def get_file_size(self, path):
 
1992
            return self._transform._tree.get_file_mtime(file_id)
 
1993
        return self._stat_limbo_file(file_id).st_mtime
 
1994
 
 
1995
    def _file_size(self, entry, stat_value):
 
1996
        return self.get_file_size(entry.file_id)
 
1997
 
 
1998
    def get_file_size(self, file_id):
2322
1999
        """See Tree.get_file_size"""
2323
 
        trans_id = self._path2trans_id(path)
2324
 
        if trans_id is None:
2325
 
            raise errors.NoSuchFile(path)
2326
 
        kind = self._transform.final_kind(trans_id)
2327
 
        if kind != 'file':
2328
 
            return None
2329
 
        if trans_id in self._transform._new_contents:
2330
 
            return self._stat_limbo_file(trans_id).st_size
2331
 
        if self.kind(path) == 'file':
2332
 
            return self._transform._tree.get_file_size(path)
 
2000
        if self.kind(file_id) == 'file':
 
2001
            return self._transform._tree.get_file_size(file_id)
2333
2002
        else:
2334
2003
            return None
2335
2004
 
2336
 
    def get_file_verifier(self, path, stat_value=None):
2337
 
        trans_id = self._path2trans_id(path)
2338
 
        if trans_id is None:
2339
 
            raise errors.NoSuchFile(path)
2340
 
        kind = self._transform._new_contents.get(trans_id)
2341
 
        if kind is None:
2342
 
            return self._transform._tree.get_file_verifier(path)
2343
 
        if kind == 'file':
2344
 
            with self.get_file(path) as fileobj:
2345
 
                return ("SHA1", sha_file(fileobj))
2346
 
 
2347
 
    def get_file_sha1(self, path, stat_value=None):
2348
 
        trans_id = self._path2trans_id(path)
2349
 
        if trans_id is None:
2350
 
            raise errors.NoSuchFile(path)
2351
 
        kind = self._transform._new_contents.get(trans_id)
2352
 
        if kind is None:
2353
 
            return self._transform._tree.get_file_sha1(path)
2354
 
        if kind == 'file':
2355
 
            with self.get_file(path) as fileobj:
 
2005
    def get_file_sha1(self, file_id, path=None, stat_value=None):
 
2006
        trans_id = self._transform.trans_id_file_id(file_id)
 
2007
        kind = self._transform._new_contents.get(trans_id)
 
2008
        if kind is None:
 
2009
            return self._transform._tree.get_file_sha1(file_id)
 
2010
        if kind == 'file':
 
2011
            fileobj = self.get_file(file_id)
 
2012
            try:
2356
2013
                return sha_file(fileobj)
 
2014
            finally:
 
2015
                fileobj.close()
2357
2016
 
2358
 
    def is_executable(self, path):
2359
 
        trans_id = self._path2trans_id(path)
2360
 
        if trans_id is None:
 
2017
    def is_executable(self, file_id, path=None):
 
2018
        if file_id is None:
2361
2019
            return False
 
2020
        trans_id = self._transform.trans_id_file_id(file_id)
2362
2021
        try:
2363
2022
            return self._transform._new_executability[trans_id]
2364
2023
        except KeyError:
2365
2024
            try:
2366
 
                return self._transform._tree.is_executable(path)
2367
 
            except OSError as e:
 
2025
                return self._transform._tree.is_executable(file_id, path)
 
2026
            except OSError, e:
2368
2027
                if e.errno == errno.ENOENT:
2369
2028
                    return False
2370
2029
                raise
2371
 
            except errors.NoSuchFile:
 
2030
            except errors.NoSuchId:
2372
2031
                return False
2373
2032
 
2374
 
    def has_filename(self, path):
2375
 
        trans_id = self._path2trans_id(path)
2376
 
        if trans_id in self._transform._new_contents:
2377
 
            return True
2378
 
        elif trans_id in self._transform._removed_contents:
2379
 
            return False
2380
 
        else:
2381
 
            return self._transform._tree.has_filename(path)
2382
 
 
2383
2033
    def path_content_summary(self, path):
2384
2034
        trans_id = self._path2trans_id(path)
2385
2035
        tt = self._transform
2398
2048
            if kind == 'file':
2399
2049
                statval = os.lstat(limbo_name)
2400
2050
                size = statval.st_size
2401
 
                if not tt._limbo_supports_executable():
 
2051
                if not supports_executable():
2402
2052
                    executable = False
2403
2053
                else:
2404
2054
                    executable = statval.st_mode & S_IEXEC
2406
2056
                size = None
2407
2057
                executable = None
2408
2058
            if kind == 'symlink':
2409
 
                link_or_sha1 = os.readlink(limbo_name)
2410
 
                if not isinstance(link_or_sha1, text_type):
2411
 
                    link_or_sha1 = link_or_sha1.decode(osutils._fs_enc)
 
2059
                link_or_sha1 = os.readlink(limbo_name).decode(osutils._fs_enc)
2412
2060
        executable = tt._new_executability.get(trans_id, executable)
2413
2061
        return kind, size, executable, link_or_sha1
2414
2062
 
2415
2063
    def iter_changes(self, from_tree, include_unchanged=False,
2416
 
                     specific_files=None, pb=None, extra_trees=None,
2417
 
                     require_versioned=True, want_unversioned=False):
 
2064
                      specific_files=None, pb=None, extra_trees=None,
 
2065
                      require_versioned=True, want_unversioned=False):
2418
2066
        """See InterTree.iter_changes.
2419
2067
 
2420
2068
        This has a fast path that is only used when the from_tree matches
2421
2069
        the transform tree, and no fancy options are supplied.
2422
2070
        """
2423
 
        if (from_tree is not self._transform._tree or include_unchanged
2424
 
                or specific_files or want_unversioned):
 
2071
        if (from_tree is not self._transform._tree or include_unchanged or
 
2072
            specific_files or want_unversioned):
2425
2073
            return tree.InterTree(from_tree, self).iter_changes(
2426
2074
                include_unchanged=include_unchanged,
2427
2075
                specific_files=specific_files,
2433
2081
            raise ValueError('want_unversioned is not supported')
2434
2082
        return self._transform.iter_changes()
2435
2083
 
2436
 
    def get_file(self, path):
 
2084
    def get_file(self, file_id, path=None):
2437
2085
        """See Tree.get_file"""
2438
 
        file_id = self.path2id(path)
2439
2086
        if not self._content_change(file_id):
2440
 
            return self._transform._tree.get_file(path)
2441
 
        trans_id = self._path2trans_id(path)
 
2087
            return self._transform._tree.get_file(file_id, path)
 
2088
        trans_id = self._transform.trans_id_file_id(file_id)
2442
2089
        name = self._transform._limbo_name(trans_id)
2443
2090
        return open(name, 'rb')
2444
2091
 
2445
 
    def get_file_with_stat(self, path):
2446
 
        return self.get_file(path), None
 
2092
    def get_file_with_stat(self, file_id, path=None):
 
2093
        return self.get_file(file_id, path), None
2447
2094
 
2448
 
    def annotate_iter(self, path,
 
2095
    def annotate_iter(self, file_id,
2449
2096
                      default_revision=_mod_revision.CURRENT_REVISION):
2450
 
        file_id = self.path2id(path)
2451
2097
        changes = self._iter_changes_cache.get(file_id)
2452
2098
        if changes is None:
2453
2099
            get_old = True
2454
2100
        else:
2455
 
            changed_content, versioned, kind = (
2456
 
                changes.changed_content, changes.versioned, changes.kind)
 
2101
            changed_content, versioned, kind = (changes[2], changes[3],
 
2102
                                                changes[6])
2457
2103
            if kind[1] is None:
2458
2104
                return None
2459
2105
            get_old = (kind[0] == 'file' and versioned[0])
2460
2106
        if get_old:
2461
 
            old_annotation = self._transform._tree.annotate_iter(
2462
 
                path, default_revision=default_revision)
 
2107
            old_annotation = self._transform._tree.annotate_iter(file_id,
 
2108
                default_revision=default_revision)
2463
2109
        else:
2464
2110
            old_annotation = []
2465
2111
        if changes is None:
2474
2120
        #       It would be nice to be able to use the new Annotator based
2475
2121
        #       approach, as well.
2476
2122
        return annotate.reannotate([old_annotation],
2477
 
                                   self.get_file(path).readlines(),
 
2123
                                   self.get_file(file_id).readlines(),
2478
2124
                                   default_revision)
2479
2125
 
2480
 
    def get_symlink_target(self, path):
 
2126
    def get_symlink_target(self, file_id):
2481
2127
        """See Tree.get_symlink_target"""
2482
 
        file_id = self.path2id(path)
2483
2128
        if not self._content_change(file_id):
2484
 
            return self._transform._tree.get_symlink_target(path)
2485
 
        trans_id = self._path2trans_id(path)
 
2129
            return self._transform._tree.get_symlink_target(file_id)
 
2130
        trans_id = self._transform.trans_id_file_id(file_id)
2486
2131
        name = self._transform._limbo_name(trans_id)
2487
2132
        return osutils.readlink(name)
2488
2133
 
2499
2144
                path_from_root = self._final_paths.get_path(child_id)
2500
2145
                basename = self._transform.final_name(child_id)
2501
2146
                file_id = self._transform.final_file_id(child_id)
2502
 
                kind = self._transform.final_kind(child_id)
2503
 
                if kind is not None:
 
2147
                try:
 
2148
                    kind = self._transform.final_kind(child_id)
2504
2149
                    versioned_kind = kind
2505
 
                else:
 
2150
                except NoSuchFile:
2506
2151
                    kind = 'unknown'
2507
 
                    versioned_kind = self._transform._tree.stored_kind(
2508
 
                        self._transform._tree.id2path(file_id))
 
2152
                    versioned_kind = self._transform._tree.stored_kind(file_id)
2509
2153
                if versioned_kind == 'directory':
2510
2154
                    subdirs.append(child_id)
2511
2155
                children.append((path_from_root, basename, kind, None,
2540
2184
    The underlying tree must not be manipulated between calls, or else
2541
2185
    the results will likely be incorrect.
2542
2186
    """
2543
 
 
2544
2187
    def __init__(self, transform):
2545
2188
        object.__init__(self)
2546
2189
        self._known_paths = {}
2548
2191
 
2549
2192
    def _determine_path(self, trans_id):
2550
2193
        if (trans_id == self.transform.root or trans_id == ROOT_PARENT):
2551
 
            return u""
 
2194
            return ""
2552
2195
        name = self.transform.final_name(trans_id)
2553
2196
        parent_id = self.transform.final_parent(trans_id)
2554
2197
        if parent_id == self.transform.root:
2566
2209
        return [(self.get_path(t), t) for t in trans_ids]
2567
2210
 
2568
2211
 
 
2212
 
2569
2213
def topology_sorted_ids(tree):
2570
2214
    """Determine the topological order of the ids in a tree"""
2571
2215
    file_ids = list(tree)
2599
2243
    :param delta_from_tree: If true, build_tree may use the input Tree to
2600
2244
        generate the inventory delta.
2601
2245
    """
2602
 
    with wt.lock_tree_write(), tree.lock_read():
2603
 
        if accelerator_tree is not None:
2604
 
            accelerator_tree.lock_read()
 
2246
    wt.lock_tree_write()
 
2247
    try:
 
2248
        tree.lock_read()
2605
2249
        try:
2606
 
            return _build_tree(tree, wt, accelerator_tree, hardlink,
2607
 
                               delta_from_tree)
2608
 
        finally:
2609
2250
            if accelerator_tree is not None:
2610
 
                accelerator_tree.unlock()
 
2251
                accelerator_tree.lock_read()
 
2252
            try:
 
2253
                return _build_tree(tree, wt, accelerator_tree, hardlink,
 
2254
                                   delta_from_tree)
 
2255
            finally:
 
2256
                if accelerator_tree is not None:
 
2257
                    accelerator_tree.unlock()
 
2258
        finally:
 
2259
            tree.unlock()
 
2260
    finally:
 
2261
        wt.unlock()
2611
2262
 
2612
2263
 
2613
2264
def _build_tree(tree, wt, accelerator_tree, hardlink, delta_from_tree):
2614
2265
    """See build_tree."""
2615
 
    for num, _unused in enumerate(wt.all_versioned_paths()):
 
2266
    for num, _unused in enumerate(wt.all_file_ids()):
2616
2267
        if num > 0:  # more than just a root
2617
2268
            raise errors.WorkingTreeAlreadyPopulated(base=wt.basedir)
 
2269
    existing_files = set()
 
2270
    for dir, files in wt.walkdirs():
 
2271
        existing_files.update(f[0] for f in files)
2618
2272
    file_trans_id = {}
2619
 
    top_pb = ui.ui_factory.nested_progress_bar()
 
2273
    top_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2620
2274
    pp = ProgressPhase("Build phase", 2, top_pb)
2621
 
    if tree.get_root_id() is not None:
 
2275
    if tree.inventory.root is not None:
2622
2276
        # This is kind of a hack: we should be altering the root
2623
2277
        # as part of the regular tree shape diff logic.
2624
2278
        # The conditional test here is to avoid doing an
2629
2283
        if wt.get_root_id() != tree.get_root_id():
2630
2284
            wt.set_root_id(tree.get_root_id())
2631
2285
            wt.flush()
2632
 
    tt = wt.get_transform()
 
2286
    tt = TreeTransform(wt)
2633
2287
    divert = set()
2634
2288
    try:
2635
2289
        pp.next_phase()
2636
 
        file_trans_id[wt.get_root_id()] = tt.trans_id_tree_path('')
2637
 
        with ui.ui_factory.nested_progress_bar() as pb:
 
2290
        file_trans_id[wt.get_root_id()] = \
 
2291
            tt.trans_id_tree_file_id(wt.get_root_id())
 
2292
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
2293
        try:
2638
2294
            deferred_contents = []
2639
2295
            num = 0
2640
 
            total = len(tree.all_versioned_paths())
 
2296
            total = len(tree.inventory)
2641
2297
            if delta_from_tree:
2642
2298
                precomputed_delta = []
2643
2299
            else:
2644
2300
                precomputed_delta = None
2645
 
            # Check if tree inventory has content. If so, we populate
2646
 
            # existing_files with the directory content. If there are no
2647
 
            # entries we skip populating existing_files as its not used.
2648
 
            # This improves performance and unncessary work on large
2649
 
            # directory trees. (#501307)
2650
 
            if total > 0:
2651
 
                existing_files = set()
2652
 
                for dir, files in wt.walkdirs():
2653
 
                    existing_files.update(f[0] for f in files)
2654
2301
            for num, (tree_path, entry) in \
2655
 
                    enumerate(tree.iter_entries_by_dir()):
2656
 
                pb.update(gettext("Building tree"), num
2657
 
                          - len(deferred_contents), total)
 
2302
                enumerate(tree.inventory.iter_entries_by_dir()):
 
2303
                pb.update("Building tree", num - len(deferred_contents), total)
2658
2304
                if entry.parent_id is None:
2659
2305
                    continue
2660
2306
                reparent = False
2666
2312
                    kind = file_kind(target_path)
2667
2313
                    if kind == "directory":
2668
2314
                        try:
2669
 
                            controldir.ControlDir.open(target_path)
 
2315
                            bzrdir.BzrDir.open(target_path)
2670
2316
                        except errors.NotBranchError:
2671
2317
                            pass
2672
2318
                        else:
2673
2319
                            divert.add(file_id)
2674
 
                    if (file_id not in divert
2675
 
                        and _content_match(
2676
 
                            tree, entry, tree_path, kind, target_path)):
 
2320
                    if (file_id not in divert and
 
2321
                        _content_match(tree, entry, file_id, kind,
 
2322
                        target_path)):
2677
2323
                        tt.delete_contents(tt.trans_id_tree_path(tree_path))
2678
2324
                        if kind == 'directory':
2679
2325
                            reparent = True
2684
2330
                    trans_id = tt.create_path(entry.name, parent_id)
2685
2331
                    file_trans_id[file_id] = trans_id
2686
2332
                    tt.version_file(file_id, trans_id)
2687
 
                    executable = tree.is_executable(tree_path)
 
2333
                    executable = tree.is_executable(file_id, tree_path)
2688
2334
                    if executable:
2689
2335
                        tt.set_executability(executable, trans_id)
2690
 
                    trans_data = (trans_id, file_id,
2691
 
                                  tree_path, entry.text_sha1)
2692
 
                    deferred_contents.append((tree_path, trans_data))
 
2336
                    trans_data = (trans_id, tree_path)
 
2337
                    deferred_contents.append((file_id, trans_data))
2693
2338
                else:
2694
 
                    file_trans_id[file_id] = new_by_entry(
2695
 
                        tree_path, tt, entry, parent_id, tree)
 
2339
                    file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
 
2340
                                                          tree)
2696
2341
                if reparent:
2697
2342
                    new_trans_id = file_trans_id[file_id]
2698
2343
                    old_parent = tt.trans_id_tree_path(tree_path)
2700
2345
            offset = num + 1 - len(deferred_contents)
2701
2346
            _create_files(tt, tree, deferred_contents, pb, offset,
2702
2347
                          accelerator_tree, hardlink)
 
2348
        finally:
 
2349
            pb.finished()
2703
2350
        pp.next_phase()
2704
2351
        divert_trans = set(file_trans_id[f] for f in divert)
2705
 
 
2706
 
        def resolver(t, c):
2707
 
            return resolve_checkout(t, c, divert_trans)
 
2352
        resolver = lambda t, c: resolve_checkout(t, c, divert_trans)
2708
2353
        raw_conflicts = resolve_conflicts(tt, pass_func=resolver)
2709
2354
        if len(raw_conflicts) > 0:
2710
2355
            precomputed_delta = None
2711
2356
        conflicts = cook_conflicts(raw_conflicts, tt)
2712
2357
        for conflict in conflicts:
2713
 
            trace.warning(text_type(conflict))
 
2358
            warning(conflict)
2714
2359
        try:
2715
2360
            wt.add_conflicts(conflicts)
2716
2361
        except errors.UnsupportedOperation:
2731
2376
        new_desired_files = desired_files
2732
2377
    else:
2733
2378
        iter = accelerator_tree.iter_changes(tree, include_unchanged=True)
2734
 
        unchanged = [
2735
 
            change.path for change in iter
2736
 
            if not (change.changed_content or change.executable[0] != change.executable[1])]
 
2379
        unchanged = [(f, p[1]) for (f, p, c, v, d, n, k, e)
 
2380
                     in iter if not (c or e[0] != e[1])]
2737
2381
        if accelerator_tree.supports_content_filtering():
2738
 
            unchanged = [(tp, ap) for (tp, ap) in unchanged
2739
 
                         if not next(accelerator_tree.iter_search_rules([ap]))]
 
2382
            unchanged = [(f, p) for (f, p) in unchanged
 
2383
                         if not accelerator_tree.iter_search_rules([p]).next()]
2740
2384
        unchanged = dict(unchanged)
2741
2385
        new_desired_files = []
2742
2386
        count = 0
2743
 
        for unused_tree_path, (trans_id, file_id, tree_path, text_sha1) in desired_files:
2744
 
            accelerator_path = unchanged.get(tree_path)
 
2387
        for file_id, (trans_id, tree_path) in desired_files:
 
2388
            accelerator_path = unchanged.get(file_id)
2745
2389
            if accelerator_path is None:
2746
 
                new_desired_files.append((tree_path,
2747
 
                                          (trans_id, file_id, tree_path, text_sha1)))
 
2390
                new_desired_files.append((file_id, (trans_id, tree_path)))
2748
2391
                continue
2749
 
            pb.update(gettext('Adding file contents'), count + offset, total)
 
2392
            pb.update('Adding file contents', count + offset, total)
2750
2393
            if hardlink:
2751
2394
                tt.create_hardlink(accelerator_tree.abspath(accelerator_path),
2752
2395
                                   trans_id)
2753
2396
            else:
2754
 
                with accelerator_tree.get_file(accelerator_path) as f:
2755
 
                    chunks = osutils.file_iterator(f)
2756
 
                    if wt.supports_content_filtering():
2757
 
                        filters = wt._content_filter_stack(tree_path)
2758
 
                        chunks = filtered_output_bytes(chunks, filters,
2759
 
                                                       ContentFilterContext(tree_path, tree))
2760
 
                    tt.create_file(chunks, trans_id, sha1=text_sha1)
 
2397
                contents = accelerator_tree.get_file(file_id, accelerator_path)
 
2398
                if wt.supports_content_filtering():
 
2399
                    filters = wt._content_filter_stack(tree_path)
 
2400
                    contents = filtered_output_bytes(contents, filters,
 
2401
                        ContentFilterContext(tree_path, tree))
 
2402
                try:
 
2403
                    tt.create_file(contents, trans_id)
 
2404
                finally:
 
2405
                    try:
 
2406
                        contents.close()
 
2407
                    except AttributeError:
 
2408
                        # after filtering, contents may no longer be file-like
 
2409
                        pass
2761
2410
            count += 1
2762
2411
        offset += count
2763
 
    for count, ((trans_id, file_id, tree_path, text_sha1), contents) in enumerate(
 
2412
    for count, ((trans_id, tree_path), contents) in enumerate(
2764
2413
            tree.iter_files_bytes(new_desired_files)):
2765
2414
        if wt.supports_content_filtering():
2766
2415
            filters = wt._content_filter_stack(tree_path)
2767
2416
            contents = filtered_output_bytes(contents, filters,
2768
 
                                             ContentFilterContext(tree_path, tree))
2769
 
        tt.create_file(contents, trans_id, sha1=text_sha1)
2770
 
        pb.update(gettext('Adding file contents'), count + offset, total)
 
2417
                ContentFilterContext(tree_path, tree))
 
2418
        tt.create_file(contents, trans_id)
 
2419
        pb.update('Adding file contents', count + offset, total)
2771
2420
 
2772
2421
 
2773
2422
def _reparent_children(tt, old_parent, new_parent):
2774
2423
    for child in tt.iter_tree_children(old_parent):
2775
2424
        tt.adjust_path(tt.final_name(child), new_parent, child)
2776
2425
 
2777
 
 
2778
2426
def _reparent_transform_children(tt, old_parent, new_parent):
2779
2427
    by_parent = tt.by_parent()
2780
2428
    for child in by_parent[old_parent]:
2781
2429
        tt.adjust_path(tt.final_name(child), new_parent, child)
2782
2430
    return by_parent[old_parent]
2783
2431
 
2784
 
 
2785
 
def _content_match(tree, entry, tree_path, kind, target_path):
 
2432
def _content_match(tree, entry, file_id, kind, target_path):
2786
2433
    if entry.kind != kind:
2787
2434
        return False
2788
2435
    if entry.kind == "directory":
2789
2436
        return True
2790
2437
    if entry.kind == "file":
2791
 
        with open(target_path, 'rb') as f1, \
2792
 
                tree.get_file(tree_path) as f2:
2793
 
            if osutils.compare_files(f1, f2):
2794
 
                return True
 
2438
        if tree.get_file(file_id).read() == file(target_path, 'rb').read():
 
2439
            return True
2795
2440
    elif entry.kind == "symlink":
2796
 
        if tree.get_symlink_target(tree_path) == os.readlink(target_path):
 
2441
        if tree.get_symlink_target(file_id) == os.readlink(target_path):
2797
2442
            return True
2798
2443
    return False
2799
2444
 
2816
2461
        # resolved
2817
2462
        final_parent = tt.final_parent(old_file)
2818
2463
        if new_file in divert:
2819
 
            new_name = tt.final_name(old_file) + '.diverted'
 
2464
            new_name = tt.final_name(old_file)+'.diverted'
2820
2465
            tt.adjust_path(new_name, final_parent, new_file)
2821
2466
            new_conflicts.add((c_type, 'Diverted to',
2822
2467
                               new_file, old_file))
2823
2468
        else:
2824
 
            new_name = tt.final_name(old_file) + '.moved'
 
2469
            new_name = tt.final_name(old_file)+'.moved'
2825
2470
            tt.adjust_path(new_name, final_parent, old_file)
2826
2471
            new_conflicts.add((c_type, 'Moved existing file to',
2827
2472
                               old_file, new_file))
2828
2473
    return new_conflicts
2829
2474
 
2830
2475
 
2831
 
def new_by_entry(path, tt, entry, parent_id, tree):
 
2476
def new_by_entry(tt, entry, parent_id, tree):
2832
2477
    """Create a new file according to its inventory entry"""
2833
2478
    name = entry.name
2834
2479
    kind = entry.kind
2835
2480
    if kind == 'file':
2836
 
        with tree.get_file(path) as f:
2837
 
            executable = tree.is_executable(path)
2838
 
            return tt.new_file(
2839
 
                name, parent_id, osutils.file_iterator(f), entry.file_id,
2840
 
                executable)
 
2481
        contents = tree.get_file(entry.file_id).readlines()
 
2482
        executable = tree.is_executable(entry.file_id)
 
2483
        return tt.new_file(name, parent_id, contents, entry.file_id,
 
2484
                           executable)
2841
2485
    elif kind in ('directory', 'tree-reference'):
2842
2486
        trans_id = tt.new_directory(name, parent_id, entry.file_id)
2843
2487
        if kind == 'tree-reference':
2844
2488
            tt.set_tree_reference(entry.reference_revision, trans_id)
2845
2489
        return trans_id
2846
2490
    elif kind == 'symlink':
2847
 
        target = tree.get_symlink_target(path)
 
2491
        target = tree.get_symlink_target(entry.file_id)
2848
2492
        return tt.new_symlink(name, parent_id, target, entry.file_id)
2849
2493
    else:
2850
2494
        raise errors.BadFileKindError(name, kind)
2851
2495
 
2852
2496
 
2853
 
def create_from_tree(tt, trans_id, tree, path, chunks=None,
2854
 
                     filter_tree_path=None):
 
2497
@deprecated_function(deprecated_in((1, 9, 0)))
 
2498
def create_by_entry(tt, entry, tree, trans_id, lines=None, mode_id=None):
 
2499
    """Create new file contents according to an inventory entry.
 
2500
 
 
2501
    DEPRECATED.  Use create_from_tree instead.
 
2502
    """
 
2503
    if entry.kind == "file":
 
2504
        if lines is None:
 
2505
            lines = tree.get_file(entry.file_id).readlines()
 
2506
        tt.create_file(lines, trans_id, mode_id=mode_id)
 
2507
    elif entry.kind == "symlink":
 
2508
        tt.create_symlink(tree.get_symlink_target(entry.file_id), trans_id)
 
2509
    elif entry.kind == "directory":
 
2510
        tt.create_directory(trans_id)
 
2511
 
 
2512
 
 
2513
def create_from_tree(tt, trans_id, tree, file_id, bytes=None,
 
2514
    filter_tree_path=None):
2855
2515
    """Create new file contents according to tree contents.
2856
 
 
 
2516
    
2857
2517
    :param filter_tree_path: the tree path to use to lookup
2858
2518
      content filters to apply to the bytes output in the working tree.
2859
2519
      This only applies if the working tree supports content filtering.
2860
2520
    """
2861
 
    kind = tree.kind(path)
 
2521
    kind = tree.kind(file_id)
2862
2522
    if kind == 'directory':
2863
2523
        tt.create_directory(trans_id)
2864
2524
    elif kind == "file":
2865
 
        if chunks is None:
2866
 
            f = tree.get_file(path)
2867
 
            chunks = osutils.file_iterator(f)
2868
 
        else:
2869
 
            f = None
2870
 
        try:
2871
 
            wt = tt._tree
2872
 
            if wt.supports_content_filtering() and filter_tree_path is not None:
2873
 
                filters = wt._content_filter_stack(filter_tree_path)
2874
 
                chunks = filtered_output_bytes(
2875
 
                    chunks, filters,
2876
 
                    ContentFilterContext(filter_tree_path, tree))
2877
 
            tt.create_file(chunks, trans_id)
2878
 
        finally:
2879
 
            if f is not None:
2880
 
                f.close()
 
2525
        if bytes is None:
 
2526
            tree_file = tree.get_file(file_id)
 
2527
            try:
 
2528
                bytes = tree_file.readlines()
 
2529
            finally:
 
2530
                tree_file.close()
 
2531
        wt = tt._tree
 
2532
        if wt.supports_content_filtering() and filter_tree_path is not None:
 
2533
            filters = wt._content_filter_stack(filter_tree_path)
 
2534
            bytes = filtered_output_bytes(bytes, filters,
 
2535
                ContentFilterContext(filter_tree_path, tree))
 
2536
        tt.create_file(bytes, trans_id)
2881
2537
    elif kind == "symlink":
2882
 
        tt.create_symlink(tree.get_symlink_target(path), trans_id)
 
2538
        tt.create_symlink(tree.get_symlink_target(file_id), trans_id)
2883
2539
    else:
2884
2540
        raise AssertionError('Unknown kind %r' % kind)
2885
2541
 
2890
2546
        tt.set_executability(entry.executable, trans_id)
2891
2547
 
2892
2548
 
 
2549
def get_backup_name(entry, by_parent, parent_trans_id, tt):
 
2550
    return _get_backup_name(entry.name, by_parent, parent_trans_id, tt)
 
2551
 
 
2552
 
 
2553
def _get_backup_name(name, by_parent, parent_trans_id, tt):
 
2554
    """Produce a backup-style name that appears to be available"""
 
2555
    def name_gen():
 
2556
        counter = 1
 
2557
        while True:
 
2558
            yield "%s.~%d~" % (name, counter)
 
2559
            counter += 1
 
2560
    for new_name in name_gen():
 
2561
        if not tt.has_named_child(by_parent, parent_trans_id, new_name):
 
2562
            return new_name
 
2563
 
 
2564
 
 
2565
def _entry_changes(file_id, entry, working_tree):
 
2566
    """Determine in which ways the inventory entry has changed.
 
2567
 
 
2568
    Returns booleans: has_contents, content_mod, meta_mod
 
2569
    has_contents means there are currently contents, but they differ
 
2570
    contents_mod means contents need to be modified
 
2571
    meta_mod means the metadata needs to be modified
 
2572
    """
 
2573
    cur_entry = working_tree.inventory[file_id]
 
2574
    try:
 
2575
        working_kind = working_tree.kind(file_id)
 
2576
        has_contents = True
 
2577
    except NoSuchFile:
 
2578
        has_contents = False
 
2579
        contents_mod = True
 
2580
        meta_mod = False
 
2581
    if has_contents is True:
 
2582
        if entry.kind != working_kind:
 
2583
            contents_mod, meta_mod = True, False
 
2584
        else:
 
2585
            cur_entry._read_tree_state(working_tree.id2path(file_id),
 
2586
                                       working_tree)
 
2587
            contents_mod, meta_mod = entry.detect_changes(cur_entry)
 
2588
            cur_entry._forget_tree_state()
 
2589
    return has_contents, contents_mod, meta_mod
 
2590
 
 
2591
 
2893
2592
def revert(working_tree, target_tree, filenames, backups=False,
2894
2593
           pb=None, change_reporter=None):
2895
2594
    """Revert a working tree's contents to those of a target tree."""
 
2595
    target_tree.lock_read()
2896
2596
    pb = ui.ui_factory.nested_progress_bar()
 
2597
    tt = TreeTransform(working_tree, pb)
2897
2598
    try:
2898
 
        with target_tree.lock_read(), working_tree.get_transform(pb) as tt:
2899
 
            pp = ProgressPhase("Revert phase", 3, pb)
2900
 
            conflicts, merge_modified = _prepare_revert_transform(
2901
 
                working_tree, target_tree, tt, filenames, backups, pp)
2902
 
            if change_reporter:
2903
 
                change_reporter = delta._ChangeReporter(
2904
 
                    unversioned_filter=working_tree.is_ignored)
2905
 
                delta.report_changes(tt.iter_changes(), change_reporter)
2906
 
            for conflict in conflicts:
2907
 
                trace.warning(text_type(conflict))
2908
 
            pp.next_phase()
2909
 
            tt.apply()
2910
 
            if working_tree.supports_merge_modified():
2911
 
                working_tree.set_merge_modified(merge_modified)
 
2599
        pp = ProgressPhase("Revert phase", 3, pb)
 
2600
        conflicts, merge_modified = _prepare_revert_transform(
 
2601
            working_tree, target_tree, tt, filenames, backups, pp)
 
2602
        if change_reporter:
 
2603
            change_reporter = delta._ChangeReporter(
 
2604
                unversioned_filter=working_tree.is_ignored)
 
2605
            delta.report_changes(tt.iter_changes(), change_reporter)
 
2606
        for conflict in conflicts:
 
2607
            warning(conflict)
 
2608
        pp.next_phase()
 
2609
        tt.apply()
 
2610
        working_tree.set_merge_modified(merge_modified)
2912
2611
    finally:
 
2612
        target_tree.unlock()
 
2613
        tt.finalize()
2913
2614
        pb.clear()
2914
2615
    return conflicts
2915
2616
 
2917
2618
def _prepare_revert_transform(working_tree, target_tree, tt, filenames,
2918
2619
                              backups, pp, basis_tree=None,
2919
2620
                              merge_modified=None):
2920
 
    with ui.ui_factory.nested_progress_bar() as child_pb:
 
2621
    child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
2622
    try:
2921
2623
        if merge_modified is None:
2922
2624
            merge_modified = working_tree.merge_modified()
2923
2625
        merge_modified = _alter_files(working_tree, target_tree, tt,
2924
2626
                                      child_pb, filenames, backups,
2925
2627
                                      merge_modified, basis_tree)
2926
 
    with ui.ui_factory.nested_progress_bar() as child_pb:
2927
 
        raw_conflicts = resolve_conflicts(
2928
 
            tt, child_pb, lambda t, c: conflict_pass(t, c, target_tree))
 
2628
    finally:
 
2629
        child_pb.finished()
 
2630
    child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
2631
    try:
 
2632
        raw_conflicts = resolve_conflicts(tt, child_pb,
 
2633
            lambda t, c: conflict_pass(t, c, target_tree))
 
2634
    finally:
 
2635
        child_pb.finished()
2929
2636
    conflicts = cook_conflicts(raw_conflicts, tt)
2930
2637
    return conflicts, merge_modified
2931
2638
 
2934
2641
                 backups, merge_modified, basis_tree=None):
2935
2642
    if basis_tree is not None:
2936
2643
        basis_tree.lock_read()
2937
 
    # We ask the working_tree for its changes relative to the target, rather
2938
 
    # than the target changes relative to the working tree. Because WT4 has an
2939
 
    # optimizer to compare itself to a target, but no optimizer for the
2940
 
    # reverse.
2941
 
    change_list = working_tree.iter_changes(
2942
 
        target_tree, specific_files=specific_files, pb=pb)
2943
 
    if not target_tree.is_versioned(u''):
 
2644
    change_list = target_tree.iter_changes(working_tree,
 
2645
        specific_files=specific_files, pb=pb)
 
2646
    if target_tree.get_root_id() is None:
2944
2647
        skip_root = True
2945
2648
    else:
2946
2649
        skip_root = False
2947
2650
    try:
2948
2651
        deferred_files = []
2949
 
        for id_num, change in enumerate(change_list):
2950
 
            file_id = change.file_id
2951
 
            target_path, wt_path = change.path
2952
 
            target_versioned, wt_versioned = change.versioned
2953
 
            target_parent, wt_parent = change.parent_id
2954
 
            target_name, wt_name = change.name
2955
 
            target_kind, wt_kind = change.kind
2956
 
            target_executable, wt_executable = change.executable
2957
 
            if skip_root and wt_parent is None:
 
2652
        for id_num, (file_id, path, changed_content, versioned, parent, name,
 
2653
                kind, executable) in enumerate(change_list):
 
2654
            if skip_root and file_id[0] is not None and parent[0] is None:
2958
2655
                continue
2959
2656
            trans_id = tt.trans_id_file_id(file_id)
2960
2657
            mode_id = None
2961
 
            if change.changed_content:
 
2658
            if changed_content:
2962
2659
                keep_content = False
2963
 
                if wt_kind == 'file' and (backups or target_kind is None):
2964
 
                    wt_sha1 = working_tree.get_file_sha1(wt_path)
2965
 
                    if merge_modified.get(wt_path) != wt_sha1:
 
2660
                if kind[0] == 'file' and (backups or kind[1] is None):
 
2661
                    wt_sha1 = working_tree.get_file_sha1(file_id)
 
2662
                    if merge_modified.get(file_id) != wt_sha1:
2966
2663
                        # acquire the basis tree lazily to prevent the
2967
2664
                        # expense of accessing it when it's not needed ?
2968
2665
                        # (Guessing, RBC, 200702)
2969
2666
                        if basis_tree is None:
2970
2667
                            basis_tree = working_tree.basis_tree()
2971
2668
                            basis_tree.lock_read()
2972
 
                        basis_path = find_previous_path(
2973
 
                            working_tree, basis_tree, wt_path)
2974
 
                        if basis_path is None:
2975
 
                            if target_kind is None and not target_versioned:
2976
 
                                keep_content = True
2977
 
                        else:
2978
 
                            if wt_sha1 != basis_tree.get_file_sha1(basis_path):
2979
 
                                keep_content = True
2980
 
                if wt_kind is not None:
 
2669
                        if file_id in basis_tree:
 
2670
                            if wt_sha1 != basis_tree.get_file_sha1(file_id):
 
2671
                                keep_content = True
 
2672
                        elif kind[1] is None and not versioned[1]:
 
2673
                            keep_content = True
 
2674
                if kind[0] is not None:
2981
2675
                    if not keep_content:
2982
2676
                        tt.delete_contents(trans_id)
2983
 
                    elif target_kind is not None:
2984
 
                        parent_trans_id = tt.trans_id_file_id(wt_parent)
2985
 
                        backup_name = tt._available_backup_name(
2986
 
                            wt_name, parent_trans_id)
 
2677
                    elif kind[1] is not None:
 
2678
                        parent_trans_id = tt.trans_id_file_id(parent[0])
 
2679
                        by_parent = tt.by_parent()
 
2680
                        backup_name = _get_backup_name(name[0], by_parent,
 
2681
                                                       parent_trans_id, tt)
2987
2682
                        tt.adjust_path(backup_name, parent_trans_id, trans_id)
2988
 
                        new_trans_id = tt.create_path(wt_name, parent_trans_id)
2989
 
                        if wt_versioned and target_versioned:
 
2683
                        new_trans_id = tt.create_path(name[0], parent_trans_id)
 
2684
                        if versioned == (True, True):
2990
2685
                            tt.unversion_file(trans_id)
2991
2686
                            tt.version_file(file_id, new_trans_id)
2992
2687
                        # New contents should have the same unix perms as old
2993
2688
                        # contents
2994
2689
                        mode_id = trans_id
2995
2690
                        trans_id = new_trans_id
2996
 
                if target_kind in ('directory', 'tree-reference'):
 
2691
                if kind[1] in ('directory', 'tree-reference'):
2997
2692
                    tt.create_directory(trans_id)
2998
 
                    if target_kind == 'tree-reference':
2999
 
                        revision = target_tree.get_reference_revision(
3000
 
                            target_path)
 
2693
                    if kind[1] == 'tree-reference':
 
2694
                        revision = target_tree.get_reference_revision(file_id,
 
2695
                                                                      path[1])
3001
2696
                        tt.set_tree_reference(revision, trans_id)
3002
 
                elif target_kind == 'symlink':
3003
 
                    tt.create_symlink(target_tree.get_symlink_target(
3004
 
                        target_path), trans_id)
3005
 
                elif target_kind == 'file':
3006
 
                    deferred_files.append(
3007
 
                        (target_path, (trans_id, mode_id, file_id)))
 
2697
                elif kind[1] == 'symlink':
 
2698
                    tt.create_symlink(target_tree.get_symlink_target(file_id),
 
2699
                                      trans_id)
 
2700
                elif kind[1] == 'file':
 
2701
                    deferred_files.append((file_id, (trans_id, mode_id)))
3008
2702
                    if basis_tree is None:
3009
2703
                        basis_tree = working_tree.basis_tree()
3010
2704
                        basis_tree.lock_read()
3011
 
                    new_sha1 = target_tree.get_file_sha1(target_path)
3012
 
                    basis_path = find_previous_path(target_tree, basis_tree, target_path)
3013
 
                    if (basis_path is not None and
3014
 
                            new_sha1 == basis_tree.get_file_sha1(basis_path)):
3015
 
                        # If the new contents of the file match what is in basis,
3016
 
                        # then there is no need to store in merge_modified.
3017
 
                        if basis_path in merge_modified:
3018
 
                            del merge_modified[basis_path]
 
2705
                    new_sha1 = target_tree.get_file_sha1(file_id)
 
2706
                    if (file_id in basis_tree and new_sha1 ==
 
2707
                        basis_tree.get_file_sha1(file_id)):
 
2708
                        if file_id in merge_modified:
 
2709
                            del merge_modified[file_id]
3019
2710
                    else:
3020
 
                        merge_modified[target_path] = new_sha1
 
2711
                        merge_modified[file_id] = new_sha1
3021
2712
 
3022
2713
                    # preserve the execute bit when backing up
3023
 
                    if keep_content and wt_executable == target_executable:
3024
 
                        tt.set_executability(target_executable, trans_id)
3025
 
                elif target_kind is not None:
3026
 
                    raise AssertionError(target_kind)
3027
 
            if not wt_versioned and target_versioned:
 
2714
                    if keep_content and executable[0] == executable[1]:
 
2715
                        tt.set_executability(executable[1], trans_id)
 
2716
                elif kind[1] is not None:
 
2717
                    raise AssertionError(kind[1])
 
2718
            if versioned == (False, True):
3028
2719
                tt.version_file(file_id, trans_id)
3029
 
            if wt_versioned and not target_versioned:
 
2720
            if versioned == (True, False):
3030
2721
                tt.unversion_file(trans_id)
3031
 
            if (target_name is not None
3032
 
                    and (wt_name != target_name or wt_parent != target_parent)):
3033
 
                if target_name == '' and target_parent is None:
 
2722
            if (name[1] is not None and
 
2723
                (name[0] != name[1] or parent[0] != parent[1])):
 
2724
                if name[1] == '' and parent[1] is None:
3034
2725
                    parent_trans = ROOT_PARENT
3035
2726
                else:
3036
 
                    parent_trans = tt.trans_id_file_id(target_parent)
3037
 
                if wt_parent is None and wt_versioned:
3038
 
                    tt.adjust_root_path(target_name, parent_trans)
 
2727
                    parent_trans = tt.trans_id_file_id(parent[1])
 
2728
                if parent[0] is None and versioned[0]:
 
2729
                    tt.adjust_root_path(name[1], parent_trans)
3039
2730
                else:
3040
 
                    tt.adjust_path(target_name, parent_trans, trans_id)
3041
 
            if wt_executable != target_executable and target_kind == "file":
3042
 
                tt.set_executability(target_executable, trans_id)
 
2731
                    tt.adjust_path(name[1], parent_trans, trans_id)
 
2732
            if executable[0] != executable[1] and kind[1] == "file":
 
2733
                tt.set_executability(executable[1], trans_id)
3043
2734
        if working_tree.supports_content_filtering():
3044
 
            for (trans_id, mode_id, file_id), bytes in (
3045
 
                    target_tree.iter_files_bytes(deferred_files)):
 
2735
            for index, ((trans_id, mode_id), bytes) in enumerate(
 
2736
                target_tree.iter_files_bytes(deferred_files)):
 
2737
                file_id = deferred_files[index][0]
3046
2738
                # We're reverting a tree to the target tree so using the
3047
2739
                # target tree to find the file path seems the best choice
3048
2740
                # here IMO - Ian C 27/Oct/2009
3049
2741
                filter_tree_path = target_tree.id2path(file_id)
3050
2742
                filters = working_tree._content_filter_stack(filter_tree_path)
3051
 
                bytes = filtered_output_bytes(
3052
 
                    bytes, filters,
 
2743
                bytes = filtered_output_bytes(bytes, filters,
3053
2744
                    ContentFilterContext(filter_tree_path, working_tree))
3054
2745
                tt.create_file(bytes, trans_id, mode_id)
3055
2746
        else:
3056
 
            for (trans_id, mode_id, file_id), bytes in target_tree.iter_files_bytes(
3057
 
                    deferred_files):
 
2747
            for (trans_id, mode_id), bytes in target_tree.iter_files_bytes(
 
2748
                deferred_files):
3058
2749
                tt.create_file(bytes, trans_id, mode_id)
3059
2750
        tt.fixup_new_roots()
3060
2751
    finally:
3068
2759
    if pass_func is None:
3069
2760
        pass_func = conflict_pass
3070
2761
    new_conflicts = set()
3071
 
    with ui.ui_factory.nested_progress_bar() as pb:
 
2762
    pb = ui.ui_factory.nested_progress_bar()
 
2763
    try:
3072
2764
        for n in range(10):
3073
 
            pb.update(gettext('Resolution pass'), n + 1, 10)
 
2765
            pb.update('Resolution pass', n+1, 10)
3074
2766
            conflicts = tt.find_conflicts()
3075
2767
            if len(conflicts) == 0:
3076
2768
                return new_conflicts
3077
2769
            new_conflicts.update(pass_func(tt, conflicts))
3078
2770
        raise MalformedTransform(conflicts=conflicts)
 
2771
    finally:
 
2772
        pb.finished()
3079
2773
 
3080
2774
 
3081
2775
def conflict_pass(tt, conflicts, path_tree=None):
3098
2792
                existing_file, new_file = conflict[2], conflict[1]
3099
2793
            else:
3100
2794
                existing_file, new_file = conflict[1], conflict[2]
3101
 
            new_name = tt.final_name(existing_file) + '.moved'
 
2795
            new_name = tt.final_name(existing_file)+'.moved'
3102
2796
            tt.adjust_path(new_name, final_parent, existing_file)
3103
2797
            new_conflicts.add((c_type, 'Moved existing file to',
3104
2798
                               existing_file, new_file))
3113
2807
 
3114
2808
        elif c_type == 'missing parent':
3115
2809
            trans_id = conflict[1]
3116
 
            if trans_id in tt._removed_contents:
3117
 
                cancel_deletion = True
3118
 
                orphans = tt._get_potential_orphans(trans_id)
3119
 
                if orphans:
3120
 
                    cancel_deletion = False
3121
 
                    # All children are orphans
3122
 
                    for o in orphans:
3123
 
                        try:
3124
 
                            tt.new_orphan(o, trans_id)
3125
 
                        except OrphaningError:
3126
 
                            # Something bad happened so we cancel the directory
3127
 
                            # deletion which will leave it in place with a
3128
 
                            # conflict. The user can deal with it from there.
3129
 
                            # Note that this also catch the case where we don't
3130
 
                            # want to create orphans and leave the directory in
3131
 
                            # place.
3132
 
                            cancel_deletion = True
3133
 
                            break
3134
 
                if cancel_deletion:
3135
 
                    # Cancel the directory deletion
3136
 
                    tt.cancel_deletion(trans_id)
3137
 
                    new_conflicts.add(('deleting parent', 'Not deleting',
3138
 
                                       trans_id))
3139
 
            else:
 
2810
            try:
 
2811
                tt.cancel_deletion(trans_id)
 
2812
                new_conflicts.add(('deleting parent', 'Not deleting',
 
2813
                                   trans_id))
 
2814
            except KeyError:
3140
2815
                create = True
3141
2816
                try:
3142
2817
                    tt.final_name(trans_id)
3145
2820
                        file_id = tt.final_file_id(trans_id)
3146
2821
                        if file_id is None:
3147
2822
                            file_id = tt.inactive_file_id(trans_id)
3148
 
                        _, entry = next(path_tree.iter_entries_by_dir(
3149
 
                            specific_files=[path_tree.id2path(file_id)]))
 
2823
                        entry = path_tree.inventory[file_id]
3150
2824
                        # special-case the other tree root (move its
3151
2825
                        # children to current root)
3152
2826
                        if entry.parent_id is None:
3167
2841
        elif c_type == 'unversioned parent':
3168
2842
            file_id = tt.inactive_file_id(conflict[1])
3169
2843
            # special-case the other tree root (move its children instead)
3170
 
            if path_tree and path_tree.path2id('') == file_id:
3171
 
                # This is the root entry, skip it
3172
 
                continue
 
2844
            if path_tree and file_id in path_tree:
 
2845
                if path_tree.inventory[file_id].parent_id is None:
 
2846
                    continue
3173
2847
            tt.version_file(file_id, conflict[1])
3174
2848
            new_conflicts.add((c_type, 'Versioned directory', conflict[1]))
3175
2849
        elif c_type == 'non-directory parent':
3178
2852
            parent_name = tt.final_name(parent_id)
3179
2853
            parent_file_id = tt.final_file_id(parent_id)
3180
2854
            new_parent_id = tt.new_directory(parent_name + '.new',
3181
 
                                             parent_parent, parent_file_id)
 
2855
                parent_parent, parent_file_id)
3182
2856
            _reparent_transform_children(tt, parent_id, new_parent_id)
3183
2857
            if parent_file_id is not None:
3184
2858
                tt.unversion_file(parent_id)
3190
2864
 
3191
2865
def cook_conflicts(raw_conflicts, tt):
3192
2866
    """Generate a list of cooked conflicts, sorted by file path"""
 
2867
    from bzrlib.conflicts import Conflict
3193
2868
    conflict_iter = iter_cook_conflicts(raw_conflicts, tt)
3194
 
    return sorted(conflict_iter, key=conflicts.Conflict.sort_key)
 
2869
    return sorted(conflict_iter, key=Conflict.sort_key)
3195
2870
 
3196
2871
 
3197
2872
def iter_cook_conflicts(raw_conflicts, tt):
 
2873
    from bzrlib.conflicts import Conflict
3198
2874
    fp = FinalPaths(tt)
3199
2875
    for conflict in raw_conflicts:
3200
2876
        c_type = conflict[0]
3202
2878
        modified_path = fp.get_path(conflict[2])
3203
2879
        modified_id = tt.final_file_id(conflict[2])
3204
2880
        if len(conflict) == 3:
3205
 
            yield conflicts.Conflict.factory(
3206
 
                c_type, action=action, path=modified_path, file_id=modified_id)
 
2881
            yield Conflict.factory(c_type, action=action, path=modified_path,
 
2882
                                     file_id=modified_id)
3207
2883
 
3208
2884
        else:
3209
2885
            conflicting_path = fp.get_path(conflict[3])
3210
2886
            conflicting_id = tt.final_file_id(conflict[3])
3211
 
            yield conflicts.Conflict.factory(
3212
 
                c_type, action=action, path=modified_path,
3213
 
                file_id=modified_id,
3214
 
                conflict_path=conflicting_path,
3215
 
                conflict_file_id=conflicting_id)
 
2887
            yield Conflict.factory(c_type, action=action, path=modified_path,
 
2888
                                   file_id=modified_id,
 
2889
                                   conflict_path=conflicting_path,
 
2890
                                   conflict_file_id=conflicting_id)
3216
2891
 
3217
2892
 
3218
2893
class _FileMover(object):
3225
2900
    def rename(self, from_, to):
3226
2901
        """Rename a file from one path to another."""
3227
2902
        try:
3228
 
            os.rename(from_, to)
3229
 
        except OSError as e:
 
2903
            osutils.rename(from_, to)
 
2904
        except OSError, e:
3230
2905
            if e.errno in (errno.EEXIST, errno.ENOTEMPTY):
3231
2906
                raise errors.FileExists(to, str(e))
3232
 
            # normal OSError doesn't include filenames so it's hard to see where
3233
 
            # the problem is, see https://bugs.launchpad.net/bzr/+bug/491763
3234
 
            raise errors.TransformRenameFailed(from_, to, str(e), e.errno)
 
2907
            raise
3235
2908
        self.past_renames.append((from_, to))
3236
2909
 
3237
2910
    def pre_delete(self, from_, to):
3247
2920
    def rollback(self):
3248
2921
        """Reverse all renames that have been performed"""
3249
2922
        for from_, to in reversed(self.past_renames):
3250
 
            try:
3251
 
                os.rename(to, from_)
3252
 
            except OSError as e:
3253
 
                raise errors.TransformRenameFailed(to, from_, str(e), e.errno)
 
2923
            osutils.rename(to, from_)
3254
2924
        # after rollback, don't reuse _FileMover
3255
 
        self.past_renames = None
3256
 
        self.pending_deletions = None
 
2925
        past_renames = None
 
2926
        pending_deletions = None
3257
2927
 
3258
2928
    def apply_deletions(self):
3259
2929
        """Apply all marked deletions"""
3260
2930
        for path in self.pending_deletions:
3261
2931
            delete_any(path)
3262
2932
        # after apply_deletions, don't reuse _FileMover
3263
 
        self.past_renames = None
3264
 
        self.pending_deletions = None
3265
 
 
3266
 
 
3267
 
def link_tree(target_tree, source_tree):
3268
 
    """Where possible, hard-link files in a tree to those in another tree.
3269
 
 
3270
 
    :param target_tree: Tree to change
3271
 
    :param source_tree: Tree to hard-link from
3272
 
    """
3273
 
    with target_tree.get_transform() as tt:
3274
 
        for change in target_tree.iter_changes(source_tree, include_unchanged=True):
3275
 
            if change.changed_content:
3276
 
                continue
3277
 
            if change.kind != ('file', 'file'):
3278
 
                continue
3279
 
            if change.executable[0] != change.executable[1]:
3280
 
                continue
3281
 
            trans_id = tt.trans_id_tree_path(change.path[1])
3282
 
            tt.delete_contents(trans_id)
3283
 
            tt.create_hardlink(source_tree.abspath(change.path[0]), trans_id)
3284
 
        tt.apply()
 
2933
        past_renames = None
 
2934
        pending_deletions = None