JGit clone repository

2019-02-09 17:07发布

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://git@github.com: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://git@github.com: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!

6条回答
趁早两清
2楼-- · 2019-02-09 17:31

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 "your_email@youremail.com"). Read the article for your environment, and you'll understand how to get a better configuration.

查看更多
欢心
3楼-- · 2019-02-09 17:34

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());
        }

    }
查看更多
来,给爷笑一个
4楼-- · 2019-02-09 17:43

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

查看更多
Ridiculous、
5楼-- · 2019-02-09 17:44

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.

查看更多
\"骚年 ilove
6楼-- · 2019-02-09 17:46

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();
}
查看更多
等我变得足够好
7楼-- · 2019-02-09 17:51

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 ;)

查看更多
登录 后发表回答