~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

  • Committer: Jelmer Vernooij
  • Date: 2010-08-28 11:19:49 UTC
  • mto: This revision was merged to the branch mainline in revision 5418.
  • Revision ID: jelmer@samba.org-20100828111949-6ke9opiop2oomr4f
Move get_config to ControlDir.

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
    )
 
40
from bzrlib.cleanup import OperationWithCleanups
41
41
""")
42
42
from bzrlib import (
43
43
    decorators,
53
53
 
54
54
def transform_tree(from_tree, to_tree, interesting_ids=None):
55
55
    from_tree.lock_tree_write()
56
 
    operation = cleanup.OperationWithCleanups(merge_inner)
 
56
    operation = OperationWithCleanups(merge_inner)
57
57
    operation.add_cleanup(from_tree.unlock)
58
58
    operation.run_simple(from_tree.branch, to_tree, from_tree,
59
59
        ignore_zero=True, interesting_ids=interesting_ids, this_tree=from_tree)
62
62
class MergeHooks(hooks.Hooks):
63
63
 
64
64
    def __init__(self):
65
 
        hooks.Hooks.__init__(self, "bzrlib.merge", "Merger.hooks")
66
 
        self.add_hook('merge_file_content',
 
65
        hooks.Hooks.__init__(self)
 
66
        self.create_hook(hooks.HookPoint('merge_file_content',
67
67
            "Called with a bzrlib.merge.Merger object to create a per file "
68
68
            "merge object when starting a merge. "
69
69
            "Should return either None or a subclass of "
73
73
            "side has deleted the file and the other has changed it). "
74
74
            "See the AbstractPerFileMerger API docs for details on how it is "
75
75
            "used by merge.",
76
 
            (2, 1))
 
76
            (2, 1), None))
77
77
 
78
78
 
79
79
class AbstractPerFileMerger(object):
458
458
    @deprecated_method(deprecated_in((2, 1, 0)))
459
459
    def file_revisions(self, file_id):
460
460
        self.ensure_revision_trees()
 
461
        def get_id(tree, file_id):
 
462
            revision_id = tree.inventory[file_id].revision
 
463
            return revision_id
461
464
        if self.this_rev_id is None:
462
465
            if self.this_basis_tree.get_file_sha1(file_id) != \
463
466
                self.this_tree.get_file_sha1(file_id):
464
467
                raise errors.WorkingTreeNotRevision(self.this_tree)
465
468
 
466
469
        trees = (self.this_basis_tree, self.other_tree)
467
 
        return [tree.get_file_revision(file_id) for tree in trees]
 
470
        return [get_id(tree, file_id) for tree in trees]
468
471
 
469
472
    @deprecated_method(deprecated_in((2, 1, 0)))
470
473
    def check_basis(self, check_clean, require_commits=True):
498
501
    def _add_parent(self):
499
502
        new_parents = self.this_tree.get_parent_ids() + [self.other_rev_id]
500
503
        new_parent_trees = []
501
 
        operation = cleanup.OperationWithCleanups(
502
 
            self.this_tree.set_parent_trees)
 
504
        operation = OperationWithCleanups(self.this_tree.set_parent_trees)
503
505
        for revision_id in new_parents:
504
506
            try:
505
507
                tree = self.revision_tree(revision_id)
580
582
            elif len(lcas) == 1:
581
583
                self.base_rev_id = list(lcas)[0]
582
584
            else: # len(lcas) > 1
583
 
                self._is_criss_cross = True
584
585
                if len(lcas) > 2:
585
586
                    # find_unique_lca can only handle 2 nodes, so we have to
586
587
                    # start back at the beginning. It is a shame to traverse
591
592
                else:
592
593
                    self.base_rev_id = self.revision_graph.find_unique_lca(
593
594
                                            *lcas)
594
 
                sorted_lca_keys = self.revision_graph.find_merge_order(                
595
 
                    revisions[0], lcas)
596
 
                if self.base_rev_id == _mod_revision.NULL_REVISION:
597
 
                    self.base_rev_id = sorted_lca_keys[0]
598
 
                
 
595
                self._is_criss_cross = True
599
596
            if self.base_rev_id == _mod_revision.NULL_REVISION:
600
597
                raise errors.UnrelatedBranches()
601
598
            if self._is_criss_cross:
602
599
                trace.warning('Warning: criss-cross merge encountered.  See bzr'
603
600
                              ' help criss-cross.')
604
601
                trace.mutter('Criss-cross lcas: %r' % lcas)
605
 
                if self.base_rev_id in lcas:
606
 
                    trace.mutter('Unable to find unique lca. '
607
 
                                 'Fallback %r as best option.' % self.base_rev_id)
608
 
                interesting_revision_ids = set(lcas)
609
 
                interesting_revision_ids.add(self.base_rev_id)
 
602
                interesting_revision_ids = [self.base_rev_id]
 
603
                interesting_revision_ids.extend(lcas)
610
604
                interesting_trees = dict((t.get_revision_id(), t)
611
605
                    for t in self.this_branch.repository.revision_trees(
612
606
                        interesting_revision_ids))
613
607
                self._cached_trees.update(interesting_trees)
614
 
                if self.base_rev_id in lcas:
615
 
                    self.base_tree = interesting_trees[self.base_rev_id]
616
 
                else:
617
 
                    self.base_tree = interesting_trees.pop(self.base_rev_id)
 
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)
618
611
                self._lca_trees = [interesting_trees[key]
619
612
                                   for key in sorted_lca_keys]
620
613
            else:
699
692
        return merge
700
693
 
701
694
    def do_merge(self):
702
 
        operation = cleanup.OperationWithCleanups(self._do_merge_to)
 
695
        operation = OperationWithCleanups(self._do_merge_to)
703
696
        self.this_tree.lock_tree_write()
704
697
        operation.add_cleanup(self.this_tree.unlock)
705
698
        if self.base_tree is not None:
811
804
            warnings.warn("pb argument to Merge3Merger is deprecated")
812
805
 
813
806
    def do_merge(self):
814
 
        operation = cleanup.OperationWithCleanups(self._do_merge)
 
807
        operation = OperationWithCleanups(self._do_merge)
815
808
        self.this_tree.lock_tree_write()
816
809
        operation.add_cleanup(self.this_tree.unlock)
817
810
        self.base_tree.lock_read()
832
825
            pass
833
826
 
834
827
    def make_preview_transform(self):
835
 
        operation = cleanup.OperationWithCleanups(self._make_preview_transform)
 
828
        operation = OperationWithCleanups(self._make_preview_transform)
836
829
        self.base_tree.lock_read()
837
830
        operation.add_cleanup(self.base_tree.unlock)
838
831
        self.other_tree.lock_read()
901
894
        """
