mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-27 09:06:43 +08:00
Git Operations: Remove ssh code duplication
This commit is contained in:
@ -0,0 +1,58 @@
|
||||
package com.example.journal;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.jcraft.jsch.JSch;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import com.jcraft.jsch.Session;
|
||||
|
||||
import org.eclipse.jgit.transport.JschConfigSessionFactory;
|
||||
import org.eclipse.jgit.transport.OpenSshConfig;
|
||||
import org.eclipse.jgit.util.FS;
|
||||
|
||||
public class CustomSshSessionFactory extends JschConfigSessionFactory {
|
||||
private String privateKeyPath;
|
||||
|
||||
public CustomSshSessionFactory(String privateKeyPath_) {
|
||||
privateKeyPath = privateKeyPath_;
|
||||
}
|
||||
|
||||
protected void configure(OpenSshConfig.Host host, Session session) {
|
||||
session.setConfig("StrictHostKeyChecking", "no");
|
||||
}
|
||||
|
||||
protected JSch createDefaultJSch(FS fs) throws JSchException {
|
||||
|
||||
class MyLogger implements com.jcraft.jsch.Logger {
|
||||
java.util.Hashtable name;
|
||||
|
||||
MyLogger() {
|
||||
name = new java.util.Hashtable();
|
||||
name.put(new Integer(DEBUG), "DEBUG: ");
|
||||
name.put(new Integer(INFO), "INFO: ");
|
||||
name.put(new Integer(WARN), "WARN: ");
|
||||
name.put(new Integer(ERROR), "ERROR: ");
|
||||
name.put(new Integer(FATAL), "FATAL: ");
|
||||
}
|
||||
|
||||
|
||||
public boolean isEnabled(int level) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void log(int level, String message) {
|
||||
System.err.print(name.get(new Integer(level)));
|
||||
System.err.println(message);
|
||||
}
|
||||
}
|
||||
JSch.setLogger(new MyLogger());
|
||||
|
||||
JSch defaultJSch = super.createDefaultJSch(fs);
|
||||
defaultJSch.addIdentity(privateKeyPath);
|
||||
|
||||
JSch.setConfig("PreferredAuthentications", "publickey");
|
||||
|
||||
Log.d("identityNames", defaultJSch.getIdentityNames().toString());
|
||||
return defaultJSch;
|
||||
}
|
||||
}
|
@ -11,18 +11,9 @@ import org.eclipse.jgit.api.errors.TransportException;
|
||||
import org.eclipse.jgit.transport.Transport;
|
||||
import org.eclipse.jgit.transport.SshTransport;
|
||||
|
||||
import org.eclipse.jgit.transport.JschConfigSessionFactory;
|
||||
import org.eclipse.jgit.transport.SshSessionFactory;
|
||||
import org.eclipse.jgit.transport.OpenSshConfig.Host;
|
||||
import org.eclipse.jgit.util.FS;
|
||||
|
||||
import org.eclipse.jgit.lib.TextProgressMonitor;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import com.jcraft.jsch.Session;
|
||||
import com.jcraft.jsch.*;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import io.flutter.plugin.common.MethodChannel.Result;
|
||||
@ -43,47 +34,6 @@ public class GitCloneTask extends AsyncTask<String, Void, Void> {
|
||||
Log.d("GitClone Directory", cloneDirPath);
|
||||
|
||||
try {
|
||||
final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
|
||||
protected void configure(Host host, Session session) {
|
||||
session.setConfig("StrictHostKeyChecking", "no");
|
||||
}
|
||||
|
||||
protected JSch createDefaultJSch(FS fs) throws JSchException {
|
||||
|
||||
class MyLogger implements com.jcraft.jsch.Logger {
|
||||
java.util.Hashtable name;
|
||||
|
||||
MyLogger() {
|
||||
name = new java.util.Hashtable();
|
||||
name.put(new Integer(DEBUG), "DEBUG: ");
|
||||
name.put(new Integer(INFO), "INFO: ");
|
||||
name.put(new Integer(WARN), "WARN: ");
|
||||
name.put(new Integer(ERROR), "ERROR: ");
|
||||
name.put(new Integer(FATAL), "FATAL: ");
|
||||
}
|
||||
|
||||
|
||||
public boolean isEnabled(int level) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void log(int level, String message) {
|
||||
System.err.print(name.get(new Integer(level)));
|
||||
System.err.println(message);
|
||||
}
|
||||
}
|
||||
JSch.setLogger(new MyLogger());
|
||||
|
||||
JSch defaultJSch = super.createDefaultJSch(fs);
|
||||
defaultJSch.addIdentity(privateKeyPath);
|
||||
|
||||
JSch.setConfig("PreferredAuthentications", "publickey");
|
||||
|
||||
Log.d("identityNames", defaultJSch.getIdentityNames().toString());
|
||||
return defaultJSch;
|
||||
}
|
||||
};
|
||||
|
||||
CloneCommand cloneCommand = Git.cloneRepository()
|
||||
.setURI(url)
|
||||
.setDirectory(cloneDir)
|
||||
@ -93,7 +43,7 @@ public class GitCloneTask extends AsyncTask<String, Void, Void> {
|
||||
@Override
|
||||
public void configure(Transport transport) {
|
||||
SshTransport sshTransport = (SshTransport) transport;
|
||||
sshTransport.setSshSessionFactory(sshSessionFactory);
|
||||
sshTransport.setSshSessionFactory(new CustomSshSessionFactory(privateKeyPath));
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -5,7 +5,6 @@ import android.util.Log;
|
||||
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
|
||||
import org.eclipse.jgit.api.PullCommand;
|
||||
import org.eclipse.jgit.api.TransportConfigCallback;
|
||||
@ -13,14 +12,6 @@ import org.eclipse.jgit.api.errors.TransportException;
|
||||
import org.eclipse.jgit.transport.Transport;
|
||||
import org.eclipse.jgit.transport.SshTransport;
|
||||
|
||||
import org.eclipse.jgit.transport.JschConfigSessionFactory;
|
||||
import org.eclipse.jgit.transport.SshSessionFactory;
|
||||
import org.eclipse.jgit.transport.OpenSshConfig.Host;
|
||||
import org.eclipse.jgit.util.FS;
|
||||
|
||||
import com.jcraft.jsch.Session;
|
||||
import com.jcraft.jsch.*;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import io.flutter.plugin.common.MethodChannel.Result;
|
||||
@ -41,47 +32,6 @@ public class GitPullTask extends AsyncTask<String, Void, Void> {
|
||||
Log.d("GitClone Directory", cloneDirPath);
|
||||
|
||||
try {
|
||||
final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
|
||||
protected void configure(Host host, Session session) {
|
||||
session.setConfig("StrictHostKeyChecking", "no");
|
||||
}
|
||||
|
||||
protected JSch createDefaultJSch(FS fs) throws JSchException {
|
||||
|
||||
class MyLogger implements com.jcraft.jsch.Logger {
|
||||
java.util.Hashtable name;
|
||||
|
||||
MyLogger() {
|
||||
name = new java.util.Hashtable();
|
||||
name.put(new Integer(DEBUG), "DEBUG: ");
|
||||
name.put(new Integer(INFO), "INFO: ");
|
||||
name.put(new Integer(WARN), "WARN: ");
|
||||
name.put(new Integer(ERROR), "ERROR: ");
|
||||
name.put(new Integer(FATAL), "FATAL: ");
|
||||
}
|
||||
|
||||
|
||||
public boolean isEnabled(int level) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void log(int level, String message) {
|
||||
System.err.print(name.get(new Integer(level)));
|
||||
System.err.println(message);
|
||||
}
|
||||
}
|
||||
JSch.setLogger(new MyLogger());
|
||||
|
||||
JSch defaultJSch = super.createDefaultJSch(fs);
|
||||
defaultJSch.addIdentity(privateKeyPath);
|
||||
|
||||
JSch.setConfig("PreferredAuthentications", "publickey");
|
||||
|
||||
Log.d("identityNames", defaultJSch.getIdentityNames().toString());
|
||||
return defaultJSch;
|
||||
}
|
||||
};
|
||||
|
||||
Git git = Git.open(cloneDir);
|
||||
|
||||
PullCommand pullCommand = git.pull();
|
||||
@ -89,7 +39,7 @@ public class GitPullTask extends AsyncTask<String, Void, Void> {
|
||||
@Override
|
||||
public void configure(Transport transport) {
|
||||
SshTransport sshTransport = (SshTransport) transport;
|
||||
sshTransport.setSshSessionFactory(sshSessionFactory);
|
||||
sshTransport.setSshSessionFactory(new CustomSshSessionFactory(privateKeyPath));
|
||||
}
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user