/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: Aaron Bentley
  • Date: 2007-12-20 19:16:02 UTC
  • mto: This revision was merged to the branch mainline in revision 3235.
  • Revision ID: abentley@panoramicfeedback.com-20071220191602-q1p5o3svshujfodd
Add support for hardlinks to TreeTransform

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
import os
 
18
import errno
 
19
from stat import S_ISREG
 
20
 
 
21
from bzrlib.lazy_import import lazy_import
 
22
lazy_import(globals(), """
 
23
from bzrlib import (
 
24
    bzrdir,
 
25
    delta,
 
26
    errors,
 
27
    inventory
 
28
    )
 
29
""")
 
30
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
 
31
                           ReusingTransform, NotVersionedError, CantMoveRoot,
 
32
                           ExistingLimbo, ImmortalLimbo, NoFinalPath,
 
33
                           UnableCreateSymlink)
 
34
from bzrlib.inventory import InventoryEntry
 
35
from bzrlib.osutils import (file_kind, supports_executable, pathjoin, lexists,
 
36
                            delete_any, has_symlinks)
 
37
from bzrlib.progress import DummyProgress, ProgressPhase
 
38
from bzrlib.symbol_versioning import (
 
39
        deprecated_function,
 
40
        zero_fifteen,
 
41
        zero_ninety,
 
42
        )
 
43
from bzrlib.trace import mutter, warning
 
44
from bzrlib import tree
 
45
import bzrlib.ui
 
46
import bzrlib.urlutils as urlutils
 
47
 
 
48
 
 
49
ROOT_PARENT = "root-parent"
 
50
 
 
51
 
 
52
def unique_add(map, key, value):
 
53
    if key in map:
 
54
        raise DuplicateKey(key=key)
 
55
    map[key] = value
 
56
 
 
57
 
 
58
class _TransformResults(object):
 
59
    def __init__(self, modified_paths, rename_count):
 
60
        object.__init__(self)
 
61
        self.modified_paths = modified_paths
 
62
        self.rename_count = rename_count
 
63
 
 
64
 
 
65
class TreeTransform(object):
 
66
    """Represent a tree transformation.
 
67
    
 
68
    This object is designed to support incremental generation of the transform,
 
69
    in any order.
 
70
 
 
71
    However, it gives optimum performance when parent directories are created
 
72
    before their contents.  The transform is then able to put child files
 
73
    directly in their parent directory, avoiding later renames.
 
74
    
 
75
    It is easy to produce malformed transforms, but they are generally
 
76
    harmless.  Attempting to apply a malformed transform will cause an
 
77
    exception to be raised before any modifications are made to the tree.  
 
78
 
 
79
    Many kinds of malformed transforms can be corrected with the 
 
80
    resolve_conflicts function.  The remaining ones indicate programming error,
 
81
    such as trying to create a file with no path.
 
82
 
 
83
    Two sets of file creation methods are supplied.  Convenience methods are:
 
84
     * new_file
 
85
     * new_directory
 
86
     * new_symlink
 
87
 
 
88
    These are composed of the low-level methods:
 
89
     * create_path
 
90
     * create_file or create_directory or create_symlink
 
91
     * version_file
 
92
     * set_executability
 
93
 
 
94
    Transform/Transaction ids
 
95
    -------------------------
 
96
    trans_ids are temporary ids assigned to all files involved in a transform.
 
97
    It's possible, even common, that not all files in the Tree have trans_ids.
 
98
 
 
99
    trans_ids are used because filenames and file_ids are not good enough
 
100
    identifiers; filenames change, and not all files have file_ids.  File-ids
 
101
    are also associated with trans-ids, so that moving a file moves its
 
102
    file-id.
 
103
 
 
104
    trans_ids are only valid for the TreeTransform that generated them.
 
105
 
 
106
    Limbo
 
107
    -----
 
108
    Limbo is a temporary directory use to hold new versions of files.
 
109
    Files are added to limbo by create_file, create_directory, create_symlink,
 
110
    and their convenience variants (new_*).  Files may be removed from limbo
 
111
    using cancel_creation.  Files are renamed from limbo into their final
 
112
    location as part of TreeTransform.apply
 
113
 
 
114
    Limbo must be cleaned up, by either calling TreeTransform.apply or
 
115
    calling TreeTransform.finalize.
 
116
 
 
117
    Files are placed into limbo inside their parent directories, where
 
118
    possible.  This reduces subsequent renames, and makes operations involving
 
119
    lots of files faster.  This optimization is only possible if the parent
 
120
    directory is created *before* creating any of its children, so avoid
 
121
    creating children before parents, where possible.
 
122
 
 
123
    Pending-deletion
 
124
    ----------------
 
125
    This temporary directory is used by _FileMover for storing files that are
 
126
    about to be deleted.  In case of rollback, the files will be restored.
 
127
    FileMover does not delete files until it is sure that a rollback will not
 
128
    happen.  
 
129
    """
 
130
    def __init__(self, tree, pb=DummyProgress()):
 
131
        """Note: a tree_write lock is taken on the tree.
 
132
        
 
133
        Use TreeTransform.finalize() to release the lock (can be omitted if
 
134
        TreeTransform.apply() called).
 
135
        """
 
136
        object.__init__(self)
 
137
        self._tree = tree
 
138
        self._tree.lock_tree_write()
 
139
        try:
 
140
            control_files = self._tree._control_files
 
141
            self._limbodir = urlutils.local_path_from_url(
 
142
                control_files.controlfilename('limbo'))
 
143
            try:
 
144
                os.mkdir(self._limbodir)
 
145
            except OSError, e:
 
146
                if e.errno == errno.EEXIST:
 
147
                    raise ExistingLimbo(self._limbodir)
 
148
            self._deletiondir = urlutils.local_path_from_url(
 
149
                control_files.controlfilename('pending-deletion'))
 
150
            try:
 
151
                os.mkdir(self._deletiondir)
 
152
            except OSError, e:
 
153
                if e.errno == errno.EEXIST:
 
154
                    raise errors.ExistingPendingDeletion(self._deletiondir)
 
155
 
 
156
        except: 
 
157
            self._tree.unlock()
 
158
            raise
 
159
 
 
160
        # counter used to generate trans-ids (which are locally unique)
 
161
        self._id_number = 0
 
162
        # mapping of trans_id -> new basename
 
163
        self._new_name = {}
 
164
        # mapping of trans_id -> new parent trans_id
 
165
        self._new_parent = {}
 
166
        # mapping of trans_id with new contents -> new file_kind
 
167
        self._new_contents = {}
 
168
        # A mapping of transform ids to their limbo filename
 
169
        self._limbo_files = {}
 
170
        # A mapping of transform ids to a set of the transform ids of children
 
171
        # that their limbo directory has
 
172
        self._limbo_children = {}
 
173
        # Map transform ids to maps of child filename to child transform id
 
174
        self._limbo_children_names = {}
 
175
        # List of transform ids that need to be renamed from limbo into place
 
176
        self._needs_rename = set()
 
177
        # Set of trans_ids whose contents will be removed
 
178
        self._removed_contents = set()
 
179
        # Mapping of trans_id -> new execute-bit value
 
180
        self._new_executability = {}
 
181
        # Mapping of trans_id -> new tree-reference value
 
182
        self._new_reference_revision = {}
 
183
        # Mapping of trans_id -> new file_id
 
184
        self._new_id = {}
 
185
        # Mapping of old file-id -> trans_id
 
186
        self._non_present_ids = {}
 
187
        # Mapping of new file_id -> trans_id
 
188
        self._r_new_id = {}
 
189
        # Set of file_ids that will be removed
 
190
        self._removed_id = set()
 
191
        # Mapping of path in old tree -> trans_id
 
192
        self._tree_path_ids = {}
 
193
        # Mapping trans_id -> path in old tree
 
194
        self._tree_id_paths = {}
 
195
        # Cache of realpath results, to speed up canonical_path
 
196
        self._realpaths = {}
 
197
        # Cache of relpath results, to speed up canonical_path
 
198
        self._relpaths = {}
 
199
        # The trans_id that will be used as the tree root
 
200
        self._new_root = self.trans_id_tree_file_id(tree.get_root_id())
 
201
        # Indictor of whether the transform has been applied
 
202
        self.__done = False
 
203
        # A progress bar
 
204
        self._pb = pb
 
205
        # A counter of how many files have been renamed
 
206
        self.rename_count = 0
 
207
 
 
208
    def __get_root(self):
 
209
        return self._new_root
 
210
 
 
211
    root = property(__get_root)
 
212
 
 
213
    def finalize(self):
 
214
        """Release the working tree lock, if held, clean up limbo dir.
 
215
 
 
216
        This is required if apply has not been invoked, but can be invoked
 
217
        even after apply.
 
218
        """
 
219
        if self._tree is None:
 
220
            return
 
221
        try:
 
222
            entries = [(self._limbo_name(t), t, k) for t, k in
 
223
                       self._new_contents.iteritems()]
 
224
            entries.sort(reverse=True)
 
225
            for path, trans_id, kind in entries:
 
226
                if kind == "directory":
 
227
                    os.rmdir(path)
 
228
                else:
 
229
                    os.unlink(path)
 
230
            try:
 
231
                os.rmdir(self._limbodir)
 
232
            except OSError:
 
233
                # We don't especially care *why* the dir is immortal.
 
234
                raise ImmortalLimbo(self._limbodir)
 
235
            try:
 
236
                os.rmdir(self._deletiondir)
 
237
            except OSError:
 
238
                raise errors.ImmortalPendingDeletion(self._deletiondir)
 
239
        finally:
 
240
            self._tree.unlock()
 
241
            self._tree = None
 
242
 
 
243
    def _assign_id(self):
 
244
        """Produce a new tranform id"""
 
245
        new_id = "new-%s" % self._id_number
 
246
        self._id_number +=1
 
247
        return new_id
 
248
 
 
249
    def create_path(self, name, parent):
 
250
        """Assign a transaction id to a new path"""
 
251
        trans_id = self._assign_id()
 
252
        unique_add(self._new_name, trans_id, name)
 
253
        unique_add(self._new_parent, trans_id, parent)
 
254
        return trans_id
 
255
 
 
256
    def adjust_path(self, name, parent, trans_id):
 
257
        """Change the path that is assigned to a transaction id."""
 
258
        if trans_id == self._new_root:
 
259
            raise CantMoveRoot
 
260
        previous_parent = self._new_parent.get(trans_id)
 
261
        previous_name = self._new_name.get(trans_id)
 
262
        self._new_name[trans_id] = name
 
263
        self._new_parent[trans_id] = parent
 
264
        if (trans_id in self._limbo_files and
 
265
            trans_id not in self._needs_rename):
 
266
            self._rename_in_limbo([trans_id])
 
267
            self._limbo_children[previous_parent].remove(trans_id)
 
268
            del self._limbo_children_names[previous_parent][previous_name]
 
