~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-04-18 04:55:00 UTC
  • mfrom: (5784.2.1 754188-apport-test)
  • Revision ID: pqm@pqm.ubuntu.com-20110418045500-ce6lkgyiq7f47q43
(mbp) Rewrite test_report_bug_legacy away from using doctest (see bug
 764188) (Martin Pool)

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
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 = OperationWithCleanups(merge_inner)
 
56
    operation = cleanup.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)
66
 
        self.create_hook(hooks.HookPoint('merge_file_content',
 
65
        hooks.Hooks.__init__(self, "bzrlib.merge", "Merger.hooks")
 
66
        self.add_hook('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), None))
 
76
            (2, 1))
77
77
 
78
78
 
79
79
class AbstractPerFileMerger(object):
501
501
    def _add_parent(self):
502
502
        new_parents = self.this_tree.get_parent_ids() + [self.other_rev_id]
503
503
        new_parent_trees = []
504
 
        operation = OperationWithCleanups(self.this_tree.set_parent_trees)
 
504
        operation = cleanup.OperationWithCleanups(
 
505
            self.this_tree.set_parent_trees)
505
506
        for revision_id in new_parents:
506
507
            try:
507
508
                tree = self.revision_tree(revision_id)
565
566
 
566
567
    def _maybe_fetch(self, source, target, revision_id):
567
568
        if not source.repository.has_same_location(target.repository):
568
 
            target.fetch(source, revision_id)
 
569
            try:
 
570
                tags_to_fetch = set(source.tags.get_reverse_tag_dict())
 
571
            except errors.TagsNotSupported:
 
572
                tags_to_fetch = None
 
573
            fetch_spec = _mod_graph.NotInOtherForRevs(target.repository,
 
574
                source.repository, [revision_id],
 
575
                if_present_ids=tags_to_fetch).execute()
 
576
            target.fetch(source, fetch_spec=fetch_spec)
569
577
 
570
578
    def find_base(self):
571
579
        revisions = [_mod_revision.ensure_null(self.this_basis),
582
590
            elif len(lcas) == 1:
583
591
                self.base_rev_id = list(lcas)[0]
584
592
            else: # len(lcas) > 1
 
593
                self._is_criss_cross = True
585
594
                if len(lcas) > 2:
586
595
                    # find_unique_lca can only handle 2 nodes, so we have to
587
596
                    # start back at the beginning. It is a shame to traverse
592
601
                else:
593
602
                    self.base_rev_id = self.revision_graph.find_unique_lca(
594
603
                                            *lcas)
595
 
                self._is_criss_cross = True
 
604
                sorted_lca_keys = self.revision_graph.find_merge_order(                
 
605
                    revisions[0], lcas)
 
606
                if self.base_rev_id == _mod_revision.NULL_REVISION:
 
607
                    self.base_rev_id = sorted_lca_keys[0]
 
608
                
596
609
            if self.base_rev_id == _mod_revision.NULL_REVISION:
597
610
                raise errors.UnrelatedBranches()
598
611
            if self._is_criss_cross:
599
612
                trace.warning('Warning: criss-cross merge encountered.  See bzr'
600
613
                              ' help criss-cross.')
601
614
                trace.mutter('Criss-cross lcas: %r' % lcas)
602
 
                interesting_revision_ids = [self.base_rev_id]
603
 
                interesting_revision_ids.extend(lcas)
 
615
                if self.base_rev_id in lcas:
 
616
                    trace.mutter('Unable to find unique lca. '
 
617
                                 'Fallback %r as best option.' % self.base_rev_id)
 
618
                interesting_revision_ids = set(lcas)
 
619
                interesting_revision_ids.add(self.base_rev_id)
604
620
                interesting_trees = dict((t.get_revision_id(), t)
605
621
                    for t in self.this_branch.repository.revision_trees(
606
622
                        interesting_revision_ids))
607
623
                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)
 
624
                if self.base_rev_id in lcas:
 
625
                    self.base_tree = interesting_trees[self.base_rev_id]
 
626
                else:
 
627
                    self.base_tree = interesting_trees.pop(self.base_rev_id)
611
628
                self._lca_trees = [interesting_trees[key]
612
629
                                   for key in sorted_lca_keys]
613
630
            else:
692
709
        return merge
693
710
 
694
711
    def do_merge(self):
695
 
        operation = OperationWithCleanups(self._do_merge_to)
 
712
        operation = cleanup.OperationWithCleanups(self._do_merge_to)
696
713
        self.this_tree.lock_tree_write()
697
714
        operation.add_cleanup(self.this_tree.unlock)
698
715
        if self.base_tree is not None:
804
821
            warnings.warn("pb argument to Merge3Merger is deprecated")
805
822
 
806
823
    def do_merge(self):
807
 
        operation = OperationWithCleanups(self._do_merge)
 
824
        operation = cleanup.OperationWithCleanups(self._do_merge)
808
825
        self.this_tree.lock_tree_write()
809
826
        operation.add_cleanup(self.this_tree.unlock)
810
827
        self.base_tree.lock_read()
825
842
            pass
826
843
 
827
844
    def make_preview_transform(self):
828
 
        operation = OperationWithCleanups(self._make_preview_transform)
 
845
        operation = cleanup.OperationWithCleanups(self._make_preview_transform)
829
846
        self.base_tree.lock_read()
830
847
        operation.add_cleanup(self.base_tree.unlock)
831
848
        self.other_tree.lock_read()
894
911
        """
895
912
        result = []
896
913
        iterator = self.other_tree.iter_changes(self.base_tree,
897
 
                include_unchanged=True, specific_files=self.interesting_files,
 
914
                specific_files=self.interesting_files,
898
915
                extra_trees=[self.this_tree])
899
916
        this_entries = dict((e.file_id, e) for p, e in
900
917
                            self.this_tree.iter_entries_by_dir(
1109
1126
        # 'other_tree.inventory.root' is not present in this tree. We are
1110
1127
        # calling adjust_path for children which *want* to be present with a
1111
1128
        # correct place to go.
1112
 
        for thing, child in self.other_tree.inventory.root.children.iteritems():
 
1129
        for _, child in self.other_tree.inventory.root.children.iteritems():
1113
1130
            trans_id = self.tt.trans_id_file_id(child.file_id)
1114
1131
            if not other_root_is_present:
1115
1132
                if self.tt.final_kind(trans_id) is not None:
1117
1134
                    # to go already.
1118
1135
                    continue
1119
1136
            # Move the item into the root
1120
 
            self.tt.adjust_path(self.tt.final_name(trans_id),
1121
 
                                self.tt.root, trans_id)
 
1137
            try:
 
1138
                final_name = self.tt.final_name(trans_id)
 
1139
            except errors.NoFinalPath:
 
1140
                # This file is not present anymore, ignore it.
 
1141
                continue
 
1142
            self.tt.adjust_path(final_name, self.tt.root, trans_id)
1122
1143
        if other_root_is_present:
1123
1144
            self.tt.cancel_creation(other_root)
1124
1145
            self.tt.cancel_versioning(other_root)