Runtime.getRuntime()。exec()git commit -m XXX错误的exitcode为“1”

问题描述 投票:1回答:1

正如标题所说,我使用Runtime.getRuntime().exec来执行git commit -m XXX命令。

不幸的是,它返回带有1的非正规退出代码(之间,正确的代码是0)。

我尝试在命令行上键入命令,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
git runtime git-commit
1个回答
1
投票

假设你正在使用bash,请尝试改为(使用“How can I debug git/git-shell related problems?”):

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

请注意\"周围的commitMsg:这可以帮助git commit正确解释您的提交消息。

© www.soinside.com 2019 - 2024. All rights reserved.