269
 
 
270
    def _rename_in_limbo(self, trans_ids):
 
271
        """Fix limbo names so that the right final path is produced.
 
272
 
 
273
        This means we outsmarted ourselves-- we tried to avoid renaming
 
274
        these files later by creating them with their final names in their
 
275
        final parents.  But now the previous name or parent is no longer
 
276
        suitable, so we have to rename them.
 
277
 
 
278
        Even for trans_ids that have no new contents, we must remove their
 
279
        entries from _limbo_files, because they are now stale.
 
280
        """
 
281
        for trans_id in trans_ids:
 
282
            old_path = self._limbo_files.pop(trans_id)
 
283
            if trans_id not in self._new_contents:
 
284
                continue
 
285
            new_path = self._limbo_name(trans_id)
 
286
            os.rename(old_path, new_path)
 
287
 
 
288
    def adjust_root_path(self, name, parent):
 
289
        """Emulate moving the root by moving all children, instead.
 
290
        
 
291
        We do this by undoing the association of root's transaction id with the
 
292
        current tree.  This allows us to create a new directory with that
 
293
        transaction id.  We unversion the root directory and version the 
 
294
        physically new directory, and hope someone versions the tree root
 
295
        later.
 
296
        """
 
297
        old_root = self._new_root
 
298
        old_root_file_id = self.final_file_id(old_root)
 
299
        # force moving all children of root
 
300
        for child_id in self.iter_tree_children(old_root):
 
301
            if child_id != parent:
 
302
                self.adjust_path(self.final_name(child_id), 
 
303
                                 self.final_parent(child_id), child_id)
 
304
            file_id = self.final_file_id(child_id)
 
305
            if file_id is not None:
 
306
                self.unversion_file(child_id)
 
307
            self.version_file(file_id, child_id)
 
308
        
 
309
        # the physical root needs a new transaction id
 
310
        self._tree_path_ids.pop("")
 
311
        self._tree_id_paths.pop(old_root)
 
312
        self._new_root = self.trans_id_tree_file_id(self._tree.get_root_id())
 
313
        if parent == old_root:
 
314
            parent = self._new_root
 
315
        self.adjust_path(name, parent, old_root)
 
316
        self.create_directory(old_root)
 
317
        self.version_file(old_root_file_id, old_root)
 
318
        self.unversion_file(self._new_root)
 
319
 
 
320
    def trans_id_tree_file_id(self, inventory_id):
 
321
        """Determine the transaction id of a working tree file.
 
322
        
 
323
        This reflects only files that already exist, not ones that will be
 
324
        added by transactions.
 
325
        """
 
326
        path = self._tree.inventory.id2path(inventory_id)
 
327
        return self.trans_id_tree_path(path)
 
328
 
 
329
    def trans_id_file_id(self, file_id):
 
330
        """Determine or set the transaction id associated with a file ID.
 
331
        A new id is only created for file_ids that were never present.  If
 
332
        a transaction has been unversioned, it is deliberately still returned.
 
333
        (this will likely lead to an unversioned parent conflict.)
 
334
        """
 
335
        if file_id in self._r_new_id and self._r_new_id[file_id] is not None:
 
336
            return self._r_new_id[file_id]
 
337
        elif file_id in self._tree.inventory:
 
338
            return self.trans_id_tree_file_id(file_id)
 
339
        elif file_id in self._non_present_ids:
 
340
            return self._non_present_ids[file_id]
 
341
        else:
 
342
            trans_id = self._assign_id()
 
343
            self._non_present_ids[file_id] = trans_id
 
344
            return trans_id
 
345
 
 
346
    def canonical_path(self, path):
 
347
        """Get the canonical tree-relative path"""
 
348
        # don't follow final symlinks
 
349
        abs = self._tree.abspath(path)
 
350
        if abs in self._relpaths:
 
351
            return self._relpaths[abs]
 
352
        dirname, basename = os.path.split(abs)
 
353
        if dirname not in self._realpaths:
 
354
            self._realpaths[dirname] = os.path.realpath(dirname)
 
355
        dirname = self._realpaths[dirname]
 
356
        abs = pathjoin(dirname, basename)
 
357
        if dirname in self._relpaths:
 
358
            relpath = pathjoin(self._relpaths[dirname], basename)
 
359
            relpath = relpath.rstrip('/\\')
 
360
        else:
 
361
            relpath = self._tree.relpath(abs)
 
362
        self._relpaths[abs] = relpath
 
363
        return relpath
 
364
 
 
365
    def trans_id_tree_path(self, path):
 
366
        """Determine (and maybe set) the transaction ID for a tree path."""
 
367
        path = self.canonical_path(path)
 
368
        if path not in self._tree_path_ids:
 
369
            self._tree_path_ids[path] = self._assign_id()
 
370
            self._tree_id_paths[self._tree_path_ids[path]] = path
 
371
        return self._tree_path_ids[path]
 
372
 
 
373
    def get_tree_parent(self, trans_id):
 
374
        """Determine id of the parent in the tree."""
 
375
        path = self._tree_id_paths[trans_id]
 
376
        if path == "":
 
377
            return ROOT_PARENT
 
378
        return self.trans_id_tree_path(os.path.dirname(path))
 
379
 
 
380
    def create_file(self, contents, trans_id, mode_id=None):
 
381
        """Schedule creation of a new file.
 
382
 
 
383
        See also new_file.
 
384
        
 
385
        Contents is an iterator of strings, all of which will be written
 
386
        to the target destination.
 
387
 
 
388
        New file takes the permissions of any existing file with that id,
 
389
        unless mode_id is specified.
 
390
        """
 
391
        name = self._limbo_name(trans_id)
 
392
        f = open(name, 'wb')
 
393
        try:
 
394
            try:
 
395
                unique_add(self._new_contents, trans_id, 'file')
 
396
            except:
 
397
                # Clean up the file, it never got registered so
 
398
                # TreeTransform.finalize() won't clean it up.
 
399
                f.close()
 
400
                os.unlink(name)
 
401
                raise
 
402
 
 
403
            f.writelines(contents)
 
404
        finally:
 
405
            f.close()
 
406
        self._set_mode(trans_id, mode_id, S_ISREG)
 
407
 
 
408
    def _set_mode(self, trans_id, mode_id, typefunc):
 
409
        """Set the mode of new file contents.
 
410
        The mode_id is the existing file to get the mode from (often the same
 
411
        as trans_id).  The operation is only performed if there's a mode match
 
412
        according to typefunc.
 
413
        """
 
414
        if mode_id is None:
 
415
            mode_id = trans_id
 
416
        try:
 
417
            old_path = self._tree_id_paths[mode_id]
 
418
        except KeyError:
 
419
            return
 
420
        try:
 
421
            mode = os.stat(self._tree.abspath(old_path)).st_mode
 
422
        except OSError, e:
 
423
            if e.errno == errno.ENOENT:
 
424
                return
 
425
            else:
 
426
                raise
 
427
        if typefunc(mode):
 
428
            os.chmod(self._limbo_name(trans_id), mode)
 
429
 
 
430
    def create_hardlink(self, path, trans_id):
 
431
        """Schedule creation of a hard link"""
 
432
        name = self._limbo_name(trans_id)
 
433
        os.link(path, name)
 
434
        try:
 
435
            unique_add(self._new_contents, trans_id, 'file')
 
436
        except:
 
437
            # Clean up the file, it never got registered so
 
438
            # TreeTransform.finalize() won't clean it up.
 
439
            os.unlink(name)
 
440
            raise
 
441
 
 
442
    def create_directory(self, trans_id):
 
443
        """Schedule creation of a new directory.
 
444
        
 
445
        See also new_directory.
 
446
        """
 
447
        os.mkdir(self._limbo_name(trans_id))
 
448
        unique_add(self._new_contents, trans_id, 'directory')
 
449
 
 
450
    def create_symlink(self, target, trans_id):
 
451
        """Schedule creation of a new symbolic link.
 
452
 
 
453
        target is a bytestring.
 
454
        See also new_symlink.
 
455
        """
 
456
        if has_symlinks():
 
457
            os.symlink(target, self._limbo_name(trans_id))
 
458
            unique_add(self._new_contents, trans_id, 'symlink')
 
459
        else:
 
460
            try:
 
461
                path = FinalPaths(self).get_path(trans_id)
 
462
            except KeyError:
 
463
                path = None
 
464
            raise UnableCreateSymlink(path=path)
 
465
 
 
466
    def cancel_creation(self, trans_id):
 
467
        """Cancel the creation of new file contents."""
 
468
        del self._new_contents[trans_id]
 
469
        children = self._limbo_children.get(trans_id)
 
470
        # if this is a limbo directory with children, move them before removing
 
471
        # the directory
 
472
        if children is not None:
 
473
            self._rename_in_limbo(children)
 
474
            del self._limbo_children[trans_id]
 
475
            del self._limbo_children_names[trans_id]
 
476
        delete_any(self._limbo_name(trans_id))
 
477
 
 
478
    def delete_contents(self, trans_id):
 
479
        """Schedule the contents of a path entry for deletion"""
 
480
        self.tree_kind(trans_id)
 
481
        self._removed_contents.add(trans_id)
 
482
 
 
483
    def cancel_deletion(self, trans_id):
 
484
        """Cancel a scheduled deletion"""
 
485
        self._removed_contents.remove(trans_id)
 
486
 
 
487
    def unversion_file(self, trans_id):
 
488
        """Schedule a path entry to become unversioned"""
 
489
        self._removed_id.add(trans_id)
 
490
 
 
491
    def delete_versioned(self, trans_id):
 
492
        """Delete and unversion a versioned file"""
 
493
        self.delete_contents(trans_id)
 
494
        self.unversion_file(trans_id)
 
495
 
 
496
    def set_executability(self, executability, trans_id):
 
497
        """Schedule setting of the 'execute' bit
 
498
        To unschedule, set to None
 
499
        """
 
500
        if executability is None:
 
501
            del self._new_executability[trans_id]
 
502
        else:
 
503
            unique_add(self._new_executability, trans_id, executability)
 
504
 
 
505
    def set_tree_reference(self, revision_id, trans_id):
 
506
        """Set the reference associated with a directory"""
 
507
        unique_add(self._new_reference_revision, trans_id, revision_id)
 
508
 
 
509
    def version_file(self, file_id, trans_id):
 
510
        """Schedule a file to become versioned."""
 
511
        assert file_id is not None
 
512
        unique_add(self._new_id, trans_id, file_id)
 
513
        unique_add(self._r_new_id, file_id, trans_id)
 
514
 
 
515
    def cancel_versioning(self, trans_id):
 
516
        """Undo a previous versioning of a file"""
 
517
        file_id = self._new_id[trans_id]
 
518
        del self._new_id[trans_id]
 
519
        del self._r_new_id[file_id]
 
520
 
 
521
    def new_paths(self):
 
522
        """Determine the paths of all new and changed files"""
 
523
        new_ids = set()
 
524
        fp = FinalPaths(self)
 
