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 vimdiff | gvimdiff | vimdiff2 | gvimdiff2 | \
51 emerge | ecmerge | diffuse | araxis | p4merge)
64 if test -z "$(get_merge_tool_cmd "$1")"; then
71 get_merge_tool_cmd () {
72 # Prints the custom command for a merge tool
76 merge_tool="$(get_merge_tool)"
79 echo "$(git config difftool.$merge_tool.cmd ||
80 git config mergetool.$merge_tool.cmd)"
82 echo "$(git config mergetool.$merge_tool.cmd)"
87 merge_tool_path="$(get_merge_tool_path "$1")" || exit
94 if $base_present; then
95 ("$merge_tool_path" --auto \
96 --L1 "$MERGED (Base)" \
97 --L2 "$MERGED (Local)" \
98 --L3 "$MERGED (Remote)" \
100 "$BASE" "$LOCAL" "$REMOTE" \
103 ("$merge_tool_path" --auto \
104 --L1 "$MERGED (Local)" \
105 --L2 "$MERGED (Remote)" \
112 ("$merge_tool_path" --auto \
114 --L2 "$MERGED (B)" "$LOCAL" "$REMOTE" \
119 "$merge_tool_path" "$LOCAL" "$REMOTE"
123 if $base_present; then
124 "$merge_tool_path" -a "$BASE" \
125 -o "$MERGED" "$LOCAL" "$REMOTE"
128 -o "$MERGED" "$LOCAL" "$REMOTE"
132 "$merge_tool_path" "$LOCAL" "$REMOTE"
138 if $base_present; then
139 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
141 "$merge_tool_path" "$LOCAL" "$LOCAL" "$REMOTE" "$MERGED"
145 "$merge_tool_path" "$LOCAL" "$REMOTE"
151 "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
154 "$merge_tool_path" "$LOCAL" "$REMOTE"
160 if $base_present; then
162 "$LOCAL" "$MERGED" "$REMOTE" \
166 "$LOCAL" "$MERGED" "$REMOTE" | cat
170 "$merge_tool_path" "$LOCAL" "$REMOTE" | cat
176 if $base_present; then
177 "$merge_tool_path" -f -d -c "wincmd J" \
178 "$MERGED" "$LOCAL" "$BASE" "$REMOTE"
180 "$merge_tool_path" -f -d -c "wincmd l" \
181 "$LOCAL" "$MERGED" "$REMOTE"
185 "$merge_tool_path" -R -f -d -c "wincmd l" \
192 "$merge_tool_path" -f -d -c "wincmd l" \
193 "$LOCAL" "$MERGED" "$REMOTE"
196 "$merge_tool_path" -R -f -d -c "wincmd l" \
203 if $base_present; then
204 "$merge_tool_path" -X --show-merged-pane \
205 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
206 -R 'Accel.Search: "Ctrl+F"' \
207 -R 'Accel.SearchForward: "Ctrl-G"' \
208 --merged-file "$MERGED" \
209 "$LOCAL" "$BASE" "$REMOTE"
211 "$merge_tool_path" -X $extra \
212 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
213 -R 'Accel.Search: "Ctrl+F"' \
214 -R 'Accel.SearchForward: "Ctrl-G"' \
215 --merged-file "$MERGED" \
221 -R 'Accel.Search: "Ctrl+F"' \
222 -R 'Accel.SearchForward: "Ctrl-G"' \
229 if $base_present; then
230 "$merge_tool_path" "$LOCAL" "$REMOTE" \
232 -merge "$MERGED" | cat
234 "$merge_tool_path" "$LOCAL" "$REMOTE" \
235 -merge "$MERGED" | cat
239 "$merge_tool_path" "$LOCAL" "$REMOTE" | cat
245 if $base_present; then
246 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" \
247 --default --mode=merge3 --to="$MERGED"
249 "$merge_tool_path" "$LOCAL" "$REMOTE" \
250 --default --mode=merge2 --to="$MERGED"
254 "$merge_tool_path" --default --mode=diff2 \
260 if $base_present; then
262 -f emerge-files-with-ancestor-command \
263 "$LOCAL" "$REMOTE" "$BASE" \
264 "$(basename "$MERGED")"
267 -f emerge-files-command \
269 "$(basename "$MERGED")"
273 "$merge_tool_path" -f emerge-files-command \
278 if $base_present; then
281 -base:"$BASE" -mine:"$LOCAL" \
282 -theirs:"$REMOTE" -merged:"$MERGED"
285 echo "TortoiseMerge cannot be used without a base" 1>&2
292 if $base_present; then
293 "$merge_tool_path" -wait -merge -3 -a1 \
294 "$BASE" "$LOCAL" "$REMOTE" "$MERGED" \
297 "$merge_tool_path" -wait -2 \
298 "$LOCAL" "$REMOTE" "$MERGED" \
303 "$merge_tool_path" -wait -2 "$LOCAL" "$REMOTE" \
308 merge_tool_cmd="$(get_merge_tool_cmd "$1")"
309 if test -z "$merge_tool_cmd"; then
316 trust_exit_code="$(git config --bool \
317 mergetool."$1".trustExitCode || echo false)"
318 if test "$trust_exit_code" = "false"; then
320 ( eval $merge_tool_cmd )
323 ( eval $merge_tool_cmd )
327 ( eval $merge_tool_cmd )
334 guess_merge_tool () {
336 tools="tortoisemerge"
340 if test -n "$DISPLAY"; then
341 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
342 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
344 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
346 tools="$tools gvimdiff diffuse ecmerge p4merge araxis"
348 case "${VISUAL:-$EDITOR}" in
350 tools="$tools vimdiff emerge"
353 tools="$tools emerge vimdiff"
356 echo >&2 "merge tool candidates: $tools"
358 # Loop over each candidate and stop when a valid merge tool is found.
361 merge_tool_path="$(translate_merge_tool_path "$i")"
362 if type "$merge_tool_path" > /dev/null 2>&1; then
368 echo >&2 "No known merge resolution program available."
372 get_configured_merge_tool () {
373 # Diff mode first tries diff.tool and falls back to merge.tool.
374 # Merge mode only checks merge.tool
376 merge_tool=$(git config diff.tool || git config merge.tool)
378 merge_tool=$(git config merge.tool)
380 if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then
381 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
382 echo >&2 "Resetting to default..."
388 get_merge_tool_path () {
389 # A merge tool has been set, so verify that it's valid.
390 if test -n "$1"; then
393 merge_tool="$(get_merge_tool)"
395 if ! valid_tool "$merge_tool"; then
396 echo >&2 "Unknown merge tool $merge_tool"
400 merge_tool_path=$(git config difftool."$merge_tool".path ||
401 git config mergetool."$merge_tool".path)
403 merge_tool_path=$(git config mergetool."$merge_tool".path)
405 if test -z "$merge_tool_path"; then
406 merge_tool_path="$(translate_merge_tool_path "$merge_tool")"
408 if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
409 ! type "$merge_tool_path" > /dev/null 2>&1; then
410 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
414 echo "$merge_tool_path"
418 # Check if a merge tool has been configured
419 merge_tool=$(get_configured_merge_tool)
420 # Try to guess an appropriate merge tool if no tool has been set.
421 if test -z "$merge_tool"; then
422 merge_tool="$(guess_merge_tool)" || exit