~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-01-13 02:43:21 UTC
  • mfrom: (5582.9.2 funky-chars)
  • Revision ID: pqm@pqm.ubuntu.com-20110113024321-d1ssmy4knbv806zp
(jelmer) Avoid hardcoded list of repository formats that do not support
 funky characters in filenames. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2010 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,
24
23
    conflicts as _mod_conflicts,
25
24
    debug,
26
25
    generate_ids,
38
37
    versionedfile,
39
38
    workingtree,
40
39
    )
41
 
from bzrlib.i18n import gettext
 
40
from bzrlib.cleanup import OperationWithCleanups
42
41
""")
43
42
from bzrlib import (
44
43
    decorators,
54
53
 
55
54
def transform_tree(from_tree, to_tree, interesting_ids=None):
56
55
    from_tree.lock_tree_write()
57
 
    operation = cleanup.OperationWithCleanups(merge_inner)
 
56
    operation = OperationWithCleanups(merge_inner)
58
57
    operation.add_cleanup(from_tree.unlock)
59
58
    operation.run_simple(from_tree.branch, to_tree, from_tree,
60
59
        ignore_zero=True, interesting_ids=interesting_ids, this_tree=from_tree)
63
62
class MergeHooks(hooks.Hooks):
64
63
 
65
64
    def __init__(self):
66
 
        hooks.Hooks.__init__(self, "bzrlib.merge", "Merger.hooks")
67
 
        self.add_hook('merge_file_content',
 
65
        hooks.Hooks.__init__(self)
 
66
        self.create_hook(hooks.HookPoint('merge_file_content',
68
67
            "Called with a bzrlib.merge.Merger object to create a per file "
69
68
            "merge object when starting a merge. "
70
69
            "Should return either None or a subclass of "
74
73
            "side has deleted the file and the other has changed it). "
75
74
            "See the AbstractPerFileMerger API docs for details on how it is "
76
75
            "used by merge.",
77
 
            (2, 1))
 
76
            (2, 1), None))
78
77
 
79
78
 
80
79
class AbstractPerFileMerger(object):
93
92
        """Attempt to merge the contents of a single file.
94
93
        
95
94
        :param merge_params: A bzrlib.merge.MergeHookParams
96
 
        :return: A tuple of (status, chunks), where status is one of
 
95
        :return : A tuple of (status, chunks), where status is one of
97
96
            'not_applicable', 'success', 'conflicted', or 'delete'.  If status
98
97
            is 'success' or 'conflicted', then chunks should be an iterable of
99
98
            strings for the new file contents.
459
458
    @deprecated_method(deprecated_in((2, 1, 0)))
460
459
    def file_revisions(self, file_id):
461
460
        self.ensure_revision_trees()
 
461
        def get_id(tree, file_id):
 
462
            revision_id = tree.inventory[file_id].revision
 
463
            return revision_id
462
464
        if self.this_rev_id is None:
463
465
            if self.this_basis_tree.get_file_sha1(file_id) != \
464
466
                self.this_tree.get_file_sha1(file_id):
465
467
                raise errors.WorkingTreeNotRevision(self.this_tree)
466
468
 
467
469
        trees = (self.this_basis_tree, self.other_tree)
468
 
        return [tree.get_file_revision(file_id) for tree in trees]
 
470
        return [get_id(tree, file_id) for tree in trees]
469
471
 
470
472
    @deprecated_method(deprecated_in((2, 1, 0)))
471
473
    def check_basis(self, check_clean, require_commits=True):
499
501
    def _add_parent(self):
500
502
        new_parents = self.this_tree.get_parent_ids() + [self.other_rev_id]
501
503
        new_parent_trees = []
502
 
        operation = cleanup.OperationWithCleanups(
503
 
            self.this_tree.set_parent_trees)
 
504
        operation = OperationWithCleanups(self.this_tree.set_parent_trees)
504
505
        for revision_id in new_parents:
505
506
            try:
506
507
                tree = self.revision_tree(revision_id)
700
701
        return merge
701
702
 
702
703
    def do_merge(self):
703
 
        operation = cleanup.OperationWithCleanups(self._do_merge_to)
 
704
        operation = OperationWithCleanups(self._do_merge_to)
704
705
        self.this_tree.lock_tree_write()
705
706
        operation.add_cleanup(self.this_tree.unlock)