525
        for id_set in (self._new_name, self._new_parent, self._new_contents,
 
526
                       self._new_id, self._new_executability):
 
527
            new_ids.update(id_set)
 
528
        new_paths = [(fp.get_path(t), t) for t in new_ids]
 
529
        new_paths.sort()
 
530
        return new_paths
 
531
 
 
532
    def tree_kind(self, trans_id):
 
533
        """Determine the file kind in the working tree.
 
534
 
 
535
        Raises NoSuchFile if the file does not exist
 
536
        """
 
537
        path = self._tree_id_paths.get(trans_id)
 
538
        if path is None:
 
539
            raise NoSuchFile(None)
 
540
        try:
 
541
            return file_kind(self._tree.abspath(path))
 
542
        except OSError, e:
 
543
            if e.errno != errno.ENOENT:
 
544
                raise
 
545
            else:
 
546
                raise NoSuchFile(path)
 
547
 
 
548
    def final_kind(self, trans_id):
 
549
        """Determine the final file kind, after any changes applied.
 
550
        
 
551
        Raises NoSuchFile if the file does not exist/has no contents.
 
552
        (It is conceivable that a path would be created without the
 
553
        corresponding contents insertion command)
 
554
        """
 
555
        if trans_id in self._new_contents:
 
556
            return self._new_contents[trans_id]
 
557
        elif trans_id in self._removed_contents:
 
558
            raise NoSuchFile(None)
 
559
        else:
 
560
            return self.tree_kind(trans_id)
 
561
 
 
562
    def tree_file_id(self, trans_id):
 
563
        """Determine the file id associated with the trans_id in the tree"""
 
564
        try:
 
565
            path = self._tree_id_paths[trans_id]
 
566
        except KeyError:
 
567
            # the file is a new, unversioned file, or invalid trans_id
 
568
            return None
 
569
        # the file is old; the old id is still valid
 
570
        if self._new_root == trans_id:
 
571
            return self._tree.get_root_id()
 
572
        return self._tree.inventory.path2id(path)
 
573
 
 
574
    def final_file_id(self, trans_id):
 
575
        """Determine the file id after any changes are applied, or None.
 
576
        
 
577
        None indicates that the file will not be versioned after changes are
 
578
        applied.
 
579
        """
 
580
        try:
 
581
            # there is a new id for this file
 
582
            assert self._new_id[trans_id] is not None
 
583
            return self._new_id[trans_id]
 
584
        except KeyError:
 
585
            if trans_id in self._removed_id:
 
586
                return None
 
587
        return self.tree_file_id(trans_id)
 
588
 
 
589
    def inactive_file_id(self, trans_id):
 
590
        """Return the inactive file_id associated with a transaction id.
 
591
        That is, the one in the tree or in non_present_ids.
 
592
        The file_id may actually be active, too.
 
593
        """
 
594
        file_id = self.tree_file_id(trans_id)
 
595
        if file_id is not None:
 
596
            return file_id
 
597
        for key, value in self._non_present_ids.iteritems():
 
598
            if value == trans_id:
 
599
                return key
 
600
 
 
601
    def final_parent(self, trans_id):
 
602
        """Determine the parent file_id, after any changes are applied.
 
603
 
 
604
        ROOT_PARENT is returned for the tree root.
 
605
        """
 
606
        try:
 
607
            return self._new_parent[trans_id]
 
608
        except KeyError:
 
609
            return self.get_tree_parent(trans_id)
 
610
 
 
611
    def final_name(self, trans_id):
 
612
        """Determine the final filename, after all changes are applied."""
 
613
        try:
 
614
            return self._new_name[trans_id]
 
615
        except KeyError:
 
616
            try:
 
617
                return os.path.basename(self._tree_id_paths[trans_id])
 
618
            except KeyError:
 
619
                raise NoFinalPath(trans_id, self)
 
620
 
 
621
    def by_parent(self):
 
622
        """Return a map of parent: children for known parents.
 
623
        
 
624
        Only new paths and parents of tree files with assigned ids are used.
 
625
        """
 
626
        by_parent = {}
 
627
        items = list(self._new_parent.iteritems())
 
628
        items.extend((t, self.final_parent(t)) for t in 
 
629
                      self._tree_id_paths.keys())
 
630
        for trans_id, parent_id in items:
 
631
            if parent_id not in by_parent:
 
632
                by_parent[parent_id] = set()
 
633
            by_parent[parent_id].add(trans_id)
 
634
        return by_parent
 
635
 
 
636
    def path_changed(self, trans_id):
 
637
        """Return True if a trans_id's path has changed."""
 
638
        return (trans_id in self._new_name) or (trans_id in self._new_parent)
 
639
 
 
640
    def new_contents(self, trans_id):
 
641
        return (trans_id in self._new_contents)
 
642
 
 
643
    def find_conflicts(self):
 
644
        """Find any violations of inventory or filesystem invariants"""
 
645
        if self.__done is True:
 
646
            raise ReusingTransform()
 
647
        conflicts = []
 
648
        # ensure all children of all existent parents are known
 
649
        # all children of non-existent parents are known, by definition.
 
650
        self._add_tree_children()
 
651
        by_parent = self.by_parent()
 
652
        conflicts.extend(self._unversioned_parents(by_parent))
 
653
        conflicts.extend(self._parent_loops())
 
654
        conflicts.extend(self._duplicate_entries(by_parent))
 
655
        conflicts.extend(self._duplicate_ids())
 
656
        conflicts.extend(self._parent_type_conflicts(by_parent))
 
657
        conflicts.extend(self._improper_versioning())
 
658
        conflicts.extend(self._executability_conflicts())
 
659
        conflicts.extend(self._overwrite_conflicts())
 
660
        return conflicts
 
661
 
 
662
    def _add_tree_children(self):
 
663
        """Add all the children of all active parents to the known paths.
 
664
 
 
665
        Active parents are those which gain children, and those which are
 
666
        removed.  This is a necessary first step in detecting conflicts.
 
667
        """
 
668
        parents = self.by_parent().keys()
 
669
        parents.extend([t for t in self._removed_contents if 
 
670
                        self.tree_kind(t) == 'directory'])
 
671
        for trans_id in self._removed_id:
 
672
            file_id = self.tree_file_id(trans_id)
 
673
            if self._tree.inventory[file_id].kind == 'directory':
 
674
                parents.append(trans_id)
 
675
 
 
676
        for parent_id in parents:
 
677
            # ensure that all children are registered with the transaction
 
678
            list(self.iter_tree_children(parent_id))
 
679
 
 
680
    def iter_tree_children(self, parent_id):
 
681
        """Iterate through the entry's tree children, if any"""
 
682
        try:
 
683
            path = self._tree_id_paths[parent_id]
 
684
        except KeyError:
 
685
            return
 
686
        try:
 
687
            children = os.listdir(self._tree.abspath(path))
 
688
        except OSError, e:
 
689
            if e.errno != errno.ENOENT and e.errno != errno.ESRCH:
 
690
                raise
 
691
            return
 
692
            
 
693
        for child in children:
 
694
            childpath = joinpath(path, child)
 
695
            if self._tree.is_control_filename(childpath):
 
696
                continue
 
697
            yield self.trans_id_tree_path(childpath)
 
698
 
 
699
    def has_named_child(self, by_parent, parent_id, name):
 
700
        try:
 
701
            children = by_parent[parent_id]
 
702
        except KeyError:
 
703
            children = []
 
704
        for child in children:
 
705
            if self.final_name(child) == name:
 
706
                return True
 
707
        try:
 
708
            path = self._tree_id_paths[parent_id]
 
709
        except KeyError:
 
710
            return False
 
711
        childpath = joinpath(path, name)
 
712
        child_id = self._tree_path_ids.get(childpath)
 
713
        if child_id is None:
 
714
            return lexists(self._tree.abspath(childpath))
 
715
        else:
 
716
            if self.final_parent(child_id) != parent_id:
 
717
                return False
 
718
            if child_id in self._removed_contents:
 
719
                # XXX What about dangling file-ids?
 
720
                return False
 
721
            else:
 
722
                return True
 
723
 
 
724
    def _parent_loops(self):
 
725
        """No entry should be its own ancestor"""
 
726
        conflicts = []
 
727
        for trans_id in self._new_parent:
 
728
            seen = set()
 
729
            parent_id = trans_id
 
730
            while parent_id is not ROOT_PARENT:
 
731
                seen.add(parent_id)
 
732
                try:
 
733
                    parent_id = self.final_parent(parent_id)
 
734
                except KeyError:
 
735
                    break
 
736
                if parent_id == trans_id:
 
737
                    conflicts.append(('parent loop', trans_id))
 
738
                if parent_id in seen:
 
739
                    break
 
740
        return conflicts
 
741
 
 
742
    def _unversioned_parents(self, by_parent):
 
743
        """If parent directories are versioned, children must be versioned."""
 
744
        conflicts = []
 
745
        for parent_id, children in by_parent.iteritems():
 
746
            if parent_id is ROOT_PARENT:
 
747
                continue
 
748
            if self.final_file_id(parent_id) is not None:
 
749
                continue
 
750
            for child_id in children:
 
751
                if self.final_file_id(child_id) is not None:
 
752
                    conflicts.append(('unversioned parent', parent_id))
 
753
                    break;
 
754
        return conflicts
 
755
 
 
756
    def _improper_versioning(self):
 
757
        """Cannot version a file with no contents, or a bad type.
 
758
        
 
759
        However, existing entries with no contents are okay.
 
760
        """
 
761
        conflicts = []
 
762
        for trans_id in self._new_id.iterkeys():
 
763
            try:
 
764
                kind = self.final_kind(trans_id)
 
765
            except NoSuchFile:
 
766
                conflicts.append(('versioning no contents', trans_id))
 
767
                continue
 
768
            if not InventoryEntry.versionable_kind(kind):
 
769
                conflicts.append(('versioning bad kind', trans_id, kind))
 
770
        return conflicts
 
771
 
 
772
    def _executability_conflicts(self):
 
773
        """Check for bad executability changes.
 
774
        
 
775
        Only versioned files may have their executability set, because
 
776
        1. only versioned entries can have executability under windows
 
777
        2. only files can be executable.  (The execute bit on a directory
 
778
           does not indicate searchability)
 
779
        """
 
780
        conflicts = []
 
781
        for trans_id in self._new_executability:
 
782
            if self.final_file_id(trans_id) is None:
 
783
                conflicts.append(('unversioned executability', trans_id))
 
784
            else:
 
785
                try:
 
786
                    non_file = self.final_kind(trans_id) != "file"
 
787
                except NoSuchFile:
 
788
                    non_file = True
 
789
                if non_file is True:
 
790
                    conflicts.append(('non-file executability', trans_id))
 
791
        return conflicts
 
792
 
 
793
    def _overwrite_conflicts(self):
 
794
        """Check for overwrites (not permitted on Win32)"""
 
795
        conflicts = []
 
