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) at 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 に答える