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
17
from __future__ import absolute_import
19
21
from bzrlib.lazy_import import lazy_import
74
78
"See the AbstractPerFileMerger API docs for details on how it is "
81
self.add_hook('pre_merge',
82
'Called before a merge. '
83
'Receives a Merger object as the single argument.',
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.',
79
92
class AbstractPerFileMerger(object):
91
104
def merge_contents(self, merge_params):
92
105
"""Attempt to merge the contents of a single file.
94
:param merge_params: A bzrlib.merge.MergeHookParams
107
:param merge_params: A bzrlib.merge.MergeFileHookParams
95
108
:return: A tuple of (status, chunks), where status is one of
96
109
'not_applicable', 'success', 'conflicted', or 'delete'. If status
97
110
is 'success' or 'conflicted', then chunks should be an iterable of
119
132
def get_filename(self, params, tree):
120
133
"""Lookup the filename (i.e. basename, not path), given a Tree (e.g.
121
self.merger.this_tree) and a MergeHookParams.
134
self.merger.this_tree) and a MergeFileHookParams.
123
136
return osutils.basename(tree.id2path(params.file_id))
125
138
def get_filepath(self, params, tree):
126
139
"""Calculate the path to the file in a tree.
128
:param params: A MergeHookParams describing the file to merge
141
:param params: A MergeFileHookParams describing the file to merge
129
142
:param tree: a Tree, e.g. self.merger.this_tree.
131
144
return tree.id2path(params.file_id)
138
151
params.winner == 'other' or
139
152
# THIS and OTHER aren't both files.
140
153
not params.is_file_merge() or
141
# The filename doesn't match *.xml
154
# The filename doesn't match
142
155
not self.file_matches(params)):
143
156
return 'not_applicable', None
144
157
return self.merge_matching(params)
441
454
revision_id = _mod_revision.ensure_null(revision_id)
442
455
return branch, self.revision_tree(revision_id, branch)
444
@deprecated_method(deprecated_in((2, 1, 0)))
445
def ensure_revision_trees(self):
446
if self.this_revision_tree is None:
447
self.this_basis_tree = self.revision_tree(self.this_basis)
448
if self.this_basis == self.this_rev_id:
449
self.this_revision_tree = self.this_basis_tree
451
if self.other_rev_id is None:
452
other_basis_tree = self.revision_tree(self.other_basis)
453
if other_basis_tree.has_changes(self.other_tree):
454
raise errors.WorkingTreeNotRevision(self.this_tree)
455
other_rev_id = self.other_basis
456
self.other_tree = other_basis_tree
458
@deprecated_method(deprecated_in((2, 1, 0)))
459
def file_revisions(self, file_id):
460
self.ensure_revision_trees()
461
if self.this_rev_id is None:
462
if self.this_basis_tree.get_file_sha1(file_id) != \
463
self.this_tree.get_file_sha1(file_id):
464
raise errors.WorkingTreeNotRevision(self.this_tree)
466
trees = (self.this_basis_tree, self.other_tree)
467
return [tree.get_file_revision(file_id) for tree in trees]
469
@deprecated_method(deprecated_in((2, 1, 0)))
470
def check_basis(self, check_clean, require_commits=True):
471
if self.this_basis is None and require_commits is True:
472
raise errors.BzrCommandError(
473
"This branch has no commits."
474
" (perhaps you would prefer 'bzr pull')")
477
if self.this_basis != self.this_rev_id:
478
raise errors.UncommittedChanges(self.this_tree)
480
@deprecated_method(deprecated_in((2, 1, 0)))
481
def compare_basis(self):
483
basis_tree = self.revision_tree(self.this_tree.last_revision())
484
except errors.NoSuchRevision:
485
basis_tree = self.this_tree.basis_tree()
486
if not self.this_tree.has_changes(basis_tree):
487
self.this_rev_id = self.this_basis
489
457
def set_interesting_files(self, file_list):
490
458
self.interesting_files = file_list
536
504
raise errors.NoCommits(self.other_branch)
537
505
if self.other_rev_id is not None:
538
506
self._cached_trees[self.other_rev_id] = self.other_tree
539
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)
541
509
def set_other_revision(self, revision_id, other_branch):
542
510
"""Set 'other' based on a branch and revision id
644
612
self._maybe_fetch(base_branch, self.this_branch, self.base_rev_id)
646
614
def make_merger(self):
647
kwargs = {'working_tree':self.this_tree, 'this_tree': self.this_tree,
615
kwargs = {'working_tree': self.this_tree, 'this_tree': self.this_tree,
648
616
'other_tree': self.other_tree,
649
617
'interesting_ids': self.interesting_ids,
650
618
'interesting_files': self.interesting_files,
651
619
'this_branch': self.this_branch,
620
'other_branch': self.other_branch,
652
621
'do_merge': False}
653
622
if self.merge_type.requires_base:
654
623
kwargs['base_tree'] = self.base_tree
680
649
merge = self.make_merger()
681
650
if self.other_branch is not None:
682
651
self.other_branch.update_references(self.this_branch)
652
for hook in Merger.hooks['pre_merge']:
655
for hook in Merger.hooks['post_merge']:
684
657
if self.recurse == 'down':
685
658
for relpath, file_id in self.this_tree.iter_references():
686
659
sub_tree = self.this_tree.get_nested_tree(file_id, relpath)
713
686
merge = operation.run_simple()
714
687
if len(merge.cooked_conflicts) == 0:
715
688
if not self.ignore_zero and not trace.is_quiet():
716
trace.note("All changes applied successfully.")
689
trace.note(gettext("All changes applied successfully."))
718
trace.note("%d conflicts encountered."
691
trace.note(gettext("%d conflicts encountered.")
719
692
% len(merge.cooked_conflicts))
721
694
return len(merge.cooked_conflicts)
753
726
interesting_ids=None, reprocess=False, show_base=False,
754
727
pb=None, pp=None, change_reporter=None,
755
728
interesting_files=None, do_merge=True,
756
cherrypick=False, lca_trees=None, this_branch=None):
729
cherrypick=False, lca_trees=None, this_branch=None,
757
731
"""Initialize the merger object and perform the merge.
759
733
:param working_tree: The working tree to apply the merge to
762
736
:param other_tree: The other tree to merge changes from
763
737
:param this_branch: The branch associated with this_tree. Defaults to
764
738
this_tree.branch if not supplied.
739
:param other_branch: The branch associated with other_tree, if any.
765
740
:param interesting_ids: The file_ids of files that should be
766
741
participate in the merge. May not be combined with
767
742
interesting_files.
789
764
this_branch = this_tree.branch
790
765
self.interesting_ids = interesting_ids
791
766
self.interesting_files = interesting_files
792
self.this_tree = working_tree
767
self.working_tree = working_tree
768
self.this_tree = this_tree
793
769
self.base_tree = base_tree
794
770
self.other_tree = other_tree
795
771
self.this_branch = this_branch
772
self.other_branch = other_branch
796
773
self._raw_conflicts = []
797
774
self.cooked_conflicts = []
798
775
self.reprocess = reprocess
815
792
def do_merge(self):
816
793
operation = cleanup.OperationWithCleanups(self._do_merge)
817
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()
818
797
operation.add_cleanup(self.this_tree.unlock)
819
798
self.base_tree.lock_read()
820
799
operation.add_cleanup(self.base_tree.unlock)
825
804
def _do_merge(self, operation):
826
self.tt = transform.TreeTransform(self.this_tree, None)
805
self.tt = transform.TreeTransform(self.working_tree, None)
827
806
operation.add_cleanup(self.tt.finalize)
828
807
self._compute_transform()
829
808
results = self.tt.apply(no_conflicts=True)
830
809
self.write_modified(results)
832
self.this_tree.add_conflicts(self.cooked_conflicts)
811
self.working_tree.add_conflicts(self.cooked_conflicts)
833
812
except errors.UnsupportedOperation:
854
833
entries = self._entries_lca()
855
834
resolver = self._lca_multi_way
835
# Prepare merge hooks
836
factories = Merger.hooks['merge_file_content']
837
# One hook for each registered one plus our default merger
838
hooks = [factory(self) for factory in factories] + [self]
839
self.active_hooks = [hook for hook in hooks if hook is not None]
856
840
child_pb = ui.ui_factory.nested_progress_bar()
858
factories = Merger.hooks['merge_file_content']
859
hooks = [factory(self) for factory in factories] + [self]
860
self.active_hooks = [hook for hook in hooks if hook is not None]
861
842
for num, (file_id, changed, parents3, names3,
862
843
executable3) in enumerate(entries):
863
child_pb.update('Preparing file merge', num, len(entries))
844
# Try merging each entry
845
child_pb.update(gettext('Preparing file merge'),
864
847
self._merge_names(file_id, parents3, names3, resolver=resolver)
866
849
file_status = self._do_merge_contents(file_id)
1143
1126
def write_modified(self, results):
1144
1127
modified_hashes = {}
1145
1128
for path in results.modified_paths:
1146
file_id = self.this_tree.path2id(self.this_tree.relpath(path))
1129
file_id = self.working_tree.path2id(self.working_tree.relpath(path))
1147
1130
if file_id is None:
1149
hash = self.this_tree.get_file_sha1(file_id)
1132
hash = self.working_tree.get_file_sha1(file_id)
1150
1133
if hash is None:
1152
1135
modified_hashes[file_id] = hash
1153
self.this_tree.set_merge_modified(modified_hashes)
1136
self.working_tree.set_merge_modified(modified_hashes)
1156
1139
def parent(entry, file_id):
1380
1363
# We have a hypothetical conflict, but if we have files, then we
1381
1364
# can try to merge the content
1382
1365
trans_id = self.tt.trans_id_file_id(file_id)
1383
params = MergeHookParams(self, file_id, trans_id, this_pair[0],
1366
params = MergeFileHookParams(self, file_id, trans_id, this_pair[0],
1384
1367
other_pair[0], winner)
1385
1368
hooks = self.active_hooks
1386
1369
hook_status = 'not_applicable'
1947
1930
entries = self._entries_to_incorporate()
1948
1931
entries = list(entries)
1949
1932
for num, (entry, parent_id) in enumerate(entries):
1950
child_pb.update('Preparing file merge', num, len(entries))
1933
child_pb.update(gettext('Preparing file merge'), num, len(entries))
1951
1934
parent_trans_id = self.tt.trans_id_file_id(parent_id)
1952
1935
trans_id = transform.new_by_entry(self.tt, entry,
1953
1936
parent_trans_id, self.other_tree)
2037
2020
merger.set_base_revision(get_revision_id(), this_branch)
2038
2021
return merger.do_merge()
2024
merge_type_registry = registry.Registry()
2025
merge_type_registry.register('diff3', Diff3Merger,
2026
"Merge using external diff3.")
2027
merge_type_registry.register('lca', LCAMerger,
2028
"LCA-newness merge.")
2029
merge_type_registry.register('merge3', Merge3Merger,
2030
"Native diff3-style merge.")
2031
merge_type_registry.register('weave', WeaveMerger,
2032
"Weave-based merge.")
2040
2035
def get_merge_type_registry():
2041
"""Merge type registry is in bzrlib.option to avoid circular imports.
2036
"""Merge type registry was previously in bzrlib.option
2043
This method provides a sanctioned way to retrieve it.
2038
This method provides a backwards compatible way to retrieve it.
2045
from bzrlib import option
2046
return option._merge_type_registry
2040
return merge_type_registry
2049
2043
def _plan_annotate_merge(annotated_a, annotated_b, ancestors_a, ancestors_b):