Auto-Starting SSH Agent when ssh-agent.exe is not running, doesn't always work flawlessly in GitBash Windows 10

Viewed 387

As I mentioned in the Question title, I have installed GitBash git version 2.33.1.windows.1 in Windows 10 Pro OS.

Now I have this function(in ~/.bashrc or ~/.profile) which works almost everytime, I have restarted system afresh, i.e. no other process apart from one instance of git-bash.exe and necessary instances of sh.exe/bash.exe whichever is required by GitBash are running:

git-login(){
    trap 'printf "%3d: " "$LINENO"' DEBUG;
    function gen-key {
        if [[ ! -f $HOME_SSH/id_rsa && ! -f $PROJECT_SSH/id_rsa ]]; then
            rm -f $HOME_SSH/* && rm -f $PROJECT_SSH/*;
            ssh-keygen -t ed25519 -C $GIT_SSH_USER -f ~/.ssh/id_rsa -q -P $GIT_SSH_PSWD;
            cat ~/.ssh/id_rsa.pub > /dev/clipboard;
        fi;
    }
    SSH_ENV=$HOME/.ssh/environment && gen-key;
    function start_agent {
        set-pswd && echo Initializing new SSH agent... && ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV};
        echo succeeded && chmod 600 ${SSH_ENV} && . ${SSH_ENV} > /dev/null;
        ssh-add ~/.ssh/id_rsa &>/dev/null && echo '' | clip;
        alias SSHAgent="ps -p $SSH_AGENT_PID | awk '{print $1,$4,$8}' | sed -n '2 p'";
    }
    if [ -f "${SSH_ENV}" ]; then
        . ${SSH_ENV} > /dev/null;
        ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || { start_agent; }
    else start_agent; fi && load-exec;
}
git-login

But imagine a situation where I have accidentally exited the ssh-agent.exe or need to start another ssh-agent.exe after killing a possibly unresponsive current ssh-agent.exe process.

Then this function doesn't execute fully and by that I mean, I don't see the messages in order like below:

Initializing new SSH agent...
succeeded
Enter passphrase for key '/c/Users/<Username>/.ssh/id_rsa':

In that weird situation where I need to start ssh-agent or restart ssh-agent for logging purposes I see only this message:

Enter passphrase for key '/c/Users/<Username>/.ssh/id_rsa':

Now the way I have done the code, this could only happen if there's anything wrong with the set-pswd method which I am calling before echoing, but it doesn't look like set-pswd would be causing errors, whose code is:

set-pswd() { alias clip='iconv -f utf-8 -t utf-16le | clip.exe' && echo $GIT_SSH_PSWD | clip; }

But I would prefer a fresh set of eyes to take a look at this so that they may find what I maybe missed ? Anyone ? Also anyone feel free to suggest any improvements that can be done in these functions(set-pswd or git-login) without any major changes to intended functionality of auto-login in Git through SSH.

Note: All other variables in the main code are accounted for and I have made sure they aren't cause of issue.

0 Answers
Related