JGit 设置 git: URI 而不是 https: 用于 CircleCI 上的远程

JGit sets git: URI instead of https: for remote on CircleCI

我有以下代码(请参阅评论以了解发生的情况):

// 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 virtual box)——它在 15.10、14.04 LTS 和 12.04.2 LTS fresh ubuntu 发行版,无法在 Windows.

上重现

重现该问题的最简单方法是 create a dummy GitHub repository, then start building 您在 CircleCI 上的虚拟项目,然后使用 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)

看起来你已经定义了

URL重写

Git 提供了一种使用以下配置重写 URLs 的方法:

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

要验证是否已设置它,请检查存储库的配置:

git config --list

您将在输出中看到以下行:

url.git://.insteadof=https://

您还可以检查您的 .gitconfig 文件以确认您的配置文件中没有此条目

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