git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v11 1/3] mergetool: add hideResolved configuration

From
Seth House <seth@eseth.com>
Date
Feb 9, 2021, 20:07 UTC
Message-ID
<20210209200712.156540-2-seth@eseth.com>
In-Reply-To
<20210209200712.156540-1-seth@eseth.com>

The purpose of a mergetool is to help the user resolve any conflicts that Git cannot automatically resolve. If there is a conflict that must be resolved manually Git will write a file named MERGED which contains everything Git was able to resolve by itself and also everything that it was not able to resolve wrapped in conflict markers.

One way to think of MERGED is as a two- or three-way diff. If each "side" of the conflict markers is separately extracted an external tool can represent those conflicts as a side-by-side diff.

However many mergetools instead diff LOCAL and REMOTE both of which contain versions of the file from before the merge. Since the conflicts Git resolved automatically are not present it forces the user to manually re-resolve those conflicts. Some mergetools also show MERGED but often only for reference and not as the focal point to resolve the conflicts.

This adds a `mergetool.hideResolved` flag that will overwrite LOCAL and REMOTE with each corresponding "side" of a conflicted file and thus hide all conflicts that Git was able to resolve itself. Overwriting these files will immediately benefit any mergetool that uses them without requiring any changes to the tool.

No adverse effects were noted in a small survey of popular mergetools[1] so this behavior defaults to `true`. However it can be globally disabled by setting `mergetool.hideResolved` to `false`.

[1] https://www.eseth.org/2020/mergetools.html
    https://github.com/whiteinge/eseth/blob/c884424769fffb05d87afb33b2cf80cecb4044c3/2020/mergetools.md
Original-implementation-by: Felipe Contreras <felipe.contreras@gmail.com>
Signed-off-by: Seth House <seth@eseth.com>
---
 Documentation/config/mergetool.txt | 10 ++++++++++
 git-mergetool.sh                   | 14 ++++++++++++++
 t/t7610-mergetool.sh               | 18 ++++++++++++++++++
 3 files changed, 42 insertions(+)
diff --git a/Documentation/config/mergetool.txt b/Documentation/config/mergetool.txt
index 16a27443a3..b858191970 100644
--- a/Documentation/config/mergetool.txt
+++ b/Documentation/config/mergetool.txt
@@ -40,6 +40,16 @@ mergetool.meld.useAutoMerge::
 	value of `false` avoids using `--auto-merge` altogether, and is the
 	default value.
 
+mergetool.hideResolved::
+	During a merge Git will automatically resolve as many conflicts as
+	possible and write the 'MERGED' file containing conflict markers around
+	any conflicts that it cannot resolve; 'LOCAL' and 'REMOTE' normally
+	represent the versions of the file from before Git's conflict
+	resolution. This flag causes 'LOCAL' and 'REMOTE' to be overwriten so
+	that only the unresolved conflicts are presented to the merge tool. Can
+	be configured per-tool via the `mergetool.<tool>.hideResolved`
+	configuration variable. Defaults to `true`.
+
 mergetool.keepBackup::
 	After performing a merge, the original file with conflict markers
 	can be saved as a file with a `.orig` extension.  If this variable
diff --git a/git-mergetool.sh b/git-mergetool.sh
index e3f6d543fb..40a103443d 100755
--- a/git-mergetool.sh
+++ b/git-mergetool.sh
@@ -239,6 +239,13 @@ checkout_staged_file () {
 	fi
 }
 
