6

I am trying to clone a git project with Java over ssh. I have username and password of a git-shell user as credentials. I can clone the project in terminal using the following command with no problem. (Of course, it asks for the password first)

git clone user@HOST:/path/Example.git

However when I try the following code using JGIT api

File localPath = new File("TempProject");
Git.cloneRepository()
    .setURI("ssh://HOST/path/example.git")
    .setDirectory(localPath)
    .setCredentialsProvider(new UsernamePasswordCredentialsProvider("***", "***"))
    .call();

I got

Exception in thread "main" org.eclipse.jgit.api.errors.TransportException: ssh://HOST/path/example.git: Auth fail

What should I do? Any ideas? (I am using OSX 10.9.4 and JDK 1.8)

Nuri Tasdemir
  • 9,720
  • 3
  • 42
  • 67

1 Answers1

6

For authentication with SSH, JGit uses JSch. JSch provides an SshSessionFactory to create and dispose SSH connections. The quickest way to tell JGit which SSH session factory should be used is to set it globally through SshSessionFactory.setInstance().

JGit provides an abstract JschConfigSessionFactory, whose configure method can be overridden to provide the password:

SshSessionFactory.setInstance( new JschConfigSessionFactory() {
    @Override
    protected void configure( Host host, Session session ) {
      session.setPassword( "password" );
    }
} );
Git.cloneRepository()
  .setURI( "ssh://username@host/path/repo.git" )
  .setDirectory( "/path/to/local/repo" )
  .call();

To set the SshSessionFactory in a more sensible way is slightly more complex. The CloneCommand - like all JGit command classes that may open a connection - inherits from TransportCommand. This class has a setTransportConfigCallback() method that can also be used to specify the SSH session factory for the actual command.

CloneCommand cloneCommand = Git.cloneRepository();
cloneCommand.setTransportConfigCallback( new TransportConfigCallback() {
  @Override
  public void configure( Transport transport ) {
    if( transport instanceof SshTransport ) {
      SshTransport sshTransport = ( SshTransport )transport;
      sshTransport.setSshSessionFactory( ... );
    }
  }
} );
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • @NuriTasdemir I've enhanced the answer with your code snippet and I recommend to revert the question to its original form to preserve the Q&A character. Others will find the _best_ answer as the first one (and that may be a different one than mine in the future). – Rüdiger Herrmann Sep 21 '14 at 10:56
  • In order to compile this code in Eclipse I had to include jsch.jar (for the com.jcraft.jsch.Session) However this time when I run the program (which is an Eclipse plugin that depends on jgit plugin), I got the following error. java.lang.LinkageError: loader constraint violation: loader previously initiated loading for a different type with name "com/jcraft/jsch/Session" Any ideas? – Nuri Tasdemir Sep 29 '14 at 12:16
  • Could it be that this is related to [bug 420903](https://bugs.eclipse.org/bugs/show_bug.cgi?id=420903)? Otherwise I recommend to post a separate question and provide more details about JGit version, JSch version, your bundle manifest, etc. – Rüdiger Herrmann Sep 29 '14 at 13:06
  • I post it as http://stackoverflow.com/questions/26102649/java-lang-linkageerror-while-using-jgit-and-jsch-for-eclipse-plugin-development – Nuri Tasdemir Sep 29 '14 at 14:48