~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

  • Committer: Martin Packman
  • Date: 2012-01-05 10:37:58 UTC
  • mto: This revision was merged to the branch mainline in revision 6427.
  • Revision ID: martin.packman@canonical.com-20120105103758-wzftnmsip5iv9n2g
Revert addition of get_message_encoding function

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
41
42
""")
42
43
from bzrlib import (
43
44
    decorators,
44
45
    errors,
45
46
    hooks,
 
47
    registry,
46
48
    )
47
49
from bzrlib.symbol_versioning import (
48
50
    deprecated_in,
138
140
            params.winner == 'other' or
139
141
            # THIS and OTHER aren't both files.
140
142
            not params.is_file_merge() or
141
 
            # The filename doesn't match *.xml
 
143
            # The filename doesn't match
142
144
            not self.file_matches(params)):
143
145
            return 'not_applicable', None
144
146
        return self.merge_matching(params)
591
593
                else:
592
594
                    self.base_rev_id = self.revision_graph.find_unique_lca(
593
595
                                            *lcas)
594
 
                sorted_lca_keys = self.revision_graph.find_merge_order(                
 
596
                sorted_lca_keys = self.revision_graph.find_merge_order(
595
597
                    revisions[0], lcas)
596
598
                if self.base_rev_id == _mod_revision.NULL_REVISION:
597
599
                    self.base_rev_id = sorted_lca_keys[0]
598
 
                
 
600
 
599
601
            if self.base_rev_id == _mod_revision.NULL_REVISION:
600
602
                raise errors.UnrelatedBranches()
601
603
            if self._is_criss_cross:
604
606
                trace.mutter('Criss-cross lcas: %r' % lcas)
605
607
                if self.base_rev_id in lcas:
606
608
                    trace.mutter('Unable to find unique lca. '
607
 
                                 'Fallback %r as best option.' % self.base_rev_id)
 
609
                                 'Fallback %r as best option.'
 
610
                                 % self.base_rev_id)
608
611
                interesting_revision_ids = set(lcas)
609
612
                interesting_revision_ids.add(self.base_rev_id)
610
613
                interesting_trees = dict((t.get_revision_id(), t)
689
692
                    continue
690
693
                sub_merge = Merger(sub_tree.branch, this_tree=sub_tree)
691
694
                sub_merge.merge_type = self.merge_type
692
 
                other_branch = self.other_branch.reference_parent(file_id, relpath)
 
695
                other_branch = self.other_branch.reference_parent(file_id,
 
696
                                                                  relpath)
693
697
                sub_merge.set_other_revision(other_revision, other_branch)
694
698
                base_revision = self.base_tree.get_reference_revision(file_id)
695
699
                sub_merge.base_tree = \
711
715
        merge = operation.run_simple()
712
716
        if len(merge.cooked_conflicts) == 0:
713
717
            if not self.ignore_zero and not trace.is_quiet():
714
 
                trace.note("All changes applied successfully.")
 
718
                trace.note(gettext("All changes applied successfully."))
715
719
        else:
716
 
            trace.note("%d conflicts encountered."
 
720
            trace.note(gettext("%d conflicts encountered.")
717
721
                       % len(merge.cooked_conflicts))
718
722
 
719
723
        return len(merge.cooked_conflicts)
851
855
        else:
852
856
            entries = self._entries_lca()
853
857
            resolver = self._lca_multi_way
 
858
        # Prepare merge hooks
 
859
        factories = Merger.hooks['merge_file_content']
 
860
        # One hook for each registered one plus our default merger
 
861
        hooks = [factory(self) for factory in factories] + [self]
 
862
        self.active_hooks = [hook for hook in hooks if hook is not None]
854
863
        child_pb = ui.ui_factory.nested_progress_bar()
855
864
        try:
856
 
            factories = Merger.hooks['merge_file_content']
857
 
            hooks = [factory(self) for factory in factories] + [self]
858
 
            self.active_hooks = [hook for hook in hooks if hook is not None]
859
865
            for num, (file_id, changed, parents3, names3,
860
866
                      executable3) in enumerate(entries):
861
 
                child_pb.update('Preparing file merge', num, len(entries))
 
867
                # Try merging each entry
 
868
                child_pb.update(gettext('Preparing file merge'),
 
869
                                num, len(entries))
862
870
                self._merge_names(file_id, parents3, names3, resolver=resolver)
863
871
                if changed:
864
872
                    file_status = self._do_merge_contents(file_id)
1387
1395
            if hook_status != 'not_applicable':
1388
1396
                # Don't try any more hooks, this one applies.
1389
1397
                break
 
1398
        # If the merge ends up replacing the content of the file, we get rid of
 
1399
        # it at the end of this method (this variable is used to track the
 
1400
        # exceptions to this rule).
 
1401
        keep_this = False
1390
1402
        result = "modified"
1391
1403
        if hook_status == 'not_applicable':
1392
 
            # This is a contents conflict, because none of the available
1393
 
            # functions could merge it.
 
1404
            # No merge hook was able to resolve the situation. Two cases exist:
 
1405
            # a content conflict or a duplicate one.
1394
1406
            result = None
1395
1407
            name = self.tt.final_name(trans_id)
1396
1408
            parent_id = self.tt.final_parent(trans_id)
1397
 
            if self.this_tree.has_id(file_id):
1398
 
                self.tt.unversion_file(trans_id)
1399
 
            file_group = self._dump_conflicts(name, parent_id, file_id,
1400
 
                                              set_version=True)
1401
 
            self._raw_conflicts.append(('contents conflict', file_group))
 
1409
            duplicate = False
 
1410
            inhibit_content_conflict = False
 
1411
            if params.this_kind is None: # file_id is not in THIS
 
1412
                # Is the name used for a different file_id ?
 
1413
                dupe_path = self.other_tree.id2path(file_id)
 
1414
                this_id = self.this_tree.path2id(dupe_path)
 
1415
                if this_id is not None:
 
1416
                    # Two entries for the same path
 
1417
                    keep_this = True
 
1418
                    # versioning the merged file will trigger a duplicate
 
1419
                    # conflict
 
1420
                    self.tt.version_file(file_id, trans_id)
 
1421
                    transform.create_from_tree(
 
1422
                        self.tt, trans_id, self.other_tree, file_id,
 
1423
                        filter_tree_path=self._get_filter_tree_path(file_id))
 
1424
                    inhibit_content_conflict = True
 
1425
            elif params.other_kind is None: # file_id is not in OTHER
 
1426
                # Is the name used for a different file_id ?
 
1427
                dupe_path = self.this_tree.id2path(file_id)
 
1428
                other_id = self.other_tree.path2id(dupe_path)
 
1429
                if other_id is not None:
 
1430
                    # Two entries for the same path again, but here, the other
 
1431
                    # entry will also be merged.  We simply inhibit the
 
1432
                    # 'content' conflict creation because we know OTHER will
 
1433
                    # create (or has already created depending on ordering) an
 
1434
                    # entry at the same path. This will trigger a 'duplicate'
 
1435
                    # conflict later.
 
1436
                    keep_this = True
 
1437
                    inhibit_content_conflict = True
 
1438
            if not inhibit_content_conflict:
 
1439
                if params.this_kind is not None:
 
1440
                    self.tt.unversion_file(trans_id)
 
1441
                # This is a contents conflict, because none of the available
 
1442
                # functions could merge it.
 
1443
                file_group = self._dump_conflicts(name, parent_id, file_id,
 
1444
                                                  set_version=True)
 
1445
                self._raw_conflicts.append(('contents conflict', file_group))
1402
1446
        elif hook_status == 'success':
1403
1447
            self.tt.create_file(lines, trans_id)
1404
1448
        elif hook_status == 'conflicted':
1420
1464
            raise AssertionError('unknown hook_status: %r' % (hook_status,))
1421
1465
        if not self.this_tree.has_id(file_id) and result == "modified":
1422
1466
            self.tt.version_file(file_id, trans_id)
1423
 
        # The merge has been performed, so the old contents should not be
1424
 
        # retained.
1425
 
        self.tt.delete_contents(trans_id)
 
1467
        if not keep_this:
 
1468
            # The merge has been performed and produced a new content, so the
 
1469
            # old contents should not be retained.
 
1470
            self.tt.delete_contents(trans_id)
1426
1471
        return result
1427
1472
 
1428
1473
    def _default_other_winner_merge(self, merge_hook_params):
1429
1474
        """Replace this contents with other."""
1430
1475
        file_id = merge_hook_params.file_id
1431
1476
        trans_id = merge_hook_params.trans_id
1432
 
        file_in_this = self.this_tree.has_id(file_id)
1433
1477
        if self.other_tree.has_id(file_id):
1434
1478
            # OTHER changed the file
1435
 
            wt = self.this_tree
1436
 
            if wt.supports_content_filtering():
1437
 
                # We get the path from the working tree if it exists.
1438
 
                # That fails though when OTHER is adding a file, so
1439
 
                # we fall back to the other tree to find the path if
1440
 
                # it doesn't exist locally.
1441
 
                try:
1442
 
                    filter_tree_path = wt.id2path(file_id)
1443
 
                except errors.NoSuchId:
1444
 
                    filter_tree_path = self.other_tree.id2path(file_id)
1445
 
            else:
1446
 
                # Skip the id2path lookup for older formats
1447
 
                filter_tree_path = None
1448
 
            transform.create_from_tree(self.tt, trans_id,
1449
 
                             self.other_tree, file_id,
1450
 
                             filter_tree_path=filter_tree_path)
 
1479
            transform.create_from_tree(
 
1480
                self.tt, trans_id, self.other_tree, file_id,
 
1481
                filter_tree_path=self._get_filter_tree_path(file_id))
1451
1482
            return 'done', None
1452
 
        elif file_in_this:
 
1483
        elif self.this_tree.has_id(file_id):
1453
1484
            # OTHER deleted the file
1454
1485
            return 'delete', None
1455
1486
        else:
1529
1560
                                              other_lines)
1530
1561
            file_group.append(trans_id)
1531
1562
 
 
1563
 
 
1564
    def _get_filter_tree_path(self, file_id):
 
1565
        if self.this_tree.supports_content_filtering():
 
1566
            # We get the path from the working tree if it exists.
 
1567
            # That fails though when OTHER is adding a file, so
 
1568
            # we fall back to the other tree to find the path if
 
1569
            # it doesn't exist locally.
 
1570
            try:
 
1571
                return self.this_tree.id2path(file_id)
 
1572
            except errors.NoSuchId:
 
1573
                return self.other_tree.id2path(file_id)
 
1574
        # Skip the id2path lookup for older formats
 
1575
        return None
 
1576
 
1532
1577
    def _dump_conflicts(self, name, parent_id, file_id, this_lines=None,
1533
1578
                        base_lines=None, other_lines=None, set_version=False,
1534
1579
                        no_base=False):
1635
1680
                if other_parent is None or other_name is None:
1636
1681
                    other_path = '<deleted>'
1637
1682
                else:
1638
 
                    parent_path =  fp.get_path(
1639
 
                        self.tt.trans_id_file_id(other_parent))
 
1683
                    if other_parent == self.other_tree.get_root_id():
 
1684
                        # The tree transform doesn't know about the other root,
 
1685
                        # so we special case here to avoid a NoFinalPath
 
1686
                        # exception
 
1687
                        parent_path = ''
 
1688
                    else:
 
1689
                        parent_path =  fp.get_path(
 
1690
                            self.tt.trans_id_file_id(other_parent))
1640
1691
                    other_path = osutils.pathjoin(parent_path, other_name)
1641
1692
                c = _mod_conflicts.Conflict.factory(
1642
1693
                    'path conflict', path=this_path,
1646
1697
                for trans_id in conflict[1]:
1647
1698
                    file_id = self.tt.final_file_id(trans_id)
1648
1699
                    if file_id is not None:
 
1700
                        # Ok we found the relevant file-id
1649
1701
                        break
1650
1702
                path = fp.get_path(trans_id)
1651
1703
                for suffix in ('.BASE', '.THIS', '.OTHER'):
1652
1704
                    if path.endswith(suffix):
 
1705
                        # Here is the raw path
1653
1706
                        path = path[:-len(suffix)]
1654
1707
                        break
1655
1708
                c = _mod_conflicts.Conflict.factory(conflict_type,
1900
1953
            entries = self._entries_to_incorporate()
1901
1954
            entries = list(entries)
1902
1955
            for num, (entry, parent_id) in enumerate(entries):
1903
 
                child_pb.update('Preparing file merge', num, len(entries))
 
1956
                child_pb.update(gettext('Preparing file merge'), num, len(entries))
1904
1957
                parent_trans_id = self.tt.trans_id_file_id(parent_id)
1905
1958
                trans_id = transform.new_by_entry(self.tt, entry,
1906
1959
                    parent_trans_id, self.other_tree)
1990
2043
    merger.set_base_revision(get_revision_id(), this_branch)
1991
2044
    return merger.do_merge()
1992
2045
 
 
2046
 
 
2047
merge_type_registry = registry.Registry()
 
2048
merge_type_registry.register('diff3', Diff3Merger,
 
2049
                             "Merge using external diff3.")
 
2050
merge_type_registry.register('lca', LCAMerger,
 
2051
                             "LCA-newness merge.")
 
2052
merge_type_registry.register('merge3', Merge3Merger,
 
2053
                             "Native diff3-style merge.")
 
2054
merge_type_registry.register('weave', WeaveMerger,
 
2055
                             "Weave-based merge.")
 
2056
 
 
2057
 
1993
2058
def get_merge_type_registry():
1994
 
    """Merge type registry is in bzrlib.option to avoid circular imports.
 
2059
    """Merge type registry was previously in bzrlib.option
1995
2060
 
1996
 
    This method provides a sanctioned way to retrieve it.
 
2061
    This method provides a backwards compatible way to retrieve it.
1997
2062
    """
1998
 
    from bzrlib import option
1999
 
    return option._merge_type_registry
 
2063
    return merge_type_registry
2000
2064
 
2001
2065
 
2002
2066
def _plan_annotate_merge(annotated_a, annotated_b, ancestors_a, ancestors_b):