1
# Copyright (C) 2004 Aaron Bentley <aaron.bentley@utoronto.ca>
 
 
3
#    This program is free software; you can redistribute it and/or modify
 
 
4
#    it under the terms of the GNU General Public License as published by
 
 
5
#    the Free Software Foundation; either version 2 of the License, or
 
 
6
#    (at your option) any later version.
 
 
8
#    This program is distributed in the hope that it will be useful,
 
 
9
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
10
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
11
#    GNU General Public License for more details.
 
 
13
#    You should have received a copy of the GNU General Public License
 
 
14
#    along with this program; if not, write to the Free Software
 
 
15
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
20
from bzrlib.trace import mutter
 
 
21
from bzrlib.osutils import rename
 
 
23
# XXX: mbp: I'm not totally convinced that we should handle conflicts
 
 
24
# as part of changeset application, rather than only in the merge
 
 
27
"""Represent and apply a changeset
 
 
29
Conflicts in applying a changeset are represented as exceptions.
 
 
32
__docformat__ = "restructuredtext"
 
 
36
class OldFailedTreeOp(Exception):
 
 
38
        Exception.__init__(self, "bzr-tree-change contains files from a"
 
 
39
                           " previous failed merge operation.")
 
 
40
def invert_dict(dict):
 
 
42
    for (key,value) in dict.iteritems():
 
 
48
class ChangeUnixPermissions(object):
 
 
49
    """This is two-way change, suitable for file modification, creation,
 
 
51
    def __init__(self, old_mode, new_mode):
 
 
52
        self.old_mode = old_mode
 
 
53
        self.new_mode = new_mode
 
 
55
    def apply(self, filename, conflict_handler, reverse=False):
 
 
57
            from_mode = self.old_mode
 
 
58
            to_mode = self.new_mode
 
 
60
            from_mode = self.new_mode
 
 
61
            to_mode = self.old_mode
 
 
63
            current_mode = os.stat(filename).st_mode &0777
 
 
65
            if e.errno == errno.ENOENT:
 
 
66
                if conflict_handler.missing_for_chmod(filename) == "skip":
 
 
69
                    current_mode = from_mode
 
 
71
        if from_mode is not None and current_mode != from_mode:
 
 
72
            if conflict_handler.wrong_old_perms(filename, from_mode, 
 
 
73
                                                current_mode) != "continue":
 
 
76
        if to_mode is not None:
 
 
78
                os.chmod(filename, to_mode)
 
 
80
                if e.errno == errno.ENOENT:
 
 
81
                    conflict_handler.missing_for_chmod(filename)
 
 
83
    def __eq__(self, other):
 
 
84
        if not isinstance(other, ChangeUnixPermissions):
 
 
86
        elif self.old_mode != other.old_mode:
 
 
88
        elif self.new_mode != other.new_mode:
 
 
93
    def __ne__(self, other):
 
 
94
        return not (self == other)
 
 
96
def dir_create(filename, conflict_handler, reverse):
 
 
97
    """Creates the directory, or deletes it if reverse is true.  Intended to be
 
 
98
    used with ReplaceContents.
 
 
100
    :param filename: The name of the directory to create
 
 
102
    :param reverse: If true, delete the directory, instead
 
 
109
            if e.errno != errno.EEXIST:
 
 
111
            if conflict_handler.dir_exists(filename) == "continue":
 
 
114
            if e.errno == errno.ENOENT:
 
 
115
                if conflict_handler.missing_parent(filename)=="continue":
 
 
116
                    file(filename, "wb").write(self.contents)
 
 
123
            if conflict_handler.rmdir_non_empty(filename) == "skip":
 
 
130
class SymlinkCreate(object):
 
 
131
    """Creates or deletes a symlink (for use with ReplaceContents)"""
 
 
132
    def __init__(self, contents):
 
 
135
        :param contents: The filename of the target the symlink should point to
 
 
138
        self.target = contents
 
 
140
    def __call__(self, filename, conflict_handler, reverse):
 
 
141
        """Creates or destroys the symlink.
 
 
143
        :param filename: The name of the symlink to create
 
 
147
            assert(os.readlink(filename) == self.target)
 
 
151
                os.symlink(self.target, filename)
 
 
153
                if e.errno != errno.EEXIST:
 
 
155
                if conflict_handler.link_name_exists(filename) == "continue":
 
 
156
                    os.symlink(self.target, filename)
 
 
158
    def __eq__(self, other):
 
 
159
        if not isinstance(other, SymlinkCreate):
 
 
161
        elif self.target != other.target:
 
 
166
    def __ne__(self, other):
 
 
167
        return not (self == other)
 
 
169
class FileCreate(object):
 
 
170
    """Create or delete a file (for use with ReplaceContents)"""
 
 
171
    def __init__(self, contents):
 
 
174
        :param contents: The contents of the file to write
 
 
177
        self.contents = contents
 
 
180
        return "FileCreate(%i b)" % len(self.contents)
 
 
182
    def __eq__(self, other):
 
 
183
        if not isinstance(other, FileCreate):
 
 
185
        elif self.contents != other.contents:
 
 
190
    def __ne__(self, other):
 
 
191
        return not (self == other)
 
 
193
    def __call__(self, filename, conflict_handler, reverse):
 
 
194
        """Create or delete a file
 
 
196
        :param filename: The name of the file to create
 
 
198
        :param reverse: Delete the file instead of creating it
 
 
203
                file(filename, "wb").write(self.contents)
 
 
205
                if e.errno == errno.ENOENT:
 
 
206
                    if conflict_handler.missing_parent(filename)=="continue":
 
 
207
                        file(filename, "wb").write(self.contents)
 
 
213
                if (file(filename, "rb").read() != self.contents):
 
 
214
                    direction = conflict_handler.wrong_old_contents(filename,
 
 
216
                    if  direction != "continue":
 
 
220
                if e.errno != errno.ENOENT:
 
 
222
                if conflict_handler.missing_for_rm(filename, undo) == "skip":
 
 
227
def reversed(sequence):
 
 
228
    max = len(sequence) - 1
 
 
229
    for i in range(len(sequence)):
 
 
230
        yield sequence[max - i]
 
 
232
class ReplaceContents(object):
 
 
233
    """A contents-replacement framework.  It allows a file/directory/symlink to
 
 
234
    be created, deleted, or replaced with another file/directory/symlink.
 
 
235
    Arguments must be callable with (filename, reverse).
 
 
237
    def __init__(self, old_contents, new_contents):
 
 
240
        :param old_contents: The change to reverse apply (e.g. a deletion), \
 
 
242
        :type old_contents: `dir_create`, `SymlinkCreate`, `FileCreate`, \
 
 
244
        :param new_contents: The second change to apply (e.g. a creation), \
 
 
