~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

  • Committer: Vincent Ladeuil
  • Date: 2011-06-15 11:36:05 UTC
  • mto: This revision was merged to the branch mainline in revision 5975.
  • Revision ID: v.ladeuil+lp@free.fr-20110615113605-p7zyyfry9wy1hquc
Make ContentConflict resolution more robust

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
    versionedfile,
39
39
    workingtree,
40
40
    )
41
 
from bzrlib.i18n import gettext
42
41
""")
43
42
from bzrlib import (
44
43
    decorators,
712
711
        merge = operation.run_simple()
713
712
        if len(merge.cooked_conflicts) == 0:
714
713
            if not self.ignore_zero and not trace.is_quiet():
715
 
                trace.note(gettext("All changes applied successfully."))
 
714
                trace.note("All changes applied successfully.")
716
715
        else:
717
 
            trace.note(gettext("%d conflicts encountered.")
 
716
            trace.note("%d conflicts encountered."
718
717
                       % len(merge.cooked_conflicts))
719
718
 
720
719
        return len(merge.cooked_conflicts)
859
858
            self.active_hooks = [hook for hook in hooks if hook is not None]
860
859
            for num, (file_id, changed, parents3, names3,
861
860
                      executable3) in enumerate(entries):
862
 
                child_pb.update(gettext('Preparing file merge'), num, len(entries))
 
861
                child_pb.update('Preparing file merge', num, len(entries))
863
862
                self._merge_names(file_id, parents3, names3, resolver=resolver)
864
863
                if changed:
865
864
                    file_status = self._do_merge_contents(file_id)
991
990
                else:
992
991
                    lca_entries.append(lca_ie)
993
992
 
994
 
            if base_inventory.has_id(file_id):
 
993
            if file_id in base_inventory:
995
994
                base_ie = base_inventory[file_id]
996
995
            else:
997
996
                base_ie = _none_entry
998
997
 
999
 
            if this_inventory.has_id(file_id):
 
998
            if file_id in this_inventory:
1000
999
                this_ie = this_inventory[file_id]
1001
1000
            else:
1002
1001
                this_ie = _none_entry
1107
1106
        other_root = self.tt.trans_id_file_id(other_root_file_id)
1108
1107
        if other_root == self.tt.root:
1109
1108
            return
1110
 
        if self.this_tree.inventory.has_id(
1111
 
            self.other_tree.inventory.root.file_id):
1112
 
            # the other tree's root is a non-root in the current tree (as
1113
 
            # when a previously unrelated branch is merged into another)
 
1109
        if self.other_tree.inventory.root.file_id in self.this_tree.inventory:
 
1110
            # the other tree's root is a non-root in the current tree (as when
 
1111
            # a previously unrelated branch is merged into another)
1114
1112
            return
1115
1113
        if self.tt.final_kind(other_root) is not None:
1116
1114
            other_root_is_present = True
1168
1166
    @staticmethod
1169
1167
    def contents_sha1(tree, file_id):
1170
1168
        """Determine the sha1 of the file contents (used as a key method)."""
1171
 
        if not tree.has_id(file_id):
 
1169
        if file_id not in tree:
1172
1170
            return None
1173
1171
        return tree.get_file_sha1(file_id)
1174
1172
 
1344
1342
    def _do_merge_contents(self, file_id):
1345
1343
        """Performs a merge on file_id contents."""
1346
1344
        def contents_pair(tree):
1347
 
            if not tree.has_id(file_id):
 
1345
            if file_id not in tree:
1348
1346
                return (None, None)
1349
1347
            kind = tree.kind(file_id)
1350
1348
            if kind == "file":
1618
1616
 
1619
1617
    def cook_conflicts(self, fs_conflicts):
1620
1618
        """Convert all conflicts into a form that doesn't depend on trans_id"""
1621
 
        content_conflict_file_ids = set()
1622
 
        cooked_conflicts = transform.cook_conflicts(fs_conflicts, self.tt)
 
