vimdiff: add tool documentation

Running 'git {merge,diff}tool --tool-help' now also prints usage information about the vimdiff tool (and its variants) instead of just its name. Two new functions ('diff_cmd_help()' and 'merge_cmd_help()') have been added to the set of functions that each merge tool (ie. scripts found inside "mergetools/") can overwrite to provided tool specific information. Right now, only 'mergetools/vimdiff' implements these functions, but other tools are encouraged to do so in the future, specially if they take configuration options not explained anywhere else (as it is the case with the 'vimdiff' tool and the new 'layout' option) Note that the function 'show_tool_names', used in the implementation of 'git mergetool --tool-help', is also used in Documentation/Makefile to generate the list of allowed values for the configuration variables '{diff,merge}.{gui,}tool'. Adjust the rule so its output is an Asciidoc "description list" instead of a plain list, with the tool name as the item and the newly added tool description as the description. In addition, a section has been added to "Documentation/git-mergetool.txt" to explain the new "layout" configuration option with examples. Helped-by: Philippe Blain <levraiphilippeblain@gmail.com> Signed-off-by: Fernando Ramos <greenfoo@u92.eu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Fernando Ramos committed Mar 30, 2022 at 21:19 UTC 7b5cf8be180940ce099c8413d02fb5707d900708
6 files changed +277 -5
Documentation/Makefile
+4 -4
@@ -302,12 +302,12 @@ $(mergetools_txt): mergetools-list.made
302
303 mergetools-list.made: ../git-mergetool--lib.sh $(wildcard ../mergetools/*)
304 $(QUIET_GEN) \
305 - $(SHELL_PATH) -c 'MERGE_TOOLS_DIR=../mergetools && \
305 + $(SHELL_PATH) -c 'MERGE_TOOLS_DIR=../mergetools && TOOL_MODE=diff && \
306 . ../git-mergetool--lib.sh && \
307 - show_tool_names can_diff "* " || :' >mergetools-diff.txt && \
308 - $(SHELL_PATH) -c 'MERGE_TOOLS_DIR=../mergetools && \
307 + show_tool_names can_diff' | sed -e "s/\([a-z0-9]*\)/\`\1\`;;/" >mergetools-diff.txt && \
308 + $(SHELL_PATH) -c 'MERGE_TOOLS_DIR=../mergetools && TOOL_MODE=merge && \
309 . ../git-mergetool--lib.sh && \
310 - show_tool_names can_merge "* " || :' >mergetools-merge.txt && \
310 + show_tool_names can_merge' | sed -e "s/\([a-z0-9]*\)/\`\1\`;;/" >mergetools-merge.txt && \
311 date >$@
312
313 TRACK_ASCIIDOCFLAGS = $(subst ','\'',$(ASCIIDOC_COMMON):$(ASCIIDOC_HTML):$(ASCIIDOC_DOCBOOK))
Documentation/config/mergetool.txt
+9
@@ -45,6 +45,15 @@ mergetool.meld.useAutoMerge::
45 value of `false` avoids using `--auto-merge` altogether, and is the
46 default value.
47
48 +mergetool.vimdiff.layout::
49 + The vimdiff backend uses this variable to control how its split
50 + windows look like. Applies even if you are using Neovim (`nvim`) or
51 + gVim (`gvim`) as the merge tool. See BACKEND SPECIFIC HINTS section
52 +ifndef::git-mergetool[]
53 + in linkgit:git-mergetool[1].
54 +endif::[]
55 + for details.
56 +
57 mergetool.hideResolved::
58 During a merge Git will automatically resolve as many conflicts as
59 possible and write the 'MERGED' file containing conflict markers around
Documentation/git-mergetool.txt
+8
@@ -101,6 +101,7 @@ success of the resolution after the custom tool has exited.
101
102 CONFIGURATION
103 -------------
104 +:git-mergetool: 1
105 include::config/mergetool.txt[]
106
107 TEMPORARY FILES
@@ -113,6 +114,13 @@ Setting the `mergetool.keepBackup` configuration variable to `false`
114 causes `git mergetool` to automatically remove the backup as files
115 are successfully merged.
116
117 +BACKEND SPECIFIC HINTS
118 +----------------------
119 +
120 +vimdiff
121 +~~~~~~~
122 +include::mergetools/vimdiff.txt[]
123 +
124 GIT
125 ---
126 Part of the linkgit:git[1] suite
Documentation/mergetools/vimdiff.txt new
+194
@@ -0,0 +1,194 @@
1 +Description
2 +^^^^^^^^^^^
3 +
4 +When specifying `--tool=vimdiff` in `git mergetool` Git will open Vim with a 4
5 +windows layout distributed in the following way:
6 +....
7 +------------------------------------------
8 +| | | |
9 +| LOCAL | BASE | REMOTE |
10 +| | | |
11 +------------------------------------------
12 +| |
13 +| MERGED |
14 +| |
15 +------------------------------------------
16 +....
17 +`LOCAL`, `BASE` and `REMOTE` are read-only buffers showing the contents of the
18 +conflicting file in specific commits ("commit you are merging into", "common
19 +ancestor commit" and "commit you are merging from" respectively)
20 +
21 +`MERGED` is a writable buffer where you have to resolve the conflicts (using the
22 +other read-only buffers as a reference). Once you are done, save and exit Vim as
23 +usual (`:wq`) or, if you want to abort, exit using `:cq`.
24 +
25 +Layout configuration
26 +^^^^^^^^^^^^^^^^^^^^
27 +
28 +You can change the windows layout used by Vim by setting configuration variable
29 +`mergetool.vimdiff.layout` which accepts a string where the following separators
30 +have special meaning:
31 +
32 + - `+` is used to "open a new tab"
33 + - `,` is used to "open a new vertical split"
34 + - `/` is used to "open a new horizontal split"
35 + - `@` is used to indicate which is the file containing the final version after
36 + solving the conflicts. If not present, `MERGED` will be used by default.
37 +
38 +The precedence of the operators is this one (you can use parentheses to change
39 +it):
40 +
41 + `@` > `+` > `/` > `,`
42 +
43 +Let's see some examples to understand how it works:
44 +
45 +* `layout = "(LOCAL,BASE,REMOTE)/MERGED"`
46 ++
47 +--
48 +This is exactly the same as the default layout we have already seen.
49 +
50 +Note that `/` has precedence over `,` and thus the parenthesis are not
51 +needed in this case. The next layout definition is equivalent:
52 +
53 + layout = "LOCAL,BASE,REMOTE / MERGED"
54 +--
55 +* `layout = "LOCAL,MERGED,REMOTE"`
56 ++
57 +--
58 +If, for some reason, we are not interested in the `BASE` buffer.
59 +....
60 +------------------------------------------
61 +| | | |
62 +| | | |
63 +| LOCAL | MERGED | REMOTE |
64 +| | | |
65 +| | | |
66 +------------------------------------------
67 +....
68 +--
69 +* `layout = "MERGED"`
70 ++
71 +--
72 +Only the `MERGED` buffer will be shown. Note, however, that all the other
73 +ones are still loaded in vim, and you can access them with the "buffers"
74 +command.
75 +....
76 +------------------------------------------
77 +| |
78 +| |
79 +| MERGED |
80 +| |
81 +| |
82 +------------------------------------------
83 +....
84 +--
85 +* `layout = "@LOCAL,REMOTE"`
86 ++
87 +--
88 +When `MERGED` is not present in the layout, you must "mark" one of the
89 +buffers with an asterisk. That will become the buffer you need to edit and
90 +save after resolving the conflicts.
91 +....
92 +------------------------------------------
93 +| | |
94 +| | |
95 +| | |
96 +| LOCAL | REMOTE |
97 +| | |
98 +| | |
99 +| | |
100 +------------------------------------------
101 +....
102 +--
103 +* `layout = "LOCAL,BASE,REMOTE / MERGED + BASE,LOCAL + BASE,REMOTE"`
104 ++
105 +--
106 +Three tabs will open: the first one is a copy of the default layout, while
107 +the other two only show the differences between (`BASE` and `LOCAL`) and
108 +(`BASE` and `REMOTE`) respectively.
109 +....
110 +------------------------------------------
111 +| <TAB #1> | TAB #2 | TAB #3 | |
112 +------------------------------------------
113 +| | | |
114 +| LOCAL | BASE | REMOTE |
115 +| | | |
116 +------------------------------------------
117 +| |
118 +| MERGED |
119 +| |
120 +------------------------------------------
121 +....
122 +....
123 +------------------------------------------
124 +| TAB #1 | <TAB #2> | TAB #3 | |
125 +------------------------------------------
126 +| | |
127 +| | |
128 +| | |
129 +| BASE | LOCAL |
130 +| | |
131 +| | |
132 +| | |
133 +------------------------------------------
134 +....
135 +....
136 +------------------------------------------
137 +| TAB #1 | TAB #2 | <TAB #3> | |
138 +------------------------------------------
139 +| | |
140 +| | |
141 +| | |
142 +| BASE | REMOTE |
143 +| | |
144 +| | |
145 +| | |
146 +------------------------------------------
147 +....
148 +--
149 +* `layout = "LOCAL,BASE,REMOTE / MERGED + BASE,LOCAL + BASE,REMOTE + (LOCAL/BASE/REMOTE),MERGED"`
150 ++
151 +--
152 +Same as the previous example, but adds a fourth tab with the same
153 +information as the first tab, with a different layout.
154 +....
155 +---------------------------------------------
156 +| TAB #1 | TAB #2 | TAB #3 | <TAB #4> |
157 +---------------------------------------------
158 +| LOCAL | |
159 +|---------------------| |
160 +| BASE | MERGED |
161 +|---------------------| |
162 +| REMOTE | |
163 +---------------------------------------------
164 +....
165 +Note how in the third tab definition we need to use parenthesis to make `,`
166 +have precedence over `/`.
167 +--
168 +
169 +Variants
170 +^^^^^^^^
171 +
172 +Instead of `--tool=vimdiff`, you can also use one of these other variants:
173 +
174 + * `--tool=gvimdiff`, to open gVim instead of Vim.
175 +
176 + * `--tool=nvimdiff`, to open Neovim instead of Vim.
177 +
178 +When using these variants, in order to specify a custom layout you will have to
179 +set configuration variables `mergetool.gvimdiff.layout` and
180 +`mergetool.nvimdiff.layout` instead of `mergetool.vimdiff.layout`
181 +
182 +In addition, for backwards compatibility with previous Git versions, you can
183 +also append `1`, `2` or `3` to either `vimdiff` or any of the variants (ex:
184 +`vimdiff3`, `nvimdiff1`, etc...) to use a predefined layout.
185 +In other words, using `--tool=[g,n,]vimdiffx` is the same as using
186 +`--tool=[g,n,]vimdiff` and setting configuration variable
187 +`mergetool.[g,n,]vimdiff.layout` to...
188 +
189 + * `x=1`: `"@LOCAL, REMOTE"`
190 + * `x=2`: `"LOCAL, MERGED, REMOTE"`
191 + * `x=3`: `"MERGED"`
192 +
193 +Example: using `--tool=gvimdiff2` will open `gvim` with three columns (LOCAL,
194 +MERGED and REMOTE).
git-mergetool--lib.sh
+9 -1
@@ -63,7 +63,7 @@ $(list_tool_variants)"
63 preamble=
64 fi
65 shown_any=yes
66 - printf "%s%s\n" "$per_line_prefix" "$toolname"
66 + printf "%s%-15s %s\n" "$per_line_prefix" "$toolname" $(diff_mode && diff_cmd_help "$toolname" || merge_cmd_help "$toolname")
67 fi
68 done
69
@@ -162,10 +162,18 @@ setup_tool () {
162 return 1
163 }
164
165 + diff_cmd_help () {
166 + return 0
167 + }
168 +
169 merge_cmd () {
170 return 1
171 }
172
173 + merge_cmd_help () {
174 + return 0
175 + }
176 +
177 hide_resolved_enabled () {
178 return 0
179 }
mergetools/vimdiff
+53
@@ -365,6 +365,25 @@ diff_cmd () {
365 }
366
367
368 +diff_cmd_help () {
369 + TOOL=$1
370 +
371 + case "$TOOL" in
372 + nvimdiff*)
373 + printf "Use Neovim"
374 + ;;
375 + gvimdiff*)
376 + printf "Use gVim (requires a graphical session)"
377 + ;;
378 + vimdiff*)
379 + printf "Use Vim"
380 + ;;
381 + esac
382 +
383 + return 0
384 +}
385 +
386 +
387 merge_cmd () {
388 layout=$(git config mergetool.vimdiff.layout)
389
@@ -436,6 +455,40 @@ merge_cmd () {
455 }
456
457
458 +merge_cmd_help () {
459 + TOOL=$1
460 +
461 + case "$TOOL" in
462 + nvimdiff*)
463 + printf "Use Neovim "
464 + ;;
465 + gvimdiff*)
466 + printf "Use gVim (requires a graphical session) "
467 + ;;
468 + vimdiff*)
469 + printf "Use Vim "
470 + ;;
471 + esac
472 +
473 + case "$TOOL" in
474 + *1)
475 + echo "with a 2 panes layout (LOCAL and REMOTE)"
476 + ;;
477 + *2)
478 + echo "with a 3 panes layout (LOCAL, MERGED and REMOTE)"
479 + ;;
480 + *3)
481 + echo "where only the MERGED file is shown"
482 + ;;
483 + *)
484 + echo "with a custom layout (see \`git help mergetool\`'s \`BACKEND SPECIFIC HINTS\` section)"
485 + ;;
486 + esac
487 +
488 + return 0
489 +}
490 +
491 +
492 translate_merge_tool_path () {
493 case "$1" in
494 nvimdiff*)