JGit clone repository
Asked Answered
S

7

9

I'm trying to clone Git repository with JGit and I have problem with UnsupportedCredentialItem.

My code:

FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(PATH).readEnvironment().findGitDir().build();

Git git = new Git(repository);              
CloneCommand clone = git.cloneRepository();
clone.setBare(false);
clone.setCloneAllBranches(true);
clone.setDirectory(PATH).setURI(url);
UsernamePasswordCredentialsProvider user = new UsernamePasswordCredentialsProvider(login, password);                
clone.setCredentialsProvider(user);
clone.call();   

It will occur Exception:

 org.eclipse.jgit.errors.UnsupportedCredentialItem: ssh://[email protected]:22: Passphrase for C:\Users\Marek\.ssh\id_rsa at
 org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider.get(UsernamePasswordCredentialsProvider.java:110)....

But if I delete file known_hosts in .ssh\ It will occur different Exception

org.eclipse.jgit.errors.UnsupportedCredentialItem: ssh://[email protected]:22: The authenticity of host 'github.com' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting?
at org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider.get(UsernamePasswordCredentialsProvider.java:110)....

Is there any possibility to type "yes" to that question or just skip it?

Thank you!

Sybilla answered 17/12, 2011 at 14:12 Comment(0)
F
6

I think if you login with username and password, you need https. For ssh you will need a public key that matches the one on record with github.

Flutter answered 6/4, 2012 at 1:15 Comment(0)
B
4

This will do it (like @michals, only less code) if using username / password with ssh

public void gitClone() throws GitAPIException {
    final File localPath = new File("./TestRepo");
    Git.cloneRepository()
        .setURI(REMOTE_URL)
        .setDirectory(localPath)
        .setCredentialsProvider(new UsernamePasswordCredentialsProvider("***", "***"))
        .call();
}
Brummell answered 12/1, 2014 at 20:0 Comment(0)
B
3

I suppose you would want to check the github help:

http://help.github.com/win-set-up-git/

Especially the part about generating ssh keys (ssh-keygen -t rsa -C "[email protected]"). Read the article for your environment, and you'll understand how to get a better configuration.

Boating answered 19/12, 2011 at 22:51 Comment(0)
L
2

I had the same problem. The reason was passphrase set for rsa private key. When I remove passphrase for this key it started work without any CredentialsProvider.

UsernamePasswordCredentialsProvider probably don't support passphrase. If you would like to have passphrase set, you could define you own CredentialProvider, which will support it, for example:

CloneCommand clone = Git.cloneRepository()
    .setURI("...")
    .setCredentialsProvider(new CredentialsProvider() {

        @Override
        public boolean supports(CredentialItem... items) {
            return true;
        }

        @Override
        public boolean isInteractive() {
            return true;
        }

        @Override
        public boolean get(URIish uri, CredentialItem... items)
                throws UnsupportedCredentialItem {

            for (CredentialItem item : items) {
                    if (item instanceof CredentialItem.StringType) {
                        ((CredentialItem.StringType) item).
                            setValue(new String("YOUR_PASSPHRASE"));
                        continue;
                    }
                }
                return true;
            }
        });

clone.call();

It works for me ;)

Lotus answered 14/6, 2013 at 12:51 Comment(0)
S
2

I had a similar issue, though my setup was a bit different. Leaving this here in case anyone else encounters something similar. I had overridden my configure method and createDefaultJSch method according to this tutorial: https://www.codeaffine.com/2014/12/09/jgit-authentication/

I had something like:

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

@Override
protected JSch createDefaultJSch( FS fs ) throws JSchException {
  JSch defaultJSch = super.createDefaultJSch( fs );
  defaultJSch.addIdentity( "/path/to/private_key" );
  return defaultJSch;
}

I ended up changing my createdDefaultJSch method to getSch (adding the appropriate parameters) and adding removeAllIdentity():

@Override
public JSch getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException {
  JSch jSch = super.getJSch(hc, fs)
  jSch.removeAllIdentity()
  jSch.addIdentity( "/path/to/private_key" )
  return jSch
} 

No idea why this worked, but I found the getSch thing from this answer (coincidentally by the same guy who wrote the tutorial): Using Keys with JGit to Access a Git Repository Securely

Schou answered 24/1, 2019 at 9:54 Comment(0)
E
1

It is not clear to me whether you want to do username/password authentication or public/private key authentication. Either way, CredentialsProvider will not be used, according to this. You need to configure the transport. First, create a transport configuration callback:

SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
  @Override
  protected void configure( Host host, Session session ) {
    // If you are using username/password authentication, add the following line
    session.setPassword( "password" );
  }
} );

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

Then configure the command with it:

clone.setTransportConfigCallback( transportConfigCallback );
Eliason answered 27/2, 2020 at 18:35 Comment(0)
S
0

If the repository is private and needs authentication, you(@Scruger) will do it using username/password with ssh for clone repository.

private UsernamePasswordCredentialsProvider configAuthentication(String user, String password) {
            return new UsernamePasswordCredentialsProvider(user, password ); 
        }

    public void clonneRepositoryWithAuthentication(String link, String directory,String branch,String user, String password){
         System.out.println("cloning repository private from bitcketebuk");
        try {
            Git.cloneRepository()//function responsible to clone repository
                                       .setURI(link)// set link to repository git
                                       .setDirectory(new File(Constants.PATH_DEFAULT + directory))//Defined the path local the cloning
                                       .setCredentialsProvider(configAuthentication(user, password))
                                       .setCloneAllBranches(true)//Defined clone all branch exists on repository
                                       .call();//execute call the clone repository git
            System.out.println("Cloning sucess.....");
        } catch (GitAPIException e) {
            System.err.println("Error Cloning repository " + link + " : "+ e.getMessage());
        }

    }
Sinus answered 20/5, 2018 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.