Skip to content

Commit

Permalink
Make SSH_AGENT_PID mapping to Windows PID
Browse files Browse the repository at this point in the history
Port of dahlbyk/posh-git#699.  This does drop -ErrorAction Ignore as unnecessary.  It further adds [0] to full name parent directory for bin / usr lookup of ssh location as the lookup of 'ps' and and will get multiple (2) results.  This ensures it only uses the first in that situation.
  • Loading branch information
hazendaz committed Mar 9, 2024
1 parent 1ca5bf6 commit 1bf68e7
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/Agent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,26 @@ function Get-SshAgent() {
else {
$agentPid = $Env:SSH_AGENT_PID
if ($agentPid) {
$sshAgentProcess = Get-Process | Where-Object { ($_.Id -eq $agentPid) -and ($_.Name -eq 'ssh-agent') }
if ($null -ne $sshAgentProcess) {
return $agentPid
# Convert cygwin PID to Windows PID
$ps = Find-Ssh('ps')
if (!$ps) {
Write-Warning 'Could not find ps'
return 0
}
else {
setenv 'SSH_AGENT_PID' $null
setenv 'SSH_AUTH_SOCK' $null
$pidMap = @{ }
(& $ps) | Select-Object -skip 1 | ForEach-Object {
$line = ($_ -split "\s+" -match "\S")
$pidMap[$line[0]] = $line[3]
}
$winPid = $pidMap[$agentPid]
if ($winPid) {
$sshAgentProcess = Get-Process | Where-Object { ($_.Id -eq $winPid) -and ($_.Name -eq 'ssh-agent') }
if ($null -ne $sshAgentProcess) {
return $winPid
}
}
setenv 'SSH_AGENT_PID' $null
setenv 'SSH_AUTH_SOCK' $null
}
}

Expand All @@ -34,12 +46,12 @@ function Find-Ssh($program = 'ssh-agent') {
return
}

$sshLocation = join-path $gitItem.directory.parent.fullname bin/$program
$sshLocation = join-path $gitItem.directory.parent.fullname[0] bin/$program
if (get-command $sshLocation -Erroraction SilentlyContinue) {
return $sshLocation
}

$sshLocation = join-path $gitItem.directory.parent.fullname usr/bin/$program
$sshLocation = join-path $gitItem.directory.parent.fullname[0] usr/bin/$program
if (get-command $sshLocation -Erroraction SilentlyContinue) {
return $sshLocation
}
Expand Down

0 comments on commit 1bf68e7

Please sign in to comment.