{"thread":{"id":"18727","subject":"[PATCH] mergetool--lib: consolidate the last redundant bits in {diff,merge}tool","startedAt":"2009-04-05T03:45:18Z","lastAt":"2009-04-05T03:45:18Z","messageCount":1,"participants":["David Aguilar"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"110378","messageId":"1238903118-7583-1-git-send-email-davvid@gmail.com","threadId":"18727","inReplyTo":null,"subject":"[PATCH] mergetool--lib: consolidate the last redundant bits in {diff,merge}tool","fromName":"David Aguilar","fromEmail":"davvid@gmail.com","sentAt":"2009-04-05T03:45:18Z","receivedAt":"2009-04-05T03:45:18Z","isPatch":true,"sender":{"key":"davvid@gmail.com","avatar":"https://avatars.githubusercontent.com/u/13196?v=4"},"body":"This adds get_merge_tool, get_merge_tool_path, get_merge_tool_cmd\nand uses them in git-mergetool and git-difftool--helper.\n\nSigned-off-by: David Aguilar <davvid@gmail.com>\n---\n Documentation/git-mergetool--lib.txt |   22 ++++---\n git-difftool--helper.sh              |   80 ++------------------------\n git-mergetool--lib.sh                |  106 +++++++++++++++++++++++++++++++--\n git-mergetool.sh                     |   65 ++------------------\n t/t7800-difftool.sh                  |    8 +++\n 5 files changed, 132 insertions(+), 149 deletions(-)\n\ndiff --git a/Documentation/git-mergetool--lib.txt b/Documentation/git-mergetool--lib.txt\nindex ffda66b..3d57031 100644\n--- a/Documentation/git-mergetool--lib.txt\n+++ b/Documentation/git-mergetool--lib.txt\n@@ -26,18 +26,22 @@ functions.  'diff' and 'merge' are valid values.\n \n FUNCTIONS\n ---------\n-get_merge_tool_path::\n-\treturns the `merge_tool_path` for a `merge_tool`.\n+get_merge_tool::\n+\treturns a merge tool\n \n-run_mergetool::\n-\tlaunches a merge tool given the tool name and a true/false\n-\tflag to indicate whether a merge base is present\n+get_merge_tool_cmd::\n+\treturns the custom command for a merge tool.\n \n-valid_tool::\n-\ttests whether a merge tool is setup correctly.\n+get_merge_tool_path::\n+\treturns the custom path for a merge tool.\n \n-get_custom_cmd::\n-\tgiven a merge tool, returns the custom command for that tool.\n+run_merge_tool::\n+\tlaunches a merge tool given the tool name and a true/false\n+\tflag to indicate whether a merge base is present.\n+\t'$merge_tool', '$merge_tool_path', and for custom commands,\n+\t'$merge_tool_cmd', must be defined prior to calling\n+\trun_merge_tool.  Additionally, '$MERGED', '$LOCAL', '$REMOTE',\n+\tand '$BASE' must be defined for use by the merge tool.\n \n Author\n ------\ndiff --git a/git-difftool--helper.sh b/git-difftool--helper.sh\nindex 602cd4f..b450036 100755\n--- a/git-difftool--helper.sh\n+++ b/git-difftool--helper.sh\n@@ -20,8 +20,7 @@ should_prompt () {\n \tfi\n }\n \n-# This function prepares temporary files and launches the appropriate\n-# merge tool.\n+# Sets up shell variables and runs a merge tool\n launch_merge_tool () {\n \t# Merged is the filename as it appears in the work tree\n \t# Local is the contents of a/filename\n@@ -41,85 +40,16 @@ launch_merge_tool () {\n \tfi\n \n \t# Run the appropriate merge tool command\n-\trun_mergetool \"$merge_tool\";\n+\trun_merge_tool \"$merge_tool\"\n }\n \n # Allow GIT_DIFF_TOOL and GIT_MERGE_TOOL to provide default values\n test -n \"$GIT_MERGE_TOOL\" && merge_tool=\"$GIT_MERGE_TOOL\"\n test -n \"$GIT_DIFF_TOOL\" && merge_tool=\"$GIT_DIFF_TOOL\"\n \n-# If merge tool was not specified then use the diff.tool\n-# configuration variable.  If that's invalid then reset merge_tool.\n-# Fallback to merge.tool.\n-if test -z \"$merge_tool\"; then\n-\tmerge_tool=$(git config diff.tool)\n-\ttest -z \"$merge_tool\" &&\n-\tmerge_tool=$(git config merge.tool)\n-\tif test -n \"$merge_tool\" && ! valid_tool \"$merge_tool\"; then\n-\t\techo >&2 \"git config option diff.tool set to unknown tool: $merge_tool\"\n-\t\techo >&2 \"Resetting to default...\"\n-\t\tunset merge_tool\n-\tfi\n-fi\n-\n-# Try to guess an appropriate merge tool if no tool has been set.\n-if test -z \"$merge_tool\"; then\n-\t# We have a $DISPLAY so try some common UNIX merge tools\n-\tif test -n \"$DISPLAY\"; then\n-\t\t# If gnome then prefer meld, otherwise, prefer kdiff3 or kompare\n-\t\tif test -n \"$GNOME_DESKTOP_SESSION_ID\" ; then\n-\t\t\tmerge_tool_candidates=\"meld kdiff3 kompare tkdiff xxdiff gvimdiff diffuse\"\n-\t\telse\n-\t\t\tmerge_tool_candidates=\"kdiff3 kompare tkdiff xxdiff meld gvimdiff diffuse\"\n-\t\tfi\n-\tfi\n-\tif echo \"${VISUAL:-$EDITOR}\" | grep 'emacs' > /dev/null 2>&1; then\n-\t\t# $EDITOR is emacs so add emerge as a candidate\n-\t\tmerge_tool_candidates=\"$merge_tool_candidates emerge opendiff vimdiff\"\n-\telif echo \"${VISUAL:-$EDITOR}\" | grep 'vim' > /dev/null 2>&1; then\n-\t\t# $EDITOR is vim so add vimdiff as a candidate\n-\t\tmerge_tool_candidates=\"$merge_tool_candidates vimdiff opendiff emerge\"\n-\telse\n-\t\tmerge_tool_candidates=\"$merge_tool_candidates opendiff emerge vimdiff\"\n-\tfi\n-\techo \"merge tool candidates: $merge_tool_candidates\"\n-\n-\t# Loop over each candidate and stop when a valid merge tool is found.\n-\tfor i in $merge_tool_candidates\n-\tdo\n-\t\tmerge_tool_path=\"$(get_merge_tool_path \"$i\")\"\n-\t\tif type \"$merge_tool_path\" > /dev/null 2>&1; then\n-\t\t\tmerge_tool=$i\n-\t\t\tbreak\n-\t\tfi\n-\tdone\n-\n-\tif test -z \"$merge_tool\" ; then\n-\t\techo \"No known merge resolution program available.\"\n-\t\texit 1\n-\tfi\n-\n-\tmerge_tool_cmd=$(get_custom_cmd \"$merge_tool\")\n-else\n-\t# A merge tool has been set, so verify that it's valid.\n-\tif ! valid_tool \"$merge_tool\"; then\n-\t\techo >&2 \"Unknown merge tool $merge_tool\"\n-\t\texit 1\n-\tfi\n-\n-\tmerge_tool_cmd=$(get_custom_cmd \"$merge_tool\")\n-\tmerge_tool_path=$(git config difftool.\"$1\".path)\n-\ttest -z \"$merge_tool_path\" &&\n-\tmerge_tool_path=$(git config mergetool.\"$1\".path)\n-\tmerge_tool_path=\"$(get_merge_tool_path \"$merge_tool\" \\\n-\t                                       \"$merge_tool_path\")\"\n-\n-\tif test -z \"$merge_tool_cmd\" && ! type \"$merge_tool_path\" > /dev/null 2>&1; then\n-\t\techo \"The merge tool $merge_tool is not available as '$merge_tool_path'\"\n-\t\texit 1\n-\tfi\n-fi\n-\n+merge_tool=$(get_merge_tool \"$merge_tool\") || exit\n+merge_tool_cmd=\"$(get_merge_tool_cmd \"$merge_tool\")\"\n+merge_tool_path=\"$(get_merge_tool_path \"$merge_tool\")\" || exit\n \n # Launch the merge tool on each path provided by 'git diff'\n while test $# -gt 6\ndiff --git a/git-mergetool--lib.sh b/git-mergetool--lib.sh\nindex b8566b2..dcd4516 100644\n--- a/git-mergetool--lib.sh\n+++ b/git-mergetool--lib.sh\n@@ -7,9 +7,10 @@ merge_mode() {\n \ttest $TOOL_MODE = \"merge\"\n }\n \n-get_merge_tool_path () {\n-\tpath=\"$1\"\n-\tif test -z \"$2\"; then\n+translate_merge_tool_path () {\n+\tif test -n \"$2\"; then\n+\t\techo \"$2\"\n+\telse\n \t\tcase \"$1\" in\n \t\temerge)\n \t\t\tpath=emacs\n@@ -18,8 +19,8 @@ get_merge_tool_path () {\n \t\t\tpath=\"$1\"\n \t\t\t;;\n \t\tesac\n+\t\techo \"$path\"\n \tfi\n-\techo \"$path\"\n }\n \n check_unchanged () {\n@@ -51,14 +52,14 @@ valid_tool () {\n \t\tfi\n \t\t;; # happy\n \t*)\n-\t\tif test -z \"$(get_custom_cmd \"$1\")\"; then\n+\t\tif test -z \"$(get_merge_tool_cmd \"$1\")\"; then\n \t\t\treturn 1\n \t\tfi\n \t\t;;\n \tesac\n }\n \n-get_custom_cmd () {\n+get_merge_tool_cmd () {\n \tdiff_mode &&\n \tcustom_cmd=\"$(git config difftool.$1.cmd)\"\n \ttest -z \"$custom_cmd\" &&\n@@ -67,7 +68,7 @@ get_custom_cmd () {\n \techo \"$custom_cmd\"\n }\n \n-run_mergetool () {\n+run_merge_tool () {\n \tbase_present=\"$2\"\n \tif diff_mode; then\n \t\tbase_present=\"false\"\n@@ -235,3 +236,94 @@ run_mergetool () {\n \tesac\n \treturn $status\n }\n+\n+guess_merge_tool () {\n+\tif diff_mode; then\n+\t\tkompare=\"kompare\"\n+\tfi\n+\tif test -n \"$DISPLAY\"; then\n+\t\tif test -n \"$GNOME_DESKTOP_SESSION_ID\" ; then\n+\t\t\ttools=\"meld kdiff3 $kompare tkdiff\"\n+\t\t\ttools=\"$tools xxdiff gvimdiff diffuse\"\n+\t\telse\n+\t\t\ttools=\"kdiff3 $kompare tkdiff xxdiff\"\n+\t\t\ttools=\"$tools meld gvimdiff diffuse\"\n+\t\tfi\n+\tfi\n+\tif echo \"${VISUAL:-$EDITOR}\" | grep emacs > /dev/null 2>&1; then\n+\t\t# $EDITOR is emacs so add emerge as a candidate\n+\t\ttools=\"$tools emerge opendiff vimdiff\"\n+\telif echo \"${VISUAL:-$EDITOR}\" | grep vim > /dev/null 2>&1; then\n+\t\t# $EDITOR is vim so add vimdiff as a candidate\n+\t\ttools=\"$tools vimdiff opendiff emerge\"\n+\telse\n+\t\ttools=\"$tools opendiff emerge vimdiff\"\n+\tfi\n+\techo >&2 \"merge tool candidates: $tools\"\n+\n+\t# Loop over each candidate and stop when a valid merge tool is found.\n+\tfor i in $tools\n+\tdo\n+\t\tmerge_tool_path=\"$(translate_merge_tool_path \"$i\")\"\n+\t\tif type \"$merge_tool_path\" > /dev/null 2>&1; then\n+\t\t\tmerge_tool=\"$i\"\n+\t\t\tbreak\n+\t\tfi\n+\tdone\n+\n+\tif test -z \"$merge_tool\" ; then\n+\t\techo >&2 \"No known merge resolution program available.\"\n+\t\treturn 1\n+\tfi\n+\techo \"$merge_tool\"\n+}\n+\n+get_configured_merge_tool () {\n+\t# Diff mode first tries diff.tool and falls back to merge.tool.\n+\t# Merge mode only checks merge.tool\n+\tif diff_mode; then\n+\t\ttool=$(git config diff.tool)\n+\tfi\n+\tif test -z \"$tool\"; then\n+\t\ttool=$(git config merge.tool)\n+\tfi\n+\tif test -n \"$merge_tool\" && ! valid_tool \"$merge_tool\"; then\n+\t\techo >&2 \"git config option $TOOL_MODE.tool set to unknown tool: $merge_tool\"\n+\t\techo >&2 \"Resetting to default...\"\n+\t\treturn 1\n+\tfi\n+\techo \"$tool\"\n+}\n+\n+get_merge_tool_path () {\n+\t# A merge tool has been set, so verify that it's valid.\n+\tif ! valid_tool \"$merge_tool\"; then\n+\t\techo >&2 \"Unknown merge tool $merge_tool\"\n+\t\texit 1\n+\tfi\n+\tif diff_mode; then\n+\t\tmerge_tool_path=$(git config difftool.\"$merge_tool\".path)\n+\tfi\n+\tif test -z \"$merge_tool_path\"; then\n+\t\tmerge_tool_path=$(git config mergetool.\"$merge_tool\".path)\n+\tfi\n+\tmerge_tool_path=\"$(translate_merge_tool_path \"$merge_tool\" \"$merge_tool_path\")\"\n+\tif test -z \"$merge_tool_cmd\" && ! type \"$merge_tool_path\" > /dev/null 2>&1; then\n+\t\techo >&2 \"The $TOOL_MODE tool $merge_tool is not available as '$merge_tool_path'\"\n+\t\texit 1\n+\tfi\n+\techo \"$merge_tool_path\"\n+}\n+\n+get_merge_tool () {\n+\tmerge_tool=\"$1\"\n+\t# Check if a merge tool has been configured\n+\tif test -z \"$merge_tool\"; then\n+\t\tmerge_tool=$(get_configured_merge_tool)\n+\tfi\n+\t# Try to guess an appropriate merge tool if no tool has been set.\n+\tif test -z \"$merge_tool\"; then\n+\t\tmerge_tool=$(guess_merge_tool) || exit\n+\tfi\n+\techo \"$merge_tool\"\n+}\ndiff --git a/git-mergetool.sh b/git-mergetool.sh\nindex 298b0a1..efa31a2 100755\n--- a/git-mergetool.sh\n+++ b/git-mergetool.sh\n@@ -178,7 +178,7 @@ merge_file () {\n     base_present &&\n     present=true\n \n-    if ! run_mergetool \"$merge_tool\" \"$present\"; then\n+    if ! run_merge_tool \"$merge_tool\" \"$present\"; then\n \techo \"merge of $MERGED failed\" 1>&2\n \tmv -- \"$BACKUP\" \"$MERGED\"\n \n@@ -254,63 +254,12 @@ prompt_after_failed_merge() {\n     done\n }\n \n-if test -z \"$merge_tool\"; then\n-    merge_tool=$(git config merge.tool)\n-    if test -n \"$merge_tool\" && ! valid_tool \"$merge_tool\"; then\n-\t    echo >&2 \"git config option merge.tool set to unknown tool: $merge_tool\"\n-\t    echo >&2 \"Resetting to default...\"\n-\t    unset merge_tool\n-    fi\n-fi\n-\n-if test -z \"$merge_tool\" ; then\n-    if test -n \"$DISPLAY\"; then\n-        if test -n \"$GNOME_DESKTOP_SESSION_ID\" ; then\n-            merge_tool_candidates=\"meld kdiff3 tkdiff xxdiff gvimdiff diffuse\"\n-        else\n-            merge_tool_candidates=\"kdiff3 tkdiff xxdiff meld gvimdiff diffuse\"\n-        fi\n-    fi\n-    if echo \"${VISUAL:-$EDITOR}\" | grep 'emacs' > /dev/null 2>&1; then\n-        merge_tool_candidates=\"$merge_tool_candidates emerge opendiff vimdiff\"\n-    elif echo \"${VISUAL:-$EDITOR}\" | grep 'vim' > /dev/null 2>&1; then\n-        merge_tool_candidates=\"$merge_tool_candidates vimdiff opendiff emerge\"\n-    else\n-        merge_tool_candidates=\"$merge_tool_candidates opendiff emerge vimdiff\"\n-    fi\n-    echo \"merge tool candidates: $merge_tool_candidates\"\n-    for i in $merge_tool_candidates; do\n-        merge_tool_path=\"$(get_merge_tool_path \"$i\" \"$merge_tool_path\")\"\n-        if type \"$merge_tool_path\" > /dev/null 2>&1; then\n-            merge_tool=$i\n-            break\n-        fi\n-    done\n-    if test -z \"$merge_tool\" ; then\n-\techo \"No known merge resolution program available.\"\n-\texit 1\n-    fi\n-    merge_tool_cmd=\"$(git config mergetool.\"$merge_tool\".cmd)\"\n-else\n-    if ! valid_tool \"$merge_tool\"; then\n-        echo >&2 \"Unknown merge_tool $merge_tool\"\n-        exit 1\n-    fi\n-\n-    merge_tool_cmd=\"$(git config mergetool.$merge_tool.cmd)\"\n-    merge_tool_path=\"$(get_merge_tool_path \"$merge_tool\")\"\n-    merge_keep_backup=\"$(git config --bool merge.keepBackup || echo true)\"\n-    merge_keep_temporaries=\"$(git config --bool mergetool.keepTemporaries || echo false)\"\n-\n-    if test -z \"$merge_tool_cmd\" && ! type \"$merge_tool_path\" > /dev/null 2>&1; then\n-        echo \"The merge tool $merge_tool is not available as '$merge_tool_path'\"\n-        exit 1\n-    fi\n-\n-    if ! test -z \"$merge_tool_cmd\"; then\n-        merge_tool_trust_exit_code=\"$(git config --bool mergetool.$merge_tool.trustExitCode || echo false)\"\n-    fi\n-fi\n+merge_tool=$(get_merge_tool \"$merge_tool\") || exit\n+merge_tool_cmd=\"$(get_merge_tool_cmd \"$merge_tool\")\"\n+merge_tool_path=\"$(get_merge_tool_path \"$merge_tool\")\" || exit\n+merge_keep_backup=\"$(git config --bool merge.keepBackup || echo true)\"\n+merge_keep_temporaries=\"$(git config --bool mergetool.keepTemporaries || echo false)\"\n+merge_tool_trust_exit_code=\"$(git config --bool mergetool.\"$merge_tool\".trustExitCode || echo false)\"\n \n last_status=0\n rollup_status=0\ndiff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh\nindex 33d07e6..cbfbe87 100755\n--- a/t/t7800-difftool.sh\n+++ b/t/t7800-difftool.sh\n@@ -200,4 +200,12 @@ test_expect_success 'difftool + mergetool config variables' '\n \trestore_test_defaults\n '\n \n+test_expect_success 'difftool.<tool>.path' '\n+\tgit config difftool.tkdiff.path echo &&\n+\tdiff=$(git difftool -y -t tkdiff branch) &&\n+\tgit config --unset difftool.tkdiff.path &&\n+\tlines=$(echo \"$diff\" | grep file | wc -l) &&\n+\ttest \"$lines\" = 1\n+'\n+\n test_done\n-- \n1.6.2.1.469.gdffc1\n"}]}