93
98
return ('not applicable', None)
96
class ConfigurableFileMerger(AbstractPerFileMerger):
101
class PerFileMerger(AbstractPerFileMerger):
102
"""Merge individual files when self.file_matches returns True.
104
This class is intended to be subclassed. The file_matches and
105
merge_matching methods should be overridden with concrete implementations.
108
def file_matches(self, params):
109
"""Return True if merge_matching should be called on this file.
111
Only called with merges of plain files with no clear winner.
113
Subclasses must override this.
115
raise NotImplementedError(self.file_matches)
117
def get_filename(self, params, tree):
118
"""Lookup the filename (i.e. basename, not path), given a Tree (e.g.
119
self.merger.this_tree) and a MergeHookParams.
121
return osutils.basename(tree.id2path(params.file_id))
123
def get_filepath(self, params, tree):
124
"""Calculate the path to the file in a tree.
126
:param params: A MergeHookParams describing the file to merge
127
:param tree: a Tree, e.g. self.merger.this_tree.
129
return tree.id2path(params.file_id)
131
def merge_contents(self, params):
132
"""Merge the contents of a single file."""
133
# Check whether this custom merge logic should be used.
135
# OTHER is a straight winner, rely on default merge.
136
params.winner == 'other' or
137
# THIS and OTHER aren't both files.
138
not params.is_file_merge() or
139
# The filename doesn't match *.xml
140
not self.file_matches(params)):
141
return 'not_applicable', None
142
return self.merge_matching(params)
144
def merge_matching(self, params):
145
"""Merge the contents of a single file that has matched the criteria
146
in PerFileMerger.merge_contents (is a conflict, is a file,
147
self.file_matches is True).
149
Subclasses must override this.
151
raise NotImplementedError(self.merge_matching)
154
class ConfigurableFileMerger(PerFileMerger):
97
155
"""Merge individual files when configured via a .conf file.
99
157
This is a base class for concrete custom file merging logic. Concrete
122
180
if self.name_prefix is None:
123
181
raise ValueError("name_prefix must be set.")
125
def filename_matches_config(self, params):
183
def file_matches(self, params):
126
184
"""Check whether the file should call the merge hook.
128
186
<name_prefix>_merge_files configuration variable is a list of files
142
200
affected_files = self.default_files
143
201
self.affected_files = affected_files
144
202
if affected_files:
145
filename = self.merger.this_tree.id2path(params.file_id)
146
if filename in affected_files:
203
filepath = self.get_filepath(params, self.merger.this_tree)
204
if filepath in affected_files:
150
def merge_contents(self, params):
151
"""Merge the contents of a single file."""
152
# First, check whether this custom merge logic should be used. We
153
# expect most files should not be merged by this handler.
155
# OTHER is a straight winner, rely on default merge.
156
params.winner == 'other' or
157
# THIS and OTHER aren't both files.
158
not params.is_file_merge() or
159
# The filename isn't listed in the 'NAME_merge_files' config
161
not self.filename_matches_config(params)):
162
return 'not_applicable', None
208
def merge_matching(self, params):
163
209
return self.merge_text(params)
165
211
def merge_text(self, params):
704
750
:param this_tree: The local tree in the merge operation
705
751
:param base_tree: The common tree in the merge operation
706
752
:param other_tree: The other tree to merge changes from
707
:param this_branch: The branch associated with this_tree
753
:param this_branch: The branch associated with this_tree. Defaults to
754
this_tree.branch if not supplied.
708
755
:param interesting_ids: The file_ids of files that should be
709
756
participate in the merge. May not be combined with
710
757
interesting_files.