issue with git blame command using java program

给你一囗甜甜゛ 提交于 2019-12-10 10:54:19

问题


I am trying to run below git command using java program on windows.

 git --git-dir D:\code_coverage\.git blame  'src/RunCodeCoverage.java'
 | grep e4ecfbe | awk '{print $7}'

it gives me error :

fatal: cannot stat path '$7}'': No such file or directory

While running this command from command prompt, it gives me results as desired.

Please help!


回答1:


On a CMD in Windows, I can make it work using double quotes for the awk parameters:

 git --git-dir D:\code_coverage\.git blame  'src/RunCodeCoverage.java'
 | grep e4ecfbe | awk "{print $7}"

Note that my awk.exe comes from Gnu On Windows.

The OP mentions using that command in Java with:

String commandToBeExecuted="git --git-dir D:\code_coverage\.git blame 'src/RunCodeCoverage.java' | grep e4ecfbe | awk "{print $7}"'"; 
Process p = Runtime.getRuntime().exec(commandToBeExecuted);

But you never pass all parameters as a single string.
Use an array, as in "Using Quotes within getRuntime().exec" and in "How to make pipes work with Runtime.exec()?"

Process p = Runtime.getRuntime().exec(new String[]{"cmd", "/c", commandToBeExecuted);

Escape the double quotes in commandToBeExecuted:

 commandToBeExecuted = "git --git-dir D:\code_coverage\.git blame  'src/RunCodeCoverage.java'
 | grep e4ecfbe | awk \"{print $7}\""


来源:https://stackoverflow.com/questions/31199139/issue-with-git-blame-command-using-java-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!