-1

I am trying to use JGit. I tried following http://www.codeaffine.com/2014/12/09/jgit-authentication/ and the following block of the code throws a ClassCastException

remoteRepository.setTransportConfigCallback(new TransportConfigCallback() {
    @Override
    public void configure(Transport transport) {
        SshTransport sshTransport = (SshTransport) transport;
        sshTransport.setSshSessionFactory(sshSessionFactory);
    }
});

Exception:

java.lang.ClassCastException: org.eclipse.jgit.transport.TransportHttp cannot be cast to org.eclipse.jgit.transport.SshTransport

What am I missing? I am using JGit version 4.10.0.201712302008-r.

Community
  • 1
  • 1
user2427771
  • 33
  • 11

2 Answers2

2

The code is only meant to handle SSH connections. If you are connecting through other protocols, you need to adjust the code to be aware that transport can be something different than SshTransport.

For example:

command.setTransportConfigCallback(new TransportConfigCallback() {
  @Override
  public void configure(Transport transport) {
    if(transport instanceof SshTransport) {
      SshTransport sshTransport = (SshTransport) transport;
      sshTransport.setSshSessionFactory(sshSessionFactory);
    } else if(transport instanceof HttpTransport) {
      // configure HTTP protocol specifics
    }
  }
} );
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • Not sure why is the downvote , but the same code use to work and I am using ssh (ssh://git@ ...) . It also involves some debug efforts where I saw that the transport is coming as an HttpTransport (transport instanceof HttpTransport is true). – user2427771 Feb 16 '18 at 15:20
0

when you set: cloneCommand.setURI("ssh://user@example.com/repo.git" ); Indicate the url with ssh protocol, the repo in github. Example - (ssh://git@github.com:githubtraining/hellogitworld.git)

Refer this https://github.com/allegro/axion-release-plugin/issues/101

  • thanks , I do not have enough reps to upvote ya , but the issue is that I did not notice I was setting http url instead of the ssh one in my test case – user2427771 Feb 16 '18 at 16:44