2

我正在使用 JGit 创建和克隆一个存储库(远程是一个 bitbucket 存储库 - 是的,我添加了我的部署密钥)。本质上,我:

  1. 创建存储库
  2. 禁用 JSch 严格的主机密钥检查
  3. 设置 JSch 凭据(我的 ssh 密钥受密码保护,因此如果我不指定密码,JSch 将失败)
  4. 克隆存储库

我的代码如下:

  // Create repository
        File gitDir = new File(localPath);
        FileRepository repo = new FileRepository(gitDir);
        repo.create();

        // Add remote origin
        SshSessionFactory.setInstance(new JschConfigSessionFactory() {
            public void configure(Host hc, Session session) {
                session.setConfig("StrictHostKeyChecking", "no");
            }
        });
        JschConfigSessionFactory sessionFactory = new JschConfigSessionFactory() {
            @Override
            protected void configure(OpenSshConfig.Host hc, Session session) {
                CredentialsProvider provider = new CredentialsProvider() {
                    @Override
                    public boolean isInteractive() {
                        return false;
                    }

                    @Override
                    public boolean supports(CredentialItem... items) {
                        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("myPassword");
                            }
                        }
                        return true;
                    }
                };
                UserInfo userInfo = new CredentialsProviderUserInfo(session, provider);
                session.setUserInfo(userInfo);
            }
        };
        SshSessionFactory.setInstance(sessionFactory);
        git = org.eclipse.jgit.api.Git.cloneRepository()
                .setURI(remote)
                .setDirectory(new File(localPath + "/git"))
                .call();

问题:克隆失败并出现以下错误

org.eclipse.jgit.api.errors.TransportException: git@bitbucket.org:username/blah.git: 拒绝 HostKey: bitbucket.org at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:137) at org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:178) 在 org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:125)

4

1 回答 1

1

我也在寻找这个问题的答案,那里的参考资料很少。我想贡献最终对我有用的东西。我试图使用 jGit 通过 ssh 命令控制台查询 Gerrit。为此,您需要提供密码和 ssh 私钥。

要建立连接,首先必须先配置 JSch:

    SshSessionFactory factory = new JschConfigSessionFactory() {

        public void configure(Host hc, Session session) {
            session.setConfig("StrictHostKeyChecking", "no");
        }

        @Override
        protected JSch
                        getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException {
            JSch jsch = super.getJSch(hc, fs);
            jsch.removeAllIdentity();
            //Where getSshKey returns content of the private key file
            if (StringUtils.isNotEmpty(data.getSshKey())) {
                jsch.addIdentity("identityName", data.getSshKey()
                    .getBytes(), null, data.getSshPassphrase()
                    .getBytes());
            }
            return jsch;
        }
    };

现在,我无法使用传统方法来使用带有私钥的会话。git.cloneRepository() 将不起作用。您必须设置传输并将会话工厂分配给它:

String targetRevision = "refs/head/master"; //or "refs/meta/config", "refs/for/master"
Transport transport = null;
transport = Transport.open(git.getRepository(), url);
((SshTransport) transport).setSshSessionFactory(factory);
RefSpec refSpec = new RefSpec().setForceUpdate(true).setSourceDestination(
                        targetRevision, targetRevision);
transport.fetch(monitor, Arrays.asList(refSpec));

CheckoutCommand co = git.checkout();
co.setName(targetRevision);
co.call();

//Add and make a change:
git.add().addFilepattern("somefile.txt").call();
RevCommit revCommit = git.commit().setMessage("Change.").call();

//Last, push the update:
RemoteRefUpdate rru =new RemoteRefUpdate(git.getRepository(), revCommit.name(),
                        targetRevision, true, null, null);
List<RemoteRefUpdate> list = new ArrayList<RemoteRefUpdate>();
list.add(rru);
PushResult r = transport.push(monitor, list);

有了它,一个简短的小圆圈教程,用于通过 ssh 连接到远程存储库、获取/签出、进行更改并推回上游。我希望这可以节省其他人尝试更好地理解 jGit 的时间。

于 2013-10-10T16:32:18.143 回答