796
        for trans_id in self._new_contents:
 
797
            try:
 
798
                self.tree_kind(trans_id)
 
799
            except NoSuchFile:
 
800
                continue
 
801
            if trans_id not in self._removed_contents:
 
802
                conflicts.append(('overwrite', trans_id,
 
803
                                 self.final_name(trans_id)))
 
804
        return conflicts
 
805
 
 
806
    def _duplicate_entries(self, by_parent):
 
807
        """No directory may have two entries with the same name."""
 
808
        conflicts = []
 
809
        if (self._new_name, self._new_parent) == ({}, {}):
 
810
            return conflicts
 
811
        for children in by_parent.itervalues():
 
812
            name_ids = [(self.final_name(t), t) for t in children]
 
813
            if not self._tree.case_sensitive:
 
814
                name_ids = [(n.lower(), t) for n, t in name_ids]
 
815
            name_ids.sort()
 
816
            last_name = None
 
817
            last_trans_id = None
 
818
            for name, trans_id in name_ids:
 
819
                try:
 
820
                    kind = self.final_kind(trans_id)
 
821
                except NoSuchFile:
 
822
                    kind = None
 
823
                file_id = self.final_file_id(trans_id)
 
824
                if kind is None and file_id is None:
 
825
                    continue
 
826
                if name == last_name:
 
827
                    conflicts.append(('duplicate', last_trans_id, trans_id,
 
828
                    name))
 
829
                last_name = name
 
830
                last_trans_id = trans_id
 
831
        return conflicts
 
832
 
 
833
    def _duplicate_ids(self):
 
834
        """Each inventory id may only be used once"""
 
835
        conflicts = []
 
836
        removed_tree_ids = set((self.tree_file_id(trans_id) for trans_id in
 
837
                                self._removed_id))
 
838
        active_tree_ids = set((f for f in self._tree.inventory if
 
839
                               f not in removed_tree_ids))
 
840
        for trans_id, file_id in self._new_id.iteritems():
 
841
            if file_id in active_tree_ids:
 
842
                old_trans_id = self.trans_id_tree_file_id(file_id)
 
843
                conflicts.append(('duplicate id', old_trans_id, trans_id))
 
844
        return conflicts
 
845
 
 
846
    def _parent_type_conflicts(self, by_parent):
 
847
        """parents must have directory 'contents'."""
 
848
        conflicts = []
 
849
        for parent_id, children in by_parent.iteritems():
 
850
            if parent_id is ROOT_PARENT:
 
851
                continue
 
852
            if not self._any_contents(children):
 
853
                continue
 
854
            for child in children:
 
855
                try:
 
856
                    self.final_kind(child)
 
857
                except NoSuchFile:
 
858
                    continue
 
859
            try:
 
860
                kind = self.final_kind(parent_id)
 
861
            except NoSuchFile:
 
862
                kind = None
 
863
            if kind is None:
 
864
                conflicts.append(('missing parent', parent_id))
 
865
            elif kind != "directory":
 
866
                conflicts.append(('non-directory parent', parent_id))
 
867
        return conflicts
 
868
 
 
869
    def _any_contents(self, trans_ids):
 
870
        """Return true if any of the trans_ids, will have contents."""
 
871
        for trans_id in trans_ids:
 
872
            try:
 
873
                kind = self.final_kind(trans_id)
 
874
            except NoSuchFile:
 
875
                continue
 
876
            return True
 
877
        return False
 
878
            
 
879
    def apply(self, no_conflicts=False, _mover=None):
 
880
        """Apply all changes to the inventory and filesystem.
 
881
        
 
882
        If filesystem or inventory conflicts are present, MalformedTransform
 
883
        will be thrown.
 
884
 
 
885
        If apply succeeds, finalize is not necessary.
 
886
 
 
887
        :param no_conflicts: if True, the caller guarantees there are no
 
888
            conflicts, so no check is made.
 
889
        :param _mover: Supply an alternate FileMover, for testing
 
890
        """
 
891
        if not no_conflicts:
 
892
            conflicts = self.find_conflicts()
 
893
            if len(conflicts) != 0:
 
894
                raise MalformedTransform(conflicts=conflicts)
 
895
        inv = self._tree.inventory
 
896
        inventory_delta = []
 
897
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
898
        try:
 
899
            if _mover is None:
 
900
                mover = _FileMover()
 
901
            else:
 
902
                mover = _mover
 
903
            try:
 
904
                child_pb.update('Apply phase', 0, 2)
 
905
                self._apply_removals(inv, inventory_delta, mover)
 
906
                child_pb.update('Apply phase', 1, 2)
 
907
                modified_paths = self._apply_insertions(inv, inventory_delta,
 
908
                                                        mover)
 
909
            except:
 
910
                mover.rollback()
 
911
                raise
 
912
            else:
 
913
                mover.apply_deletions()
 
914
        finally:
 
915
            child_pb.finished()
 
916
        self._tree.apply_inventory_delta(inventory_delta)
 
917
        self.__done = True
 
918
        self.finalize()
 
919
        return _TransformResults(modified_paths, self.rename_count)
 
920
 
 
921
    def _limbo_name(self, trans_id):
 
922
        """Generate the limbo name of a file"""
 
923
        limbo_name = self._limbo_files.get(trans_id)
 
924
        if limbo_name is not None:
 
925
            return limbo_name
 
926
        parent = self._new_parent.get(trans_id)
 
927
        # if the parent directory is already in limbo (e.g. when building a
 
928
        # tree), choose a limbo name inside the parent, to reduce further
 
929
        # renames.
 
930
        use_direct_path = False
 
931
        if self._new_contents.get(parent) == 'directory':
 
932
            filename = self._new_name.get(trans_id)
 
933
            if filename is not None:
 
934
                if parent not in self._limbo_children:
 
935
                    self._limbo_children[parent] = set()
 
936
                    self._limbo_children_names[parent] = {}
 
937
                    use_direct_path = True
 
938
                # the direct path can only be used if no other file has
 
939
                # already taken this pathname, i.e. if the name is unused, or
 
940
                # if it is already associated with this trans_id.
 
941
                elif self._tree.case_sensitive:
 
942
                    if (self._limbo_children_names[parent].get(filename)
 
943
                        in (trans_id, None)):
 
944
                        use_direct_path = True
 
945
                else:
 
946
                    for l_filename, l_trans_id in\
 
947
                        self._limbo_children_names[parent].iteritems():
 
948
                        if l_trans_id == trans_id:
 
949
                            continue
 
950
                        if l_filename.lower() == filename.lower():
 
951
                            break
 
952
                    else:
 
953
                        use_direct_path = True
 
954
 
 
955
        if use_direct_path:
 
956
            limbo_name = pathjoin(self._limbo_files[parent], filename)
 
957
            self._limbo_children[parent].add(trans_id)
 
958
            self._limbo_children_names[parent][filename] = trans_id
 
959
        else:
 
960
            limbo_name = pathjoin(self._limbodir, trans_id)
 
961
            self._needs_rename.add(trans_id)
 
962
        self._limbo_files[trans_id] = limbo_name
 
963
        return limbo_name
 
964
 
 
965
    def _apply_removals(self, inv, inventory_delta, mover):
 
966
        """Perform tree operations that remove directory/inventory names.
 
967
        
 
968
        That is, delete files that are to be deleted, and put any files that
 
969
        need renaming into limbo.  This must be done in strict child-to-parent
 
970
        order.
 
971
        """
 
972
        tree_paths = list(self._tree_path_ids.iteritems())
 
973
        tree_paths.sort(reverse=True)
 
974
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
975
        try:
 
976
            for num, data in enumerate(tree_paths):
 
977
                path, trans_id = data
 
978
                child_pb.update('removing file', num, len(tree_paths))
 
979
                full_path = self._tree.abspath(path)
 
980
                if trans_id in self._removed_contents:
 
981
                    mover.pre_delete(full_path, os.path.join(self._deletiondir,
 
982
                                     trans_id))
 
983
                elif trans_id in self._new_name or trans_id in \
 
984
                    self._new_parent:
 
985
                    try:
 
986
                        mover.rename(full_path, self._limbo_name(trans_id))
 
987
                    except OSError, e:
 
988
                        if e.errno != errno.ENOENT:
 
989
                            raise
 
990
                    else:
 
991
                        self.rename_count += 1
 
992
                if trans_id in self._removed_id:
 
993
                    if trans_id == self._new_root:
 
994
                        file_id = self._tree.get_root_id()
 
995
                    else:
 
996
                        file_id = self.tree_file_id(trans_id)
 
997
                    assert file_id is not None
 
998
                    inventory_delta.append((path, None, file_id, None))
 
999
        finally:
 
1000
            child_pb.finished()
 
1001
 
 
1002
    def _apply_insertions(self, inv, inventory_delta, mover):
 
1003
        """Perform tree operations that insert directory/inventory names.
 
1004
        
 
1005
        That is, create any files that need to be created, and restore from
 
1006
        limbo any files that needed renaming.  This must be done in strict
 
1007
        parent-to-child order.
 
1008
        """
 
1009
        new_paths = self.new_paths()
 
1010
        modified_paths = []
 
1011
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1012
        completed_new = []
 
1013
        try:
 
1014
            for num, (path, trans_id) in enumerate(new_paths):
 
1015
                new_entry = None
 
1016
                child_pb.update('adding file', num, len(new_paths))
 
1017
                try:
 
1018
                    kind = self._new_contents[trans_id]
 
1019
                except KeyError:
 
1020
                    kind = contents = None
 
1021
                if trans_id in self._new_contents or \
 
1022
                    self.path_changed(trans_id):
 
1023
                    full_path = self._tree.abspath(path)
 
1024
                    if trans_id in self._needs_rename:
 
1025
                        try:
 
1026
                            mover.rename(self._limbo_name(trans_id), full_path)
 
1027
                        except OSError, e:
 
1028
                            # We may be renaming a dangling inventory id
 
1029
                            if e.errno != errno.ENOENT:
 
1030
                                raise
 
1031
                        else:
 
1032
                            self.rename_count += 1
 
1033
                    if trans_id in self._new_contents:
 
1034
                        modified_paths.append(full_path)
 
1035
                        completed_new.append(trans_id)
 
1036
 
 
1037
                if trans_id in self._new_id:
 
1038
                    if kind is None:
 
1039
                        kind = file_kind(self._tree.abspath(path))
 
1040
                    if trans_id in self._new_reference_revision:
 
1041
                        new_entry = inventory.TreeReference(
 
1042
                            self._new_id[trans_id],
 
1043
                            self._new_name[trans_id], 
 
1044
                            self.final_file_id(self._new_parent[trans_id]),
 
1045
                            None, self._new_reference_revision[trans_id])
 
1046
                    else:
 
1047
                        new_entry = inventory.make_entry(kind,
 
1048
                            self.final_name(trans_id),
 
1049
                            self.final_file_id(self.final_parent(trans_id)),
 
1050
                            self._new_id[trans_id])
 
