mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-27 17:29:50 +08:00
Add support for GitPull
This commit is contained in:
114
android/app/src/main/java/com/example/journal/GitPullTask.java
Normal file
114
android/app/src/main/java/com/example/journal/GitPullTask.java
Normal file
@ -0,0 +1,114 @@
|
||||
package com.example.journal;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
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;
|
||||
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;
|
||||
|
||||
public class GitPullTask extends AsyncTask<String, Void, Void> {
|
||||
private final static String TAG = "GitPull";
|
||||
private Result result;
|
||||
|
||||
public GitPullTask(Result _result) {
|
||||
result = _result;
|
||||
}
|
||||
|
||||
protected Void doInBackground(String... params) {
|
||||
String cloneDirPath = params[0];
|
||||
final String privateKeyPath = params[1];
|
||||
|
||||
File cloneDir = new File(cloneDirPath);
|
||||
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();
|
||||
pullCommand.setTransportConfigCallback(new TransportConfigCallback() {
|
||||
@Override
|
||||
public void configure(Transport transport) {
|
||||
SshTransport sshTransport = (SshTransport) transport;
|
||||
sshTransport.setSshSessionFactory(sshSessionFactory);
|
||||
}
|
||||
});
|
||||
|
||||
pullCommand.call();
|
||||
} catch (TransportException e) {
|
||||
Log.d(TAG, e.toString());
|
||||
result.error("FAILED", e.toString(), null);
|
||||
return null;
|
||||
} catch (GitAPIException e) {
|
||||
Log.d(TAG, e.toString());
|
||||
result.error("FAILED", e.toString(), null);
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
Log.d(TAG, e.toString());
|
||||
result.error("FAILED", e.toString(), null);
|
||||
return null;
|
||||
}
|
||||
|
||||
result.success(null);
|
||||
return null;
|
||||
}
|
||||
}
|
@ -14,52 +14,84 @@ import io.flutter.plugin.common.MethodChannel;
|
||||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
|
||||
import io.flutter.plugin.common.MethodChannel.Result;
|
||||
|
||||
|
||||
// For EventChannel
|
||||
import io.flutter.plugin.common.EventChannel;
|
||||
|
||||
import io.flutter.util.PathUtils;
|
||||
|
||||
import org.eclipse.jgit.api.CloneCommand;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
|
||||
public class MainActivity extends FlutterActivity {
|
||||
private static final String CHANNEL = "gitjournal.io/git";
|
||||
private static final String CHANNEL = "gitjournal.io/git";
|
||||
private static final String STREAM_CLONE_CHANNEL = "gitjournal.io/gitClone";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
GeneratedPluginRegistrant.registerWith(this);
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
GeneratedPluginRegistrant.registerWith(this);
|
||||
|
||||
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
|
||||
new MethodCallHandler() {
|
||||
@Override
|
||||
public void onMethodCall(MethodCall call, Result result) {
|
||||
if (call.method.equals("gitClone")) {
|
||||
String cloneUrl = call.argument("cloneUrl");
|
||||
String folderName = call.argument("folderName");
|
||||
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
|
||||
new MethodCallHandler() {
|
||||
@Override
|
||||
public void onMethodCall(MethodCall call, Result result) {
|
||||
if (call.method.equals("gitClone")) {
|
||||
String cloneUrl = call.argument("cloneUrl");
|
||||
String folderName = call.argument("folderName");
|
||||
|
||||
if (cloneUrl.isEmpty() || folderName.isEmpty()) {
|
||||
result.error("Invalid Parameters", "Arguments Invalid", null);
|
||||
return;
|
||||
}
|
||||
if (cloneUrl.isEmpty() || folderName.isEmpty()) {
|
||||
result.error("Invalid Parameters", "Arguments Invalid", null);
|
||||
return;
|
||||
}
|
||||
|
||||
String filesDir = PathUtils.getFilesDir(getApplicationContext());
|
||||
String cloneLocation = filesDir + "/" + folderName;
|
||||
String filesDir = PathUtils.getFilesDir(getApplicationContext());
|
||||
String cloneLocation = filesDir + "/" + folderName;
|
||||
|
||||
final String privateKeyPath = filesDir + "/ssh/id_rsa";
|
||||
new GitCloneTask(result).execute(cloneUrl, cloneLocation, privateKeyPath);
|
||||
return;
|
||||
}
|
||||
final String privateKeyPath = filesDir + "/ssh/id_rsa";
|
||||
new GitCloneTask(result).execute(cloneUrl, cloneLocation, privateKeyPath);
|
||||
return;
|
||||
}
|
||||
|
||||
if (call.method.equals("generateSSHKeys")) {
|
||||
String appFilesDir = PathUtils.getFilesDir(getApplicationContext());
|
||||
String sshKeysLocation = appFilesDir + "/ssh";
|
||||
if (call.method.equals("gitPull")) {
|
||||
String folderName = call.argument("folderName");
|
||||
|
||||
new GenerateSSHKeysTask(result).execute(sshKeysLocation);
|
||||
return;
|
||||
}
|
||||
if (folderName.isEmpty()) {
|
||||
result.error("Invalid Parameters", "Arguments Invalid", null);
|
||||
return;
|
||||
}
|
||||
|
||||
result.notImplemented();
|
||||
}
|
||||
});
|
||||
}
|
||||
String filesDir = PathUtils.getFilesDir(getApplicationContext());
|
||||
String cloneLocation = filesDir + "/" + folderName;
|
||||
|
||||
final String privateKeyPath = filesDir + "/ssh/id_rsa";
|
||||
new GitPullTask(result).execute(cloneLocation, privateKeyPath);
|
||||
return;
|
||||
}
|
||||
|
||||
if (call.method.equals("generateSSHKeys")) {
|
||||
String appFilesDir = PathUtils.getFilesDir(getApplicationContext());
|
||||
String sshKeysLocation = appFilesDir + "/ssh";
|
||||
|
||||
new GenerateSSHKeysTask(result).execute(sshKeysLocation);
|
||||
return;
|
||||
}
|
||||
|
||||
result.notImplemented();
|
||||
}
|
||||
});
|
||||
|
||||
new EventChannel(getFlutterView(), STREAM_CLONE_CHANNEL).setStreamHandler(
|
||||
new EventChannel.StreamHandler() {
|
||||
@Override
|
||||
public void onListen(Object args, final EventChannel.EventSink events) {
|
||||
Log.w("CloneStream", "adding listener");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel(Object args) {
|
||||
Log.w("CloneStream", "cancelling listener");
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -32,6 +32,12 @@ buildGitButtons() {
|
||||
await gitClone();
|
||||
},
|
||||
),
|
||||
RaisedButton(
|
||||
child: Text("Git Pull"),
|
||||
onPressed: () async {
|
||||
await gitPull();
|
||||
},
|
||||
),
|
||||
RaisedButton(
|
||||
child: Text("New File"),
|
||||
onPressed: () {
|
||||
@ -47,7 +53,7 @@ Future gitClone() async {
|
||||
print("Going to git clone");
|
||||
try {
|
||||
await platform.invokeMethod('gitClone', {
|
||||
'cloneUrl': "root@bcn.vhanda.in:git/notes",
|
||||
'cloneUrl': "root@bcn.vhanda.in:git/test",
|
||||
'folderName': "journal",
|
||||
});
|
||||
print("Done");
|
||||
@ -66,3 +72,17 @@ Future generateSSHKeys() async {
|
||||
print("Failed to generateSSHKeys: '${e.message}'.");
|
||||
}
|
||||
}
|
||||
|
||||
Future gitPull() async {
|
||||
const platform = const MethodChannel('gitjournal.io/git');
|
||||
|
||||
print("Going to git pull");
|
||||
try {
|
||||
await platform.invokeMethod('gitPull', {
|
||||
'folderName': "journal",
|
||||
});
|
||||
print("Done");
|
||||
} on PlatformException catch (e) {
|
||||
print("gitPull Failed: '${e.message}'.");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user