Skip to content

Commit

Permalink
Git: Windows: Look for ssh-agent in OpenSSH dir
Browse files Browse the repository at this point in the history
  • Loading branch information
alarr46 committed Apr 6, 2020
1 parent b84eada commit 127bb59
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
71 changes: 39 additions & 32 deletions extensions/git/src/git.ts
Expand Up @@ -318,25 +318,23 @@ class SshAgent {
}

async start(): Promise<void> {
const sshAgentCommand = await this.getCommand('ssh-agent');
if (!sshAgentCommand) {
return;
}

const sshAgentProcess = cp.spawn(sshAgentCommand, ['-s']);
const sshAgentResult = await exec(sshAgentProcess);
sshAgentResult.stdout.toString().split('\n')
.forEach(outputLine => {
const sshAgentPidMatch = outputLine.match(/SSH_AGENT_PID=([^;]*)/);
if (sshAgentPidMatch && sshAgentPidMatch.length === 2) {
this._sshAgentPid = sshAgentPidMatch[1];
}
return this.getCommand('ssh-agent')
.then(sshAgentCommand => exec(cp.spawn(sshAgentCommand, ['-s'])))
.then(sshAgentResult => sshAgentResult.stdout
.toString()
.split('\n')
.forEach(outputLine => {
const sshAgentPidMatch = outputLine.match(/SSH_AGENT_PID=([^;]*)/);
if (sshAgentPidMatch && sshAgentPidMatch.length === 2) {
this._sshAgentPid = sshAgentPidMatch[1];
}

const sshAuthSockMatch = outputLine.match(/SSH_AUTH_SOCK=([^;]*)/);
if (sshAuthSockMatch && sshAuthSockMatch.length === 2) {
this._sshAuthSock = sshAuthSockMatch[1];
}
});
const sshAuthSockMatch = outputLine.match(/SSH_AUTH_SOCK=([^;]*)/);
if (sshAuthSockMatch && sshAuthSockMatch.length === 2) {
this._sshAuthSock = sshAuthSockMatch[1];
}
})
);
}

async addKey(privateKeyPath: string): Promise<boolean> {
Expand Down Expand Up @@ -375,20 +373,29 @@ class SshAgent {

private async getCommand(commandName: string): Promise<string> {
const gitConfig = workspace.getConfiguration('git');
const sshAgentDirectory = gitConfig.get<string>('sshAgentDirectory') ||
isWindows ? 'C:\\Program Files\\Git\\usr\\bin' : '/usr/bin';

const command = sshAgentDirectory ? path.join(sshAgentDirectory, commandName) : commandName;

return (new Promise<string>((c, e) => which(command, (err, path) => err ? e(err) : c(path))))
.then(undefined, () => new Promise<string>(c => which(commandName, (err, path) => {
if (err) {
window.showErrorMessage(localize('failed to find command', "Failed to find command '{0}'", commandName));
return c();
} else {
return c(path);
}
})));
const sshAgentDirectoryConfig = gitConfig.get<string>('sshAgentDirectory');

const basePromise = sshAgentDirectoryConfig
? this.checkIfCommandExists(path.join(sshAgentDirectoryConfig, commandName)).catch(() => this.checkIfCommandExists(commandName))
: this.checkIfCommandExists(commandName);
const directoriesToSearch = isWindows ? [
'C:\\Program Files\\Git\\usr\\bin',
'C:\\Program Files\\OpenSSH-Win64',
'C:\\Program Files\\OpenSSH',
] : [];

return directoriesToSearch
.reduce(
(previousPromise: Promise<string>, directoryName: string) => previousPromise.catch(() => this.checkIfCommandExists(directoryName)),
basePromise
).catch(reason => {
window.showErrorMessage(localize('failed to find command', "Failed to find command '{0}'", commandName));
return Promise.reject(reason);
});
}

private async checkIfCommandExists(command: string): Promise<string> {
return new Promise<string>((c, e) => which(command, (err, path) => err ? e(err) : c(path)));
}
}

Expand Down
3 changes: 2 additions & 1 deletion extensions/git/src/main.ts
Expand Up @@ -56,7 +56,8 @@ async function createModel(context: ExtensionContext, outputChannel: OutputChann
}

const git = new Git({ gitPath: info.path, version: info.version, env });
await git.sshAgent.start();
await git.sshAgent.start().catch(() => null);


const model = new Model(git, context.globalState, outputChannel);
disposables.push(model);
Expand Down

0 comments on commit 127bb59

Please sign in to comment.