~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

(gz) Fix deprecations of win32utils path function unicode wrappers (Martin
 Packman)

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
from __future__ import absolute_import
 
18
 
17
19
import warnings
18
20
 
19
21
from bzrlib.lazy_import import lazy_import
38
40
    versionedfile,
39
41
    workingtree,
40
42
    )
 
43
from bzrlib.i18n import gettext
41
44
""")
42
45
from bzrlib import (
43
46
    decorators,
44
47
    errors,
45
48
    hooks,
 
49
    registry,
46
50
    )
47
51
from bzrlib.symbol_versioning import (
48
52
    deprecated_in,
138
142
            params.winner == 'other' or
139
143
            # THIS and OTHER aren't both files.
140
144
            not params.is_file_merge() or
141
 
            # The filename doesn't match *.xml
 
145
            # The filename doesn't match
142
146
            not self.file_matches(params)):
143
147
            return 'not_applicable', None
144
148
        return self.merge_matching(params)
441
445
        revision_id = _mod_revision.ensure_null(revision_id)
442
446
        return branch, self.revision_tree(revision_id, branch)
443
447
 
444
 
    @deprecated_method(deprecated_in((2, 1, 0)))
445
 
    def ensure_revision_trees(self):
446
 
        if self.this_revision_tree is None:
447
 
            self.this_basis_tree = self.revision_tree(self.this_basis)
448
 
            if self.this_basis == self.this_rev_id:
449
 
                self.this_revision_tree = self.this_basis_tree
450
 
 
451
 
        if self.other_rev_id is None:
452
 
            other_basis_tree = self.revision_tree(self.other_basis)
453
 
            if other_basis_tree.has_changes(self.other_tree):
454
 
                raise errors.WorkingTreeNotRevision(self.this_tree)
455
 
            other_rev_id = self.other_basis
456
 
            self.other_tree = other_basis_tree
457
 
 
458
 
    @deprecated_method(deprecated_in((2, 1, 0)))
459
 
    def file_revisions(self, file_id):
460
 
        self.ensure_revision_trees()
461
 
        if self.this_rev_id is None:
462
 
            if self.this_basis_tree.get_file_sha1(file_id) != \
463
 
                self.this_tree.get_file_sha1(file_id):
464
 
                raise errors.WorkingTreeNotRevision(self.this_tree)
465
 
 
466
 
        trees = (self.this_basis_tree, self.other_tree)
467
 
        return [tree.get_file_revision(file_id) for tree in trees]
468
 
 
469
 
    @deprecated_method(deprecated_in((2, 1, 0)))
470
 
    def check_basis(self, check_clean, require_commits=True):
471
 
        if self.this_basis is None and require_commits is True:
472
 
            raise errors.BzrCommandError(
473
 
                "This branch has no commits."
474
 
                " (perhaps you would prefer 'bzr pull')")
475
 
        if check_clean:
476
 
            self.compare_basis()
477
 
            if self.this_basis != self.this_rev_id:
478
 
                raise errors.UncommittedChanges(self.this_tree)
479
 
 
480
 
    @deprecated_method(deprecated_in((2, 1, 0)))
481
 
    def compare_basis(self):
482
 
        try:
483
 
            basis_tree = self.revision_tree(self.this_tree.last_revision())
484
 
        except errors.NoSuchRevision:
485
 
            basis_tree = self.this_tree.basis_tree()
486
 
        if not self.this_tree.has_changes(basis_tree):
487
 
            self.this_rev_id = self.this_basis
488
 
 
489
448
    def set_interesting_files(self, file_list):
490
449
        self.interesting_files = file_list
491
450
 
536
495
                raise errors.NoCommits(self.other_branch)
537
496
        if self.other_rev_id is not None:
538
497
            self._cached_trees[self.other_rev_id] = self.other_tree
539
 
        self._maybe_fetch(self.other_branch,self.this_branch, self.other_basis)
 
498
        self._maybe_fetch(self.other_branch, self.this_branch, self.other_basis)
540
499
 
541
500
    def set_other_revision(self, revision_id, other_branch):
542
501
        """Set 'other' based on a branch and revision id