902
895
        result = []
903
896
        iterator = self.other_tree.iter_changes(self.base_tree,
904
 
                specific_files=self.interesting_files,
 
897
                include_unchanged=True, specific_files=self.interesting_files,
905
898
                extra_trees=[self.this_tree])
906
899
        this_entries = dict((e.file_id, e) for p, e in
907
900
                            self.this_tree.iter_entries_by_dir(
1092
1085
        return result
1093
1086
 
1094
1087
    def fix_root(self):
1095
 
        if self.tt.final_kind(self.tt.root) is None:
 
1088
        try:
 
1089
            self.tt.final_kind(self.tt.root)
 
1090
        except errors.NoSuchFile:
1096
1091
            self.tt.cancel_deletion(self.tt.root)
1097
1092
        if self.tt.final_file_id(self.tt.root) is None:
1098
1093
            self.tt.version_file(self.tt.tree_file_id(self.tt.root),
1107
1102
            # the other tree's root is a non-root in the current tree (as when
1108
1103
            # a previously unrelated branch is merged into another)
1109
1104
            return
1110
 
        if self.tt.final_kind(other_root) is not None:
 
1105
        try:
 
1106
            self.tt.final_kind(other_root)
1111
1107
            other_root_is_present = True
1112
 
        else:
 
1108
        except errors.NoSuchFile:
1113
1109
            # other_root doesn't have a physical representation. We still need
1114
1110
            # to move any references to the actual root of the tree.
1115
1111
            other_root_is_present = False
1116
1112
        # 'other_tree.inventory.root' is not present in this tree. We are
1117
1113
        # calling adjust_path for children which *want* to be present with a
1118
1114
        # correct place to go.
1119
 
        for _, child in self.other_tree.inventory.root.children.iteritems():
 
1115
        for thing, child in self.other_tree.inventory.root.children.iteritems():
1120
1116
            trans_id = self.tt.trans_id_file_id(child.file_id)
1121
1117
            if not other_root_is_present:
1122
 
                if self.tt.final_kind(trans_id) is not None:
 
1118
                # FIXME: Make final_kind returns None instead of raising
 
1119
                # NoSuchFile to avoid the ugly construct below -- vila 20100402
 
1120
                try:
 
1121
                    self.tt.final_kind(trans_id)
1123
1122
                    # The item exist in the final tree and has a defined place
1124
1123
                    # to go already.
1125
1124
                    continue
 
1125
                except errors.NoSuchFile, e:
 
1126
                    pass
1126
1127
            # Move the item into the root
1127
 
            try:
1128
 
                final_name = self.tt.final_name(trans_id)
1129
 
            except errors.NoFinalPath:
1130
 
                # This file is not present anymore, ignore it.
1131
 
                continue
1132
 
            self.tt.adjust_path(final_name, self.tt.root, trans_id)
 
1128
            self.tt.adjust_path(self.tt.final_name(trans_id),
 
1129
                                self.tt.root, trans_id)
1133
1130
        if other_root_is_present:
1134
1131
            self.tt.cancel_creation(other_root)
1135
1132
            self.tt.cancel_versioning(other_root)
1408
1405
            self.tt.version_file(file_id, trans_id)
1409
1406
        # The merge has been performed, so the old contents should not be
1410
1407
        # retained.
1411
 
        self.tt.delete_contents(trans_id)
 
1408
        try:
 
1409
            self.tt.delete_contents(trans_id)
 
1410
        except errors.NoSuchFile:
 
1411
            pass
1412
1412
        return result
1413
1413
 
1414
1414
    def _default_other_winner_merge(self, merge_hook_params):
1586
1586
        if winner == 'this' and file_status != "modified":
1587
1587
            return
1588
1588
        trans_id = self.tt.trans_id_file_id(file_id)
1589
 
        if self.tt.final_kind(trans_id) != "file":
 
1589
        try:
 
1590
            if self.tt.final_kind(trans_id) != "file":
 
1591
                return
 
1592
        except errors.NoSuchFile:
1590
1593
            return
1591
1594
        if winner == "this":
1592
1595
            executability = this_executable