Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy execute a git shell command

Tags:

shell

groovy

I'm trying to execute git shell command in groovy. The first is executed well but the second returns the exit code 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()

what's the problem with this code?

like image 351
Antonio Avatar asked Jun 11 '26 00:06

Antonio


1 Answers

The second process is starting before the first one completes. When the second git process starts git recognizes that there is already a git process operating in that same directory which could cause problems so it errors out. If you read the error stream from the first process you will see something like this:

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.

If you wait for the first one to finish before starting the second one, that should work. Something like this:

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()
like image 155
Jeff Scott Brown Avatar answered Jun 13 '26 23:06

Jeff Scott Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!