-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractGit.java
87 lines (70 loc) · 2.87 KB
/
AbstractGit.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.transport.*;
import org.eclipse.jgit.util.FS;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Callable;
abstract class AbstractGit implements Callable<Integer> {
private static final String SSH_ID_RSA_DEFAULT = ".ssh/id_rsa";
@Option(names = {"-i", "--identity"}, description = "Identity file in PEM format (default: ~/" + SSH_ID_RSA_DEFAULT + ")")
File identityFile = new File(System.getProperty("user.home"), SSH_ID_RSA_DEFAULT);
@Option(names = {"-b", "--branch"}, description = "Branch name")
String branch;
@Parameters(description = "The repository to clone from")
String repository;
@Override
public Integer call() throws Exception {
final CloneCommand cloneCommand = Git.cloneRepository()
.setURI(repository)
.setDirectory(getCloneCommandDirectory())
.setBranch(branch)
.setBare(isBare())
.setNoCheckout(isNoCheckout());
if (!repository.startsWith("https://")) {
cloneCommand.setTransportConfigCallback(new SshTransportConfigCallback());
}
cloneCommand.call();
return 0;
}
public abstract File getCloneDirectory() throws Exception;
public boolean isBare() {
return false;
}
public boolean isNoCheckout() {
return false;
}
private File getCloneCommandDirectory() throws Exception {
File cloneDirectory = getCloneDirectory();
if (cloneDirectory == null) {
cloneDirectory = new File(".");
} else if (!cloneDirectory.exists() && !cloneDirectory.mkdirs()) {
throw new IOException("Failed to create directory: " + cloneDirectory);
}
return cloneDirectory;
}
private class SshTransportConfigCallback implements TransportConfigCallback {
private final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
@Override
protected JSch createDefaultJSch(final FS fs) throws JSchException {
final JSch jSch = super.createDefaultJSch(fs);
jSch.addIdentity(identityFile.getAbsolutePath());
return jSch;
}
@Override
protected void configure(final OpenSshConfig.Host hc, final Session session) {
session.setConfig("StrictHostKeyChecking", "no");
}
};
@Override
public void configure(final Transport transport) {
((SshTransport) transport).setSshSessionFactory(sshSessionFactory);
}
}
}