我有以下代码(请看注释了解其作用):
// Clone repository from GitHub into a local directory.
Git git = Git.cloneRepository()
.setBranch("gh-pages")
.setURI("https://github.com/RAnders00/KayonDoc.git")
.setDirectory(new File("/home/ubuntu/KayonDoc"))
.call();
// Print out remotes in config from JGit
Config config = git.getRepository().getConfig();
config.getSubsections("remote").forEach(it -> {
System.out.println(config.getString("remote", it, "url"));
});
// Prints https://github.com/RAnders00/KayonDoc.git
// Everything seems OK
// You could perform some changes to the repository here...
// Push changes to origin
git.push()
.setCredentialsManager(new UsernamePasswordCredentialsProvider("RAnders00", "hunter2"))
.call();
// Throws exception (look below)
Caught: org.eclipse.jgit.api.errors.TransportException: git@github.com:RAnders00/KayonDoc.git: push not permitted
org.eclipse.jgit.api.errors.TransportException: git@github.com:RAnders00/KayonDoc.git: push not permitted
at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:164)
at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:80)
at <your class> (YourClass.java:?)
Caused by: org.eclipse.jgit.errors.TransportException: git@github.com:RAnders00/KayonDoc.git: push not permitted
at org.eclipse.jgit.transport.BasePackPushConnection.noRepository(BasePackPushConnection.java:176)
at org.eclipse.jgit.transport.BasePackConnection.readAdvertisedRefsImpl(BasePackConnection.java:200)
at org.eclipse.jgit.transport.BasePackConnection.readAdvertisedRefs(BasePackConnection.java:178)
at org.eclipse.jgit.transport.TransportGitSsh$SshPushConnection.<init>(TransportGitSsh.java:341)
at org.eclipse.jgit.transport.TransportGitSsh.openPush(TransportGitSsh.java:166)
at org.eclipse.jgit.transport.PushProcess.execute(PushProcess.java:154)
at org.eclipse.jgit.transport.Transport.push(Transport.java:1200)
at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:157)
... 3 more
JGit将git:
URL保存在.git/FETCH_HEAD
文件中,然后用于推送。由于git:
协议不支持身份验证,我无法推送到远程仓库,进程失败。
.git/config
文件包含远程的正确https:
URI(这就是代码打印https:
URI的原因)。
我的问题是:
我该如何使JGit正确设置https:
URI
(这将允许我再次进行推送)?
这个问题只会在非常特殊的环境下出现(在CircleCI上,Ubuntu 12.04.2 LTS虚拟机上),它在15.10、14.04 LTS和12.04.2 LTS新的ubuntu分布版中无法重现,也不会在Windows上出现。
最简单的复现方法是创建一个虚拟的GitHub存储库,然后开始构建你的虚拟项目,在SSH模式下重新运行第一次构建。然后你有30分钟的SSH时间来上传任何Groovy/Java文件到机器上。30分钟后,机器将被强制关闭。
如果我在此被克隆的目录中使用git remote -v
,我会得到以下输出:(这表明确实使用了git:
URI)
origin git@github.com:RAnders00/KayonDoc.git (fetch)
origin git@github.com:RAnders00/KayonDoc.git (push)