comparison mercurial/filemerge.py @ 48981:f3aafd785e65

filemerge: add support for partial conflict resolution by external tool A common class of merge conflicts is in imports/#includes/etc. It's relatively easy to write a tool that can resolve these conflicts, perhaps by naively just unioning the statements and leaving any cleanup to other tools to do later [1]. Such specialized tools cannot generally resolve all conflicts in a file, of course. Let's therefore call them "partial merge tools". Note that the internal simplemerge algorithm is such a partial merge tool - one that only resolves trivial "conflicts" where one side is unchanged or both sides change in the same way. One can also imagine having smarter language-aware partial tools that merge the AST. It may be useful for such tools to interactively let the user resolve any conflicts it can't resolve itself. However, having the option of implementing it as a partial merge tool means that the developer doesn't *need* to create a UI for it. Instead, the user can resolve any remaining conflicts with their regular merge tool (e.g. `:merge3` or `meld). We don't currently have a way to let the user define such partial merge tools. That's what this patch addresses. It lets the user configure partial merge tools to run. Each tool can be configured to run only on files matching certain patterns (e.g. "*.py"). The tool takes three inputs (local, base, other) and resolves conflicts by updating these in place. For example, let's say the inputs are these: base: ``` import sys def main(): print('Hello') ``` local: ``` import os import sys def main(): print('Hi') ``` other: ``` import re import sys def main(): print('Howdy') ``` A partial merge tool could now resolve the conflicting imports by replacing the import statements in *all* files by the following snippet, while leaving the remainder of the files unchanged. ``` import os import re import sys ``` As a result, simplemerge and any regular merge tool that runs after the partial merge tool(s) will consider the imports to be non-conflicting and will only present the conflict in `main()` to the user. Differential Revision: https://phab.mercurial-scm.org/D12356
author Martin von Zweigbergk <martinvonz@google.com>
date Tue, 18 Jan 2022 13:05:21 -0800
parents 4057563ebc6b
children 9dfbea54b680
comparison
equal deleted inserted replaced
48978:c80544aa4971 48981:f3aafd785e65
1049 markerstyle = _toolstr(ui, tool, b'mergemarkers') 1049 markerstyle = _toolstr(ui, tool, b'mergemarkers')
1050 else: 1050 else:
1051 markerstyle = internalmarkerstyle 1051 markerstyle = internalmarkerstyle
1052 1052
1053 if mergetype == fullmerge: 1053 if mergetype == fullmerge:
1054 _run_partial_resolution_tools(repo, local, other, base)
1054 # conflict markers generated by premerge will use 'detailed' 1055 # conflict markers generated by premerge will use 'detailed'
1055 # settings if either ui.mergemarkers or the tool's mergemarkers 1056 # settings if either ui.mergemarkers or the tool's mergemarkers
1056 # setting is 'detailed'. This way tools can have basic labels in 1057 # setting is 'detailed'. This way tools can have basic labels in
1057 # space-constrained areas of the UI, but still get full information 1058 # space-constrained areas of the UI, but still get full information
1058 # in conflict markers if premerge is 'keep' or 'keep-merge3'. 1059 # in conflict markers if premerge is 'keep' or 'keep-merge3'.
1113 finally: 1114 finally:
1114 if not r and backup is not None: 1115 if not r and backup is not None:
1115 backup.remove() 1116 backup.remove()
1116 1117
1117 1118
1119 def _run_partial_resolution_tools(repo, local, other, base):
1120 """Runs partial-resolution tools on the three inputs and updates them."""
1121 ui = repo.ui
1122 # Tuples of (order, name, executable path)
1123 tools = []
1124 seen = set()
1125 section = b"partial-merge-tools"
1126 for k, v in ui.configitems(section):
1127 name = k.split(b'.')[0]
1128 if name in seen:
1129 continue
1130 patterns = ui.configlist(section, b'%s.patterns' % name, [])
1131 is_match = True
1132 if patterns:
1133 m = match.match(repo.root, b'', patterns)
1134 is_match = m(local.fctx.path())
1135 if is_match:
1136 order = ui.configint(section, b'%s.order' % name, 0)
1137 executable = ui.config(section, b'%s.executable' % name, name)
1138 tools.append((order, name, executable))
1139
1140 if not tools:
1141 return
1142 # Sort in configured order (first in tuple)
1143 tools.sort()
1144
1145 files = [
1146 (b"local", local.fctx.path(), local.text()),
1147 (b"base", base.fctx.path(), base.text()),
1148 (b"other", other.fctx.path(), other.text()),
1149 ]
1150
1151 with _maketempfiles(files) as temppaths:
1152 localpath, basepath, otherpath = temppaths
1153
1154 for order, name, executable in tools:
1155 cmd = procutil.shellquote(executable)
1156 # TODO: Allow the user to configure the command line using
1157 # $local, $base, $other.
1158 cmd = b'%s %s %s %s' % (cmd, localpath, basepath, otherpath)
1159 r = ui.system(cmd, cwd=repo.root, blockedtag=b'partial-mergetool')
1160 if r:
1161 raise error.StateError(
1162 b'partial merge tool %s exited with code %d' % (name, r)
1163 )
1164 local_text = util.readfile(localpath)
1165 other_text = util.readfile(otherpath)
1166 if local_text == other_text:
1167 # No need to run other tools if all conflicts have been resolved
1168 break
1169
1170 local.set_text(local_text)
1171 base.set_text(util.readfile(basepath))
1172 other.set_text(other_text)
1173
1174
1118 def _haltmerge(): 1175 def _haltmerge():
1119 msg = _(b'merge halted after failed merge (see hg resolve)') 1176 msg = _(b'merge halted after failed merge (see hg resolve)')
1120 raise error.InterventionRequired(msg) 1177 raise error.InterventionRequired(msg)
1121 1178
1122 1179