|
| 1 | +@REM Do not use "echo off" to not affect any child calls. |
| 2 | + |
| 3 | +@REM Enable extensions, the `verify` call 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 |
| 9 | +) |
| 10 | + |
| 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!\usr\bin\ssh-agent.exe") DO @SET SSH_AGENT=%%~s |
| 22 | + @IF NOT EXIST "!SSH_AGENT!" @GOTO ssh-agent-done |
| 23 | + ) |
| 24 | + @REM Get the ssh-add executable |
| 25 | + @FOR %%s IN ("!SSH_AGENT!") DO @SET BIN_DIR=%%~dps |
| 26 | + @FOR %%s in ("!BIN_DIR!") DO @SET BIN_DIR=!BIN_DIR:~0,-1! |
| 27 | + @FOR /D %%s in ("!BIN_DIR!\ssh-add.exe") DO @SET SSH_ADD=%%~s |
| 28 | + @IF NOT EXIST "!SSH_ADD!" @GOTO ssh-agent-done |
| 29 | + @REM Check if the agent is running |
| 30 | + @FOR /f "tokens=1-2" %%a IN ('tasklist /fi "imagename eq ssh-agent.exe"') DO @( |
| 31 | + @ECHO %%b | @FINDSTR /r /c:"[0-9][0-9]*" > NUL |
| 32 | + @IF "!ERRORLEVEL!" == "0" @( |
| 33 | + @SET SSH_AGENT_PID=%%b |
| 34 | + ) else @( |
| 35 | + @REM Unset in the case a user kills the agent while a session is open |
| 36 | + @REM needed to remove the old files and prevent a false message |
| 37 | + @SET SSH_AGENT_PID= |
| 38 | + ) |
| 39 | + ) |
| 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:\=/! |
| 51 | + ) |
| 52 | + ) |
| 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= |
| 58 | + ) |
| 59 | + ) |
| 60 | + @REM See if we have the key |
| 61 | + @SET "HOME=%USERPROFILE%" |
| 62 | + @"!SSH_ADD!" -l 1>NUL 2>NUL |
| 63 | + @SET result=!ERRORLEVEL! |
| 64 | + @IF NOT !result! == 0 @( |
| 65 | + @IF !result! == 2 @( |
| 66 | + @ECHO | @SET /p=Starting ssh-agent: |
| 67 | + @FOR /f "tokens=1-2 delims==;" %%a IN ('"!SSH_AGENT!"') DO @( |
| 68 | + @IF NOT [%%b] == [] @SET %%a=%%b |
| 69 | + ) |
| 70 | + @ECHO. done |
| 71 | + ) |
| 72 | + @"!SSH_ADD!" |
| 73 | + @ECHO. |
| 74 | + ) |
| 75 | +) |
| 76 | + |
| 77 | +:ssh-agent-done |
| 78 | +:failure |
| 79 | + |
| 80 | +@ENDLOCAL & @SET "SSH_AUTH_SOCK=%SSH_AUTH_SOCK%" ^ |
| 81 | + & @SET "SSH_AGENT_PID=%SSH_AGENT_PID%" |
| 82 | + |
| 83 | +@ECHO %cmdcmdline% | @FINDSTR /l "\"\"" >NUL |
| 84 | +@IF NOT ERRORLEVEL 1 @( |
| 85 | + @CALL cmd %* |
| 86 | +) |
0 commit comments