2640
2689
directory=None,
2642
2691
from bzrlib.tag import _merge_tags_if_possible
2692
# This is actually a branch (or merge-directive) *location*.
2643
2696
if merge_type is None:
2644
2697
merge_type = _mod_merge.Merge3Merger
2646
2699
if directory is None: directory = u'.'
2647
# XXX: jam 20070225 WorkingTree should be locked before you extract its
2648
# inventory. Because merge is a mutating operation, it really
2649
# should be a lock_write() for the whole cmd_merge operation.
2650
# However, cmd_merge open's its own tree in _merge_helper, which
2651
# means if we lock here, the later lock_write() will always block.
2652
# Either the merge helper code should be updated to take a tree,
2653
# (What about tree.merge_from_branch?)
2700
possible_transports = []
2702
allow_pending = True
2703
verified = 'inapplicable'
2654
2704
tree = WorkingTree.open_containing(directory)[0]
2655
2705
change_reporter = delta._ChangeReporter(
2656
2706
unversioned_filter=tree.is_ignored)
2658
other_transport = None
2659
other_revision_id = None
2660
base_revision_id = None
2661
possible_transports = []
2663
if branch is not None:
2664
mergeable, other_transport = _get_bundle_helper(branch)
2666
if revision is not None:
2667
raise errors.BzrCommandError(
2668
'Cannot use -r with merge directives or bundles')
2669
mergeable.install_revisions(tree.branch.repository)
2670
base_revision_id, other_revision_id, verified =\
2671
mergeable.get_merge_request(tree.branch.repository)
2672
if base_revision_id in tree.branch.repository.get_ancestry(
2673
tree.branch.last_revision(), topo_sorted=False):
2674
base_revision_id = None
2679
possible_transports.append(other_transport)
2681
if other_revision_id is None:
2682
verified = 'inapplicable'
2683
if revision is None \
2684
or len(revision) < 1 or revision[0].needs_branch():
2685
branch = self._get_remembered_parent(tree, branch,
2688
if revision is None or len(revision) < 1:
2691
other = [branch, None]
2694
other = [branch, -1]
2695
other_branch, path = Branch.open_containing(branch,
2696
possible_transports)
2709
pb = ui.ui_factory.nested_progress_bar()
2710
cleanups.append(pb.finished)
2712
cleanups.append(tree.unlock)
2713
if location is not None:
2714
mergeable, other_transport = _get_mergeable_helper(location)
2717
raise errors.BzrCommandError('Cannot use --uncommitted'
2718
' with bundles or merge directives.')
2720
if revision is not None:
2721
raise errors.BzrCommandError(
2722
'Cannot use -r with merge directives or bundles')
2723
merger, verified = _mod_merge.Merger.from_mergeable(tree,
2725
possible_transports.append(other_transport)
2727
if merger is None and uncommitted:
2728
if revision is not None and len(revision) > 0:
2699
2729
raise errors.BzrCommandError('Cannot use --uncommitted and'
2700
2730
' --revision at the same time.')
2701
branch = revision[0].get_branch() or branch
2702
if len(revision) == 1:
2704
other_branch, path = Branch.open_containing(
2705
branch, possible_transports)
2706
revno = revision[0].in_history(other_branch).revno
2707
other = [branch, revno]
2709
assert len(revision) == 2
2710
if None in revision:
2711
raise errors.BzrCommandError(
2712
"Merge doesn't permit empty revision specifier.")
2713
base_branch, path = Branch.open_containing(
2714
branch, possible_transports)
2715
branch1 = revision[1].get_branch() or branch
2716
other_branch, path1 = Branch.open_containing(
2717
branch1, possible_transports)
2718
if revision[0].get_branch() is not None:
2719
# then path was obtained from it, and is None.
2722
base = [branch, revision[0].in_history(base_branch).revno]
2724
revision[1].in_history(other_branch).revno]
2731
location = self._select_branch_location(tree, location)[0]
2732
other_tree, other_path = WorkingTree.open_containing(location)
2733
merger = _mod_merge.Merger.from_uncommitted(tree, other_tree,
2735
allow_pending = False
2738
merger, allow_pending = self._get_merger_from_branch(tree,
2739
location, revision, remember, possible_transports, pb)
2741
merger.merge_type = merge_type
2742
merger.reprocess = reprocess
2743
merger.show_base = show_base
2744
merger.change_reporter = change_reporter
2745
self.sanity_check_merger(merger)
2746
if (merger.base_rev_id == merger.other_rev_id and
2747
merger.other_rev_id != None):
2748
note('Nothing to do.')
2751
if merger.interesting_files is not None:
2752
raise BzrCommandError('Cannot pull individual files')
2753
if (merger.base_rev_id == tree.last_revision()):
2754
result = tree.pull(merger.other_branch, False,
2755
merger.other_rev_id)
2756
result.report(self.outf)
2758
merger.check_basis(not force)
2759
conflict_count = merger.do_merge()
2761
merger.set_pending()
2762
if verified == 'failed':
2763
warning('Preview patch does not match changes')
2764
if conflict_count != 0:
2769
for cleanup in reversed(cleanups):
2772
def sanity_check_merger(self, merger):
2773
if (merger.show_base and
2774
not merger.merge_type is _mod_merge.Merge3Merger):
2775
raise errors.BzrCommandError("Show-base is not supported for this"
2776
" merge type. %s" % merger.merge_type)
2777
if merger.reprocess and not merger.merge_type.supports_reprocess:
2778
raise errors.BzrCommandError("Conflict reduction is not supported"
2779
" for merge type %s." %
2781
if merger.reprocess and merger.show_base:
2782
raise errors.BzrCommandError("Cannot do conflict reduction and"
2785
def _get_merger_from_branch(self, tree, location, revision, remember,
2786
possible_transports, pb):
2787
"""Produce a merger from a location, assuming it refers to a branch."""
2788
from bzrlib.tag import _merge_tags_if_possible
2789
assert revision is None or len(revision) < 3
2790
# find the branch locations
2791
other_loc, location = self._select_branch_location(tree, location,
2793
if revision is not None and len(revision) == 2:
2794
base_loc, location = self._select_branch_location(tree, location,
2797
base_loc = other_loc
2799
other_branch, other_path = Branch.open_containing(other_loc,
2800
possible_transports)
2801
if base_loc == other_loc:
2802
base_branch = other_branch
2804
base_branch, base_path = Branch.open_containing(base_loc,
2805
possible_transports)
2806
# Find the revision ids
2807
if revision is None or len(revision) < 1 or revision[-1] is None:
2808
other_revision_id = _mod_revision.ensure_null(
2809
other_branch.last_revision())
2811
other_revision_id = \
2812
_mod_revision.ensure_null(
2813
revision[-1].in_history(other_branch).rev_id)
2814
if (revision is not None and len(revision) == 2
2815
and revision[0] is not None):
2816
base_revision_id = \
2817
_mod_revision.ensure_null(
2818
revision[0].in_history(base_branch).rev_id)
2820
base_revision_id = None
2726
2821
# Remember where we merge from
2727
2822
if ((tree.branch.get_parent() is None or remember) and
2728
2823
other_branch is not None):
2729
2824
tree.branch.set_parent(other_branch.base)
2731
# pull tags now... it's a bit inconsistent to do it ahead of copying
2732
# the history but that's done inside the merge code
2733
if other_branch is not None:
2734
_merge_tags_if_possible(other_branch, tree.branch)
2737
interesting_files = [path]
2825
_merge_tags_if_possible(other_branch, tree.branch)
2826
merger = _mod_merge.Merger.from_revision_ids(pb, tree,
2827
other_revision_id, base_revision_id, other_branch, base_branch)
2828
if other_path != '':
2829
allow_pending = False
2830
merger.interesting_files = [other_path]
2739
interesting_files = None
2740
pb = ui.ui_factory.nested_progress_bar()
2743
conflict_count = _merge_helper(
2744
other, base, other_rev_id=other_revision_id,
2745
base_rev_id=base_revision_id,
2746
check_clean=(not force),
2747
merge_type=merge_type,
2748
reprocess=reprocess,
2749
show_base=show_base,
2752
pb=pb, file_list=interesting_files,
2753
change_reporter=change_reporter,
2754
possible_transports=possible_transports)
2757
if verified == 'failed':
2758
warning('Preview patch does not match changes')
2759
if conflict_count != 0:
2763
except errors.AmbiguousBase, e:
2764
m = ("sorry, bzr can't determine the right merge base yet\n"
2765
"candidates are:\n "
2766
+ "\n ".join(e.bases)
2768
"please specify an explicit base with -r,\n"
2769
"and (if you want) report this to the bzr developers\n")
2832
allow_pending = True
2833
return merger, allow_pending
2835
def _select_branch_location(self, tree, location, revision=None,
2837
"""Select a branch location, according to possible inputs.
2839
If provided, branches from ``revision`` are preferred. (Both
2840
``revision`` and ``index`` must be supplied.)
2842
Otherwise, the ``location`` parameter is used. If it is None, then the
2843
``parent`` location is used, and a note is printed.
2845
:param tree: The working tree to select a branch for merging into
2846
:param location: The location entered by the user
2847
:param revision: The revision parameter to the command
2848
:param index: The index to use for the revision parameter. Negative
2849
indices are permitted.
2850
:return: (selected_location, default_location). The default location
2851
will be the user-entered location, if any, or else the remembered
2854
if (revision is not None and index is not None
2855
and revision[index] is not None):
2856
branch = revision[index].get_branch()
2857
if branch is not None:
2858
return branch, location
2859
location = self._get_remembered_parent(tree, location, 'Merging from')
2860
return location, location
2772
2862
# TODO: move up to common parent; this isn't merge-specific anymore.
2773
2863
def _get_remembered_parent(self, tree, supplied_location, verb_string):
3784
3897
revision_id = ensure_null(revision_id)
3785
3898
if revision_id == NULL_REVISION:
3786
3899
raise errors.BzrCommandError('No revisions to submit.')
3787
directive = merge_directive.MergeDirective2.from_objects(
3788
branch.repository, revision_id, time.time(),
3789
osutils.local_time_offset(), submit_branch,
3790
public_branch=public_branch, include_patch=not no_patch,
3791
include_bundle=not no_bundle, message=None,
3792
base_revision_id=base_revision_id)
3901
directive = merge_directive.MergeDirective2.from_objects(
3902
branch.repository, revision_id, time.time(),
3903
osutils.local_time_offset(), submit_branch,
3904
public_branch=public_branch, include_patch=not no_patch,
3905
include_bundle=not no_bundle, message=None,
3906
base_revision_id=base_revision_id)
3907
elif format == '0.9':
3910
patch_type = 'bundle'
3912
raise errors.BzrCommandError('Format 0.9 does not'
3913
' permit bundle with no patch')
3919
directive = merge_directive.MergeDirective.from_objects(
3920
branch.repository, revision_id, time.time(),
3921
osutils.local_time_offset(), submit_branch,
3922
public_branch=public_branch, patch_type=patch_type,
3793
3925
outfile.writelines(directive.to_lines())
3795
if output is not None:
3796
3928
outfile.close()
3931
class cmd_bundle_revisions(cmd_send):
3933
"""Create a merge-directive for submiting changes.
3935
A merge directive provides many things needed for requesting merges:
3937
* A machine-readable description of the merge to perform
3939
* An optional patch that is a preview of the changes requested
3941
* An optional bundle of revision data, so that the changes can be applied
3942
directly from the merge directive, without retrieving data from a
3945
If --no-bundle is specified, then public_branch is needed (and must be
3946
up-to-date), so that the receiver can perform the merge using the
3947
public_branch. The public_branch is always included if known, so that
3948
people can check it later.
3950
The submit branch defaults to the parent, but can be overridden. Both
3951
submit branch and public branch will be remembered if supplied.
3953
If a public_branch is known for the submit_branch, that public submit
3954
branch is used in the merge instructions. This means that a local mirror
3955
can be used as your actual submit branch, once you have set public_branch
3958
Two formats are currently supported: "4" uses revision bundle format 4 and
3959
merge directive format 2. It is significantly faster and smaller than
3960
older formats. It is compatible with Bazaar 0.19 and later. It is the
3961
default. "0.9" uses revision bundle format 0.9 and merge directive
3962
format 1. It is compatible with Bazaar 0.12 - 0.18.
3965
aliases = ['bundle']
3967
_see_also = ['send', 'merge']
3971
def run(self, submit_branch=None, public_branch=None, no_bundle=False,
3972
no_patch=False, revision=None, remember=False, output=None,
3973
format='4', **kwargs):
3976
return self._run(submit_branch, revision, public_branch, remember,
3977
format, no_bundle, no_patch, output,
3978
kwargs.get('from', '.'))
3798
3981
class cmd_tag(Command):
3799
"""Create a tag naming a revision.
3982
"""Create, remove or modify a tag naming a revision.
3801
3984
Tags give human-meaningful names to revisions. Commands that take a -r
3802
3985
(--revision) option can be given -rtag:X, where X is any previously
3879
4062
self.outf.write('%-20s %s\n' % (tag_name, target))
3882
# command-line interpretation helper for merge-related commands
3883
def _merge_helper(other_revision, base_revision,
3884
check_clean=True, ignore_zero=False,
3885
this_dir=None, backup_files=False,
3887
file_list=None, show_base=False, reprocess=False,
3890
change_reporter=None,
3891
other_rev_id=None, base_rev_id=None,
3892
possible_transports=None):
3893
"""Merge changes into a tree.
3896
list(path, revno) Base for three-way merge.
3897
If [None, None] then a base will be automatically determined.
3899
list(path, revno) Other revision for three-way merge.
3901
Directory to merge changes into; '.' by default.
3903
If true, this_dir must have no uncommitted changes before the
3905
ignore_zero - If true, suppress the "zero conflicts" message when
3906
there are no conflicts; should be set when doing something we expect
3907
to complete perfectly.
3908
file_list - If supplied, merge only changes to selected files.
3910
All available ancestors of other_revision and base_revision are
3911
automatically pulled into the branch.
3913
The revno may be -1 to indicate the last revision on the branch, which is
3916
This function is intended for use from the command line; programmatic
3917
clients might prefer to call merge.merge_inner(), which has less magic
3920
# Loading it late, so that we don't always have to import bzrlib.merge
3921
if merge_type is None:
3922
merge_type = _mod_merge.Merge3Merger
3923
if this_dir is None:
3925
this_tree = WorkingTree.open_containing(this_dir)[0]
3926
if show_base and not merge_type is _mod_merge.Merge3Merger:
3927
raise errors.BzrCommandError("Show-base is not supported for this merge"
3928
" type. %s" % merge_type)
3929
if reprocess and not merge_type.supports_reprocess:
3930
raise errors.BzrCommandError("Conflict reduction is not supported for merge"
3931
" type %s." % merge_type)
3932
if reprocess and show_base:
3933
raise errors.BzrCommandError("Cannot do conflict reduction and show base.")
3934
# TODO: jam 20070226 We should really lock these trees earlier. However, we
3935
# only want to take out a lock_tree_write() if we don't have to pull
3936
# any ancestry. But merge might fetch ancestry in the middle, in
3937
# which case we would need a lock_write().
3938
# Because we cannot upgrade locks, for now we live with the fact that
3939
# the tree will be locked multiple times during a merge. (Maybe
3940
# read-only some of the time, but it means things will get read
3943
merger = _mod_merge.Merger(this_tree.branch, this_tree=this_tree,
3944
pb=pb, change_reporter=change_reporter)
3945
merger.pp = ProgressPhase("Merge phase", 5, pb)
3946
merger.pp.next_phase()
3947
merger.check_basis(check_clean)
3948
if other_rev_id is not None:
3949
merger.set_other_revision(other_rev_id, this_tree.branch)
3951
merger.set_other(other_revision, possible_transports)
3952
merger.pp.next_phase()
3953
if base_rev_id is not None:
3954
merger.set_base_revision(base_rev_id, this_tree.branch)
3955
elif base_revision is not None:
3956
merger.set_base(base_revision)
3959
if merger.base_rev_id == merger.other_rev_id:
3960
note('Nothing to do.')
3962
if file_list is None:
3963
if pull and merger.base_rev_id == merger.this_rev_id:
3964
# FIXME: deduplicate with pull
3965
result = merger.this_tree.pull(merger.this_branch,
3966
False, merger.other_rev_id)
3967
if result.old_revid == result.new_revid:
3968
note('No revisions to pull.')
3970
note('Now on revision %d.' % result.new_revno)
3972
merger.backup_files = backup_files
3973
merger.merge_type = merge_type
3974
merger.set_interesting_files(file_list)
3975
merger.show_base = show_base
3976
merger.reprocess = reprocess
3977
conflicts = merger.do_merge()
3978
if file_list is None:
3979
merger.set_pending()
3985
4065
def _create_prefix(cur_transport):
3986
4066
needed = [cur_transport]
3987
4067
# Recurse upwards until we can create a directory successfully