以下git命令适用于Android Studio终端
git --no-pager show -s --format='%an <%ae>' c1ff6aa
但是从 groovy 脚本中调用时它不起作用:
def getGitCommitAuthor(commitId){
def cmd2 = 'git --no-pager show -s --format=\'%an <%ae>\' ' + commitId
def proc2 = cmd2.execute()
proc2.text.trim()
}
答案 0 :(得分:2)
使用“array”语法执行它:
groovy:000> ["git", "--no-pager", "show", "-s", "--format='%an <%ae>'"].execute().text
===> 'John Doe <jd@example.com>'
如果没有正确分隔的参数,则会出现一些警报并且命令会导致错误:
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>...]'