~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/merge.py

  • Committer: Jelmer Vernooij
  • Date: 2012-01-05 16:03:11 UTC
  • mfrom: (6432 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6433.
  • Revision ID: jelmer@samba.org-20120105160311-12o5p67kin1v3zps
Merge bzr.dev.

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
76
78
            "See the AbstractPerFileMerger API docs for details on how it is "
77
79
            "used by merge.",
78
80
            (2, 1))
 
81
        self.add_hook('pre_merge',
 
82
            'Called before a merge. '
 
83
            'Receives a Merger object as the single argument.',
 
84
            (2, 5))
 
85
        self.add_hook('post_merge',
 
86
            'Called after a merge. '
 
87
            'Receives a Merger object as the single argument. '
 
88
            'The return value is ignored.',
 
89
            (2, 5))
79
90
 
80
91
 
81
92
class AbstractPerFileMerger(object):
93
104
    def merge_contents(self, merge_params):
94
105
        """Attempt to merge the contents of a single file.
95
106
        
96
 
        :param merge_params: A bzrlib.merge.MergeHookParams
 
107
        :param merge_params: A bzrlib.merge.MergeFileHookParams
97
108
        :return: A tuple of (status, chunks), where status is one of
98
109
            'not_applicable', 'success', 'conflicted', or 'delete'.  If status
99
110
            is 'success' or 'conflicted', then chunks should be an iterable of
120
131
 
121
132
    def get_filename(self, params, tree):
122
133
        """Lookup the filename (i.e. basename, not path), given a Tree (e.g.
123
 
        self.merger.this_tree) and a MergeHookParams.
 
134
        self.merger.this_tree) and a MergeFileHookParams.
124
135
        """
125
136
        return osutils.basename(tree.id2path(params.file_id))
126
137
 
127
138
    def get_filepath(self, params, tree):
128
139
        """Calculate the path to the file in a tree.
129
140
 
130
 
        :param params: A MergeHookParams describing the file to merge
 
141
        :param params: A MergeFileHookParams describing the file to merge
131
142
        :param tree: a Tree, e.g. self.merger.this_tree.
132
143
        """
133
144
        return tree.id2path(params.file_id)
222
233
        raise NotImplementedError(self.merge_text)
223
234
 
224
235
 
225
 
class MergeHookParams(object):
 
236
class MergeFileHookParams(object):
226
237
    """Object holding parameters passed to merge_file_content hooks.
227
238
 
228
239
    There are some fields hooks can access:
443
454
        revision_id = _mod_revision.ensure_null(revision_id)
444
455
        return branch, self.revision_tree(revision_id, branch)
445
456
 
446
 
    @deprecated_method(deprecated_in((2, 1, 0)))
447
 
    def ensure_revision_trees(self):
448
 
        if self.this_revision_tree is None:
449
 
            self.this_basis_tree = self.revision_tree(self.this_basis)
450
 
            if self.this_basis == self.this_rev_id:
451
 
                self.this_revision_tree = self.this_basis_tree
452
 
 
453
 
        if self.other_rev_id is None:
454
 
            other_basis_tree = self.revision_tree(self.other_basis)
455
 
            if other_basis_tree.has_changes(self.other_tree):
456
 
                raise errors.WorkingTreeNotRevision(self.this_tree)
457
 
            other_rev_id = self.other_basis
458
 
            self.other_tree = other_basis_tree
459
 
 
460
 
    @deprecated_method(deprecated_in((2, 1, 0)))
461
 
    def file_revisions(self, file_id):
462
 
        self.ensure_revision_trees()
463
 
        if self.this_rev_id is None:
464
 
            if self.this_basis_tree.get_file_sha1(file_id) != \
465
 
                self.this_tree.get_file_sha1(file_id):
466
 
                raise errors.WorkingTreeNotRevision(self.this_tree)
467
 
 
468
 
        trees = (self.this_basis_tree, self.other_tree)
469
 
        return [tree.get_file_revision(file_id) for tree in trees]
470
 
 
471
 
    @deprecated_method(deprecated_in((2, 1, 0)))
472
 
    def check_basis(self, check_clean, require_commits=True):
473
 
        if self.this_basis is None and require_commits is True:
474
 
            raise errors.BzrCommandError(
475
 
                "This branch has no commits."
476
 
                " (perhaps you would prefer 'bzr pull')")
477
 
        if check_clean:
478
 
            self.compare_basis()
479
 
            if self.this_basis != self.this_rev_id:
480
 
                raise errors.UncommittedChanges(self.this_tree)
481
 
 
482
 
    @deprecated_method(deprecated_in((2, 1, 0)))
483
 
    def compare_basis(self):
484
 
        try:
485
 
            basis_tree = self.revision_tree(self.this_tree.last_revision())
486
 
        except errors.NoSuchRevision:
487
 
            basis_tree = self.this_tree.basis_tree()
488
 
        if not self.this_tree.has_changes(basis_tree):
489
 
            self.this_rev_id = self.this_basis
490
 
 
491
457
    def set_interesting_files(self, file_list):
492
458
        self.interesting_files = file_list
493
459
 
538
504
                raise errors.NoCommits(self.other_branch)
539
505
        if self.other_rev_id is not None:
540
506
            self._cached_trees[self.other_rev_id] = self.other_tree
541
 
        self._maybe_fetch(self.other_branch,self.this_branch, self.other_basis)
 
507
        self._maybe_fetch(self.other_branch, self.this_branch, self.other_basis)
542
508
 
543
509
    def set_other_revision(self, revision_id, other_branch):
