i using jgit create , clone repository (the remote bitbucket repo - , yes, added deployment key). in essence, i:
- create repository
- disable jsch strict hostkey checking
- set jsch credentials (my ssh key password protected, hence jsch fail if don't specify password)
- clone repository
my code 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 { (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: clone fails following error
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)
i searching answer few references out there. wanted contribute worked me. trying use jgit query gerrit via ssh command console. work, need provide passphrase , ssh private key.
to set connection, first must configure jsch first:
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 private key file if (stringutils.isnotempty(data.getsshkey())) { jsch.addidentity("identityname", data.getsshkey() .getbytes(), null, data.getsshpassphrase() .getbytes()); } return jsch; } };
now, unable use traditional methods using session private key. git.clonerepository() not work. have setup transport , assign session factory it:
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 , make change: git.add().addfilepattern("somefile.txt").call(); revcommit revcommit = git.commit().setmessage("change.").call(); //last, push 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);
there have it, short small circle tutorial connecting via ssh remote repository, fetch/checkout, make change, , push upstream. hope saves others time trying understand jgit better.
Comments
Post a Comment