java - 使用java程序的git blame命令问题

标签 java git runtime

我正在尝试在 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

从命令提示符运行此命令时,它会根据需要提供结果。

请帮忙!

最佳答案

在 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}\""

关于java - 使用java程序的git blame命令问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31199139/

相关文章:

java - 解决 Maven 多模块项目的依赖项时出错

git - 这是有效的 git 分支行为吗?

compiler-construction - 用托管语言编写内存管理器?

c - ANSI C 的可嵌入 VM 运行时

java - 有没有办法检查 pentaho 中是否存在文件夹?

java - java中的UNICODE正则表达式

java - 升级到 hibernate 6 : Encountered deprecated setting [javax. persistence.lock.timeout],使用 [jakarta.persistence.lock.timeout] 代替

git - 如何获取 dokku 应用程序的 git 远程 URL

git - 是否有可以在我自己的服务器上运行的 PHP Github 克隆?

android - 根据用户偏好加载资源