1051
                else:
 
1052
                    if trans_id in self._new_name or trans_id in\
 
1053
                        self._new_parent or\
 
1054
                        trans_id in self._new_executability:
 
1055
                        file_id = self.final_file_id(trans_id)
 
1056
                        if file_id is not None:
 
1057
                            entry = inv[file_id]
 
1058
                            new_entry = entry.copy()
 
1059
 
 
1060
                    if trans_id in self._new_name or trans_id in\
 
1061
                        self._new_parent:
 
1062
                            if new_entry is not None:
 
1063
                                new_entry.name = self.final_name(trans_id)
 
1064
                                parent = self.final_parent(trans_id)
 
1065
                                parent_id = self.final_file_id(parent)
 
1066
                                new_entry.parent_id = parent_id
 
1067
 
 
1068
                if trans_id in self._new_executability:
 
1069
                    self._set_executability(path, new_entry, trans_id)
 
1070
                if new_entry is not None:
 
1071
                    if new_entry.file_id in inv:
 
1072
                        old_path = inv.id2path(new_entry.file_id)
 
1073
                    else:
 
1074
                        old_path = None
 
1075
                    inventory_delta.append((old_path, path,
 
1076
                                            new_entry.file_id,
 
1077
                                            new_entry))
 
1078
        finally:
 
1079
            child_pb.finished()
 
1080
        for trans_id in completed_new:
 
1081
            del self._new_contents[trans_id]
 
1082
        return modified_paths
 
1083
 
 
1084
    def _set_executability(self, path, entry, trans_id):
 
1085
        """Set the executability of versioned files """
 
1086
        new_executability = self._new_executability[trans_id]
 
1087
        entry.executable = new_executability
 
1088
        if supports_executable():
 
1089
            abspath = self._tree.abspath(path)
 
1090
            current_mode = os.stat(abspath).st_mode
 
1091
            if new_executability:
 
1092
                umask = os.umask(0)
 
1093
                os.umask(umask)
 
1094
                to_mode = current_mode | (0100 & ~umask)
 
1095
                # Enable x-bit for others only if they can read it.
 
1096
                if current_mode & 0004:
 
1097
                    to_mode |= 0001 & ~umask
 
1098
                if current_mode & 0040:
 
1099
                    to_mode |= 0010 & ~umask
 
1100
            else:
 
1101
                to_mode = current_mode & ~0111
 
1102
            os.chmod(abspath, to_mode)
 
1103
 
 
1104
    def _new_entry(self, name, parent_id, file_id):
 
1105
        """Helper function to create a new filesystem entry."""
 
1106
        trans_id = self.create_path(name, parent_id)
 
1107
        if file_id is not None:
 
1108
            self.version_file(file_id, trans_id)
 
1109
        return trans_id
 
1110
 
 
1111
    def new_file(self, name, parent_id, contents, file_id=None, 
 
1112
                 executable=None):
 
1113
        """Convenience method to create files.
 
1114
        
 
1115
        name is the name of the file to create.
 
1116
        parent_id is the transaction id of the parent directory of the file.
 
1117
        contents is an iterator of bytestrings, which will be used to produce
 
1118
        the file.
 
1119
        :param file_id: The inventory ID of the file, if it is to be versioned.
 
1120
        :param executable: Only valid when a file_id has been supplied.
 
1121
        """
 
1122
        trans_id = self._new_entry(name, parent_id, file_id)
 
1123
        # TODO: rather than scheduling a set_executable call,
 
1124
        # have create_file create the file with the right mode.
 
1125
        self.create_file(contents, trans_id)
 
1126
        if executable is not None:
 
1127
            self.set_executability(executable, trans_id)
 
1128
        return trans_id
 
1129
 
 
1130
    def new_directory(self, name, parent_id, file_id=None):
 
1131
        """Convenience method to create directories.
 
1132
 
 
1133
        name is the name of the directory to create.
 
1134
        parent_id is the transaction id of the parent directory of the
 
1135
        directory.
 
1136
        file_id is the inventory ID of the directory, if it is to be versioned.
 
1137
        """
 
1138
        trans_id = self._new_entry(name, parent_id, file_id)
 
1139
        self.create_directory(trans_id)
 
1140
        return trans_id 
 
1141
 
 
1142
    def new_symlink(self, name, parent_id, target, file_id=None):
 
1143
        """Convenience method to create symbolic link.
 
1144
        
 
1145
        name is the name of the symlink to create.
 
1146
        parent_id is the transaction id of the parent directory of the symlink.
 
1147
        target is a bytestring of the target of the symlink.
 
1148
        file_id is the inventory ID of the file, if it is to be versioned.
 
1149
        """
 
1150
        trans_id = self._new_entry(name, parent_id, file_id)
 
1151
        self.create_symlink(target, trans_id)
 
1152
        return trans_id
 
1153
 
 
1154
    def _affected_ids(self):
 
1155
        """Return the set of transform ids affected by the transform"""
 
1156
        trans_ids = set(self._removed_id)
 
1157
        trans_ids.update(self._new_id.keys())
 
1158
        trans_ids.update(self._removed_contents)
 
1159
        trans_ids.update(self._new_contents.keys())
 
1160
        trans_ids.update(self._new_executability.keys())
 
1161
        trans_ids.update(self._new_name.keys())
 
1162
        trans_ids.update(self._new_parent.keys())
 
1163
        return trans_ids
 
1164
 
 
1165
    def _get_file_id_maps(self):
 
1166
        """Return mapping of file_ids to trans_ids in the to and from states"""
 
1167
        trans_ids = self._affected_ids()
 
1168
        from_trans_ids = {}
 
1169
        to_trans_ids = {}
 
1170
        # Build up two dicts: trans_ids associated with file ids in the
 
1171
        # FROM state, vs the TO state.
 
1172
        for trans_id in trans_ids:
 
1173
            from_file_id = self.tree_file_id(trans_id)
 
1174
            if from_file_id is not None:
 
1175
                from_trans_ids[from_file_id] = trans_id
 
1176
            to_file_id = self.final_file_id(trans_id)
 
1177
            if to_file_id is not None:
 
1178
                to_trans_ids[to_file_id] = trans_id
 
1179
        return from_trans_ids, to_trans_ids
 
1180
 
 
1181
    def _from_file_data(self, from_trans_id, from_versioned, file_id):
 
1182
        """Get data about a file in the from (tree) state
 
1183
 
 
1184
        Return a (name, parent, kind, executable) tuple
 
1185
        """
 
1186
        from_path = self._tree_id_paths.get(from_trans_id)
 
1187
        if from_versioned:
 
1188
            # get data from working tree if versioned
 
1189
            from_entry = self._tree.inventory[file_id]
 
1190
            from_name = from_entry.name
 
1191
            from_parent = from_entry.parent_id
 
1192
        else:
 
1193
            from_entry = None
 
1194
            if from_path is None:
 
1195
                # File does not exist in FROM state
 
1196
                from_name = None
 
1197
                from_parent = None
 
1198
            else:
 
1199
                # File exists, but is not versioned.  Have to use path-
 
1200
                # splitting stuff
 
1201
                from_name = os.path.basename(from_path)
 
1202
                tree_parent = self.get_tree_parent(from_trans_id)
 
1203
                from_parent = self.tree_file_id(tree_parent)
 
1204
        if from_path is not None:
 
1205
            from_kind, from_executable, from_stats = \
 
1206
                self._tree._comparison_data(from_entry, from_path)
 
1207
        else:
 
1208
            from_kind = None
 
1209
            from_executable = False
 
1210
        return from_name, from_parent, from_kind, from_executable
 
1211
 
 
1212
    def _to_file_data(self, to_trans_id, from_trans_id, from_executable):
 
1213
        """Get data about a file in the to (target) state
 
1214
 
 
1215
        Return a (name, parent, kind, executable) tuple
 
1216
        """
 
1217
        to_name = self.final_name(to_trans_id)
 
1218
        try:
 
1219
            to_kind = self.final_kind(to_trans_id)
 
1220
        except NoSuchFile:
 
1221
            to_kind = None
 
1222
        to_parent = self.final_file_id(self.final_parent(to_trans_id))
 
1223
        if to_trans_id in self._new_executability:
 
1224
            to_executable = self._new_executability[to_trans_id]
 
1225
        elif to_trans_id == from_trans_id:
 
1226
            to_executable = from_executable
 
1227
        else:
 
1228
            to_executable = False
 
1229
        return to_name, to_parent, to_kind, to_executable
 
1230
 
 
1231
    def _iter_changes(self):
 
1232
        """Produce output in the same format as Tree._iter_changes.
 
1233
 
 
1234
        Will produce nonsensical results if invoked while inventory/filesystem
 
1235
        conflicts (as reported by TreeTransform.find_conflicts()) are present.
 
1236
 
 
1237
        This reads the Transform, but only reproduces changes involving a
 
1238
        file_id.  Files that are not versioned in either of the FROM or TO
 
1239
        states are not reflected.
 
1240
        """
 
1241
        final_paths = FinalPaths(self)
 
1242
        from_trans_ids, to_trans_ids = self._get_file_id_maps()
 
1243
        results = []
 
1244
        # Now iterate through all active file_ids
 
1245
        for file_id in set(from_trans_ids.keys() + to_trans_ids.keys()):
 
1246
            modified = False
 
1247
            from_trans_id = from_trans_ids.get(file_id)
 
1248
            # find file ids, and determine versioning state
 
1249
            if from_trans_id is None:
 
1250
                from_versioned = False
 
1251
                from_trans_id = to_trans_ids[file_id]
 
1252
            else:
 
1253
                from_versioned = True
 
1254
            to_trans_id = to_trans_ids.get(file_id)
 
1255
            if to_trans_id is None:
 
1256
                to_versioned = False
 
1257
                to_trans_id = from_trans_id
 
1258
            else:
 
1259
                to_versioned = True
 
1260
 
 
1261
            from_name, from_parent, from_kind, from_executable = \
 
1262
                self._from_file_data(from_trans_id, from_versioned, file_id)
 
1263
 
 
1264
            to_name, to_parent, to_kind, to_executable = \
 
1265
                self._to_file_data(to_trans_id, from_trans_id, from_executable)
 
1266
 
 
1267
            if not from_versioned:
 
1268
                from_path = None
 
1269
            else:
 
1270
                from_path = self._tree_id_paths.get(from_trans_id)
 
1271
            if not to_versioned:
 
1272
                to_path = None
 
1273
            else:
 
1274
                to_path = final_paths.get_path(to_trans_id)
 
1275
            if from_kind != to_kind:
 
1276
                modified = True
 
1277
            elif to_kind in ('file', 'symlink') and (
 
1278
                to_trans_id != from_trans_id or
 
1279
                to_trans_id in self._new_contents):
 
1280
                modified = True
 
1281
            if (not modified and from_versioned == to_versioned and
 
1282
                from_parent==to_parent and from_name == to_name and
 
1283
                from_executable == to_executable):
 
