正如标题所说,我使用Runtime.getRuntime().exec
来执行git commit -m XXX
命令。
不幸的是,它返回了带有1的非正常exitcode(顺便说一句,正确的代码是0(。
我试着在命令行上键入命令,提交命令工作正常。
有人知道问题出在哪里吗?
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
正确解释提交消息。