Я пытаюсь использовать Process Builder для выполнения команды git. Но он, похоже, не работает никакой командой.ProcessBuilder с командой GIT не дает результата
git log
дает мне правильный результат.
git shortlog -s
но заканчивается на TimeOut!
Обе команды, запущенные на терминале, обеспечивают правильный результат! Я тестировал Win и Mac.
Кто-нибудь есть идеи, как я могу отладить это, или где моя ошибка?
Или, может быть, другое решение? Цель состоит в том, чтобы прочитать коммиттер определенного файла в репозитории git.
Фактическая команда Git: git shortlog -s -p FeilePath
?
Мой Тест Код:
@Test
public void testCommandListDir() {
File execDir = new File("./");
String returnValue = "";
try {
returnValue = runCommand(execDir, 10, TimeUnit.SECONDS, "git", "log");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
assertTrue(returnValue.contains("+ try and error for git shortlog"));
}
@Test
public void testCommandGitShortlog() {
File execDir = new File("./");
String returnValue = "";
try {
returnValue = runCommand(execDir, 10, TimeUnit.SECONDS, "git", "shortlog", "-s");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
assertTrue(returnValue.equals(""));
}
public static String runCommand(File directory, long timeout, TimeUnit unit, String... command) throws IOException, InterruptedException {
StringBuilder sb = new StringBuilder();
ProcessBuilder pb = new ProcessBuilder(command)
.redirectErrorStream(true).directory(directory);
Process p = pb.start();
InputStream is = p.getInputStream();
InputStream es = p.getErrorStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
BufferedReader bre = new BufferedReader(new InputStreamReader(es));
String in;
boolean timeOut = false;
if (!p.waitFor(timeout, unit)) {
//timeout - kill the process.
p.destroy(); // consider using destroyForcibly instead
timeOut = true;
}
if (timeOut) {
//write time out msg
sb.append("RunCommand time out! after " + timeout + " " + unit.toString() + "\n");
sb.append(" directory: " + directory.getAbsolutePath() + "\n");
sb.append(" command: " + command + "\n");
} else {
while ((in = br.readLine()) != null) {
sb.append(in + "\n");
}
while ((in = bre.readLine()) != null) {
sb.append(in + "\n");
}
}
return sb.toString();
}