正如标题所说,我使用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
假设你正在使用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
正确解释您的提交消息。