<commit_before># Bash runtime configuration | SSH agent


# ensure interactive shell
[[ $- != *i* ]] && return

# ensure non-SSH shell
[[ -n "$SSH_TTY" ]] && return


# ensure SSH agent

export SSH_ENV="$HOME/.ssh/env"

function ssh_add
{
    ssh-add
}

function start_ssh_agent
{

    ssh_dir="$(dirname "$SSH_ENV")"
    if [[ ! -d "$ssh_dir" ]]; then
        if [[ ! -e "$ssh_dir" ]]; then
            mkdir -p -m 0700 "$ssh_dir"
        else
            echo "cannot start SSH agent: '$ssh_dir' is not a directory" >&2
            return
        fi
    fi

    ssh-agent -s | grep -v ^echo > "$SSH_ENV"
    chmod 0600 "$SSH_ENV"

    . "$SSH_ENV" > /dev/null

    trap 'ssh-agent -k' EXIT
}

if [[ -e "$SSH_AUTH_SOCK"
      && -n "$SSH_AGENT_PID"
      && -n "$(ps -fp "$SSH_AGENT_PID" | grep ssh-agent)" ]]; then
    # SSH agent was initialized elsewhere and was set up in this shell's env

    if ! ssh-add -l >& /dev/null; then
        # SSH agent has no keys

        ssh_add
    fi
elif [[ -f "$SSH_ENV" ]]; then
    # SSH agent was initialized by this script (or equivalent)

    . "$SSH_ENV" > /dev/null
    if ! [[ -e "$SSH_AUTH_SOCK"
            && -n "$SSH_AGENT_PID"
            && -n "$(ps -fp "$SSH_AGENT_PID" | grep ssh-agent)" ]]; then
        # SSH agent died

        start_ssh_agent
        ssh_add
    fi
else
    # SSH agent was not initialized

    start_ssh_agent
    ssh_add
fi

unset ssh_add
unset start_ssh_agent<commit_msg>Bash config: improve SSH agent setup<commit_after># Bash runtime configuration | SSH agent


export SSH_ENV="$HOME/.ssh/env"


function ssh_agent_add_keys
{
    ssh-add
}


function ssh_agent_start
{

    ssh_dir="$(dirname "$SSH_ENV")"
    if [[ ! -d "$ssh_dir" ]]; then
        if [[ ! -e "$ssh_dir" ]]; then
            mkdir -p -m 0700 "$ssh_dir"
        else
            echo "cannot start SSH agent: '$ssh_dir' is not a directory" >&2
            return
        fi
    fi

    ssh-agent -s | grep -v ^echo > "$SSH_ENV"
    chmod 0600 "$SSH_ENV"

    . "$SSH_ENV" > /dev/null
}


if [[ -e "$SSH_AUTH_SOCK"
      && -n "$SSH_AGENT_PID"
      && -n "$(ps -fp "$SSH_AGENT_PID" | grep ssh-agent)" ]]; then
    # SSH agent was initialized elsewhere and was set up in this shell's env

    if [[ $- == *i* ]]; then
        # interactive shell

        if ! ssh-add -l >& /dev/null; then
            # SSH agent has no keys

            ssh_agent_add_keys
        fi
    fi

elif [[ -f "$SSH_ENV" ]]; then
    # SSH agent was initialized by this script (or equivalent)

    . "$SSH_ENV" > /dev/null

    if ! [[ -e "$SSH_AUTH_SOCK"
            && -n "$SSH_AGENT_PID"
            && -n "$(ps -fp "$SSH_AGENT_PID" | grep ssh-agent)" ]]; then
        # SSH agent died

        unset SSH_AUTH_SOCK
        unset SSH_AGENT_PID

        if [[ $- == *i* ]]; then
            # interactive shell

            ssh_agent_start
            ssh_agent_add_keys
        fi
    fi

else
    # SSH agent was not initialized

    if [[ $- == *i* ]]; then
        # interactive shell

        ssh_agent_start
        ssh_agent_add_keys
    fi
fi

unset ssh_agent_add_keys
unset ssh_agent_start
{'fraction_non_alphanumeric': 0.1296, 'fraction_numerical': 0.00576, 'mean_word_length': 1.9686609686609686, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7351458', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1080, 'n_words': 320}