JGit克隆存储库

时间:2011-12-17 14:12:48

标签: repository clone jgit

我正在尝试使用JGit克隆Git存储库,但我遇到了UnsupportedCredentialItem的问题。

我的代码:

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

是否有可能对该问题键入“是”或只是跳过它?

谢谢!

7 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

我想你想检查一下github帮助:

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

特别是关于生成ssh密钥(ssh-keygen -t rsa -C "your_email@youremail.com")的部分。阅读适合您环境的文章,您将了解如何获得更好的配置。

答案 2 :(得分:3)

如果在ssh中使用用户名/密码,这样做(如@michals,只需更少的代码)

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

答案 3 :(得分: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();

对我有用;)

答案 4 :(得分:0)

如果存储库是私有的并且需要身份验证,那么您(@Scruger)将使用用户名 / 密码和ssh进行克隆存储库来执行此操作。

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

    }

答案 5 :(得分:0)

我有一个类似的问题,尽管我的设置有些不同。如果有人遇到类似的情况,请留在这里。根据本教程,我已经覆盖了configure方法和createDefaultJSch方法:https://www.codeaffine.com/2014/12/09/jgit-authentication/

我有类似的东西:

@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;
}

我最终将我的createdDefaultJSch方法更改为getSch(添加了适当的参数)并添加了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
} 

不知道为什么要这样做,但是我从这个答案中找到了getSch东西(碰巧是由写教程的那个人):Using Keys with JGit to Access a Git Repository Securely

答案 6 :(得分:0)

我不清楚您要进行用户名/密码认证还是公钥/私钥认证。无论哪种方式,都不会使用CredentialsProvider。您需要配置传输。首先,创建传输配置回调:

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

然后使用它配置命令:

clone.setTransportConfigCallback( transportConfigCallback );