706
707
        if self.base_tree is not None:
712
713
        merge = operation.run_simple()
713
714
        if len(merge.cooked_conflicts) == 0:
714
715
            if not self.ignore_zero and not trace.is_quiet():
715
 
                trace.note(gettext("All changes applied successfully."))
 
716
                trace.note("All changes applied successfully.")
716
717
        else:
717
 
            trace.note(gettext("%d conflicts encountered.")
 
718
            trace.note("%d conflicts encountered."
718
719
                       % len(merge.cooked_conflicts))
719
720
 
720
721
        return len(merge.cooked_conflicts)
812
813
            warnings.warn("pb argument to Merge3Merger is deprecated")
813
814
 
814
815
    def do_merge(self):
815
 
        operation = cleanup.OperationWithCleanups(self._do_merge)
 
816
        operation = OperationWithCleanups(self._do_merge)
816
817
        self.this_tree.lock_tree_write()
817
818
        operation.add_cleanup(self.this_tree.unlock)
818
819
        self.base_tree.lock_read()
833
834
            pass
834
835
 
835
836
    def make_preview_transform(self):
836
 
        operation = cleanup.OperationWithCleanups(self._make_preview_transform)
 
837
        operation = OperationWithCleanups(self._make_preview_transform)
837
838
        self.base_tree.lock_read()
838
839
        operation.add_cleanup(self.base_tree.unlock)
839
840
        self.other_tree.lock_read()
859
860
            self.active_hooks = [hook for hook in hooks if hook is not None]
860
861
            for num, (file_id, changed, parents3, names3,
861
862
                      executable3) in enumerate(entries):
862
 
                child_pb.update(gettext('Preparing file merge'), num, len(entries))
 
863
                child_pb.update('Preparing file merge', num, len(entries))
863
864
                self._merge_names(file_id, parents3, names3, resolver=resolver)
864
865
                if changed:
865
866
                    file_status = self._do_merge_contents(file_id)
869
870
                    executable3, file_status, resolver=resolver)
870
871
        finally:
871
872
            child_pb.finished()
872
 
        self.tt.fixup_new_roots()
 
873
        self.fix_root()
873
874
        self._finish_computing_transform()
874
875
 
875
876
    def _finish_computing_transform(self):
889
890
                self.tt.iter_changes(), self.change_reporter)
890
891
        self.cook_conflicts(fs_conflicts)
891
892
        for conflict in self.cooked_conflicts:
892
 
            trace.warning(unicode(conflict))
 
893
            trace.warning(conflict)
893
894
 
894
895
    def _entries3(self):
895
896
        """Gather data about files modified between three trees.
902
903
        """
903
904
        result = []
904
905
        iterator = self.other_tree.iter_changes(self.base_tree,
905
 
                specific_files=self.interesting_files,
 
906
                include_unchanged=True, specific_files=self.interesting_files,
906
907
                extra_trees=[self.this_tree])
