I am using JGit to create and clone a repository (the remote is a bitbucket repo - and yes, I added my deployment key). In essence, I:
- Create repository
- Disable JSch strict hostkey checking
- Set JSch credentials (my ssh key is password protected, hence JSch will fail if I don't specify a password)
- Clone the repository
My code is as follows:
// 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();
Problem: The clone fails with the following error
org.eclipse.jgit.api.errors.TransportException: git@bitbucket.org:username/blah.git: reject 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)