591
550
                else:
592
551
                    self.base_rev_id = self.revision_graph.find_unique_lca(
593
552
                                            *lcas)
594
 
                sorted_lca_keys = self.revision_graph.find_merge_order(                
 
553
                sorted_lca_keys = self.revision_graph.find_merge_order(
595
554
                    revisions[0], lcas)
596
555
                if self.base_rev_id == _mod_revision.NULL_REVISION:
597
556
                    self.base_rev_id = sorted_lca_keys[0]
598
 
                
 
557
 
599
558
            if self.base_rev_id == _mod_revision.NULL_REVISION:
600
559
                raise errors.UnrelatedBranches()
601
560
            if self._is_criss_cross:
604
563
                trace.mutter('Criss-cross lcas: %r' % lcas)
605
564
                if self.base_rev_id in lcas:
606
565
                    trace.mutter('Unable to find unique lca. '
607
 
                                 'Fallback %r as best option.' % self.base_rev_id)
 
566
                                 'Fallback %r as best option.'
 
567
                                 % self.base_rev_id)
608
568
                interesting_revision_ids = set(lcas)
609
569
                interesting_revision_ids.add(self.base_rev_id)
610
570
                interesting_trees = dict((t.get_revision_id(), t)
689
649
                    continue
690
650
                sub_merge = Merger(sub_tree.branch, this_tree=sub_tree)
691
651
                sub_merge.merge_type = self.merge_type
692
 
                other_branch = self.other_branch.reference_parent(file_id, relpath)
 
652
                other_branch = self.other_branch.reference_parent(file_id,
 
653
                                                                  relpath)
693
654
                sub_merge.set_other_revision(other_revision, other_branch)
694
655
                base_revision = self.base_tree.get_reference_revision(file_id)
695
656
                sub_merge.base_tree = \
711
672
        merge = operation.run_simple()
712
673
        if len(merge.cooked_conflicts) == 0:
713
674
            if not self.ignore_zero and not trace.is_quiet():
714
 
                trace.note("All changes applied successfully.")
 
675
                trace.note(gettext("All changes applied successfully."))
715
676
        else:
716
 
            trace.note("%d conflicts encountered."
 
677
            trace.note(gettext("%d conflicts encountered.")
717
678
                       % len(merge.cooked_conflicts))
718
679
 
719
680
        return len(merge.cooked_conflicts)
851
812
        else:
852
813
            entries = self._entries_lca()
853
814
            resolver = self._lca_multi_way
 
815
        # Prepare merge hooks
 
816
        factories = Merger.hooks['merge_file_content']
 
817
        # One hook for each registered one plus our default merger
 
818
        hooks = [factory(self) for factory in factories] + [self]
 
819
        self.active_hooks = [hook for hook in hooks if hook is not None]
854
820
        child_pb = ui.ui_factory.nested_progress_bar()
855
821
        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
822
            for num, (file_id, changed, parents3, names3,
860
823
                      executable3) in enumerate(entries):
861
 
                child_pb.update('Preparing file merge', num, len(entries))
 
824
                # Try merging each entry
 
825
                child_pb.update(gettext('Preparing file merge'),
 
826
                                num, len(entries))
862
827
                self._merge_names(file_id, parents3, names3, resolver=resolver)
863
828
                if changed:
864
829
                    file_status = self._do_merge_contents(file_id)
1387
1352
            if hook_status != 'not_applicable':
1388
1353
                # Don't try any more hooks, this one applies.
1389
1354
                break
 
1355
        # If the merge ends up replacing the content of the file, we get rid of
 
1356
        # it at the end of this method (this variable is used to track the
 
1357
        # exceptions to this rule).
 
1358
        keep_this = False
1390
1359
        result = "modified"
1391
1360
        if hook_status == 'not_applicable':
1392
 
            # This is a contents conflict, because none of the available
1393
 
            # functions could merge it.
 
1361
            # No merge hook was able to resolve the situation. Two cases exist:
 
1362
            # a content conflict or a duplicate one.
1394
1363
            result = None
1395
1364
            name = self.tt.final_name(trans_id)
1396
1365
            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))
 