246
        :type new_contents: `dir_create`, `SymlinkCreate`, `FileCreate`, \
 
 
249
        self.old_contents=old_contents
 
 
250
        self.new_contents=new_contents
 
 
253
        return "ReplaceContents(%r -> %r)" % (self.old_contents,
 
 
256
    def __eq__(self, other):
 
 
257
        if not isinstance(other, ReplaceContents):
 
 
259
        elif self.old_contents != other.old_contents:
 
 
261
        elif self.new_contents != other.new_contents:
 
 
265
    def __ne__(self, other):
 
 
266
        return not (self == other)
 
 
268
    def apply(self, filename, conflict_handler, reverse=False):
 
 
269
        """Applies the FileReplacement to the specified filename
 
 
271
        :param filename: The name of the file to apply changes to
 
 
273
        :param reverse: If true, apply the change in reverse
 
 
277
            undo = self.old_contents
 
 
278
            perform = self.new_contents
 
 
280
            undo = self.new_contents
 
 
281
            perform = self.old_contents
 
 
285
                mode = os.lstat(filename).st_mode
 
 
286
                if stat.S_ISLNK(mode):
 
 
289
                if e.errno != errno.ENOENT:
 
 
291
                if conflict_handler.missing_for_rm(filename, undo) == "skip":
 
 
293
            undo(filename, conflict_handler, reverse=True)
 
 
294
        if perform is not None:
 
 
295
            perform(filename, conflict_handler, reverse=False)
 
 
297
                os.chmod(filename, mode)
 
 
299
class ApplySequence(object):
 
 
300
    def __init__(self, changes=None):
 
 
302
        if changes is not None:
 
 
303
            self.changes.extend(changes)
 
 
305
    def __eq__(self, other):
 
 
306
        if not isinstance(other, ApplySequence):
 
 
308
        elif len(other.changes) != len(self.changes):
 
 
311
            for i in range(len(self.changes)):
 
 
312
                if self.changes[i] != other.changes[i]:
 
 
316
    def __ne__(self, other):
 
 
317
        return not (self == other)
 
 
320
    def apply(self, filename, conflict_handler, reverse=False):
 
 
324
            iter = reversed(self.changes)
 
 
326
            change.apply(filename, conflict_handler, reverse)
 
 
329
class Diff3Merge(object):
 
 
330
    def __init__(self, file_id, base, other):
 
 
331
        self.file_id = file_id
 
 
335
    def __eq__(self, other):
 
 
336
        if not isinstance(other, Diff3Merge):
 
 
338
        return (self.base == other.base and 
 
 
339
                self.other == other.other and self.file_id == other.file_id)
 
 
341
    def __ne__(self, other):
 
 
342
        return not (self == other)
 
 
344
    def apply(self, filename, conflict_handler, reverse=False):
 
 
345
        new_file = filename+".new"
 
 
346
        base_file = self.base.readonly_path(self.file_id)
 
 
347
        other_file = self.other.readonly_path(self.file_id)
 
 
354
        status = patch.diff3(new_file, filename, base, other)
 
 
356
            os.chmod(new_file, os.stat(filename).st_mode)
 
 
357
            rename(new_file, filename)
 
 
361
            def get_lines(filename):
 
 
362
                my_file = file(base, "rb")
 
 
363
                lines = my_file.readlines()
 
 
365
            base_lines = get_lines(base)
 
 
366
            other_lines = get_lines(other)
 
 
367
            conflict_handler.merge_conflict(new_file, filename, base_lines, 
 
 
372
    """Convenience function to create a directory.
 
 
374
    :return: A ReplaceContents that will create a directory
 
 
375
    :rtype: `ReplaceContents`
 
 
377
    return ReplaceContents(None, dir_create)
 
 
380
    """Convenience function to delete a directory.
 
 
382
    :return: A ReplaceContents that will delete a directory
 
 
383
    :rtype: `ReplaceContents`
 
 
385
    return ReplaceContents(dir_create, None)
 
 
387
def CreateFile(contents):
 
 
388
    """Convenience fucntion to create a file.
 
 
390
    :param contents: The contents of the file to create 
 
 
392
    :return: A ReplaceContents that will create a file 
 
 
393
    :rtype: `ReplaceContents`
 
 
395
    return ReplaceContents(None, FileCreate(contents))
 
 
397
def DeleteFile(contents):
 
 
398
    """Convenience fucntion to delete a file.
 
 
400
    :param contents: The contents of the file to delete
 
 
402
    :return: A ReplaceContents that will delete a file 
 
 
403
    :rtype: `ReplaceContents`
 
 
405
    return ReplaceContents(FileCreate(contents), None)
 
 
407
def ReplaceFileContents(old_contents, new_contents):
 
 
408
    """Convenience fucntion to replace the contents of a file.
 
 
410
    :param old_contents: The contents of the file to replace 
 
 
411
    :type old_contents: str
 
 
412
    :param new_contents: The contents to replace the file with
 
 
413
    :type new_contents: str
 
 
414
    :return: A ReplaceContents that will replace the contents of a file a file 
 
 
415
    :rtype: `ReplaceContents`
 
 
417
    return ReplaceContents(FileCreate(old_contents), FileCreate(new_contents))
 
 
419
def CreateSymlink(target):
 
 
420
    """Convenience fucntion to create a symlink.
 
 
422
    :param target: The path the link should point to
 
 
424
    :return: A ReplaceContents that will delete a file 
 
 
425
    :rtype: `ReplaceContents`
 
 
427
    return ReplaceContents(None, SymlinkCreate(target))
 
 
429
def DeleteSymlink(target):
 
 
430
    """Convenience fucntion to delete a symlink.
 
 
432
    :param target: The path the link should point to
 
 
434
    :return: A ReplaceContents that will delete a file 
 
 
435
    :rtype: `ReplaceContents`
 
 
437
    return ReplaceContents(SymlinkCreate(target), None)
 
 
439
def ChangeTarget(old_target, new_target):
 
 
440
    """Convenience fucntion to change the target of a symlink.
 
 
442
    :param old_target: The current link target
 
 
443
    :type old_target: str
 
 
444
    :param new_target: The new link target to use
 
 
445
    :type new_target: str
 
 
446
    :return: A ReplaceContents that will delete a file 
 
 
447
    :rtype: `ReplaceContents`
 
 
449
    return ReplaceContents(SymlinkCreate(old_target), SymlinkCreate(new_target))
 
 
452
class InvalidEntry(Exception):
 
 
453
    """Raise when a ChangesetEntry is invalid in some way"""
 
 
454
    def __init__(self, entry, problem):
 
 
457
        :param entry: The invalid ChangesetEntry
 
 
458
        :type entry: `ChangesetEntry`
 
 
459
        :param problem: The problem with the entry
 
 
462
        msg = "Changeset entry for %s (%s) is invalid.\n%s" % (entry.id, 
 
 
465
        Exception.__init__(self, msg)
 
 
469
class SourceRootHasName(InvalidEntry):
 
 
470
    """This changeset entry has a name other than "", but its parent is !NULL"""
 
 
471
    def __init__(self, entry, name):
 
 
474
        :param entry: The invalid ChangesetEntry
 
 
475
        :type entry: `ChangesetEntry`
 
 
476
        :param name: The name of the entry
 
 
479
        msg = 'Child of !NULL is named "%s", not "./.".' % name
 
 
480
        InvalidEntry.__init__(self, entry, msg)
 
 
482
class NullIDAssigned(InvalidEntry):
 
 
483
    """The id !NULL was assigned to a real entry"""
 
 
484
    def __init__(self, entry):
 
 
487
        :param entry: The invalid ChangesetEntry
 
 
488
        :type entry: `ChangesetEntry`
 
 
490
        msg = '"!NULL" id assigned to a file "%s".' % entry.path
 
 
491
        InvalidEntry.__init__(self, entry, msg)
 
 
493
class ParentIDIsSelf(InvalidEntry):
 
 
494
    """An entry is marked as its own parent"""
 
 
495
    def __init__(self, entry):
 
 
498
        :param entry: The invalid ChangesetEntry
 
 
499
        :type entry: `ChangesetEntry`
 
 
501
        msg = 'file %s has "%s" id for both self id and parent id.' % \
 
 
502
            (entry.path, entry.id)
 
 
503
        InvalidEntry.__init__(self, entry, msg)
 
 
505
class ChangesetEntry(object):
 
 
506
    """An entry the changeset"""
 
 
507
    def __init__(self, id, parent, path):
 
 
508
        """Constructor. Sets parent and name assuming it was not
 
 
509
        renamed/created/deleted.
 
 
510
        :param id: The id associated with the entry
 
 
511
        :param parent: The id of the parent of this entry (or !NULL if no
 
 
513
        :param path: The file path relative to the tree root of this entry
 
 
519
        self.new_parent = parent
 
 
520
        self.contents_change = None
 
 
521
        self.metadata_change = None
 
 
522
        if parent == NULL_ID and path !='./.':
 
 
523
            raise SourceRootHasName(self, path)
 
 
524
        if self.id == NULL_ID:
 
 
525
            raise NullIDAssigned(self)
 
 
526
        if self.id  == self.parent:
 
 
527
            raise ParentIDIsSelf(self)
 
 
530
        return "ChangesetEntry(%s)" % self.id
 
 
533
        if self.path is None:
 
 
535
        return os.path.dirname(self.path)
 
 
537
    def __set_dir(self, dir):
 
 
538
        self.path = os.path.join(dir, os.path.basename(self.path))
 
 
540
    dir = property(__get_dir, __set_dir)
 
 
542
    def __get_name(self):
 
 
543
        if self.path is None:
 
 
545
        return os.path.basename(self.path)
 
 
547
    def __set_name(self, name):
 
 
548
        self.path = os.path.join(os.path.dirname(self.path), name)
 
 
550
    name = property(__get_name, __set_name)
 
 
552
    def __get_new_dir(self):
 
 
553
        if self.new_path is None:
 
 
555
        return os.path.dirname(self.new_path)
 
 
557
    def __set_new_dir(self, dir):
 
 
558
        self.new_path = os.path.join(dir, os.path.basename(self.new_path))
 
 
560
    new_dir = property(__get_new_dir, __set_new_dir)
 
 
562
    def __get_new_name(self):
 
 
563
        if self.new_path is None:
 
 
565
        return os.path.basename(self.new_path)
 
 
567
    def __set_new_name(self, name):
 
 
568
        self.new_path = os.path.join(os.path.dirname(self.new_path), name)
 
 
570
    new_name = property(__get_new_name, __set_new_name)
 
 
572
    def needs_rename(self):
 
 
573
        """Determines whether the entry requires renaming.
 
 
578
        return (self.parent != self.new_parent or self.name != self.new_name)
 
 
580
    def is_deletion(self, reverse):
 
 
581
        """Return true if applying the entry would delete a file/directory.
 
 
583
        :param reverse: if true, the changeset is being applied in reverse
 
 
586
        return ((self.new_parent is None and not reverse) or 
 
 
587
                (self.parent is None and reverse))
 
 
589
    def is_creation(self, reverse):
 
 
590
        """Return true if applying the entry would create a file/directory.
 
 
592
        :param reverse: if true, the changeset is being applied in reverse
 
 
595
        return ((self.parent is None and not reverse) or 
 
 
596
                (self.new_parent is None and reverse))
 
 
598
    def is_creation_or_deletion(self):
 
 
599
        """Return true if applying the entry would create or delete a 
 
 
604
        return self.parent is None or self.new_parent is None
 
 
606
    def get_cset_path(self, mod=False):
 
 
607
        """Determine the path of the entry according to the changeset.
 
 
609
        :param changeset: The changeset to derive the path from
 
 
610
        :type changeset: `Changeset`
 
 
611
        :param mod: If true, generate the MOD path.  Otherwise, generate the \
 
 
613
        :return: the path of the entry, or None if it did not exist in the \
 
 
615
        :rtype: str or NoneType
 
 
618
            if self.new_parent == NULL_ID:
 
 
620
            elif self.new_parent is None:
 
 
624
            if self.parent == NULL_ID:
 
 
626
            elif self.parent is None:
 
 
630
    def summarize_name(self, reverse=False):
 
 
631
        """Produce a one-line summary of the filename.  Indicates renames as
 
 
632
        old => new, indicates creation as None => new, indicates deletion as
 
 
635
        :param changeset: The changeset to get paths from
 
 
636
        :type changeset: `Changeset`
 
 
637
        :param reverse: If true, reverse the names in the output
 
 
641
        orig_path = self.get_cset_path(False)
 
 
642
        mod_path = self.get_cset_path(True)
 
 
643
        if orig_path is not None:
 
 
644
            orig_path = orig_path[2:]
 
 
645
        if mod_path is not None:
 
 
646
            mod_path = mod_path[2:]
 
 
647
        if orig_path == mod_path:
 
 
651
                return "%s => %s" % (orig_path, mod_path)
 
 
653
                return "%s => %s" % (mod_path, orig_path)
 
 
656
    def get_new_path(self, id_map, changeset, reverse=False):
 
 
657
        """Determine the full pathname to rename to
 
 
659
        :param id_map: The map of ids to filenames for the tree
 
 
660
        :type id_map: Dictionary
 
 
661
        :param changeset: The changeset to get data from
 
 
662
        :type changeset: `Changeset`
 
 
663
        :param reverse: If true, we're applying the changeset in reverse
 
 
667
        mutter("Finding new path for %s" % self.summarize_name())
 
 
671
            from_dir = self.new_dir
 
 
673
            from_name = self.new_name
 
 
675
            parent = self.new_parent
 
 
676
            to_dir = self.new_dir
 
 
678
            to_name = self.new_name
 
 
679
            from_name = self.name
 
 
684
        if parent == NULL_ID or parent is None:
 
 
686
                raise SourceRootHasName(self, to_name)
 
 
689
        if from_dir == to_dir:
 
 
690
            dir = os.path.dirname(id_map[self.id])
 
 
692
            mutter("path, new_path: %r %r" % (self.path, self.new_path))
 
 
693
            parent_entry = changeset.entries[parent]
 
 
694
            dir = parent_entry.get_new_path(id_map, changeset, reverse)
 
 
695
        if from_name == to_name:
 
 
696
            name = os.path.basename(id_map[self.id])
 
 
699
            assert(from_name is None or from_name == os.path.basename(id_map[self.id]))
 
 
700
        return os.path.join(dir, name)
 
 
703
        """Determines whether the entry does nothing
 
 
705
        :return: True if the entry does no renames or content changes
 
 
708
        if self.contents_change is not None:
 
 
710
        elif self.metadata_change is not None:
 
 
712
        elif self.parent != self.new_parent:
 
 
714
        elif self.name != self.new_name:
 
 
719
    def apply(self, filename, conflict_handler, reverse=False):
 
 
720
        """Applies the file content and/or metadata changes.
 
 
722
        :param filename: the filename of the entry
 
 
724
        :param reverse: If true, apply the changes in reverse
 
 
727
        if self.is_deletion(reverse) and self.metadata_change is not None:
 
 
728
            self.metadata_change.apply(filename, conflict_handler, reverse)
 
 
729
        if self.contents_change is not None:
 
 
730
            self.contents_change.apply(filename, conflict_handler, reverse)
 
 
731
        if not self.is_deletion(reverse) and self.metadata_change is not None:
 
 
732
            self.metadata_change.apply(filename, conflict_handler, reverse)
 
 
734
class IDPresent(Exception):
 
 
735
    def __init__(self, id):
 
 
736
        msg = "Cannot add entry because that id has already been used:\n%s" %\
 
 
738
        Exception.__init__(self, msg)
 
 
741
class Changeset(object):
 
 
742
    """A set of changes to apply"""
 
 
746
    def add_entry(self, entry):
 
 
747
        """Add an entry to the list of entries"""
 
 
748
        if self.entries.has_key(entry.id):
 
 
749
            raise IDPresent(entry.id)
 
 
750
        self.entries[entry.id] = entry
 
 
752
def my_sort(sequence, key, reverse=False):
 
 
753
    """A sort function that supports supplying a key for comparison
 
 
755
    :param sequence: The sequence to sort
 
 
756
    :param key: A callable object that returns the values to be compared
 
 
757
    :param reverse: If true, sort in reverse order
 
 
760
    def cmp_by_key(entry_a, entry_b):
 
 
765
        return cmp(key(entry_a), key(entry_b))
 
 
766
    sequence.sort(cmp_by_key)
 
 
768
def get_rename_entries(changeset, inventory, reverse):
 
 
769
    """Return a list of entries that will be renamed.  Entries are sorted from
 
 
770
    longest to shortest source path and from shortest to longest target path.
 
 
772
    :param changeset: The changeset to look in
 
 
773
    :type changeset: `Changeset`
 
 
774
    :param inventory: The source of current tree paths for the given ids
 
 
775
    :type inventory: Dictionary
 
 
776
    :param reverse: If true, the changeset is being applied in reverse
 
 
778
    :return: source entries and target entries as a tuple
 
 
781
    source_entries = [x for x in changeset.entries.itervalues() 
 
 
783
    # these are done from longest path to shortest, to avoid deleting a
 
 
784
    # parent before its children are deleted/renamed 
 
 
785
    def longest_to_shortest(entry):
 
 
786
        path = inventory.get(entry.id)
 
 
791
    my_sort(source_entries, longest_to_shortest, reverse=True)
 
 
793
    target_entries = source_entries[:]
 
 
794
    # These are done from shortest to longest path, to avoid creating a
 
 
795
    # child before its parent has been created/renamed
 
 
796
    def shortest_to_longest(entry):
 
 
797
        path = entry.get_new_path(inventory, changeset, reverse)
 
 
802
    my_sort(target_entries, shortest_to_longest)
 
 
803
    return (source_entries, target_entries)
 
 
805
def rename_to_temp_delete(source_entries, inventory, dir, temp_dir, 
 
 
806
                          conflict_handler, reverse):
 
 
807
    """Delete and rename entries as appropriate.  Entries are renamed to temp
 
 
808
    names.  A map of id -> temp name (or None, for deletions) is returned.
 
 
810
    :param source_entries: The entries to rename and delete
 
 
811
    :type source_entries: List of `ChangesetEntry`
 
 
812
    :param inventory: The map of id -> filename in the current tree
 
 
813
    :type inventory: Dictionary
 
 
814
    :param dir: The directory to apply changes to
 
 
816
    :param reverse: Apply changes in reverse
 
 
818
    :return: a mapping of id to temporary name
 
 
822
    for i in range(len(source_entries)):
 
 
823
        entry = source_entries[i]
 
 
824
        if entry.is_deletion(reverse):
 
 
825
            path = os.path.join(dir, inventory[entry.id])
 
 
826
            entry.apply(path, conflict_handler, reverse)
 
 
827
            temp_name[entry.id] = None
 
 
830
            to_name = os.path.join(temp_dir, str(i))
 
 
831
            src_path = inventory.get(entry.id)
 
 
832
            if src_path is not None:
 
 
833
                src_path = os.path.join(dir, src_path)
 
 
835
                    rename(src_path, to_name)
 
 
836
                    temp_name[entry.id] = to_name
 
 
838
                    if e.errno != errno.ENOENT:
 
 
840
                    if conflict_handler.missing_for_rename(src_path) == "skip":
 
 
846
def rename_to_new_create(changed_inventory, target_entries, inventory, 
 
 
847
                         changeset, dir, conflict_handler, reverse):
 
 
848
    """Rename entries with temp names to their final names, create new files.
 
 
850
    :param changed_inventory: A mapping of id to temporary name
 
 
851
    :type changed_inventory: Dictionary
 
 
852
    :param target_entries: The entries to apply changes to
 
 
853
    :type target_entries: List of `ChangesetEntry`
 
 
854
    :param changeset: The changeset to apply
 
 
855
    :type changeset: `Changeset`
 
 
856
    :param dir: The directory to apply changes to
 
 
858
    :param reverse: If true, apply changes in reverse
 
 
861
    for entry in target_entries:
 
 
862
        new_tree_path = entry.get_new_path(inventory, changeset, reverse)
 
 
863
        if new_tree_path is None:
 
 
865
        new_path = os.path.join(dir, new_tree_path)
 
 
866
        old_path = changed_inventory.get(entry.id)
 
 
867
        if os.path.exists(new_path):
 
 
868
            if conflict_handler.target_exists(entry, new_path, old_path) == \
 
 
871
        if entry.is_creation(reverse):
 
 
872
            entry.apply(new_path, conflict_handler, reverse)
 
 
873
            changed_inventory[entry.id] = new_tree_path
 
 
878
                rename(old_path, new_path)
 
 
879
                changed_inventory[entry.id] = new_tree_path
 
 
881
                raise Exception ("%s is missing" % new_path)
 
 
883
class TargetExists(Exception):
 
 
884
    def __init__(self, entry, target):
 
 
885
        msg = "The path %s already exists" % target
 
 
886
        Exception.__init__(self, msg)
 
 
890
class RenameConflict(Exception):
 
 
891
    def __init__(self, id, this_name, base_name, other_name):
 
 
892
        msg = """Trees all have different names for a file
 
 
896
   id: %s""" % (this_name, base_name, other_name, id)
 
 
897
        Exception.__init__(self, msg)
 
 
898
        self.this_name = this_name
 
 
899
        self.base_name = base_name
 
 
900
        self_other_name = other_name
 
 
902
class MoveConflict(Exception):
 
 
903
    def __init__(self, id, this_parent, base_parent, other_parent):
 
 
904
        msg = """The file is in different directories in every tree
 
 
908
   id: %s""" % (this_parent, base_parent, other_parent, id)
 
 
909
        Exception.__init__(self, msg)
 
 
910
        self.this_parent = this_parent
 
 
911
        self.base_parent = base_parent
 
 
912
        self_other_parent = other_parent
 
 
914
class MergeConflict(Exception):
 
 
915
    def __init__(self, this_path):
 
 
916
        Exception.__init__(self, "Conflict applying changes to %s" % this_path)
 
 
917
        self.this_path = this_path
 
 
919
class MergePermissionConflict(Exception):
 
 
920
    def __init__(self, this_path, base_path, other_path):
 
 
921
        this_perms = os.stat(this_path).st_mode & 0755
 
 
922
        base_perms = os.stat(base_path).st_mode & 0755
 
 
923
        other_perms = os.stat(other_path).st_mode & 0755
 
 
924
        msg = """Conflicting permission for %s
 
 
928
        """ % (this_path, this_perms, base_perms, other_perms)
 
 
929
        self.this_path = this_path
 
 
930
        self.base_path = base_path
 
 
931
        self.other_path = other_path
 
 
932
        Exception.__init__(self, msg)
 
 
934
class WrongOldContents(Exception):
 
 
935
    def __init__(self, filename):
 
 
936
        msg = "Contents mismatch deleting %s" % filename
 
 
937
        self.filename = filename
 
 
938
        Exception.__init__(self, msg)
 
 
940
class WrongOldPermissions(Exception):
 
 
941
    def __init__(self, filename, old_perms, new_perms):
 
 
942
        msg = "Permission missmatch on %s:\n" \
 
 
943
        "Expected 0%o, got 0%o." % (filename, old_perms, new_perms)
 
 
944
        self.filename = filename
 
 
945
        Exception.__init__(self, msg)
 
 
947
class RemoveContentsConflict(Exception):
 
 
948
    def __init__(self, filename):
 
 
949
        msg = "Conflict deleting %s, which has different contents in BASE"\
 
 
950
            " and THIS" % filename
 
 
951
        self.filename = filename
 
 
952
        Exception.__init__(self, msg)
 
 
954
class DeletingNonEmptyDirectory(Exception):
 
 
955
    def __init__(self, filename):
 
 
956
        msg = "Trying to remove dir %s while it still had files" % filename
 
 
957
        self.filename = filename
 
 
958
        Exception.__init__(self, msg)
 
 
961
class PatchTargetMissing(Exception):
 
 
962
    def __init__(self, filename):
 
 
963
        msg = "Attempt to patch %s, which does not exist" % filename
 
 
964
        Exception.__init__(self, msg)
 
 
965
        self.filename = filename
 
 
967
class MissingPermsFile(Exception):
 
 
968
    def __init__(self, filename):
 
 
969
        msg = "Attempt to change permissions on  %s, which does not exist" %\
 
 
971
        Exception.__init__(self, msg)
 
 
972
        self.filename = filename
 
 
974
class MissingForRm(Exception):
 
 
975
    def __init__(self, filename):
 
 
976
        msg = "Attempt to remove missing path %s" % filename
 
 
977
        Exception.__init__(self, msg)
 
 
978
        self.filename = filename
 
 
981
class MissingForRename(Exception):
 
 
982
    def __init__(self, filename):
 
 
983
        msg = "Attempt to move missing path %s" % (filename)
 
 
984
        Exception.__init__(self, msg)
 
 
985
        self.filename = filename
 
 
987
class NewContentsConflict(Exception):
 
 
988
    def __init__(self, filename):
 
 
989
        msg = "Conflicting contents for new file %s" % (filename)
 
 
990
        Exception.__init__(self, msg)
 
 
993
class MissingForMerge(Exception):
 
 
994
    def __init__(self, filename):
 
 
995
        msg = "The file %s was modified, but does not exist in this tree"\
 
 
997
        Exception.__init__(self, msg)
 
 
1000
class ExceptionConflictHandler(object):
 
 
1001
    """Default handler for merge exceptions.
 
 
1003
    This throws an error on any kind of conflict.  Conflict handlers can
 
 
1004
    descend from this class if they have a better way to handle some or
 
 
1005
    all types of conflict.
 
 
1007
    def missing_parent(self, pathname):
 
 
1008
        parent = os.path.dirname(pathname)
 
 
1009
        raise Exception("Parent directory missing for %s" % pathname)
 
 
1011
    def dir_exists(self, pathname):
 
 
1012
        raise Exception("Directory already exists for %s" % pathname)
 
 
1014
    def failed_hunks(self, pathname):
 
 
1015
        raise Exception("Failed to apply some hunks for %s" % pathname)
 
 
1017
    def target_exists(self, entry, target, old_path):
 
 
1018
        raise TargetExists(entry, target)
 
 
1020
    def rename_conflict(self, id, this_name, base_name, other_name):
 
 
1021
        raise RenameConflict(id, this_name, base_name, other_name)
 
 
1023
    def move_conflict(self, id, this_dir, base_dir, other_dir):
 
 
1024
        raise MoveConflict(id, this_dir, base_dir, other_dir)
 
 
1026
    def merge_conflict(self, new_file, this_path, base_lines, other_lines):
 
 
1028
        raise MergeConflict(this_path)
 
 
1030
    def permission_conflict(self, this_path, base_path, other_path):
 
 
1031
        raise MergePermissionConflict(this_path, base_path, other_path)
 
 
1033
    def wrong_old_contents(self, filename, expected_contents):
 
 
1034
        raise WrongOldContents(filename)
 
 
1036
    def rem_contents_conflict(self, filename, this_contents, base_contents):
 
 
1037
        raise RemoveContentsConflict(filename)
 
 
1039
    def wrong_old_perms(self, filename, old_perms, new_perms):
 
 
1040
        raise WrongOldPermissions(filename, old_perms, new_perms)
 
 
1042
    def rmdir_non_empty(self, filename):
 
 
1043
        raise DeletingNonEmptyDirectory(filename)
 
 
1045
    def link_name_exists(self, filename):
 
 
1046
        raise TargetExists(filename)
 
 
1048
    def patch_target_missing(self, filename, contents):
 
 
1049
        raise PatchTargetMissing(filename)
 
 
1051
    def missing_for_chmod(self, filename):
 
 
1052
        raise MissingPermsFile(filename)
 
 
1054
    def missing_for_rm(self, filename, change):
 
 
1055
        raise MissingForRm(filename)
 
 
1057
    def missing_for_rename(self, filename):
 
 
1058
        raise MissingForRename(filename)
 
 
1060
    def missing_for_merge(self, file_id, other_path):
 
 
1061
        raise MissingForMerge(other_path)
 
 
1063
    def new_contents_conflict(self, filename, other_contents):
 
 
1064
        raise NewContentsConflict(filename)
 
 
1069
def apply_changeset(changeset, inventory, dir, conflict_handler=None, 
 
 
1071
    """Apply a changeset to a directory.
 
 
1073
    :param changeset: The changes to perform
 
 
1074
    :type changeset: `Changeset`
 
 
1075
    :param inventory: The mapping of id to filename for the directory
 
 
1076
    :type inventory: Dictionary
 
 
1077
    :param dir: The path of the directory to apply the changes to
 
 
1079
    :param reverse: If true, apply the changes in reverse
 
 
1081
    :return: The mapping of the changed entries
 
 
1084
    if conflict_handler is None:
 
 
1085
        conflict_handler = ExceptionConflictHandler()
 
 
1086
    temp_dir = os.path.join(dir, "bzr-tree-change")
 
 
1090
        if e.errno == errno.EEXIST:
 
 
1094
                if e.errno == errno.ENOTEMPTY:
 
 
1095
                    raise OldFailedTreeOp()
 
 
1100
    #apply changes that don't affect filenames
 
 
1101
    for entry in changeset.entries.itervalues():
 
 
1102
        if not entry.is_creation_or_deletion():
 
 
1103
            path = os.path.join(dir, inventory[entry.id])
 
 
1104
            entry.apply(path, conflict_handler, reverse)
 
 
1106
    # Apply renames in stages, to minimize conflicts:
 
 
1107
    # Only files whose name or parent change are interesting, because their
 
 
1108
    # target name may exist in the source tree.  If a directory's name changes,
 
 
1109
    # that doesn't make its children interesting.
 
 
1110
    (source_entries, target_entries) = get_rename_entries(changeset, inventory,
 
 
1113
    changed_inventory = rename_to_temp_delete(source_entries, inventory, dir,
 
 
1114
                                              temp_dir, conflict_handler,
 
 
1117
    rename_to_new_create(changed_inventory, target_entries, inventory,
 
 
1118
                         changeset, dir, conflict_handler, reverse)
 
 
1120
    return changed_inventory
 
 
1123
def apply_changeset_tree(cset, tree, reverse=False):
 
 
1125
    for entry in tree.source_inventory().itervalues():
 
 
1126
        inventory[entry.id] = entry.path
 
 
1127
    new_inventory = apply_changeset(cset, r_inventory, tree.root,
 
 
1129
    new_entries, remove_entries = \
 
 
1130
        get_inventory_change(inventory, new_inventory, cset, reverse)
 
 
1131
    tree.update_source_inventory(new_entries, remove_entries)
 
 
1134
def get_inventory_change(inventory, new_inventory, cset, reverse=False):
 
 
1137
    for entry in cset.entries.itervalues():
 
 
1138
        if entry.needs_rename():
 
 
1139
            new_path = entry.get_new_path(inventory, cset)
 
 
1140
            if new_path is None:
 
 
1141
                remove_entries.append(entry.id)
 
 
1143
                new_entries[new_path] = entry.id
 
 
1144
    return new_entries, remove_entries
 
 
1147
def print_changeset(cset):
 
 
1148
    """Print all non-boring changeset entries
 
 
1150
    :param cset: The changeset to print
 
 
1151
    :type cset: `Changeset`
 
 
1153
    for entry in cset.entries.itervalues():
 
 
1154
        if entry.is_boring():
 
 
1157
        print entry.summarize_name(cset)
 
 
1159
class CompositionFailure(Exception):
 
 
1160
    def __init__(self, old_entry, new_entry, problem):
 
 
1161
        msg = "Unable to conpose entries.\n %s" % problem
 
 
1162
        Exception.__init__(self, msg)
 
 
1164
class IDMismatch(CompositionFailure):
 
 
1165
    def __init__(self, old_entry, new_entry):
 
 
1166
        problem = "Attempt to compose entries with different ids: %s and %s" %\
 
 
1167
            (old_entry.id, new_entry.id)
 
 
1168
        CompositionFailure.__init__(self, old_entry, new_entry, problem)
 
 
1170
def compose_changesets(old_cset, new_cset):
 
 
1171
    """Combine two changesets into one.  This works well for exact patching.
 
 
1172
    Otherwise, not so well.
 
 
1174
    :param old_cset: The first changeset that would be applied
 
 
1175
    :type old_cset: `Changeset`
 
 
1176
    :param new_cset: The second changeset that would be applied
 
 
1177
    :type new_cset: `Changeset`
 
 
1178
    :return: A changeset that combines the changes in both changesets
 
 
1181
    composed = Changeset()
 
 
1182
    for old_entry in old_cset.entries.itervalues():
 
 
1183
        new_entry = new_cset.entries.get(old_entry.id)
 
 
1184
        if new_entry is None:
 
 
1185
            composed.add_entry(old_entry)
 
 
1187
            composed_entry = compose_entries(old_entry, new_entry)
 
 
1188
            if composed_entry.parent is not None or\
 
 
1189
                composed_entry.new_parent is not None:
 
 
1190
                composed.add_entry(composed_entry)
 
 
1191
    for new_entry in new_cset.entries.itervalues():
 
 
1192
        if not old_cset.entries.has_key(new_entry.id):
 
 
1193
            composed.add_entry(new_entry)
 
 
1196
def compose_entries(old_entry, new_entry):
 
 
1197
    """Combine two entries into one.
 
 
1199
    :param old_entry: The first entry that would be applied
 
 
1200
    :type old_entry: ChangesetEntry
 
 
1201
    :param old_entry: The second entry that would be applied
 
 
1202
    :type old_entry: ChangesetEntry
 
 
1203
    :return: A changeset entry combining both entries
 
 
1204
    :rtype: `ChangesetEntry`
 
 
1206
    if old_entry.id != new_entry.id:
 
 
1207
        raise IDMismatch(old_entry, new_entry)
 
 
1208
    output = ChangesetEntry(old_entry.id, old_entry.parent, old_entry.path)
 
 
1210
    if (old_entry.parent != old_entry.new_parent or 
 
 
1211
        new_entry.parent != new_entry.new_parent):
 
 
1212
        output.new_parent = new_entry.new_parent
 
 
1214
    if (old_entry.path != old_entry.new_path or 
 
 
1215
        new_entry.path != new_entry.new_path):
 
 
1216
        output.new_path = new_entry.new_path
 
 
1218
    output.contents_change = compose_contents(old_entry, new_entry)
 
 
1219
    output.metadata_change = compose_metadata(old_entry, new_entry)
 
 
1222
def compose_contents(old_entry, new_entry):
 
 
1223
    """Combine the contents of two changeset entries.  Entries are combined
 
 
1224
    intelligently where possible, but the fallback behavior returns an 
 
 
1227
    :param old_entry: The first entry that would be applied
 
 
1228
    :type old_entry: `ChangesetEntry`
 
 
1229
    :param new_entry: The second entry that would be applied
 
 
1230
    :type new_entry: `ChangesetEntry`
 
 
1231
    :return: A combined contents change
 
 
1232
    :rtype: anything supporting the apply(reverse=False) method
 
 
1234
    old_contents = old_entry.contents_change
 
 
1235
    new_contents = new_entry.contents_change
 
 
1236
    if old_entry.contents_change is None:
 
 
1237
        return new_entry.contents_change
 
 
1238
    elif new_entry.contents_change is None:
 
 
1239
        return old_entry.contents_change
 
 
1240
    elif isinstance(old_contents, ReplaceContents) and \
 
 
1241
        isinstance(new_contents, ReplaceContents):
 
 
1242
        if old_contents.old_contents == new_contents.new_contents:
 
 
1245
            return ReplaceContents(old_contents.old_contents,
 
 
1246
                                   new_contents.new_contents)
 
 
1247
    elif isinstance(old_contents, ApplySequence):
 
 
1248
        output = ApplySequence(old_contents.changes)
 
 
1249
        if isinstance(new_contents, ApplySequence):
 
 
1250
            output.changes.extend(new_contents.changes)
 
 
1252
            output.changes.append(new_contents)
 
 
1254
    elif isinstance(new_contents, ApplySequence):
 
 
1255
        output = ApplySequence((old_contents.changes,))
 
 
1256
        output.extend(new_contents.changes)
 
 
1259
        return ApplySequence((old_contents, new_contents))
 
 
1261
def compose_metadata(old_entry, new_entry):
 
 
1262
    old_meta = old_entry.metadata_change
 
 
1263
    new_meta = new_entry.metadata_change
 
 
1264
    if old_meta is None:
 
 
1266
    elif new_meta is None:
 
 
1268
    elif isinstance(old_meta, ChangeUnixPermissions) and \
 
 
1269
        isinstance(new_meta, ChangeUnixPermissions):
 
 
1270
        return ChangeUnixPermissions(old_meta.old_mode, new_meta.new_mode)
 
 
1272
        return ApplySequence(old_meta, new_meta)
 
 
1275
def changeset_is_null(changeset):
 
 
1276
    for entry in changeset.entries.itervalues():
 
 
1277
        if not entry.is_boring():
 
 
1281
class UnsuppportedFiletype(Exception):
 
 
1282
    def __init__(self, full_path, stat_result):
 
 
1283
        msg = "The file \"%s\" is not a supported filetype." % full_path
 
 
1284
        Exception.__init__(self, msg)
 
 
1285
        self.full_path = full_path
 
 
1286
        self.stat_result = stat_result
 
 
1288
def generate_changeset(tree_a, tree_b, interesting_ids=None):
 
 
1289
    return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
 
 
1291
class ChangesetGenerator(object):
 
 
1292
    def __init__(self, tree_a, tree_b, interesting_ids=None):
 
 
1293
        object.__init__(self)
 
 
1294
        self.tree_a = tree_a
 
 
1295
        self.tree_b = tree_b
 
 
1296
        self._interesting_ids = interesting_ids
 
 
1298
    def iter_both_tree_ids(self):
 
 
1299
        for file_id in self.tree_a:
 
 
1301
        for file_id in self.tree_b:
 
 
1302
            if file_id not in self.tree_a:
 
 
1307
        for file_id in self.iter_both_tree_ids():
 
 
1308
            cs_entry = self.make_entry(file_id)
 
 
1309
            if cs_entry is not None and not cs_entry.is_boring():
 
 
1310
                cset.add_entry(cs_entry)
 
 
1312
        for entry in list(cset.entries.itervalues()):
 
 
1313
            if entry.parent != entry.new_parent:
 
 
1314
                if not cset.entries.has_key(entry.parent) and\
 
 
1315
                    entry.parent != NULL_ID and entry.parent is not None:
 
 
1316
                    parent_entry = self.make_boring_entry(entry.parent)
 
 
1317
                    cset.add_entry(parent_entry)
 
 
1318
                if not cset.entries.has_key(entry.new_parent) and\
 
 
1319
                    entry.new_parent != NULL_ID and \
 
 
1320
                    entry.new_parent is not None:
 
 
1321
                    parent_entry = self.make_boring_entry(entry.new_parent)
 
 
1322
                    cset.add_entry(parent_entry)
 
 
1325
    def iter_inventory(self, tree):
 
 
1326
        for file_id in tree:
 
 
1327
            yield self.get_entry(file_id, tree)
 
 
1329
    def get_entry(self, file_id, tree):
 
 
1330
        if not tree.has_or_had_id(file_id):
 
 
1332
        return tree.tree.inventory[file_id]
 
 
1334
    def get_entry_parent(self, entry):
 
 
1337
        return entry.parent_id
 
 
1339
    def get_path(self, file_id, tree):
 
 
1340
        if not tree.has_or_had_id(file_id):
 
 
1342
        path = tree.id2path(file_id)
 
 
1348
    def make_basic_entry(self, file_id, only_interesting):
 
 
1349
        entry_a = self.get_entry(file_id, self.tree_a)
 
 
1350
        entry_b = self.get_entry(file_id, self.tree_b)
 
 
1351
        if only_interesting and not self.is_interesting(entry_a, entry_b):
 
 
1353
        parent = self.get_entry_parent(entry_a)
 
 
1354
        path = self.get_path(file_id, self.tree_a)
 
 
1355
        cs_entry = ChangesetEntry(file_id, parent, path)
 
 
1356
        new_parent = self.get_entry_parent(entry_b)
 
 
1358
        new_path = self.get_path(file_id, self.tree_b)
 
 
1360
        cs_entry.new_path = new_path
 
 
1361
        cs_entry.new_parent = new_parent
 
 
1364
    def is_interesting(self, entry_a, entry_b):
 
 
1365
        if self._interesting_ids is None:
 
 
1367
        if entry_a is not None:
 
 
1368
            file_id = entry_a.file_id
 
 
1369
        elif entry_b is not None:
 
 
1370
            file_id = entry_b.file_id
 
 
1373
        return file_id in self._interesting_ids
 
 
1375
    def make_boring_entry(self, id):
 
 
1376
        cs_entry = self.make_basic_entry(id, only_interesting=False)
 
 
1377
        if cs_entry.is_creation_or_deletion():
 
 
1378
            return self.make_entry(id, only_interesting=False)
 
 
1383
    def make_entry(self, id, only_interesting=True):
 
 
1384
        cs_entry = self.make_basic_entry(id, only_interesting)
 
 
1386
        if cs_entry is None:
 
 
1388
        if id in self.tree_a and id in self.tree_b:
 
 
1389
            a_sha1 = self.tree_a.get_file_sha1(id)
 
 
1390
            b_sha1 = self.tree_b.get_file_sha1(id)
 
 
1391
            if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
 
 
1394
        full_path_a = self.tree_a.readonly_path(id)
 
 
1395
        full_path_b = self.tree_b.readonly_path(id)
 
 
1396
        stat_a = self.lstat(full_path_a)
 
 
1397
        stat_b = self.lstat(full_path_b)
 
 
1399
        cs_entry.metadata_change = self.make_mode_change(stat_a, stat_b)
 
 
1400
        cs_entry.contents_change = self.make_contents_change(full_path_a,
 
 
1406
    def make_mode_change(self, stat_a, stat_b):
 
 
1408
        if stat_a is not None and not stat.S_ISLNK(stat_a.st_mode):
 
 
1409
            mode_a = stat_a.st_mode & 0777
 
 
1411
        if stat_b is not None and not stat.S_ISLNK(stat_b.st_mode):
 
 
1412
            mode_b = stat_b.st_mode & 0777
 
 
1413
        if mode_a == mode_b:
 
 
1415
        return ChangeUnixPermissions(mode_a, mode_b)
 
 
1417
    def make_contents_change(self, full_path_a, stat_a, full_path_b, stat_b):
 
 
1418
        if stat_a is None and stat_b is None:
 
 
1420
        if None not in (stat_a, stat_b) and stat.S_ISDIR(stat_a.st_mode) and\
 
 
1421
            stat.S_ISDIR(stat_b.st_mode):
 
 
1423
        if None not in (stat_a, stat_b) and stat.S_ISREG(stat_a.st_mode) and\
 
 
1424
            stat.S_ISREG(stat_b.st_mode):
 
 
1425
            if stat_a.st_ino == stat_b.st_ino and \
 
 
1426
                stat_a.st_dev == stat_b.st_dev:
 
 
1429
        a_contents = self.get_contents(stat_a, full_path_a)
 
 
1430
        b_contents = self.get_contents(stat_b, full_path_b)
 
 
1431
        if a_contents == b_contents:
 
 
1433
        return ReplaceContents(a_contents, b_contents)
 
 
1435
    def get_contents(self, stat_result, full_path):
 
 
1436
        if stat_result is None:
 
 
1438
        elif stat.S_ISREG(stat_result.st_mode):
 
 
1439
            return FileCreate(file(full_path, "rb").read())
 
 
1440
        elif stat.S_ISDIR(stat_result.st_mode):
 
 
1442
        elif stat.S_ISLNK(stat_result.st_mode):
 
 
1443
            return SymlinkCreate(os.readlink(full_path))
 
 
1445
            raise UnsupportedFiletype(full_path, stat_result)
 
 
1447
    def lstat(self, full_path):
 
 
1449
        if full_path is not None:
 
 
1451
                stat_result = os.lstat(full_path)
 
 
1453
                if e.errno != errno.ENOENT:
 
 
1458
def full_path(entry, tree):
 
 
1459
    return os.path.join(tree.root, entry.path)
 
 
1461
def new_delete_entry(entry, tree, inventory, delete):
 
 
1462
    if entry.path == "":
 
 
1465
        parent = inventory[dirname(entry.path)].id
 
 
1466
    cs_entry = ChangesetEntry(parent, entry.path)
 
 
1468
        cs_entry.new_path = None
 
 
1469
        cs_entry.new_parent = None
 
 
1471
        cs_entry.path = None
 
 
1472
        cs_entry.parent = None
 
 
1473
    full_path = full_path(entry, tree)
 
 
1474
    status = os.lstat(full_path)
 
 
1475
    if stat.S_ISDIR(file_stat.st_mode):
 
 
1481
# XXX: Can't we unify this with the regular inventory object
 
 
1482
class Inventory(object):
 
 
1483
    def __init__(self, inventory):
 
 
1484
        self.inventory = inventory
 
 
1485
        self.rinventory = None
 
 
1487
    def get_rinventory(self):
 
 
1488
        if self.rinventory is None:
 
 
1489
            self.rinventory  = invert_dict(self.inventory)
 
 
1490
        return self.rinventory
 
 
1492
    def get_path(self, id):
 
 
1493
        return self.inventory.get(id)
 
 
1495
    def get_name(self, id):
 
 
1496
        path = self.get_path(id)
 
 
1500
            return os.path.basename(path)
 
 
1502
    def get_dir(self, id):
 
 
1503
        path = self.get_path(id)
 
 
1508
        return os.path.dirname(path)
 
 
1510
    def get_parent(self, id):
 
 
1511
        if self.get_path(id) is None:
 
 
1513
        directory = self.get_dir(id)
 
 
1514
        if directory == '.':
 
 
1516
        if directory is None:
 
 
1518
        return self.get_rinventory().get(directory)