Git: Implement all the remaining operations

Many of these do not actually need an AsyncTask, but for now I'm just
following this pattern since I don't know if blocking on the receiever
thread will result in the app's UI being blocked.
This commit is contained in:
Vishesh Handa
2019-01-07 15:51:42 +01:00
parent 9dc88455e9
commit 3cdb454482
6 changed files with 284 additions and 27 deletions

View File

@ -0,0 +1,55 @@
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.api.AddCommand;
import org.eclipse.jgit.api.errors.TransportException;
import java.io.File;
import io.flutter.plugin.common.MethodChannel.Result;
public class GitAddTask extends AsyncTask<String, Void, Void> {
private final static String TAG = "GitAdd";
private Result result;
public GitAddTask(Result _result) {
result = _result;
}
protected Void doInBackground(String... params) {
final String cloneDirPath = params[0];
final String filePattern = params[1];
File cloneDir = new File(cloneDirPath);
Log.d("GitClone Directory", cloneDirPath);
try {
Git git = Git.open(cloneDir);
AddCommand addCommand = git.add();
addCommand.addFilepattern(filePattern);
addCommand.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;
}
}

View File

@ -19,6 +19,7 @@ import java.io.File;
import io.flutter.plugin.common.MethodChannel.Result;
public class GitCloneTask extends AsyncTask<String, Void, Void> {
private final static String TAG = "GitClone";
private Result result;
public GitCloneTask(Result _result) {
@ -49,15 +50,15 @@ public class GitCloneTask extends AsyncTask<String, Void, Void> {
cloneCommand.call();
} catch (TransportException e) {
Log.d("gitClone", e.toString());
Log.d(TAG, e.toString());
result.error("FAILED", e.toString(), null);
return null;
} catch (GitAPIException e) {
Log.d("gitClone", e.toString());
Log.d(TAG, e.toString());
result.error("FAILED", e.toString(), null);
return null;
} catch (Exception e) {
Log.d("gitClone", e.toString());
Log.d(TAG, e.toString());
result.error("FAILED", e.toString(), null);
return null;
}

View File

@ -0,0 +1,58 @@
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.api.CommitCommand;
import org.eclipse.jgit.api.errors.TransportException;
import java.io.File;
import io.flutter.plugin.common.MethodChannel.Result;
public class GitCommitTask extends AsyncTask<String, Void, Void> {
private final static String TAG = "GitCommit";
private Result result;
public GitCommitTask(Result _result) {
result = _result;
}
protected Void doInBackground(String... params) {
final String cloneDirPath = params[0];
final String authorName = params[1];
final String authorEmail = params[2];
final String message = params[3];
File cloneDir = new File(cloneDirPath);
Log.d("GitClone Directory", cloneDirPath);
try {
Git git = Git.open(cloneDir);
CommitCommand commitCommand = git.commit();
commitCommand.setAuthor(authorName, authorEmail);
commitCommand.setMessage(message);
commitCommand.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;
}
}

View File

@ -0,0 +1,64 @@
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.api.PushCommand;
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 java.io.File;
import io.flutter.plugin.common.MethodChannel.Result;
public class GitPushTask extends AsyncTask<String, Void, Void> {
private final static String TAG = "GitPush";
private Result result;
public GitPushTask(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 {
Git git = Git.open(cloneDir);
PushCommand pushCommand = git.push();
pushCommand.setTransportConfigCallback(new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(new CustomSshSessionFactory(privateKeyPath));
}
});
pushCommand.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;
}
}

View File

@ -34,6 +34,10 @@ public class MainActivity extends FlutterActivity {
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
final String filesDir = PathUtils.getFilesDir(getApplicationContext());
final String sshKeysLocation = filesDir + "/ssh";
final String privateKeyPath = sshKeysLocation + "/id_rsa";
if (call.method.equals("gitClone")) {
String cloneUrl = call.argument("cloneUrl");
String folderName = call.argument("folderName");
@ -43,15 +47,11 @@ public class MainActivity extends FlutterActivity {
return;
}
String filesDir = PathUtils.getFilesDir(getApplicationContext());
String cloneLocation = filesDir + "/" + folderName;
final String privateKeyPath = filesDir + "/ssh/id_rsa";
new GitCloneTask(result).execute(cloneUrl, cloneLocation, privateKeyPath);
return;
}
if (call.method.equals("gitPull")) {
} else if (call.method.equals("gitPull")) {
String folderName = call.argument("folderName");
if (folderName.isEmpty()) {
@ -59,18 +59,51 @@ public class MainActivity extends FlutterActivity {
return;
}
String filesDir = PathUtils.getFilesDir(getApplicationContext());
String cloneLocation = filesDir + "/" + folderName;
final String privateKeyPath = filesDir + "/ssh/id_rsa";
new GitPullTask(result).execute(cloneLocation, privateKeyPath);
return;
}
} else if (call.method.equals("gitPush")) {
String folderName = call.argument("folderName");
if (call.method.equals("generateSSHKeys")) {
String appFilesDir = PathUtils.getFilesDir(getApplicationContext());
String sshKeysLocation = appFilesDir + "/ssh";
if (folderName.isEmpty()) {
result.error("Invalid Parameters", "Arguments Invalid", null);
return;
}
String cloneLocation = filesDir + "/" + folderName;
new GitPushTask(result).execute(cloneLocation, privateKeyPath);
return;
} else if (call.method.equals("gitAdd")) {
String folderName = call.argument("folderName");
String filePattern = call.argument("filePattern");
if (folderName.isEmpty() || filePattern.isEmpty()) {
result.error("Invalid Parameters", "Arguments Invalid", null);
return;
}
String cloneLocation = filesDir + "/" + folderName;
new GitAddTask(result).execute(cloneLocation, filePattern);
return;
} else if (call.method.equals("gitCommit")) {
String folderName = call.argument("folderName");
String authorName = call.argument("authorName");
String authorEmail = call.argument("authorEmail");
String message = call.argument("message");
if (folderName.isEmpty() || authorName.isEmpty() || authorEmail.isEmpty() || message.isEmpty()) {
result.error("Invalid Parameters", "Arguments Invalid", null);
return;
}
String cloneLocation = filesDir + "/" + folderName;
new GitCommitTask(result).execute(cloneLocation, authorName, authorEmail, message);
return;
} else if (call.method.equals("generateSSHKeys")) {
new GenerateSSHKeysTask(result).execute(sshKeysLocation);
return;
}

View File

@ -22,27 +22,27 @@ buildGitButtons() {
return <Widget>[
RaisedButton(
child: Text("Generate Keys"),
onPressed: () async {
await generateSSHKeys();
},
onPressed: generateSSHKeys,
),
RaisedButton(
child: Text("Git Clone"),
onPressed: () async {
await gitClone();
},
onPressed: gitClone,
),
RaisedButton(
child: Text("Git Pull"),
onPressed: () async {
await gitPull();
},
onPressed: gitPull,
),
RaisedButton(
child: Text("New File"),
onPressed: () {
print("FOO");
},
child: Text("Git Add"),
onPressed: gitAdd,
),
RaisedButton(
child: Text("Git Push"),
onPressed: gitPush,
),
RaisedButton(
child: Text("Git Commit"),
onPressed: gitCommit,
),
];
}
@ -86,3 +86,49 @@ Future gitPull() async {
print("gitPull Failed: '${e.message}'.");
}
}
Future gitAdd() async {
const platform = const MethodChannel('gitjournal.io/git');
print("Going to git add");
try {
await platform.invokeMethod('gitAdd', {
'folderName': "journal",
'filePattern': ".",
});
print("Done");
} on PlatformException catch (e) {
print("gitAdd Failed: '${e.message}'.");
}
}
Future gitPush() async {
const platform = const MethodChannel('gitjournal.io/git');
print("Going to git push");
try {
await platform.invokeMethod('gitPush', {
'folderName': "journal",
});
print("Done");
} on PlatformException catch (e) {
print("gitPush Failed: '${e.message}'.");
}
}
Future gitCommit() async {
const platform = const MethodChannel('gitjournal.io/git');
print("Going to git commit");
try {
await platform.invokeMethod('gitCommit', {
'folderName': "journal",
'authorName': "Vishesh Handa",
'authorEmail': "noemail@example.com",
'message': "Default message from GitJournal",
});
print("Done");
} on PlatformException catch (e) {
print("gitCommit Failed: '${e.message}'.");
}
}