{"thread":{"id":"57003","subject":"[PATCH v3 0/3] vimdiff: new implementation with layout support","startedAt":"2021-11-30T20:43:44Z","lastAt":"2021-12-04T08:32:07Z","messageCount":9,"participants":["Fernando Ramos","Eric Sunshine","Ævar Arnfjörð Bjarmason"],"isPatch":true,"patchVersion":3,"patchTotal":3},"messages":[{"id":"442674","messageId":"20211130204333.174967-1-greenfoo@u92.eu","threadId":"57003","inReplyTo":null,"subject":"[PATCH v3 0/3] vimdiff: new implementation with layout support","fromName":"Fernando Ramos","fromEmail":"greenfoo@u92.eu","sentAt":"2021-11-30T20:43:30Z","receivedAt":"2021-11-30T20:43:44Z","isPatch":true,"sender":{"key":"greenfoo@u92.eu","avatar":"https://avatars.githubusercontent.com/u/42691086?v=4"},"body":"A few weeks ago I presented this RFC [1] where I introduced a new variant of the\nvimdiff merge tool (\"vimdiff4\") that creates three tabs (instead of just one)\nthat look like this:\n\n    ------------------------------------------\n    | <TAB #1> |  TAB #2  |  TAB #3  |       |\n    ------------------------------------------\n    |             |           |              |\n    |   LOCAL     |   BASE    |   REMOTE     |\n    |             |           |              |   <---- Same information\n    ------------------------------------------         presented by the\n    |                                        |         \"standard\" vimdiff\n    |                MERGED                  |         merge tool\n    |                                        |\n    ------------------------------------------\n    \n    ------------------------------------------\n    |  TAB #1  | <TAB #2> |  TAB #3  |       |\n    ------------------------------------------\n    |                   |                    |\n    |                   |                    |\n    |                   |                    |\n    |     BASE          |    LOCAL           |   <---- Only differences\n    |                   |                    |         between BASE and\n    |                   |                    |         LOCAL are shown\n    |                   |                    |\n    ------------------------------------------\n    \n    ------------------------------------------\n    |  TAB #1  |  TAB #2  | <TAB #3> |       |\n    ------------------------------------------\n    |                   |                    |\n    |                   |                    |\n    |                   |                    |\n    |     BASE          |    REMOTE          |   <---- Only differences\n    |                   |                    |         between BASE and\n    |                   |                    |         REMOTE are shown\n    |                   |                    |\n    ------------------------------------------\n\n\nThe motivation behind this was that, for non-trivial merges, the three way diff\npresented in the first tab tends to be very confusing and in these cases\nindivial diffs between BASE and LOCAL and between BASE and REMOTE are very\nuseful.\n\nI have been using a \"custom\" merge tool for months to achieve this same result\nby adding these lines to my .gitconfig file:\n\n  [mergetool \"supermerge\"]\n        cmd = vim -f -d -c \\\"4wincmd w | wincmd J | tabnew | edit $LOCAL | vertical diffsplit $BASE | tabnew | edit $REMOTE | vertical diffsplit $BASE | 2tabprevious\\\" \\\"$LOCAL\\\" \\\"$BASE\\\" \\\"$REMOTE\\\" \\\"$MERGED\\\"\n        trustExitCode = true\n\n...and, because I found this \"trick\" very useful, I thought it would be a good\nidea to add it as a git built-in merge tool (called \"vimdiff4\" because  1, 2 and\n3 had already been taken) for everyone to use... and that's exactly what the RFC\nI published did.\n\nNow... as you can see in the RFC thread [1], David and Juno suggested that\nmaybe, instead of creating *yet another vimdiff variant*, we should take this\nopportunity to:\n\n  * Come up with a more general way of defining arbitrary vim layouts.\n  \n  * Re-implement \"vimdiff1\", \"vimdiff2\" and \"vimdiff3\" using this new mechanism\n    (after all, the only difference among them is that they present different\n    layouts to the user)\n\n  * Add documentation to all of this.\n\nAnd the result of that work is what I'm presenting today :)\n\nSome things I would like to mention:\n\n  1. There are three commits in this patch series:\n\n     - The first one implements the logic to generate new arbitrary layouts and\n       also re-defines \"vimdiff1\", \"vimdiff2\" and \"vimdiff3\" on top of it.\n\n     - The second one adds documentation. It is probably a good idea to start\n       reviewing this commit before the first one!\n\n     - The last commit *is not meant to be merged now*. It removes \"vimdiff1\",\n       \"vimdiff2\" and \"vimdiff3\", which is something that should only be done\n       after one or two releases with a deprecation notice and only if everyone\n       agrees to do so :)\n\n  2. \"mergetools/vimdiff\" is now a ~800 lines bash script, but most of it is\n     documentation (which is embedded in the tool itself for easier maintenance)\n     and unit tests.\n     I have only tested it with bash, but I've tried not to use any command not\n     already being used somewhere else, so I expect it to work in the same\n     places it was working before (however, let me know if there are some shell\n     compatibility requirements and I'll try to check them).\n\n  3. Regarding unit tests, \"mergetool/vimdiff\" contains instructions on how to\n     run them (just call the script without arguments after making changes, to\n     make sure you didn't break anything).\n     Right now it prints \"OK\" on all test cases (obviously) [2]\n\n  3. The \"git {diff,merge}tool --tool-help\" command now also prints the\n     documentation for each tool (instead of just its name, as before).\n     You can see an example of the output here ([3] and [4])\n\nFinally, let me say that, while I like what this patch series achieves, I would\nalso *completely* understand if you decide not to merge it due to being a\ncomplex solution to a simple problem that can be solved (as I had been doing up\nuntil today) by just adding three line to one's .gitconfig.\n\n  [mergetool \"supermerge\"]\n        cmd = vim -f -d -c ...(custom complex sequence of vim commands)...\n        trustExitCode = true\n\nLet me know what you think.\n\nThanks.\n\nReferences:\n\n  [1] https://lore.kernel.org/git/20211019212020.25385-1-greenfoo@u92.eu/#r\n  [2] https://pastebin.com/kuQ5pETG\n  [3] https://pastebin.com/yvLWxeiM\n  [4] https://pastebin.com/qNc7qymp\n\n\nNew in v2:\n\n  * Remove deprecation of vimdiff{1,2,3} (they will remain for backwards\n    compatibility, but built on top of the new generic layout mechanism)\n\n  * Remove unnecessary \"IFS=\"\n\n  * Replace DEBUG --> GIT_MERGETOOL_VIMDIFF_DEBUG\n\n  * Stop using \"local\" (turns out it was not necessary)\n\n  * Stop using bash arrays (use arrays of variables instead)\n\n  * Stop using bash substring expansion (use a new \"substring\" function\n    instead).\n\n  * Refactor some of the internal loops to make the code faster. This was needed\n    because the two previous changes (specially the one where I stop using\n    \"substring expansion\") slowed down the script to a point where I had to wait\n    for a few *seconds* before the \"layout\" string was resolved (recursion +\n    bash forks are a recipe for sluggishness). Fortunately we are back to the\n    low hundreds of milliseconds range.\n\n  * Change markers:\n    - File to save: * --> @\n    - New tab     : ; --> +\n    - Vert split  : | --> ,\n    - Horz split  : - --> /\n\n  * Rewrite examples to use as few parenthesis as possible (typically zero)\n    and better explain operators priority.\n\n  * Other fixes to remove problems reported by \"shellcheck --shell=sh\" (which\n    checks syntax agains the POSIX shell spec)\n\n  * Rename \"index_...\" vars to make more obvious what they do.\n\n  * Use \"$\" inside arithmetic expressions $((...))\n    NOTE: \"shellcheck\" issues a warning stating that \"$\" is not needed inside\n    arithmetic expressions.\n\n  * Use \"test -n\" instead of \"! test -z\"\n\n  * Use \"=\" instead of \"==\" in \"test\"\n\n  * Use \"= 0\" instead of \"-eq 0\"\n\n  * Do not use \"eval\" nor \"--\" when copying a file to MERGED\n\n  * Do not use \"-o\" with grep\n\n  * Use <<-\\EOF instead of <<\\ENDOFMESSAGE and remove extra indent in \"here\n    docs\".\n\n  * Also, let me put here some answers to questions made in the replies to\n    version v1 of this patch set:\n\n    > What do backticks do in here?\n    >\n    >     some_var=(\n    >         `# Name`       \"Rick Deckard\"\n    >         `# Age`        \"Unknown\"\n    >         `# Occupation` \"Blade runner\"\n    >     )\n\n    The backticks execute the code inside, which is a comment (everything after\n    a \"#\" is considered a comment by the shell), thus it does nothing.\n\n    The `# ...` trick can be used to insert inline comments in bash. Another\n    example:\n\n        $ ls -l `# long format` -h `# human readable` *.txt\n\n    In any case, as you can see in this new revision, because I'm no longer\n    using bash arrays, this trick to comment each test case is not needed\n    anymore.\n\n    > Why is \"eval\" needed here:\n    >\n    >     eval \"$merge_tool_path\" \\\n    >              -f \"$FINAL_CMD\" \"$LOCAL\" \"$BASE\" \"$REMOTE\" \"$MERGED\"\n\n    Variable \"$FINAL_CMD\" contains a string that looks like this:\n\n        $ echo $FINAL_CMD\n        -c \"vim cmd 1 | vim cmd 2 | ...\" -c \"tabfirst\"\n\n    We need to call \"vim\" exactly like that, but if we do this...\n\n         $ vim $FINAL_CMD\n\n    ...then \"vim\" will be excev'ed with the following arguments:\n\n        1. -c\n        2. \"vim\n        3. cmd\n        4. 1\n        ...\n\n    ...instead of the desired scenario:\n\n       1. -c\n       2. cmd 1 | cmd 2 | ...\n       3. -c\n       4. tabfirst\n\n     Using \"eval\" fixes this. A shorter example shows how this works:\n\n         $ A=\"1 \\\"2 3\\\"\"\n\n         $ ls $A\n         ls: cannot access '1': No such file or directory\n         ls: cannot access '\"2': No such file or directory\n         ls: cannot access '3\"': No such file or directory\n\n         $ eval ls $A\n         ls: cannot access '1': No such file or directory\n         ls: cannot access '2 3': No such file or directory\n\n  * Finally, I think I have addressed all comments to v1 *except for two\n    things*:\n\n        1. Moving the documentation to \"Documentation/\" instead of having it\n           embedded inside the \"mergetools/vimdiff\" script.\n\n        2. Move unit tests to the \"test suit\"\n\n    For (1) I would like more details. Is the idea not to print any\n    documentation when running \"git mergetool --tool-help\" and have all the\n    details included in \"git help mergetool\" instead?\n    Right now \"git help mergetool\" does not mention any tool specific details\n    and, instead, redirects the user to \"git mergetool --tool-help\" (that's why\n    I originally placed the new documentation there).\n    In any case, all the doc has been placed on its own commit, so once we\n    decide how to proceed its just a matter of reverting it.\n\n    For (2) I still need to investigate how this is done and prepare a \"v3\" if\n    needed :)\n\n\nNew in v3:\n\n   * Rebase on top of latest master\n\n   * Documentation moved to \"Documentation/\". Running \"git mergetool\n     --tool-help\" now works like before (ie. it just lists all available merge\n     tools) except that a one line reference to a \"man\" entry is also printed\n     for those entries providing one (right now, only vimdiff):\n\n         $ git mergetool --tool-help\n         'git mergetool --tool=<tool>' may be set to one of the following:\n                         araxis\n                         meld\n                         vimdiff\n                                 Run 'man git-mergetool--vimdiff' for details\n                         vimdiff1\n                                 Run 'man git-mergetool--vimdiff' for details\n                         ...\n\n   * New test file \"t/7609-mergetool--lib.sh\" created. Right now it only\n     contains one test which calls \"mergetools/vimdiff\" with a special\n     environment set, causing layout unit tests to run.\n\nFernando Ramos (3):\n  vimdiff: new implementation with layout support\n  vimdiff: add tool documentation\n  vimdiff: integrate layout tests in the unit tests framework ('t'\n    folder)\n\n Documentation/git-difftool--vimdiff.txt  |  41 ++\n Documentation/git-mergetool--vimdiff.txt | 195 ++++++++\n git-mergetool--lib.sh                    |  14 +\n mergetools/vimdiff                       | 577 ++++++++++++++++++++++-\n t/t7609-mergetool--lib.sh                |  17 +\n 5 files changed, 819 insertions(+), 25 deletions(-)\n create mode 100644 Documentation/git-difftool--vimdiff.txt\n create mode 100644 Documentation/git-mergetool--vimdiff.txt\n create mode 100755 t/t7609-mergetool--lib.sh\n\n\nbase-commit: 35151cf0720460a897cde9b8039af364743240e7\n-- \n2.34.0\n\n"},{"id":"442675","messageId":"20211130204333.174967-2-greenfoo@u92.eu","threadId":"57003","inReplyTo":"20211130204333.174967-1-greenfoo@u92.eu","subject":"[PATCH v3 1/3] vimdiff: new implementation with layout support","fromName":"Fernando Ramos","fromEmail":"greenfoo@u92.eu","sentAt":"2021-11-30T20:43:31Z","receivedAt":"2021-11-30T20:43:50Z","isPatch":true,"sender":{"key":"greenfoo@u92.eu","avatar":"https://avatars.githubusercontent.com/u/42691086?v=4"},"body":"When running 'git mergetool -t vimdiff', a new configuration option\n('mergetool.vimdiff.layout') can now be used to select how the user\nwants the different windows, tabs and buffers to be displayed.\n\nIf the option is not provided, the layout will be the same one that was\nbeing used before this commit (ie. two rows with LOCAL, BASE and COMMIT\nin the top one and MERGED in the bottom one).\n\nThe 'vimdiff' variants ('vimdiff{1,2,3}') still work but, because they\nrepresented nothing else than different layouts, are now internally\nimplemented as a subcase of 'vimdiff' with the corresponding\npre-configured 'layout'.\n\nSigned-off-by: Fernando Ramos <greenfoo@u92.eu>\n---\n mergetools/vimdiff | 559 ++++++++++++++++++++++++++++++++++++++++++---\n 1 file changed, 532 insertions(+), 27 deletions(-)\n\ndiff --git a/mergetools/vimdiff b/mergetools/vimdiff\nindex 96f6209a04..ed0d8584f7 100644\n--- a/mergetools/vimdiff\n+++ b/mergetools/vimdiff\n@@ -1,48 +1,538 @@\n+#!/bin/bash\n+\n+# This script can be run in two different contexts:\n+#\n+#   - From git, when the user invokes the \"vimdiff\" merge tool. In this context\n+#     this script expects the following environment variables (among others) to\n+#     be defined (which is something \"git\" takes care of):\n+#\n+#       - $BASE\n+#       - $LOCAL\n+#       - $REMOTE\n+#       - $MERGED\n+#\n+#     In this mode, all this script does is to run the next command:\n+#\n+#         vim -f -c ... $LOCAL $BASE $REMOTE $MERGED\n+#\n+#     ...where the \"...\" string depends on the value of the\n+#     \"mergetool.vimdiff.layout\" configuration variable and is used to open vim\n+#     with a certain layout of buffers, windows and tabs.\n+#\n+#   - From the shell, manually. This is only expected to be done by developers\n+#     who are editing this script. When this happens, the script runs a battery\n+#     of unit tests to make sure nothing breaks.\n+#     In this context this script does not expect any particular environment\n+#     variable to be set.\n+#\n+\n+\n+\n+################################################################################\n+## Internal functions (not meant to be used outside this script)\n+################################################################################\n+\n+debug_print() { \n+\t# Send message to stderr if global variable DEBUG is set to \"true\"\n+\n+\tif test -n \"$GIT_MERGETOOL_VIMDIFF_DEBUG\"\n+\tthen\n+\t\t>&2 echo \"$@\"\n+\tfi\n+}\n+\n+substring() {\n+\t# Return a substring of $1 containing $3 characters starting at\n+\t# zero-based offset $2.\n+\t# \n+\t# Examples:\n+\t#\n+\t#   substring \"Hello world\" 0 4  --> \"Hell\"\n+\t#   substring \"Hello world\" 3 4  --> \"lo w\"\n+\t#   substring \"Hello world\" 3 10 --> \"lo world\"\n+\n+\tSTRING=$1\n+\tSTART=$2\n+\tLEN=$3\n+\n+\techo \"$STRING\" | cut -c$(( $START + 1 ))-$(( $START + $LEN))\n+}\n+\n+gen_cmd_aux() {\n+\t# Auxiliary function used from \"gen_cmd()\".\n+\t# Read that other function documentation for more details.\n+\n+\tLAYOUT=$1 \n+\tCMD=$2  # This is a second (hidden) argument used for recursion  \n+\n+\tdebug_print\n+\tdebug_print \"LAYOUT    : $LAYOUT\"\n+\tdebug_print \"CMD       : $CMD\"\n+\n+\tif test -z \"$CMD\"\n+\tthen\n+\t\tCMD=\"echo\" # vim \"nop\" operator\n+\tfi\n+\n+\tstart=0\n+\tend=${#LAYOUT}\n+\n+\tnested=0\n+\tnested_min=100\n+\n+\n+\t# Step 1:\n+\t#\n+\t# Increase/decrease \"start\"/\"end\" indices respectively to get rid of\n+\t# outer parenthesis.\n+\t#\n+\t# Example:\n+\t#\n+\t#   - BEFORE: (( LOCAL , BASE ) / MERGED )\n+\t#   - AFTER :  ( LOCAL , BASE ) / MERGED \n+\n+\toldIFS=$IFS\n+\tIFS=#\n+\tfor c in $(echo \"$LAYOUT\" | sed 's:.:&#:g')\n+\tdo\n+\t\tif test \"$c\" = \" \"\n+\t\tthen\n+\t\t\tcontinue\n+\t\tfi\n+\n+\t\tif test \"$c\" = \"(\"\n+\t\tthen\n+\t\t\tnested=$(( $nested + 1 ))\n+\t\t\tcontinue\n+\t\tfi\n+\n+\t\tif test \"$c\" = \")\"\n+\t\tthen\n+\t\t\tnested=$(( $nested - 1 ))\n+\t\t\tcontinue\n+\t\tfi\n+\n+\t\tif test \"$nested\" -lt \"$nested_min\"\n+\t\tthen\n+\t\t\tnested_min=$nested\n+\t\tfi\n+\tdone\n+\tIFS=$oldIFS\n+\n+\tdebug_print \"NESTED MIN: $nested_min\"\n+\n+\twhile test \"$nested_min\" -gt \"0\"\n+\tdo\n+\t\tstart=$(( $start + 1 ))\n+\t\tend=$(( $end - 1 ))\n+\n+\t\tstart_minus_one=$(( $start - 1 ))\n+\n+\t\twhile ! test \"$(substring \"$LAYOUT\" \"$start_minus_one\" 1)\" = \"(\"\n+\t\tdo\n+\t\t\tstart=$(( $start + 1 ))\n+\t\t\tstart_minus_one=$(( $start_minus_one + 1 ))\n+\t\tdone\n+\n+\t\twhile ! test \"$(substring \"$LAYOUT\" \"$end\" 1)\" = \")\"\n+\t\tdo\n+\t\t\tend=$(( $end - 1 ))\n+\t\tdone\n+\n+\t\tnested_min=$(( $nested_min - 1 ))\n+\tdone\n+\n+\tdebug_print \"CLEAN     : $(substring \"$LAYOUT\" \"$start\" \"$(( $end - $start ))\")\"\n+\n+\n+\t# Step 2:\n+\t#\n+\t# Search for all valid separators (\"+\", \"/\" or \",\") which are *not*\n+\t# inside parenthesis. Save the index at which each of them makes the\n+\t# first appearance.\n+\n+\tindex_new_tab=\"\"\n+\tindex_horizontal_split=\"\"\n+\tindex_vertical_split=\"\"\n+\n+\tnested=0\n+\ti=$(( $start - 1 ))\n+\n+\toldIFS=$IFS\n+\tIFS=#\n+\tfor c in $(substring \"$LAYOUT\" \"$start\" \"$(( $end - $start ))\" | sed 's:.:&#:g');\n+\tdo\n+\t\ti=$(( $i + 1 ))\n+\n+\t\tif test \"$c\" = \" \"\n+\t\tthen\n+\t\t\tcontinue\n+\t\tfi\n+\n+\t\tif test \"$c\" = \"(\"\n+\t\tthen\n+\t\t\tnested=$(( $nested + 1 ))\n+\t\t\tcontinue\n+\t\tfi\n+\n+\t\tif test \"$c\" = \")\"\n+\t\tthen\n+\t\t\tnested=$(( $nested - 1 ))\n+\t\t\tcontinue\n+\t\tfi\n+\n+\t\tif test \"$nested\" = 0\n+\t\tthen\n+\t\t\tcurrent=$c\n+\t\t\t\n+\t\t\tif test \"$current\" = \"+\"\n+\t\t\tthen\n+\t\t\t\tif test -z \"$index_new_tab\"\n+\t\t\t\tthen\n+\t\t\t\t\tindex_new_tab=$i\n+\t\t\t\tfi\n+\n+\t\t\telif test \"$current\" = \"/\"\n+\t\t\tthen\n+\t\t\t\tif test -z \"$index_horizontal_split\"\n+\t\t\t\tthen\n+\t\t\t\t\tindex_horizontal_split=$i\n+\t\t\t\tfi\n+\n+\t\t\telif test \"$current\" = \",\"\n+\t\t\tthen\n+\t\t\t\tif test -z \"$index_vertical_split\"\n+\t\t\t\tthen\n+\t\t\t\t\tindex_vertical_split=$i\n+\t\t\t\tfi\n+\t\t\tfi\n+\t\tfi\n+\tdone\n+\tIFS=$oldIFS\n+\n+\n+\t# Step 3:\n+\t#\n+\t# Process the separator with the highest order of precedence\n+\t# (\";\" has the highest precedence and \"|\" the lowest one).\n+\t#\n+\t# By \"process\" I mean recursively call this function twice: the first\n+\t# one with the substring at the left of the separator and the second one\n+\t# with the one at its right.\n+\n+\tterminate=\"false\"\n+\n+\tif ! test -z \"$index_new_tab\"\n+\tthen\n+\t\tbefore=\"-tabnew\"\n+\t\tafter=\"tabnext\"\n+\t\tindex=$index_new_tab\n+\t\tterminate=\"true\"\n+\n+\telif ! test -z \"$index_horizontal_split\"\n+\tthen\n+\t\tbefore=\"split\"\n+\t\tafter=\"wincmd j\"\n+\t\tindex=$index_horizontal_split\n+\t\tterminate=\"true\"\n+\n+\telif ! test -z \"$index_vertical_split\"\n+\tthen\n+\t\tbefore=\"vertical split\"\n+\t\tafter=\"wincmd l\"\n+\t\tindex=$index_vertical_split\n+\t\tterminate=\"true\"\n+\tfi\n+\n+\tif  test \"$terminate\" = \"true\"\n+\tthen\n+\t\tCMD=\"$CMD | $before\"\n+\t\tCMD=$(gen_cmd_aux \"$(substring \"$LAYOUT\" \"$start\" \"$(( $index - $start ))\")\" \"$CMD\")\n+\t\tCMD=\"$CMD | $after\"\n+\t\tCMD=$(gen_cmd_aux \"$(substring \"$LAYOUT\" \"$(( $index + 1 ))\" \"$(( ${#LAYOUT} - $index ))\")\" \"$CMD\")\n+\t\techo \"$CMD\"\n+\t\treturn\n+\tfi\n+\n+\n+\t# Step 4:\n+\t#\n+\t# If we reach this point, it means there are no separators and we just\n+\t# need to print the command to display the specified buffer\n+\n+\ttarget=$(substring \"$LAYOUT\" \"$start\" \"$(( $end - $start ))\" | sed 's:[ @();|-]::g')\n+\n+\tif test \"$target\" = \"LOCAL\"\n+\tthen\n+\t\tCMD=\"$CMD | 1b\"\n+\n+\telif test \"$target\" = \"BASE\"\n+\tthen\n+\t\tCMD=\"$CMD | 2b\"\n+\n+\telif test \"$target\" = \"REMOTE\"\n+\tthen\n+\t\tCMD=\"$CMD | 3b\"\n+\n+\telif test \"$target\" = \"MERGED\"\n+\tthen\n+\t\tCMD=\"$CMD | 4b\"\n+\n+\telse\n+\t\tCMD=\"$CMD | ERROR: >$target<\"\n+\tfi\n+\n+\techo \"$CMD\"\n+\treturn\n+}\n+\n+\n+gen_cmd() {\n+\t# This function returns (in global variable FINAL_CMD) the string that\n+\t# you can use when invoking \"vim\" (as shown next) to obtain a given\n+\t# layout:\n+\t#\n+\t#   $ vim -f $FINAL_CMD \"$LOCAL\" \"$BASE\" \"$REMOTE\" \"$MERGED\"\n+\t#\n+\t# It takes one single argument: a string containing the desired layout\n+\t# definition.\n+\t#\n+\t# The syntax of the \"layout definitions\" is explained in ... (TODO)...\n+\t# but you can already intuitively understand how it works by knowing\n+\t# that...\n+\t#\n+\t#   * \";\" means \"a new vim tab\"\n+\t#   * \"-\" means \"a new vim horizontal split\"\n+\t#   * \"|\" means \"a new vim vertical split\"\n+\t#\n+\t# It also returns (in global variable FINAL_TARGET) the name (\"LOCAL\",\n+\t# \"BASE\", \"REMOTE\" or \"MERGED\") of the file that is marked with an \"@\",\n+\t# or \"MERGED\" if none of them is.\n+\t#\n+\t# Example:\n+\t#\n+\t#     gen_cmd \"@LOCAL , REMOTE\"\n+\t#     |\n+\t#     `-> FINAL_CMD    == \"-c \\\"echo | vertical split | 1b | wincmd l | 3b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\t#         FINAL_TARGET == \"LOCAL\"\n+\n+\tLAYOUT=$1 \n+\n+\n+\t# Search for a \"@\" in one of the files identifiers (\"LOCAL\", \"BASE\",\n+\t# \"REMOTE\", \"MERGED\"). If not found, use \"MERGE\" as the default file\n+\t# where changes will be saved.\n+\n+\tif echo \"$LAYOUT\" | grep @LOCAL >/dev/null\n+\tthen\n+\t\tFINAL_TARGET=\"LOCAL\"\n+\telif echo \"$LAYOUT\" | grep @BASE >/dev/null\n+\tthen\n+\t\tFINAL_TARGET=\"BASE\"\n+\telse\n+\t\tFINAL_TARGET=\"MERGED\"\n+\tfi\n+\n+\n+\t# Obtain the first part of vim \"-c\" option to obtain the desired layout\n+\n+\tCMD=$(gen_cmd_aux \"$LAYOUT\")\n+\n+\n+\t# Adjust the just obtained script depending on whether more than one\n+\t# windows are visible or not\n+\n+\tif echo \"$LAYOUT\" | grep \",\\|/\" >/dev/null\n+\tthen\n+\t\tCMD=\"$CMD | tabdo windo diffthis\"\n+\telse\n+\t\tCMD=\"$CMD | bufdo diffthis\"\n+\tfi\n+\n+\n+\t# Add an extra \"-c\" option to move to the first tab (notice that we\n+\t# can't simply append the command to the previous \"-c\" string as\n+\t# explained here: https://github.com/vim/vim/issues/9076\n+\n+\tFINAL_CMD=\"-c \\\"$CMD\\\" -c \\\"tabfirst\\\"\"\n+}\n+\n+\n+run_unit_tests() {\n+\t# Function to make sure that we don't break anything when modifying this\n+\t# script.\n+\t#\n+\t# This function is automatically executed when you execute this directly\n+\t# from the shell\n+\n+\tNUMBER_OF_TEST_CASES=16\n+\n+\tTEST_CASE_01=\"LOCAL,MERGED,REMOTE\"\n+\tTEST_CASE_02=\"LOCAL/MERGED/REMOTE\"\n+\tTEST_CASE_03=\"(LOCAL/REMOTE),MERGED\"\n+\tTEST_CASE_04=\"MERGED,(LOCAL/REMOTE)\"\n+\tTEST_CASE_05=\"(LOCAL,REMOTE)/MERGED\"\n+\tTEST_CASE_06=\"MERGED/(LOCAL,REMOTE)\"\n+\tTEST_CASE_07=\"(LOCAL,BASE,REMOTE)/MERGED\"\n+\tTEST_CASE_08=\"(LOCAL/BASE/REMOTE),MERGED\"\n+\tTEST_CASE_09=\"@LOCAL,REMOTE\"\n+\tTEST_CASE_10=\"MERGED\"\n+\tTEST_CASE_11=\"(LOCAL,BASE,REMOTE)/MERGED+BASE,LOCAL+BASE,REMOTE+(LOCAL/BASE/REMOTE),MERGED\"\n+\tTEST_CASE_12=\"((LOCAL,REMOTE)/BASE),MERGED\"\n+\tTEST_CASE_13=\"((LOCAL,REMOTE)/BASE),((LOCAL/REMOTE),MERGED)\"\n+\tTEST_CASE_14=\"BASE,REMOTE+BASE,LOCAL\"\n+\tTEST_CASE_15=\"  ((  (LOCAL , BASE , REMOTE) / MERGED))   +(BASE)   , LOCAL+ BASE , REMOTE+ (((LOCAL / BASE / REMOTE)) ,    MERGED   )  \"\n+\tTEST_CASE_16=\"LOCAL,BASE,REMOTE / MERGED + BASE,LOCAL + BASE,REMOTE + (LOCAL / BASE / REMOTE),MERGED\"\n+\n+\tEXPECTED_CMD_01=\"-c \\\"echo | vertical split | 1b | wincmd l | vertical split | 4b | wincmd l | 3b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\tEXPECTED_CMD_02=\"-c \\\"echo | split | 1b | wincmd j | split | 4b | wincmd j | 3b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\tEXPECTED_CMD_03=\"-c \\\"echo | vertical split | split | 1b | wincmd j | 3b | wincmd l | 4b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\tEXPECTED_CMD_04=\"-c \\\"echo | vertical split | 4b | wincmd l | split | 1b | wincmd j | 3b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\tEXPECTED_CMD_05=\"-c \\\"echo | split | vertical split | 1b | wincmd l | 3b | wincmd j | 4b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\tEXPECTED_CMD_06=\"-c \\\"echo | split | 4b | wincmd j | vertical split | 1b | wincmd l | 3b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\tEXPECTED_CMD_07=\"-c \\\"echo | split | vertical split | 1b | wincmd l | vertical split | 2b | wincmd l | 3b | wincmd j | 4b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\tEXPECTED_CMD_08=\"-c \\\"echo | vertical split | split | 1b | wincmd j | split | 2b | wincmd j | 3b | wincmd l | 4b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\tEXPECTED_CMD_09=\"-c \\\"echo | vertical split | 1b | wincmd l | 3b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\tEXPECTED_CMD_10=\"-c \\\"echo | 4b | bufdo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\tEXPECTED_CMD_11=\"-c \\\"echo | -tabnew | split | vertical split | 1b | wincmd l | vertical split | 2b | wincmd l | 3b | wincmd j | 4b | tabnext | -tabnew | vertical split | 2b | wincmd l | 1b | tabnext | -tabnew | vertical split | 2b | wincmd l | 3b | tabnext | vertical split | split | 1b | wincmd j | split | 2b | wincmd j | 3b | wincmd l | 4b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\tEXPECTED_CMD_12=\"-c \\\"echo | vertical split | split | vertical split | 1b | wincmd l | 3b | wincmd j | 2b | wincmd l | 4b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\tEXPECTED_CMD_13=\"-c \\\"echo | vertical split | split | vertical split | 1b | wincmd l | 3b | wincmd j | 2b | wincmd l | vertical split | split | 1b | wincmd j | 3b | wincmd l | 4b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\tEXPECTED_CMD_14=\"-c \\\"echo | -tabnew | vertical split | 2b | wincmd l | 3b | tabnext | vertical split | 2b | wincmd l | 1b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\tEXPECTED_CMD_15=\"-c \\\"echo | -tabnew | split | vertical split | 1b | wincmd l | vertical split | 2b | wincmd l | 3b | wincmd j | 4b | tabnext | -tabnew | vertical split | 2b | wincmd l | 1b | tabnext | -tabnew | vertical split | 2b | wincmd l | 3b | tabnext | vertical split | split | 1b | wincmd j | split | 2b | wincmd j | 3b | wincmd l | 4b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\tEXPECTED_CMD_16=\"-c \\\"echo | -tabnew | split | vertical split | 1b | wincmd l | vertical split | 2b | wincmd l | 3b | wincmd j | 4b | tabnext | -tabnew | vertical split | 2b | wincmd l | 1b | tabnext | -tabnew | vertical split | 2b | wincmd l | 3b | tabnext | vertical split | split | 1b | wincmd j | split | 2b | wincmd j | 3b | wincmd l | 4b | tabdo windo diffthis\\\" -c \\\"tabfirst\\\"\"\n+\n+\tEXPECTED_TARGET_01=\"MERGED\"\n+\tEXPECTED_TARGET_02=\"MERGED\"\n+\tEXPECTED_TARGET_03=\"MERGED\"\n+\tEXPECTED_TARGET_04=\"MERGED\"\n+\tEXPECTED_TARGET_05=\"MERGED\"\n+\tEXPECTED_TARGET_06=\"MERGED\"\n+\tEXPECTED_TARGET_07=\"MERGED\"\n+\tEXPECTED_TARGET_08=\"MERGED\"\n+\tEXPECTED_TARGET_09=\"LOCAL\"\n+\tEXPECTED_TARGET_10=\"MERGED\"\n+\tEXPECTED_TARGET_11=\"MERGED\"\n+\tEXPECTED_TARGET_12=\"MERGED\"\n+\tEXPECTED_TARGET_13=\"MERGED\"\n+\tEXPECTED_TARGET_14=\"MERGED\"\n+\tEXPECTED_TARGET_15=\"MERGED\"\n+\tEXPECTED_TARGET_16=\"MERGED\"\n+\n+\tat_least_one_ko=\"false\"\n+\n+\tfor i in $(seq -w 1 99)\n+\tdo\n+\t\tif test \"$i\" -gt $NUMBER_OF_TEST_CASES\n+\t\tthen\n+\t\t\tbreak\n+\t\tfi\n+\n+\t\tgen_cmd \"$(eval echo \\${TEST_CASE_\"$i\"})\"\n+\n+\t\tif test \"$FINAL_CMD\" = \"$(eval echo \\${EXPECTED_CMD_\"$i\"})\" \\\n+\t\t\t&& test \"$FINAL_TARGET\" = \"$(eval echo \\${EXPECTED_TARGET_\"$i\"})\"\n+\t\tthen\n+\t\t\tprintf \"Test Case #%02d: OK\\n\" \"$(echo \"$i\" | sed 's/^0*//')\"\n+\t\telse\n+\t\t\tprintf \"Test Case #%02d: KO !!!!\\n\" \"$(echo \"$i\" | sed 's/^0*//')\"\n+\t\t\techo \"  FINAL_CMD              : $FINAL_CMD\"\n+\t\t\techo \"  FINAL_CMD (expected)   : $(eval echo \\${EXPECTED_CMD_\"$i\"})\"\n+\t\t\techo \"  FINAL_TARGET           : $FINAL_TARGET\"\n+\t\t\techo \"  FINAL_TARGET (expected): $(eval echo \\${EXPECTED_TARGET_\"$i\"})\"\n+\t\t\tat_least_one_ko=\"true\"\n+\t\tfi\n+\tdone\n+\t\n+\tif test \"$at_least_one_ko\" = \"true\"\n+\tthen\n+\t\treturn 255\n+\telse\n+\t\treturn 0\n+\tfi\n+}\n+\n+\n+################################################################################\n+## API functions (called from \"git-mergetool--lib.sh\")\n+################################################################################\n+\n diff_cmd () {\n \t\"$merge_tool_path\" -R -f -d \\\n \t\t-c 'wincmd l' -c 'cd $GIT_PREFIX' \"$LOCAL\" \"$REMOTE\"\n }\n \n+\n merge_cmd () {\n+\tlayout=$(git config mergetool.$merge_tool.layout)\n+\n \tcase \"$1\" in\n \t*vimdiff)\n-\t\tif $base_present\n+\t\tif test -z \"$layout\"\n \t\tthen\n-\t\t\t\"$merge_tool_path\" -f -d -c '4wincmd w | wincmd J' \\\n-\t\t\t\t\"$LOCAL\" \"$BASE\" \"$REMOTE\" \"$MERGED\"\n-\t\telse\n-\t\t\t\"$merge_tool_path\" -f -d -c 'wincmd l' \\\n-\t\t\t\t\"$LOCAL\" \"$MERGED\" \"$REMOTE\"\n+\t\t\t# Default layout when none is specified\n+\t\t\tlayout=\"(LOCAL,BASE,REMOTE)/MERGED\"\n \t\tfi\n \t\t;;\n \t*vimdiff1)\n-\t\t\"$merge_tool_path\" -f -d \\\n-\t\t\t-c 'echon \"Resolve conflicts leftward then save. Use :cq to abort.\"' \\\n-\t\t\t\"$LOCAL\" \"$REMOTE\"\n-\t\tret=\"$?\"\n-\t\tif test \"$ret\" -eq 0\n-\t\tthen\n-\t\t\tcp -- \"$LOCAL\" \"$MERGED\"\n-\t\tfi\n-\t\treturn \"$ret\"\n+\t\tlayout=\"@LOCAL,MERGED\"\n \t\t;;\n \t*vimdiff2)\n-\t\t\"$merge_tool_path\" -f -d -c 'wincmd l' \\\n-\t\t\t\"$LOCAL\" \"$MERGED\" \"$REMOTE\"\n+\t\tlayout=\"LOCAL,MERGED,REMOTE\"\n \t\t;;\n \t*vimdiff3)\n-\t\tif $base_present\n-\t\tthen\n-\t\t\t\"$merge_tool_path\" -f -d -c 'hid | hid | hid' \\\n-\t\t\t\t\"$LOCAL\" \"$REMOTE\" \"$BASE\" \"$MERGED\"\n-\t\telse\n-\t\t\t\"$merge_tool_path\" -f -d -c 'hid | hid' \\\n-\t\t\t\t\"$LOCAL\" \"$REMOTE\" \"$MERGED\"\n-\t\tfi\n+\t\tlayout=\"MERGED\"\n \t\t;;\n \tesac\n+\n+\tgen_cmd \"$layout\"\n+\n+\tdebug_print \"\"\n+\tdebug_print \"FINAL CMD : $FINAL_CMD\"\n+\tdebug_print \"FINAL TAR : $FINAL_TARGET\"\n+\n+\tif $base_present\n+\tthen\n+\t\teval \"$merge_tool_path\" \\\n+\t\t\t-f \"$FINAL_CMD\" \"$LOCAL\" \"$BASE\" \"$REMOTE\" \"$MERGED\"\n+\telse\n+\t\t# If there is no BASE (example: a merge conflict in a new file\n+\t\t# with the same name created in both braches which didn't exist\n+\t\t# before), close all BASE windows using vim's \"quit\" command\n+\n+\t\tFINAL_CMD=$(echo \"$FINAL_CMD\" | \\\n+\t\t\tsed -e 's:2b:quit:g' -e 's:3b:2b:g' -e 's:4b:3b:g')\n+\n+\t\teval \"$merge_tool_path\" \\\n+\t\t\t-f \"$FINAL_CMD\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"\n+\tfi\n+\n+\tret=\"$?\"\n+\n+\tif test \"$ret\" -eq 0\n+\tthen\n+\t\tcase \"$FINAL_TARGET\" in\n+\t\tLOCAL)\n+\t\t\tsource_path=\"$LOCAL\"\n+\t\t\t;;\n+\t\tREMOTE)\n+\t\t\tsource_path=\"$REMOTE\"\n+\t\t\t;;\n+\t\tMERGED|*)\n+\t\t\t# Do nothing\n+\t\t\tsource_path=\n+\t\t\t;;\n+\t\tesac\n+\n+\t\tif test -n \"$source_path\"\n+\t\tthen\n+\t\t\tcp \"$source_path\" \"$MERGED\"\n+\t\tfi\n+\tfi\n+\n+\treturn \"$ret\"\n }\n \n+\n translate_merge_tool_path() {\n \tcase \"$1\" in\n \tnvimdiff*)\n@@ -57,14 +547,29 @@ translate_merge_tool_path() {\n \tesac\n }\n \n+\n exit_code_trustable () {\n \ttrue\n }\n \n+\n list_tool_variants () {\n-\tfor prefix in '' g n; do\n-\t\tfor suffix in '' 1 2 3; do\n+\tfor prefix in '' g n\n+\tdo\n+\t\tfor suffix in '' 1 2 3\n+\t\tdo\n \t\t\techo \"${prefix}vimdiff${suffix}\"\n \t\tdone\n \tdone\n }\n+\n+\n+################################################################################\n+## Run unit tests when calling this script with a special env variable set\n+################################################################################\n+\n+if test -n \"$GIT_MERGETOOL_VIMDIFF_UNITTESTS\"\n+then\n+\trun_unit_tests\n+fi\n+\n-- \n2.34.0\n\n"},{"id":"442676","messageId":"20211130204333.174967-3-greenfoo@u92.eu","threadId":"57003","inReplyTo":"20211130204333.174967-1-greenfoo@u92.eu","subject":"[PATCH v3 2/3] vimdiff: add tool documentation","fromName":"Fernando Ramos","fromEmail":"greenfoo@u92.eu","sentAt":"2021-11-30T20:43:32Z","receivedAt":"2021-11-30T20:43:53Z","isPatch":true,"sender":{"key":"greenfoo@u92.eu","avatar":"https://avatars.githubusercontent.com/u/42691086?v=4"},"body":"Running 'git {merge,diff}tool --tool-help' now also prints usage\ninformation about the vimdiff tool (and its variantes) instead of just\nits name.\n\nTwo new functions ('diff_cmd_help()' and 'merge_cmd_help()') have been\nadded to the set of functions that each merge tool (ie. scripts found\ninside \"mergetools/\") can overwrite to provided tool specific\ninformation.\n\nRight now, only 'mergetools/vimdiff' implements these functions, but\nother tools are encouraged to do so in the future, specially if they\ntake configuration options not explained anywhere else (as it is the\ncase with the 'vimdiff' tool and the new 'layout' option)\n\nSigned-off-by: Fernando Ramos <greenfoo@u92.eu>\n---\n Documentation/git-difftool--vimdiff.txt  |  41 +++++\n Documentation/git-mergetool--vimdiff.txt | 195 +++++++++++++++++++++++\n git-mergetool--lib.sh                    |  14 ++\n mergetools/vimdiff                       |  22 +++\n 4 files changed, 272 insertions(+)\n create mode 100644 Documentation/git-difftool--vimdiff.txt\n create mode 100644 Documentation/git-mergetool--vimdiff.txt\n\ndiff --git a/Documentation/git-difftool--vimdiff.txt b/Documentation/git-difftool--vimdiff.txt\nnew file mode 100644\nindex 0000000000..b520129ee5\n--- /dev/null\n+++ b/Documentation/git-difftool--vimdiff.txt\n@@ -0,0 +1,41 @@\n+git-mergetool{litdd}vimdiff(1)\n+==============================\n+\n+NAME\n+----\n+git-difftool--vimdiff - Helper script to run vim as a diff tool\n+\n+SYNOPSIS\n+--------\n+[verse]\n+'git difftool' --tool=vimdiff ... (rest of 'git difftool' options)\n+\n+DESCRIPTION\n+-----------\n+When specifying '--tool=vimdiff' in 'git difftool', git will open vim with a\n+with two vertical windows: LOCAL changes will be placed in the left window and\n+REMOTE changes in the right one.\n+\n+           ------------------------------------------\n+           |                   |                    |\n+           |                   |                    |\n+           |                   |                    |\n+           |     LOCAL         |    REMOTE          |\n+           |                   |                    |\n+           |                   |                    |\n+           |                   |                    |\n+           ------------------------------------------\n+\n+VARIANTS\n+--------\n+Instead of '--tool=vimdiff', you can also use these other variants:\n+\n+  * '--tool=gvimdiff', to open gvim instead of vim.\n+\n+  * '--tool=nvimdiff', to open nvim ('neovim') instead of vim.\n+\n+GIT\n+---\n+Part of the linkgit:git[1] suite\n+\n+\ndiff --git a/Documentation/git-mergetool--vimdiff.txt b/Documentation/git-mergetool--vimdiff.txt\nnew file mode 100644\nindex 0000000000..b7b419464d\n--- /dev/null\n+++ b/Documentation/git-mergetool--vimdiff.txt\n@@ -0,0 +1,195 @@\n+git-difftool{litdd}vimdiff(1)\n+=============================\n+\n+NAME\n+----\n+git-mergetool--vimdiff - Helper script to run vim as a merge tool\n+\n+SYNOPSIS\n+--------\n+[verse]\n+'git mergetool' --tool=vimdiff ... (rest of 'git mergetool' options)\n+\n+DESCRIPTION\n+-----------\n+When specifying '--tool=vimdiff' in 'git mergetool' git will open vim with a 4\n+windows layout distributed in the following way:\n+\n+    ------------------------------------------\n+    |             |           |              |\n+    |   LOCAL     |   BASE    |   REMOTE     |\n+    |             |           |              |\n+    ------------------------------------------\n+    |                                        |\n+    |                MERGED                  |\n+    |                                        |\n+    ------------------------------------------\n+\n+\"LOCAL\", \"BASE\" and \"REMOTE\" are read-only buffers showing the contents of the\n+conflicting file in a specific git commit (\"commit you are merging into\",\n+\"common ancestor commit\" and \"commit you are merging from\" respectively)\n+\n+\"MERGED\" is a writable buffer where you have to resolve the conflicts (using the\n+other read-only buffers as a reference). Once you are done, save and exit \"vim\"\n+as usual (\":wq\") or, if you want to abort, exit using \":cq\".\n+\n+LAYOUT CONFIGURATION\n+--------------------\n+You can change the windows layout used by vim by setting configuration variable\n+\"mergetool.vimdiff.layout\" which accepts a string where the following separators\n+have special meaning:\n+\n+  - \"+\" is used to \"open a new tab\"\n+  - \",\" is used to \"open a new vertical split\"\n+  - \"/\" is used to \"open a new horizontal split\"\n+  - \"@\" is used to indicate which is the file containing the final version after\n+    solving the conflicts. In not present, \"MERGED\" will be used by default.\n+\n+The precedence of the operators is this one (you can use parenthesis to change\n+it):\n+\n+    @ > + > / > ,\n+\n+Let's see some examples to undertand how it works:\n+\n+  * layout = \"(LOCAL,BASE,REMOTE)/MERGED\"\n+\n+    This is exactly the same as the default layout we have already seen.\n+\n+    Note that \"/\" has precedence over \",\" and thus the parenthesis are not\n+    needed in this case. The next layout definition is equivalent:\n+\n+        layout = \"LOCAL,BASE,REMOTE / MERGED\"\n+\n+  * layout = \"LOCAL,MERGED,REMOTE\"\n+\n+    If, for some reason, we are not interested in the \"BASE\" buffer.\n+\n+           ------------------------------------------\n+           |             |           |              |\n+           |             |           |              |\n+           |   LOCAL     |   MERGED  |   REMOTE     |\n+           |             |           |              |\n+           |             |           |              |\n+           ------------------------------------------\n+\n+  * layout = \"MERGED\"\n+\n+    Only the \"MERGED\" buffer will be shown. Note, however, that all the other\n+    ones are still loaded in vim, and you can access them with the \"buffers\"\n+    command. \n+\n+           ------------------------------------------\n+           |                                        |\n+           |                                        |\n+           |                 MERGED                 |\n+           |                                        |\n+           |                                        |\n+           ------------------------------------------\n+\n+  * layout = \"@LOCAL,REMOTE\"\n+\n+    When \"MERGED\" is not present in the layout, you must \"mark\" one of the\n+    buffers with an asterisk. That will become the buffer you need to edit and\n+    save after resolving the conflicts.\n+\n+           ------------------------------------------\n+           |                   |                    |\n+           |                   |                    |\n+           |                   |                    |\n+           |     LOCAL         |    REMOTE          |\n+           |                   |                    |\n+           |                   |                    |\n+           |                   |                    |\n+           ------------------------------------------\n+\n+  * layout = \"LOCAL,BASE,REMOTE / MERGED + BASE,LOCAL + BASE,REMOTE\"\n+\n+    Three tabs will open: the first one is a copy of the default layout, while\n+    the other two only show the differences between \"BASE\" and \"LOCAL\" and\n+    \"BASE\" and \"REMOTE\" respectively.\n+ \n+           ------------------------------------------\n+           | <TAB #1> |  TAB #2  |  TAB #3  |       |\n+           ------------------------------------------\n+           |             |           |              |\n+           |   LOCAL     |   BASE    |   REMOTE     |\n+           |             |           |              |\n+           ------------------------------------------\n+           |                                        |\n+           |                MERGED                  |\n+           |                                        |\n+           ------------------------------------------\n+\n+           ------------------------------------------\n+           |  TAB #1  | <TAB #2> |  TAB #3  |       |\n+           ------------------------------------------\n+           |                   |                    |\n+           |                   |                    |\n+           |                   |                    |\n+           |     BASE          |    LOCAL           |\n+           |                   |                    |\n+           |                   |                    |\n+           |                   |                    |\n+           ------------------------------------------\n+\n+           ------------------------------------------\n+           |  TAB #1  |  TAB #2  | <TAB #3> |       |\n+           ------------------------------------------\n+           |                   |                    |\n+           |                   |                    |\n+           |                   |                    |\n+           |     BASE          |    REMOTE          |\n+           |                   |                    |\n+           |                   |                    |\n+           |                   |                    |\n+           ------------------------------------------\n+\n+  * layout = \"LOCAL,BASE,REMOTE / MERGED + BASE,LOCAL + BASE,REMOTE + (LOCAL/BASE/REMOTE),MERGED\"\n+\n+    Same as the previous example, but adds a fourth tab with the same\n+    information as the first tab, with a different layout.\n+ \n+           ---------------------------------------------\n+           |  TAB #1  |  TAB #2  |  TAB #3  | <TAB #4> |\n+           ---------------------------------------------\n+           |       LOCAL         |                     |\n+           |---------------------|                     |\n+           |       BASE          |        MERGED       |\n+           |---------------------|                     |\n+           |       REMOTE        |                     |\n+           ---------------------------------------------\n+\n+    Note how in the third tab definition we need to use parenthesis to make \",\"\n+    more prioritary than \"/\".\n+\n+VARIANTS\n+--------\n+Instead of '--tool=vimdiff', you can also use these other variants:\n+\n+  * '--tool=gvimdiff', to open gvim instead of vim.\n+\n+  * '--tool=nvimdiff', to open nvim ('neovim') instead of vim.\n+\n+When using these variants, in order to specify a custom layout you will have to\n+set configuration variables 'mergetool.gvimdiff.layout' and\n+'mergetool.nvimdiff.layout' instead of 'mergetool.vimdiff.layout'\n+\n+In addition, for backwards compatibility with previous git versions, you can\n+also append '1', '2' or '3' to either 'vimdiff' or any of the variants (ex:\n+'vimdiff3', 'nvimdiff1', etc...) to use a predefined layout.\n+In other words, using '--tool=[g,n,]vimdiffx' is the same as using\n+'--tool=[g,n,]vimdiff' and setting configuration variable\n+'mergetool.[g,n,]vimdiff.layout' to... \n+\n+  * x=1 --> \"@LOCAL, REMOTE\"\n+  * x=2 --> \"LOCAL, MERGED, REMOTE\"\n+  * x=3 --> \"MERGED\"\n+\n+Example: using '--tool=gvimdiff2' will open 'gvim' with three columns (LOCAL,\n+MERGED and REMOTE).\n+\n+GIT\n+---\n+Part of the linkgit:git[1] suite\n+\ndiff --git a/git-mergetool--lib.sh b/git-mergetool--lib.sh\nindex 542a6a75eb..d7420c5d1a 100644\n--- a/git-mergetool--lib.sh\n+++ b/git-mergetool--lib.sh\n@@ -64,6 +64,12 @@ $(list_tool_variants)\"\n \t\t\t\tfi\n \t\t\t\tshown_any=yes\n \t\t\t\tprintf \"%s%s\\n\" \"$per_line_prefix\" \"$toolname\"\n+\n+                                (diff_mode && diff_cmd_help \"$toolname\" || merge_cmd_help \"$toolname\") |\n+\t\t\t\twhile read -r line\n+\t\t\t\tdo\n+\t\t\t\t\tprintf \"%s\\t%s\\n\" \"$per_line_prefix\" \"$line\"\n+\t\t\t\tdone\n \t\t\tfi\n \t\tdone\n \n@@ -162,10 +168,18 @@ setup_tool () {\n \t\treturn 1\n \t}\n \n+\tdiff_cmd_help () {\n+\t\treturn 0\n+\t}\n+\n \tmerge_cmd () {\n \t\treturn 1\n \t}\n \n+\tmerge_cmd_help () {\n+\t\treturn 0\n+\t}\n+\n \thide_resolved_enabled () {\n \t\treturn 0\n \t}\ndiff --git a/mergetools/vimdiff b/mergetools/vimdiff\nindex ed0d8584f7..8614253ef1 100644\n--- a/mergetools/vimdiff\n+++ b/mergetools/vimdiff\n@@ -462,6 +462,17 @@ diff_cmd () {\n }\n \n \n+diff_cmd_help() {\n+\tcase \"$1\" in\n+\t*)\n+\t\techo \"Run 'man git-difftool--vimdiff' for details\"\n+\t\t;;\n+\tesac\n+\n+\treturn 0\n+}\n+\n+\n merge_cmd () {\n \tlayout=$(git config mergetool.$merge_tool.layout)\n \n@@ -533,6 +544,17 @@ merge_cmd () {\n }\n \n \n+merge_cmd_help() {\n+\tcase \"$1\" in\n+\t*)\n+\t\techo \"Run 'man git-mergetool--vimdiff' for details\"\n+\t\t;;\n+\tesac\n+\n+\treturn 0\n+}\n+\n+\n translate_merge_tool_path() {\n \tcase \"$1\" in\n \tnvimdiff*)\n-- \n2.34.0\n\n"},{"id":"442677","messageId":"20211130204333.174967-4-greenfoo@u92.eu","threadId":"57003","inReplyTo":"20211130204333.174967-1-greenfoo@u92.eu","subject":"[PATCH v3 3/3] vimdiff: integrate layout tests in the unit tests framework ('t' folder)","fromName":"Fernando Ramos","fromEmail":"greenfoo@u92.eu","sentAt":"2021-11-30T20:43:33Z","receivedAt":"2021-11-30T20:43:55Z","isPatch":true,"sender":{"key":"greenfoo@u92.eu","avatar":"https://avatars.githubusercontent.com/u/42691086?v=4"},"body":"Create a new test case file for the different available merge tools.\nRight now it only tests the 'mergetool.vimdiff.layout' options. Other\nmerge tools might be interested in adding their own tests here too.\n\nSigned-off-by: Fernando Ramos <greenfoo@u92.eu>\n---\n mergetools/vimdiff        |  2 +-\n t/t7609-mergetool--lib.sh | 17 +++++++++++++++++\n 2 files changed, 18 insertions(+), 1 deletion(-)\n create mode 100755 t/t7609-mergetool--lib.sh\n\ndiff --git a/mergetools/vimdiff b/mergetools/vimdiff\nindex 8614253ef1..5085e4ad0e 100644\n--- a/mergetools/vimdiff\n+++ b/mergetools/vimdiff\n@@ -552,6 +552,7 @@ merge_cmd_help() {\n \tesac\n \n \treturn 0\n+\n }\n \n \n@@ -594,4 +595,3 @@ if test -n \"$GIT_MERGETOOL_VIMDIFF_UNITTESTS\"\n then\n \trun_unit_tests\n fi\n-\ndiff --git a/t/t7609-mergetool--lib.sh b/t/t7609-mergetool--lib.sh\nnew file mode 100755\nindex 0000000000..35cc287124\n--- /dev/null\n+++ b/t/t7609-mergetool--lib.sh\n@@ -0,0 +1,17 @@\n+#!/bin/sh\n+\n+test_description='git mergetool\n+\n+Testing basic merge tools options'\n+\n+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'mergetool --tool=vimdiff creates the expected layout' '\n+\tGIT_MERGETOOL_VIMDIFF_UNITTESTS=1 bash $GIT_BUILD_DIR/mergetools/vimdiff\n+'\n+\n+test_done\n+\n-- \n2.34.0\n\n"},{"id":"442740","messageId":"CAPig+cQg=P1BNCwfmNAE3oHZHh4CPJ5QNQh-vgfOQDGGwenMow@mail.gmail.com","threadId":"57003","inReplyTo":"20211130204333.174967-1-greenfoo@u92.eu","subject":"Re: [PATCH v3 0/3] vimdiff: new implementation with layout support","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2021-12-01T05:03:52Z","receivedAt":"2021-12-01T05:04:05Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Tue, Nov 30, 2021 at 3:43 PM Fernando Ramos <greenfoo@u92.eu> wrote:\n>   * Use \"$\" inside arithmetic expressions $((...))\n>     NOTE: \"shellcheck\" issues a warning stating that \"$\" is not needed inside\n>     arithmetic expressions.\n\nJust a side note: I understand that [1] recommended adding the \"$\"\ninside arithmetic expressions, but by chance I happened to be browsing\nthe history of CodingGuidelines and ran across 32b5fe7f0e\n(CodingGuidelines: drop arithmetic expansion advice to use \"$x\",\n2020-05-04) which dropped[2] the recommendation to use \"$\" inside the\nexpression. Anyhow, certainly not worth a re-roll.\n\nBy the way, since you've removed Bashisms, do you still need the\n`#!/bin/bash` at the top of the script?\n\n[1]: https://lore.kernel.org/git/CAJDDKr5KZK3wpox6FJPOL9koPr=k5+qTzZtPPFEmg4iZsvwoog@mail.gmail.com/\n[2]: https://lore.kernel.org/git/20200504160709.GB12842@coredump.intra.peff.net/\n"},{"id":"442785","messageId":"YafDzJmSHoiJFmKT@zacax395.localdomain","threadId":"57003","inReplyTo":"CAPig+cQg=P1BNCwfmNAE3oHZHh4CPJ5QNQh-vgfOQDGGwenMow@mail.gmail.com","subject":"Re: [PATCH v3 0/3] vimdiff: new implementation with layout support","fromName":"Fernando Ramos","fromEmail":"greenfoo@u92.eu","sentAt":"2021-12-01T18:49:48Z","receivedAt":"2021-12-01T18:49:57Z","isPatch":true,"sender":{"key":"greenfoo@u92.eu","avatar":"https://avatars.githubusercontent.com/u/42691086?v=4"},"body":"On 21/12/01 12:03AM, Eric Sunshine wrote:\n> Just a side note: I understand that [1] recommended adding the \"$\"\n> inside arithmetic expressions, but by chance I happened to be browsing\n> the history of CodingGuidelines and ran across 32b5fe7f0e\n> (CodingGuidelines: drop arithmetic expansion advice to use \"$x\",\n> 2020-05-04) which dropped[2] the recommendation to use \"$\" inside the\n> expression. Anyhow, certainly not worth a re-roll.\n\nI don't mind preparing a v4 with this change. I also think it makes the code\ncleaner. I'll do that :)\n\n\n> By the way, since you've removed Bashisms, do you still need the\n> `#!/bin/bash` at the top of the script?\n\nGood catch. Will do.\n\nI'll wait a few more days for extra comments and prepare a v4 with these\nchanges. Thanks for the suggestions.\n\n"},{"id":"442806","messageId":"211201.86ilw89fuz.gmgdl@evledraar.gmail.com","threadId":"57003","inReplyTo":"20211130204333.174967-4-greenfoo@u92.eu","subject":"Re: [PATCH v3 3/3] vimdiff: integrate layout tests in the unit tests framework ('t' folder)","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-12-01T21:25:15Z","receivedAt":"2021-12-01T21:26:17Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Tue, Nov 30 2021, Fernando Ramos wrote:\n\n> Create a new test case file for the different available merge tools.\n> Right now it only tests the 'mergetool.vimdiff.layout' options. Other\n> merge tools might be interested in adding their own tests here too.\n>\n> Signed-off-by: Fernando Ramos <greenfoo@u92.eu>\n> ---\n>  mergetools/vimdiff        |  2 +-\n>  t/t7609-mergetool--lib.sh | 17 +++++++++++++++++\n>  2 files changed, 18 insertions(+), 1 deletion(-)\n>  create mode 100755 t/t7609-mergetool--lib.sh\n>\n> diff --git a/mergetools/vimdiff b/mergetools/vimdiff\n> index 8614253ef1..5085e4ad0e 100644\n> --- a/mergetools/vimdiff\n> +++ b/mergetools/vimdiff\n> @@ -552,6 +552,7 @@ merge_cmd_help() {\n>  \tesac\n>  \n>  \treturn 0\n> +\n>  }\n>  \n>  \n> @@ -594,4 +595,3 @@ if test -n \"$GIT_MERGETOOL_VIMDIFF_UNITTESTS\"\n>  then\n>  \trun_unit_tests\n>  fi\n> -\n\nStray unrelated whitespace changes...\n\n> diff --git a/t/t7609-mergetool--lib.sh b/t/t7609-mergetool--lib.sh\n> new file mode 100755\n> index 0000000000..35cc287124\n> --- /dev/null\n> +++ b/t/t7609-mergetool--lib.sh\n> @@ -0,0 +1,17 @@\n> +#!/bin/sh\n> +\n> +test_description='git mergetool\n> +\n> +Testing basic merge tools options'\n> +\n> +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n> +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n\nYou don't seem to need this particular boilerplate, no?\n\n> +\n> +. ./test-lib.sh\n> +\n> +test_expect_success 'mergetool --tool=vimdiff creates the expected layout' '\n> +\tGIT_MERGETOOL_VIMDIFF_UNITTESTS=1 bash $GIT_BUILD_DIR/mergetools/vimdiff\n\nDon't use \"bash\", or if you do there should be appropriate prereqs to\ncheck whether bash is there etc. See some existing tests that really do\nmean to use it.\n"},{"id":"442807","messageId":"211201.86ee6w9fq8.gmgdl@evledraar.gmail.com","threadId":"57003","inReplyTo":"20211130204333.174967-3-greenfoo@u92.eu","subject":"Re: [PATCH v3 2/3] vimdiff: add tool documentation","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-12-01T21:27:42Z","receivedAt":"2021-12-01T21:29:12Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\n> +                                (diff_mode && diff_cmd_help \"$toolname\" || merge_cmd_help \"$toolname\") |\n\nMis-indenting with spaces v.s. tabs below.\n\nThen this:\n\n> +\tdiff_cmd_help () {\n> +\t\treturn 0\n> +\t}\n> +\n>  \tmerge_cmd () {\n>  \t\treturn 1\n>  \t}\n>  \n> +\tmerge_cmd_help () {\n> +\t\treturn 0\n> +\t}\n> +\n>  \thide_resolved_enabled () {\n>  \t\treturn 0\n>  \t}\n> diff --git a/mergetools/vimdiff b/mergetools/vimdiff\n> index ed0d8584f7..8614253ef1 100644\n> --- a/mergetools/vimdiff\n> +++ b/mergetools/vimdiff\n> @@ -462,6 +462,17 @@ diff_cmd () {\n>  }\n>  \n>  \n> +diff_cmd_help() {\n> +\tcase \"$1\" in\n> +\t*)\n> +\t\techo \"Run 'man git-difftool--vimdiff' for details\"\n> +\t\t;;\n> +\tesac\n> +\n> +\treturn 0\n> +}\n> +\n> +\n>  merge_cmd () {\n>  \tlayout=$(git config mergetool.$merge_tool.layout)\n>  \n> @@ -533,6 +544,17 @@ merge_cmd () {\n>  }\n>  \n>  \n> +merge_cmd_help() {\n> +\tcase \"$1\" in\n> +\t*)\n> +\t\techo \"Run 'man git-mergetool--vimdiff' for details\"\n> +\t\t;;\n> +\tesac\n> +\n> +\treturn 0\n> +}\n> +\n> +\n\nIsn't the case/esac in those functions entirely redundant and they could\njust \"echo\"?\n"},{"id":"443069","messageId":"YasnfjKqEmI+7rLG@zacax395.localdomain","threadId":"57003","inReplyTo":"211201.86ee6w9fq8.gmgdl@evledraar.gmail.com","subject":"Re: [PATCH v3 2/3] vimdiff: add tool documentation","fromName":"Fernando Ramos","fromEmail":"greenfoo@u92.eu","sentAt":"2021-12-04T08:31:58Z","receivedAt":"2021-12-04T08:32:07Z","isPatch":true,"sender":{"key":"greenfoo@u92.eu","avatar":"https://avatars.githubusercontent.com/u/42691086?v=4"},"body":"On 21/12/01 10:27PM, Ævar Arnfjörð Bjarmason wrote:\n> \n> > +merge_cmd_help() {\n> > +\tcase \"$1\" in\n> > +\t*)\n> > +\t\techo \"Run 'man git-mergetool--vimdiff' for details\"\n> > +\t\t;;\n> > +\tesac\n> > +\n> > +\treturn 0\n> > +}\n> > +\n> > +\n> \n> Isn't the case/esac in those functions entirely redundant and they could\n> just \"echo\"?\n\nYes, I originally left it here in case we wanted to print different messages for\ndifferent variants (vimdiff, vimdiff2, ...).\n\nBut now that the documentation has been placed into a single file, I don't see\na reason to keep this. I'll remove it. Thanks for noticing.\n"}]}