1 # git-mergetool--lib is a library for common merge tool functions
3 test "$TOOL_MODE" = diff
7 test "$TOOL_MODE" = merge
10 translate_merge_tool_path () {
31 if test "$MERGED" -nt "$BACKUP"; then
35 echo "$MERGED seems unchanged."
36 printf "Was the merge successful? [y/n] "
37 read answer < /dev/tty
39 y*|Y*) status=0; break ;;
40 n*|N*) status=1; break ;;
48 kdiff3 | tkdiff | xxdiff | meld | opendiff | \
49 emerge | vimdiff | gvimdiff | ecmerge | diffuse | araxis | p4merge)
62 if test -z "$(get_merge_tool_cmd "$1")"; then
69 get_merge_tool_cmd () {
70 # Prints the custom command for a merge tool
74 merge_tool="$(get_merge_tool)"
77 echo "$(git config difftool.$merge_tool.cmd ||
78 git config mergetool.$merge_tool.cmd)"
80 echo "$(git config mergetool.$merge_tool.cmd)"
85 merge_tool_path="$(get_merge_tool_path "$1")" || exit
92 if $base_present; then
93 ("$merge_tool_path" --auto \
94 --L1 "$MERGED (Base)" \
95 --L2 "$MERGED (Local)" \
96 --L3 "$MERGED (Remote)" \
98 "$BASE" "$LOCAL" "$REMOTE" \
101 ("$merge_tool_path" --auto \
102 --L1 "$MERGED (Local)" \
103 --L2 "$MERGED (Remote)" \
110 ("$merge_tool_path" --auto \
112 --L2 "$MERGED (B)" "$LOCAL" "$REMOTE" \
117 "$merge_tool_path" "$LOCAL" "$REMOTE"
121 if $base_present; then
122 "$merge_tool_path" -a "$BASE" \
123 -o "$MERGED" "$LOCAL" "$REMOTE"
126 -o "$MERGED" "$LOCAL" "$REMOTE"
130 "$merge_tool_path" "$LOCAL" "$REMOTE"
136 if $base_present; then
137 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
139 "$merge_tool_path" "$LOCAL" "$LOCAL" "$REMOTE" "$MERGED"
143 "$merge_tool_path" "$LOCAL" "$REMOTE"
149 "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
152 "$merge_tool_path" "$LOCAL" "$REMOTE"
158 if $base_present; then
160 "$LOCAL" "$MERGED" "$REMOTE" \
164 "$LOCAL" "$MERGED" "$REMOTE" | cat
168 "$merge_tool_path" "$LOCAL" "$REMOTE" | cat
174 "$merge_tool_path" -d -c "wincmd l" \
175 "$LOCAL" "$MERGED" "$REMOTE"
178 "$merge_tool_path" -d -c "wincmd l" \
185 "$merge_tool_path" -d -c "wincmd l" -f \
186 "$LOCAL" "$MERGED" "$REMOTE"
189 "$merge_tool_path" -d -c "wincmd l" -f \
196 if $base_present; then
197 "$merge_tool_path" -X --show-merged-pane \
198 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
199 -R 'Accel.Search: "Ctrl+F"' \
200 -R 'Accel.SearchForward: "Ctrl-G"' \
201 --merged-file "$MERGED" \
202 "$LOCAL" "$BASE" "$REMOTE"
204 "$merge_tool_path" -X $extra \
205 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
206 -R 'Accel.Search: "Ctrl+F"' \
207 -R 'Accel.SearchForward: "Ctrl-G"' \
208 --merged-file "$MERGED" \
214 -R 'Accel.Search: "Ctrl+F"' \
215 -R 'Accel.SearchForward: "Ctrl-G"' \
222 if $base_present; then
223 "$merge_tool_path" "$LOCAL" "$REMOTE" \
225 -merge "$MERGED" | cat
227 "$merge_tool_path" "$LOCAL" "$REMOTE" \
228 -merge "$MERGED" | cat
232 "$merge_tool_path" "$LOCAL" "$REMOTE" | cat
238 if $base_present; then
239 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" \
240 --default --mode=merge3 --to="$MERGED"
242 "$merge_tool_path" "$LOCAL" "$REMOTE" \
243 --default --mode=merge2 --to="$MERGED"
247 "$merge_tool_path" --default --mode=diff2 \
253 if $base_present; then
255 -f emerge-files-with-ancestor-command \
256 "$LOCAL" "$REMOTE" "$BASE" \
257 "$(basename "$MERGED")"
260 -f emerge-files-command \
262 "$(basename "$MERGED")"
266 "$merge_tool_path" -f emerge-files-command \
271 if $base_present; then
274 -base:"$BASE" -mine:"$LOCAL" \
275 -theirs:"$REMOTE" -merged:"$MERGED"
278 echo "TortoiseMerge cannot be used without a base" 1>&2
285 if $base_present; then
286 "$merge_tool_path" -wait -merge -3 -a1 \
287 "$BASE" "$LOCAL" "$REMOTE" "$MERGED" \
290 "$merge_tool_path" -wait -2 \
291 "$LOCAL" "$REMOTE" "$MERGED" \
296 "$merge_tool_path" -wait -2 "$LOCAL" "$REMOTE" \
301 merge_tool_cmd="$(get_merge_tool_cmd "$1")"
302 if test -z "$merge_tool_cmd"; then
309 trust_exit_code="$(git config --bool \
310 mergetool."$1".trustExitCode || echo false)"
311 if test "$trust_exit_code" = "false"; then
313 ( eval $merge_tool_cmd )
316 ( eval $merge_tool_cmd )
320 ( eval $merge_tool_cmd )
327 guess_merge_tool () {
329 tools="tortoisemerge"
333 if test -n "$DISPLAY"; then
334 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
335 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
337 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
339 tools="$tools gvimdiff diffuse ecmerge p4merge araxis"
341 case "${VISUAL:-$EDITOR}" in
343 tools="$tools vimdiff emerge"
346 tools="$tools emerge vimdiff"
349 echo >&2 "merge tool candidates: $tools"
351 # Loop over each candidate and stop when a valid merge tool is found.
354 merge_tool_path="$(translate_merge_tool_path "$i")"
355 if type "$merge_tool_path" > /dev/null 2>&1; then
361 echo >&2 "No known merge resolution program available."
365 get_configured_merge_tool () {
366 # Diff mode first tries diff.tool and falls back to merge.tool.
367 # Merge mode only checks merge.tool
369 merge_tool=$(git config diff.tool || git config merge.tool)
371 merge_tool=$(git config merge.tool)
373 if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then
374 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
375 echo >&2 "Resetting to default..."
381 get_merge_tool_path () {
382 # A merge tool has been set, so verify that it's valid.
383 if test -n "$1"; then
386 merge_tool="$(get_merge_tool)"
388 if ! valid_tool "$merge_tool"; then
389 echo >&2 "Unknown merge tool $merge_tool"
393 merge_tool_path=$(git config difftool."$merge_tool".path ||
394 git config mergetool."$merge_tool".path)
396 merge_tool_path=$(git config mergetool."$merge_tool".path)
398 if test -z "$merge_tool_path"; then
399 merge_tool_path="$(translate_merge_tool_path "$merge_tool")"
401 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
402 ! type "$merge_tool_path" > /dev/null 2>&1; then
403 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
407 echo "$merge_tool_path"
411 # Check if a merge tool has been configured
412 merge_tool=$(get_configured_merge_tool)
413 # Try to guess an appropriate merge tool if no tool has been set.
414 if test -z "$merge_tool"; then
415 merge_tool="$(guess_merge_tool)" || exit