Merge pull request #154 from csware/tortoisegitplink
[msysgit.git] / cmd / start-ssh-agent.cmd
blobeb575bd671cd1cc4126c4cc9cf46015d645a65cb
1 @echo off
3 rem Enable extensions, the `verify other 2>nul` is a trick from the setlocal help
4 verify other 2>nul
5 setlocal EnableDelayedExpansion
6 if errorlevel 1 (
7 echo Unable to enable extensions
8 goto failure
11 rem Start the ssh-agent if needed by git
12 for %%i in ("git.exe") do set GIT=%%~$PATH:i
13 if exist "%GIT%" (
14 rem Get the ssh-agent executable
15 for %%i in ("ssh-agent.exe") do set SSH_AGENT=%%~$PATH:i
16 if not exist "%SSH_AGENT%" (
17 for %%s in ("%GIT%") do set GIT_DIR=%%~dps
18 for %%s in ("!GIT_DIR!") do set GIT_DIR=!GIT_DIR:~0,-1!
19 for %%s in ("!GIT_DIR!") do set GIT_ROOT=%%~dps
20 for %%s in ("!GIT_ROOT!") do set GIT_ROOT=!GIT_ROOT:~0,-1!
21 for /D %%s in ("!GIT_ROOT!\bin\ssh-agent.exe") do set SSH_AGENT=%%~s
22 if not exist "!SSH_AGENT!" (
23 goto ssh-agent-done
26 rem Get the ssh-add executable
27 for %%s in ("!SSH_AGENT!") do set BIN_DIR=%%~dps
28 for %%s in ("!BIN_DIR!") do set BIN_DIR=!BIN_DIR:~0,-1!
29 for /D %%s in ("!BIN_DIR!\ssh-add.exe") do set SSH_ADD=%%~s
30 if not exist "!SSH_ADD!" (
31 goto ssh-agent-done
33 rem Check if the agent is running
34 for /f "tokens=1-2" %%a in ('tasklist /fi "imagename eq ssh-agent.exe"') do (
35 echo %%b | findstr /r /c:"[0-9][0-9]*" > nul
36 if "!errorlevel!" == "0" (
37 set SSH_AGENT_PID=%%b
40 rem Connect up the current ssh-agent
41 if [!SSH_AGENT_PID!] == [] (
42 echo Removing old ssh-agent sockets
43 for /d %%d in (%TEMP%\ssh-??????*) do rmdir /s /q %%d
44 ) else (
45 echo Found ssh-agent at !SSH_AGENT_PID!
46 for /d %%d in (%TEMP%\ssh-??????*) do (
47 for %%f in (%%d\agent.*) do (
48 set SSH_AUTH_SOCK=%%f
49 set SSH_AUTH_SOCK=!SSH_AUTH_SOCK:%TEMP%=/tmp!
50 set SSH_AUTH_SOCK=!SSH_AUTH_SOCK:\=/!
53 if not [!SSH_AUTH_SOCK!] == [] (
54 echo Found ssh-agent socket at !SSH_AUTH_SOCK!
55 ) else (
56 echo Failed to find ssh-agent socket
57 set SSH_AGENT_PID=
60 rem See if we have the key
61 "!SSH_ADD!" 1>nul 2>nul
62 set result=!ERRORLEVEL!
63 if not !result! == 0 (
64 if !result! == 2 (
65 echo|set /p=Starting ssh-agent:
66 for /f "tokens=1-2 delims==;" %%a in ('"!SSH_AGENT!"') do (
67 if not [%%b] == [] (
68 set %%a=%%b
71 echo. done
73 set "HOME=%USERPROFILE%"
74 "!SSH_ADD!"
75 echo.
79 :ssh-agent-done
80 :failure
82 endlocal & set "SSH_AUTH_SOCK=%SSH_AUTH_SOCK%" ^
83 & set "SSH_AGENT_PID=%SSH_AGENT_PID%"
85 echo %cmdcmdline% | findstr /l "\"\"" >nul
86 if not errorlevel 1 (
87 call cmd %*