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 # If GIT_PREFIX is empty then we cannot use it in tools
90 # that expect to be able to chdir() to its value.
91 GIT_PREFIX
=${GIT_PREFIX:-.}
94 merge_tool_path
="$(get_merge_tool_path "$1")" ||
exit
102 if $base_present; then
103 "$merge_tool_path" -wait -merge -3 -a1 \
104 "$BASE" "$LOCAL" "$REMOTE" "$MERGED" \
107 "$merge_tool_path" -wait -2 \
108 "$LOCAL" "$REMOTE" "$MERGED" \
113 "$merge_tool_path" -wait -2 "$LOCAL" "$REMOTE" \
120 if $base_present; then
121 "$merge_tool_path" "$LOCAL" "$REMOTE" "$BASE" \
122 -mergeoutput="$MERGED"
124 "$merge_tool_path" "$LOCAL" "$REMOTE" \
125 -mergeoutput="$MERGED"
129 "$merge_tool_path" "$LOCAL" "$REMOTE"
135 if $base_present; then
137 "$LOCAL" "$MERGED" "$REMOTE" \
141 "$LOCAL" "$MERGED" "$REMOTE" |
cat
145 "$merge_tool_path" "$LOCAL" "$REMOTE" |
cat
151 if $base_present; then
152 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" \
153 --default --mode=merge3
--to="$MERGED"
155 "$merge_tool_path" "$LOCAL" "$REMOTE" \
156 --default --mode=merge2
--to="$MERGED"
160 "$merge_tool_path" --default --mode=diff2 \
166 if $base_present; then
168 -f emerge-files-with-ancestor-command \
169 "$LOCAL" "$REMOTE" "$BASE" \
170 "$(basename "$MERGED")"
173 -f emerge-files-command \
175 "$(basename "$MERGED")"
179 "$merge_tool_path" -f emerge-files-command \
186 if $base_present; then
187 "$merge_tool_path" -f -d -c "wincmd J" \
188 "$MERGED" "$LOCAL" "$BASE" "$REMOTE"
190 "$merge_tool_path" -f -d -c "wincmd l" \
191 "$LOCAL" "$MERGED" "$REMOTE"
195 "$merge_tool_path" -R -f -d -c "wincmd l" \
196 -c 'cd $GIT_PREFIX' \
203 "$merge_tool_path" -f -d -c "wincmd l" \
204 "$LOCAL" "$MERGED" "$REMOTE"
207 "$merge_tool_path" -R -f -d -c "wincmd l" \
208 -c 'cd $GIT_PREFIX' \
214 if $base_present; then
215 ("$merge_tool_path" --auto \
216 --L1 "$MERGED (Base)" \
217 --L2 "$MERGED (Local)" \
218 --L3 "$MERGED (Remote)" \
220 "$BASE" "$LOCAL" "$REMOTE" \
223 ("$merge_tool_path" --auto \
224 --L1 "$MERGED (Local)" \
225 --L2 "$MERGED (Remote)" \
232 ("$merge_tool_path" --auto \
234 --L2 "$MERGED (B)" "$LOCAL" "$REMOTE" \
239 "$merge_tool_path" "$LOCAL" "$REMOTE"
244 "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
247 "$merge_tool_path" "$LOCAL" "$REMOTE"
253 if $base_present; then
254 "$merge_tool_path" "$LOCAL" "$REMOTE" \
256 -merge "$MERGED" |
cat
258 "$merge_tool_path" "$LOCAL" "$REMOTE" \
259 -merge "$MERGED" |
cat
263 "$merge_tool_path" "$LOCAL" "$REMOTE" |
cat
269 $base_present ||
>"$BASE"
270 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
273 "$merge_tool_path" "$LOCAL" "$REMOTE"
278 if $base_present; then
279 "$merge_tool_path" -a "$BASE" \
280 -o "$MERGED" "$LOCAL" "$REMOTE"
283 -o "$MERGED" "$LOCAL" "$REMOTE"
287 "$merge_tool_path" "$LOCAL" "$REMOTE"
291 if $base_present; then
294 -base:"$BASE" -mine:"$LOCAL" \
295 -theirs:"$REMOTE" -merged:"$MERGED"
298 echo "TortoiseMerge cannot be used without a base" 1>&2
305 if $base_present; then
306 "$merge_tool_path" -X --show-merged-pane \
307 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
308 -R 'Accel.Search: "Ctrl+F"' \
309 -R 'Accel.SearchForward: "Ctrl-G"' \
310 --merged-file "$MERGED" \
311 "$LOCAL" "$BASE" "$REMOTE"
313 "$merge_tool_path" -X $extra \
314 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
315 -R 'Accel.Search: "Ctrl+F"' \
316 -R 'Accel.SearchForward: "Ctrl-G"' \
317 --merged-file "$MERGED" \
323 -R 'Accel.Search: "Ctrl+F"' \
324 -R 'Accel.SearchForward: "Ctrl-G"' \
329 merge_tool_cmd
="$(get_merge_tool_cmd "$1")"
330 if test -z "$merge_tool_cmd"; then
337 trust_exit_code
="$(git config --bool \
338 mergetool."$1".trustExitCode || echo false)"
339 if test "$trust_exit_code" = "false"; then
341 ( eval $merge_tool_cmd )
344 ( eval $merge_tool_cmd )
348 ( eval $merge_tool_cmd )
355 guess_merge_tool
() {
357 tools
="tortoisemerge"
361 if test -n "$DISPLAY"; then
362 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
363 tools
="meld opendiff kdiff3 tkdiff xxdiff $tools"
365 tools
="opendiff kdiff3 tkdiff xxdiff meld $tools"
367 tools
="$tools gvimdiff diffuse ecmerge p4merge araxis bc3"
369 case "${VISUAL:-$EDITOR}" in
371 tools
="$tools vimdiff emerge"
374 tools
="$tools emerge vimdiff"
377 echo >&2 "merge tool candidates: $tools"
379 # Loop over each candidate and stop when a valid merge tool is found.
382 merge_tool_path
="$(translate_merge_tool_path "$i")"
383 if type "$merge_tool_path" > /dev
/null
2>&1; then
389 echo >&2 "No known merge resolution program available."
393 get_configured_merge_tool
() {
394 # Diff mode first tries diff.tool and falls back to merge.tool.
395 # Merge mode only checks merge.tool
397 merge_tool
=$
(git config
diff.tool || git config merge.tool
)
399 merge_tool
=$
(git config merge.tool
)
401 if test -n "$merge_tool" && ! valid_tool
"$merge_tool"; then
402 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
403 echo >&2 "Resetting to default..."
409 get_merge_tool_path
() {
410 # A merge tool has been set, so verify that it's valid.
411 if test -n "$1"; then
414 merge_tool
="$(get_merge_tool)"
416 if ! valid_tool
"$merge_tool"; then
417 echo >&2 "Unknown merge tool $merge_tool"
421 merge_tool_path
=$
(git config difftool.
"$merge_tool".path ||
422 git config mergetool.
"$merge_tool".path
)
424 merge_tool_path
=$
(git config mergetool.
"$merge_tool".path
)
426 if test -z "$merge_tool_path"; then
427 merge_tool_path
="$(translate_merge_tool_path "$merge_tool")"
429 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
430 ! type "$merge_tool_path" > /dev
/null
2>&1; then
431 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
435 echo "$merge_tool_path"
439 # Check if a merge tool has been configured
440 merge_tool
=$
(get_configured_merge_tool
)
441 # Try to guess an appropriate merge tool if no tool has been set.
442 if test -z "$merge_tool"; then
443 merge_tool
="$(guess_merge_tool)" ||
exit