1

I am trying to run below code:

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);
    }
  }
}

But this gives me results without grepping the output as mentioned in grep.

Can anyone tell what is wrong i am doing in this code?

Running this code on Linux machine.

Magnus Bäck
  • 11,381
  • 3
  • 47
  • 59
  • On which repo you are running command? – Foolish Jul 06 '15 at 12:16
  • I have a local git repo, where i have code checked out, "git --git-dir **/home/data/gitCode/.git** blame StandOnLinux.java ";//"git blame StandOnLinux.java | grep e8a93e7d" – Manish Mankar Jul 06 '15 at 12:36
  • Try any other command first to see if it works i.e. `git log` or something. – Foolish Jul 06 '15 at 12:37
  • `--grep` flag does nothing for `git blame` command. check it with command line and it won't grep anything as well. W – Denis Tulskiy Jul 06 '15 at 12:58
  • @Foolish, i have tried run **git --git-dir **/home/data/gitCode/.git ** log --pretty=format:\"revision=\"%h\",%nauthor=\"%an\",%nmessage=\"%s\",%ndate=\"%cd\",\" --grep="message" ** and this is working properly and gives desired output. Issue is only when i use grep with blame, even this blame command mentioned avove is working fine if i run this on SourceTree terminal window. ###SourceTree is GUI of git. – Manish Mankar Jul 06 '15 at 13:03
  • @Denis, this grep command is working fine with git log but not with git blame. Not getting whats the issue with this git blame. – Manish Mankar Jul 06 '15 at 13:05
  • @ManishMankar `git blame` does not have a `--grep` option that I know of; `git log` does. That means you have to use pipe, hence my sub-shell suggestion below. – VonC Jul 06 '15 at 14:43

1 Answers1

0

For a pipe to work, you might try to execute the commands in a separate shell, as in "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);

Make sure the default $PATH does include git.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250