~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: Andrew Bennetts
  • Date: 2008-01-21 23:04:54 UTC
  • mfrom: (3193 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3195.
  • Revision ID: andrew.bennetts@canonical.com-20080121230454-0mdqybl4b2tte6d4
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2759
2759
            bzr merge -r 81..82 ../bzr.dev
2760
2760
    """
2761
2761
 
 
2762
    encoding_type = 'exact'
2762
2763
    _see_also = ['update', 'remerge', 'status-flags']
2763
2764
    takes_args = ['branch?']
2764
2765
    takes_options = [
2783
2784
               short_name='d',
2784
2785
               type=unicode,
2785
2786
               ),
 
2787
        Option('preview', help='Instead of merging, show a diff of the merge.')
2786
2788
    ]
2787
2789
 
2788
2790
    def run(self, branch=None, revision=None, force=False, merge_type=None,
2789
2791
            show_base=False, reprocess=False, remember=False,
2790
2792
            uncommitted=False, pull=False,
2791
2793
            directory=None,
 
2794
            preview=False,
2792
2795
            ):
2793
2796
        # This is actually a branch (or merge-directive) *location*.
2794
2797
        location = branch
2844
2847
            merger.merge_type = merge_type
2845
2848
            merger.reprocess = reprocess
2846
2849
            merger.show_base = show_base
2847
 
            merger.change_reporter = change_reporter
2848
2850
            self.sanity_check_merger(merger)
2849
2851
            if (merger.base_rev_id == merger.other_rev_id and
2850
2852
                merger.other_rev_id != None):
2859
2861
                    result.report(self.outf)
2860
2862
                    return 0
2861
2863
            merger.check_basis(not force)
2862
 
            conflict_count = merger.do_merge()
2863
 
            if allow_pending:
2864
 
                merger.set_pending()
2865
 
            if verified == 'failed':
2866
 
                warning('Preview patch does not match changes')
2867
 
            if conflict_count != 0:
2868
 
                return 1
 
2864
            if preview:
 
2865
                return self._do_preview(merger)
2869
2866
            else:
2870
 
                return 0
 
2867
                return self._do_merge(merger, change_reporter, allow_pending,
 
2868
                                      verified)
2871
2869
        finally:
2872
2870
            for cleanup in reversed(cleanups):
2873
2871
                cleanup()
2874
2872
 
 
2873
    def _do_preview(self, merger):
 
2874
        from bzrlib.diff import show_diff_trees
 
2875
        tree_merger = merger.make_merger()
 
2876
        tt = tree_merger.make_preview_transform()
 
2877
        result_tree = tt.get_preview_tree()
 
2878
        show_diff_trees(merger.this_tree, result_tree, self.outf, old_label='',
 
2879
                        new_label='')
 
2880
 
 
2881
    def _do_merge(self, merger, change_reporter, allow_pending, verified):
 
2882
        merger.change_reporter = change_reporter
 
2883
        conflict_count = merger.do_merge()
 
2884
        if allow_pending:
 
2885
            merger.set_pending()
 
2886
        if verified == 'failed':
 
2887
            warning('Preview patch does not match changes')
 
2888
        if conflict_count != 0:
 
2889
            return 1
 
2890
        else:
 
2891
            return 0
 
2892
 
2875
2893
    def sanity_check_merger(self, merger):
2876
2894
        if (merger.show_base and
2877
2895
            not merger.merge_type is _mod_merge.Merge3Merger):
2891
2909
        from bzrlib.tag import _merge_tags_if_possible
2892
2910
        assert revision is None or len(revision) < 3
2893
2911
        # find the branch locations
2894
 
        other_loc, location = self._select_branch_location(tree, location,
 
2912
        other_loc, user_location = self._select_branch_location(tree, location,
2895
2913
            revision, -1)
2896
2914
        if revision is not None and len(revision) == 2:
2897
 
            base_loc, location = self._select_branch_location(tree, location,
2898
 
                                                              revision, 0)
 
2915
            base_loc, _unused = self._select_branch_location(tree,
 
2916
                location, revision, 0)
2899
2917
        else:
2900
2918
            base_loc = other_loc
2901
2919
        # Open the branches
2922
2940
        else:
2923
2941
            base_revision_id = None
2924
2942
        # Remember where we merge from
2925
 
        if ((tree.branch.get_parent() is None or remember) and
2926
 
            other_branch is not None):
2927
 
            tree.branch.set_parent(other_branch.base)
 
2943
        if ((remember or tree.branch.get_submit_branch() is None) and
 
2944
             user_location is not None):
 
2945
            tree.branch.set_submit_branch(other_branch.base)
2928
2946
        _merge_tags_if_possible(other_branch, tree.branch)
2929
2947
        merger = _mod_merge.Merger.from_revision_ids(pb, tree,
2930
2948
            other_revision_id, base_revision_id, other_branch, base_branch)
2935
2953
            allow_pending = True
2936
2954
        return merger, allow_pending
2937
2955
 
2938
 
    def _select_branch_location(self, tree, location, revision=None,
 
2956
    def _select_branch_location(self, tree, user_location, revision=None,
2939
2957
                                index=None):
2940
2958
        """Select a branch location, according to possible inputs.
2941
2959
 
2943
2961
        ``revision`` and ``index`` must be supplied.)
2944
2962
 
2945
2963
        Otherwise, the ``location`` parameter is used.  If it is None, then the
2946
 
        ``parent`` location is used, and a note is printed.
 
2964
        ``submit`` or ``parent`` location is used, and a note is printed.
2947
2965
 
2948
2966
        :param tree: The working tree to select a branch for merging into
2949
2967
        :param location: The location entered by the user
2950
2968
        :param revision: The revision parameter to the command
2951
2969
        :param index: The index to use for the revision parameter.  Negative
2952
2970
            indices are permitted.
2953
 
        :return: (selected_location, default_location).  The default location
2954
 
            will be the user-entered location, if any, or else the remembered
2955
 
            location.
 
2971
        :return: (selected_location, user_location).  The default location
 
2972
            will be the user-entered location.
2956
2973
        """
2957
2974
        if (revision is not None and index is not None
2958
2975
            and revision[index] is not None):
2959
2976
            branch = revision[index].get_branch()
2960
2977
            if branch is not None:
2961
 
                return branch, location
2962
 
        location = self._get_remembered_parent(tree, location, 'Merging from')
2963
 
        return location, location
 
2978
                return branch, branch
 
2979
        if user_location is None:
 
2980
            location = self._get_remembered(tree, 'Merging from')
 
2981
        else:
 
2982
            location = user_location
 
2983
        return location, user_location
2964
2984
 
2965
 
    # TODO: move up to common parent; this isn't merge-specific anymore. 
2966
 
    def _get_remembered_parent(self, tree, supplied_location, verb_string):
 
2985
    def _get_remembered(self, tree, verb_string):
2967
2986
        """Use tree.branch's parent if none was supplied.
2968
2987
 
2969
2988
        Report if the remembered location was used.
2970
2989
        """
2971
 
        if supplied_location is not None:
2972
 
            return supplied_location
2973
 
        stored_location = tree.branch.get_parent()
 
2990
        stored_location = tree.branch.get_submit_branch()
 
2991
        if stored_location is None:
 
2992
            stored_location = tree.branch.get_parent()
2974
2993
        mutter("%s", stored_location)
2975
2994
        if stored_location is None:
2976
2995
            raise errors.BzrCommandError("No location specified or remembered")