mergetool--lib: add functions for finding available tools

Refactor show_tool_help() so that the tool-finding logic is broken out into a separate show_tool_names() function. Signed-off-by: David Aguilar <davvid@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Aguilar committed Jan 27, 2013 at 16:52 UTC 17a1f1c5b7041afab2e184304d8194a2f81f196d
1 file changed +65 -36
git-mergetool--lib.sh
+65 -36
@@ -2,6 +2,53 @@
2 # git-mergetool--lib is a library for common merge tool functions
3 MERGE_TOOLS_DIR=$(git --exec-path)/mergetools
4
5 +mode_ok () {
6 + if diff_mode
7 + then
8 + can_diff
9 + elif merge_mode
10 + then
11 + can_merge
12 + else
13 + false
14 + fi
15 +}
16 +
17 +is_available () {
18 + merge_tool_path=$(translate_merge_tool_path "$1") &&
19 + type "$merge_tool_path" >/dev/null 2>&1
20 +}
21 +
22 +show_tool_names () {
23 + condition=${1:-true} per_line_prefix=${2:-} preamble=${3:-}
24 + not_found_msg=${4:-}
25 +
26 + shown_any=
27 + ( cd "$MERGE_TOOLS_DIR" && ls ) | {
28 + while read toolname
29 + do
30 + if setup_tool "$toolname" 2>/dev/null &&
31 + (eval "$condition" "$toolname")
32 + then
33 + if test -n "$preamble"
34 + then
35 + printf "%s\n" "$preamble"
36 + preamble=
37 + fi
38 + shown_any=yes
39 + printf "%s%s\n" "$per_line_prefix" "$toolname"
40 + fi
41 + done
42 +
43 + if test -n "$preamble" && test -n "$not_found_msg"
44 + then
45 + printf "%s\n" "$not_found_msg"
46 + fi
47 +
48 + test -n "$shown_any"
49 + }
50 +}
51 +
52 diff_mode() {
53 test "$TOOL_MODE" = diff
54 }
@@ -199,37 +246,24 @@ list_merge_tool_candidates () {
246 }
247
248 show_tool_help () {
202 - unavailable= available= LF='
203 -'
204 - for i in "$MERGE_TOOLS_DIR"/*
205 - do
206 - tool=$(basename "$i")
207 - setup_tool "$tool" 2>/dev/null || continue
249 + tool_opt="'git ${TOOL_MODE}tool --tool-<tool>'"
250
209 - merge_tool_path=$(translate_merge_tool_path "$tool")
210 - if type "$merge_tool_path" >/dev/null 2>&1
211 - then
212 - available="$available$tool$LF"
213 - else
214 - unavailable="$unavailable$tool$LF"
215 - fi
216 - done
251 + tab=' '
252 + LF='
253 +'
254 + any_shown=no
255
256 cmd_name=${TOOL_MODE}tool
219 - if test -n "$available"
220 - then
221 - echo "'git $cmd_name --tool=<tool>' may be set to one of the following:"
222 - echo "$available" | sort | sed -e 's/^/ /'
223 - else
224 - echo "No suitable tool for 'git $cmd_name --tool=<tool>' found."
225 - fi
226 - if test -n "$unavailable"
227 - then
228 - echo
229 - echo 'The following tools are valid, but not currently available:'
230 - echo "$unavailable" | sort | sed -e 's/^/ /'
231 - fi
232 - if test -n "$unavailable$available"
257 + show_tool_names 'mode_ok && is_available' "$tab$tab" \
258 + "$tool_opt may be set to one of the following:" \
259 + "No suitable tool for 'git $cmd_name --tool=<tool>' found." &&
260 + any_shown=yes
261 +
262 + show_tool_names 'mode_ok && ! is_available' "$tab$tab" \
263 + "${LF}The following tools are valid, but not currently available:" &&
264 + any_shown=yes
265 +
266 + if test "$any_shown" = yes
267 then
268 echo
269 echo "Some of the tools listed above only work in a windowed"
@@ -249,17 +283,12 @@ guess_merge_tool () {
283 EOF
284
285 # Loop over each candidate and stop when a valid merge tool is found.
252 - for i in $tools
286 + for tool in $tools
287 do
254 - merge_tool_path=$(translate_merge_tool_path "$i")
255 - if type "$merge_tool_path" >/dev/null 2>&1
256 - then
257 - echo "$i"
258 - return 0
259 - fi
288 + is_available "$tool" && echo "$tool" && return 0
289 done
290
262 - echo >&2 "No known merge resolution program available."
291 + echo >&2 "No known ${TOOL_MODE} tool is available."
292 return 1
293 }
294