From 1bf68e7225d8e4d4eca77e53096fd35fc0a3ab50 Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Sat, 9 Mar 2024 15:19:00 -0500 Subject: [PATCH] Make SSH_AGENT_PID mapping to Windows PID Port of https://github.com/dahlbyk/posh-git/pull/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. --- src/Agent.ps1 | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Agent.ps1 b/src/Agent.ps1 index cf5e876..b10e1ff 100644 --- a/src/Agent.ps1 +++ b/src/Agent.ps1 @@ -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 } } @@ -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 }