mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-28 18:03:14 +08:00
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:
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -19,6 +19,7 @@ import java.io.File;
|
|||||||
import io.flutter.plugin.common.MethodChannel.Result;
|
import io.flutter.plugin.common.MethodChannel.Result;
|
||||||
|
|
||||||
public class GitCloneTask extends AsyncTask<String, Void, Void> {
|
public class GitCloneTask extends AsyncTask<String, Void, Void> {
|
||||||
|
private final static String TAG = "GitClone";
|
||||||
private Result result;
|
private Result result;
|
||||||
|
|
||||||
public GitCloneTask(Result _result) {
|
public GitCloneTask(Result _result) {
|
||||||
@ -49,15 +50,15 @@ public class GitCloneTask extends AsyncTask<String, Void, Void> {
|
|||||||
|
|
||||||
cloneCommand.call();
|
cloneCommand.call();
|
||||||
} catch (TransportException e) {
|
} catch (TransportException e) {
|
||||||
Log.d("gitClone", e.toString());
|
Log.d(TAG, e.toString());
|
||||||
result.error("FAILED", e.toString(), null);
|
result.error("FAILED", e.toString(), null);
|
||||||
return null;
|
return null;
|
||||||
} catch (GitAPIException e) {
|
} catch (GitAPIException e) {
|
||||||
Log.d("gitClone", e.toString());
|
Log.d(TAG, e.toString());
|
||||||
result.error("FAILED", e.toString(), null);
|
result.error("FAILED", e.toString(), null);
|
||||||
return null;
|
return null;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.d("gitClone", e.toString());
|
Log.d(TAG, e.toString());
|
||||||
result.error("FAILED", e.toString(), null);
|
result.error("FAILED", e.toString(), null);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -34,6 +34,10 @@ public class MainActivity extends FlutterActivity {
|
|||||||
new MethodCallHandler() {
|
new MethodCallHandler() {
|
||||||
@Override
|
@Override
|
||||||
public void onMethodCall(MethodCall call, Result result) {
|
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")) {
|
if (call.method.equals("gitClone")) {
|
||||||
String cloneUrl = call.argument("cloneUrl");
|
String cloneUrl = call.argument("cloneUrl");
|
||||||
String folderName = call.argument("folderName");
|
String folderName = call.argument("folderName");
|
||||||
@ -43,15 +47,11 @@ public class MainActivity extends FlutterActivity {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String filesDir = PathUtils.getFilesDir(getApplicationContext());
|
|
||||||
String cloneLocation = filesDir + "/" + folderName;
|
String cloneLocation = filesDir + "/" + folderName;
|
||||||
|
|
||||||
final String privateKeyPath = filesDir + "/ssh/id_rsa";
|
|
||||||
new GitCloneTask(result).execute(cloneUrl, cloneLocation, privateKeyPath);
|
new GitCloneTask(result).execute(cloneUrl, cloneLocation, privateKeyPath);
|
||||||
return;
|
return;
|
||||||
}
|
} else if (call.method.equals("gitPull")) {
|
||||||
|
|
||||||
if (call.method.equals("gitPull")) {
|
|
||||||
String folderName = call.argument("folderName");
|
String folderName = call.argument("folderName");
|
||||||
|
|
||||||
if (folderName.isEmpty()) {
|
if (folderName.isEmpty()) {
|
||||||
@ -59,18 +59,51 @@ public class MainActivity extends FlutterActivity {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String filesDir = PathUtils.getFilesDir(getApplicationContext());
|
|
||||||
String cloneLocation = filesDir + "/" + folderName;
|
String cloneLocation = filesDir + "/" + folderName;
|
||||||
|
|
||||||
final String privateKeyPath = filesDir + "/ssh/id_rsa";
|
|
||||||
new GitPullTask(result).execute(cloneLocation, privateKeyPath);
|
new GitPullTask(result).execute(cloneLocation, privateKeyPath);
|
||||||
return;
|
return;
|
||||||
}
|
} else if (call.method.equals("gitPush")) {
|
||||||
|
String folderName = call.argument("folderName");
|
||||||
|
|
||||||
if (call.method.equals("generateSSHKeys")) {
|
if (folderName.isEmpty()) {
|
||||||
String appFilesDir = PathUtils.getFilesDir(getApplicationContext());
|
result.error("Invalid Parameters", "Arguments Invalid", null);
|
||||||
String sshKeysLocation = appFilesDir + "/ssh";
|
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);
|
new GenerateSSHKeysTask(result).execute(sshKeysLocation);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -22,27 +22,27 @@ buildGitButtons() {
|
|||||||
return <Widget>[
|
return <Widget>[
|
||||||
RaisedButton(
|
RaisedButton(
|
||||||
child: Text("Generate Keys"),
|
child: Text("Generate Keys"),
|
||||||
onPressed: () async {
|
onPressed: generateSSHKeys,
|
||||||
await generateSSHKeys();
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
RaisedButton(
|
RaisedButton(
|
||||||
child: Text("Git Clone"),
|
child: Text("Git Clone"),
|
||||||
onPressed: () async {
|
onPressed: gitClone,
|
||||||
await gitClone();
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
RaisedButton(
|
RaisedButton(
|
||||||
child: Text("Git Pull"),
|
child: Text("Git Pull"),
|
||||||
onPressed: () async {
|
onPressed: gitPull,
|
||||||
await gitPull();
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
RaisedButton(
|
RaisedButton(
|
||||||
child: Text("New File"),
|
child: Text("Git Add"),
|
||||||
onPressed: () {
|
onPressed: gitAdd,
|
||||||
print("FOO");
|
),
|
||||||
},
|
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}'.");
|
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}'.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user