JGit を使用してリポジトリの作成とクローンを作成しています (リモートは bitbucket リポジトリです - はい、デプロイ キーを追加しました)。本質的に、私は:
- リポジトリを作成
- JSch の厳密なホストキー チェックを無効にする
- JSch 資格情報を設定します (私の ssh キーはパスワードで保護されているため、パスワードを指定しないと JSch は失敗します)
- リポジトリをクローンする
私のコードは次のとおりです。
// 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)