]> git.ipfire.org Git - thirdparty/git.git/blobdiff - git-mergetool--lib.sh
Merge branch 'en/ort-perf-batch-9'
[thirdparty/git.git] / git-mergetool--lib.sh
index 83bf52494cd26437eaa3c755c3359951937f1ab2..542a6a75eb3c4b93d8b4a394551d5d38d2a88f6e 100644 (file)
@@ -43,7 +43,16 @@ show_tool_names () {
 
        shown_any=
        ( cd "$MERGE_TOOLS_DIR" && ls ) | {
-               while read toolname
+               while read scriptname
+               do
+                       setup_tool "$scriptname" 2>/dev/null
+                       # We need an actual line feed here
+                       variants="$variants
+$(list_tool_variants)"
+               done
+               variants="$(echo "$variants" | sort -u)"
+
+               for toolname in $variants
                do
                        if setup_tool "$toolname" 2>/dev/null &&
                                (eval "$condition" "$toolname")
@@ -80,14 +89,18 @@ show_tool_names () {
        }
 }
 
-diff_mode() {
+diff_mode () {
        test "$TOOL_MODE" = diff
 }
 
-merge_mode() {
+merge_mode () {
        test "$TOOL_MODE" = merge
 }
 
+gui_mode () {
+       test "$GIT_MERGETOOL_GUI" = true
+}
+
 translate_merge_tool_path () {
        echo "$1"
 }
@@ -127,6 +140,10 @@ setup_user_tool () {
        merge_cmd () {
                ( eval $merge_tool_cmd )
        }
+
+       list_tool_variants () {
+               echo "$tool"
+       }
 }
 
 setup_tool () {
@@ -149,10 +166,18 @@ setup_tool () {
                return 1
        }
 
+       hide_resolved_enabled () {
+               return 0
+       }
+
        translate_merge_tool_path () {
                echo "$1"
        }
 
+       list_tool_variants () {
+               echo "$tool"
+       }
+
        # Most tools' exit codes cannot be trusted, so By default we ignore
        # their exit code and check the merged file's modification time in
        # check_unchanged() to determine whether or not the merge was
@@ -174,19 +199,26 @@ setup_tool () {
                false
        }
 
-
-       if ! test -f "$MERGE_TOOLS_DIR/$tool"
+       if test -f "$MERGE_TOOLS_DIR/$tool"
        then
+               . "$MERGE_TOOLS_DIR/$tool"
+       elif test -f "$MERGE_TOOLS_DIR/${tool%[0-9]}"
+       then
+               . "$MERGE_TOOLS_DIR/${tool%[0-9]}"
+       else
                setup_user_tool
                return $?
        fi
 
-       # Load the redefined functions
-       . "$MERGE_TOOLS_DIR/$tool"
        # Now let the user override the default command for the tool.  If
        # they have not done so then this will return 1 which we ignore.
        setup_user_tool
 
+       if ! list_tool_variants | grep -q "^$tool$"
+       then
+               return 1
+       fi
+
        if merge_mode && ! can_merge
        then
                echo "error: '$tool' can not be used to resolve merges" >&2
@@ -222,6 +254,10 @@ trust_exit_code () {
        fi
 }
 
+initialize_merge_tool () {
+       # Bring tool-specific functions into scope
+       setup_tool "$1" || return 1
+}
 
 # Entry point for running tools
 run_merge_tool () {
@@ -233,9 +269,6 @@ run_merge_tool () {
        merge_tool_path=$(get_merge_tool_path "$1") || exit
        base_present="$2"
 
-       # Bring tool-specific functions into scope
-       setup_tool "$1" || return 1
-
        if merge_mode
        then
                run_merge_cmd "$1"
@@ -279,13 +312,17 @@ list_merge_tool_candidates () {
                fi
                tools="$tools gvimdiff diffuse diffmerge ecmerge"
                tools="$tools p4merge araxis bc codecompare"
+               tools="$tools smerge"
        fi
        case "${VISUAL:-$EDITOR}" in
+       *nvim*)
+               tools="$tools nvimdiff vimdiff emerge"
+               ;;
        *vim*)
-               tools="$tools vimdiff emerge"
+               tools="$tools vimdiff nvimdiff emerge"
                ;;
        *)
-               tools="$tools emerge vimdiff"
+               tools="$tools emerge vimdiff nvimdiff"
                ;;
        esac
 }
@@ -350,20 +387,36 @@ guess_merge_tool () {
 }
 
 get_configured_merge_tool () {
-       # If first argument is true, find the guitool instead
-       if test "$1" = true
-       then
-               gui_prefix=gui
-       fi
-
-       # Diff mode first tries diff.(gui)tool and falls back to merge.(gui)tool.
-       # Merge mode only checks merge.(gui)tool
+       keys=
        if diff_mode
        then
-               merge_tool=$(git config diff.${gui_prefix}tool || git config merge.${gui_prefix}tool)
+               if gui_mode
+               then
+                       keys="diff.guitool merge.guitool diff.tool merge.tool"
+               else
+                       keys="diff.tool merge.tool"
+               fi
        else
-               merge_tool=$(git config merge.${gui_prefix}tool)
+               if gui_mode
+               then
+                       keys="merge.guitool merge.tool"
+               else
+                       keys="merge.tool"
+               fi
        fi
+
+       merge_tool=$(
+               IFS=' '
+               for key in $keys
+               do
+                       selected=$(git config $key)
+                       if test -n "$selected"
+                       then
+                               echo "$selected"
+                               return
+                       fi
+               done)
+
        if test -n "$merge_tool" && ! valid_tool "$merge_tool"
        then
                echo >&2 "git config option $TOOL_MODE.${gui_prefix}tool set to unknown tool: $merge_tool"
@@ -403,14 +456,17 @@ get_merge_tool_path () {
 }
 
 get_merge_tool () {
+       is_guessed=false
        # 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"
        then
                merge_tool=$(guess_merge_tool) || exit
+               is_guessed=true
        fi
        echo "$merge_tool"
+       test "$is_guessed" = false
 }
 
 mergetool_find_win32_cmd () {