mergetool: move main program flow into a main() function

Make it easier to follow the program's flow by isolating all
logic into functions.  Isolate the main execution code path into
a single unit instead of having prompt_after_failed_merge()
interrupt it partyway through.

The use of a main() function is borrowing a convention from C,
Python, Perl, and many other languages.  This helps readers more
familiar with other languages understand the purpose of each
function when diving into the codebase with fresh eyes.

Signed-off-by: David Aguilar <davvid@gmail.com>
Reviewed-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
David Aguilar 2016-10-07 16:58:04 -07:00 committed by Junio C Hamano
parent 8827b3a887
commit 08221e3fa2
1 changed files with 92 additions and 86 deletions

View File

@ -366,51 +366,6 @@ merge_file () {
return 0 return 0
} }


prompt=$(git config --bool mergetool.prompt)
guessed_merge_tool=false

while test $# != 0
do
case "$1" in
--tool-help=*)
TOOL_MODE=${1#--tool-help=}
show_tool_help
;;
--tool-help)
show_tool_help
;;
-t|--tool*)
case "$#,$1" in
*,*=*)
merge_tool=$(expr "z$1" : 'z-[^=]*=\(.*\)')
;;
1,*)
usage ;;
*)
merge_tool="$2"
shift ;;
esac
;;
-y|--no-prompt)
prompt=false
;;
--prompt)
prompt=true
;;
--)
shift
break
;;
-*)
usage
;;
*)
break
;;
esac
shift
done

prompt_after_failed_merge () { prompt_after_failed_merge () {
while true while true
do do
@ -427,57 +382,108 @@ prompt_after_failed_merge () {
done done
} }


git_dir_init main () {
require_work_tree prompt=$(git config --bool mergetool.prompt)
guessed_merge_tool=false

while test $# != 0
do
case "$1" in
--tool-help=*)
TOOL_MODE=${1#--tool-help=}
show_tool_help
;;
--tool-help)
show_tool_help
;;
-t|--tool*)
case "$#,$1" in
*,*=*)
merge_tool=$(expr "z$1" : 'z-[^=]*=\(.*\)')
;;
1,*)
usage ;;
*)
merge_tool="$2"
shift ;;
esac
;;
-y|--no-prompt)
prompt=false
;;
--prompt)
prompt=true
;;
--)
shift
break
;;
-*)
usage
;;
*)
break
;;
esac
shift
done

git_dir_init
require_work_tree


if test -z "$merge_tool"
then
# Check if a merge tool has been configured
merge_tool=$(get_configured_merge_tool)
# Try to guess an appropriate merge tool if no tool has been set.
if test -z "$merge_tool" if test -z "$merge_tool"
then then
merge_tool=$(guess_merge_tool) || exit # Check if a merge tool has been configured
guessed_merge_tool=true merge_tool=$(get_configured_merge_tool)
# Try to guess an appropriate merge tool if no tool has been set.
if test -z "$merge_tool"
then
merge_tool=$(guess_merge_tool) || exit
guessed_merge_tool=true
fi
fi fi
fi merge_keep_backup="$(git config --bool mergetool.keepBackup || echo true)"
merge_keep_backup="$(git config --bool mergetool.keepBackup || echo true)" merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"
merge_keep_temporaries="$(git config --bool mergetool.keepTemporaries || echo false)"


files= files=


if test $# -eq 0 if test $# -eq 0
then
cd_to_toplevel

if test -e "$GIT_DIR/MERGE_RR"
then then
files=$(git rerere remaining) cd_to_toplevel

if test -e "$GIT_DIR/MERGE_RR"
then
files=$(git rerere remaining)
else
files=$(git ls-files -u |
sed -e 's/^[^ ]* //' | sort -u)
fi
else else
files=$(git ls-files -u | sed -e 's/^[^ ]* //' | sort -u) files=$(git ls-files -u -- "$@" |
sed -e 's/^[^ ]* //' | sort -u)
fi fi
else
files=$(git ls-files -u -- "$@" | sed -e 's/^[^ ]* //' | sort -u)
fi


if test -z "$files" if test -z "$files"
then
echo "No files need merging"
exit 0
fi

printf "Merging:\n"
printf "%s\n" "$files"

rc=0
for i in $files
do
printf "\n"
if ! merge_file "$i"
then then
rc=1 echo "No files need merging"
prompt_after_failed_merge || exit 1 exit 0
fi fi
done


exit $rc printf "Merging:\n"
printf "%s\n" "$files"

rc=0
for i in $files
do
printf "\n"
if ! merge_file "$i"
then
rc=1
prompt_after_failed_merge || exit 1
fi
done

exit $rc
}

main "$@"