1284
                continue
 
1285
            results.append((file_id, (from_path, to_path), modified,
 
1286
                   (from_versioned, to_versioned),
 
1287
                   (from_parent, to_parent),
 
1288
                   (from_name, to_name),
 
1289
                   (from_kind, to_kind),
 
1290
                   (from_executable, to_executable)))
 
1291
        return iter(sorted(results, key=lambda x:x[1]))
 
1292
 
 
1293
 
 
1294
def joinpath(parent, child):
 
1295
    """Join tree-relative paths, handling the tree root specially"""
 
1296
    if parent is None or parent == "":
 
1297
        return child
 
1298
    else:
 
1299
        return pathjoin(parent, child)
 
1300
 
 
1301
 
 
1302
class FinalPaths(object):
 
1303
    """Make path calculation cheap by memoizing paths.
 
1304
 
 
1305
    The underlying tree must not be manipulated between calls, or else
 
1306
    the results will likely be incorrect.
 
1307
    """
 
1308
    def __init__(self, transform):
 
1309
        object.__init__(self)
 
1310
        self._known_paths = {}
 
1311
        self.transform = transform
 
1312
 
 
1313
    def _determine_path(self, trans_id):
 
1314
        if trans_id == self.transform.root:
 
1315
            return ""
 
1316
        name = self.transform.final_name(trans_id)
 
1317
        parent_id = self.transform.final_parent(trans_id)
 
1318
        if parent_id == self.transform.root:
 
1319
            return name
 
1320
        else:
 
1321
            return pathjoin(self.get_path(parent_id), name)
 
1322
 
 
1323
    def get_path(self, trans_id):
 
1324
        """Find the final path associated with a trans_id"""
 
1325
        if trans_id not in self._known_paths:
 
1326
            self._known_paths[trans_id] = self._determine_path(trans_id)
 
1327
        return self._known_paths[trans_id]
 
1328
 
 
1329
 
 
1330
def topology_sorted_ids(tree):
 
1331
    """Determine the topological order of the ids in a tree"""
 
1332
    file_ids = list(tree)
 
1333
    file_ids.sort(key=tree.id2path)
 
1334
    return file_ids
 
1335
 
 
1336
 
 
1337
def build_tree(tree, wt, accelerator_tree=None):
 
1338
    """Create working tree for a branch, using a TreeTransform.
 
1339
    
 
1340
    This function should be used on empty trees, having a tree root at most.
 
1341
    (see merge and revert functionality for working with existing trees)
 
1342
 
 
1343
    Existing files are handled like so:
 
1344
    
 
1345
    - Existing bzrdirs take precedence over creating new items.  They are
 
1346
      created as '%s.diverted' % name.
 
1347
    - Otherwise, if the content on disk matches the content we are building,
 
1348
      it is silently replaced.
 
1349
    - Otherwise, conflict resolution will move the old file to 'oldname.moved'.
 
1350
 
 
1351
    :param tree: The tree to convert wt into a copy of
 
1352
    :param wt: The working tree that files will be placed into
 
1353
    :param accelerator_tree: A tree which can be used for retrieving file
 
1354
        contents more quickly than tree itself, i.e. a workingtree.  tree
 
1355
        will be used for cases where accelerator_tree's content is different.
 
1356
    """
 
1357
    wt.lock_tree_write()
 
1358
    try:
 
1359
        tree.lock_read()
 
1360
        try:
 
1361
            if accelerator_tree is not None:
 
1362
                accelerator_tree.lock_read()
 
1363
            try:
 
1364
                return _build_tree(tree, wt, accelerator_tree)
 
1365
            finally:
 
1366
                if accelerator_tree is not None:
 
1367
                    accelerator_tree.unlock()
 
1368
        finally:
 
1369
            tree.unlock()
 
1370
    finally:
 
1371
        wt.unlock()
 
1372
 
 
1373
 
 
1374
def _build_tree(tree, wt, accelerator_tree):
 
1375
    """See build_tree."""
 
1376
    if len(wt.inventory) > 1:  # more than just a root
 
1377
        raise errors.WorkingTreeAlreadyPopulated(base=wt.basedir)
 
1378
    file_trans_id = {}
 
1379
    top_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1380
    pp = ProgressPhase("Build phase", 2, top_pb)
 
1381
    if tree.inventory.root is not None:
 
1382
        # This is kind of a hack: we should be altering the root
 
1383
        # as part of the regular tree shape diff logic.
 
1384
        # The conditional test here is to avoid doing an
 
1385
        # expensive operation (flush) every time the root id
 
1386
        # is set within the tree, nor setting the root and thus
 
1387
        # marking the tree as dirty, because we use two different
 
1388
        # idioms here: tree interfaces and inventory interfaces.
 
1389
        if wt.get_root_id() != tree.get_root_id():
 
1390
            wt.set_root_id(tree.get_root_id())
 
1391
            wt.flush()
 
1392
    tt = TreeTransform(wt)
 
1393
    divert = set()
 
1394
    try:
 
1395
        pp.next_phase()
 
1396
        file_trans_id[wt.get_root_id()] = \
 
1397
            tt.trans_id_tree_file_id(wt.get_root_id())
 
1398
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1399
        try:
 
1400
            deferred_contents = []
 
1401
            for num, (tree_path, entry) in \
 
1402
                enumerate(tree.inventory.iter_entries_by_dir()):
 
1403
                pb.update("Building tree", num - len(deferred_contents),
 
1404
                          len(tree.inventory))
 
1405
                if entry.parent_id is None:
 
1406
                    continue
 
1407
                reparent = False
 
1408
                file_id = entry.file_id
 
1409
                target_path = wt.abspath(tree_path)
 
1410
                try:
 
1411
                    kind = file_kind(target_path)
 
1412
                except NoSuchFile:
 
1413
                    pass
 
1414
                else:
 
1415
                    if kind == "directory":
 
1416
                        try:
 
1417
                            bzrdir.BzrDir.open(target_path)
 
1418
                        except errors.NotBranchError:
 
1419
                            pass
 
1420
                        else:
 
1421
                            divert.add(file_id)
 
1422
                    if (file_id not in divert and
 
1423
                        _content_match(tree, entry, file_id, kind,
 
1424
                        target_path)):
 
1425
                        tt.delete_contents(tt.trans_id_tree_path(tree_path))
 
1426
                        if kind == 'directory':
 
1427
                            reparent = True
 
1428
                if entry.parent_id not in file_trans_id:
 
1429
                    raise AssertionError(
 
1430
                        'entry %s parent id %r is not in file_trans_id %r'
 
1431
                        % (entry, entry.parent_id, file_trans_id))
 
1432
                parent_id = file_trans_id[entry.parent_id]
 
1433
                if entry.kind == 'file':
 
1434
                    # We *almost* replicate new_by_entry, so that we can defer
 
1435
                    # getting the file text, and get them all at once.
 
1436
                    trans_id = tt.create_path(entry.name, parent_id)
 
1437
                    file_trans_id[file_id] = trans_id
 
1438
                    tt.version_file(entry.file_id, trans_id)
 
1439
                    executable = tree.is_executable(entry.file_id, tree_path)
 
1440
                    if executable is not None:
 
1441
                        tt.set_executability(executable, trans_id)
 
1442
                    deferred_contents.append((entry.file_id, trans_id))
 
1443
                else:
 
1444
                    file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
 
1445
                                                          tree)
 
1446
                if reparent:
 
1447
                    new_trans_id = file_trans_id[file_id]
 
1448
                    old_parent = tt.trans_id_tree_path(tree_path)
 
1449
                    _reparent_children(tt, old_parent, new_trans_id)
 
1450
            for num, (trans_id, bytes) in enumerate(
 
1451
                _iter_files_bytes_accelerated(tree, accelerator_tree,
 
1452
                                              deferred_contents)):
 
1453
                tt.create_file(bytes, trans_id)
 
1454
                pb.update('Adding file contents',
 
1455
                          (num + len(tree.inventory) - len(deferred_contents)),
 
1456
                          len(tree.inventory))
 
1457
        finally:
 
1458
            pb.finished()
 
1459
        pp.next_phase()
 
1460
        divert_trans = set(file_trans_id[f] for f in divert)
 
1461
        resolver = lambda t, c: resolve_checkout(t, c, divert_trans)
 
1462
        raw_conflicts = resolve_conflicts(tt, pass_func=resolver)
 
1463
        conflicts = cook_conflicts(raw_conflicts, tt)
 
1464
        for conflict in conflicts:
 
1465
            warning(conflict)
 
1466
        try:
 
1467
            wt.add_conflicts(conflicts)
 
1468
        except errors.UnsupportedOperation:
 
1469
            pass
 
1470
        result = tt.apply(no_conflicts=True)
 
1471
    finally:
 
1472
        tt.finalize()
 
1473
        top_pb.finished()
 
1474
    return result
 
1475
 
 
1476
 
 
1477
def _iter_files_bytes_accelerated(tree, accelerator_tree, desired_files):
 
1478
    if accelerator_tree is None:
 
1479
        new_desired_files = desired_files
 
1480
    else:
 
1481
        iter = accelerator_tree._iter_changes(tree, include_unchanged=True)
 
1482
        unchanged = dict((f, p[0]) for (f, p, c, v, d, n, k, e)
 
1483
                         in iter if not c)
 
1484
        new_desired_files = []
 
1485
        for file_id, identifier in desired_files:
 
1486
            accelerator_path = unchanged.get(file_id)
 
1487
            if accelerator_path is None:
 
1488
                new_desired_files.append((file_id, identifier))
 
1489
                continue
 
1490
            contents = accelerator_tree.get_file(file_id, accelerator_path)
 
1491
            try:
 
1492
                want_new = False
 
1493
                contents_bytes = (contents.read(),)
 
1494
            finally:
 
1495
                contents.close()
 
1496
            yield identifier, contents_bytes
 
1497
    for result in tree.iter_files_bytes(new_desired_files):
 
1498
        yield result
 
1499
 
 
1500
 
 
1501
def _reparent_children(tt, old_parent, new_parent):
 
1502
    for child in tt.iter_tree_children(old_parent):
 
1503
        tt.adjust_path(tt.final_name(child), new_parent, child)
 
1504
 
 
1505
 
 
1506
def _content_match(tree, entry, file_id, kind, target_path):
 
1507
    if entry.kind != kind:
 
1508
        return False
 
1509
    if entry.kind == "directory":
 
1510
        return True
 
1511
    if entry.kind == "file":
 
1512
        if tree.get_file(file_id).read() == file(target_path, 'rb').read():
 
1513
            return True
 
1514
    elif entry.kind == "symlink":
 
1515
        if tree.get_symlink_target(file_id) == os.readlink(target_path):
 
1516
            return True
 
1517
    return False
 
1518
 
 
1519
 
 
1520
def resolve_checkout(tt, conflicts, divert):
 
1521
    new_conflicts = set()
 
