Groovy执行Git shell命令

3

我正在尝试在Groovy中执行git shell命令。第一个命令可以成功执行,但第二个命令返回退出码128:

   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
是的,在该目录下这两个命令都如预期般在shell中工作。 - Antonio
如果在 def p 行之后添加 p.consumeProcessOutput(System.out, System.err) 这一行,会打印什么内容? - tim_yates
1
此外,这些调用不应该是异步的吗?所以 "git add .".execute(null, workingDir) 应该改为 "git add .".execute(null, workingDir).waitFor() - tim_yates
https://gist.github.com/katta/5465317 - Ultimo_m
1个回答

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()

我知道这与问题无关,但我真的很喜欢这个新的堆栈溢出回答者Jeff Brown。 :) 很高兴看到Grails开发人员出现在SO上帮助解决问题。 - grantmcconnaughey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接