907
908
        this_entries = dict((e.file_id, e) for p, e in
908
909
                            self.this_tree.iter_entries_by_dir(
934
935
        it then compares with THIS and BASE.
935
936
 
936
937
        For the multi-valued entries, the format will be (BASE, [lca1, lca2])
937
 
 
938
 
        :return: [(file_id, changed, parents, names, executable)], where:
939
 
 
940
 
            * file_id: Simple file_id of the entry
941
 
            * changed: Boolean, True if the kind or contents changed else False
942
 
            * parents: ((base, [parent_id, in, lcas]), parent_id_other,
943
 
                        parent_id_this)
944
 
            * names:   ((base, [name, in, lcas]), name_in_other, name_in_this)
945
 
            * executable: ((base, [exec, in, lcas]), exec_in_other,
946
 
                        exec_in_this)
 
938
        :return: [(file_id, changed, parents, names, executable)]
 
939
            file_id     Simple file_id of the entry
 
940
            changed     Boolean, True if the kind or contents changed
 
941
                        else False
 
942
            parents     ((base, [parent_id, in, lcas]), parent_id_other,
 
943
                         parent_id_this)
 
944
            names       ((base, [name, in, lcas]), name_in_other, name_in_this)
 
945
            executable  ((base, [exec, in, lcas]), exec_in_other, exec_in_this)
947
946
        """
948
947
        if self.interesting_files is not None:
949
948
            lookup_trees = [self.this_tree, self.base_tree]
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
1094
1093
                          ))
1095
1094
        return result
1096
1095
 
1097
 
    @deprecated_method(deprecated_in((2, 4, 0)))
1098
1096
    def fix_root(self):
1099
1097
        if self.tt.final_kind(self.tt.root) is None:
1100
1098
            self.tt.cancel_deletion(self.tt.root)
1107
1105
        other_root = self.tt.trans_id_file_id(other_root_file_id)
1108
1106
        if other_root == self.tt.root:
1109
1107
            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)
 
1108
        if self.other_tree.inventory.root.file_id in self.this_tree.inventory:
 
1109
            # the other tree's root is a non-root in the current tree (as when
 
1110
            # a previously unrelated branch is merged into another)
1114
1111
            return
1115
1112
        if self.tt.final_kind(other_root) is not None:
1116
1113
            other_root_is_present = True
1121
1118
        # 'other_tree.inventory.root' is not present in this tree. We are
1122
1119
        # calling adjust_path for children which *want* to be present with a
1123
1120
        # correct place to go.
1124
 
        for _, child in self.other_tree.inventory.root.children.iteritems():
 
1121
        for thing, child in self.other_tree.inventory.root.children.iteritems():
1125
1122
            trans_id = self.tt.trans_id_file_id(child.file_id)
1126
1123
            if not other_root_is_present:
1127
1124
                if self.tt.final_kind(trans_id) is not None:
1129
1126
                    # to go already.
1130
1127
                    continue
1131
1128
            # Move the item into the root
1132
 
            try:
1133
 
                final_name = self.tt.final_name(trans_id)
1134
 
            except errors.NoFinalPath:
1135
 
                # This file is not present anymore, ignore it.
1136
 
                continue
1137
 
            self.tt.adjust_path(final_name, self.tt.root, trans_id)
 
1129
            self.tt.adjust_path(self.tt.final_name(trans_id),
 
1130
                                self.tt.root, trans_id)
1138
1131
        if other_root_is_present:
1139
1132
            self.tt.cancel_creation(other_root)
1140
1133
            self.tt.cancel_versioning(other_root)
1168
1161
    @staticmethod
1169
1162
    def contents_sha1(tree, file_id):
1170
1163
        """Determine the sha1 of the file contents (used as a key method)."""
1171
 
        if not tree.has_id(file_id):
 
1164
        if file_id not in tree:
1172
1165
            return None
1173
1166
        return tree.get_file_sha1(file_id)
1174
1167
 
1319
1312
            self._raw_conflicts.append(('path conflict', trans_id, file_id,
1320
1313
                                        this_parent, this_name,
1321
1314
                                        other_parent, other_name))
1322
 
        if not self.other_tree.has_id(file_id):
 
1315
        if other_name is None:
1323
1316
            # it doesn't matter whether the result was 'other' or
1324
 
            # 'conflict'-- if it has no file id, we leave it alone.
 
1317
            # 'conflict'-- if there's no 'other', we leave it alone.
1325
1318
            return
1326
1319
        parent_id = parents[self.winner_idx[parent_id_winner]]
1327
 
        name = names[self.winner_idx[name_winner]]
1328
 
        if parent_id is not None or name is not None:
 
1320
        if parent_id is not None:
1329
1321
            # if we get here, name_winner and parent_winner are set to safe
1330
1322
            # values.
1331
 
            if parent_id is None and name is not None:
1332
 
                # if parent_id is None and name is non-None, current file is
1333
 
                # the tree root.
1334
 
                if names[self.winner_idx[parent_id_winner]] != '':
1335
 
                    raise AssertionError(
1336
 
                        'File looks like a root, but named %s' %
1337
 
                        names[self.winner_idx[parent_id_winner]])
1338
 
                parent_trans_id = transform.ROOT_PARENT
1339
 
            else:
1340
 
                parent_trans_id = self.tt.trans_id_file_id(parent_id)
1341
 
            self.tt.adjust_path(name, parent_trans_id,
 
1323
            self.tt.adjust_path(names[self.winner_idx[name_winner]],
 
1324
                                self.tt.trans_id_file_id(parent_id),
1342
1325
                                self.tt.trans_id_file_id(file_id))
1343
1326
 
1344
1327
    def _do_merge_contents(self, file_id):
1345
1328
        """Performs a merge on file_id contents."""
1346
1329
        def contents_pair(tree):
1347
 
            if not tree.has_id(file_id):
 
1330
            if file_id not in tree:
1348
1331
                return (None, None)
1349
1332
            kind = tree.kind(file_id)
1350
1333
            if kind == "file":
1618
1601
 
1619
1602
    def cook_conflicts(self, fs_conflicts):
1620
1603
        """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)
 
1604
        self.cooked_conflicts.extend(transform.cook_conflicts(
 
1605
                fs_conflicts, self.tt))
1623
1606
        fp = transform.FinalPaths(self.tt)
1624
1607
        for conflict in self._raw_conflicts:
1625
1608
            conflict_type = conflict[0]
1636
1619
                if other_parent is None or other_name is None:
1637
1620
                    other_path = '<deleted>'
1638
1621
                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))
 
