Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-20356] windows-compliant GIT_SSH
  • Loading branch information
ndeloof committed Oct 31, 2013
1 parent 307dab1 commit 6a44f4a
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Expand Up @@ -933,7 +933,7 @@ private String launchCommandWithCredentials(ArgumentListBuilder args, File workD
listener.getLogger().println("using GIT_SSH to set credentials " + sshUser.getDescription());

key = createSshKeyFile(key, sshUser);
ssh = createGitSSH(key, ssh);
ssh = createGitSSH(key);

env = new EnvVars(env);
env.put("GIT_SSH", ssh.getAbsolutePath());
Expand Down Expand Up @@ -965,15 +965,37 @@ private File createSshKeyFile(File key, SSHUserPrivateKey sshUser) throws IOExce
return key;
}

private File createGitSSH(File key, File ssh) throws IOException {
private File createGitSSH(File key) throws IOException {

if (File.pathSeparatorChar == ';')
return createWindowsGitSSH(key);
else
return createUnixGitSSH(key);
}

private File createWindowsGitSSH(File key) throws IOException {
File ssh = File.createTempFile("ssh", ".bat");
PrintWriter w;
// windows git installer place C:\Program Files\Git\cmd\git.exe in PATH
File sshexe = new File(new File(gitExe).getParentFile().getParentFile(), "bin/ssh.exe");
if (!sshexe.exists()) {
throw new RuntimeException("git plugin only support official git client http://git-scm.com/download/win");
}
w = new PrintWriter(ssh);
w .println("@echo off");
w .println("\"" + sshexe.getAbsolutePath() + "\" -i \"" + key.getAbsolutePath() +"\" -o StrictHostKeyChecking=no %* ");
w.flush();
w.close();
ssh.setExecutable(true);
return ssh;
}

ssh = File.createTempFile("ssh", ".exe");
private File createUnixGitSSH(File key) throws IOException {
File ssh = File.createTempFile("ssh", ".sh");
PrintWriter w;
w = new PrintWriter(ssh);
if (File.pathSeparatorChar != ';') {
w.println("#!/bin/sh");
}
w.println("ssh -i \"" + key.getAbsolutePath() + "\" \"$@\"");
w.println("#!/bin/sh");
w.println("ssh -i \"" + key.getAbsolutePath() + "\" -o StrictHostKeyChecking=no \"$@\"");
w.close();
ssh.setExecutable(true);
return ssh;
Expand Down

0 comments on commit 6a44f4a

Please sign in to comment.