【问题标题】:issue with git blame command using java program使用java程序的git blame命令问题
【发布时间】:2015-07-03 05:30:54
【问题描述】:

我正在尝试在 Windows 上使用 java 程序在 git 命令下运行。

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

它给了我错误:

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

从命令提示符运行此命令时,它会为我提供所需的结果。

请帮忙!

【问题讨论】:

    标签: java git runtime


    【解决方案1】:

    在 Windows 的 CMD 上,我可以使用 awk 参数的双引号使其工作:

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

    请注意,我的 awk.exe 来自 Gnu On Windows

    OP 提到在 Java 中使用该命令:

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

    但您永远不会将所有参数作为单个字符串传递。
    使用数组,如“Using Quotes within getRuntime().exec”和“How to make pipes work with Runtime.exec()?

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

    转义commandToBeExecuted中的双引号:

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

    【讨论】:

    • 感谢 VonC,它通过 CMD 工作,但是在使用 java Runtime 从 java 程序运行它时,它给出了提到的错误。:致命:无法统计路径 '$7}'':没有这样的文件或目录
    • @ManishMankar 您使用什么确切的 Java 代码将参数传递给 java Runtime.exec() 命令?
    • String commandToBeExecuted="git --git-dir D:\code_coverage\.git blame 'src/RunCodeCoverage.java' | grep e4ecfbe | awk "{print $7}"'";进程 p = Runtime.getRuntime().exec(commandToBeExecuted);
    • @ManishMankar 我已经相应地修改了答案
    • 谢谢@VonC!但仍然遇到问题“致命:无法统计路径''src/RunCodeCoverage.java'':没有这样的文件或目录”
    猜你喜欢
    • 2012-06-17
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 2015-04-27
    • 2019-03-31
    • 2021-02-27
    • 2019-12-20
    相关资源
    最近更新 更多