~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-16 16:40:10 UTC
  • mto: This revision was merged to the branch mainline in revision 6391.
  • Revision ID: jelmer@samba.org-20111216164010-z3hy00xrnclnkf7a
Update tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
20
20
lazy_import(globals(), """
21
21
from bzrlib import (
22
22
    branch as _mod_branch,
 
23
    cleanup,
23
24
    conflicts as _mod_conflicts,
24
25
    debug,
25
26
    generate_ids,
37
38
    versionedfile,
38
39
    workingtree,
39
40
    )
40
 
from bzrlib.cleanup import OperationWithCleanups
 
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,
53
55
 
54
56
def transform_tree(from_tree, to_tree, interesting_ids=None):
55
57
    from_tree.lock_tree_write()
56
 
    operation = OperationWithCleanups(merge_inner)
 
58
    operation = cleanup.OperationWithCleanups(merge_inner)
57
59
    operation.add_cleanup(from_tree.unlock)
58
60
    operation.run_simple(from_tree.branch, to_tree, from_tree,
59
61
        ignore_zero=True, interesting_ids=interesting_ids, this_tree=from_tree)
62
64
class MergeHooks(hooks.Hooks):
63
65
 
64
66
    def __init__(self):
65
 
        hooks.Hooks.__init__(self)
66
 
        self.create_hook(hooks.HookPoint('merge_file_content',
 
67
        hooks.Hooks.__init__(self, "bzrlib.merge", "Merger.hooks")
 
68
        self.add_hook('merge_file_content',
67
69
            "Called with a bzrlib.merge.Merger object to create a per file "
68
70
            "merge object when starting a merge. "
69
71
            "Should return either None or a subclass of "
73
75
            "side has deleted the file and the other has changed it). "
74
76
            "See the AbstractPerFileMerger API docs for details on how it is "
75
77
            "used by merge.",
76
 
            (2, 1), None))
 
78
            (2, 1))
77
79
 
78
80
 
79
81
class AbstractPerFileMerger(object):
92
94
        """Attempt to merge the contents of a single file.
93
95
        
94
96
        :param merge_params: A bzrlib.merge.MergeHookParams
95
 
        :return : A tuple of (status, chunks), where status is one of
 
97
        :return: A tuple of (status, chunks), where status is one of
96
98
            'not_applicable', 'success', 'conflicted', or 'delete'.  If status
97
99
            is 'success' or 'conflicted', then chunks should be an iterable of
98
100
            strings for the new file contents.
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)
458
460
    @deprecated_method(deprecated_in((2, 1, 0)))
459
461
    def file_revisions(self, file_id):
460
462
        self.ensure_revision_trees()
461
 
        def get_id(tree, file_id):
462
 
            revision_id = tree.inventory[file_id].revision
463
 
            return revision_id
464
463
        if self.this_rev_id is None:
465
464
            if self.this_basis_tree.get_file_sha1(file_id) != \
466
465
                self.this_tree.get_file_sha1(file_id):
467
466
                raise errors.WorkingTreeNotRevision(self.this_tree)
468
467
 
469
468
        trees = (self.this_basis_tree, self.other_tree)
470
 
        return [get_id(tree, file_id) for tree in trees]
 
469
        return [tree.get_file_revision(file_id) for tree in trees]
471
470
 
472
471
    @deprecated_method(deprecated_in((2, 1, 0)))
473
472
    def check_basis(self, check_clean, require_commits=True):
501
500
    def _add_parent(self):
502
501
        new_parents = self.this_tree.get_parent_ids() + [self.other_rev_id]
503
502
        new_parent_trees = []
504
 
        operation = OperationWithCleanups(self.this_tree.set_parent_trees)
 
503
        operation = cleanup.OperationWithCleanups(
 
504
            self.this_tree.set_parent_trees)
505
505
        for revision_id in new_parents:
506
506
            try:
507
507
                tree = self.revision_tree(revision_id)
582
582
            elif len(lcas) == 1:
583
583
                self.base_rev_id = list(lcas)[0]
584
584
            else: # len(lcas) > 1
 
585
                self._is_criss_cross = True
585
586
                if len(lcas) > 2:
586
587
                    # find_unique_lca can only handle 2 nodes, so we have to
587
588
                    # start back at the beginning. It is a shame to traverse
592
593
                else:
593
594
                    self.base_rev_id = self.revision_graph.find_unique_lca(
594
595
                                            *lcas)
