Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issues with openssh-win32 agent. #295

Closed
wants to merge 7 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions GitUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ 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) {
if (((Get-Process -Id $agentPid -FileVersionInfo -ErrorAction SilentlyContinue).Filename).Contains("Git")) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If $agentPid is valid but the process has been killed, this will blow up when you try to access Filename.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Get-Process -Id 11111111 -FileVersionInfo -ErrorAction SilentlyContinue).Filename

doesn't throw any error. you will get empty data

return $agentPid
} else {
setenv 'SSH_AGENT_PID', $null
Expand All @@ -282,15 +281,30 @@ function Find-Pageant() {

# Attempt to guess $program's location. For ssh-agent/ssh-add.
function Find-Ssh($program = 'ssh-agent') {
$sshLocation = 'C:\Program Files\Git\usr\bin\'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be given as preference from user perspective. not sure where to keep this and how to import that var here. temp hardcoded

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually necessary? I would expect the existing code in Find-Ssh to be able to resolve the correct location relative to the discovered git command.


if($sshLocation -and (get-command (join-path $sshLocation $program))){
return join-path $sshLocation $program
}

$sshLocation = Get-Command $program -TotalCount 1 -ErrorAction SilentlyContinue
if($sshLocation){
return $sshLocation
}

Write-Verbose "$program not in path. Trying to guess location."
$gitItem = Get-Command git -Erroraction SilentlyContinue | Get-Item
if ($gitItem -eq $null) { Write-Warning 'git not in path'; return }

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

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

# Loosely based on bash script from http://help.github.com/ssh-key-passphrases/
Expand All @@ -312,8 +326,7 @@ function Start-SshAgent([switch]$Quiet) {
if (!$pageant) { Write-Warning "Could not find Pageant."; return }
Start-Process -NoNewWindow $pageant
} else {
$sshAgent = Get-Command ssh-agent -TotalCount 1 -ErrorAction SilentlyContinue
$sshAgent = if ($sshAgent) {$sshAgent} else {Find-Ssh('ssh-agent')}
$sshAgent = Find-Ssh('ssh-agent')
if (!$sshAgent) { Write-Warning 'Could not find ssh-agent'; return }

& $sshAgent | foreach {
Expand All @@ -339,7 +352,6 @@ function Add-SshKey() {
if (!$pageant) { Write-Warning 'Could not find Pageant'; return }

if ($args.Count -eq 0) {
$keystring = ""
$keyPath = Join-Path $Env:HOME ".ssh"
$keys = Get-ChildItem $keyPath/"*.ppk" -ErrorAction SilentlyContinue | Select -ExpandProperty FullName
& $pageant $keys
Expand All @@ -349,10 +361,8 @@ function Add-SshKey() {
}
}
} else {
$sshAdd = Get-Command ssh-add -TotalCount 1 -ErrorAction SilentlyContinue
$sshAdd = if ($sshAdd) {$sshAdd} else {Find-Ssh('ssh-add')}
$sshAdd = Find-Ssh('ssh-add')
if (!$sshAdd) { Write-Warning 'Could not find ssh-add'; return }

if ($args.Count -eq 0) {
& $sshAdd
} else {
Expand Down