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 () {
35 if test "$MERGED" -nt "$BACKUP"; then
39 echo "$MERGED seems unchanged."
40 printf "Was the merge successful? [y/n] "
43 y*|Y*) status=0; break ;;
44 n*|N*) status=1; break ;;
52 araxis | bc3 | diffuse | ecmerge | emerge | gvimdiff | gvimdiff2 | \
53 kdiff3 | meld | opendiff | p4merge | tkdiff | vimdiff | vimdiff2 | xxdiff)
66 if test -z "$(get_merge_tool_cmd "$1")"; then
73 get_merge_tool_cmd () {
74 # Prints the custom command for a merge tool
78 merge_tool="$(get_merge_tool)"
81 echo "$(git config difftool.$merge_tool.cmd ||
82 git config mergetool.$merge_tool.cmd)"
84 echo "$(git config mergetool.$merge_tool.cmd)"
89 merge_tool_path="$(get_merge_tool_path "$1")" || exit
97 if $base_present; then
98 "$merge_tool_path" -wait -merge -3 -a1 \
99 "$BASE" "$LOCAL" "$REMOTE" "$MERGED" \
102 "$merge_tool_path" -wait -2 \
103 "$LOCAL" "$REMOTE" "$MERGED" \
108 "$merge_tool_path" -wait -2 "$LOCAL" "$REMOTE" \
115 if $base_present; then
116 "$merge_tool_path" "$LOCAL" "$REMOTE" "$BASE" \
117 -mergeoutput="$MERGED"
119 "$merge_tool_path" "$LOCAL" "$REMOTE" \
120 -mergeoutput="$MERGED"
124 "$merge_tool_path" "$LOCAL" "$REMOTE"
130 if $base_present; then
132 "$LOCAL" "$MERGED" "$REMOTE" \
136 "$LOCAL" "$MERGED" "$REMOTE" | cat
140 "$merge_tool_path" "$LOCAL" "$REMOTE" | cat
146 if $base_present; then
147 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" \
148 --default --mode=merge3 --to="$MERGED"
150 "$merge_tool_path" "$LOCAL" "$REMOTE" \
151 --default --mode=merge2 --to="$MERGED"
155 "$merge_tool_path" --default --mode=diff2 \
161 if $base_present; then
163 -f emerge-files-with-ancestor-command \
164 "$LOCAL" "$REMOTE" "$BASE" \
165 "$(basename "$MERGED")"
168 -f emerge-files-command \
170 "$(basename "$MERGED")"
174 "$merge_tool_path" -f emerge-files-command \
181 if $base_present; then
182 "$merge_tool_path" -f -d -c "wincmd J" \
183 "$MERGED" "$LOCAL" "$BASE" "$REMOTE"
185 "$merge_tool_path" -f -d -c "wincmd l" \
186 "$LOCAL" "$MERGED" "$REMOTE"
190 "$merge_tool_path" -R -f -d -c "wincmd l" \
197 "$merge_tool_path" -f -d -c "wincmd l" \
198 "$LOCAL" "$MERGED" "$REMOTE"
201 "$merge_tool_path" -R -f -d -c "wincmd l" \
207 if $base_present; then
208 ("$merge_tool_path" --auto \
209 --L1 "$MERGED (Base)" \
210 --L2 "$MERGED (Local)" \
211 --L3 "$MERGED (Remote)" \
213 "$BASE" "$LOCAL" "$REMOTE" \
216 ("$merge_tool_path" --auto \
217 --L1 "$MERGED (Local)" \
218 --L2 "$MERGED (Remote)" \
225 ("$merge_tool_path" --auto \
227 --L2 "$MERGED (B)" "$LOCAL" "$REMOTE" \
232 "$merge_tool_path" "$LOCAL" "$REMOTE"
237 "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
240 "$merge_tool_path" "$LOCAL" "$REMOTE"
246 if $base_present; then
247 "$merge_tool_path" "$LOCAL" "$REMOTE" \
249 -merge "$MERGED" | cat
251 "$merge_tool_path" "$LOCAL" "$REMOTE" \
252 -merge "$MERGED" | cat
256 "$merge_tool_path" "$LOCAL" "$REMOTE" | cat
262 $base_present || >"$BASE"
263 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
266 "$merge_tool_path" "$LOCAL" "$REMOTE"
271 if $base_present; then
272 "$merge_tool_path" -a "$BASE" \
273 -o "$MERGED" "$LOCAL" "$REMOTE"
276 -o "$MERGED" "$LOCAL" "$REMOTE"
280 "$merge_tool_path" "$LOCAL" "$REMOTE"
284 if $base_present; then
287 -base:"$BASE" -mine:"$LOCAL" \
288 -theirs:"$REMOTE" -merged:"$MERGED"
291 echo "TortoiseMerge cannot be used without a base" 1>&2
298 if $base_present; then
299 "$merge_tool_path" -X --show-merged-pane \
300 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
301 -R 'Accel.Search: "Ctrl+F"' \
302 -R 'Accel.SearchForward: "Ctrl-G"' \
303 --merged-file "$MERGED" \
304 "$LOCAL" "$BASE" "$REMOTE"
306 "$merge_tool_path" -X $extra \
307 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
308 -R 'Accel.Search: "Ctrl+F"' \
309 -R 'Accel.SearchForward: "Ctrl-G"' \
310 --merged-file "$MERGED" \
316 -R 'Accel.Search: "Ctrl+F"' \
317 -R 'Accel.SearchForward: "Ctrl-G"' \
322 merge_tool_cmd="$(get_merge_tool_cmd "$1")"
323 if test -z "$merge_tool_cmd"; then
330 trust_exit_code="$(git config --bool \
331 mergetool."$1".trustExitCode || echo false)"
332 if test "$trust_exit_code" = "false"; then
334 ( eval $merge_tool_cmd )
337 ( eval $merge_tool_cmd )
341 ( eval $merge_tool_cmd )
348 guess_merge_tool () {
350 tools="tortoisemerge"
354 if test -n "$DISPLAY"; then
355 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
356 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
358 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
360 tools="$tools gvimdiff diffuse ecmerge p4merge araxis bc3"
362 case "${VISUAL:-$EDITOR}" in
364 tools="$tools vimdiff emerge"
367 tools="$tools emerge vimdiff"
370 echo >&2 "merge tool candidates: $tools"
372 # Loop over each candidate and stop when a valid merge tool is found.
375 merge_tool_path="$(translate_merge_tool_path "$i")"
376 if type "$merge_tool_path" > /dev/null 2>&1; then
382 echo >&2 "No known merge resolution program available."
386 get_configured_merge_tool () {
387 # Diff mode first tries diff.tool and falls back to merge.tool.
388 # Merge mode only checks merge.tool
390 merge_tool=$(git config diff.tool || git config merge.tool)
392 merge_tool=$(git config merge.tool)
394 if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then
395 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
396 echo >&2 "Resetting to default..."
402 get_merge_tool_path () {
403 # A merge tool has been set, so verify that it's valid.
404 if test -n "$1"; then
407 merge_tool="$(get_merge_tool)"
409 if ! valid_tool "$merge_tool"; then
410 echo >&2 "Unknown merge tool $merge_tool"
414 merge_tool_path=$(git config difftool."$merge_tool".path ||
415 git config mergetool."$merge_tool".path)
417 merge_tool_path=$(git config mergetool."$merge_tool".path)
419 if test -z "$merge_tool_path"; then
420 merge_tool_path="$(translate_merge_tool_path "$merge_tool")"
422 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
423 ! type "$merge_tool_path" > /dev/null 2>&1; then
424 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
428 echo "$merge_tool_path"
432 # Check if a merge tool has been configured
433 merge_tool=$(get_configured_merge_tool)
434 # Try to guess an appropriate merge tool if no tool has been set.
435 if test -z "$merge_tool"; then
436 merge_tool="$(guess_merge_tool)" || exit