Groovy 执行 git shell 命令



我正在尝试在 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()

这段代码有什么问题?

第二个进程在第一个进程完成之前启动。 当第二个 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()

最新更新