544
510
        """Set 'other' based on a branch and revision id
646
612
            self._maybe_fetch(base_branch, self.this_branch, self.base_rev_id)
647
613
 
648
614
    def make_merger(self):
649
 
        kwargs = {'working_tree':self.this_tree, 'this_tree': self.this_tree,
 
615
        kwargs = {'working_tree': self.this_tree, 'this_tree': self.this_tree,
650
616
                  'other_tree': self.other_tree,
651
617
                  'interesting_ids': self.interesting_ids,
652
618
                  'interesting_files': self.interesting_files,
653
619
                  'this_branch': self.this_branch,
 
620
                  'other_branch': self.other_branch,
654
621
                  'do_merge': False}
655
622
        if self.merge_type.requires_base:
656
623
            kwargs['base_tree'] = self.base_tree
682
649
        merge = self.make_merger()
683
650
        if self.other_branch is not None:
684
651
            self.other_branch.update_references(self.this_branch)
 
652
        for hook in Merger.hooks['pre_merge']:
 
653
            hook(merge)
685
654
        merge.do_merge()
 
655
        for hook in Merger.hooks['post_merge']:
 
656
            hook(merge)
686
657
        if self.recurse == 'down':
687
658
            for relpath, file_id in self.this_tree.iter_references():
688
659
                sub_tree = self.this_tree.get_nested_tree(file_id, relpath)
755
726
                 interesting_ids=None, reprocess=False, show_base=False,
756
727
                 pb=None, pp=None, change_reporter=None,
757
728
                 interesting_files=None, do_merge=True,
758
 
                 cherrypick=False, lca_trees=None, this_branch=None):
 
729
                 cherrypick=False, lca_trees=None, this_branch=None,
 
730
                 other_branch=None):
759
731
        """Initialize the merger object and perform the merge.
760
732
 
761
733
        :param working_tree: The working tree to apply the merge to
764
736
        :param other_tree: The other tree to merge changes from
765
737
        :param this_branch: The branch associated with this_tree.  Defaults to
766
738
            this_tree.branch if not supplied.
 
739
        :param other_branch: The branch associated with other_tree, if any.
767
740
        :param interesting_ids: The file_ids of files that should be
768
741
            participate in the merge.  May not be combined with
769
742
            interesting_files.
791
764
            this_branch = this_tree.branch
792
765
        self.interesting_ids = interesting_ids
793
766
        self.interesting_files = interesting_files
794
 
        self.this_tree = working_tree
 
767
        self.working_tree = working_tree
 
768
        self.this_tree = this_tree
795
769
        self.base_tree = base_tree
796
770
        self.other_tree = other_tree
797
771
        self.this_branch = this_branch
 
772
        self.other_branch = other_branch
798
773
        self._raw_conflicts = []
799
774
        self.cooked_conflicts = []
800
775
        self.reprocess = reprocess
816
791
 
817
792
    def do_merge(self):
818
793
        operation = cleanup.OperationWithCleanups(self._do_merge)
819
 
        self.this_tree.lock_tree_write()
 
794
        self.working_tree.lock_tree_write()
 
795
        operation.add_cleanup(self.working_tree.unlock)
 
796
        self.this_tree.lock_read()
820
797
        operation.add_cleanup(self.this_tree.unlock)
821
798
        self.base_tree.lock_read()
822
799
        operation.add_cleanup(self.base_tree.unlock)
825
802
        operation.run()
826
803
 
827
804
    def _do_merge(self, operation):
828
 
        self.tt = transform.TreeTransform(self.this_tree, None)
 
805
        self.tt = transform.TreeTransform(self.working_tree, None)
829
806
        operation.add_cleanup(self.tt.finalize)
830
807
        self._compute_transform()
831
808
        results = self.tt.apply(no_conflicts=True)
832
809
        self.write_modified(results)
833
810
        try:
834
 
            self.this_tree.add_conflicts(self.cooked_conflicts)
 
811
            self.working_tree.add_conflicts(self.cooked_conflicts)
835
812
        except errors.UnsupportedOperation:
836
813
            pass
837
814
 
844
821
        return operation.run_simple()
845
822
 
846
823
    def _make_preview_transform(self):
847
 
        self.tt = transform.TransformPreview(self.this_tree)
 
824
        self.tt = transform.TransformPreview(self.working_tree)
848
825
        self._compute_transform()
849
826
        return self.tt
850
827
 
1149
1126
    def write_modified(self, results):
1150
1127
        modified_hashes = {}
1151
1128
        for path in results.modified_paths:
1152
 
            file_id = self.this_tree.path2id(self.this_tree.relpath(path))
 
1129
            file_id = self.working_tree.path2id(self.working_tree.relpath(path))
1153
1130
            if file_id is None:
1154
1131
                continue
1155
 
            hash = self.this_tree.get_file_sha1(file_id)
 
1132
            hash = self.working_tree.get_file_sha1(file_id)
1156
1133
            if hash is None:
1157
1134
                continue
1158
1135
            modified_hashes[file_id] = hash
1159
 
        self.this_tree.set_merge_modified(modified_hashes)
 
1136
        self.working_tree.set_merge_modified(modified_hashes)
1160
1137
 
1161
1138
    @staticmethod
1162
1139
    def parent(entry, file_id):
1386
1363
        # We have a hypothetical conflict, but if we have files, then we
1387
1364
        # can try to merge the content
1388
1365
        trans_id = self.tt.trans_id_file_id(file_id)
1389
 
        params = MergeHookParams(self, file_id, trans_id, this_pair[0],
 
1366
        params = MergeFileHookParams(self, file_id, trans_id, this_pair[0],
1390
1367
            other_pair[0], winner)
1391
1368
        hooks = self.active_hooks
1392
1369
        hook_status = 'not_applicable'