1366
            duplicate = False
 
1367
            inhibit_content_conflict = False
 
1368
            if params.this_kind is None: # file_id is not in THIS
 
1369
                # Is the name used for a different file_id ?
 
1370
                dupe_path = self.other_tree.id2path(file_id)
 
1371
                this_id = self.this_tree.path2id(dupe_path)
 
1372
                if this_id is not None:
 
1373
                    # Two entries for the same path
 
1374
                    keep_this = True
 
1375
                    # versioning the merged file will trigger a duplicate
 
1376
                    # conflict
 
1377
                    self.tt.version_file(file_id, trans_id)
 
1378
                    transform.create_from_tree(
 
1379
                        self.tt, trans_id, self.other_tree, file_id,
 
1380
                        filter_tree_path=self._get_filter_tree_path(file_id))
 
1381
                    inhibit_content_conflict = True
 
1382
            elif params.other_kind is None: # file_id is not in OTHER
 
1383
                # Is the name used for a different file_id ?
 
1384
                dupe_path = self.this_tree.id2path(file_id)
 
1385
                other_id = self.other_tree.path2id(dupe_path)
 
1386
                if other_id is not None:
 
1387
                    # Two entries for the same path again, but here, the other
 
1388
                    # entry will also be merged.  We simply inhibit the
 
1389
                    # 'content' conflict creation because we know OTHER will
 
1390
                    # create (or has already created depending on ordering) an
 
1391
                    # entry at the same path. This will trigger a 'duplicate'
 
1392
                    # conflict later.
 
1393
                    keep_this = True
 
1394
                    inhibit_content_conflict = True
 
1395
            if not inhibit_content_conflict:
 
1396
                if params.this_kind is not None:
 
1397
                    self.tt.unversion_file(trans_id)
 
1398
                # This is a contents conflict, because none of the available
 
1399
                # functions could merge it.
 
1400
                file_group = self._dump_conflicts(name, parent_id, file_id,
 
1401
                                                  set_version=True)
 
1402
                self._raw_conflicts.append(('contents conflict', file_group))
1402
1403
        elif hook_status == 'success':
1403
1404
            self.tt.create_file(lines, trans_id)
1404
1405
        elif hook_status == 'conflicted':
1420
1421
            raise AssertionError('unknown hook_status: %r' % (hook_status,))
1421
1422
        if not self.this_tree.has_id(file_id) and result == "modified":
1422
1423
            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)
 
1424
        if not keep_this:
 
1425
            # The merge has been performed and produced a new content, so the
 
1426
            # old contents should not be retained.
 
1427
            self.tt.delete_contents(trans_id)
1426
1428
        return result
1427
1429
 
1428
1430
    def _default_other_winner_merge(self, merge_hook_params):
1429
1431
        """Replace this contents with other."""
1430
1432
        file_id = merge_hook_params.file_id
1431
1433
        trans_id = merge_hook_params.trans_id
1432
 
        file_in_this = self.this_tree.has_id(file_id)
1433
1434
        if self.other_tree.has_id(file_id):
1434
1435
            # 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)
 
1436
            transform.create_from_tree(
 
1437
                self.tt, trans_id, self.other_tree, file_id,
 
1438
                filter_tree_path=self._get_filter_tree_path(file_id))
1451
1439
            return 'done', None
1452
 
        elif file_in_this:
 
1440
        elif self.this_tree.has_id(file_id):
1453
1441
            # OTHER deleted the file
1454
1442
            return 'delete', None
1455
1443
        else:
1529
1517
                                              other_lines)
1530
1518
            file_group.append(trans_id)
1531
1519
 
 
1520
 
 
1521
    def _get_filter_tree_path(self, file_id):
 
1522
        if self.this_tree.supports_content_filtering():
 
1523
            # We get the path from the working tree if it exists.
 
