1 # This script can be run in two different contexts:
3 # - From git, when the user invokes the "vimdiff" merge tool. In this context
4 # this script expects the following environment variables (among others) to
5 # be defined (which is something "git" takes care of):
12 # In this mode, all this script does is to run the next command:
14 # vim -f -c ... $LOCAL $BASE $REMOTE $MERGED
16 # ...where the "..." string depends on the value of the
17 # "mergetool.vimdiff.layout" configuration variable and is used to open vim
18 # with a certain layout of buffers, windows and tabs.
20 # - From a script inside the unit tests framework folder ("t" folder) by
21 # sourcing this script and then manually calling "run_unit_tests", which
22 # will run a battery of unit tests to make sure nothing breaks.
23 # In this context this script does not expect any particular environment
27 ################################################################################
28 ## Internal functions (not meant to be used outside this script)
29 ################################################################################
32 # Send message to stderr if global variable GIT_MERGETOOL_VIMDIFF_DEBUG
35 if test -n "$GIT_MERGETOOL_VIMDIFF_DEBUG"
42 # Return a substring of $1 containing $3 characters starting at
43 # zero-based offset $2.
47 # substring "Hello world" 0 4 --> "Hell"
48 # substring "Hello world" 3 4 --> "lo w"
49 # substring "Hello world" 3 10 --> "lo world"
55 echo "$STRING" | cut -c$(( START + 1 ))-$(( START + $LEN ))
59 # Auxiliary function used from "gen_cmd()".
60 # Read that other function documentation for more details.
63 CMD=$2 # This is a second (hidden) argument used for recursion
66 debug_print "LAYOUT : $LAYOUT"
67 debug_print "CMD : $CMD"
78 # Increase/decrease "start"/"end" indices respectively to get rid of
83 # - BEFORE: (( LOCAL , BASE ) / MERGED )
84 # - AFTER : ( LOCAL , BASE ) / MERGED
88 for c in $(echo "$LAYOUT" | sed 's:.:&#:g')
97 nested=$(( nested + 1 ))
103 nested=$(( nested - 1 ))
107 if test "$nested" -lt "$nested_min"
114 debug_print "NESTED MIN: $nested_min"
116 while test "$nested_min" -gt "0"
118 start=$(( start + 1 ))
121 start_minus_one=$(( start - 1 ))
123 while ! test "$(substring "$LAYOUT" "$start_minus_one" 1)" = "("
125 start=$(( start + 1 ))
126 start_minus_one=$(( start_minus_one + 1 ))
129 while ! test "$(substring "$LAYOUT" "$end" 1)" = ")"
134 nested_min=$(( nested_min - 1 ))
137 debug_print "CLEAN : $(substring "$LAYOUT" "$start" "$(( end - start ))")"
142 # Search for all valid separators ("/" or ",") which are *not*
143 # inside parenthesis. Save the index at which each of them makes the
146 index_horizontal_split=""
147 index_vertical_split=""
154 for c in $(substring "$LAYOUT" "$start" "$(( end - start ))" | sed 's:.:&#:g');
165 nested=$(( nested + 1 ))
171 nested=$(( nested - 1 ))
175 if test "$nested" = 0
179 if test "$current" = "/"
181 if test -z "$index_horizontal_split"
183 index_horizontal_split=$i
186 elif test "$current" = ","
188 if test -z "$index_vertical_split"
190 index_vertical_split=$i
200 # Process the separator with the highest order of precedence
201 # (";" has the highest precedence and "|" the lowest one).
203 # By "process" I mean recursively call this function twice: the first
204 # one with the substring at the left of the separator and the second one
205 # with the one at its right.
209 if ! test -z "$index_horizontal_split"
211 before="leftabove split"
213 index=$index_horizontal_split
216 elif ! test -z "$index_vertical_split"
218 before="leftabove vertical split"
220 index=$index_vertical_split
224 if test "$terminate" = "true"
227 CMD=$(gen_cmd_aux "$(substring "$LAYOUT" "$start" "$(( index - start ))")" "$CMD")
229 CMD=$(gen_cmd_aux "$(substring "$LAYOUT" "$(( index + 1 ))" "$(( ${#LAYOUT} - index ))")" "$CMD")
237 # If we reach this point, it means there are no separators and we just
238 # need to print the command to display the specified buffer
240 target=$(substring "$LAYOUT" "$start" "$(( end - start ))" | sed 's:[ @();|-]::g')
242 if test "$target" = "LOCAL"
246 elif test "$target" = "BASE"
250 elif test "$target" = "REMOTE"
254 elif test "$target" = "MERGED"
259 CMD="$CMD | ERROR: >$target<"
268 # This function returns (in global variable FINAL_CMD) the string that
269 # you can use when invoking "vim" (as shown next) to obtain a given
272 # $ vim -f $FINAL_CMD "$LOCAL" "$BASE" "$REMOTE" "$MERGED"
274 # It takes one single argument: a string containing the desired layout
277 # The syntax of the "layout definitions" is explained in "Documentation/
278 # mergetools/vimdiff.txt" but you can already intuitively understand how
279 # it works by knowing that...
281 # * "+" means "a new vim tab"
282 # * "/" means "a new vim horizontal split"
283 # * "," means "a new vim vertical split"
285 # It also returns (in global variable FINAL_TARGET) the name ("LOCAL",
286 # "BASE", "REMOTE" or "MERGED") of the file that is marked with an "@",
287 # or "MERGED" if none of them is.
291 # gen_cmd "@LOCAL , REMOTE"
293 # `-> FINAL_CMD == "-c \"echo | leftabove vertical split | 1b | wincmd l | 3b | tabdo windo diffthis\" -c \"tabfirst\""
294 # FINAL_TARGET == "LOCAL"
299 # Search for a "@" in one of the files identifiers ("LOCAL", "BASE",
300 # "REMOTE", "MERGED"). If not found, use "MERGE" as the default file
301 # where changes will be saved.
303 if echo "$LAYOUT" | grep @LOCAL >/dev/null
306 elif echo "$LAYOUT" | grep @BASE >/dev/null
310 FINAL_TARGET="MERGED"
314 # Obtain the first part of vim "-c" option to obtain the desired layout
323 CMD="echo" # vim "nop" operator
328 # If this is a single window diff with all the buffers
329 if ! echo "$tab" | grep ",\|/" >/dev/null
331 CMD="$CMD | silent execute 'bufdo diffthis'"
334 CMD=$(gen_cmd_aux "$tab" "$CMD")
338 CMD="$CMD | execute 'tabdo windo diffthis'"
340 FINAL_CMD="-c \"set hidden diffopt-=hiddenoff | $CMD | tabfirst\""
344 ################################################################################
345 ## API functions (called from "git-mergetool--lib.sh")
346 ################################################################################
349 "$merge_tool_path" -R -f -d \
350 -c 'wincmd l' -c 'cd $GIT_PREFIX' "$LOCAL" "$REMOTE"
362 printf "Use gVim (requires a graphical session)"
374 layout=$(git config mergetool.vimdiff.layout)
380 # Default layout when none is specified
381 layout="(LOCAL,BASE,REMOTE)/MERGED"
385 layout="@LOCAL,REMOTE"
388 layout="LOCAL,MERGED,REMOTE"
398 debug_print "FINAL CMD : $FINAL_CMD"
399 debug_print "FINAL TAR : $FINAL_TARGET"
403 eval '"$merge_tool_path"' \
404 -f "$FINAL_CMD" '"$LOCAL"' '"$BASE"' '"$REMOTE"' '"$MERGED"'
406 # If there is no BASE (example: a merge conflict in a new file
407 # with the same name created in both braches which didn't exist
408 # before), close all BASE windows using vim's "quit" command
410 FINAL_CMD=$(echo "$FINAL_CMD" | \
411 sed -e 's:2b:quit:g' -e 's:3b:2b:g' -e 's:4b:3b:g')
413 eval '"$merge_tool_path"' \
414 -f "$FINAL_CMD" '"$LOCAL"' '"$REMOTE"' '"$MERGED"'
421 case "$FINAL_TARGET" in
426 source_path="$REMOTE"
434 if test -n "$source_path"
436 cp "$source_path" "$MERGED"
452 printf "Use gVim (requires a graphical session) "
461 echo "with a 2 panes layout (LOCAL and REMOTE)"
464 echo "with a 3 panes layout (LOCAL, MERGED and REMOTE)"
467 echo "where only the MERGED file is shown"
470 echo "with a custom layout (see \`git help mergetool\`'s \`BACKEND SPECIFIC HINTS\` section)"
478 translate_merge_tool_path () {
493 exit_code_trustable () {
498 list_tool_variants () {
499 if test "$TOOL_MODE" = "diff"
503 echo "${prefix}vimdiff"
508 for suffix in '' 1 2 3
510 echo "${prefix}vimdiff${suffix}"
517 ################################################################################
518 ## Unit tests (called from scripts inside the "t" folder)
519 ################################################################################
522 # Function to make sure that we don't break anything when modifying this
525 NUMBER_OF_TEST_CASES=16
527 TEST_CASE_01="(LOCAL,BASE,REMOTE)/MERGED" # default behaviour
528 TEST_CASE_02="@LOCAL,REMOTE" # when using vimdiff1
529 TEST_CASE_03="LOCAL,MERGED,REMOTE" # when using vimdiff2
530 TEST_CASE_04="MERGED" # when using vimdiff3
531 TEST_CASE_05="LOCAL/MERGED/REMOTE"
532 TEST_CASE_06="(LOCAL/REMOTE),MERGED"
533 TEST_CASE_07="MERGED,(LOCAL/REMOTE)"
534 TEST_CASE_08="(LOCAL,REMOTE)/MERGED"
535 TEST_CASE_09="MERGED/(LOCAL,REMOTE)"
536 TEST_CASE_10="(LOCAL/BASE/REMOTE),MERGED"
537 TEST_CASE_11="(LOCAL,BASE,REMOTE)/MERGED+BASE,LOCAL+BASE,REMOTE+(LOCAL/BASE/REMOTE),MERGED"
538 TEST_CASE_12="((LOCAL,REMOTE)/BASE),MERGED"
539 TEST_CASE_13="((LOCAL,REMOTE)/BASE),((LOCAL/REMOTE),MERGED)"
540 TEST_CASE_14="BASE,REMOTE+BASE,LOCAL"
541 TEST_CASE_15=" (( (LOCAL , BASE , REMOTE) / MERGED)) +(BASE) , LOCAL+ BASE , REMOTE+ (((LOCAL / BASE / REMOTE)) , MERGED ) "
542 TEST_CASE_16="LOCAL,BASE,REMOTE / MERGED + BASE,LOCAL + BASE,REMOTE + (LOCAL / BASE / REMOTE),MERGED"
544 EXPECTED_CMD_01="-c \"set hidden diffopt-=hiddenoff | echo | leftabove split | leftabove vertical split | 1b | wincmd l | leftabove vertical split | 2b | wincmd l | 3b | wincmd j | 4b | execute 'tabdo windo diffthis' | tabfirst\""
545 EXPECTED_CMD_02="-c \"set hidden diffopt-=hiddenoff | echo | leftabove vertical split | 1b | wincmd l | 3b | execute 'tabdo windo diffthis' | tabfirst\""
546 EXPECTED_CMD_03="-c \"set hidden diffopt-=hiddenoff | echo | leftabove vertical split | 1b | wincmd l | leftabove vertical split | 4b | wincmd l | 3b | execute 'tabdo windo diffthis' | tabfirst\""
547 EXPECTED_CMD_04="-c \"set hidden diffopt-=hiddenoff | echo | silent execute 'bufdo diffthis' | 4b | execute 'tabdo windo diffthis' | tabfirst\""
548 EXPECTED_CMD_05="-c \"set hidden diffopt-=hiddenoff | echo | leftabove split | 1b | wincmd j | leftabove split | 4b | wincmd j | 3b | execute 'tabdo windo diffthis' | tabfirst\""
549 EXPECTED_CMD_06="-c \"set hidden diffopt-=hiddenoff | echo | leftabove vertical split | leftabove split | 1b | wincmd j | 3b | wincmd l | 4b | execute 'tabdo windo diffthis' | tabfirst\""
550 EXPECTED_CMD_07="-c \"set hidden diffopt-=hiddenoff | echo | leftabove vertical split | 4b | wincmd l | leftabove split | 1b | wincmd j | 3b | execute 'tabdo windo diffthis' | tabfirst\""
551 EXPECTED_CMD_08="-c \"set hidden diffopt-=hiddenoff | echo | leftabove split | leftabove vertical split | 1b | wincmd l | 3b | wincmd j | 4b | execute 'tabdo windo diffthis' | tabfirst\""
552 EXPECTED_CMD_09="-c \"set hidden diffopt-=hiddenoff | echo | leftabove split | 4b | wincmd j | leftabove vertical split | 1b | wincmd l | 3b | execute 'tabdo windo diffthis' | tabfirst\""
553 EXPECTED_CMD_10="-c \"set hidden diffopt-=hiddenoff | echo | leftabove vertical split | leftabove split | 1b | wincmd j | leftabove split | 2b | wincmd j | 3b | wincmd l | 4b | execute 'tabdo windo diffthis' | tabfirst\""
554 EXPECTED_CMD_11="-c \"set hidden diffopt-=hiddenoff | echo | leftabove split | leftabove vertical split | 1b | wincmd l | leftabove vertical split | 2b | wincmd l | 3b | wincmd j | 4b | tabnew | leftabove vertical split | 2b | wincmd l | 1b | tabnew | leftabove vertical split | 2b | wincmd l | 3b | tabnew | leftabove vertical split | leftabove split | 1b | wincmd j | leftabove split | 2b | wincmd j | 3b | wincmd l | 4b | execute 'tabdo windo diffthis' | tabfirst\""
555 EXPECTED_CMD_12="-c \"set hidden diffopt-=hiddenoff | echo | leftabove vertical split | leftabove split | leftabove vertical split | 1b | wincmd l | 3b | wincmd j | 2b | wincmd l | 4b | execute 'tabdo windo diffthis' | tabfirst\""
556 EXPECTED_CMD_13="-c \"set hidden diffopt-=hiddenoff | echo | leftabove vertical split | leftabove split | leftabove vertical split | 1b | wincmd l | 3b | wincmd j | 2b | wincmd l | leftabove vertical split | leftabove split | 1b | wincmd j | 3b | wincmd l | 4b | execute 'tabdo windo diffthis' | tabfirst\""
557 EXPECTED_CMD_14="-c \"set hidden diffopt-=hiddenoff | echo | leftabove vertical split | 2b | wincmd l | 3b | tabnew | leftabove vertical split | 2b | wincmd l | 1b | execute 'tabdo windo diffthis' | tabfirst\""
558 EXPECTED_CMD_15="-c \"set hidden diffopt-=hiddenoff | echo | leftabove split | leftabove vertical split | 1b | wincmd l | leftabove vertical split | 2b | wincmd l | 3b | wincmd j | 4b | tabnew | leftabove vertical split | 2b | wincmd l | 1b | tabnew | leftabove vertical split | 2b | wincmd l | 3b | tabnew | leftabove vertical split | leftabove split | 1b | wincmd j | leftabove split | 2b | wincmd j | 3b | wincmd l | 4b | execute 'tabdo windo diffthis' | tabfirst\""
559 EXPECTED_CMD_16="-c \"set hidden diffopt-=hiddenoff | echo | leftabove split | leftabove vertical split | 1b | wincmd l | leftabove vertical split | 2b | wincmd l | 3b | wincmd j | 4b | tabnew | leftabove vertical split | 2b | wincmd l | 1b | tabnew | leftabove vertical split | 2b | wincmd l | 3b | tabnew | leftabove vertical split | leftabove split | 1b | wincmd j | leftabove split | 2b | wincmd j | 3b | wincmd l | 4b | execute 'tabdo windo diffthis' | tabfirst\""
561 EXPECTED_TARGET_01="MERGED"
562 EXPECTED_TARGET_02="LOCAL"
563 EXPECTED_TARGET_03="MERGED"
564 EXPECTED_TARGET_04="MERGED"
565 EXPECTED_TARGET_05="MERGED"
566 EXPECTED_TARGET_06="MERGED"
567 EXPECTED_TARGET_07="MERGED"
568 EXPECTED_TARGET_08="MERGED"
569 EXPECTED_TARGET_09="MERGED"
570 EXPECTED_TARGET_10="MERGED"
571 EXPECTED_TARGET_11="MERGED"
572 EXPECTED_TARGET_12="MERGED"
573 EXPECTED_TARGET_13="MERGED"
574 EXPECTED_TARGET_14="MERGED"
575 EXPECTED_TARGET_15="MERGED"
576 EXPECTED_TARGET_16="MERGED"
578 at_least_one_ko="false"
580 for i in $(seq -w 1 99)
582 if test "$i" -gt $NUMBER_OF_TEST_CASES
587 gen_cmd "$(eval echo \${TEST_CASE_"$i"})"
589 if test "$FINAL_CMD" = "$(eval echo \${EXPECTED_CMD_"$i"})" \
590 && test "$FINAL_TARGET" = "$(eval echo \${EXPECTED_TARGET_"$i"})"
592 printf "Test Case #%02d: OK\n" "$(echo "$i" | sed 's/^0*//')"
594 printf "Test Case #%02d: KO !!!!\n" "$(echo "$i" | sed 's/^0*//')"
595 echo " FINAL_CMD : $FINAL_CMD"
596 echo " FINAL_CMD (expected) : $(eval echo \${EXPECTED_CMD_"$i"})"
597 echo " FINAL_TARGET : $FINAL_TARGET"
598 echo " FINAL_TARGET (expected): $(eval echo \${EXPECTED_TARGET_"$i"})"
599 at_least_one_ko="true"
603 # verify that `merge_cmd` handles paths with spaces
604 record_parameters () {
617 merge_tool_path=record_parameters
619 merge_cmd vimdiff || at_least_one_ko=true
624 set hidden diffopt-=hiddenoff | echo | leftabove split | leftabove vertical split | 1b | wincmd l | leftabove vertical split | quit | wincmd l | 2b | wincmd j | 3b | execute 'tabdo windo diffthis' | tabfirst
630 diff -u expect actual || at_least_one_ko=true
632 if test "$at_least_one_ko" = "true"