Runtime.getRuntime (). Exec () git commit -m XXX с неверным кодом выхода «1» - PullRequest
0 голосов
/ 01 ноября 2018

Как видно из заголовка, я использую Runtime.getRuntime().exec для выполнения git commit -m XXX команды.

К сожалению, он возвращает нестандартный код выхода с 1 (кстати, правильный код 0).

Я пытаюсь набрать команду в командной строке command команда commit работает нормально.

Кто-нибудь знает, где проблема?

public static int commit(String dir,String commitMsg) {
    String command = "git commit -m " + commitMsg;
    exitCode = ProcessUtil.safeSyncRun(command, dir);
    System.out.println(command + " exitcode = " + exitCode);
    return exitCode;
}
public static int safeSyncRun(String command, String workingDir) {
    Process process;
    int exitValue = -1;
    try {
        process = Runtime.getRuntime().exec(command, null, new File(workingDir));
        process.waitFor();
        exitValue = process.exitValue();
    } catch (IOException | InterruptedException e) {
        System.out.println("exception : " + e);
    }finally{
        process = null;
    }
    return exitValue;
}

Выходы ниже:

git commit -m test commit msg 
exitcode = 1

1 Ответ

0 голосов
/ 04 апреля 2019

Если вы используете bash, попробуйте вместо этого (используя " Как я могу отладить проблемы, связанные с git / git-shell? "):

String command = "bash -c 'export GIT_TRACE=true; export GIT_TRACE_SETUP =true; git commit -m \"" + commitMsg + "\"'";

Обратите внимание на \" вокруг вашего commitMsg: это может помочь git commit правильно интерпретировать ваше сообщение о коммите.

...