From b7e2990e47b5e4f869fc66bd27d2ae4c20ee076b Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 7 Jan 2019 15:15:14 +0100 Subject: [PATCH] Add support for GitPull --- .../java/com/example/journal/GitPullTask.java | 114 ++++++++++++++++++ .../com/example/journal/MainActivity.java | 102 ++++++++++------ lib/gitapp.dart | 22 +++- 3 files changed, 202 insertions(+), 36 deletions(-) create mode 100644 android/app/src/main/java/com/example/journal/GitPullTask.java diff --git a/android/app/src/main/java/com/example/journal/GitPullTask.java b/android/app/src/main/java/com/example/journal/GitPullTask.java new file mode 100644 index 00000000..901a7613 --- /dev/null +++ b/android/app/src/main/java/com/example/journal/GitPullTask.java @@ -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 { + 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; + } +} diff --git a/android/app/src/main/java/com/example/journal/MainActivity.java b/android/app/src/main/java/com/example/journal/MainActivity.java index 3e4f742a..c7754ef4 100644 --- a/android/app/src/main/java/com/example/journal/MainActivity.java +++ b/android/app/src/main/java/com/example/journal/MainActivity.java @@ -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"); + } + } + ); + } } diff --git a/lib/gitapp.dart b/lib/gitapp.dart index 10fe7bf1..e3716b37 100644 --- a/lib/gitapp.dart +++ b/lib/gitapp.dart @@ -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}'."); + } +}