gpt4 book ai didi

java - 使用 JGit 访问现有克隆?

转载 作者:行者123 更新时间:2023-12-01 19:20:30 24 4
gpt4 key购买 nike

我的计算机上已经有一个存储库的克隆。我希望创建一个 Java 程序,在打开时 pull ,在关闭时推送。

这就是我启动 JGit 的方式:

auth = new UsernamePasswordCredentialsProvider("[username]", "[pass]");

git = Git.open(new File(path_to_git + "\\.git"));

git.checkout().setName("master").call();

git.pull().setCredentialsProvider(auth).call();

这在我第一次启动时有效,但如果我关闭并重新打开程序,我会收到错误

Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$2(LauncherImpl.java:352)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.UnsupportedOperationException: Cannot check out from unborn branch
at org.eclipse.jgit.api.CheckoutCommand.call(CheckoutCommand.java:235)
at com.company.app.Main.<clinit>(Main.java:59)
... 11 more

当程序关闭时,我调用函数pushToGit(),其定义为:

public static void pushToGit() {

try {

git.remoteAdd()
.setName("origin")
.setUri(new URIish("https://github.com/[username]/[repo]"))
.call();

git.commit().setMessage("from database application").setAuthor(new PersonIdent("[name]","[email]")).call();

git.push().setCredentialsProvider(auth).call();

} catch (GitAPIException | URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

每当我运行该程序时,我都需要删除本地存储库并重新克隆它。

最佳答案

您可以尝试使用以下示例代码片段。

Git git = Git.cloneRepository()
.setURI( "https://github.com/someRepoName" )
.setDirectory( "/path/to/repo" )
.setBranch( "refs/heads/master" )
.call();

您可以引用下面的链接了解更多详情。 https://www.codeaffine.com/2015/11/30/jgit-clone-repository/

在高版本的Jgit api中,可以使用CloneCommand,可以查看下面的链接。

https://www.programcreek.com/java-api-examples/?api=org.eclipse.jgit.api.CloneCommand

关于java - 使用 JGit 访问现有克隆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59362233/

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