595
 
                self._is_criss_cross = True
 
596
                sorted_lca_keys = self.revision_graph.find_merge_order(
 
597
                    revisions[0], lcas)
 
598
                if self.base_rev_id == _mod_revision.NULL_REVISION:
 
599
                    self.base_rev_id = sorted_lca_keys[0]
 
600
 
596
601
            if self.base_rev_id == _mod_revision.NULL_REVISION:
597
602
                raise errors.UnrelatedBranches()
598
603
            if self._is_criss_cross:
599
604
                trace.warning('Warning: criss-cross merge encountered.  See bzr'
600
605
                              ' help criss-cross.')
601
606
                trace.mutter('Criss-cross lcas: %r' % lcas)
602
 
                interesting_revision_ids = [self.base_rev_id]
603
 
                interesting_revision_ids.extend(lcas)
 
607
                if self.base_rev_id in lcas:
 
608
                    trace.mutter('Unable to find unique lca. '
 
609
                                 'Fallback %r as best option.'
 
610
                                 % self.base_rev_id)
 
611
                interesting_revision_ids = set(lcas)
 
612
                interesting_revision_ids.add(self.base_rev_id)
604
613
                interesting_trees = dict((t.get_revision_id(), t)
605
614
                    for t in self.this_branch.repository.revision_trees(
606
615
                        interesting_revision_ids))
607
616
                self._cached_trees.update(interesting_trees)
608
 
                self.base_tree = interesting_trees.pop(self.base_rev_id)
609
 
                sorted_lca_keys = self.revision_graph.find_merge_order(
610
 
                    revisions[0], lcas)
 
617
                if self.base_rev_id in lcas:
 
618
                    self.base_tree = interesting_trees[self.base_rev_id]
 
619
                else:
 
620
                    self.base_tree = interesting_trees.pop(self.base_rev_id)
611
621
                self._lca_trees = [interesting_trees[key]
612
622
                                   for key in sorted_lca_keys]
613
623
            else:
682
692
                    continue
683
693
                sub_merge = Merger(sub_tree.branch, this_tree=sub_tree)
684
694
                sub_merge.merge_type = self.merge_type
685
 
                other_branch = self.other_branch.reference_parent(file_id, relpath)
 
695
                other_branch = self.other_branch.reference_parent(file_id,
 
696
                                                                  relpath)
686
697
                sub_merge.set_other_revision(other_revision, other_branch)
687
698
                base_revision = self.base_tree.get_reference_revision(file_id)
688
699
                sub_merge.base_tree = \
692
703
        return merge
693
704
 
694
705
    def do_merge(self):
695
 
        operation = OperationWithCleanups(self._do_merge_to)
 
706
        operation = cleanup.OperationWithCleanups(self._do_merge_to)
696
707
        self.this_tree.lock_tree_write()
697
708
        operation.add_cleanup(self.this_tree.unlock)
698
709
        if self.base_tree is not None:
704
715
        merge = operation.run_simple()
705
716
        if len(merge.cooked_conflicts) == 0:
706
717
            if not self.ignore_zero and not trace.is_quiet():
707
 
                trace.note("All changes applied successfully.")
 
718
                trace.note(gettext("All changes applied successfully."))
708
719
        else:
709
 
            trace.note("%d conflicts encountered."
 
720
            trace.note(gettext("%d conflicts encountered.")
710
721
                       % len(merge.cooked_conflicts))
711
722
 
712
723
        return len(merge.cooked_conflicts)
804
815
            warnings.warn("pb argument to Merge3Merger is deprecated")
805
816
 
806
817
    def do_merge(self):
807
 
        operation = OperationWithCleanups(self._do_merge)
 
818
        operation = cleanup.OperationWithCleanups(self._do_merge)
808
819
        self.this_tree.lock_tree_write()
809
820
        operation.add_cleanup(self.this_tree.unlock)
810
821
        self.base_tree.lock_read()
825
836
            pass
826
837
 
827
838
    def make_preview_transform(self):
828
 
        operation = OperationWithCleanups(self._make_preview_transform)
 
839
        operation = cleanup.OperationWithCleanups(self._make_preview_transform)
829
840
        self.base_tree.lock_read()
830
841
        operation.add_cleanup(self.base_tree.unlock)
831
842
        self.other_tree.lock_read()
844
855
        else:
845
856
            entries = self._entries_lca()
846
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]
847
863
        child_pb = ui.ui_factory.nested_progress_bar()