+hide_resolved () {
+	git merge-file --ours -q -p "$LOCAL" "$BASE" "$REMOTE" >"$LCONFL"
+	git merge-file --theirs -q -p "$LOCAL" "$BASE" "$REMOTE" >"$RCONFL"
+	mv -- "$LCONFL" "$LOCAL"
+	mv -- "$RCONFL" "$REMOTE"
+}
+
 merge_file () {
 	MERGED="$1"
 
@@ -276,7 +283,9 @@ merge_file () {
 
 	BACKUP="$MERGETOOL_TMPDIR/${BASE}_BACKUP_$$$ext"
 	LOCAL="$MERGETOOL_TMPDIR/${BASE}_LOCAL_$$$ext"
+	LCONFL="$MERGETOOL_TMPDIR/${BASE}_LOCAL_LCONFL_$$$ext"
 	REMOTE="$MERGETOOL_TMPDIR/${BASE}_REMOTE_$$$ext"
+	RCONFL="$MERGETOOL_TMPDIR/${BASE}_REMOTE_RCONFL_$$$ext"
 	BASE="$MERGETOOL_TMPDIR/${BASE}_BASE_$$$ext"
 
 	base_mode= local_mode= remote_mode=
@@ -322,6 +331,11 @@ merge_file () {
 	checkout_staged_file 2 "$MERGED" "$LOCAL"
 	checkout_staged_file 3 "$MERGED" "$REMOTE"
 
+	if test "$(git config --type=bool mergetool.hideResolved)" != "false"
+	then
+		hide_resolved
+	fi
+
 	if test -z "$local_mode" || test -z "$remote_mode"
 	then
 		echo "Deleted merge conflict for '$MERGED':"
diff --git a/t/t7610-mergetool.sh b/t/t7610-mergetool.sh
index 04b0095072..cec4a860ef 100755
--- a/t/t7610-mergetool.sh
+++ b/t/t7610-mergetool.sh
@@ -842,4 +842,22 @@ test_expect_success 'mergetool --tool-help shows recognized tools' '
 	grep meld mergetools
 '
 
+test_expect_success 'mergetool hideResolved' '
+	test_config mergetool.hideResolved true &&
+	test_when_finished "git reset --hard" &&
+	git checkout -b test${test_count}_b master &&
+	test_write_lines >file1 base "" a &&
+	git commit -a -m "base" &&
+	test_write_lines >file1 base "" c &&
+	git commit -a -m "remote update" &&
+	git checkout -b test${test_count}_a HEAD~ &&
+	test_write_lines >file1 local "" b &&
+	git commit -a -m "local update" &&
+	test_must_fail git merge test${test_count}_b &&
+	yes "" | git mergetool file1 &&
+	test_write_lines >expect local "" c &&
+	test_cmp expect file1 &&
+	git commit -m "test resolved with mergetool"
+'
+
 test_done
-- 
2.29.2
Previous: Seth HouseNext: Jonathan Nieder
Message 61 of 80 in “mergetool: remove unconflicted lines”
  1. 0/1 mergetool: remove unconflicted linesFelipe Contreras, Dec 23, 2020
  2. 1/1 mergetool: add automerge configurationFelipe Contreras, Dec 23, 2020
  3. Junio C HamanoDec 23, 2020
  4. Felipe ContrerasDec 23, 2020
  5. Junio C HamanoDec 23, 2020
  6. Felipe ContrerasDec 24, 2020
  7. Junio C HamanoDec 24, 2020
  8. Felipe ContrerasDec 24, 2020
  9. Junio C HamanoDec 24, 2020
  10. Felipe ContrerasDec 24, 2020
  11. Junio C HamanoDec 24, 2020
  12. Felipe ContrerasDec 27, 2020
  13. Junio C HamanoDec 24, 2020
  14. Felipe ContrerasDec 24, 2020
  15. Johannes SchindelinDec 30, 2020
  16. Felipe ContrerasDec 30, 2020
  17. 0/1 mergetool: add automerge configurationSeth House, Dec 27, 2020
  18. 2/2 mergetool: Add per-tool support for the autoMerge flagSeth House, Dec 27, 2020
  19. Junio C HamanoDec 27, 2020
  20. 1/2 mergetool: add automerge configurationSeth House, Dec 27, 2020
  21. Junio C HamanoDec 27, 2020
  22. Seth HouseDec 27, 2020
  23. Junio C HamanoDec 27, 2020
  24. 0/2 mergetool: add automerge configurationSeth House, Dec 28, 2020
  25. 2/2 mergetool: Add per-tool support for the autoMerge flagSeth House, Dec 28, 2020
  26. Felipe ContrerasDec 28, 2020
  27. 1/2 mergetool: add automerge configurationSeth House, Dec 28, 2020
  28. 0/4 mergetool: add automerge configurationSeth House, Dec 28, 2020
  29. 1/4 mergetool: add automerge configurationSeth House, Dec 28, 2020
  30. Johannes SixtDec 28, 2020
  31. 2/4 mergetool: Add per-tool support for the autoMerge flagSeth House, Dec 28, 2020
  32. Junio C HamanoDec 28, 2020
  33. 4/4 mergetool: Add automerge_enabled tool-specific override functionSeth House, Dec 28, 2020
  34. Johannes SixtDec 28, 2020
  35. Junio C HamanoDec 28, 2020
  36. 3/4 mergetool: Break setup_tool out into separate initialization functionSeth House, Dec 28, 2020
  37. Johannes SixtDec 28, 2020
  38. 0/5 mergetool: add automerge configurationSeth House, Dec 28, 2020
  39. 5/5 mergetool: add automerge_enabled tool-specific override functionSeth House, Dec 28, 2020
  40. Felipe ContrerasDec 29, 2020
  41. Junio C HamanoJan 6, 2021
  42. Seth HouseJan 7, 2021
  43. Junio C HamanoJan 7, 2021
  44. Seth HouseJan 7, 2021
  45. Junio C HamanoJan 7, 2021
  46. Johannes SchindelinJan 8, 2021
  47. 3/5 mergetool: add per-tool support for the autoMerge flagSeth House, Dec 28, 2020
  48. 4/5 mergetool: break setup_tool out into separate initialization functionSeth House, Dec 28, 2020
  49. Johannes SixtDec 29, 2020
  50. Seth HouseDec 29, 2020
  51. 2/5 mergetool: alphabetize the mergetool config docsSeth House, Dec 28, 2020
  52. 1/5 mergetool: add automerge configurationSeth House, Dec 28, 2020
  53. 0/3 mergetool: add hideResolved configuration (was automerge)Seth House, Jan 30, 2021
  54. 3/3 mergetool: add per-tool support and overrides for the hideResolved flagSeth House, Jan 30, 2021
  55. Junio C HamanoJan 30, 2021
  56. 2/3 mergetool: break setup_tool out into separate initialization functionSeth House, Jan 30, 2021
  57. 1/3 mergetool: add hideResolved configurationSeth House, Jan 30, 2021
  58. Junio C HamanoJan 30, 2021
  59. 0/3 mergetool: add hideResolved configuration (was automerge)Seth House, Feb 9, 2021
  60. 2/3 mergetool: break setup_tool out into separate initialization functionSeth House, Feb 9, 2021
  61. 1/3 mergetool: add hideResolved configurationSeth House, Feb 9, 2021
  62. mergetool: do not enable hideResolved by defaultJonathan Nieder, Mar 9, 2021
  63. Seth HouseMar 9, 2021
  64. Jonathan NiederMar 10, 2021
  65. Junio C HamanoMar 10, 2021
  66. Junio C HamanoMar 11, 2021
  67. Junio C HamanoMar 12, 2021
  68. Jonathan NiederMar 12, 2021
  69. Junio C HamanoMar 12, 2021
  70. 0/2 mergetool: do not enable hideResolved by defaultJonathan Nieder, Mar 13, 2021
  71. 1/2 mergetool: do not enable hideResolved by defaultJonathan Nieder, Mar 13, 2021
  72. 2/2 doc: describe mergetool configuration in git-mergetool(1)Jonathan Nieder, Mar 13, 2021
  73. Junio C HamanoMar 13, 2021
  74. Junio C HamanoMar 13, 2021
  75. 3/3 mergetool: add per-tool support and overrides for the hideResolved flagSeth House, Feb 9, 2021
  76. Junio C HamanoFeb 9, 2021
  77. Seth HouseFeb 9, 2021
  78. Junio C HamanoDec 28, 2020
  79. Felipe ContrerasDec 28, 2020
  80. Felipe ContrerasDec 28, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.