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
#testcases flat tree
$ . "$TESTDIR/narrow-library.sh"
#if tree
$ cat << EOF >> $HGRCPATH
> [experimental]
> treemanifest = 1
> EOF
#endif
create full repo
$ hg init master
$ cd master
$ cat >> .hg/hgrc <<EOF
> [narrow]
> serveellipses=True
> EOF
$ mkdir inside
$ echo inside1 > inside/f1
$ echo inside2 > inside/f2
$ mkdir outside
$ echo outside1 > outside/f1
$ echo outside2 > outside/f2
$ hg ci -Aqm 'initial'
$ echo modified > inside/f1
$ hg ci -qm 'modify inside/f1'
$ hg update -q 0
$ echo modified > inside/f2
$ hg ci -qm 'modify inside/f2'
$ hg update -q 0
$ echo modified2 > inside/f1
$ hg ci -qm 'conflicting inside/f1'
$ hg update -q 0
$ echo modified > outside/f1
$ hg ci -qm 'modify outside/f1'
$ hg update -q 0
$ echo modified2 > outside/f1
$ hg ci -qm 'conflicting outside/f1'
$ cd ..
$ hg clone --narrow ssh://user@dummy/master narrow --include inside
requesting all changes
adding changesets
adding manifests
adding file changes
added 6 changesets with 5 changes to 2 files (+4 heads)
new changesets *:* (glob)
updating to branch default
2 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ cd narrow
$ hg update -q 0
Can merge in when no files outside narrow spec are involved
$ hg update -q 'desc("modify inside/f1")'
$ hg merge 'desc("modify inside/f2")'
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)
$ hg commit -m 'merge inside changes'
Can merge conflicting changes inside narrow spec
$ hg update -q 'desc("modify inside/f1")'
$ hg merge 'desc("conflicting inside/f1")' 2>&1 | egrep -v '(warning:|incomplete!)'
merging inside/f1
0 files updated, 0 files merged, 0 files removed, 1 files unresolved
use 'hg resolve' to retry unresolved file merges or 'hg merge --abort' to abandon
$ echo modified3 > inside/f1
$ hg resolve -m
(no more unresolved files)
$ hg commit -m 'merge inside/f1'
TODO: Can merge non-conflicting changes outside narrow spec
$ hg update -q 'desc("modify inside/f1")'
#if flat
$ hg merge 'desc("modify outside/f1")'
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)
status should be clean
$ hg status
? inside/f1.orig
file out of the spec should still not be in the dirstate at all
$ hg debugdirstate | grep outside/f1
[1]
Commit that merge
$ hg ci -m 'merge from outside to inside'
status should be clean
$ hg status
? inside/f1.orig
file out of the spec should not be in the mergestate anymore
$ hg debugmergestate | grep outside/f1
[1]
file out of the spec should still not be in the dirstate at all
$ hg debugdirstate | grep outside/f1
[1]
The filenode used should come from p2
$ hg manifest --debug --rev . | grep outside/f1
83cd11431a3b2aff8a3995e5f27bcf33cdb5be98 644 outside/f1
$ hg manifest --debug --rev 'p1(.)' | grep outside/f1
c6b956c48be2cd4fa94be16002aba311143806fa 644 outside/f1
$ hg manifest --debug --rev 'p2(.)' | grep outside/f1
83cd11431a3b2aff8a3995e5f27bcf33cdb5be98 644 outside/f1
remove the commit to get in the previous situation again
$ hg debugstrip -r .
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
saved backup bundle to $TESTTMP/narrow/.hg/strip-backup/48eb25338b19-a1bb8350-backup.hg
#else
$ hg merge 'desc("modify outside/f1")'
abort: merge affects file 'outside/' outside narrow, which is not yet supported (tree !)
(merging in the other direction may work)
[255]
#endif
$ hg update -q 'desc("modify outside/f1")'
$ hg merge 'desc("modify inside/f1")'
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)
$ hg ci -m 'merge from inside to outside'
Refuses merge of conflicting outside changes
$ hg update -q 'desc("modify outside/f1")'
$ hg merge 'desc("conflicting outside/f1")'
abort: conflict in file 'outside/f1' is outside narrow clone (flat !)
abort: conflict in file 'outside/' is outside narrow clone (tree !)
[20]