1522
    for c_type, conflict in ((c[0], c) for c in conflicts):
 
1523
        # Anything but a 'duplicate' would indicate programmer error
 
1524
        assert c_type == 'duplicate', c_type
 
1525
        # Now figure out which is new and which is old
 
1526
        if tt.new_contents(conflict[1]):
 
1527
            new_file = conflict[1]
 
1528
            old_file = conflict[2]
 
1529
        else:
 
1530
            new_file = conflict[2]
 
1531
            old_file = conflict[1]
 
1532
 
 
1533
        # We should only get here if the conflict wasn't completely
 
1534
        # resolved
 
1535
        final_parent = tt.final_parent(old_file)
 
1536
        if new_file in divert:
 
1537
            new_name = tt.final_name(old_file)+'.diverted'
 
1538
            tt.adjust_path(new_name, final_parent, new_file)
 
1539
            new_conflicts.add((c_type, 'Diverted to',
 
1540
                               new_file, old_file))
 
1541
        else:
 
1542
            new_name = tt.final_name(old_file)+'.moved'
 
1543
            tt.adjust_path(new_name, final_parent, old_file)
 
1544
            new_conflicts.add((c_type, 'Moved existing file to',
 
1545
                               old_file, new_file))
 
1546
    return new_conflicts
 
1547
 
 
1548
 
 
1549
def new_by_entry(tt, entry, parent_id, tree):
 
1550
    """Create a new file according to its inventory entry"""
 
1551
    name = entry.name
 
1552
    kind = entry.kind
 
1553
    if kind == 'file':
 
1554
        contents = tree.get_file(entry.file_id).readlines()
 
1555
        executable = tree.is_executable(entry.file_id)
 
1556
        return tt.new_file(name, parent_id, contents, entry.file_id, 
 
1557
                           executable)
 
1558
    elif kind in ('directory', 'tree-reference'):
 
1559
        trans_id = tt.new_directory(name, parent_id, entry.file_id)
 
1560
        if kind == 'tree-reference':
 
1561
            tt.set_tree_reference(entry.reference_revision, trans_id)
 
1562
        return trans_id 
 
1563
    elif kind == 'symlink':
 
1564
        target = tree.get_symlink_target(entry.file_id)
 
1565
        return tt.new_symlink(name, parent_id, target, entry.file_id)
 
1566
    else:
 
1567
        raise errors.BadFileKindError(name, kind)
 
1568
 
 
1569
 
 
1570
def create_by_entry(tt, entry, tree, trans_id, lines=None, mode_id=None):
 
1571
    """Create new file contents according to an inventory entry."""
 
1572
    if entry.kind == "file":
 
1573
        if lines is None:
 
1574
            lines = tree.get_file(entry.file_id).readlines()
 
1575
        tt.create_file(lines, trans_id, mode_id=mode_id)
 
1576
    elif entry.kind == "symlink":
 
1577
        tt.create_symlink(tree.get_symlink_target(entry.file_id), trans_id)
 
1578
    elif entry.kind == "directory":
 
1579
        tt.create_directory(trans_id)
 
1580
 
 
1581
 
 
1582
def create_entry_executability(tt, entry, trans_id):
 
1583
    """Set the executability of a trans_id according to an inventory entry"""
 
1584
    if entry.kind == "file":
 
1585
        tt.set_executability(entry.executable, trans_id)
 
1586
 
 
1587
 
 
1588
@deprecated_function(zero_fifteen)
 
1589
def find_interesting(working_tree, target_tree, filenames):
 
1590
    """Find the ids corresponding to specified filenames.
 
1591
    
 
1592
    Deprecated: Please use tree1.paths2ids(filenames, [tree2]).
 
1593
    """
 
1594
    working_tree.lock_read()
 
1595
    try:
 
1596
        target_tree.lock_read()
 
1597
        try:
 
1598
            return working_tree.paths2ids(filenames, [target_tree])
 
1599
        finally:
 
1600
            target_tree.unlock()
 
1601
    finally:
 
1602
        working_tree.unlock()
 
1603
 
 
1604
 
 
1605
@deprecated_function(zero_ninety)
 
1606
def change_entry(tt, file_id, working_tree, target_tree, 
 
1607
                 trans_id_file_id, backups, trans_id, by_parent):
 
1608
    """Replace a file_id's contents with those from a target tree."""
 
1609
    if file_id is None and target_tree is None:
 
1610
        # skip the logic altogether in the deprecation test
 
1611
        return
 
1612
    e_trans_id = trans_id_file_id(file_id)
 
1613
    entry = target_tree.inventory[file_id]
 
1614
    has_contents, contents_mod, meta_mod, = _entry_changes(file_id, entry, 
 
1615
                                                           working_tree)
 
1616
    if contents_mod:
 
1617
        mode_id = e_trans_id
 
1618
        if has_contents:
 
1619
            if not backups:
 
1620
                tt.delete_contents(e_trans_id)
 
1621
            else:
 
1622
                parent_trans_id = trans_id_file_id(entry.parent_id)
 
1623
                backup_name = get_backup_name(entry, by_parent,
 
1624
                                              parent_trans_id, tt)
 
1625
                tt.adjust_path(backup_name, parent_trans_id, e_trans_id)
 
1626
                tt.unversion_file(e_trans_id)
 
1627
                e_trans_id = tt.create_path(entry.name, parent_trans_id)
 
1628
                tt.version_file(file_id, e_trans_id)
 
1629
                trans_id[file_id] = e_trans_id
 
1630
        create_by_entry(tt, entry, target_tree, e_trans_id, mode_id=mode_id)
 
1631
        create_entry_executability(tt, entry, e_trans_id)
 
1632
 
 
1633
    elif meta_mod:
 
1634
        tt.set_executability(entry.executable, e_trans_id)
 
1635
    if tt.final_name(e_trans_id) != entry.name:
 
1636
        adjust_path  = True
 
1637
    else:
 
1638
        parent_id = tt.final_parent(e_trans_id)
 
1639
        parent_file_id = tt.final_file_id(parent_id)
 
1640
        if parent_file_id != entry.parent_id:
 
1641
            adjust_path = True
 
1642
        else:
 
1643
            adjust_path = False
 
1644
    if adjust_path:
 
1645
        parent_trans_id = trans_id_file_id(entry.parent_id)
 
1646
        tt.adjust_path(entry.name, parent_trans_id, e_trans_id)
 
1647
 
 
1648
 
 
1649
def get_backup_name(entry, by_parent, parent_trans_id, tt):
 
1650
    return _get_backup_name(entry.name, by_parent, parent_trans_id, tt)
 
1651
 
 
1652
 
 
1653
def _get_backup_name(name, by_parent, parent_trans_id, tt):
 
1654
    """Produce a backup-style name that appears to be available"""
 
1655
    def name_gen():
 
1656
        counter = 1
 
1657
        while True:
 
1658
            yield "%s.~%d~" % (name, counter)
 
1659
            counter += 1
 
1660
    for new_name in name_gen():
 
1661
        if not tt.has_named_child(by_parent, parent_trans_id, new_name):
 
1662
            return new_name
 
1663
 
 
1664
 
 
1665
def _entry_changes(file_id, entry, working_tree):
 
1666
    """Determine in which ways the inventory entry has changed.
 
1667
 
 
1668
    Returns booleans: has_contents, content_mod, meta_mod
 
1669
    has_contents means there are currently contents, but they differ
 
1670
    contents_mod means contents need to be modified
 
1671
    meta_mod means the metadata needs to be modified
 
1672
    """
 
1673
    cur_entry = working_tree.inventory[file_id]
 
1674
    try:
 
1675
        working_kind = working_tree.kind(file_id)
 
1676
        has_contents = True
 
1677
    except NoSuchFile:
 
1678
        has_contents = False
 
1679
        contents_mod = True
 
1680
        meta_mod = False
 
1681
    if has_contents is True:
 
1682
        if entry.kind != working_kind:
 
1683
            contents_mod, meta_mod = True, False
 
1684
        else:
 
1685
            cur_entry._read_tree_state(working_tree.id2path(file_id), 
 
1686
                                       working_tree)
 
1687
            contents_mod, meta_mod = entry.detect_changes(cur_entry)
 
1688
            cur_entry._forget_tree_state()
 
1689
    return has_contents, contents_mod, meta_mod
 
1690
 
 
1691
 
 
1692
def revert(working_tree, target_tree, filenames, backups=False,
 
1693
           pb=DummyProgress(), change_reporter=None):
 
1694
    """Revert a working tree's contents to those of a target tree."""
 
1695
    target_tree.lock_read()
 
1696
    tt = TreeTransform(working_tree, pb)
 
1697
    try:
 
1698
        pp = ProgressPhase("Revert phase", 3, pb)
 
1699
        pp.next_phase()
 
1700
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1701
        try:
 
1702
            merge_modified = _alter_files(working_tree, target_tree, tt,
 
1703
                                          child_pb, filenames, backups)
 
1704
        finally:
 
1705
            child_pb.finished()
 
1706
        pp.next_phase()
 
1707
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
1708
        try:
 
1709
            raw_conflicts = resolve_conflicts(tt, child_pb,
 
1710
                lambda t, c: conflict_pass(t, c, target_tree))
 
1711
        finally:
 
1712
            child_pb.finished()
 
1713
        conflicts = cook_conflicts(raw_conflicts, tt)
 
1714
        if change_reporter:
 
1715
            change_reporter = delta._ChangeReporter(
 
1716
                unversioned_filter=working_tree.is_ignored)
 
1717
            delta.report_changes(tt._iter_changes(), change_reporter)
 
1718
        for conflict in conflicts:
 
1719
            warning(conflict)
 
1720
        pp.next_phase()
 
1721
        tt.apply()
 
1722
        working_tree.set_merge_modified(merge_modified)
 
1723
    finally:
 
1724
        target_tree.unlock()
 
1725
        tt.finalize()
 
1726
        pb.clear()
 
1727
    return conflicts
 
1728
 
 
1729
 
 
1730
def _alter_files(working_tree, target_tree, tt, pb, specific_files,
 
1731
                 backups):
 
1732
    merge_modified = working_tree.merge_modified()
 
1733
    change_list = target_tree._iter_changes(working_tree,
 
1734
        specific_files=specific_files, pb=pb)
 
1735
    if target_tree.inventory.root is None:
 
1736
        skip_root = True
 
1737
    else:
 
1738
        skip_root = False
 
1739
    basis_tree = None
 
1740
    try:
 
1741
        deferred_files = []
 
1742
        for id_num, (file_id, path, changed_content, versioned, parent, name,
 
1743
                kind, executable) in enumerate(change_list):
 
1744
            if skip_root and file_id[0] is not None and parent[0] is None:
 
1745
                continue
 
1746
            trans_id = tt.trans_id_file_id(file_id)
 
1747
            mode_id = None
 
1748
            if changed_content:
 
1749
                keep_content = False
 
