提交 bd27be08 编写于 作者: D dty

Older versions of bash improperly decide that a script is a binary file if there

is non-ASCII on the first line of the file, preventing the step from executing
properly. This was discovered by the DirectoryBrowserSupport unit test failing.
This can be worked around by inserting a leading carriage return so that bash
won't find the non-ASCII on the first line.

   - Modify the Shell task to insert a carriage return for shell scripts that
     don't start with #! or already contain a leading carriage return.

     main/core/src/main/java/hudson/tasks/Shell.java

   - This test failed after the change because the inserted carriage return
     is returned from getContent(). Trim the result before comparing it to
     the expected data.

     main/test/src/test/java/hudson/model/FreeStyleProjectTest.java


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@23006 71c3de6d-444a-0410-be80-ed276b4c234a
上级 34387b2f
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Jene Jasper
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Jene Jasper, Yahoo! Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
......@@ -70,6 +70,21 @@ public class Shell extends CommandInterpreter {
return s;
}
/**
* Older versions of bash have a bug where non-ASCII on the first line
* makes the shell think the file is a binary file and not a script. Adding
* a leading carriage return works around this problem.
*/
private static String addCrForNonASCII(String s) {
if(!s.startsWith("#!")) {
if (s.indexOf('\n')!=0) {
return new String("\n" + s);
}
}
return s;
}
protected String[] buildCommandLine(FilePath script) {
if(command.startsWith("#!")) {
// interpreter override
......@@ -85,7 +100,7 @@ public class Shell extends CommandInterpreter {
}
protected String getContents() {
return fixCrLf(command);
return addCrForNonASCII(fixCrLf(command));
}
protected String getFileExtension() {
......
......@@ -57,7 +57,7 @@ public class FreeStyleProjectTest extends HudsonTestCase {
List<Builder> builders = project.getBuilders();
assertEquals(1,builders.size());
assertEquals(Shell.class,builders.get(0).getClass());
assertEquals("echo hello",((Shell)builders.get(0)).getCommand());
assertEquals("echo hello",((Shell)builders.get(0)).getCommand().trim());
assertTrue(builders.get(0)!=shell);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册