package org.dstadler.jgit.porcelain;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.LsRemoteCommand;
import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.transport.SshSessionFactory;
import org.eclipse.jgit.transport.SshTransport;
import org.eclipse.jgit.transport.Transport;
import org.eclipse.jgit.util.FS;

import java.util.Map;

/**
 * Simple snippet which shows how to fetch commits from a remote Git repository
 * via ssh protocol authentication.
 * <p>
 * Created by zhengmaoshao on 2019/4/3 上午12:38
 */
public class FetchRemoteCommitsWithSshAuth
{

    private static final String REMOTE_URL = "ssh://<user>@<host>:22/<path-to-remote-repo>/";

    public static void main(String[] args) throws GitAPIException {

        final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory()
        {
            @Override
            protected void configure(OpenSshConfig.Host host, Session session) {

            }

            @Override
            protected JSch createDefaultJSch(FS fs) throws JSchException {

                JSch defaultJSch = super.createDefaultJSch(fs);
                defaultJSch.addIdentity("/path/to/private_key");
                return defaultJSch;
            }
        };

        LsRemoteCommand lsRemoteCommand = Git.lsRemoteRepository();
        lsRemoteCommand.setRemote(REMOTE_URL);
        lsRemoteCommand.setTransportConfigCallback(new TransportConfigCallback()
        {
            @Override
            public void configure(Transport transport) {
                SshTransport sshTransport = (SshTransport) transport;
                sshTransport.setSshSessionFactory(sshSessionFactory);
            }
        });

        final Map<String, Ref> map = lsRemoteCommand.setHeads(true)
                .setTags(true)
                .callAsMap();

        for (Map.Entry<String, Ref> entry : map.entrySet()) {
            System.out.println("Key: " + entry.getKey()/*eg.refs/heads/develop*/ + ", Ref: " + entry.getValue().getObjectId().getName()/*eg.e16c937848d5c1ad50ef163003c7b076103f7e37*/);
        }

    }
}

@@ -778,16 +778,137 @@
 cation.%0A
+ *%0A * You need to provide proper values for the URL of the Git repository and%0A * the location of the private key file%0A *%0A
  * %3Cp%3E%0A 
@@ -1003,11 +1003,10 @@
 Auth
-%0A%7B%0A
+ %7B
 %0A   
@@ -1095,16 +1095,86 @@
 repo%3E/%22;
+%0A    private static final String PRIVATE_KEY = %22/path/to/private_key%22;
 %0A%0A    pu
@@ -1318,24 +1318,16 @@
 actory()
-%0A       
  %7B%0A     
@@ -1424,17 +1424,16 @@
 sion) %7B%0A
-%0A
         
@@ -1528,33 +1528,32 @@
 JSchException %7B%0A
-%0A
                 
@@ -1640,38 +1640,27 @@
 dentity(
-%22/path/to/private_key%22
+PRIVATE_KEY
 );%0A     
@@ -2551,21 +2551,20 @@
 ;%0A        %7D%0A
-%0A
     %7D%0A%7D%0A

Reformat and update comment of new snippet