docs: put listed example commands in backticks
[git/git.git] / git-mergetool--lib.sh
CommitLineData
02e51243 1#!/bin/sh
21d0ba7e
DA
2# git-mergetool--lib is a library for common merge tool functions
3diff_mode() {
4 test "$TOOL_MODE" = diff
5}
6
7merge_mode() {
8 test "$TOOL_MODE" = merge
9}
10
11translate_merge_tool_path () {
47d65924 12 case "$1" in
aa03f604
SS
13 araxis)
14 echo compare
47d65924 15 ;;
ffe6dc08
SS
16 bc3)
17 echo bcompare
47d65924
DA
18 ;;
19 emerge)
20 echo emacs
21 ;;
aa03f604
SS
22 gvimdiff|gvimdiff2)
23 echo gvim
24 ;;
25 vimdiff|vimdiff2)
26 echo vim
b6f0621a 27 ;;
47d65924
DA
28 *)
29 echo "$1"
30 ;;
31 esac
21d0ba7e
DA
32}
33
34check_unchanged () {
35 if test "$MERGED" -nt "$BACKUP"; then
36 status=0
37 else
38 while true; do
39 echo "$MERGED seems unchanged."
40 printf "Was the merge successful? [y/n] "
af314714 41 read answer
21d0ba7e
DA
42 case "$answer" in
43 y*|Y*) status=0; break ;;
44 n*|N*) status=1; break ;;
45 esac
46 done
47 fi
48}
49
50valid_tool () {
51 case "$1" in
ffe6dc08 52 araxis | bc3 | diffuse | ecmerge | emerge | gvimdiff | gvimdiff2 | \
aa03f604 53 kdiff3 | meld | opendiff | p4merge | tkdiff | vimdiff | vimdiff2 | xxdiff)
21d0ba7e 54 ;; # happy
aa03f604
SS
55 kompare)
56 if ! diff_mode; then
21d0ba7e
DA
57 return 1
58 fi
59 ;;
aa03f604
SS
60 tortoisemerge)
61 if ! merge_mode; then
21d0ba7e
DA
62 return 1
63 fi
64 ;;
65 *)
66 if test -z "$(get_merge_tool_cmd "$1")"; then
67 return 1
68 fi
69 ;;
70 esac
71}
72
73get_merge_tool_cmd () {
47d65924
DA
74 # Prints the custom command for a merge tool
75 if test -n "$1"; then
76 merge_tool="$1"
77 else
78 merge_tool="$(get_merge_tool)"
79 fi
80 if diff_mode; then
81 echo "$(git config difftool.$merge_tool.cmd ||
82 git config mergetool.$merge_tool.cmd)"
83 else
84 echo "$(git config mergetool.$merge_tool.cmd)"
85 fi
21d0ba7e
DA
86}
87
88run_merge_tool () {
f9ad901f
DA
89 # If GIT_PREFIX is empty then we cannot use it in tools
90 # that expect to be able to chdir() to its value.
91 GIT_PREFIX=${GIT_PREFIX:-.}
92 export GIT_PREFIX
93
47d65924 94 merge_tool_path="$(get_merge_tool_path "$1")" || exit
21d0ba7e
DA
95 base_present="$2"
96 status=0
97
98 case "$1" in
aa03f604 99 araxis)
21d0ba7e 100 if merge_mode; then
aa03f604 101 touch "$BACKUP"
21d0ba7e 102 if $base_present; then
aa03f604
SS
103 "$merge_tool_path" -wait -merge -3 -a1 \
104 "$BASE" "$LOCAL" "$REMOTE" "$MERGED" \
105 >/dev/null 2>&1
21d0ba7e 106 else
aa03f604
SS
107 "$merge_tool_path" -wait -2 \
108 "$LOCAL" "$REMOTE" "$MERGED" \
109 >/dev/null 2>&1
21d0ba7e 110 fi
aa03f604 111 check_unchanged
21d0ba7e 112 else
aa03f604
SS
113 "$merge_tool_path" -wait -2 "$LOCAL" "$REMOTE" \
114 >/dev/null 2>&1
21d0ba7e
DA
115 fi
116 ;;
ffe6dc08 117 bc3)
21d0ba7e 118 if merge_mode; then
ffe6dc08 119 touch "$BACKUP"
21d0ba7e 120 if $base_present; then
ffe6dc08
SS
121 "$merge_tool_path" "$LOCAL" "$REMOTE" "$BASE" \
122 -mergeoutput="$MERGED"
21d0ba7e 123 else
ffe6dc08
SS
124 "$merge_tool_path" "$LOCAL" "$REMOTE" \
125 -mergeoutput="$MERGED"
21d0ba7e 126 fi
ffe6dc08 127 check_unchanged
21d0ba7e
DA
128 else
129 "$merge_tool_path" "$LOCAL" "$REMOTE"
130 fi
131 ;;
aa03f604 132 diffuse)
c8998b48 133 if merge_mode; then
aa03f604 134 touch "$BACKUP"
c8998b48 135 if $base_present; then
aa03f604
SS
136 "$merge_tool_path" \
137 "$LOCAL" "$MERGED" "$REMOTE" \
138 "$BASE" | cat
c8998b48 139 else
21d0ba7e 140 "$merge_tool_path" \
aa03f604 141 "$LOCAL" "$MERGED" "$REMOTE" | cat
c8998b48
SC
142 fi
143 check_unchanged
144 else
aa03f604 145 "$merge_tool_path" "$LOCAL" "$REMOTE" | cat
c8998b48
SC
146 fi
147 ;;
aa03f604 148 ecmerge)
21d0ba7e
DA
149 if merge_mode; then
150 touch "$BACKUP"
c8998b48 151 if $base_present; then
aa03f604
SS
152 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" \
153 --default --mode=merge3 --to="$MERGED"
c8998b48 154 else
aa03f604
SS
155 "$merge_tool_path" "$LOCAL" "$REMOTE" \
156 --default --mode=merge2 --to="$MERGED"
c8998b48 157 fi
21d0ba7e
DA
158 check_unchanged
159 else
aa03f604
SS
160 "$merge_tool_path" --default --mode=diff2 \
161 "$LOCAL" "$REMOTE"
21d0ba7e
DA
162 fi
163 ;;
aa03f604 164 emerge)
21d0ba7e 165 if merge_mode; then
21d0ba7e
DA
166 if $base_present; then
167 "$merge_tool_path" \
aa03f604
SS
168 -f emerge-files-with-ancestor-command \
169 "$LOCAL" "$REMOTE" "$BASE" \
170 "$(basename "$MERGED")"
21d0ba7e
DA
171 else
172 "$merge_tool_path" \
aa03f604
SS
173 -f emerge-files-command \
174 "$LOCAL" "$REMOTE" \
175 "$(basename "$MERGED")"
21d0ba7e 176 fi
aa03f604 177 status=$?
21d0ba7e 178 else
aa03f604
SS
179 "$merge_tool_path" -f emerge-files-command \
180 "$LOCAL" "$REMOTE"
21d0ba7e
DA
181 fi
182 ;;
aa03f604 183 gvimdiff|vimdiff)
21d0ba7e
DA
184 if merge_mode; then
185 touch "$BACKUP"
829ef383
DM
186 if $base_present; then
187 "$merge_tool_path" -f -d -c "wincmd J" \
188 "$MERGED" "$LOCAL" "$BASE" "$REMOTE"
189 else
190 "$merge_tool_path" -f -d -c "wincmd l" \
191 "$LOCAL" "$MERGED" "$REMOTE"
192 fi
21d0ba7e
DA
193 check_unchanged
194 else
853c0ffe 195 "$merge_tool_path" -R -f -d -c "wincmd l" \
f9ad901f 196 -c 'cd $GIT_PREFIX' \
21d0ba7e
DA
197 "$LOCAL" "$REMOTE"
198 fi
199 ;;
aa03f604 200 gvimdiff2|vimdiff2)
00086690
DM
201 if merge_mode; then
202 touch "$BACKUP"
203 "$merge_tool_path" -f -d -c "wincmd l" \
204 "$LOCAL" "$MERGED" "$REMOTE"
205 check_unchanged
206 else
853c0ffe 207 "$merge_tool_path" -R -f -d -c "wincmd l" \
f9ad901f 208 -c 'cd $GIT_PREFIX' \
00086690
DM
209 "$LOCAL" "$REMOTE"
210 fi
211 ;;
aa03f604 212 kdiff3)
21d0ba7e 213 if merge_mode; then
21d0ba7e 214 if $base_present; then
aa03f604
SS
215 ("$merge_tool_path" --auto \
216 --L1 "$MERGED (Base)" \
217 --L2 "$MERGED (Local)" \
218 --L3 "$MERGED (Remote)" \
219 -o "$MERGED" \
220 "$BASE" "$LOCAL" "$REMOTE" \
221 > /dev/null 2>&1)
21d0ba7e 222 else
aa03f604
SS
223 ("$merge_tool_path" --auto \
224 --L1 "$MERGED (Local)" \
225 --L2 "$MERGED (Remote)" \
226 -o "$MERGED" \
227 "$LOCAL" "$REMOTE" \
228 > /dev/null 2>&1)
21d0ba7e 229 fi
aa03f604
SS
230 status=$?
231 else
232 ("$merge_tool_path" --auto \
233 --L1 "$MERGED (A)" \
234 --L2 "$MERGED (B)" "$LOCAL" "$REMOTE" \
235 > /dev/null 2>&1)
236 fi
237 ;;
238 kompare)
239 "$merge_tool_path" "$LOCAL" "$REMOTE"
240 ;;
241 meld)
242 if merge_mode; then
243 touch "$BACKUP"
244 "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
21d0ba7e
DA
245 check_unchanged
246 else
aa03f604 247 "$merge_tool_path" "$LOCAL" "$REMOTE"
21d0ba7e
DA
248 fi
249 ;;
250 opendiff)
251 if merge_mode; then
252 touch "$BACKUP"
253 if $base_present; then
254 "$merge_tool_path" "$LOCAL" "$REMOTE" \
255 -ancestor "$BASE" \
256 -merge "$MERGED" | cat
257 else
258 "$merge_tool_path" "$LOCAL" "$REMOTE" \
259 -merge "$MERGED" | cat
260 fi
261 check_unchanged
262 else
263 "$merge_tool_path" "$LOCAL" "$REMOTE" | cat
264 fi
265 ;;
aa03f604 266 p4merge)
21d0ba7e 267 if merge_mode; then
0a0ec7bd
CJ
268 touch "$BACKUP"
269 $base_present || >"$BASE"
270 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
21d0ba7e
DA
271 check_unchanged
272 else
aa03f604 273 "$merge_tool_path" "$LOCAL" "$REMOTE"
21d0ba7e
DA
274 fi
275 ;;
aa03f604 276 tkdiff)
21d0ba7e
DA
277 if merge_mode; then
278 if $base_present; then
aa03f604
SS
279 "$merge_tool_path" -a "$BASE" \
280 -o "$MERGED" "$LOCAL" "$REMOTE"
21d0ba7e
DA
281 else
282 "$merge_tool_path" \
aa03f604 283 -o "$MERGED" "$LOCAL" "$REMOTE"
21d0ba7e
DA
284 fi
285 status=$?
286 else
aa03f604 287 "$merge_tool_path" "$LOCAL" "$REMOTE"
21d0ba7e
DA
288 fi
289 ;;
290 tortoisemerge)
291 if $base_present; then
292 touch "$BACKUP"
293 "$merge_tool_path" \
294 -base:"$BASE" -mine:"$LOCAL" \
295 -theirs:"$REMOTE" -merged:"$MERGED"
296 check_unchanged
297 else
298 echo "TortoiseMerge cannot be used without a base" 1>&2
299 status=1
300 fi
301 ;;
aa03f604 302 xxdiff)
b6f0621a
DA
303 if merge_mode; then
304 touch "$BACKUP"
305 if $base_present; then
aa03f604
SS
306 "$merge_tool_path" -X --show-merged-pane \
307 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
308 -R 'Accel.Search: "Ctrl+F"' \
309 -R 'Accel.SearchForward: "Ctrl-G"' \
310 --merged-file "$MERGED" \
311 "$LOCAL" "$BASE" "$REMOTE"
b6f0621a 312 else
aa03f604
SS
313 "$merge_tool_path" -X $extra \
314 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
315 -R 'Accel.Search: "Ctrl+F"' \
316 -R 'Accel.SearchForward: "Ctrl-G"' \
317 --merged-file "$MERGED" \
318 "$LOCAL" "$REMOTE"
b6f0621a
DA
319 fi
320 check_unchanged
321 else
aa03f604
SS
322 "$merge_tool_path" \
323 -R 'Accel.Search: "Ctrl+F"' \
324 -R 'Accel.SearchForward: "Ctrl-G"' \
325 "$LOCAL" "$REMOTE"
b6f0621a
DA
326 fi
327 ;;
21d0ba7e 328 *)
47d65924 329 merge_tool_cmd="$(get_merge_tool_cmd "$1")"
21d0ba7e
DA
330 if test -z "$merge_tool_cmd"; then
331 if merge_mode; then
332 status=1
333 fi
334 break
335 fi
336 if merge_mode; then
47d65924
DA
337 trust_exit_code="$(git config --bool \
338 mergetool."$1".trustExitCode || echo false)"
339 if test "$trust_exit_code" = "false"; then
21d0ba7e
DA
340 touch "$BACKUP"
341 ( eval $merge_tool_cmd )
342 check_unchanged
343 else
344 ( eval $merge_tool_cmd )
345 status=$?
346 fi
347 else
348 ( eval $merge_tool_cmd )
349 fi
350 ;;
351 esac
352 return $status
353}
354
355guess_merge_tool () {
356 if merge_mode; then
357 tools="tortoisemerge"
358 else
359 tools="kompare"
360 fi
361 if test -n "$DISPLAY"; then
362 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
363 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
364 else
365 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
366 fi
ffe6dc08 367 tools="$tools gvimdiff diffuse ecmerge p4merge araxis bc3"
21d0ba7e 368 fi
7b104229
RS
369 case "${VISUAL:-$EDITOR}" in
370 *vim*)
21d0ba7e 371 tools="$tools vimdiff emerge"
7b104229
RS
372 ;;
373 *)
21d0ba7e 374 tools="$tools emerge vimdiff"
7b104229
RS
375 ;;
376 esac
21d0ba7e
DA
377 echo >&2 "merge tool candidates: $tools"
378
379 # Loop over each candidate and stop when a valid merge tool is found.
380 for i in $tools
381 do
382 merge_tool_path="$(translate_merge_tool_path "$i")"
383 if type "$merge_tool_path" > /dev/null 2>&1; then
47d65924
DA
384 echo "$i"
385 return 0
21d0ba7e
DA
386 fi
387 done
388
47d65924
DA
389 echo >&2 "No known merge resolution program available."
390 return 1
21d0ba7e
DA
391}
392
393get_configured_merge_tool () {
394 # Diff mode first tries diff.tool and falls back to merge.tool.
395 # Merge mode only checks merge.tool
396 if diff_mode; then
47d65924
DA
397 merge_tool=$(git config diff.tool || git config merge.tool)
398 else
399 merge_tool=$(git config merge.tool)
21d0ba7e
DA
400 fi
401 if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then
402 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
403 echo >&2 "Resetting to default..."
404 return 1
405 fi
47d65924 406 echo "$merge_tool"
21d0ba7e
DA
407}
408
409get_merge_tool_path () {
410 # A merge tool has been set, so verify that it's valid.
47d65924
DA
411 if test -n "$1"; then
412 merge_tool="$1"
413 else
414 merge_tool="$(get_merge_tool)"
415 fi
21d0ba7e
DA
416 if ! valid_tool "$merge_tool"; then
417 echo >&2 "Unknown merge tool $merge_tool"
418 exit 1
419 fi
420 if diff_mode; then
47d65924
DA
421 merge_tool_path=$(git config difftool."$merge_tool".path ||
422 git config mergetool."$merge_tool".path)
423 else
424 merge_tool_path=$(git config mergetool."$merge_tool".path)
21d0ba7e
DA
425 fi
426 if test -z "$merge_tool_path"; then
47d65924 427 merge_tool_path="$(translate_merge_tool_path "$merge_tool")"
21d0ba7e 428 fi
47d65924
DA
429 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
430 ! type "$merge_tool_path" > /dev/null 2>&1; then
431 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
432 "'$merge_tool_path'"
21d0ba7e
DA
433 exit 1
434 fi
435 echo "$merge_tool_path"
436}
437
438get_merge_tool () {
21d0ba7e 439 # Check if a merge tool has been configured
47d65924 440 merge_tool=$(get_configured_merge_tool)
21d0ba7e
DA
441 # Try to guess an appropriate merge tool if no tool has been set.
442 if test -z "$merge_tool"; then
47d65924 443 merge_tool="$(guess_merge_tool)" || exit
21d0ba7e
DA
444 fi
445 echo "$merge_tool"
446}