gpt4 book ai didi

java - grep 命令不适用于 git blame 文件和 git log 以进行修订

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:28:13 26 4
gpt4 key购买 nike

我正在尝试运行以下代码:

public class StandOnLinux {
public static void main(String[] args) throws IOException {
//String commandToBeExecuted = "git blame '"+files[i]+"' | grep "+revision+" | awk '{print $7}'";
//String commandToBeExecuted = "git --git-dir D:\\code_coverage\\.git blame src\\RunCodeCoverage.java | grep 'e4ecfbe'"; git blame --git-dir D:/code_coverage/.git src/RunCodeCoverage.java| grep 59e3fdc | gawk '{print $7}'
String commandToBeExecuted = "git blame StandOnLinux.java --grep=\"e8a93e7d\"";
String temp = "";
File file = new File("C:/Program Files (x86)/Git/bin");
System.out.println(commandToBeExecuted);
Process p = Runtime.getRuntime().exec(commandToBeExecuted);
//Process p = Runtime.getRuntime().exec(new String[]{"cmd", "/c", commandToBeExecuted});
//Process p =Runtime.getRuntime().exec(commandToBeExecuted, null, file);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
p.getInputStream()));

BufferedReader stdError = new BufferedReader(new InputStreamReader(
p.getErrorStream()));
while((temp = stdInput.readLine()) != null) {
System.out.println(temp);
}
while((temp = stdError.readLine()) != null) {
System.out.println(temp);
}
}
}

但这给了我结果,而没有像 grep 中提到的那样对输出进行 grep。

谁能告诉我在这段代码中做错了什么?

在 Linux 机器上运行这段代码。

最佳答案

要使管道正常工作,您可以尝试在单独的 shell 中执行命令,如“How to make pipes work with Runtime.exec()?

String[] cmd = {
"/bin/sh",
"-c",
"git blame StandOnLinux.java | grep e8a93e7d"
};

Process p = Runtime.getRuntime().exec(cmd);

确保默认的 $PATH 包含 git

关于java - grep 命令不适用于 git blame 文件和 git log 以进行修订,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31245334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com