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 () {
33 if test "$MERGED" -nt "$BACKUP"; then
37 echo "$MERGED seems unchanged."
38 printf "Was the merge successful? [y/n] "
39 read answer < /dev/tty
41 y*|Y*) status=0; break ;;
42 n*|N*) status=1; break ;;
50 kdiff3 | tkdiff | xxdiff | meld | opendiff | \
51 emerge | vimdiff | gvimdiff | ecmerge | diffuse)
64 if test -z "$(get_merge_tool_cmd "$1")"; then
71 get_merge_tool_cmd () {
73 custom_cmd="$(git config difftool.$1.cmd)"
74 test -z "$custom_cmd" &&
75 custom_cmd="$(git config mergetool.$1.cmd)"
76 test -n "$custom_cmd" &&
87 if $base_present; then
88 ("$merge_tool_path" --auto \
89 --L1 "$MERGED (Base)" \
90 --L2 "$MERGED (Local)" \
91 --L3 "$MERGED (Remote)" \
93 "$BASE" "$LOCAL" "$REMOTE" \
96 ("$merge_tool_path" --auto \
97 --L1 "$MERGED (Local)" \
98 --L2 "$MERGED (Remote)" \
105 ("$merge_tool_path" --auto \
107 --L2 "$MERGED (B)" "$LOCAL" "$REMOTE" \
112 "$merge_tool_path" "$LOCAL" "$REMOTE"
116 if $base_present; then
117 "$merge_tool_path" -a "$BASE" \
118 -o "$MERGED" "$LOCAL" "$REMOTE"
121 -o "$MERGED" "$LOCAL" "$REMOTE"
125 "$merge_tool_path" "$LOCAL" "$REMOTE"
131 "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"
134 "$merge_tool_path" "$LOCAL" "$REMOTE"
140 if $base_present; then
142 "$LOCAL" "$MERGED" "$REMOTE" \
146 "$LOCAL" "$MERGED" "$REMOTE" | cat
150 "$merge_tool_path" "$LOCAL" "$REMOTE" | cat
156 "$merge_tool_path" -d -c "wincmd l" \
157 "$LOCAL" "$MERGED" "$REMOTE"
160 "$merge_tool_path" -d -c "wincmd l" \
167 "$merge_tool_path" -d -c "wincmd l" -f \
168 "$LOCAL" "$MERGED" "$REMOTE"
171 "$merge_tool_path" -d -c "wincmd l" -f \
178 if $base_present; then
179 "$merge_tool_path" -X --show-merged-pane \
180 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
181 -R 'Accel.Search: "Ctrl+F"' \
182 -R 'Accel.SearchForward: "Ctrl-G"' \
183 --merged-file "$MERGED" \
184 "$LOCAL" "$BASE" "$REMOTE"
186 "$merge_tool_path" -X $extra \
187 -R 'Accel.SaveAsMerged: "Ctrl-S"' \
188 -R 'Accel.Search: "Ctrl+F"' \
189 -R 'Accel.SearchForward: "Ctrl-G"' \
190 --merged-file "$MERGED" \
196 -R 'Accel.Search: "Ctrl+F"' \
197 -R 'Accel.SearchForward: "Ctrl-G"' \
204 if $base_present; then
205 "$merge_tool_path" "$LOCAL" "$REMOTE" \
207 -merge "$MERGED" | cat
209 "$merge_tool_path" "$LOCAL" "$REMOTE" \
210 -merge "$MERGED" | cat
214 "$merge_tool_path" "$LOCAL" "$REMOTE" | cat
220 if $base_present; then
221 "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" \
222 --default --mode=merge3 --to="$MERGED"
224 "$merge_tool_path" "$LOCAL" "$REMOTE" \
225 --default --mode=merge2 --to="$MERGED"
229 "$merge_tool_path" "$LOCAL" "$REMOTE" \
230 --default --mode=merge2 --to="$MERGED"
235 if $base_present; then
237 -f emerge-files-with-ancestor-command \
238 "$LOCAL" "$REMOTE" "$BASE" \
239 "$(basename "$MERGED")"
242 -f emerge-files-command \
244 "$(basename "$MERGED")"
248 "$merge_tool_path" -f emerge-files-command \
249 "$LOCAL" "$REMOTE" "$(basename "$MERGED")"
253 if $base_present; then
256 -base:"$BASE" -mine:"$LOCAL" \
257 -theirs:"$REMOTE" -merged:"$MERGED"
260 echo "TortoiseMerge cannot be used without a base" 1>&2
265 if test -z "$merge_tool_cmd"; then
272 if test "$merge_tool_trust_exit_code" = "false"; then
274 ( eval $merge_tool_cmd )
277 ( eval $merge_tool_cmd )
281 ( eval $merge_tool_cmd )
288 guess_merge_tool () {
290 tools="tortoisemerge"
294 if test -n "$DISPLAY"; then
295 if test -n "$GNOME_DESKTOP_SESSION_ID" ; then
296 tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
298 tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
300 tools="$tools gvimdiff diffuse ecmerge"
302 if echo "${VISUAL:-$EDITOR}" | grep emacs > /dev/null 2>&1; then
303 # $EDITOR is emacs so add emerge as a candidate
304 tools="$tools emerge vimdiff"
305 elif echo "${VISUAL:-$EDITOR}" | grep vim > /dev/null 2>&1; then
306 # $EDITOR is vim so add vimdiff as a candidate
307 tools="$tools vimdiff emerge"
309 tools="$tools emerge vimdiff"
311 echo >&2 "merge tool candidates: $tools"
313 # Loop over each candidate and stop when a valid merge tool is found.
316 merge_tool_path="$(translate_merge_tool_path "$i")"
317 if type "$merge_tool_path" > /dev/null 2>&1; then
323 if test -z "$merge_tool" ; then
324 echo >&2 "No known merge resolution program available."
330 get_configured_merge_tool () {
331 # Diff mode first tries diff.tool and falls back to merge.tool.
332 # Merge mode only checks merge.tool
334 tool=$(git config diff.tool)
336 if test -z "$tool"; then
337 tool=$(git config merge.tool)
339 if test -n "$merge_tool" && ! valid_tool "$merge_tool"; then
340 echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
341 echo >&2 "Resetting to default..."
347 get_merge_tool_path () {
348 # A merge tool has been set, so verify that it's valid.
349 if ! valid_tool "$merge_tool"; then
350 echo >&2 "Unknown merge tool $merge_tool"
354 merge_tool_path=$(git config difftool."$merge_tool".path)
356 if test -z "$merge_tool_path"; then
357 merge_tool_path=$(git config mergetool."$merge_tool".path)
359 merge_tool_path="$(translate_merge_tool_path "$merge_tool" "$merge_tool_path")"
360 if test -z "$merge_tool_cmd" && ! type "$merge_tool_path" > /dev/null 2>&1; then
361 echo >&2 "The $TOOL_MODE tool $merge_tool is not available as '$merge_tool_path'"
364 echo "$merge_tool_path"
369 # Check if a merge tool has been configured
370 if test -z "$merge_tool"; then
371 merge_tool=$(get_configured_merge_tool)
373 # Try to guess an appropriate merge tool if no tool has been set.
374 if test -z "$merge_tool"; then
375 merge_tool=$(guess_merge_tool) || exit