1622
                    parent_path =  fp.get_path(
 
1623
                        self.tt.trans_id_file_id(other_parent))
1647
1624
                    other_path = osutils.pathjoin(parent_path, other_name)
1648
1625
                c = _mod_conflicts.Conflict.factory(
1649
1626
                    'path conflict', path=this_path,
1661
1638
                        break
1662
1639
                c = _mod_conflicts.Conflict.factory(conflict_type,
1663
1640
                                                    path=path, file_id=file_id)
1664
 
                content_conflict_file_ids.add(file_id)
1665
1641
            elif conflict_type == 'text conflict':
1666
1642
                trans_id = conflict[1]
1667
1643
                path = fp.get_path(trans_id)
1670
1646
                                                    path=path, file_id=file_id)
1671
1647
            else:
1672
1648
                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
1649
            self.cooked_conflicts.append(c)
1685
1650
        self.cooked_conflicts.sort(key=_mod_conflicts.Conflict.sort_key)
1686
1651
 
1907
1872
            entries = self._entries_to_incorporate()
1908
1873
            entries = list(entries)
1909
1874
            for num, (entry, parent_id) in enumerate(entries):
1910
 
                child_pb.update(gettext('Preparing file merge'), num, len(entries))
 
1875
                child_pb.update('Preparing file merge', num, len(entries))
1911
1876
                parent_trans_id = self.tt.trans_id_file_id(parent_id)
1912
1877
                trans_id = transform.new_by_entry(self.tt, entry,
1913
1878
                    parent_trans_id, self.other_tree)
1931
1896
        name_in_target = osutils.basename(self._target_subdir)
1932
1897
        merge_into_root = subdir.copy()
1933
1898
        merge_into_root.name = name_in_target
1934
 
        if self.this_tree.inventory.has_id(merge_into_root.file_id):
 
1899
        if merge_into_root.file_id in self.this_tree.inventory:
1935
1900
            # Give the root a new file-id.
1936
1901
            # This can happen fairly easily if the directory we are
1937
1902
            # incorporating is the root, and both trees have 'TREE_ROOT' as
1974
1939
    """
1975
1940
    if this_tree is None:
1976
1941
        raise errors.BzrError("bzrlib.merge.merge_inner requires a this_tree "
1977
 
                              "parameter")
 
1942
                              "parameter as of bzrlib version 0.8.")
1978
1943
    merger = Merger(this_branch, other_tree, base_tree, this_tree=this_tree,
1979
1944
                    pb=pb, change_reporter=change_reporter)
1980
1945
    merger.backup_files = backup_files
2434
2399
class _PlanLCAMerge(_PlanMergeBase):
2435
2400
    """
2436
2401
    This merge algorithm differs from _PlanMerge in that:
2437
 
 
2438
2402
    1. comparisons are done against LCAs only
2439
2403
    2. cases where a contested line is new versus one LCA but old versus
2440
2404
       another are marked as conflicts, by emitting the line as conflicted-a
2481
2445
 
2482
2446
        If a line is killed and new, this indicates that the two merge
2483
2447
        revisions contain differing conflict resolutions.
2484
 
 
2485
2448
        :param revision_id: The id of the revision in which the lines are
2486
2449
            unique
2487
2450
        :param unique_line_numbers: The line numbers of unique lines.
2488
 
        :return: a tuple of (new_this, killed_other)
 
2451
        :return a tuple of (new_this, killed_other):
2489
2452
        """
2490
2453
        new = set()
2491
2454
        killed = set()