我正在使用JGit创建和克隆存储库(远程是一个bitbucket repo - 是的,我添加了我的部署密钥)。 从本质上讲,我:
我的代码如下:
// 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:reject HostKey:bitbucket.org 在org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:137) 在org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:178) 在org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:125)
答案 0 :(得分: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。