gpt4 book ai didi

java - 使用 Java Process Builder 启动 GitLog

转载 作者:行者123 更新时间:2023-11-30 02:30:33 39 4
gpt4 key购买 nike

我尝试通过 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 目录的路径。我将 gitpath 硬编码到 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 - 使用 Java Process Builder 启动 GitLog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44407664/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com