Skip to content

Instantly share code, notes, and snippets.

@porcelli
Created September 1, 2021 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save porcelli/9d41a6f42d0aa6ab7f8a6b1b4d9ad51f to your computer and use it in GitHub Desktop.
Save porcelli/9d41a6f42d0aa6ab7f8a6b1b4d9ad51f to your computer and use it in GitHub Desktop.
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.eclipse.jgit:org.eclipse.jgit:5.4.0.201906121030-r
//DEPS org.slf4j:slf4j-nop:1.7.31
package me.porcelli.jgit;
import com.jcraft.jsch.*;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.SshTransport;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.util.FS;
public class jbang {
public static void main(String... args) throws Exception {
CloneCommand cloneCommand = Git.cloneRepository();
cloneCommand.setURI( args[0] );
cloneCommand.setTransportConfigCallback(transport -> {
final SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(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;
}
});
});
cloneCommand.call();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment