JGit在CircleCI上为远程设置git URI而非https。

15

我有以下代码(请看注释了解其作用):

// 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)

我可以确认在Windows上,https URL保持不变。如果在Linux上不是这种情况,那么似乎是一个bug,我会提交一个bugzilla。 - Rüdiger Herrmann
是的,然而快速查看代码并没有显示出明显的原因,配置是从您设置的URL构建的,没有对方案进行任何调整。也许您可以在您的机器上快速调试JGit,以排除代码中API的错误使用... - centic
如果我是你,我会只使用带有SSH密钥的Git协议。 - AdamSkywalker
也许你可以查看这个链接:/http://www.codeaffine.com/2014/12/09/jgit-authentication/,因为我猜测你需要提供身份验证细节,既然你正在推送。 - Dan King
1个回答

3

看起来你已经定义了

URL 重写

Git 提供了一种通过以下配置重写 URL 的方式:

git config --global url."git://".insteadOf https://

为了验证您是否设置正确,请检查您的代码库配置:

git config --list

您将在输出中看到以下行:
url.git://.insteadof=https://

您还可以检查您的.gitconfig文件,以验证您的配置文件中没有这个条目。

[url "git://"]
    insteadOf = https://

1
哦,我的天啊,这个绝妙的答案就是解决方案。在CircleCI的machine部分的circle.yml文件中运行> ~/.gitconfig(以覆盖配置)即可删除此配置:ubuntu@box304:~$ git config --list返回url.git@github.com:.insteadof=https://github.com/ - randers

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接