2011-12-17 121 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(); 
}