1750
                if kind[0] == 'file' and (backups or kind[1] is None):
 
1751
                    wt_sha1 = working_tree.get_file_sha1(file_id)
 
1752
                    if merge_modified.get(file_id) != wt_sha1:
 
1753
                        # acquire the basis tree lazily to prevent the
 
1754
                        # expense of accessing it when it's not needed ?
 
1755
                        # (Guessing, RBC, 200702)
 
1756
                        if basis_tree is None:
 
1757
                            basis_tree = working_tree.basis_tree()
 
1758
                            basis_tree.lock_read()
 
1759
                        if file_id in basis_tree:
 
1760
                            if wt_sha1 != basis_tree.get_file_sha1(file_id):
 
1761
                                keep_content = True
 
1762
                        elif kind[1] is None and not versioned[1]:
 
1763
                            keep_content = True
 
1764
                if kind[0] is not None:
 
1765
                    if not keep_content:
 
1766
                        tt.delete_contents(trans_id)
 
1767
                    elif kind[1] is not None:
 
1768
                        parent_trans_id = tt.trans_id_file_id(parent[0])
 
1769
                        by_parent = tt.by_parent()
 
1770
                        backup_name = _get_backup_name(name[0], by_parent,
 
1771
                                                       parent_trans_id, tt)
 
1772
                        tt.adjust_path(backup_name, parent_trans_id, trans_id)
 
1773
                        new_trans_id = tt.create_path(name[0], parent_trans_id)
 
1774
                        if versioned == (True, True):
 
1775
                            tt.unversion_file(trans_id)
 
1776
                            tt.version_file(file_id, new_trans_id)
 
1777
                        # New contents should have the same unix perms as old
 
1778
                        # contents
 
1779
                        mode_id = trans_id
 
1780
                        trans_id = new_trans_id
 
1781
                if kind[1] == 'directory':
 
1782
                    tt.create_directory(trans_id)
 
1783
                elif kind[1] == 'symlink':
 
1784
                    tt.create_symlink(target_tree.get_symlink_target(file_id),
 
1785
                                      trans_id)
 
1786
                elif kind[1] == 'file':
 
1787
                    deferred_files.append((file_id, (trans_id, mode_id)))
 
1788
                    if basis_tree is None:
 
1789
                        basis_tree = working_tree.basis_tree()
 
1790
                        basis_tree.lock_read()
 
1791
                    new_sha1 = target_tree.get_file_sha1(file_id)
 
1792
                    if (file_id in basis_tree and new_sha1 ==
 
1793
                        basis_tree.get_file_sha1(file_id)):
 
1794
                        if file_id in merge_modified:
 
1795
                            del merge_modified[file_id]
 
1796
                    else:
 
1797
                        merge_modified[file_id] = new_sha1
 
1798
 
 
1799
                    # preserve the execute bit when backing up
 
1800
                    if keep_content and executable[0] == executable[1]:
 
1801
                        tt.set_executability(executable[1], trans_id)
 
1802
                else:
 
1803
                    assert kind[1] is None
 
1804
            if versioned == (False, True):
 
1805
                tt.version_file(file_id, trans_id)
 
1806
            if versioned == (True, False):
 
1807
                tt.unversion_file(trans_id)
 
1808
            if (name[1] is not None and 
 
1809
                (name[0] != name[1] or parent[0] != parent[1])):
 
1810
                tt.adjust_path(
 
1811
                    name[1], tt.trans_id_file_id(parent[1]), trans_id)
 
1812
            if executable[0] != executable[1] and kind[1] == "file":
 
1813
                tt.set_executability(executable[1], trans_id)
 
1814
        for (trans_id, mode_id), bytes in target_tree.iter_files_bytes(
 
1815
            deferred_files):
 
1816
            tt.create_file(bytes, trans_id, mode_id)
 
1817
    finally:
 
1818
        if basis_tree is not None:
 
1819
            basis_tree.unlock()
 
1820
    return merge_modified
 
1821
 
 
1822
 
 
1823
def resolve_conflicts(tt, pb=DummyProgress(), pass_func=None):
 
1824
    """Make many conflict-resolution attempts, but die if they fail"""
 
1825
    if pass_func is None:
 
1826
        pass_func = conflict_pass
 
1827
    new_conflicts = set()
 
1828
    try:
 
1829
        for n in range(10):
 
1830
            pb.update('Resolution pass', n+1, 10)
 
1831
            conflicts = tt.find_conflicts()
 
1832
            if len(conflicts) == 0:
 
1833
                return new_conflicts
 
1834
            new_conflicts.update(pass_func(tt, conflicts))
 
1835
        raise MalformedTransform(conflicts=conflicts)
 
1836
    finally:
 
1837
        pb.clear()
 
1838
 
 
1839
 
 
1840
def conflict_pass(tt, conflicts, path_tree=None):
 
1841
    """Resolve some classes of conflicts.
 
1842
 
 
1843
    :param tt: The transform to resolve conflicts in
 
1844
    :param conflicts: The conflicts to resolve
 
1845
    :param path_tree: A Tree to get supplemental paths from
 
1846
    """
 
1847
    new_conflicts = set()
 
1848
    for c_type, conflict in ((c[0], c) for c in conflicts):
 
1849
        if c_type == 'duplicate id':
 
1850
            tt.unversion_file(conflict[1])
 
1851
            new_conflicts.add((c_type, 'Unversioned existing file',
 
1852
                               conflict[1], conflict[2], ))
 
1853
        elif c_type == 'duplicate':
 
1854
            # files that were renamed take precedence
 
1855
            final_parent = tt.final_parent(conflict[1])
 
1856
            if tt.path_changed(conflict[1]):
 
1857
                existing_file, new_file = conflict[2], conflict[1]
 
1858
            else:
 
1859
                existing_file, new_file = conflict[1], conflict[2]
 
1860
            new_name = tt.final_name(existing_file)+'.moved'
 
1861
            tt.adjust_path(new_name, final_parent, existing_file)
 
1862
            new_conflicts.add((c_type, 'Moved existing file to', 
 
1863
                               existing_file, new_file))
 
1864
        elif c_type == 'parent loop':
 
1865
            # break the loop by undoing one of the ops that caused the loop
 
1866
            cur = conflict[1]
 
1867
            while not tt.path_changed(cur):
 
1868
                cur = tt.final_parent(cur)
 
1869
            new_conflicts.add((c_type, 'Cancelled move', cur,
 
1870
                               tt.final_parent(cur),))
 
1871
            tt.adjust_path(tt.final_name(cur), tt.get_tree_parent(cur), cur)
 
1872
            
 
1873
        elif c_type == 'missing parent':
 
1874
            trans_id = conflict[1]
 
1875
            try:
 
1876
                tt.cancel_deletion(trans_id)
 
1877
                new_conflicts.add(('deleting parent', 'Not deleting', 
 
1878
                                   trans_id))
 
1879
            except KeyError:
 
1880
                tt.create_directory(trans_id)
 
1881
                new_conflicts.add((c_type, 'Created directory', trans_id))
 
1882
                try:
 
1883
                    tt.final_name(trans_id)
 
1884
                except NoFinalPath:
 
1885
                    if path_tree is not None:
 
1886
                        file_id = tt.final_file_id(trans_id)
 
1887
                        entry = path_tree.inventory[file_id]
 
1888
                        parent_trans_id = tt.trans_id_file_id(entry.parent_id)
 
1889
                        tt.adjust_path(entry.name, parent_trans_id, trans_id)
 
1890
        elif c_type == 'unversioned parent':
 
1891
            tt.version_file(tt.inactive_file_id(conflict[1]), conflict[1])
 
1892
            new_conflicts.add((c_type, 'Versioned directory', conflict[1]))
 
1893
    return new_conflicts
 
1894
 
 
1895
 
 
1896
def cook_conflicts(raw_conflicts, tt):
 
1897
    """Generate a list of cooked conflicts, sorted by file path"""
 
1898
    from bzrlib.conflicts import Conflict
 
1899
    conflict_iter = iter_cook_conflicts(raw_conflicts, tt)
 
1900
    return sorted(conflict_iter, key=Conflict.sort_key)
 
1901
 
 
1902
 
 
1903
def iter_cook_conflicts(raw_conflicts, tt):
 
1904
    from bzrlib.conflicts import Conflict
 
1905
    fp = FinalPaths(tt)
 
1906
    for conflict in raw_conflicts:
 
1907
        c_type = conflict[0]
 
1908
        action = conflict[1]
 
1909
        modified_path = fp.get_path(conflict[2])
 
1910
        modified_id = tt.final_file_id(conflict[2])
 
1911
        if len(conflict) == 3:
 
1912
            yield Conflict.factory(c_type, action=action, path=modified_path,
 
1913
                                     file_id=modified_id)
 
1914
             
 
1915
        else:
 
1916
            conflicting_path = fp.get_path(conflict[3])
 
1917
            conflicting_id = tt.final_file_id(conflict[3])
 
1918
            yield Conflict.factory(c_type, action=action, path=modified_path,
 
1919
                                   file_id=modified_id, 
 
1920
                                   conflict_path=conflicting_path,
 
1921
                                   conflict_file_id=conflicting_id)
 
1922
 
 
1923
 
 
1924
class _FileMover(object):
 
1925
    """Moves and deletes files for TreeTransform, tracking operations"""
 
1926
 
 
1927
    def __init__(self):
 
1928
        self.past_renames = []
 
1929
        self.pending_deletions = []
 
1930
 
 
1931
    def rename(self, from_, to):
 
1932
        """Rename a file from one path to another.  Functions like os.rename"""
 
1933
        try:
 
1934
            os.rename(from_, to)
 
1935
        except OSError, e:
 
1936
            if e.errno in (errno.EEXIST, errno.ENOTEMPTY):
 
1937
                raise errors.FileExists(to, str(e))
 
1938
            raise
 
1939
        self.past_renames.append((from_, to))
 
1940
 
 
1941
    def pre_delete(self, from_, to):
 
1942
        """Rename a file out of the way and mark it for deletion.
 
1943
 
 
1944
        Unlike os.unlink, this works equally well for files and directories.
 
1945
        :param from_: The current file path
 
1946
        :param to: A temporary path for the file
 
1947
        """
 
1948
        self.rename(from_, to)
 
1949
        self.pending_deletions.append(to)
 
1950
 
 
1951
    def rollback(self):
 
1952
        """Reverse all renames that have been performed"""
 
1953
        for from_, to in reversed(self.past_renames):
 
1954
            os.rename(to, from_)
 
1955
        # after rollback, don't reuse _FileMover
 
1956
        past_renames = None
 
1957
        pending_deletions = None
 
1958
 
 
1959
    def apply_deletions(self):
 
1960
        """Apply all marked deletions"""
 
1961
        for path in self.pending_deletions:
 
1962
            delete_any(path)
 
1963
        # after apply_deletions, don't reuse _FileMover
 
1964
        past_renames = None
 
1965
        pending_deletions = None