Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was searching for an answer to this as well with very few references out there. I wanted to contribute what eventually worked for me. I was trying to use jGit to query Gerrit via the ssh command console. For that to work, you need to provide a passphrase and ssh private key.</p> <p>To set up the connection, you first must configure JSch first:</p> <pre><code> 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; } }; </code></pre> <p>Now, I was unable to use traditional methods for using the session with the private key. git.cloneRepository() will not work. You have to setup a transport and assign the session factory to it:</p> <pre><code>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&lt;RemoteRefUpdate&gt; list = new ArrayList&lt;RemoteRefUpdate&gt;(); list.add(rru); PushResult r = transport.push(monitor, list); </code></pre> <p>There you have it, a short small circle tutorial for connecting via ssh to remote repository, fetch/checkout, make a change, and push back upstream. I hope this saves others time trying to understand jGit better.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload