grep命令不能使用git blame文件和git log来进行修订

时间:2015-07-06 12:01:28

标签: java linux git runtime

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

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机器上运行此代码。

1 个答案:

答案 0 :(得分:0)

要使管道正常工作,您可以尝试在单独的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