~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

(vila) Generate a 'duplicate' conflict instead of a 'content' conflict when
 the same path is involved in both branches. (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
591
591
                else:
592
592
                    self.base_rev_id = self.revision_graph.find_unique_lca(
593
593
                                            *lcas)
594
 
                sorted_lca_keys = self.revision_graph.find_merge_order(                
 
594
                sorted_lca_keys = self.revision_graph.find_merge_order(
595
595
                    revisions[0], lcas)
596
596
                if self.base_rev_id == _mod_revision.NULL_REVISION:
597
597
                    self.base_rev_id = sorted_lca_keys[0]
598
 
                
 
598
 
599
599
            if self.base_rev_id == _mod_revision.NULL_REVISION:
600
600
                raise errors.UnrelatedBranches()
601
601
            if self._is_criss_cross:
604
604
                trace.mutter('Criss-cross lcas: %r' % lcas)
605
605
                if self.base_rev_id in lcas:
606
606
                    trace.mutter('Unable to find unique lca. '
607
 
                                 'Fallback %r as best option.' % self.base_rev_id)
 
607
                                 'Fallback %r as best option.'
 
608
                                 % self.base_rev_id)
608
609
                interesting_revision_ids = set(lcas)
609
610
                interesting_revision_ids.add(self.base_rev_id)
610
611
                interesting_trees = dict((t.get_revision_id(), t)
689
690
                    continue
690
691
                sub_merge = Merger(sub_tree.branch, this_tree=sub_tree)
691
692
                sub_merge.merge_type = self.merge_type
692
 
                other_branch = self.other_branch.reference_parent(file_id, relpath)
 
693
                other_branch = self.other_branch.reference_parent(file_id,
 
694
                                                                  relpath)
693
695
                sub_merge.set_other_revision(other_revision, other_branch)
694
696
                base_revision = self.base_tree.get_reference_revision(file_id)
695
697
                sub_merge.base_tree = \
1387
1389
            if hook_status != 'not_applicable':
1388
1390
                # Don't try any more hooks, this one applies.
1389
1391
                break
 
1392
        # If the merge ends up replacing the content of the file, we get rid of
 
1393
        # it at the end of this method (this variable is used to track the
 
1394
        # exceptions to this rule).
 
1395
        keep_this = False
1390
1396
        result = "modified"
1391
1397
        if hook_status == 'not_applicable':
1392
 
            # This is a contents conflict, because none of the available
1393
 
            # functions could merge it.
 
1398
            # No merge hook was able to resolve the situation. Two cases exist:
 
1399
            # a content conflict or a duplicate one.
1394
1400
            result = None
1395
1401
            name = self.tt.final_name(trans_id)
1396
1402
            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))
 
1403
            duplicate = False
 
1404
            inhibit_content_conflict = False
 
1405
            if params.this_kind is None: # file_id is not in THIS
 
1406
                # Is the name used for a different file_id ?
 
1407
                dupe_path = self.other_tree.id2path(file_id)
 
1408
                this_id = self.this_tree.path2id(dupe_path)
 
1409
                if this_id is not None:
 
1410
                    # Two entries for the same path
 
1411
                    keep_this = True
 
1412
                    # versioning the merged file will trigger a duplicate
 
1413
                    # conflict
 
1414
                    self.tt.version_file(file_id, trans_id)
 
1415
                    transform.create_from_tree(
 
1416
                        self.tt, trans_id, self.other_tree, file_id,
 
1417
                        filter_tree_path=self._get_filter_tree_path(file_id))
 
1418
                    inhibit_content_conflict = True
 
1419
            elif params.other_kind is None: # file_id is not in OTHER
 
1420
                # Is the name used for a different file_id ?
 
1421
                dupe_path = self.this_tree.id2path(file_id)
 
1422
                other_id = self.other_tree.path2id(dupe_path)
 
1423
                if other_id is not None:
 
