1#!/bin/sh
2# git-mergetool--lib is a library for common merge tool functions
3MERGE_TOOLS_DIR=$(git --exec-path)/mergetools
45
diff_mode() {
6test "$TOOL_MODE" = diff
7}
89
merge_mode() {
10test "$TOOL_MODE" = merge
11}
1213
translate_merge_tool_path () {
14echo "$1"
15}
1617
check_unchanged () {
18if test "$MERGED" -nt "$BACKUP"
19then
20status=0
21else
22while true
23do
24echo "$MERGED seems unchanged."
25printf "Was the merge successful? [y/n] "
26read answer || return 1
27case "$answer" in
28y*|Y*) status=0; break ;;
29n*|N*) status=1; break ;;
30esac
31done
32fi
33}
3435
valid_tool_config () {
36if test -n "$(get_merge_tool_cmd "$1")"
37then
38return 0
39else
40return 1
41fi
42}
4344
valid_tool () {
45setup_tool "$1" || valid_tool_config "$1"
46}
4748
setup_tool () {
49tool="$1"
5051
# Fallback definitions, to be overriden by tools.
52can_merge () {
53return 0
54}
5556
can_diff () {
57return 0
58}
5960
diff_cmd () {
61status=1
62return $status
63}
6465
merge_cmd () {
66status=1
67return $status
68}
6970
translate_merge_tool_path () {
71echo "$1"
72}
7374
if ! test -f "$MERGE_TOOLS_DIR/$tool"
75then
76# Use a special return code for this case since we want to
77# source "defaults" even when an explicit tool path is
78# configured since the user can use that to override the
79# default path in the scriptlet.
80return 2
81fi
8283
# Load the redefined functions
84. "$MERGE_TOOLS_DIR/$tool"
8586
if merge_mode && ! can_merge
87then
88echo "error: '$tool' can not be used to resolve merges" >&2
89return 1
90elif diff_mode && ! can_diff
91then
92echo "error: '$tool' can only be used to resolve merges" >&2
93return 1
94fi
95return 0
96}
9798
get_merge_tool_cmd () {
99# Prints the custom command for a merge tool
100merge_tool="$1"
101if diff_mode
102then
103echo "$(git config difftool.$merge_tool.cmd ||
104git config mergetool.$merge_tool.cmd)"
105else
106echo "$(git config mergetool.$merge_tool.cmd)"
107fi
108}
109110
# Entry point for running tools
111run_merge_tool () {
112# If GIT_PREFIX is empty then we cannot use it in tools
113# that expect to be able to chdir() to its value.
114GIT_PREFIX=${GIT_PREFIX:-.}
115export GIT_PREFIX
116117
merge_tool_path="$(get_merge_tool_path "$1")" || exit
118base_present="$2"
119status=0
120121
# Bring tool-specific functions into scope
122setup_tool "$1"
123exitcode=$?
124case $exitcode in
1250)
126:
127;;
1282)
129# The configured tool is not a built-in tool.
130test -n "$merge_tool_path" || return 1
131;;
132*)
133return $exitcode
134;;
135esac
136137
if merge_mode
138then
139run_merge_cmd "$1"
140else
141run_diff_cmd "$1"
142fi
143return $status
144}
145146
# Run a either a configured or built-in diff tool
147run_diff_cmd () {
148merge_tool_cmd="$(get_merge_tool_cmd "$1")"
149if test -n "$merge_tool_cmd"
150then
151( eval $merge_tool_cmd )
152status=$?
153return $status
154else
155diff_cmd "$1"
156fi
157}
158159
# Run a either a configured or built-in merge tool
160run_merge_cmd () {
161merge_tool_cmd="$(get_merge_tool_cmd "$1")"
162if test -n "$merge_tool_cmd"
163then
164trust_exit_code="$(git config --bool \
165mergetool."$1".trustExitCode || echo false)"
166if test "$trust_exit_code" = "false"
167then
168touch "$BACKUP"
169( eval $merge_tool_cmd )
170status=$?
171check_unchanged
172else
173( eval $merge_tool_cmd )
174status=$?
175fi
176return $status
177else
178merge_cmd "$1"
179fi
180}
181182
list_merge_tool_candidates () {
183if merge_mode
184then
185tools="tortoisemerge"
186else
187tools="kompare"
188fi
189if test -n "$DISPLAY"
190then
191if test -n "$GNOME_DESKTOP_SESSION_ID"
192then
193tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
194else
195tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
196fi
197tools="$tools gvimdiff diffuse ecmerge p4merge araxis bc3 codecompare"
198fi
199case "${VISUAL:-$EDITOR}" in
200*vim*)
201tools="$tools vimdiff emerge"
202;;
203*)
204tools="$tools emerge vimdiff"
205;;
206esac
207}
208209
show_tool_help () {
210unavailable= available= LF='
211'
212for i in "$MERGE_TOOLS_DIR"/*
213do
214tool=$(basename "$i")
215setup_tool "$tool" 2>/dev/null || continue
216217
merge_tool_path=$(translate_merge_tool_path "$tool")
218if type "$merge_tool_path" >/dev/null 2>&1
219then
220available="$available$tool$LF"
221else
222unavailable="$unavailable$tool$LF"
223fi
224done
225226
cmd_name=${TOOL_MODE}tool
227if test -n "$available"
228then
229echo "'git $cmd_name --tool=<tool>' may be set to one of the following:"
230echo "$available" | sort | sed -e 's/^/ /'
231else
232echo "No suitable tool for 'git $cmd_name --tool=<tool>' found."
233fi
234if test -n "$unavailable"
235then
236echo
237echo 'The following tools are valid, but not currently available:'
238echo "$unavailable" | sort | sed -e 's/^/ /'
239fi
240if test -n "$unavailable$available"
241then
242echo
243echo "Some of the tools listed above only work in a windowed"
244echo "environment. If run in a terminal-only session, they will fail."
245fi
246exit 0
247}
248249
guess_merge_tool () {
250list_merge_tool_candidates
251echo >&2 "merge tool candidates: $tools"
252253
# Loop over each candidate and stop when a valid merge tool is found.
254for i in $tools
255do
256merge_tool_path="$(translate_merge_tool_path "$i")"
257if type "$merge_tool_path" >/dev/null 2>&1
258then
259echo "$i"
260return 0
261fi
262done
263264
echo >&2 "No known merge resolution program available."
265return 1
266}
267268
get_configured_merge_tool () {
269# Diff mode first tries diff.tool and falls back to merge.tool.
270# Merge mode only checks merge.tool
271if diff_mode
272then
273merge_tool=$(git config diff.tool || git config merge.tool)
274else
275merge_tool=$(git config merge.tool)
276fi
277if test -n "$merge_tool" && ! valid_tool "$merge_tool"
278then
279echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
280echo >&2 "Resetting to default..."
281return 1
282fi
283echo "$merge_tool"
284}
285286
get_merge_tool_path () {
287# A merge tool has been set, so verify that it's valid.
288merge_tool="$1"
289if ! valid_tool "$merge_tool"
290then
291echo >&2 "Unknown merge tool $merge_tool"
292exit 1
293fi
294if diff_mode
295then
296merge_tool_path=$(git config difftool."$merge_tool".path ||
297git config mergetool."$merge_tool".path)
298else
299merge_tool_path=$(git config mergetool."$merge_tool".path)
300fi
301if test -z "$merge_tool_path"
302then
303merge_tool_path="$(translate_merge_tool_path "$merge_tool")"
304fi
305if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
306! type "$merge_tool_path" >/dev/null 2>&1
307then
308echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
309"'$merge_tool_path'"
310exit 1
311fi
312echo "$merge_tool_path"
313}
314315
get_merge_tool () {
316# Check if a merge tool has been configured
317merge_tool="$(get_configured_merge_tool)"
318# Try to guess an appropriate merge tool if no tool has been set.
319if test -z "$merge_tool"
320then
321merge_tool="$(guess_merge_tool)" || exit
322fi
323echo "$merge_tool"
324}