{"thread":{"id":"44243","subject":"[PATCH v2 2/4] mergetool: move main program flow into a main() function","startedAt":"2016-10-07T06:24:53Z","lastAt":"2016-10-07T06:24:53Z","messageCount":1,"participants":["David Aguilar"],"isPatch":true,"patchVersion":2,"patchTotal":4},"messages":[{"id":"303550","messageId":"20161007062415.21551-1-davvid@gmail.com","threadId":"44243","inReplyTo":null,"subject":"[PATCH v2 2/4] mergetool: move main program flow into a main() function","fromName":"David Aguilar","fromEmail":"davvid@gmail.com","sentAt":"2016-10-07T06:24:15Z","receivedAt":"2016-10-07T06:24:53Z","isPatch":true,"sender":{"key":"davvid@gmail.com","avatar":"https://avatars.githubusercontent.com/u/13196?v=4"},"body":"Make it easier to follow the program's flow by isolating all\nlogic into functions.  Isolate the main execution code path into\na single unit instead of having prompt_after_failed_merge()\ninterrupt it partyway through.\n\nThe use of a main() function is borrowing a convention from C,\nPython, Perl, and many other languages.  This helps readers more\nfamiliar with other languages understand the purpose of each\nfunction when diving into the codebase with fresh eyes.\n\nSigned-off-by: David Aguilar <davvid@gmail.com>\n---\nAs suggested by Hannes, v2 provides a better commit message.\n\nThis is a stylistic choice, but since I was about to start hacking on it,\nI couldn't help but notice the organic sprawl that could use some tidying\nbefore adding new features.\n\n git-mergetool.sh | 180 ++++++++++++++++++++++++++++---------------------------\n 1 file changed, 93 insertions(+), 87 deletions(-)\n\ndiff --git a/git-mergetool.sh b/git-mergetool.sh\nindex 300ce7f..b2cd0a4 100755\n--- a/git-mergetool.sh\n+++ b/git-mergetool.sh\n@@ -366,51 +366,6 @@ merge_file () {\n \treturn 0\n }\n \n-prompt=$(git config --bool mergetool.prompt)\n-guessed_merge_tool=false\n-\n-while test $# != 0\n-do\n-\tcase \"$1\" in\n-\t--tool-help=*)\n-\t\tTOOL_MODE=${1#--tool-help=}\n-\t\tshow_tool_help\n-\t\t;;\n-\t--tool-help)\n-\t\tshow_tool_help\n-\t\t;;\n-\t-t|--tool*)\n-\t\tcase \"$#,$1\" in\n-\t\t*,*=*)\n-\t\t\tmerge_tool=$(expr \"z$1\" : 'z-[^=]*=\\(.*\\)')\n-\t\t\t;;\n-\t\t1,*)\n-\t\t\tusage ;;\n-\t\t*)\n-\t\t\tmerge_tool=\"$2\"\n-\t\t\tshift ;;\n-\t\tesac\n-\t\t;;\n-\t-y|--no-prompt)\n-\t\tprompt=false\n-\t\t;;\n-\t--prompt)\n-\t\tprompt=true\n-\t\t;;\n-\t--)\n-\t\tshift\n-\t\tbreak\n-\t\t;;\n-\t-*)\n-\t\tusage\n-\t\t;;\n-\t*)\n-\t\tbreak\n-\t\t;;\n-\tesac\n-\tshift\n-done\n-\n prompt_after_failed_merge () {\n \twhile true\n \tdo\n@@ -427,57 +382,108 @@ prompt_after_failed_merge () {\n \tdone\n }\n \n-git_dir_init\n-require_work_tree\n+main () {\n+\tprompt=$(git config --bool mergetool.prompt)\n+\tguessed_merge_tool=false\n+\n+\twhile test $# != 0\n+\tdo\n+\t\tcase \"$1\" in\n+\t\t--tool-help=*)\n+\t\t\tTOOL_MODE=${1#--tool-help=}\n+\t\t\tshow_tool_help\n+\t\t\t;;\n+\t\t--tool-help)\n+\t\t\tshow_tool_help\n+\t\t\t;;\n+\t\t-t|--tool*)\n+\t\t\tcase \"$#,$1\" in\n+\t\t\t*,*=*)\n+\t\t\t\tmerge_tool=$(expr \"z$1\" : 'z-[^=]*=\\(.*\\)')\n+\t\t\t\t;;\n+\t\t\t1,*)\n+\t\t\t\tusage ;;\n+\t\t\t*)\n+\t\t\t\tmerge_tool=\"$2\"\n+\t\t\t\tshift ;;\n+\t\t\tesac\n+\t\t\t;;\n+\t\t-y|--no-prompt)\n+\t\t\tprompt=false\n+\t\t\t;;\n+\t\t--prompt)\n+\t\t\tprompt=true\n+\t\t\t;;\n+\t\t--)\n+\t\t\tshift\n+\t\t\tbreak\n+\t\t\t;;\n+\t\t-*)\n+\t\t\tusage\n+\t\t\t;;\n+\t\t*)\n+\t\t\tbreak\n+\t\t\t;;\n+\t\tesac\n+\t\tshift\n+\tdone\n+\n+\tgit_dir_init\n+\trequire_work_tree\n \n-if test -z \"$merge_tool\"\n-then\n-\t# Check if a merge tool has been configured\n-\tmerge_tool=$(get_configured_merge_tool)\n-\t# Try to guess an appropriate merge tool if no tool has been set.\n \tif test -z \"$merge_tool\"\n \tthen\n-\t\tmerge_tool=$(guess_merge_tool) || exit\n-\t\tguessed_merge_tool=true\n+\t\t# Check if a merge tool has been configured\n+\t\tmerge_tool=$(get_configured_merge_tool)\n+\t\t# Try to guess an appropriate merge tool if no tool has been set.\n+\t\tif test -z \"$merge_tool\"\n+\t\tthen\n+\t\t\tmerge_tool=$(guess_merge_tool) || exit\n+\t\t\tguessed_merge_tool=true\n+\t\tfi\n \tfi\n-fi\n-merge_keep_backup=\"$(git config --bool mergetool.keepBackup || echo true)\"\n-merge_keep_temporaries=\"$(git config --bool mergetool.keepTemporaries || echo false)\"\n-\n-files=\n+\tmerge_keep_backup=\"$(git config --bool mergetool.keepBackup || echo true)\"\n+\tmerge_keep_temporaries=\"$(git config --bool mergetool.keepTemporaries || echo false)\"\n \n-if test $# -eq 0\n-then\n-\tcd_to_toplevel\n+\tfiles=\n \n-\tif test -e \"$GIT_DIR/MERGE_RR\"\n+\tif test $# -eq 0\n \tthen\n-\t\tfiles=$(git rerere remaining)\n+\t\tcd_to_toplevel\n+\n+\t\tif test -e \"$GIT_DIR/MERGE_RR\"\n+\t\tthen\n+\t\t\tfiles=$(git rerere remaining)\n+\t\telse\n+\t\t\tfiles=$(git ls-files -u |\n+\t\t\t\tsed -e 's/^[^\t]*\t//' | sort -u)\n+\t\tfi\n \telse\n-\t\tfiles=$(git ls-files -u | sed -e 's/^[^\t]*\t//' | sort -u)\n+\t\tfiles=$(git ls-files -u -- \"$@\" |\n+\t\t\tsed -e 's/^[^\t]*\t//' | sort -u)\n \tfi\n-else\n-\tfiles=$(git ls-files -u -- \"$@\" | sed -e 's/^[^\t]*\t//' | sort -u)\n-fi\n-\n-if test -z \"$files\"\n-then\n-\techo \"No files need merging\"\n-\texit 0\n-fi\n-\n-printf \"Merging:\\n\"\n-printf \"%s\\n\" \"$files\"\n-\n-rc=0\n-for i in $files\n-do\n-\tprintf \"\\n\"\n-\tif ! merge_file \"$i\"\n+\n+\tif test -z \"$files\"\n \tthen\n-\t\trc=1\n-\t\tprompt_after_failed_merge || exit 1\n+\t\techo \"No files need merging\"\n+\t\texit 0\n \tfi\n-done\n \n-exit $rc\n+\tprintf \"Merging:\\n\"\n+\tprintf \"%s\\n\" \"$files\"\n+\n+\trc=0\n+\tfor i in $files\n+\tdo\n+\t\tprintf \"\\n\"\n+\t\tif ! merge_file \"$i\"\n+\t\tthen\n+\t\t\trc=1\n+\t\t\tprompt_after_failed_merge || exit 1\n+\t\tfi\n+\tdone\n+\n+\texit $rc\n+}\n+\n+main \"$@\"\n-- \n2.10.1.386.g42aabb0\n\n"}]}