{"thread":{"id":"57585","subject":"[PATCH v5 0/3] vimdiff: new implementation with layout support","startedAt":"2022-03-19T09:11:57Z","lastAt":"2022-03-24T20:50:47Z","messageCount":8,"participants":["Fernando Ramos","Junio C Hamano","Fernando"],"isPatch":true,"patchVersion":5,"patchTotal":3},"messages":[{"id":"451666","messageId":"20220319091141.4911-1-greenfoo@u92.eu","threadId":"57585","inReplyTo":null,"subject":"[PATCH v5 0/3] vimdiff: new implementation with layout support","fromName":"Fernando Ramos","fromEmail":"greenfoo@u92.eu","sentAt":"2022-03-19T09:11:38Z","receivedAt":"2022-03-19T09:11:57Z","isPatch":true,"sender":{"key":"greenfoo@u92.eu","avatar":"https://avatars.githubusercontent.com/u/42691086?v=4"},"body":"Hi everyone.\n\nA few months ago I presented a patch series that reached v4, but then feedback\nslowly died (see [5])\n\nI'm re-posting a v5 (which is just a rebase of v4 on top of master) in case\nthere is still interest in considered this feature for inclusion.\n\nAs I said originally, I would completely understand if you decided not to merge\nthis work, as similar functionality can already be obtained by editing\n\".gitconfig\" (this is all explained below).\n\nAnyway... this is the text from the original RFC and subsequent revisions:\n\n---\n\nA 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\nindividual 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  [5] https://lore.kernel.org/git/YdSwXJzwuVM%2FMCAZ@zacax395.localdomain/\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 for\n    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\n\nNew in v4:\n\n  * Rebase on top of latest master\n\n  * Stop using \"$\" inside arithmetic expressions.\n\n  * Remove \"!/bin/bash\" from the top of the \"vimdiff\" script.\n\n  * Update comment explaining how the \"vimdiff\" script can be run.\n\n  * Fix spaces/tabs.\n\n  * Change the way unit tests are run (now the script is directly sourced\n    instead of calling bash)\n\n  * Remove unnecessary \"case/esac\" structure.\n\n  * Fix typo in documentation\n\n\nNew in v5:\n\n  * Rebase on top of latest master\n\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  |  40 ++\n Documentation/git-mergetool--vimdiff.txt | 195 ++++++++\n git-mergetool--lib.sh                    |  14 +\n mergetools/vimdiff                       | 560 +++++++++++++++++++++--\n t/t7609-mergetool--lib.sh                |  15 +\n 5 files changed, 797 insertions(+), 27 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-- \n2.35.1\n\n"},{"id":"451667","messageId":"20220319091141.4911-2-greenfoo@u92.eu","threadId":"57585","inReplyTo":"20220319091141.4911-1-greenfoo@u92.eu","subject":"[PATCH v5 1/3] vimdiff: new implementation with layout support","fromName":"Fernando Ramos","fromEmail":"greenfoo@u92.eu","sentAt":"2022-03-19T09:11:39Z","receivedAt":"2022-03-19T09:11:59Z","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 | 548 ++++++++++++++++++++++++++++++++++++++++++---\n 1 file changed, 521 insertions(+), 27 deletions(-)\n\ndiff --git a/mergetools/vimdiff b/mergetools/vimdiff\nindex 96f6209a04..5bf77a5388 100644\n--- a/mergetools/vimdiff\n+++ b/mergetools/vimdiff\n@@ -1,48 +1,440 @@\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 unit tests framework (\"t\" folder) by sourcing this script and\n+#     then manually calling \"run_unit_tests\", which will run a battery of unit\n+#     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+## 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+################################################################################\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 +449,116 @@ 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+## Unit tests (called from scripts inside the \"t\" folder)\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-- \n2.35.1\n\n"},{"id":"451668","messageId":"20220319091141.4911-3-greenfoo@u92.eu","threadId":"57585","inReplyTo":"20220319091141.4911-1-greenfoo@u92.eu","subject":"[PATCH v5 2/3] vimdiff: add tool documentation","fromName":"Fernando Ramos","fromEmail":"greenfoo@u92.eu","sentAt":"2022-03-19T09:11:40Z","receivedAt":"2022-03-19T09:12:06Z","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  |  40 +++++\n Documentation/git-mergetool--vimdiff.txt | 195 +++++++++++++++++++++++\n git-mergetool--lib.sh                    |  14 ++\n mergetools/vimdiff                       |  12 ++\n 4 files changed, 261 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..5788f8e149\n--- /dev/null\n+++ b/Documentation/git-difftool--vimdiff.txt\n@@ -0,0 +1,40 @@\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+\ndiff --git a/Documentation/git-mergetool--vimdiff.txt b/Documentation/git-mergetool--vimdiff.txt\nnew file mode 100644\nindex 0000000000..fb69443d3d\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 understand 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+    have precedence over \"/\".\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..956c276e1d 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+\t\t\t\t(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 5bf77a5388..db6cb75728 100644\n--- a/mergetools/vimdiff\n+++ b/mergetools/vimdiff\n@@ -364,6 +364,12 @@ diff_cmd () {\n }\n \n \n+diff_cmd_help() {\n+\techo \"Run 'man git-difftool--vimdiff' for details\"\n+\treturn 0\n+}\n+\n+\n merge_cmd () {\n \tlayout=$(git config mergetool.$merge_tool.layout)\n \n@@ -435,6 +441,12 @@ merge_cmd () {\n }\n \n \n+merge_cmd_help() {\n+\techo \"Run 'man git-mergetool--vimdiff' for details\"\n+\treturn 0\n+}\n+\n+\n translate_merge_tool_path() {\n \tcase \"$1\" in\n \tnvimdiff*)\n-- \n2.35.1\n\n"},{"id":"451669","messageId":"20220319091141.4911-4-greenfoo@u92.eu","threadId":"57585","inReplyTo":"20220319091141.4911-1-greenfoo@u92.eu","subject":"[PATCH v5 3/3] vimdiff: integrate layout tests in the unit tests framework ('t' folder)","fromName":"Fernando Ramos","fromEmail":"greenfoo@u92.eu","sentAt":"2022-03-19T09:11:41Z","receivedAt":"2022-03-19T09:12:07Z","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 t/t7609-mergetool--lib.sh | 15 +++++++++++++++\n 1 file changed, 15 insertions(+)\n create mode 100755 t/t7609-mergetool--lib.sh\n\ndiff --git a/t/t7609-mergetool--lib.sh b/t/t7609-mergetool--lib.sh\nnew file mode 100755\nindex 0000000000..8cfc22e27c\n--- /dev/null\n+++ b/t/t7609-mergetool--lib.sh\n@@ -0,0 +1,15 @@\n+#!/bin/sh\n+\n+test_description='git mergetool\n+\n+Testing basic merge tools options'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'mergetool --tool=vimdiff creates the expected layout' '\n+\t. $GIT_BUILD_DIR/mergetools/vimdiff && \n+\trun_unit_tests\n+'\n+\n+test_done\n+\n-- \n2.35.1\n\n"},{"id":"452003","messageId":"xmqqsfr8sjpl.fsf@gitster.g","threadId":"57585","inReplyTo":"20220319091141.4911-2-greenfoo@u92.eu","subject":"Re: [PATCH v5 1/3] vimdiff: new implementation with layout support","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-03-23T16:43:34Z","receivedAt":"2022-03-23T16:43:42Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Fernando Ramos <greenfoo@u92.eu> writes:\n\n> When running 'git mergetool -t vimdiff', a new configuration option\n> ('mergetool.vimdiff.layout') can now be used to select how the user\n> wants the different windows, tabs and buffers to be displayed.\n>\n> If the option is not provided, the layout will be the same one that was\n> being used before this commit (ie. two rows with LOCAL, BASE and COMMIT\n> in the top one and MERGED in the bottom one).\n>\n> The 'vimdiff' variants ('vimdiff{1,2,3}') still work but, because they\n> represented nothing else than different layouts, are now internally\n> implemented as a subcase of 'vimdiff' with the corresponding\n> pre-configured 'layout'.\n>\n> Signed-off-by: Fernando Ramos <greenfoo@u92.eu>\n> ---\n>  mergetools/vimdiff | 548 ++++++++++++++++++++++++++++++++++++++++++---\n>  1 file changed, 521 insertions(+), 27 deletions(-)\n>\n> diff --git a/mergetools/vimdiff b/mergetools/vimdiff\n> index 96f6209a04..5bf77a5388 100644\n> --- a/mergetools/vimdiff\n> +++ b/mergetools/vimdiff\n> @@ -1,48 +1,440 @@\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 unit tests framework (\"t\" folder) by sourcing this script and\n> +#     then manually calling \"run_unit_tests\", which will run a battery of unit\n> +#     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> +## 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\nDo we want to keep this helper, and many calls to it sprinkled in\nthis file, or are they leftover cruft?\n\nStyle.  \"debug_print () {\", i.e. SPACE on both sides of \"()\".\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\nThe lack of space before the second closing \"))\" makes it look\ninconsistent.  We should be able to do this no external commands\nand just two variable substitutions and without relying on\nbash-isms, but the above should do.\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> ...\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\nI wonder if there were an easy way to \"compare\" the $FINAL_CMD this\nnew code feeds to $merge_tool_path and what was fed to it by the\noriginal code to show that we are not regressing what the end user\nsees.\n\nThe \"run_unit_tests()\" only compares the cmd generated for each\ngiven layout, but the original vimdiff$N didn't express them in\nterms of $layout this patch introduces, so unfortunately that is not\nit.\n\nIdeas?\n"},{"id":"452109","messageId":"29c787c2-8916-4d04-85c1-a4c0597b9848@www.fastmail.com","threadId":"57585","inReplyTo":"xmqqsfr8sjpl.fsf@gitster.g","subject":"Re: [PATCH v5 1/3] vimdiff: new implementation with layout support","fromName":"Fernando","fromEmail":"greenfoo@u92.eu","sentAt":"2022-03-24T12:21:05Z","receivedAt":"2022-03-24T12:21:39Z","isPatch":true,"sender":{"key":"greenfoo@u92.eu","avatar":"https://avatars.githubusercontent.com/u/42691086?v=4"},"body":"> > +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> Do we want to keep this helper, and many calls to it sprinkled in\n> this file, or are they leftover cruft?\n\nI left it in case we ever need to debug this script in the future. But if you\nthink it's not worth it, I can delete it. We have three options:\n\n  A) Leave it\n  B) Completely remove it\n  C) Remove the function and replace the places where it is being called by\n     a commented out \"echo\" to stderr\n\nLet me know what you prefer.\n\n\n> Style.  \"debug_print () {\", i.e. SPACE on both sides of \"()\".\n\nSure. No problem. I'll fix all function declarations (they are all missing the\nextra space before the opening parenthesis)\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> The lack of space before the second closing \"))\" makes it look\n> inconsistent. \n\nNo problem. I'll fix it.\n\n\n> We should be able to do this no external commands\n> and just two variable substitutions and without relying on\n> bash-isms, but the above should do.\n> \n\nIn v1 of this patch series, instead of this function, I was doing this other\nthing:\n\n    X=${Y:a:b}\n\n...but that is a bash-ism, so I replaced it with what you see above (\"echo\" +\n\"cut\")\n\nI was not able to find another way of doing it using just standard POSIX shell\nsyntax [1] (notice that \"cut\" is included in IEEE Std 1003.1 [2] so it shouldn't\nbe an issue to rely on it)\n\nIn any case, if anyone knows how we could achieve the same without using\nexternal commands, please let me know and I'll change it (in the meantime I'll\nkeep searching for alternatives too). If after a reasonable amount of time none\nof us manages to find a solution I suggest to leave it as it is now.\n\n[1] https://pubs.opengroup.org/onlinepubs/009604499/utilities/xcu_chap02.html\n[2] https://pubs.opengroup.org/onlinepubs/009696699/utilities/cut.html\n\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> \n> I wonder if there were an easy way to \"compare\" the $FINAL_CMD this\n> new code feeds to $merge_tool_path and what was fed to it by the\n> original code to show that we are not regressing what the end user\n> sees.\n> \n> The \"run_unit_tests()\" only compares the cmd generated for each\n> given layout, but the original vimdiff$N didn't express them in\n> terms of $layout this patch introduces, so unfortunately that is not\n> it.\n> \n> Ideas?\n\nBefore this patch series, this is what each variant fed into \"$merge_tool_path\":\n\n  - vimdiff:\n      -f -d -c '4wincmd w | wincmd J' $LOCAL $BASE $REMOTE $MERGED\n\n  - vimdiff1:\n      -f -d -c 'echon \"...\"' $LOCAL $REMOTE\n\n  - vimdiff2:\n      -f -d -c 'wincmd l' $LOCAL $MERGED $REMOTE\n\n  - vimdiff3:\n      -f -d -c 'hid | hid | hid' $LOCAL $REMOTE $BASE $MERGED\n\nAfter this patch series, when one of these predefined variants is selected, a\nfixed layout is chosen and translated into the final string fed into\n\"$merge_tool_path\":\n\n  - vimdiff --> (LOCAL,BASE,REMOTE)/MERGED\n      -f -c \"echo | split | vertical split | 1b | wincmd l | vertical split | 2b | wincmd l | 3b | wincmd j | 4b | tabdo windo diffthis\" -c \"tabfirst\" $LOCAL $BASE $REMOTE $MERGED\n\n  - vimdiff1 --> @LOCAL,REMOTE\n      -f -c \"echo | vertical split | 1b | wincmd l | 3b | tabdo windo diffthis\" -c \"tabfirst\" $LOCAL $BASE $REMOTE $MERGED\n \n  - vimdiff2 --> LOCAL,MERGED,REMOTE\n      -f -c \"echo | vertical split | 1b | wincmd l | vertical split | 4b | wincmd l | 3b | tabdo windo diffthis\" -c \"tabfirst\" $LOCAL $BASE $REMOTE $MERGED\n\n  - vimdiff3 --> MERGED\n      -f -c \"echo | 4b | bufdo diffthis\" -c \"tabfirst\" $LOCAL $BASE $REMOTE $MERGED\n\nThus, we need to do two things:\n\n  1. Manually check (one time operation) that the *before* and *after* strings\n     are equivalent (from the point of view of vim).\n\n  2. Add a unit test that verifies that the layout associated to each variant\n     actually produces the string listed above. That way we make sure the\n     functionality does not break in the future.\n\nStep (1) is what I have already done (but I encourage others to do the same...\nthe more eyes the better).\n\nStep (2)... is already done! Notice how, on the \"run_unit_tests()\" function,\nthese layouts correspond to test cases numbers #07, #09, #01 and #10 :)\n\n  NOTE: While re-reviewing this, I noticed the layout definition for \"vimdiff1\"\n        was set to \"@LOCAL,MERGED\" instead of \"@LOCAL,REMOTE\", which is the\n        correct value. I'll fix this in v6.\n\nShould this be considered enough test for backwards compatibility?\n\n  NOTE: Another option would be to *completely bypass* the layout mechanism when\n        using one of the old variants and, in that case, feed exactly the same\n        string we were feeding before... but I just remembered we decided\n        against that in v2... so back to the question above :)\n\n\n\n\n"},{"id":"452189","messageId":"xmqqee2rl2cg.fsf@gitster.g","threadId":"57585","inReplyTo":"29c787c2-8916-4d04-85c1-a4c0597b9848@www.fastmail.com","subject":"Re: [PATCH v5 1/3] vimdiff: new implementation with layout support","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-03-24T16:52:47Z","receivedAt":"2022-03-24T16:54:34Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Fernando <greenfoo@u92.eu> writes:\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>> Do we want to keep this helper, and many calls to it sprinkled in\n>> this file, or are they leftover cruft?\n>\n> I left it in case we ever need to debug this script in the future. But if you\n> think it's not worth it, I can delete it. We have three options:\n>\n>   A) Leave it\n>   B) Completely remove it\n>   C) Remove the function and replace the places where it is being called by\n>      a commented out \"echo\" to stderr\n>\n> Let me know what you prefer.\n\nIf you anticipate that you'd need more debugging sessions as time\ngoes and people want to try to come up with different layouts, (A)\nis preferrable, I would think.\n\n>> We should be able to do this no external commands\n>> and just two variable substitutions and without relying on\n>> bash-isms, but the above should do.\n>\n> In v1 of this patch series, instead of this function, I was doing this other\n> thing:\n>\n>     X=${Y:a:b}\n>\n> ...but that is a bash-ism, so I replaced it with what you see above (\"echo\" +\n> \"cut\")\n\nYes, I think I was the one who spotted the bash-ism in an earlier\nround, if I am not mistaken.\n\n> I was not able to find another way of doing it using just standard POSIX shell\n> syntax [1] (notice that \"cut\" is included in IEEE Std 1003.1 [2] so it shouldn't\n> be an issue to rely on it)\n\nAlthough \"echo\" is often built-in, \"cut\" and pipe mean a fork to\ncreate an extra process, which some folks may want less of.\n\n> In any case, if anyone knows how we could achieve the same without using\n> external commands, please let me know and I'll change it (in the meantime I'll\n> keep searching for alternatives too). If after a reasonable amount of time none\n> of us manages to find a solution I suggest to leave it as it is now.\n\nI already said that this is good enough for now, didn't I?  But see\nbelow, and do not use it, it should work but it is uglier than a\nsimple single liner pipe.\n\n>> I wonder if there were an easy way to \"compare\" the $FINAL_CMD this\n>> new code feeds to $merge_tool_path and what was fed to it by the\n>> original code to show that we are not regressing what the end user\n>> sees.\n>> \n>> The \"run_unit_tests()\" only compares the cmd generated for each\n>> given layout, but the original vimdiff$N didn't express them in\n>> terms of $layout this patch introduces, so unfortunately that is not\n>> it.\n>> \n>> Ideas?\n>\n> Before this patch series, this is what each variant fed into \"$merge_tool_path\":\n>\n>   - vimdiff:\n>       -f -d -c '4wincmd w | wincmd J' $LOCAL $BASE $REMOTE $MERGED\n>\n>   - vimdiff1:\n>       -f -d -c 'echon \"...\"' $LOCAL $REMOTE\n>\n>   - vimdiff2:\n>       -f -d -c 'wincmd l' $LOCAL $MERGED $REMOTE\n>\n>   - vimdiff3:\n>       -f -d -c 'hid | hid | hid' $LOCAL $REMOTE $BASE $MERGED\n>\n> After this patch series, when one of these predefined variants is selected, a\n> fixed layout is chosen and translated into the final string fed into\n> \"$merge_tool_path\":\n>\n>   - vimdiff --> (LOCAL,BASE,REMOTE)/MERGED\n>       -f -c \"echo | split | vertical split | 1b | wincmd l | vertical split | 2b | wincmd l | 3b | wincmd j | 4b | tabdo windo diffthis\" -c \"tabfirst\" $LOCAL $BASE $REMOTE $MERGED\n>\n>   - vimdiff1 --> @LOCAL,REMOTE\n>       -f -c \"echo | vertical split | 1b | wincmd l | 3b | tabdo windo diffthis\" -c \"tabfirst\" $LOCAL $BASE $REMOTE $MERGED\n>  \n>   - vimdiff2 --> LOCAL,MERGED,REMOTE\n>       -f -c \"echo | vertical split | 1b | wincmd l | vertical split | 4b | wincmd l | 3b | tabdo windo diffthis\" -c \"tabfirst\" $LOCAL $BASE $REMOTE $MERGED\n>\n>   - vimdiff3 --> MERGED\n>       -f -c \"echo | 4b | bufdo diffthis\" -c \"tabfirst\" $LOCAL $BASE $REMOTE $MERGED\n>\n> Thus, we need to do two things:\n>\n>   1. Manually check (one time operation) that the *before* and *after* strings\n>      are equivalent (from the point of view of vim).\n\nYes, and it is a good way to convey that the author did sufficient\ndue diligence to note in the proposed commit log message (1) these\ntwo sets of actual vim commands, and (2) the fact that the author\nmade sure these \"not literally identical\" command line options\nproduce identical results for these existing modes.\n\n>   2. Add a unit test that verifies that the layout associated to each variant\n>      actually produces the string listed above. That way we make sure the\n>      functionality does not break in the future.\n\nHave a comment next to each of these four layout tests what variant\nfrom pre-layout era it correspnds to, if you haven't done so.\n\n> Step (1) is what I have already done (but I encourage others to do the same...\n> the more eyes the better).\n>\n> Step (2)... is already done! Notice how, on the \"run_unit_tests()\" function,\n> these layouts correspond to test cases numbers #07, #09, #01 and #10 :)\n>\n>   NOTE: While re-reviewing this, I noticed the layout definition for \"vimdiff1\"\n>         was set to \"@LOCAL,MERGED\" instead of \"@LOCAL,REMOTE\", which is the\n>         correct value. I'll fix this in v6.\n>\n> Should this be considered enough test for backwards compatibility?\n\nYes, with a bit to extend the proposed log message to help #1, and a\nbit of comments next to the test cases to help #2.\n\nThanks.\n\n\n\n\n#!/bin/sh\nsubstring () {\n\tSTRING=$1 START=$2 LEN=$3\n\n\tlocal strip= \n\twhile test 0 -lt \"$START\"\n\tdo\n\t\tstrip=\"?$strip\"\n\t\tSTART=$(($START - 1))\n\tdone\n\tif test -n \"$strip\"\n\tthen\n\t\tSTRING=${STRING#$strip}\n\tfi\n\tstrip=\n\tCOUNT=$(( ${#STRING} - $LEN ))\n\twhile test 0 -lt \"$COUNT\"\n\tdo\n\t\tstrip=\"?$strip\"\n\t\tCOUNT=$(($COUNT - 1))\n\tdone\n\tif test -n \"$strip\"\n\tthen\n\t\tSTRING=${STRING%$strip}\n\tfi\n\techo \"$STRING\"\n}\n\nwhile read string start len\ndo\n\tsubstring $string $start $len\ndone\n\n"},{"id":"452229","messageId":"YjzZlAqlH6bXh8+4@zacax395.localdomain","threadId":"57585","inReplyTo":"xmqqee2rl2cg.fsf@gitster.g","subject":"Re: [PATCH v5 1/3] vimdiff: new implementation with layout support","fromName":"Fernando","fromEmail":"greenfoo@u92.eu","sentAt":"2022-03-24T20:50:28Z","receivedAt":"2022-03-24T20:50:47Z","isPatch":true,"sender":{"key":"greenfoo@u92.eu","avatar":"https://avatars.githubusercontent.com/u/42691086?v=4"},"body":"> > In any case, if anyone knows how we could achieve the same without using\n> > external commands, please let me know and I'll change it (in the meantime I'll\n> > keep searching for alternatives too). If after a reasonable amount of time none\n> > of us manages to find a solution I suggest to leave it as it is now.\n> \n> I already said that this is good enough for now, didn't I?  But see\n> below, and do not use it, it should work but it is uglier than a\n> simple single liner pipe.\n\nHats off to the clever usage of \"???...\" as a regex to remove the characters\nthat are not part of the desired substring.\n\n\n> > Should this be considered enough test for backwards compatibility?\n> \n> Yes, with a bit to extend the proposed log message to help #1, and a\n> bit of comments next to the test cases to help #2.\n\nGood idea. I'll add both to v6.\n\nThanks for your feedback!\n\n"}]}