我正在使用JGit,并希望从远程存储库中提取到我的本地存储库中。
第一个方法是克隆存储库,并且运行良好:
CredentialsProvider cp = new UsernamePasswordCredentialsProvider(username, password);
try (Git result = Git.cloneRepository()
.setURI("http://172.20.1.2/team/myrepo.git")
.setDirectory(new File("c:\\temp\\gittest"))
.setCredentialsProvider(cp)
.call()) {
System.out.println("Having repository: " + result.getRepository().getDirectory());
}
但是在第二次调用之后,不需要再次克隆存储库。所以我以为我需要拉
Git git = Git.open(new File("c:\\temp\\gittest"));
git.pull().call();
但是我收到以下错误:
org.eclipse.jgit.api.errors.TransportException: http://172.20.1.2/team/myrepo.git: Authentication is required but no CredentialsProvider has been registered
我不知道在哪里可以将pull命令传递给凭据。
您可以使用与相同的方式将传递CredentialsProvider
给。PushCommand
CloneCommand
例如:
CredentialsProvider cp = new UsernamePasswordCredentialsProvider(username, password);
git.pull().setCredentialsProvider(cp).call();
连接到远程存储库的所有命令都具有一个公共基类:TransportCommand
。此类提供了指定身份验证提供程序的方法。
要了解有关使用JGit进行身份验证的更多信息,您可能还想看看我前一段时间写的JGit Authentication Explained文章。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句