1

I'm working on Java to show some information from Git repository.I'm using "Runtime.getRuntime().exec" to execute the command.

So when I write

Process process = Runtime.getRuntime().exec("git --git-dir=/home/gh/git/.git --work-tree=/home/gh/git log");

it works perfect

However, I need to get the log information for a specific person, so when I write the following statement, it doesn't display any result

String activeDeveloper = "Carlos Rica";
Process process = Runtime.getRuntime().exec("git --git-dir=/home/ghadeer/git/.git --work-tree=/home/ghadeer/git log --author=" + activeDeveloper);

anyone knows how what is the problem?

Ghadeer
  • 608
  • 2
  • 7
  • 21
  • 2
    Disregarding Java for the moment, how would you enter that command to search for a specific person *on the command line*? Make sure you can get the correct results there before trying to put it inside Java. – Greg Hewgill Aug 11 '12 at 22:28
  • This is the way to enter the command on the terminal. "git --git-dir=/home/ghadeer/git/.git --work-tree=/home/ghadeer/git log --author="Carlos Rica"). And, it works perfect as well. But, it doesn't in Java !!! – Ghadeer Aug 11 '12 at 22:41
  • 1
    You have spaces in your developer name. If this gets passed to `exec` is will be treated as two arguments, and if you don't have a developer called Carlos (with no last name), `git log` can't return results. You must quote the name correctly to do that. – Jochen Aug 11 '12 at 22:42
  • 1
    `String activeDeveloper = "\"Carlos Rica\"";` – mazaneicha Aug 11 '12 at 22:50
  • Jochen, you are right; it doesn't work because of the space. But, what I can do to make it work even if the name has the space?!!! – Ghadeer Aug 11 '12 at 23:12

1 Answers1

0

Following mazaneicha's suggestion, the usual way to add those quotes around the author name would be:

Process process = Runtime.getRuntime().exec("git --git-dir=/home/ghadeer/git/.git --work-tree=/home/ghadeer/git log --author=\"" + activeDeveloper + "\"");

However, if this still doesn't work, you would need to tokenize the all command, as illustrated in the "Runtime.getRuntime().exec()" SO question.

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