2637
2728
directory=None,
2639
2730
from bzrlib.tag import _merge_tags_if_possible
2640
other_revision_id = None
2731
# This is actually a branch (or merge-directive) *location*.
2641
2735
if merge_type is None:
2642
2736
merge_type = _mod_merge.Merge3Merger
2644
2738
if directory is None: directory = u'.'
2645
# XXX: jam 20070225 WorkingTree should be locked before you extract its
2646
# inventory. Because merge is a mutating operation, it really
2647
# should be a lock_write() for the whole cmd_merge operation.
2648
# However, cmd_merge open's its own tree in _merge_helper, which
2649
# means if we lock here, the later lock_write() will always block.
2650
# Either the merge helper code should be updated to take a tree,
2651
# (What about tree.merge_from_branch?)
2739
possible_transports = []
2741
allow_pending = True
2742
verified = 'inapplicable'
2652
2743
tree = WorkingTree.open_containing(directory)[0]
2653
2744
change_reporter = delta._ChangeReporter(
2654
2745
unversioned_filter=tree.is_ignored)
2656
if branch is not None:
2658
mergeable = bundle.read_mergeable_from_url(
2660
except errors.NotABundle:
2661
pass # Continue on considering this url a Branch
2663
if revision is not None:
2664
raise errors.BzrCommandError(
2665
'Cannot use -r with merge directives or bundles')
2666
other_revision_id = mergeable.install_revisions(
2667
tree.branch.repository)
2668
revision = [RevisionSpec.from_string(
2669
'revid:' + other_revision_id)]
2671
if revision is None \
2672
or len(revision) < 1 or revision[0].needs_branch():
2673
branch = self._get_remembered_parent(tree, branch, 'Merging from')
2675
if revision is None or len(revision) < 1:
2678
other = [branch, None]
2681
other = [branch, -1]
2682
other_branch, path = Branch.open_containing(branch)
2685
raise errors.BzrCommandError('Cannot use --uncommitted and'
2686
' --revision at the same time.')
2687
branch = revision[0].get_branch() or branch
2688
if len(revision) == 1:
2690
if other_revision_id is not None:
2695
other_branch, path = Branch.open_containing(branch)
2696
revno = revision[0].in_history(other_branch).revno
2697
other = [branch, revno]
2699
assert len(revision) == 2
2700
if None in revision:
2701
raise errors.BzrCommandError(
2702
"Merge doesn't permit empty revision specifier.")
2703
base_branch, path = Branch.open_containing(branch)
2704
branch1 = revision[1].get_branch() or branch
2705
other_branch, path1 = Branch.open_containing(branch1)
2706
if revision[0].get_branch() is not None:
2707
# then path was obtained from it, and is None.
2710
base = [branch, revision[0].in_history(base_branch).revno]
2711
other = [branch1, revision[1].in_history(other_branch).revno]
2748
pb = ui.ui_factory.nested_progress_bar()
2749
cleanups.append(pb.finished)
2751
cleanups.append(tree.unlock)
2752
if location is not None:
2753
mergeable, other_transport = _get_mergeable_helper(location)
2756
raise errors.BzrCommandError('Cannot use --uncommitted'
2757
' with bundles or merge directives.')
2759
if revision is not None:
2760
raise errors.BzrCommandError(
2761
'Cannot use -r with merge directives or bundles')
2762
merger, verified = _mod_merge.Merger.from_mergeable(tree,
2764
possible_transports.append(other_transport)
2766
if merger is None and uncommitted:
2767
if revision is not None and len(revision) > 0:
2768
raise errors.BzrCommandError('Cannot use --uncommitted and'
2769
' --revision at the same time.')
2770
location = self._select_branch_location(tree, location)[0]
2771
other_tree, other_path = WorkingTree.open_containing(location)
2772
merger = _mod_merge.Merger.from_uncommitted(tree, other_tree,
2774
allow_pending = False
2777
merger, allow_pending = self._get_merger_from_branch(tree,
2778
location, revision, remember, possible_transports, pb)
2780
merger.merge_type = merge_type
2781
merger.reprocess = reprocess
2782
merger.show_base = show_base
2783
merger.change_reporter = change_reporter
2784
self.sanity_check_merger(merger)
2785
if (merger.base_rev_id == merger.other_rev_id and
2786
merger.other_rev_id != None):
2787
note('Nothing to do.')
2790
if merger.interesting_files is not None:
2791
raise BzrCommandError('Cannot pull individual files')
2792
if (merger.base_rev_id == tree.last_revision()):
2793
result = tree.pull(merger.other_branch, False,
2794
merger.other_rev_id)
2795
result.report(self.outf)
2797
merger.check_basis(not force)
2798
conflict_count = merger.do_merge()
2800
merger.set_pending()
2801
if verified == 'failed':
2802
warning('Preview patch does not match changes')
2803
if conflict_count != 0:
2808
for cleanup in reversed(cleanups):
2811
def sanity_check_merger(self, merger):
2812
if (merger.show_base and
2813
not merger.merge_type is _mod_merge.Merge3Merger):
2814
raise errors.BzrCommandError("Show-base is not supported for this"
2815
" merge type. %s" % merger.merge_type)
2816
if merger.reprocess and not merger.merge_type.supports_reprocess:
2817
raise errors.BzrCommandError("Conflict reduction is not supported"
2818
" for merge type %s." %
2820
if merger.reprocess and merger.show_base:
2821
raise errors.BzrCommandError("Cannot do conflict reduction and"
2824
def _get_merger_from_branch(self, tree, location, revision, remember,
2825
possible_transports, pb):
2826
"""Produce a merger from a location, assuming it refers to a branch."""
2827
from bzrlib.tag import _merge_tags_if_possible
2828
assert revision is None or len(revision) < 3
2829
# find the branch locations
2830
other_loc, location = self._select_branch_location(tree, location,
2832
if revision is not None and len(revision) == 2:
2833
base_loc, location = self._select_branch_location(tree, location,
2836
base_loc = other_loc
2838
other_branch, other_path = Branch.open_containing(other_loc,
2839
possible_transports)
2840
if base_loc == other_loc:
2841
base_branch = other_branch
2843
base_branch, base_path = Branch.open_containing(base_loc,
2844
possible_transports)
2845
# Find the revision ids
2846
if revision is None or len(revision) < 1 or revision[-1] is None:
2847
other_revision_id = _mod_revision.ensure_null(
2848
other_branch.last_revision())
2850
other_revision_id = \
2851
_mod_revision.ensure_null(
2852
revision[-1].in_history(other_branch).rev_id)
2853
if (revision is not None and len(revision) == 2
2854
and revision[0] is not None):
2855
base_revision_id = \
2856
_mod_revision.ensure_null(
2857
revision[0].in_history(base_branch).rev_id)
2859
base_revision_id = None
2860
# Remember where we merge from
2713
2861
if ((tree.branch.get_parent() is None or remember) and
2714
2862
other_branch is not None):
2715
2863
tree.branch.set_parent(other_branch.base)
2717
# pull tags now... it's a bit inconsistent to do it ahead of copying
2718
# the history but that's done inside the merge code
2719
if other_branch is not None:
2720
_merge_tags_if_possible(other_branch, tree.branch)
2723
interesting_files = [path]
2864
_merge_tags_if_possible(other_branch, tree.branch)
2865
merger = _mod_merge.Merger.from_revision_ids(pb, tree,
2866
other_revision_id, base_revision_id, other_branch, base_branch)
2867
if other_path != '':
2868
allow_pending = False
2869
merger.interesting_files = [other_path]
2725
interesting_files = None
2726
pb = ui.ui_factory.nested_progress_bar()
2729
conflict_count = _merge_helper(
2730
other, base, other_rev_id=other_revision_id,
2731
check_clean=(not force),
2732
merge_type=merge_type,
2733
reprocess=reprocess,
2734
show_base=show_base,
2737
pb=pb, file_list=interesting_files,
2738
change_reporter=change_reporter)
2741
if conflict_count != 0:
2745
except errors.AmbiguousBase, e:
2746
m = ("sorry, bzr can't determine the right merge base yet\n"
2747
"candidates are:\n "
2748
+ "\n ".join(e.bases)
2750
"please specify an explicit base with -r,\n"
2751
"and (if you want) report this to the bzr developers\n")
2871
allow_pending = True
2872
return merger, allow_pending
2874
def _select_branch_location(self, tree, location, revision=None,
2876
"""Select a branch location, according to possible inputs.
2878
If provided, branches from ``revision`` are preferred. (Both
2879
``revision`` and ``index`` must be supplied.)
2881
Otherwise, the ``location`` parameter is used. If it is None, then the
2882
``parent`` location is used, and a note is printed.
2884
:param tree: The working tree to select a branch for merging into
2885
:param location: The location entered by the user
2886
:param revision: The revision parameter to the command
2887
:param index: The index to use for the revision parameter. Negative
2888
indices are permitted.
2889
:return: (selected_location, default_location). The default location
2890
will be the user-entered location, if any, or else the remembered
2893
if (revision is not None and index is not None
2894
and revision[index] is not None):
2895
branch = revision[index].get_branch()
2896
if branch is not None:
2897
return branch, location
2898
location = self._get_remembered_parent(tree, location, 'Merging from')
2899
return location, location
2754
2901
# TODO: move up to common parent; this isn't merge-specific anymore.
2755
2902
def _get_remembered_parent(self, tree, supplied_location, verb_string):
3626
3815
s.send_email(message)
3818
class cmd_send(Command):
3819
"""Mail or create a merge-directive for submiting changes.
3821
A merge directive provides many things needed for requesting merges:
3823
* A machine-readable description of the merge to perform
3825
* An optional patch that is a preview of the changes requested
3827
* An optional bundle of revision data, so that the changes can be applied
3828
directly from the merge directive, without retrieving data from a
3831
If --no-bundle is specified, then public_branch is needed (and must be
3832
up-to-date), so that the receiver can perform the merge using the
3833
public_branch. The public_branch is always included if known, so that
3834
people can check it later.
3836
The submit branch defaults to the parent, but can be overridden. Both
3837
submit branch and public branch will be remembered if supplied.
3839
If a public_branch is known for the submit_branch, that public submit
3840
branch is used in the merge instructions. This means that a local mirror
3841
can be used as your actual submit branch, once you have set public_branch
3844
Mail is sent using your preferred mail program. This should be transparent
3845
on Windows (it uses MAPI). On *nix, it requires the xdg-email utility. If
3846
the preferred client can't be found (or used), your editor will be used.
3848
To use a specific mail program, set the mail_client configuration option.
3849
(For Thunderbird 1.5, this works around some bugs.) Supported values are
3850
"thunderbird", "evolution", "editor", "xdg-email", "mapi", "kmail" and
3853
If mail is being sent, a to address is required. This can be supplied
3854
either on the commandline, or by setting the submit_to configuration
3857
Two formats are currently supported: "4" uses revision bundle format 4 and
3858
merge directive format 2. It is significantly faster and smaller than
3859
older formats. It is compatible with Bazaar 0.19 and later. It is the
3860
default. "0.9" uses revision bundle format 0.9 and merge directive
3861
format 1. It is compatible with Bazaar 0.12 - 0.18.
3864
encoding_type = 'exact'
3866
_see_also = ['merge']
3868
takes_args = ['submit_branch?', 'public_branch?']
3872
help='Do not include a bundle in the merge directive.'),
3873
Option('no-patch', help='Do not include a preview patch in the merge'
3876
help='Remember submit and public branch.'),
3878
help='Branch to generate the submission from, '
3879
'rather than the one containing the working directory.',
3882
Option('output', short_name='o', help='Write directive to this file.',
3884
Option('mail-to', help='Mail the request to this address.',
3888
RegistryOption.from_kwargs('format',
3889
'Use the specified output format.',
3890
**{'4': 'Bundle format 4, Merge Directive 2 (default)',
3891
'0.9': 'Bundle format 0.9, Merge Directive 1',})
3894
def run(self, submit_branch=None, public_branch=None, no_bundle=False,
3895
no_patch=False, revision=None, remember=False, output=None,
3896
format='4', mail_to=None, message=None, **kwargs):
3897
return self._run(submit_branch, revision, public_branch, remember,
3898
format, no_bundle, no_patch, output,
3899
kwargs.get('from', '.'), mail_to, message)
3901
def _run(self, submit_branch, revision, public_branch, remember, format,
3902
no_bundle, no_patch, output, from_, mail_to, message):
3903
from bzrlib.revision import ensure_null, NULL_REVISION
3905
outfile = StringIO()
3909
outfile = open(output, 'wb')
3911
branch = Branch.open_containing(from_)[0]
3913
config = branch.get_config()
3915
mail_to = config.get_user_option('submit_to')
3917
raise errors.BzrCommandError('No mail-to address'
3919
mail_client = config.get_mail_client()
3920
if remember and submit_branch is None:
3921
raise errors.BzrCommandError(
3922
'--remember requires a branch to be specified.')
3923
stored_submit_branch = branch.get_submit_branch()
3924
remembered_submit_branch = False
3925
if submit_branch is None:
3926
submit_branch = stored_submit_branch
3927
remembered_submit_branch = True
3929
if stored_submit_branch is None or remember:
3930
branch.set_submit_branch(submit_branch)
3931
if submit_branch is None:
3932
submit_branch = branch.get_parent()
3933
remembered_submit_branch = True
3934
if submit_branch is None:
3935
raise errors.BzrCommandError('No submit branch known or'
3937
if remembered_submit_branch:
3938
note('Using saved location: %s', submit_branch)
3940
stored_public_branch = branch.get_public_branch()
3941
if public_branch is None:
3942
public_branch = stored_public_branch
3943
elif stored_public_branch is None or remember:
3944
branch.set_public_branch(public_branch)
3945
if no_bundle and public_branch is None:
3946
raise errors.BzrCommandError('No public branch specified or'
3948
base_revision_id = None
3950
if revision is not None:
3951
if len(revision) > 2:
3952
raise errors.BzrCommandError('bzr send takes '
3953
'at most two one revision identifiers')
3954
revision_id = revision[-1].in_history(branch).rev_id
3955
if len(revision) == 2:
3956
base_revision_id = revision[0].in_history(branch).rev_id
3957
if revision_id is None:
3958
revision_id = branch.last_revision()
3959
if revision_id == NULL_REVISION:
3960
raise errors.BzrCommandError('No revisions to submit.')
3962
directive = merge_directive.MergeDirective2.from_objects(
3963
branch.repository, revision_id, time.time(),
3964
osutils.local_time_offset(), submit_branch,
3965
public_branch=public_branch, include_patch=not no_patch,
3966
include_bundle=not no_bundle, message=message,
3967
base_revision_id=base_revision_id)
3968
elif format == '0.9':
3971
patch_type = 'bundle'
3973
raise errors.BzrCommandError('Format 0.9 does not'
3974
' permit bundle with no patch')
3980
directive = merge_directive.MergeDirective.from_objects(
3981
branch.repository, revision_id, time.time(),
3982
osutils.local_time_offset(), submit_branch,
3983
public_branch=public_branch, patch_type=patch_type,
3986
outfile.writelines(directive.to_lines())
3988
subject = '[MERGE] '
3989
if message is not None:
3992
revision = branch.repository.get_revision(revision_id)
3993
subject += revision.get_summary()
3994
mail_client.compose_merge_request(mail_to, subject,
4001
class cmd_bundle_revisions(cmd_send):
4003
"""Create a merge-directive for submiting changes.
4005
A merge directive provides many things needed for requesting merges:
4007
* A machine-readable description of the merge to perform
4009
* An optional patch that is a preview of the changes requested
4011
* An optional bundle of revision data, so that the changes can be applied
4012
directly from the merge directive, without retrieving data from a
4015
If --no-bundle is specified, then public_branch is needed (and must be
4016
up-to-date), so that the receiver can perform the merge using the
4017
public_branch. The public_branch is always included if known, so that
4018
people can check it later.
4020
The submit branch defaults to the parent, but can be overridden. Both
4021
submit branch and public branch will be remembered if supplied.
4023
If a public_branch is known for the submit_branch, that public submit
4024
branch is used in the merge instructions. This means that a local mirror
4025
can be used as your actual submit branch, once you have set public_branch
4028
Two formats are currently supported: "4" uses revision bundle format 4 and
4029
merge directive format 2. It is significantly faster and smaller than
4030
older formats. It is compatible with Bazaar 0.19 and later. It is the
4031
default. "0.9" uses revision bundle format 0.9 and merge directive
4032
format 1. It is compatible with Bazaar 0.12 - 0.18.
4037
help='Do not include a bundle in the merge directive.'),
4038
Option('no-patch', help='Do not include a preview patch in the merge'
4041
help='Remember submit and public branch.'),
4043
help='Branch to generate the submission from, '
4044
'rather than the one containing the working directory.',
4047
Option('output', short_name='o', help='Write directive to this file.',
4050
RegistryOption.from_kwargs('format',
4051
'Use the specified output format.',
4052
**{'4': 'Bundle format 4, Merge Directive 2 (default)',
4053
'0.9': 'Bundle format 0.9, Merge Directive 1',})
4055
aliases = ['bundle']
4057
_see_also = ['send', 'merge']
4061
def run(self, submit_branch=None, public_branch=None, no_bundle=False,
4062
no_patch=False, revision=None, remember=False, output=None,
4063
format='4', **kwargs):
4066
return self._run(submit_branch, revision, public_branch, remember,
4067
format, no_bundle, no_patch, output,
4068
kwargs.get('from', '.'), None, None)
3629
4071
class cmd_tag(Command):
3630
"""Create a tag naming a revision.
4072
"""Create, remove or modify a tag naming a revision.
3632
4074
Tags give human-meaningful names to revisions. Commands that take a -r
3633
4075
(--revision) option can be given -rtag:X, where X is any previously
3710
4152
self.outf.write('%-20s %s\n' % (tag_name, target))
3713
# command-line interpretation helper for merge-related commands
3714
def _merge_helper(other_revision, base_revision,
3715
check_clean=True, ignore_zero=False,
3716
this_dir=None, backup_files=False,
3718
file_list=None, show_base=False, reprocess=False,
3721
change_reporter=None,
3723
"""Merge changes into a tree.
3726
list(path, revno) Base for three-way merge.
3727
If [None, None] then a base will be automatically determined.
3729
list(path, revno) Other revision for three-way merge.
3731
Directory to merge changes into; '.' by default.
3733
If true, this_dir must have no uncommitted changes before the
3735
ignore_zero - If true, suppress the "zero conflicts" message when
3736
there are no conflicts; should be set when doing something we expect
3737
to complete perfectly.
3738
file_list - If supplied, merge only changes to selected files.
3740
All available ancestors of other_revision and base_revision are
3741
automatically pulled into the branch.
3743
The revno may be -1 to indicate the last revision on the branch, which is
3746
This function is intended for use from the command line; programmatic
3747
clients might prefer to call merge.merge_inner(), which has less magic
3750
# Loading it late, so that we don't always have to import bzrlib.merge
3751
if merge_type is None:
3752
merge_type = _mod_merge.Merge3Merger
3753
if this_dir is None:
3755
this_tree = WorkingTree.open_containing(this_dir)[0]
3756
if show_base and not merge_type is _mod_merge.Merge3Merger:
3757
raise errors.BzrCommandError("Show-base is not supported for this merge"
3758
" type. %s" % merge_type)
3759
if reprocess and not merge_type.supports_reprocess:
3760
raise errors.BzrCommandError("Conflict reduction is not supported for merge"
3761
" type %s." % merge_type)
3762
if reprocess and show_base:
3763
raise errors.BzrCommandError("Cannot do conflict reduction and show base.")
3764
# TODO: jam 20070226 We should really lock these trees earlier. However, we
3765
# only want to take out a lock_tree_write() if we don't have to pull
3766
# any ancestry. But merge might fetch ancestry in the middle, in
3767
# which case we would need a lock_write().
3768
# Because we cannot upgrade locks, for now we live with the fact that
3769
# the tree will be locked multiple times during a merge. (Maybe
3770
# read-only some of the time, but it means things will get read
3773
merger = _mod_merge.Merger(this_tree.branch, this_tree=this_tree,
3774
pb=pb, change_reporter=change_reporter)
3775
merger.pp = ProgressPhase("Merge phase", 5, pb)
3776
merger.pp.next_phase()
3777
merger.check_basis(check_clean)
3778
if other_rev_id is not None:
3779
merger.set_other_revision(other_rev_id, this_tree.branch)
3781
merger.set_other(other_revision)
3782
merger.pp.next_phase()
3783
merger.set_base(base_revision)
3784
if merger.base_rev_id == merger.other_rev_id:
3785
note('Nothing to do.')
3787
if file_list is None:
3788
if pull and merger.base_rev_id == merger.this_rev_id:
3789
# FIXME: deduplicate with pull
3790
result = merger.this_tree.pull(merger.this_branch,
3791
False, merger.other_rev_id)
3792
if result.old_revid == result.new_revid:
3793
note('No revisions to pull.')
3795
note('Now on revision %d.' % result.new_revno)
3797
merger.backup_files = backup_files
3798
merger.merge_type = merge_type
3799
merger.set_interesting_files(file_list)
3800
merger.show_base = show_base
3801
merger.reprocess = reprocess
3802
conflicts = merger.do_merge()
3803
if file_list is None:
3804
merger.set_pending()
3810
4155
def _create_prefix(cur_transport):
3811
4156
needed = [cur_transport]
3812
4157
# Recurse upwards until we can create a directory successfully