2011-12-17 124 views
7

我想克隆与JGit的Git存储库,我有UnsupportedCredentialItem的问题。JGit克隆库

我的代码:

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

它会出现例外:

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

但是,如果我的.ssh删除文件的known_hosts \它会出现不同的异常

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

有对该问题输入“是”还是只是略过它的可能性?

谢谢!

回答

4

我想如果你用用户名和密码登录,你需要https。对于ssh,你需要一个与github上记录的公钥匹配的公钥。

2

我有同样的问题。原因是为rsa私钥设置了密码短语。当我删除该密钥的密码时,它开始工作,没有任何CredentialsProvider

UsernamePasswordCredentialsProvider可能不支持密码。如果你想有密码设定,你可以定义你自己CredentialProvider,这将支持它,例如:

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

这对我的作品;)

3

这将做到这一点(如@michals,只有更少的代码)如果使用用户名/密码ssh

public void gitClone() throws GitAPIException { 
    final File localPath = new File("./TestRepo"); 
    Git.cloneRepository() 
     .setURI(REMOTE_URL) 
     .setDirectory(localPath) 
     .setCredentialsProvider(new UsernamePasswordCredentialsProvider("***", "***")) 
     .call(); 
}