5

Whenever I "synchronize" (pull, push) my repository in Visual Studio, I notice the git-askpass.exe window pop up and ask me for my ssh private key passphrase for my git repository. That's fine, but I was wondering whether there is a way to get it to work with ssh-agent.exe.

When starting my git bash interpreter, I always also start its own ssh-agent.exe (it's in Program Files/git as opposed to Visual Studio's Program Files (x86)/Microsoft Visual Studio/2017/Professional/Common7/IDE/CommonExtensions/Microsoft/TeamFoundation/Team Explorer/Git/mingw32/libexec/git-core) and ssh-add.exe my private key so that it asks me once for passphrase and then stops bothering me. From Visual Studio I always have to reenter the passphrase for my private key upon "sync-ing" the projects. Is there any way to slipstream this?

I was thinking of deleting that whole Visual Studio folder and creating a hard link towards the Git for Windows folder...

There's also Connect to Git repository with SSH using Visual Studio 2017 which doesn't feel like it covers my case.

foxx1337
  • 1,859
  • 3
  • 19
  • 23

1 Answers1

7

Ok, I think i figured this one out.

Visual Studio 2017 launches git from C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\cmd and git.exe from there attempts its own auth methods until one works.

The auth method I care about in my question deals with the already running ssh-agent process. Upon launching it, ssh-agent detects its environment and "conveniently" advises upon which "standard" env variables to set, such as:

SSH_AUTH_SOCK=/tmp/ssh-iTGtZyR9tAxO/agent.12088; export SSH_AUTH_SOCK;
SSH_AGENT_PID=972; export SSH_AGENT_PID;
#echo Agent pid 972;

Notice that the path is in "MINGW-speak", resolving to paths such as %LOCALAPPDATA%\Temp. It's actually OK, even outside of git bash to refer to paths like that (git.exe, ssh-agent.exe will be able to resolve them).

So I have 2 scripts, one that loads my private key, which is started by my .bashrc the first time I open a git bash:

10_ssh.sh:

#!/usr/bin/env bash

SSH_ENV=$HOME/.ssh/environment

function start_agent {
    echo "Initialising new SSH agent..."

    # run it with eval so that it sticks after terminal completion
    eval "/usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}"

    echo succeeded
    chmod 600 ${SSH_ENV}
    . ${SSH_ENV} > /dev/null

    /usr/bin/ssh-add;
}

# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then
    . ${SSH_ENV} > /dev/null
    #ps ${SSH_AGENT_PID} doesn't work under cywgin
    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi

and a new one I just created, that also puts into "Windows" those variables:

15_ssh_windows_hacks.sh:

#!/usr/bin/env bash

# This one makes sure that the Windows environment recognizes the ssh agent started before

if [[ `uname` == MINGW* ]]
then
    setx SSH_AUTH_SOCK $SSH_AUTH_SOCK > /dev/null
    setx SSH_AGENT_PID $SSH_AGENT_PID > /dev/null
fi

The only caveat is that Visual Studio has to be started after executing this script, because apparently it only reads its ENV once when it starts, and not every time it spawns the child git.exe process.

foxx1337
  • 1,859
  • 3
  • 19
  • 23
  • I combined the 2 scripts into one. That way I can just launch it independently from .bashrc or .bash_profile. Reason for combining the 2: The first script does not create permanent environment variables if not launched from .bashrc. – Daniel Hillebrand Nov 01 '19 at 18:30
  • @DanielHillebrand I'm using these scripts on both Linux and Windows, that's why I'm keeping them separated. – foxx1337 Nov 02 '19 at 19:14
  • That makes perfectly sense for your use-case. In my case I am only using Windows at the moment and I lost quite some time analysing error messages and trying to get .bashrc executing the first script. Merging the 2 scripts works best in my case. Thanks a lot for this resource! – Daniel Hillebrand Nov 05 '19 at 09:53
  • I also added something to help with `ssh-agent` in the PowerShell world - https://github.com/foxx1337/psprofile. – foxx1337 Sep 23 '20 at 09:41
  • 1
    For me it didn't work with `if [[ `uname` == MINGW* ]]` condition, because I was starting bash from: `C:\Program Files\Git\usr\bin` instead of `C:\Program Files\Git\bin`. In my case uname returned: `MSYS...` – Mr Patience Dec 13 '21 at 19:08
  • Could you explain what the problems were and what your script does to fix it? you give the environment variables but don't ever say what's wrong with them, you make a "tongue in cheek" comment about them but don't explain. You even say "its actually OK" which makes me think these are acceptable? – diox8tony Jan 10 '23 at 17:33