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

RAn*_*s00 15 java git jgit ubuntu-12.04 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)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

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存储库,然后开始在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)
Run Code Online (Sandbox Code Playgroud)

Cod*_*ard 3

看起来你已经定义了

网址重写

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

git config --global url."git://".insteadOf https://
Run Code Online (Sandbox Code Playgroud)

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

git config --list
Run Code Online (Sandbox Code Playgroud)

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

url.git://.insteadof=https://
Run Code Online (Sandbox Code Playgroud)

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

[url "git://"]
    insteadOf = https://
Run Code Online (Sandbox Code Playgroud)