2 # git-mergetool--lib is a library for common merge tool functions
4 test "$TOOL_MODE" = diff
8 test "$TOOL_MODE" = merge
11 translate_merge_tool_path
() {
32 if test "$MERGED" -nt "$BACKUP"; then
36 echo "$MERGED seems unchanged."
37 printf "Was the merge successful? [y/n] "
40 y
*|Y
*) status
=0; break ;;
41 n
*|N
*) status
=1; break ;;
49 kdiff3 | tkdiff | xxdiff | meld | opendiff | \
50 emerge | vimdiff | gvimdiff | ecmerge | diffuse | araxis | p4merge
)
63 if test -z "$(get_merge_tool_cmd "$1")"; then
70 get_merge_tool_cmd
() {
71 # Prints the custom command for a merge tool
75 merge_tool
="$(get_merge_tool)"
78 echo "$(git config difftool.$merge_tool.cmd ||
79 git config mergetool.$merge_tool.cmd)"
81 echo "$(git config mergetool.$merge_tool.cmd)"
86 merge_tool_path
="$(get_merge_tool_path "$1")" ||
exit
93 if $base_present; then
94 ("$merge_tool_path" --auto \
95 --L1 "$MERGED (Base)" \
96 --L2 "$MERGED (Local)" \
97 --L3 "$MERGED (Remote)" \
99 "$BASE" "$LOCAL" "$REMOTE" \
102 ("$merge_tool_path" --auto \
103 --L1 "$MERGED (Local)" \
104 --L2 "$MERGED (Remote)" \
111 ("$merge_tool_path" --auto \
113 --L2 "$MERGED (B)" "$LOCAL" "$REMOTE" \
118 "$merge_tool_path" "$LOCAL" "$REMOTE"
122 if $base_present; then
123 "$merge_tool_path" -a "$BASE" \
124 -o "$MERGED" "$LOCAL" "$REMOTE"
127 -o "$MERGED" "$LOCAL" "$REMOTE"
131 "$merge_tool_path" "$LOCAL" "$REMOTE"
137 if $base_present; then
138 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
140 "$merge_tool_path" "$LOCAL" "$LOCAL" "$REMOTE" "$MERGED"
144 "$merge_tool_path" "$LOCAL" "$REMOTE"
150 "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
153 "$merge_tool_path" "$LOCAL" "$REMOTE"
159 if $base_present; then
161 "$LOCAL" "$MERGED" "$REMOTE" \
165 "$LOCAL" "$MERGED" "$REMOTE" |
cat
169 "$merge_tool_path" "$LOCAL" "$REMOTE" |
cat
175 if $base_present; then
176 "$merge_tool_path" -f -d -c "wincmd J" \
177 "$MERGED" "$LOCAL" "$BASE" "$REMOTE"
179 "$merge_tool_path" -f -d -c "wincmd l" \
180 "$LOCAL" "$MERGED" "$REMOTE"
184 "$merge_tool_path" -f -d -c "wincmd l" \
191 if $base_present; then
192 "$merge_tool_path" -X --show-merged-pane \
193 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
194 -R 'Accel.Search: "Ctrl+F"' \
195 -R 'Accel.SearchForward: "Ctrl-G"' \
196 --merged-file "$MERGED" \
197 "$LOCAL" "$BASE" "$REMOTE"
199 "$merge_tool_path" -X $extra \
200 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
201 -R 'Accel.Search: "Ctrl+F"' \
202 -R 'Accel.SearchForward: "Ctrl-G"' \
203 --merged-file "$MERGED" \
209 -R 'Accel.Search: "Ctrl+F"' \
210 -R 'Accel.SearchForward: "Ctrl-G"' \
217 if $base_present; then
218 "$merge_tool_path" "$LOCAL" "$REMOTE" \
220 -merge "$MERGED" |
cat
222 "$merge_tool_path" "$LOCAL" "$REMOTE" \
223 -merge "$MERGED" |
cat
227 "$merge_tool_path" "$LOCAL" "$REMOTE" |
cat
233 if $base_present; then
234 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" \
235 --default --mode=merge3
--to="$MERGED"
237 "$merge_tool_path" "$LOCAL" "$REMOTE" \
238 --default --mode=merge2
--to="$MERGED"
242 "$merge_tool_path" --default --mode=diff2 \
248 if $base_present; then
250 -f emerge-files-with-ancestor-command \
251 "$LOCAL" "$REMOTE" "$BASE" \
252 "$(basename "$MERGED")"
255 -f emerge-files-command \
257 "$(basename "$MERGED")"
261 "$merge_tool_path" -f emerge-files-command \
266 if $base_present; then
269 -base:"$BASE" -mine:"$LOCAL" \
270 -theirs:"$REMOTE" -merged:"$MERGED"
273 echo "TortoiseMerge cannot be used without a base" 1>&2
280 if $base_present; then
281 "$merge_tool_path" -wait -merge -3 -a1 \
282 "$BASE" "$LOCAL" "$REMOTE" "$MERGED" \
285 "$merge_tool_path" -wait -2 \
286 "$LOCAL" "$REMOTE" "$MERGED" \
291 "$merge_tool_path" -wait -2 "$LOCAL" "$REMOTE" \
296 merge_tool_cmd
="$(get_merge_tool_cmd "$1")"
297 if test -z "$merge_tool_cmd"; then
304 trust_exit_code
="$(git config --bool \
305 mergetool."$1".trustExitCode || echo false)"
306 if test "$trust_exit_code" = "false"; then
308 ( eval $merge_tool_cmd )
311 ( eval $merge_tool_cmd )
315 ( eval $merge_tool_cmd )
322 guess_merge_tool
() {
324 tools
="tortoisemerge"
328 if test -n "$DISPLAY"; then
329 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
330 tools
="meld opendiff kdiff3 tkdiff xxdiff $tools"
332 tools
="opendiff kdiff3 tkdiff xxdiff meld $tools"
334 tools
="$tools gvimdiff diffuse ecmerge p4merge araxis"
336 case "${VISUAL:-$EDITOR}" in
338 tools
="$tools vimdiff emerge"
341 tools
="$tools emerge vimdiff"
344 echo >&2 "merge tool candidates: $tools"
346 # Loop over each candidate and stop when a valid merge tool is found.
349 merge_tool_path
="$(translate_merge_tool_path "$i")"
350 if type "$merge_tool_path" > /dev
/null
2>&1; then
356 echo >&2 "No known merge resolution program available."
360 get_configured_merge_tool
() {
361 # Diff mode first tries diff.tool and falls back to merge.tool.
362 # Merge mode only checks merge.tool
364 merge_tool
=$
(git config
diff.tool || git config merge.tool
)
366 merge_tool
=$
(git config merge.tool
)
368 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"; then
369 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
370 echo >&2 "Resetting to default..."
376 get_merge_tool_path
() {
377 # A merge tool has been set, so verify that it's valid.
378 if test -n "$1"; then
381 merge_tool
="$(get_merge_tool)"
383 if ! valid_tool
"$merge_tool"; then
384 echo >&2 "Unknown merge tool $merge_tool"
388 merge_tool_path
=$
(git config difftool.
"$merge_tool".path ||
389 git config mergetool.
"$merge_tool".path
)
391 merge_tool_path
=$
(git config mergetool.
"$merge_tool".path
)
393 if test -z "$merge_tool_path"; then
394 merge_tool_path
="$(translate_merge_tool_path "$merge_tool")"
396 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
397 ! type "$merge_tool_path" > /dev
/null
2>&1; then
398 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
402 echo "$merge_tool_path"
406 # Check if a merge tool has been configured
407 merge_tool
=$
(get_configured_merge_tool
)
408 # Try to guess an appropriate merge tool if no tool has been set.
409 if test -z "$merge_tool"; then
410 merge_tool
="$(guess_merge_tool)" ||
exit