Skip to content

Instantly share code, notes, and snippets.

@ederign
Forked from porcelli/jbang.java
Created Oct 4, 2021
Embed
What would you like to do?
///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