2012-11-24 124 views
3

我想在groovy中執行git shell命令。第一個執行得很好,但第二個返回退出代碼128:Groovy執行一個git shell命令

def workingDir = new File("path/to/dir") 
    "git add .".execute(null, workingDir) 
    def p = "git reset --hard".execute(null, workingDir) 
    p.text.eachLine {println it} 
    println p.exitValue() 

這段代碼有什麼問題?

+1

如果您在workingDir的常規shell中執行這些命令,它們是否按預期工作?聽起來像重置失敗,出於某種原因... – mmigdol

+0

是的,這兩個命令在shell中的預期工作該目錄 – Antonio

+0

什麼添加'p.consumeProcessOutput(System.out,System.err)'print(如果添加'def p'行之後的那一行)? –

回答

4

第二個過程在第一個過程完成之前開始。當第二個git進程啓動時,git認識到已經有一個git進程在同一個目錄下運行,這可能會導致問題,從而導致出錯。如果從第一進程讀取錯誤流,你會看到這樣的事情:

fatal: Unable to create 'path/to/dir/.git/index.lock': File exists. 

If no other git process is currently running, this probably means a 
git process crashed in this repository earlier. Make sure no other git 
process is running and remove the file manually to continue. 

如果等待第一個開始在第二個之前完成,應該工作。例如:

def workingDir = new File("path/to/dir/") 

def p = "git add .".execute(null, workingDir) 
p.waitFor() 
p = "git reset --hard".execute(null, workingDir) 
p.text.eachLine {println it} 
println p.exitValue() 
+0

我知道這與問題無關,但我真的很喜歡這個新的堆棧溢出回答傑夫布朗。 :)真的很高興看到Grails開發人員出現在SO上。 – grantmcconnaughey