1524
            # That fails though when OTHER is adding a file, so
 
1525
            # we fall back to the other tree to find the path if
 
1526
            # it doesn't exist locally.
 
1527
            try:
 
1528
                return self.this_tree.id2path(file_id)
 
1529
            except errors.NoSuchId:
 
1530
                return self.other_tree.id2path(file_id)
 
1531
        # Skip the id2path lookup for older formats
 
1532
        return None
 
1533
 
1532
1534
    def _dump_conflicts(self, name, parent_id, file_id, this_lines=None,
1533
1535
                        base_lines=None, other_lines=None, set_version=False,
1534
1536
                        no_base=False):
1635
1637
                if other_parent is None or other_name is None:
1636
1638
                    other_path = '<deleted>'
1637
1639
                else:
1638
 
                    parent_path =  fp.get_path(
1639
 
                        self.tt.trans_id_file_id(other_parent))
 
1640
                    if other_parent == self.other_tree.get_root_id():
 
1641
                        # The tree transform doesn't know about the other root,
 
1642
                        # so we special case here to avoid a NoFinalPath
 
1643
                        # exception
 
1644
                        parent_path = ''
 
1645
                    else:
 
1646
                        parent_path =  fp.get_path(
 
1647
                            self.tt.trans_id_file_id(other_parent))
1640
1648
                    other_path = osutils.pathjoin(parent_path, other_name)
1641
1649
                c = _mod_conflicts.Conflict.factory(
1642
1650
                    'path conflict', path=this_path,
1646
1654
                for trans_id in conflict[1]:
1647
1655
                    file_id = self.tt.final_file_id(trans_id)
1648
1656
                    if file_id is not None:
 
1657
                        # Ok we found the relevant file-id
1649
1658
                        break
1650
1659
                path = fp.get_path(trans_id)
1651
1660
                for suffix in ('.BASE', '.THIS', '.OTHER'):
1652
1661
                    if path.endswith(suffix):
 
1662
                        # Here is the raw path
1653
1663
                        path = path[:-len(suffix)]
1654
1664
                        break
1655
1665
                c = _mod_conflicts.Conflict.factory(conflict_type,
1900
1910
            entries = self._entries_to_incorporate()
1901
1911
            entries = list(entries)
1902
1912
            for num, (entry, parent_id) in enumerate(entries):
1903
 
                child_pb.update('Preparing file merge', num, len(entries))
 
1913
                child_pb.update(gettext('Preparing file merge'), num, len(entries))
1904
1914
                parent_trans_id = self.tt.trans_id_file_id(parent_id)
1905
1915
                trans_id = transform.new_by_entry(self.tt, entry,
1906
1916
                    parent_trans_id, self.other_tree)
1990
2000
    merger.set_base_revision(get_revision_id(), this_branch)
1991
2001
    return merger.do_merge()
1992
2002
 
 
2003
 
 
2004
merge_type_registry = registry.Registry()
 
2005
merge_type_registry.register('diff3', Diff3Merger,
 
2006
                             "Merge using external diff3.")
 
2007
merge_type_registry.register('lca', LCAMerger,
 
2008
                             "LCA-newness merge.")
 
2009
merge_type_registry.register('merge3', Merge3Merger,
 
2010
                             "Native diff3-style merge.")
 
2011
merge_type_registry.register('weave', WeaveMerger,
 
2012
                             "Weave-based merge.")
 
2013
 
 
2014
 
1993
2015
def get_merge_type_registry():
1994
 
    """Merge type registry is in bzrlib.option to avoid circular imports.
 
2016
    """Merge type registry was previously in bzrlib.option
1995
2017
 
1996
 
    This method provides a sanctioned way to retrieve it.
 
2018
    This method provides a backwards compatible way to retrieve it.
1997
2019
    """
1998
 
    from bzrlib import option
1999
 
    return option._merge_type_registry
 
2020
    return merge_type_registry
2000
2021
 
2001
2022
 
2002
2023
def _plan_annotate_merge(annotated_a, annotated_b, ancestors_a, ancestors_b):