1424
                    # Two entries for the same path again, but here, the other
 
1425
                    # entry will also be merged.  We simply inhibit the
 
1426
                    # 'content' conflict creation because we know OTHER will
 
1427
                    # create (or has already created depending on ordering) an
 
1428
                    # entry at the same path. This will trigger a 'duplicate'
 
1429
                    # conflict later.
 
1430
                    keep_this = True
 
1431
                    inhibit_content_conflict = True
 
1432
            if not inhibit_content_conflict:
 
1433
                if params.this_kind is not None:
 
1434
                    self.tt.unversion_file(trans_id)
 
1435
                # This is a contents conflict, because none of the available
 
1436
                # functions could merge it.
 
1437
                file_group = self._dump_conflicts(name, parent_id, file_id,
 
1438
                                                  set_version=True)
 
1439
                self._raw_conflicts.append(('contents conflict', file_group))
1402
1440
        elif hook_status == 'success':
1403
1441
            self.tt.create_file(lines, trans_id)
1404
1442
        elif hook_status == 'conflicted':
1420
1458
            raise AssertionError('unknown hook_status: %r' % (hook_status,))
1421
1459
        if not self.this_tree.has_id(file_id) and result == "modified":
1422
1460
            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)
 
1461
        if not keep_this:
 
1462
            # The merge has been performed and produced a new content, so the
 
1463
            # old contents should not be retained.
 
1464
            self.tt.delete_contents(trans_id)
1426
1465
        return result
1427
1466
 
1428
1467
    def _default_other_winner_merge(self, merge_hook_params):
1429
1468
        """Replace this contents with other."""
1430
1469
        file_id = merge_hook_params.file_id
1431
1470
        trans_id = merge_hook_params.trans_id
1432
 
        file_in_this = self.this_tree.has_id(file_id)
1433
1471
        if self.other_tree.has_id(file_id):
1434
1472
            # 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)
 
1473
            transform.create_from_tree(
 
1474
                self.tt, trans_id, self.other_tree, file_id,
 
1475
                filter_tree_path=self._get_filter_tree_path(file_id))
1451
1476
            return 'done', None
1452
 
        elif file_in_this:
 
1477
        elif self.this_tree.has_id(file_id):
1453
1478
            # OTHER deleted the file
1454
1479
            return 'delete', None
1455
1480
        else:
1529
1554
                                              other_lines)
1530
1555
            file_group.append(trans_id)
1531
1556
 
 
1557
 
 
1558
    def _get_filter_tree_path(self, file_id):
 
1559
        if self.this_tree.supports_content_filtering():
 
1560
            # We get the path from the working tree if it exists.
 
1561
            # That fails though when OTHER is adding a file, so
 
1562
            # we fall back to the other tree to find the path if
 
1563
            # it doesn't exist locally.
 
1564
            try:
 
1565
                return self.this_tree.id2path(file_id)
 
1566
            except errors.NoSuchId:
 
1567
                return self.other_tree.id2path(file_id)
 
1568
        # Skip the id2path lookup for older formats
 
1569
        return None
 
1570
 
1532
1571
    def _dump_conflicts(self, name, parent_id, file_id, this_lines=None,
1533
1572
                        base_lines=None, other_lines=None, set_version=False,
1534
1573
                        no_base=False):
1652
1691
                for trans_id in conflict[1]:
1653
1692
                    file_id = self.tt.final_file_id(trans_id)
1654
1693
                    if file_id is not None:
 
1694
                        # Ok we found the relevant file-id
1655
1695
                        break
1656
1696
                path = fp.get_path(trans_id)
1657
1697
                for suffix in ('.BASE', '.THIS', '.OTHER'):
1658
1698
                    if path.endswith(suffix):
 
1699
                        # Here is the raw path
1659
1700
                        path = path[:-len(suffix)]
1660
1701
                        break
1661
1702
                c = _mod_conflicts.Conflict.factory(conflict_type,