Skip to content

Commit

Permalink
[FIXED JENKINS-8449] Use correct shell interpreter on Windows slave.
Browse files Browse the repository at this point in the history
  • Loading branch information
ssogabe authored and kohsuke committed Apr 21, 2011
1 parent a61600b commit 6d8fd54
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions core/src/main/java/hudson/tasks/Shell.java
@@ -1,7 +1,7 @@
/*
* The MIT License
*
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Jene Jasper, Yahoo! Inc.
* Copyright (c) 2004-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi, Jene Jasper, Yahoo! Inc., Seiji Sogabe
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -28,7 +28,10 @@
import hudson.Util;
import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.remoting.Callable;
import hudson.remoting.VirtualChannel;
import hudson.util.FormValidation;
import java.io.IOException;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
Expand All @@ -37,6 +40,7 @@
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Logger;

/**
* Executes a series of commands by using a shell.
Expand Down Expand Up @@ -96,8 +100,8 @@ public String[] buildCommandLine(FilePath script) {
args.add(script.getRemote());
args.set(0,args.get(0).substring(2)); // trim off "#!"
return args.toArray(new String[args.size()]);
} else
return new String[] { getDescriptor().getShellOrDefault(),"-xe",script.getRemote()};
} else
return new String[] { getDescriptor().getShellOrDefault(script.getChannel()), "-xe", script.getRemote()};
}

protected String getContents() {
Expand Down Expand Up @@ -132,12 +136,35 @@ public String getShell() {
return shell;
}

/**
* @deprecated 1.403
* Use {@link #getShellOrDefault(hudson.remoting.VirtualChannel) }.
*/
public String getShellOrDefault() {
if(shell==null)
return Functions.isWindows() ?"sh":"/bin/sh";
return shell;
}

public String getShellOrDefault(VirtualChannel channel) {
if (shell != null)
return shell;

String interpreter = null;
try {
interpreter = channel.call(new Shellinterpreter());
} catch (IOException e) {
LOGGER.warning(e.getMessage());
} catch (InterruptedException e) {
LOGGER.warning(e.getMessage());
}
if (interpreter == null) {
interpreter = getShellOrDefault();
}

return interpreter;
}

public void setShell(String shell) {
this.shell = Util.fixEmptyAndTrim(shell);
save();
Expand Down Expand Up @@ -165,5 +192,17 @@ public FormValidation doCheck(@QueryParameter String value) {
// Executable requires admin permission
return FormValidation.validateExecutable(value);
}

private static final class Shellinterpreter implements Callable<String, IOException> {

private static final long serialVersionUID = 1L;

public String call() throws IOException {
return Functions.isWindows() ? "sh" : "/bin/sh";
}
}

}

private static final Logger LOGGER = Logger.getLogger(Shell.class.getName());
}

0 comments on commit 6d8fd54

Please sign in to comment.