848
864
        try:
849
 
            factories = Merger.hooks['merge_file_content']
850
 
            hooks = [factory(self) for factory in factories] + [self]
851
 
            self.active_hooks = [hook for hook in hooks if hook is not None]
852
865
            for num, (file_id, changed, parents3, names3,
853
866
                      executable3) in enumerate(entries):
854
 
                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))
855
870
                self._merge_names(file_id, parents3, names3, resolver=resolver)
856
871
                if changed:
857
872
                    file_status = self._do_merge_contents(file_id)
861
876
                    executable3, file_status, resolver=resolver)
862
877
        finally:
863
878
            child_pb.finished()
864
 
        self.fix_root()
 
879
        self.tt.fixup_new_roots()
865
880
        self._finish_computing_transform()
866
881
 
867
882
    def _finish_computing_transform(self):
881
896
                self.tt.iter_changes(), self.change_reporter)
882
897
        self.cook_conflicts(fs_conflicts)
883
898
        for conflict in self.cooked_conflicts:
884
 
            trace.warning(conflict)
 
899
            trace.warning(unicode(conflict))
885
900
 
886
901
    def _entries3(self):
887
902
        """Gather data about files modified between three trees.
894
909
        """
895
910
        result = []
896
911
        iterator = self.other_tree.iter_changes(self.base_tree,
897
 
                include_unchanged=True, specific_files=self.interesting_files,
 
912
                specific_files=self.interesting_files,
898
913
                extra_trees=[self.this_tree])
