_git_tool_completion() {
    local -a typed=()
    local index part
    for (( index = 1; index < COMP_CWORD; index++ )); do
        part=${COMP_WORDS[index]}
        if [[ ${#typed[@]} -gt 0 && ( $part == [:=] || ${COMP_WORDS[index-1]} == [:=] ) ]]; then
            typed[$(( ${#typed[@]} - 1 ))]+=$part
        else
            typed+=("$part")
        fi
    done
    local current=${COMP_WORDS[COMP_CWORD]}
    local last=$(( ${#typed[@]} - 1 ))
    if [[ $current == = ]]; then
        current=
    elif [[ $last -ge 0 && ${COMP_WORDS[COMP_CWORD-1]} == = ]]; then
        typed[$last]=${typed[$last]%=}
    fi
    local IFS=$'\n'
    local words="${typed[*]}"
    local reply; reply="$(_DOCOPT2_COMPLETE=2 _DOCOPT2_WORDS="$words" "${COMP_WORDS[0]}" 2>/dev/null)"
    COMPREPLY=( $(compgen -W "$(cut -f1 <<< "$reply")" -- "$current") )
    if [[ $reply == $'\t'* ]]; then
        local -a files=()
        local file
        while IFS= read -r file; do
            [[ -e ${file/#\~\//$HOME/} || -L $file ]] && files+=("$file")
        done <<< "$(compgen -f -- "$current")"
        if [[ ${#COMPREPLY[@]} -eq 0 ]]; then
            type compopt &>/dev/null && compopt -o filenames
            COMPREPLY=( "${files[@]}" )
        else
            for file in "${files[@]}"; do
                [[ -d $file ]] && file+=/
                COMPREPLY+=( "$(printf '%q' "$file")" )
            done
        fi
    fi
}
complete -F _git_tool_completion git-tool
