Git命令在终端中工作,但不是来自groovy脚本 [英] Git command works in terminal but not from groovy script

查看:305
本文介绍了Git命令在终端中工作,但不是来自groovy脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下git命令在Android Studio终端中运行

The following git command works in the Android Studio Terminal

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

但从 groovy 脚本调用时不起作用:

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


推荐答案

使用数组 / p>

Use the "array" syntax to execute it:

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

如果没有正确分隔的参数,该命令导致错误:

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>...]'

这篇关于Git命令在终端中工作,但不是来自groovy脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