git-mergetool--lib.shon commit regex: use regexec_buf() (b7d36ff)
   1# git-mergetool--lib is a shell library for common merge tool functions
   2
   3: ${MERGE_TOOLS_DIR=$(git --exec-path)/mergetools}
   4
   5IFS='
   6'
   7
   8mode_ok () {
   9        if diff_mode
  10        then
  11                can_diff
  12        elif merge_mode
  13        then
  14                can_merge
  15        else
  16                false
  17        fi
  18}
  19
  20is_available () {
  21        merge_tool_path=$(translate_merge_tool_path "$1") &&
  22        type "$merge_tool_path" >/dev/null 2>&1
  23}
  24
  25list_config_tools () {
  26        section=$1
  27        line_prefix=${2:-}
  28
  29        git config --get-regexp $section'\..*\.cmd' |
  30        while read -r key value
  31        do
  32                toolname=${key#$section.}
  33                toolname=${toolname%.cmd}
  34
  35                printf "%s%s\n" "$line_prefix" "$toolname"
  36        done
  37}
  38
  39show_tool_names () {
  40        condition=${1:-true} per_line_prefix=${2:-} preamble=${3:-}
  41        not_found_msg=${4:-}
  42        extra_content=${5:-}
  43
  44        shown_any=
  45        ( cd "$MERGE_TOOLS_DIR" && ls ) | {
  46                while read toolname
  47                do
  48                        if setup_tool "$toolname" 2>/dev/null &&
  49                                (eval "$condition" "$toolname")
  50                        then
  51                                if test -n "$preamble"
  52                                then
  53                                        printf "%s\n" "$preamble"
  54                                        preamble=
  55                                fi
  56                                shown_any=yes
  57                                printf "%s%s\n" "$per_line_prefix" "$toolname"
  58                        fi
  59                done
  60
  61                if test -n "$extra_content"
  62                then
  63                        if test -n "$preamble"
  64                        then
  65                                # Note: no '\n' here since we don't want a
  66                                # blank line if there is no initial content.
  67                                printf "%s" "$preamble"
  68                                preamble=
  69                        fi
  70                        shown_any=yes
  71                        printf "\n%s\n" "$extra_content"
  72                fi
  73
  74                if test -n "$preamble" && test -n "$not_found_msg"
  75                then
  76                        printf "%s\n" "$not_found_msg"
  77                fi
  78
  79                test -n "$shown_any"
  80        }
  81}
  82
  83diff_mode() {
  84        test "$TOOL_MODE" = diff
  85}
  86
  87merge_mode() {
  88        test "$TOOL_MODE" = merge
  89}
  90
  91translate_merge_tool_path () {
  92        echo "$1"
  93}
  94
  95check_unchanged () {
  96        if test "$MERGED" -nt "$BACKUP"
  97        then
  98                return 0
  99        else
 100                while true
 101                do
 102                        echo "$MERGED seems unchanged."
 103                        printf "Was the merge successful [y/n]? "
 104                        read answer || return 1
 105                        case "$answer" in
 106                        y*|Y*) return 0 ;;
 107                        n*|N*) return 1 ;;
 108                        esac
 109                done
 110        fi
 111}
 112
 113valid_tool () {
 114        setup_tool "$1" && return 0
 115        cmd=$(get_merge_tool_cmd "$1")
 116        test -n "$cmd"
 117}
 118
 119setup_user_tool () {
 120        merge_tool_cmd=$(get_merge_tool_cmd "$tool")
 121        test -n "$merge_tool_cmd" || return 1
 122
 123        diff_cmd () {
 124                ( eval $merge_tool_cmd )
 125        }
 126
 127        merge_cmd () {
 128                trust_exit_code=$(git config --bool \
 129                        "mergetool.$1.trustExitCode" || echo false)
 130                if test "$trust_exit_code" = "false"
 131                then
 132                        touch "$BACKUP"
 133                        ( eval $merge_tool_cmd )
 134                        check_unchanged
 135                else
 136                        ( eval $merge_tool_cmd )
 137                fi
 138        }
 139}
 140
 141setup_tool () {
 142        tool="$1"
 143
 144        # Fallback definitions, to be overridden by tools.
 145        can_merge () {
 146                return 0
 147        }
 148
 149        can_diff () {
 150                return 0
 151        }
 152
 153        diff_cmd () {
 154                return 1
 155        }
 156
 157        merge_cmd () {
 158                return 1
 159        }
 160
 161        translate_merge_tool_path () {
 162                echo "$1"
 163        }
 164
 165        if ! test -f "$MERGE_TOOLS_DIR/$tool"
 166        then
 167                setup_user_tool
 168                return $?
 169        fi
 170
 171        # Load the redefined functions
 172        . "$MERGE_TOOLS_DIR/$tool"
 173        # Now let the user override the default command for the tool.  If
 174        # they have not done so then this will return 1 which we ignore.
 175        setup_user_tool
 176
 177        if merge_mode && ! can_merge
 178        then
 179                echo "error: '$tool' can not be used to resolve merges" >&2
 180                return 1
 181        elif diff_mode && ! can_diff
 182        then
 183                echo "error: '$tool' can only be used to resolve merges" >&2
 184                return 1
 185        fi
 186        return 0
 187}
 188
 189get_merge_tool_cmd () {
 190        merge_tool="$1"
 191        if diff_mode
 192        then
 193                git config "difftool.$merge_tool.cmd" ||
 194                git config "mergetool.$merge_tool.cmd"
 195        else
 196                git config "mergetool.$merge_tool.cmd"
 197        fi
 198}
 199
 200# Entry point for running tools
 201run_merge_tool () {
 202        # If GIT_PREFIX is empty then we cannot use it in tools
 203        # that expect to be able to chdir() to its value.
 204        GIT_PREFIX=${GIT_PREFIX:-.}
 205        export GIT_PREFIX
 206
 207        merge_tool_path=$(get_merge_tool_path "$1") || exit
 208        base_present="$2"
 209
 210        # Bring tool-specific functions into scope
 211        setup_tool "$1" || return 1
 212
 213        if merge_mode
 214        then
 215                run_merge_cmd "$1"
 216        else
 217                run_diff_cmd "$1"
 218        fi
 219}
 220
 221# Run a either a configured or built-in diff tool
 222run_diff_cmd () {
 223        diff_cmd "$1"
 224}
 225
 226# Run a either a configured or built-in merge tool
 227run_merge_cmd () {
 228        merge_cmd "$1"
 229}
 230
 231list_merge_tool_candidates () {
 232        if merge_mode
 233        then
 234                tools="tortoisemerge"
 235        else
 236                tools="kompare"
 237        fi
 238        if test -n "$DISPLAY"
 239        then
 240                if test -n "$GNOME_DESKTOP_SESSION_ID"
 241                then
 242                        tools="meld opendiff kdiff3 tkdiff xxdiff $tools"
 243                else
 244                        tools="opendiff kdiff3 tkdiff xxdiff meld $tools"
 245                fi
 246                tools="$tools gvimdiff diffuse diffmerge ecmerge"
 247                tools="$tools p4merge araxis bc codecompare"
 248        fi
 249        case "${VISUAL:-$EDITOR}" in
 250        *vim*)
 251                tools="$tools vimdiff emerge"
 252                ;;
 253        *)
 254                tools="$tools emerge vimdiff"
 255                ;;
 256        esac
 257}
 258
 259show_tool_help () {
 260        tool_opt="'git ${TOOL_MODE}tool --tool=<tool>'"
 261
 262        tab='   '
 263        LF='
 264'
 265        any_shown=no
 266
 267        cmd_name=${TOOL_MODE}tool
 268        config_tools=$({
 269                diff_mode && list_config_tools difftool "$tab$tab"
 270                list_config_tools mergetool "$tab$tab"
 271        } | sort)
 272        extra_content=
 273        if test -n "$config_tools"
 274        then
 275                extra_content="${tab}user-defined:${LF}$config_tools"
 276        fi
 277
 278        show_tool_names 'mode_ok && is_available' "$tab$tab" \
 279                "$tool_opt may be set to one of the following:" \
 280                "No suitable tool for 'git $cmd_name --tool=<tool>' found." \
 281                "$extra_content" &&
 282                any_shown=yes
 283
 284        show_tool_names 'mode_ok && ! is_available' "$tab$tab" \
 285                "${LF}The following tools are valid, but not currently available:" &&
 286                any_shown=yes
 287
 288        if test "$any_shown" = yes
 289        then
 290                echo
 291                echo "Some of the tools listed above only work in a windowed"
 292                echo "environment. If run in a terminal-only session, they will fail."
 293        fi
 294        exit 0
 295}
 296
 297guess_merge_tool () {
 298        list_merge_tool_candidates
 299        cat >&2 <<-EOF
 300
 301        This message is displayed because '$TOOL_MODE.tool' is not configured.
 302        See 'git ${TOOL_MODE}tool --tool-help' or 'git help config' for more details.
 303        'git ${TOOL_MODE}tool' will now attempt to use one of the following tools:
 304        $tools
 305        EOF
 306
 307        # Loop over each candidate and stop when a valid merge tool is found.
 308        IFS=' '
 309        for tool in $tools
 310        do
 311                is_available "$tool" && echo "$tool" && return 0
 312        done
 313
 314        echo >&2 "No known ${TOOL_MODE} tool is available."
 315        return 1
 316}
 317
 318get_configured_merge_tool () {
 319        # Diff mode first tries diff.tool and falls back to merge.tool.
 320        # Merge mode only checks merge.tool
 321        if diff_mode
 322        then
 323                merge_tool=$(git config diff.tool || git config merge.tool)
 324        else
 325                merge_tool=$(git config merge.tool)
 326        fi
 327        if test -n "$merge_tool" && ! valid_tool "$merge_tool"
 328        then
 329                echo >&2 "git config option $TOOL_MODE.tool set to unknown tool: $merge_tool"
 330                echo >&2 "Resetting to default..."
 331                return 1
 332        fi
 333        echo "$merge_tool"
 334}
 335
 336get_merge_tool_path () {
 337        # A merge tool has been set, so verify that it's valid.
 338        merge_tool="$1"
 339        if ! valid_tool "$merge_tool"
 340        then
 341                echo >&2 "Unknown merge tool $merge_tool"
 342                exit 1
 343        fi
 344        if diff_mode
 345        then
 346                merge_tool_path=$(git config difftool."$merge_tool".path ||
 347                                  git config mergetool."$merge_tool".path)
 348        else
 349                merge_tool_path=$(git config mergetool."$merge_tool".path)
 350        fi
 351        if test -z "$merge_tool_path"
 352        then
 353                merge_tool_path=$(translate_merge_tool_path "$merge_tool")
 354        fi
 355        if test -z "$(get_merge_tool_cmd "$merge_tool")" &&
 356                ! type "$merge_tool_path" >/dev/null 2>&1
 357        then
 358                echo >&2 "The $TOOL_MODE tool $merge_tool is not available as"\
 359                         "'$merge_tool_path'"
 360                exit 1
 361        fi
 362        echo "$merge_tool_path"
 363}
 364
 365get_merge_tool () {
 366        # Check if a merge tool has been configured
 367        merge_tool=$(get_configured_merge_tool)
 368        # Try to guess an appropriate merge tool if no tool has been set.
 369        if test -z "$merge_tool"
 370        then
 371                merge_tool=$(guess_merge_tool) || exit
 372        fi
 373        echo "$merge_tool"
 374}