15

I have the following code (see the comments for what's happening):

// 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 is saving the git: url into the .git/FETCH_HEAD file, which is then being used for pushing. Since the git: protocol does not support authentication, I am unable to push to the remote and the process fails.

The .git/config file contains the correct https: URI for the remote (that's why the code is printing the https: URI).

My question is:

What can I do to make JGit set the https: URI correctly
(which would then allow me to push again)?


This issue only arises in a very special environment (on CircleCI, a Ubuntu 12.04.2 LTS virtual box) - it's not reproducable on 15.10, 14.04 LTS and 12.04.2 LTS fresh ubuntu distributions and not reproducable on Windows.

The easiest way to reproduce the issue is to create a dummy GitHub repository, then start building your dummy project on CircleCI, and then to rerun your first build with SSH. You then have 30 minutes of SSH time to upload any groovy/java files to the box. After the 30 minutes the box will be forcefully shut down.


If I use git remote -v in the directory this was cloned into, I get: (which points me to the fact that the git: URIs are indeed used)

origin  git@github.com:RAnders00/KayonDoc.git (fetch)
origin  git@github.com:RAnders00/KayonDoc.git (push)
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
randers
  • 5,031
  • 5
  • 37
  • 64
  • I can confirm that on Windows the https URLs remain intact. If this isn't the case with Linux it seems like a bug and I would [file a bugzilla](https://eclipse.org/jgit/support/). – Rüdiger Herrmann Nov 21 '15 at 10:09
  • Yes, however a quick look at the code did not show an obvious reason for this, the config is built from the URL that you set without any adjustment of the scheme. Maybe you can do a quick debugging into JGit on your machine to rule out some incorrect usage of the APIs in your code... – centic Nov 21 '15 at 22:44
  • if I were you, I'd just use git protocol with ssh keys – AdamSkywalker Dec 03 '15 at 19:49
  • Maybe check out this limk:/http://www.codeaffine.com/2014/12/09/jgit-authentication/ since you are pushing I'm guessing you need to provide authentication details – Dan King Dec 10 '15 at 05:44

1 Answers1

3

Looks like you have defined

URL Rewriting

Git provides a way to rewrite URLs with the following config:

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

To verify if you have set it check the configuration of your repository:

git config --list

You'll see the following line in the output:

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

You can also check your .gitconfig files to verify that you don't have this entry in your config files

[url "git://"]
    insteadOf = https://
Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 1
    Oh my god, this brilliant answer is the solution. `ubuntu@box304:~$ git config --list` yields `url.git@github.com:.insteadof=https://github.com/` - to remove this configuration, run `> ~/.gitconfig`(to override the config) on CircleCI in the `machine` section of your `circle.yml` file. – randers Jan 05 '16 at 19:48