2013-12-21 19 views
2

为什么提交消息中的空格导致p2失败?当命令有空格时执行groovy进程的问题

def envp = null 
def repoDir = File.createTempFile("test","txt").getParentFile() 

Process p = "git init".execute(null, repoDir) 
p.waitForProcessOutput(System.out, System.err) 

///////////// 

println "Commit1 start" 
Process p1 = "git commit --allow-empty -m \"Commit1\"".execute(null, repoDir) 
p1.waitForProcessOutput(System.out, System.err) 
println "Commit1 done" 


///////////// 

println "Commit2 start" 
Process p2 = "git commit --allow-empty -m \"Commit number 2\"".execute(null, repoDir) 
p2.waitForProcessOutput(System.out, System.err) 
println "Commit2 done" 
+0

你尝试过'''git commit --allow-empty -m'提交编号2'''?用单引号。 – dmahapatro

+0

是的。试过也。没有工作。我在Mac Mavericks上 – DarVar

回答

3

下面的代码可以解决你的问题:

println "Commit2 start" 
Process p2 = ["git", "commit", "--allow-empty", "-m \"Commit number 2\""].execute(null, repoDir) 
p2.waitForProcessOutput(System.out, System.err) 
println "Commit2 done 

使用数组(而不是字符串)作为报价,参数与 - 空间问题的变通方法记录在这里: http://groovy.codehaus.org/Executing+External+Processes+From+Groovy