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>
This commit is contained in:
David Aguilar 2013-01-27 16:52:25 -08:00 committed by Junio C Hamano
parent 5338a6a924
commit 17a1f1c5b7

View File

@ -2,6 +2,53 @@
# git-mergetool--lib is a library for common merge tool functions # git-mergetool--lib is a library for common merge tool functions
MERGE_TOOLS_DIR=$(git --exec-path)/mergetools MERGE_TOOLS_DIR=$(git --exec-path)/mergetools
mode_ok () {
if diff_mode
then
can_diff
elif merge_mode
then
can_merge
else
false
fi
}
is_available () {
merge_tool_path=$(translate_merge_tool_path "$1") &&
type "$merge_tool_path" >/dev/null 2>&1
}
show_tool_names () {
condition=${1:-true} per_line_prefix=${2:-} preamble=${3:-}
not_found_msg=${4:-}
shown_any=
( cd "$MERGE_TOOLS_DIR" && ls ) | {
while read toolname
do
if setup_tool "$toolname" 2>/dev/null &&
(eval "$condition" "$toolname")
then
if test -n "$preamble"
then
printf "%s\n" "$preamble"
preamble=
fi
shown_any=yes
printf "%s%s\n" "$per_line_prefix" "$toolname"
fi
done
if test -n "$preamble" && test -n "$not_found_msg"
then
printf "%s\n" "$not_found_msg"
fi
test -n "$shown_any"
}
}
diff_mode() { diff_mode() {
test "$TOOL_MODE" = diff test "$TOOL_MODE" = diff
} }
@ -199,37 +246,24 @@ list_merge_tool_candidates () {
} }
show_tool_help () { show_tool_help () {
unavailable= available= LF=' tool_opt="'git ${TOOL_MODE}tool --tool-<tool>'"
'
for i in "$MERGE_TOOLS_DIR"/*
do
tool=$(basename "$i")
setup_tool "$tool" 2>/dev/null || continue
merge_tool_path=$(translate_merge_tool_path "$tool") tab=' '
if type "$merge_tool_path" >/dev/null 2>&1 LF='
then '
available="$available$tool$LF" any_shown=no
else
unavailable="$unavailable$tool$LF"
fi
done
cmd_name=${TOOL_MODE}tool cmd_name=${TOOL_MODE}tool
if test -n "$available" show_tool_names 'mode_ok && is_available' "$tab$tab" \
then "$tool_opt may be set to one of the following:" \
echo "'git $cmd_name --tool=<tool>' may be set to one of the following:" "No suitable tool for 'git $cmd_name --tool=<tool>' found." &&
echo "$available" | sort | sed -e 's/^/ /' any_shown=yes
else
echo "No suitable tool for 'git $cmd_name --tool=<tool>' found." show_tool_names 'mode_ok && ! is_available' "$tab$tab" \
fi "${LF}The following tools are valid, but not currently available:" &&
if test -n "$unavailable" any_shown=yes
then
echo if test "$any_shown" = yes
echo 'The following tools are valid, but not currently available:'
echo "$unavailable" | sort | sed -e 's/^/ /'
fi
if test -n "$unavailable$available"
then then
echo echo
echo "Some of the tools listed above only work in a windowed" echo "Some of the tools listed above only work in a windowed"
@ -249,17 +283,12 @@ guess_merge_tool () {
EOF EOF
# Loop over each candidate and stop when a valid merge tool is found. # Loop over each candidate and stop when a valid merge tool is found.
for i in $tools for tool in $tools
do do
merge_tool_path=$(translate_merge_tool_path "$i") is_available "$tool" && echo "$tool" && return 0
if type "$merge_tool_path" >/dev/null 2>&1
then
echo "$i"
return 0
fi
done done
echo >&2 "No known merge resolution program available." echo >&2 "No known ${TOOL_MODE} tool is available."
return 1 return 1
} }