Repository JGit克隆存储库

Repository JGit克隆存储库,repository,clone,jgit,Repository,Clone,Jgit,我正在尝试用JGit克隆Git存储库,但我对UnsupportedCredentialItem有问题 我的代码: FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(PATH).readEnvironment().findGitDir().build(); Git git = new Git(repository);

我正在尝试用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\中的已知\u 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)....
有没有可能对这个问题输入“是”或直接跳过它


谢谢大家!

我想您应该查看github帮助:


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

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

我也遇到了同样的问题。原因是为rsa私钥设置了密码短语。当我删除此密钥的密码短语时,它在没有任何
凭证提供程序的情况下开始工作

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();
它对我有用;)

如果将用户名/密码与ssh一起使用,则可以做到这一点(如@michals,只需更少的代码)

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

如果存储库是私有的并且需要身份验证,您(@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());
        }

    }

我有一个类似的问题,虽然我的设置有点不同。把这个留在这里,以防其他人遇到类似的情况。根据本教程,我已重写了配置方法和createDefaultJSch方法:

我有点像:

@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():


不知道为什么会这样,但我从这个答案中找到了getSch(恰好是编写教程的同一个人):

我不清楚您是想进行用户名/密码身份验证还是公钥/私钥身份验证。根据本文,无论哪种方式,
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 );
clone.setTransportConfigCallback( transportConfigCallback );