1619
        self.cooked_conflicts.extend(transform.cook_conflicts(
 
1620
                fs_conflicts, self.tt))
1623
1621
        fp = transform.FinalPaths(self.tt)
1624
1622
        for conflict in self._raw_conflicts:
1625
1623
            conflict_type = conflict[0]
1636
1634
                if other_parent is None or other_name is None:
1637
1635
                    other_path = '<deleted>'
1638
1636
                else:
1639
 
                    if other_parent == self.other_tree.get_root_id():
1640
 
                        # The tree transform doesn't know about the other root,
1641
 
                        # so we special case here to avoid a NoFinalPath
1642
 
                        # exception
1643
 
                        parent_path = ''
1644
 
                    else:
1645
 
                        parent_path =  fp.get_path(
1646
 
                            self.tt.trans_id_file_id(other_parent))
 
1637
                    parent_path =  fp.get_path(
 
1638
                        self.tt.trans_id_file_id(other_parent))
1647
1639
                    other_path = osutils.pathjoin(parent_path, other_name)
1648
1640
                c = _mod_conflicts.Conflict.factory(
1649
1641
                    'path conflict', path=this_path,
1661
1653
                        break
1662
1654
                c = _mod_conflicts.Conflict.factory(conflict_type,
1663
1655
                                                    path=path, file_id=file_id)
1664
 
                content_conflict_file_ids.add(file_id)
1665
1656
            elif conflict_type == 'text conflict':
1666
1657
                trans_id = conflict[1]
1667
1658
                path = fp.get_path(trans_id)
1670
1661
                                                    path=path, file_id=file_id)
1671
1662
            else:
1672
1663
                raise AssertionError('bad conflict type: %r' % (conflict,))
1673
 
            cooked_conflicts.append(c)
1674
 
 
1675
 
        self.cooked_conflicts = []
1676
 
        # We want to get rid of path conflicts when a corresponding contents
1677
 
        # conflict exists. This can occur when one branch deletes a file while
1678
 
        # the other renames *and* modifies it. In this case, the content
1679
 
        # conflict is enough.
1680
 
        for c in cooked_conflicts:
1681
 
            if (c.typestring == 'path conflict'
1682
 
                and c.file_id in content_conflict_file_ids):
1683
 
                continue
1684
1664
            self.cooked_conflicts.append(c)
1685
1665
        self.cooked_conflicts.sort(key=_mod_conflicts.Conflict.sort_key)
1686
1666
 
1907
1887
            entries = self._entries_to_incorporate()
1908
1888
            entries = list(entries)
1909
1889
            for num, (entry, parent_id) in enumerate(entries):
1910
 
                child_pb.update(gettext('Preparing file merge'), num, len(entries))
 
1890
                child_pb.update('Preparing file merge', num, len(entries))
1911
1891
                parent_trans_id = self.tt.trans_id_file_id(parent_id)
1912
1892
                trans_id = transform.new_by_entry(self.tt, entry,
1913
1893
                    parent_trans_id, self.other_tree)
1931
1911
        name_in_target = osutils.basename(self._target_subdir)
1932
1912
        merge_into_root = subdir.copy()
1933
1913
        merge_into_root.name = name_in_target
1934
 
        if self.this_tree.inventory.has_id(merge_into_root.file_id):
 
1914
        if merge_into_root.file_id in self.this_tree.inventory:
1935
1915
            # Give the root a new file-id.
1936
1916
            # This can happen fairly easily if the directory we are
1937
1917
            # incorporating is the root, and both trees have 'TREE_ROOT' as
1974
1954
    """
1975
1955
    if this_tree is None:
1976
1956
        raise errors.BzrError("bzrlib.merge.merge_inner requires a this_tree "
1977
 
                              "parameter")
 
1957
                              "parameter as of bzrlib version 0.8.")
1978
1958
    merger = Merger(this_branch, other_tree, base_tree, this_tree=this_tree,
1979
1959
                    pb=pb, change_reporter=change_reporter)
1980
1960
    merger.backup_files = backup_files