git: Remove handling for GIT_PREFIX
[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 () {
47d65924 89 merge_tool_path="$(get_merge_tool_path "$1")" || exit
21d0ba7e
DA
90 base_present="$2"
91 status=0
92
93 case "$1" in
aa03f604 94 araxis)
21d0ba7e 95 if merge_mode; then
aa03f604 96 touch "$BACKUP"
21d0ba7e 97 if $base_present; then
aa03f604
SS
98 "$merge_tool_path" -wait -merge -3 -a1 \
99 "$BASE" "$LOCAL" "$REMOTE" "$MERGED" \
100 >/dev/null 2>&1
21d0ba7e 101 else
aa03f604
SS
102 "$merge_tool_path" -wait -2 \
103 "$LOCAL" "$REMOTE" "$MERGED" \
104 >/dev/null 2>&1
21d0ba7e 105 fi
aa03f604 106 check_unchanged
21d0ba7e 107 else
aa03f604
SS
108 "$merge_tool_path" -wait -2 "$LOCAL" "$REMOTE" \
109 >/dev/null 2>&1
21d0ba7e
DA
110 fi
111 ;;
ffe6dc08 112 bc3)
21d0ba7e 113 if merge_mode; then
ffe6dc08 114 touch "$BACKUP"
21d0ba7e 115 if $base_present; then
ffe6dc08
SS
116 "$merge_tool_path" "$LOCAL" "$REMOTE" "$BASE" \
117 -mergeoutput="$MERGED"
21d0ba7e 118 else
ffe6dc08
SS
119 "$merge_tool_path" "$LOCAL" "$REMOTE" \
120 -mergeoutput="$MERGED"
21d0ba7e 121 fi
ffe6dc08 122 check_unchanged
21d0ba7e
DA
123 else
124 "$merge_tool_path" "$LOCAL" "$REMOTE"
125 fi
126 ;;
aa03f604 127 diffuse)
c8998b48 128 if merge_mode; then
aa03f604 129 touch "$BACKUP"
c8998b48 130 if $base_present; then
aa03f604
SS
131 "$merge_tool_path" \
132 "$LOCAL" "$MERGED" "$REMOTE" \
133 "$BASE" | cat
c8998b48 134 else
21d0ba7e 135 "$merge_tool_path" \
aa03f604 136 "$LOCAL" "$MERGED" "$REMOTE" | cat
c8998b48
SC
137 fi
138 check_unchanged
139 else
aa03f604 140 "$merge_tool_path" "$LOCAL" "$REMOTE" | cat
c8998b48
SC
141 fi
142 ;;
aa03f604 143 ecmerge)
21d0ba7e
DA
144 if merge_mode; then
145 touch "$BACKUP"
c8998b48 146 if $base_present; then
aa03f604
SS
147 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" \
148 --default --mode=merge3 --to="$MERGED"
c8998b48 149 else
aa03f604
SS
150 "$merge_tool_path" "$LOCAL" "$REMOTE" \
151 --default --mode=merge2 --to="$MERGED"
c8998b48 152 fi
21d0ba7e
DA
153 check_unchanged
154 else
aa03f604
SS
155 "$merge_tool_path" --default --mode=diff2 \
156 "$LOCAL" "$REMOTE"
21d0ba7e
DA
157 fi
158 ;;
aa03f604 159 emerge)
21d0ba7e 160 if merge_mode; then
21d0ba7e
DA
161 if $base_present; then
162 "$merge_tool_path" \
aa03f604
SS
163 -f emerge-files-with-ancestor-command \
164 "$LOCAL" "$REMOTE" "$BASE" \
165 "$(basename "$MERGED")"
21d0ba7e
DA
166 else
167 "$merge_tool_path" \
aa03f604
SS
168 -f emerge-files-command \
169 "$LOCAL" "$REMOTE" \
170 "$(basename "$MERGED")"
21d0ba7e 171 fi
aa03f604 172 status=$?
21d0ba7e 173 else
aa03f604
SS
174 "$merge_tool_path" -f emerge-files-command \
175 "$LOCAL" "$REMOTE"
21d0ba7e
DA
176 fi
177 ;;
aa03f604 178 gvimdiff|vimdiff)
21d0ba7e
DA
179 if merge_mode; then
180 touch "$BACKUP"
829ef383
DM
181 if $base_present; then
182 "$merge_tool_path" -f -d -c "wincmd J" \
183 "$MERGED" "$LOCAL" "$BASE" "$REMOTE"
184 else
185 "$merge_tool_path" -f -d -c "wincmd l" \
186 "$LOCAL" "$MERGED" "$REMOTE"
187 fi
21d0ba7e
DA
188 check_unchanged
189 else
853c0ffe 190 "$merge_tool_path" -R -f -d -c "wincmd l" \
21d0ba7e
DA
191 "$LOCAL" "$REMOTE"
192 fi
193 ;;
aa03f604 194 gvimdiff2|vimdiff2)
00086690
DM
195 if merge_mode; then
196 touch "$BACKUP"
197 "$merge_tool_path" -f -d -c "wincmd l" \
198 "$LOCAL" "$MERGED" "$REMOTE"
199 check_unchanged
200 else
853c0ffe 201 "$merge_tool_path" -R -f -d -c "wincmd l" \
00086690
DM
202 "$LOCAL" "$REMOTE"
203 fi
204 ;;
aa03f604 205 kdiff3)
21d0ba7e 206 if merge_mode; then
21d0ba7e 207 if $base_present; then
aa03f604
SS
208 ("$merge_tool_path" --auto \
209 --L1 "$MERGED (Base)" \
210 --L2 "$MERGED (Local)" \
211 --L3 "$MERGED (Remote)" \
212 -o "$MERGED" \
213 "$BASE" "$LOCAL" "$REMOTE" \
214 > /dev/null 2>&1)
21d0ba7e 215 else
aa03f604
SS
216 ("$merge_tool_path" --auto \
217 --L1 "$MERGED (Local)" \
218 --L2 "$MERGED (Remote)" \
219 -o "$MERGED" \
220 "$LOCAL" "$REMOTE" \
221 > /dev/null 2>&1)
21d0ba7e 222 fi
aa03f604
SS
223 status=$?
224 else
225 ("$merge_tool_path" --auto \
226 --L1 "$MERGED (A)" \
227 --L2 "$MERGED (B)" "$LOCAL" "$REMOTE" \
228 > /dev/null 2>&1)
229 fi
230 ;;
231 kompare)
232 "$merge_tool_path" "$LOCAL" "$REMOTE"
233 ;;
234 meld)
235 if merge_mode; then
236 touch "$BACKUP"
237 "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
21d0ba7e
DA
238 check_unchanged
239 else
aa03f604 240 "$merge_tool_path" "$LOCAL" "$REMOTE"
21d0ba7e
DA
241 fi
242 ;;
243 opendiff)
244 if merge_mode; then
245 touch "$BACKUP"
246 if $base_present; then
247 "$merge_tool_path" "$LOCAL" "$REMOTE" \
248 -ancestor "$BASE" \
249 -merge "$MERGED" | cat
250 else
251 "$merge_tool_path" "$LOCAL" "$REMOTE" \
252 -merge "$MERGED" | cat
253 fi
254 check_unchanged
255 else
256 "$merge_tool_path" "$LOCAL" "$REMOTE" | cat
257 fi
258 ;;
aa03f604 259 p4merge)
21d0ba7e 260 if merge_mode; then
aa03f604 261 touch "$BACKUP"
21d0ba7e 262 if $base_present; then
aa03f604 263 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
21d0ba7e 264 else
aa03f604 265 "$merge_tool_path" "$LOCAL" "$LOCAL" "$REMOTE" "$MERGED"
21d0ba7e
DA
266 fi
267 check_unchanged
268 else
aa03f604 269 "$merge_tool_path" "$LOCAL" "$REMOTE"
21d0ba7e
DA
270 fi
271 ;;
aa03f604 272 tkdiff)
21d0ba7e
DA
273 if merge_mode; then
274 if $base_present; then
aa03f604
SS
275 "$merge_tool_path" -a "$BASE" \
276 -o "$MERGED" "$LOCAL" "$REMOTE"
21d0ba7e
DA
277 else
278 "$merge_tool_path" \
aa03f604 279 -o "$MERGED" "$LOCAL" "$REMOTE"
21d0ba7e
DA
280 fi
281 status=$?
282 else
aa03f604 283 "$merge_tool_path" "$LOCAL" "$REMOTE"
21d0ba7e
DA
284 fi
285 ;;
286 tortoisemerge)
287 if $base_present; then
288 touch "$BACKUP"
289 "$merge_tool_path" \
290 -base:"$BASE" -mine:"$LOCAL" \
291 -theirs:"$REMOTE" -merged:"$MERGED"
292 check_unchanged
293 else
294 echo "TortoiseMerge cannot be used without a base" 1>&2
295 status=1
296 fi
297 ;;
aa03f604 298 xxdiff)
b6f0621a
DA
299 if merge_mode; then
300 touch "$BACKUP"
301 if $base_present; then
aa03f604
SS
302 "$merge_tool_path" -X --show-merged-pane \
303 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
304 -R 'Accel.Search: "Ctrl+F"' \
305 -R 'Accel.SearchForward: "Ctrl-G"' \
306 --merged-file "$MERGED" \
307 "$LOCAL" "$BASE" "$REMOTE"
b6f0621a 308 else
aa03f604
SS
309 "$merge_tool_path" -X $extra \
310 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
311 -R 'Accel.Search: "Ctrl+F"' \
312 -R 'Accel.SearchForward: "Ctrl-G"' \
313 --merged-file "$MERGED" \
314 "$LOCAL" "$REMOTE"
b6f0621a
DA
315 fi
316 check_unchanged
317 else
aa03f604
SS
318 "$merge_tool_path" \
319 -R 'Accel.Search: "Ctrl+F"' \
320 -R 'Accel.SearchForward: "Ctrl-G"' \
321 "$LOCAL" "$REMOTE"
b6f0621a
DA
322 fi
323 ;;
21d0ba7e 324 *)
47d65924 325 merge_tool_cmd="$(get_merge_tool_cmd "$1")"
21d0ba7e
DA
326 if test -z "$merge_tool_cmd"; then
327 if merge_mode; then
328 status=1
329 fi
330 break
331 fi
332 if merge_mode; then
47d65924
DA
333 trust_exit_code="$(git config --bool \
334 mergetool."$1".trustExitCode || echo false)"
335 if test "$trust_exit_code" = "false"; then
21d0ba7e
DA
336 touch "$BACKUP"
337 ( eval $merge_tool_cmd )
338 check_unchanged
339 else
340 ( eval $merge_tool_cmd )
341 status=$?
342 fi
343 else
344 ( eval $merge_tool_cmd )
345 fi
346 ;;
347 esac
348 return $status
349}
350
351guess_merge_tool () {
352 if merge_mode; then
353 tools="tortoisemerge"
354 else
355 tools="kompare"
356 fi
357 if test -n "$DISPLAY"; then
358 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
359 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
360 else
361 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
362 fi
ffe6dc08 363 tools="$tools gvimdiff diffuse ecmerge p4merge araxis bc3"
21d0ba7e 364 fi
7b104229
RS
365 case "${VISUAL:-$EDITOR}" in
366 *vim*)
21d0ba7e 367 tools="$tools vimdiff emerge"
7b104229
RS
368 ;;
369 *)
21d0ba7e 370 tools="$tools emerge vimdiff"
7b104229
RS
371 ;;
372 esac
21d0ba7e
DA
373 echo >&2 "merge tool candidates: $tools"
374
375 # Loop over each candidate and stop when a valid merge tool is found.
376 for i in $tools
377 do
378 merge_tool_path="$(translate_merge_tool_path "$i")"
379 if type "$merge_tool_path" > /dev/null 2>&1; then
47d65924
DA
380 echo "$i"
381 return 0
21d0ba7e
DA
382 fi
383 done
384
47d65924
DA
385 echo >&2 "No known merge resolution program available."
386 return 1
21d0ba7e
DA
387}
388
389get_configured_merge_tool () {
390 # Diff mode first tries diff.tool and falls back to merge.tool.
391 # Merge mode only checks merge.tool
392 if diff_mode; then
47d65924
DA
393 merge_tool=$(git config diff.tool || git config merge.tool)
394 else
395 merge_tool=$(git config merge.tool)
21d0ba7e
DA
396 fi
397 if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then
398 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
399 echo >&2 "Resetting to default..."
400 return 1
401 fi
47d65924 402 echo "$merge_tool"
21d0ba7e
DA
403}
404
405get_merge_tool_path () {
406 # A merge tool has been set, so verify that it's valid.
47d65924
DA
407 if test -n "$1"; then
408 merge_tool="$1"
409 else
410 merge_tool="$(get_merge_tool)"
411 fi
21d0ba7e
DA
412 if ! valid_tool "$merge_tool"; then
413 echo >&2 "Unknown merge tool $merge_tool"
414 exit 1
415 fi
416 if diff_mode; then
47d65924
DA
417 merge_tool_path=$(git config difftool."$merge_tool".path ||
418 git config mergetool."$merge_tool".path)
419 else
420 merge_tool_path=$(git config mergetool."$merge_tool".path)
21d0ba7e
DA
421 fi
422 if test -z "$merge_tool_path"; then
47d65924 423 merge_tool_path="$(translate_merge_tool_path "$merge_tool")"
21d0ba7e 424 fi
47d65924
DA
425 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
426 ! type "$merge_tool_path" > /dev/null 2>&1; then
427 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
428 "'$merge_tool_path'"
21d0ba7e
DA
429 exit 1
430 fi
431 echo "$merge_tool_path"
432}
433
434get_merge_tool () {
21d0ba7e 435 # Check if a merge tool has been configured
47d65924 436 merge_tool=$(get_configured_merge_tool)
21d0ba7e
DA
437 # Try to guess an appropriate merge tool if no tool has been set.
438 if test -z "$merge_tool"; then
47d65924 439 merge_tool="$(guess_merge_tool)" || exit
21d0ba7e
DA
440 fi
441 echo "$merge_tool"
442}