Mehedi
Mehedi

Reputation: 394

Git command works in terminal but not from groovy script

The following git command works in the Android Studio Terminal

git --no-pager show -s --format='%an <%ae>' c1ff6aa

But it doesn't work when invoked from the groovy script like this:

def getGitCommitAuthor(commitId){
 def cmd2 = 'git --no-pager show -s --format=\'%an <%ae>\' ' + commitId

 def proc2 = cmd2.execute()
 proc2.text.trim()
}

Upvotes: 1

Views: 1111

Answers (1)

cfrick
cfrick

Reputation: 37033

Use the "array" syntax to execute it:

groovy:000> ["git", "--no-pager", "show", "-s", "--format='%an <%ae>'"].execute().text
===> 'John Doe <[email protected]>'

Without properly separated params there is some hickup and the command results in an error:

groovy:000> sout = new StringBuilder()
===> 
groovy:000> serr = new StringBuilder()
===> 
groovy:000> p="git  --no-pager show -s --format='%an <%ae>'".execute()
===> java.lang.UNIXProcess@5dcb4f5f
groovy:000> p.consumeProcessOutput(sout,serr)
===> null
groovy:000> p.waitFor()
// XXX exit code!
===> 128
groovy:000> serr
// XXX error
===> fatal: ambiguous argument '<%ae>'': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Upvotes: 2

Related Questions