55
class MergeHooks(hooks.Hooks):
58
hooks.Hooks.__init__(self)
59
self.create_hook(hooks.HookPoint('merge_file_content',
60
"Called when file content needs to be merged (including when one "
61
"side has deleted the file and the other has changed it)."
62
"merge_file_content is called with a "
63
"bzrlib.merge.MergeHookParams. The function should return a tuple "
64
"of (status, lines), where status is one of 'not_applicable', "
65
"'success', 'conflicted', or 'delete'. If status is success or "
66
"conflicted, then lines should be an iterable of strings of the "
71
class MergeHookParams(object):
72
"""Object holding parameters passed to merge_file_content hooks.
74
There are 3 fields hooks can access:
76
:ivar merger: the Merger object
77
:ivar file_id: the file ID of the file being merged
78
:ivar trans_id: the transform ID for the merge of this file
79
:ivar this_kind: kind of file_id in 'this' tree
80
:ivar other_kind: kind of file_id in 'other' tree
81
:ivar winner: one of 'this', 'other', 'conflict'
84
def __init__(self, merger, file_id, trans_id, this_kind, other_kind,
87
self.file_id = file_id
88
self.trans_id = trans_id
89
self.this_kind = this_kind
90
self.other_kind = other_kind
93
def is_file_merge(self):
94
"""True if this_kind and other_kind are both 'file'."""
95
return self.this_kind == 'file' and self.other_kind == 'file'
97
@decorators.cachedproperty
99
"""The lines of the 'base' version of the file."""
100
return self.merger.get_lines(self.merger.base_tree, self.file_id)
102
@decorators.cachedproperty
103
def this_lines(self):
104
"""The lines of the 'this' version of the file."""
105
return self.merger.get_lines(self.merger.this_tree, self.file_id)
107
@decorators.cachedproperty
108
def other_lines(self):
109
"""The lines of the 'other' version of the file."""
110
return self.merger.get_lines(self.merger.other_tree, self.file_id)
53
113
class Merger(object):
54
117
def __init__(self, this_branch, other_tree=None, base_tree=None,
55
118
this_tree=None, pb=None, change_reporter=None,
56
119
recurse='down', revision_graph=None):
1140
1193
if winner == 'this':
1141
1194
# No interesting changes introduced by OTHER
1142
1195
return "unmodified"
1196
# We have a hypothetical conflict, but if we have files, then we
1197
# can try to merge the content
1143
1198
trans_id = self.tt.trans_id_file_id(file_id)
1144
if winner == 'other':
1199
params = MergeHookParams(self, file_id, trans_id, this_pair[0],
1200
other_pair[0], winner)
1201
hooks = Merger.hooks['merge_file_content']
1202
hooks = list(hooks) + [self.default_text_merge]
1203
hook_status = 'not_applicable'
1205
hook_status, lines = hook(params)
1206
if hook_status != 'not_applicable':
1207
# Don't try any more hooks, this one applies.
1210
if hook_status == 'not_applicable':
1211
# This is a contents conflict, because none of the available
1212
# functions could merge it.
1214
name = self.tt.final_name(trans_id)
1215
parent_id = self.tt.final_parent(trans_id)
1216
if self.this_tree.has_id(file_id):
1217
self.tt.unversion_file(trans_id)
1218
file_group = self._dump_conflicts(name, parent_id, file_id,
1220
self._raw_conflicts.append(('contents conflict', file_group))
1221
elif hook_status == 'success':
1222
self.tt.create_file(lines, trans_id)
1223
elif hook_status == 'conflicted':
1224
# XXX: perhaps the hook should be able to provide
1225
# the BASE/THIS/OTHER files?
1226
self.tt.create_file(lines, trans_id)
1227
self._raw_conflicts.append(('text conflict', trans_id))
1228
name = self.tt.final_name(trans_id)
1229
parent_id = self.tt.final_parent(trans_id)
1230
self._dump_conflicts(name, parent_id, file_id)
1231
elif hook_status == 'delete':
1232
self.tt.unversion_file(trans_id)
1234
elif hook_status == 'done':
1235
# The hook function did whatever it needs to do directly, no
1236
# further action needed here.
1239
raise AssertionError('unknown hook_status: %r' % (hook_status,))
1240
if not self.this_tree.has_id(file_id) and result == "modified":
1241
self.tt.version_file(file_id, trans_id)
1242
# The merge has been performed, so the old contents should not be
1245
self.tt.delete_contents(trans_id)
1246
except errors.NoSuchFile:
1250
def _default_other_winner_merge(self, merge_hook_params):
1251
"""Replace this contents with other."""
1252
file_id = merge_hook_params.file_id
1253
trans_id = merge_hook_params.trans_id
1254
file_in_this = self.this_tree.has_id(file_id)
1255
if self.other_tree.has_id(file_id):
1256
# OTHER changed the file
1258
if wt.supports_content_filtering():
1259
# We get the path from the working tree if it exists.
1260
# That fails though when OTHER is adding a file, so
1261
# we fall back to the other tree to find the path if
1262
# it doesn't exist locally.
1264
filter_tree_path = wt.id2path(file_id)
1265
except errors.NoSuchId:
1266
filter_tree_path = self.other_tree.id2path(file_id)
1268
# Skip the id2path lookup for older formats
1269
filter_tree_path = None
1270
transform.create_from_tree(self.tt, trans_id,
1271
self.other_tree, file_id,
1272
filter_tree_path=filter_tree_path)
1275
# OTHER deleted the file
1276
return 'delete', None
1278
raise AssertionError(
1279
'winner is OTHER, but file_id %r not in THIS or OTHER tree'
1282
def default_text_merge(self, merge_hook_params):
1283
if merge_hook_params.winner == 'other':
1145
1284
# OTHER is a straight winner, so replace this contents with other
1146
file_in_this = file_id in self.this_tree
1148
# Remove any existing contents
1149
self.tt.delete_contents(trans_id)
1150
if file_id in self.other_tree:
1151
# OTHER changed the file
1153
if wt.supports_content_filtering():
1154
# We get the path from the working tree if it exists.
1155
# That fails though when OTHER is adding a file, so
1156
# we fall back to the other tree to find the path if
1157
# it doesn't exist locally.
1159
filter_tree_path = wt.id2path(file_id)
1160
except errors.NoSuchId:
1161
filter_tree_path = self.other_tree.id2path(file_id)
1163
# Skip the id2path lookup for older formats
1164
filter_tree_path = None
1165
transform.create_from_tree(self.tt, trans_id,
1166
self.other_tree, file_id,
1167
filter_tree_path=filter_tree_path)
1168
if not file_in_this:
1169
self.tt.version_file(file_id, trans_id)
1172
# OTHER deleted the file
1173
self.tt.unversion_file(trans_id)
1285
return self._default_other_winner_merge(merge_hook_params)
1286
elif merge_hook_params.is_file_merge():
1287
# THIS and OTHER are both files, so text merge. Either
1288
# BASE is a file, or both converted to files, so at least we
1289
# have agreement that output should be a file.
1291
self.text_merge(merge_hook_params.file_id,
1292
merge_hook_params.trans_id)
1293
except errors.BinaryFile:
1294
return 'not_applicable', None
1176
# We have a hypothetical conflict, but if we have files, then we
1177
# can try to merge the content
1178
if this_pair[0] == 'file' and other_pair[0] == 'file':
1179
# THIS and OTHER are both files, so text merge. Either
1180
# BASE is a file, or both converted to files, so at least we
1181
# have agreement that output should be a file.
1183
self.text_merge(file_id, trans_id)
1184
except errors.BinaryFile:
1185
return contents_conflict()
1186
if file_id not in self.this_tree:
1187
self.tt.version_file(file_id, trans_id)
1189
self.tt.tree_kind(trans_id)
1190
self.tt.delete_contents(trans_id)
1191
except errors.NoSuchFile:
1195
return contents_conflict()
1297
return 'not_applicable', None
1197
1299
def get_lines(self, tree, file_id):
1198
1300
"""Return the lines in a file, or an empty list."""
1301
if tree.has_id(file_id):
1200
1302
return tree.get_file(file_id).readlines()