899
914
        this_entries = dict((e.file_id, e) for p, e in
900
915
                            self.this_tree.iter_entries_by_dir(
926
941
        it then compares with THIS and BASE.
927
942
 
928
943
        For the multi-valued entries, the format will be (BASE, [lca1, lca2])
929
 
        :return: [(file_id, changed, parents, names, executable)]
930
 
            file_id     Simple file_id of the entry
931
 
            changed     Boolean, True if the kind or contents changed
932
 
                        else False
933
 
            parents     ((base, [parent_id, in, lcas]), parent_id_other,
934
 
                         parent_id_this)
935
 
            names       ((base, [name, in, lcas]), name_in_other, name_in_this)
936
 
            executable  ((base, [exec, in, lcas]), exec_in_other, exec_in_this)
 
944
 
 
945
        :return: [(file_id, changed, parents, names, executable)], where:
 
946
 
 
947
            * file_id: Simple file_id of the entry
 
948
            * changed: Boolean, True if the kind or contents changed else False
 
949
            * parents: ((base, [parent_id, in, lcas]), parent_id_other,
 
950
                        parent_id_this)
 
951
            * names:   ((base, [name, in, lcas]), name_in_other, name_in_this)
 
952
            * executable: ((base, [exec, in, lcas]), exec_in_other,
 
953
                        exec_in_this)
937
954
        """
938
955
        if self.interesting_files is not None:
939
956
            lookup_trees = [self.this_tree, self.base_tree]
981
998
                else:
982
999
                    lca_entries.append(lca_ie)
983
1000
 
984
 
            if file_id in base_inventory:
 
1001
            if base_inventory.has_id(file_id):
985
1002
                base_ie = base_inventory[file_id]
986
1003
            else:
987
1004
                base_ie = _none_entry
988
1005
 
989
 
            if file_id in this_inventory:
 
1006
            if this_inventory.has_id(file_id):
990
1007
                this_ie = this_inventory[file_id]
991
1008
            else:
992
1009
                this_ie = _none_entry
1084
1101
                          ))
1085
1102
        return result
1086
1103
 
 
1104
    @deprecated_method(deprecated_in((2, 4, 0)))
1087
1105
    def fix_root(self):
1088
1106
        if self.tt.final_kind(self.tt.root) is None:
1089
1107
            self.tt.cancel_deletion(self.tt.root)
1096
1114
        other_root = self.tt.trans_id_file_id(other_root_file_id)
1097
1115
        if other_root == self.tt.root:
1098
1116
            return
1099
 
        if self.other_tree.inventory.root.file_id in self.this_tree.inventory:
1100
 
            # the other tree's root is a non-root in the current tree (as when
1101
 
            # a previously unrelated branch is merged into another)
 
1117
        if self.this_tree.inventory.has_id(
 
1118
            self.other_tree.inventory.root.file_id):
 
1119
            # the other tree's root is a non-root in the current tree (as
 
1120
            # when a previously unrelated branch is merged into another)
1102
1121
            return
1103
1122
        if self.tt.final_kind(other_root) is not None:
1104
1123
            other_root_is_present = True
1109
1128
        # 'other_tree.inventory.root' is not present in this tree. We are
1110
1129
        # calling adjust_path for children which *want* to be present with a
1111
1130
        # correct place to go.
1112
 
        for thing, child in self.other_tree.inventory.root.children.iteritems():
 
1131
        for _, child in self.other_tree.inventory.root.children.iteritems():
1113
1132
            trans_id = self.tt.trans_id_file_id(child.file_id)
1114
1133
            if not other_root_is_present:
1115
1134
                if self.tt.final_kind(trans_id) is not None:
1117
1136
                    # to go already.
1118
1137
                    continue
1119
1138
            # Move the item into the root
1120
 
            self.tt.adjust_path(self.tt.final_name(trans_id),
1121
 
                                self.tt.root, trans_id)
 
1139
            try:
 
1140
                final_name = self.tt.final_name(trans_id)
 
1141
            except errors.NoFinalPath:
 
1142
                # This file is not present anymore, ignore it.
 
1143
                continue
 
1144
            self.tt.adjust_path(final_name, self.tt.root, trans_id)
1122
1145
        if other_root_is_present:
1123
1146
            self.tt.cancel_creation(other_root)
1124
1147
            self.tt.cancel_versioning(other_root)
1152
1175
    @staticmethod
1153
1176
    def contents_sha1(tree, file_id):
1154
1177
        """Determine the sha1 of the file contents (used as a key method)."""
1155
 
        if file_id not in tree:
 
1178
        if not tree.has_id(file_id):
1156
1179
            return None
1157
1180
        return tree.get_file_sha1(file_id)
1158
1181
 
1303
1326
            self._raw_conflicts.append(('path conflict', trans_id, file_id,
1304
1327
                                        this_parent, this_name,
1305
1328
                                        other_parent, other_name))
1306
 
        if other_name is None:
 
1329
        if not self.other_tree.has_id(file_id):
1307
1330
            # it doesn't matter whether the result was 'other' or
1308
 
            # 'conflict'-- if there's no 'other', we leave it alone.
 
1331
            # 'conflict'-- if it has no file id, we leave it alone.
1309
1332
            return
1310
1333
        parent_id = parents[self.winner_idx[parent_id_winner]]
1311
 
        if parent_id is not None:
 
1334
        name = names[self.winner_idx[name_winner]]
 
1335
        if parent_id is not None or name is not None:
1312
1336
            # if we get here, name_winner and parent_winner are set to safe
1313
1337
            # values.
1314
 
            self.tt.adjust_path(names[self.winner_idx[name_winner]],
1315
 
                                self.tt.trans_id_file_id(parent_id),
 
1338
            if parent_id is None and name is not None:
 
1339
                # if parent_id is None and name is non-None, current file is
 
1340
                # the tree root.
 
1341
                if names[self.winner_idx[parent_id_winner]] != '':
 
1342
                    raise AssertionError(
 
1343
                        'File looks like a root, but named %s' %
 
1344
                        names[self.winner_idx[parent_id_winner]])
 
1345
                parent_trans_id = transform.ROOT_PARENT
 
1346
            else:
 
1347
                parent_trans_id = self.tt.trans_id_file_id(parent_id)
 
1348
            self.tt.adjust_path(name, parent_trans_id,
1316
1349
                                self.tt.trans_id_file_id(file_id))
1317
1350
 
1318
1351
    def _do_merge_contents(self, file_id):
1319
1352
        """Performs a merge on file_id contents."""
1320
1353
        def contents_pair(tree):
1321
 
            if file_id not in tree:
 
1354
            if not tree.has_id(file_id):
1322
1355
                return (None, None)
1323
1356
            kind = tree.kind(file_id)
1324
1357
            if kind == "file":
1362
1395
            if hook_status != 'not_applicable':
1363
1396
                # Don't try any more hooks, this one applies.
1364
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
1365
1402
        result = "modified"
1366
1403
        if hook_status == 'not_applicable':
1367
 
            # This is a contents conflict, because none of the available
1368
 
            # 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.
1369
1406
            result = None
1370
1407
            name = self.tt.final_name(trans_id)
1371
1408
            parent_id = self.tt.final_parent(trans_id)
1372
 
            if self.this_tree.has_id(file_id):
1373
 
                self.tt.unversion_file(trans_id)
1374
 
            file_group = self._dump_conflicts(name, parent_id, file_id,
1375
 
                                              set_version=True)
1376
 
            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))
1377
1446
        elif hook_status == 'success':
1378
1447
            self.tt.create_file(lines, trans_id)
1379
1448
        elif hook_status == 'conflicted':
1395
1464
            raise AssertionError('unknown hook_status: %r' % (hook_status,))
1396
1465
        if not self.this_tree.has_id(file_id) and result == "modified":
1397
1466
            self.tt.version_file(file_id, trans_id)
1398
 
        # The merge has been performed, so the old contents should not be
1399
 
        # retained.
1400
 
        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)
1401
1471
        return result
1402
1472
 
1403
1473
    def _default_other_winner_merge(self, merge_hook_params):
1404
1474
        """Replace this contents with other."""
1405
1475
        file_id = merge_hook_params.file_id
1406
1476
        trans_id = merge_hook_params.trans_id
1407
 
        file_in_this = self.this_tree.has_id(file_id)
1408
1477
        if self.other_tree.has_id(file_id):
1409
1478
            # OTHER changed the file
1410
 
            wt = self.this_tree
1411
 
            if wt.supports_content_filtering():
1412
 
                # We get the path from the working tree if it exists.
1413
 
                # That fails though when OTHER is adding a file, so
1414
 
                # we fall back to the other tree to find the path if
1415
 
                # it doesn't exist locally.
1416
 
                try:
1417
 
                    filter_tree_path = wt.id2path(file_id)
1418
 
                except errors.NoSuchId:
1419
 
                    filter_tree_path = self.other_tree.id2path(file_id)
1420
 
            else:
1421
 
                # Skip the id2path lookup for older formats
1422
 
                filter_tree_path = None
1423
 
            transform.create_from_tree(self.tt, trans_id,
1424
 
                             self.other_tree, file_id,
1425
 
                             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))
1426
1482
            return 'done', None
1427
 
        elif file_in_this:
 
1483
        elif self.this_tree.has_id(file_id):
1428
1484
            # OTHER deleted the file
1429
1485
            return 'delete', None
1430
1486
        else:
1504
1560
                                              other_lines)
1505
1561
            file_group.append(trans_id)
1506
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
 
1507
1577
    def _dump_conflicts(self, name, parent_id, file_id, this_lines=None,
1508
1578
                        base_lines=None, other_lines=None, set_version=False,
1509
1579
                        no_base=False):
1592
1662
 
1593
1663
    def cook_conflicts(self, fs_conflicts):
1594
1664
        """Convert all conflicts into a form that doesn't depend on trans_id"""
1595
 
        self.cooked_conflicts.extend(transform.cook_conflicts(
1596
 
                fs_conflicts, self.tt))
 
1665
        content_conflict_file_ids = set()
 
1666
        cooked_conflicts = transform.cook_conflicts(fs_conflicts, self.tt)
1597
1667
        fp = transform.FinalPaths(self.tt)
1598
1668
        for conflict in self._raw_conflicts:
1599
1669
            conflict_type = conflict[0]
1610
1680
                if other_parent is None or other_name is None:
1611
1681
                    other_path = '<deleted>'
1612
1682
                else:
1613
 
                    parent_path =  fp.get_path(
1614
 
                        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))
1615
1691
                    other_path = osutils.pathjoin(parent_path, other_name)
1616
1692
                c = _mod_conflicts.Conflict.factory(
1617
1693
                    'path conflict', path=this_path,
1621
1697
                for trans_id in conflict[1]:
1622
1698
                    file_id = self.tt.final_file_id(trans_id)
1623
1699
                    if file_id is not None:
 
1700
                        # Ok we found the relevant file-id
1624
1701
                        break
1625
1702
                path = fp.get_path(trans_id)
1626
1703
                for suffix in ('.BASE', '.THIS', '.OTHER'):
1627
1704
                    if path.endswith(suffix):
 
1705
                        # Here is the raw path
1628
1706
                        path = path[:-len(suffix)]
1629
1707
                        break
1630
1708
                c = _mod_conflicts.Conflict.factory(conflict_type,
1631
1709
                                                    path=path, file_id=file_id)
 
1710
                content_conflict_file_ids.add(file_id)
1632
1711
            elif conflict_type == 'text conflict':
1633
1712
                trans_id = conflict[1]
1634
1713
                path = fp.get_path(trans_id)
1637
1716
                                                    path=path, file_id=file_id)
1638
1717
            else:
1639
1718
                raise AssertionError('bad conflict type: %r' % (conflict,))
 
1719
            cooked_conflicts.append(c)
 
1720
 
 
1721
        self.cooked_conflicts = []
 
1722
        # We want to get rid of path conflicts when a corresponding contents
 
1723
        # conflict exists. This can occur when one branch deletes a file while
 
1724
        # the other renames *and* modifies it. In this case, the content
 
1725
        # conflict is enough.
 
1726
        for c in cooked_conflicts:
 
1727
            if (c.typestring == 'path conflict'
 
1728
                and c.file_id in content_conflict_file_ids):
 
1729
                continue
1640
1730
            self.cooked_conflicts.append(c)
1641
1731
        self.cooked_conflicts.sort(key=_mod_conflicts.Conflict.sort_key)
1642
1732
 
1863
1953
            entries = self._entries_to_incorporate()
1864
1954
            entries = list(entries)
1865
1955
            for num, (entry, parent_id) in enumerate(entries):
1866
 
                child_pb.update('Preparing file merge', num, len(entries))
 
1956
                child_pb.update(gettext('Preparing file merge'), num, len(entries))
1867
1957
                parent_trans_id = self.tt.trans_id_file_id(parent_id)
1868
1958
                trans_id = transform.new_by_entry(self.tt, entry,
1869
1959
                    parent_trans_id, self.other_tree)
1887
1977
        name_in_target = osutils.basename(self._target_subdir)
1888
1978
        merge_into_root = subdir.copy()
1889
1979
        merge_into_root.name = name_in_target
1890
 
        if merge_into_root.file_id in self.this_tree.inventory:
 
1980
        if self.this_tree.inventory.has_id(merge_into_root.file_id):
1891
1981
            # Give the root a new file-id.
1892
1982
            # This can happen fairly easily if the directory we are
1893
1983
            # incorporating is the root, and both trees have 'TREE_ROOT' as
1930
2020
    """
1931
2021
    if this_tree is None:
1932
2022
        raise errors.BzrError("bzrlib.merge.merge_inner requires a this_tree "
1933
 
                              "parameter as of bzrlib version 0.8.")
 
2023
                              "parameter")
1934
2024
    merger = Merger(this_branch, other_tree, base_tree, this_tree=this_tree,
1935
2025
                    pb=pb, change_reporter=change_reporter)
1936
2026
    merger.backup_files = backup_files
1953
2043
    merger.set_base_revision(get_revision_id(), this_branch)
1954
2044
    return merger.do_merge()
1955
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
 
1956
2058
def get_merge_type_registry():
1957
 
    """Merge type registry is in bzrlib.option to avoid circular imports.
 
2059
    """Merge type registry was previously in bzrlib.option
1958
2060
 
1959
 
    This method provides a sanctioned way to retrieve it.
 
2061
    This method provides a backwards compatible way to retrieve it.
1960
2062
    """
1961
 
    from bzrlib import option
1962
 
    return option._merge_type_registry
 
2063
    return merge_type_registry
1963
2064
 
1964
2065
 
1965
2066
def _plan_annotate_merge(annotated_a, annotated_b, ancestors_a, ancestors_b):
2390
2491
class _PlanLCAMerge(_PlanMergeBase):
2391
2492
    """
2392
2493
    This merge algorithm differs from _PlanMerge in that:
 
2494
 
2393
2495
    1. comparisons are done against LCAs only
2394
2496
    2. cases where a contested line is new versus one LCA but old versus
2395
2497
       another are marked as conflicts, by emitting the line as conflicted-a
2436
2538
 
2437
2539
        If a line is killed and new, this indicates that the two merge
2438
2540
        revisions contain differing conflict resolutions.
 
2541
 
2439
2542
        :param revision_id: The id of the revision in which the lines are
2440
2543
            unique
2441
2544
        :param unique_line_numbers: The line numbers of unique lines.
2442
 
        :return a tuple of (new_this, killed_other):
 
2545
        :return: a tuple of (new_this, killed_other)
2443
2546
        """
2444
2547
        new = set()
2445
2548
        killed = set()