使用 Java Process Builder 启动 GitLog
Start GitLog with Java Process Builder
我尝试在 Java 中通过 Processbuilder 启动 GitLog 命令。
GitLog 命令:
git --git-dir=C:/Users/User/Code/code1/git/.git log
--pretty=format:"%H \"%an\" %ad \"%s\"" --numstat --date=short
这是我的代码。 Path 是 git 目录的路径。
我将 git 路径硬编码到 git 目录以进行测试。
public void createGitLog( Path path ) {
try
{
String gitpath = "--git-dir=C:/Users/User/Code/code1/git/.git";
String options = "--pretty=format:\"%H \\"%an\\" %ad \\"%s\\"\" --numstat --date=short";
ProcessBuilder builder = new ProcessBuilder("git", gitpath, "log", options );
Process process = builder.start();
builder.redirectOutput(ProcessBuilder.Redirect.to( path.resolve("gitlog.dat").toFile() ) );
int exitValue = process.waitFor();
if ( exitValue != 0 )
{
// throw
}
}
catch (IOException e) {
}
}
如果我在 cmd 中尝试此命令,它会起作用,但在 Java 中,我总是得到退出代码 128。
这个过程有什么问题?
在我的情况下,终端中的 运行 命令有效:
"/bin/bash" - path to your bash
"-c" - states that next param is command
"command" - full command you want to execute from terminal (like git log --pretty=format:"%H \"%an\" %ad \"%s\"" --numstat --date=short
)
String command = "git " + gitpath + " log " + options;
ProcessBuilder builder = new ProcessBuilder("/bin/bash" , "-c" , command);
如果您想从特定目录启动进程,您也可以在 ProcessBuilder directory()
上使用;
.directory(new File("C:/Users/User/Code/code1/git/"))
我尝试在 Java 中通过 Processbuilder 启动 GitLog 命令。
GitLog 命令:
git --git-dir=C:/Users/User/Code/code1/git/.git log --pretty=format:"%H \"%an\" %ad \"%s\"" --numstat --date=short
这是我的代码。 Path 是 git 目录的路径。 我将 git 路径硬编码到 git 目录以进行测试。
public void createGitLog( Path path ) {
try
{
String gitpath = "--git-dir=C:/Users/User/Code/code1/git/.git";
String options = "--pretty=format:\"%H \\"%an\\" %ad \\"%s\\"\" --numstat --date=short";
ProcessBuilder builder = new ProcessBuilder("git", gitpath, "log", options );
Process process = builder.start();
builder.redirectOutput(ProcessBuilder.Redirect.to( path.resolve("gitlog.dat").toFile() ) );
int exitValue = process.waitFor();
if ( exitValue != 0 )
{
// throw
}
}
catch (IOException e) {
}
}
如果我在 cmd 中尝试此命令,它会起作用,但在 Java 中,我总是得到退出代码 128。
这个过程有什么问题?
在我的情况下,终端中的 运行 命令有效:
"/bin/bash" - path to your bash
"-c" - states that next param is command
"command" - full command you want to execute from terminal (like
git log --pretty=format:"%H \"%an\" %ad \"%s\"" --numstat --date=short
)
String command = "git " + gitpath + " log " + options;
ProcessBuilder builder = new ProcessBuilder("/bin/bash" , "-c" , command);
如果您想从特定目录启动进程,您也可以在 ProcessBuilder directory()
上使用;
.directory(new File("C:/Users/User/Code/code1/git/"))