This is an archive of the discontinued Mercurial Phabricator instance.

filemerge: add support for partial conflict resolution by external tool
ClosedPublic

Authored by martinvonz on Mar 4 2022, 4:09 PM.

Details

Summary

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.

Diff Detail

Repository
rHG Mercurial
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

martinvonz created this revision.Mar 4 2022, 4:09 PM
Alphare requested changes to this revision.Mar 14 2022, 6:05 AM
Alphare added a subscriber: Alphare.

Some relatively minor changes but still important enough to warrant a Request Changes.

This is a cool feature! I'm not seeing anything obviously wrong with the design or execution, but I am still quite new to the merge code, so someone with more experience may point out issues I haven't seen.

mercurial/filemerge.py
1130

This should be gated by the presence of patterns so as to not create and run a match every time if there are no patterns

tests/test-merge-partial-tool.t
48

Please use hg up or hg update instead. I'm not the only one who's lost time in other places in the codebase due to thinking this was somehow commit. :)

This revision now requires changes to proceed.Mar 14 2022, 6:05 AM
martinvonz marked 2 inline comments as done.Mar 14 2022, 2:57 PM
martinvonz updated this revision to Diff 32640.
martinvonz added inline comments.Mar 14 2022, 2:58 PM
mercurial/filemerge.py
1130

I'm not sure that will make a significant difference, but it's also not much more code to check it, so I've made the change.

(There's no "every time" here. It would be nice to read the config etc. only once per merge, or even once per command, but filemerge.filemerge() is called once per file. We could refactor it to pass some object containing matchers etc. into filemerge, but I'd rather leave that for later, once we've seen if it's even a problem.)

tests/test-merge-partial-tool.t
48

Interesting, I don't think I'd heard that complaint before. That's good to know.

martinvonz updated this revision to Diff 32661.Thu, Mar 17, 6:18 PM
Alphare accepted this revision.Tue, Mar 22, 4:49 AM
This revision is now accepted and ready to land.Tue, Mar 22, 4:49 AM