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 if echo "${VISUAL:-$EDITOR}" |
grep emacs
> /dev
/null
2>&1; then
342 # $EDITOR is emacs so add emerge as a candidate
343 tools
="$tools emerge vimdiff"
344 elif echo "${VISUAL:-$EDITOR}" |
grep vim
> /dev
/null
2>&1; then
345 # $EDITOR is vim so add vimdiff as a candidate
346 tools
="$tools vimdiff emerge"
348 tools
="$tools emerge vimdiff"
350 echo >&2 "merge tool candidates: $tools"
352 # Loop over each candidate and stop when a valid merge tool is found.
355 merge_tool_path
="$(translate_merge_tool_path "$i")"
356 if type "$merge_tool_path" > /dev
/null
2>&1; then
362 echo >&2 "No known merge resolution program available."
366 get_configured_merge_tool
() {
367 # Diff mode first tries diff.tool and falls back to merge.tool.
368 # Merge mode only checks merge.tool
370 merge_tool
=$
(git config
diff.tool || git config merge.tool
)
372 merge_tool
=$
(git config merge.tool
)
374 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"; then
375 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
376 echo >&2 "Resetting to default..."
382 get_merge_tool_path
() {
383 # A merge tool has been set, so verify that it's valid.
384 if test -n "$1"; then
387 merge_tool
="$(get_merge_tool)"
389 if ! valid_tool
"$merge_tool"; then
390 echo >&2 "Unknown merge tool $merge_tool"
394 merge_tool_path
=$
(git config difftool.
"$merge_tool".path ||
395 git config mergetool.
"$merge_tool".path
)
397 merge_tool_path
=$
(git config mergetool.
"$merge_tool".path
)
399 if test -z "$merge_tool_path"; then
400 merge_tool_path
="$(translate_merge_tool_path "$merge_tool")"
402 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
403 ! type "$merge_tool_path" > /dev
/null
2>&1; then
404 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
408 echo "$merge_tool_path"
412 # Check if a merge tool has been configured
413 merge_tool
=$
(get_configured_merge_tool
)
414 # Try to guess an appropriate merge tool if no tool has been set.
415 if test -z "$merge_tool"; then
416 merge_tool
="$(guess_merge_tool)" ||
exit