mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-28 18:03:14 +08:00
Android: Use the c library for git pull
It's not perfect as our implementation of git pull results in an extra merge commit and doesn't handle conflicts properly, but it is a basic implementation, so at least we have something.
This commit is contained in:
@ -3,18 +3,6 @@ package io.gitjournal.gitjournal;
|
|||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import org.eclipse.jgit.api.Git;
|
|
||||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
|
||||||
|
|
||||||
import org.eclipse.jgit.api.PullCommand;
|
|
||||||
import org.eclipse.jgit.api.TransportConfigCallback;
|
|
||||||
import org.eclipse.jgit.api.errors.TransportException;
|
|
||||||
import org.eclipse.jgit.merge.MergeStrategy;
|
|
||||||
import org.eclipse.jgit.transport.Transport;
|
|
||||||
import org.eclipse.jgit.transport.SshTransport;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
import io.flutter.plugin.common.MethodChannel.Result;
|
import io.flutter.plugin.common.MethodChannel.Result;
|
||||||
|
|
||||||
public class GitPullTask extends AsyncTask<String, Void, Void> {
|
public class GitPullTask extends AsyncTask<String, Void, Void> {
|
||||||
@ -27,35 +15,18 @@ public class GitPullTask extends AsyncTask<String, Void, Void> {
|
|||||||
|
|
||||||
protected Void doInBackground(String... params) {
|
protected Void doInBackground(String... params) {
|
||||||
String cloneDirPath = params[0];
|
String cloneDirPath = params[0];
|
||||||
final String privateKeyPath = params[1];
|
final String publicKeyPath = params[1];
|
||||||
|
final String privateKeyPath = params[2];
|
||||||
|
final String authorName = params[3];
|
||||||
|
final String authorEmail = params[4];
|
||||||
|
|
||||||
File cloneDir = new File(cloneDirPath);
|
|
||||||
Log.d("GitClone Directory", cloneDirPath);
|
Log.d("GitClone Directory", cloneDirPath);
|
||||||
|
|
||||||
try {
|
Git git = new Git();
|
||||||
Git git = Git.open(cloneDir);
|
git.setSshKeys(publicKeyPath, privateKeyPath, "");
|
||||||
|
String errorStr = git.pull(cloneDirPath, authorName, authorEmail);
|
||||||
PullCommand pullCommand = git.pull().setStrategy(MergeStrategy.THEIRS);
|
if (!errorStr.isEmpty()) {
|
||||||
pullCommand.setTransportConfigCallback(new TransportConfigCallback() {
|
result.error("FAILED", errorStr, null);
|
||||||
@Override
|
|
||||||
public void configure(Transport transport) {
|
|
||||||
SshTransport sshTransport = (SshTransport) transport;
|
|
||||||
sshTransport.setSshSessionFactory(new CustomSshSessionFactory(privateKeyPath));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
pullCommand.call();
|
|
||||||
} catch (TransportException e) {
|
|
||||||
Log.d(TAG, e.toString());
|
|
||||||
result.error("FAILED", e.getMessage(), null);
|
|
||||||
return null;
|
|
||||||
} catch (GitAPIException e) {
|
|
||||||
Log.d(TAG, e.toString());
|
|
||||||
result.error("FAILED", e.getMessage(), null);
|
|
||||||
return null;
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.d(TAG, e.toString());
|
|
||||||
result.error("FAILED", e.getMessage(), null);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,8 +3,6 @@ package io.gitjournal.gitjournal;
|
|||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
import io.flutter.plugin.common.MethodChannel.Result;
|
import io.flutter.plugin.common.MethodChannel.Result;
|
||||||
|
|
||||||
public class GitPushTask extends AsyncTask<String, Void, Void> {
|
public class GitPushTask extends AsyncTask<String, Void, Void> {
|
||||||
@ -20,7 +18,6 @@ public class GitPushTask extends AsyncTask<String, Void, Void> {
|
|||||||
final String publicKeyPath = params[1];
|
final String publicKeyPath = params[1];
|
||||||
final String privateKeyPath = params[2];
|
final String privateKeyPath = params[2];
|
||||||
|
|
||||||
File cloneDir = new File(cloneDirPath);
|
|
||||||
Log.d("GitClone Directory", cloneDirPath);
|
Log.d("GitClone Directory", cloneDirPath);
|
||||||
|
|
||||||
Git git = new Git();
|
Git git = new Git();
|
||||||
|
@ -65,15 +65,25 @@ public class MainActivity extends FlutterActivity implements MethodCallHandler {
|
|||||||
return;
|
return;
|
||||||
} else if (call.method.equals("gitPull")) {
|
} else if (call.method.equals("gitPull")) {
|
||||||
String folderName = call.argument("folderName");
|
String folderName = call.argument("folderName");
|
||||||
|
String authorName = call.argument("authorName");
|
||||||
|
String authorEmail = call.argument("authorEmail");
|
||||||
|
|
||||||
if (folderName == null || folderName.isEmpty()) {
|
if (folderName == null || folderName.isEmpty()) {
|
||||||
result.error("Invalid Parameters", "folderName Invalid", null);
|
result.error("Invalid Parameters", "folderName Invalid", null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (authorName == null || authorName.isEmpty()) {
|
||||||
|
result.error("Invalid Parameters", "authorName Invalid", null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (authorEmail == null || authorEmail.isEmpty()) {
|
||||||
|
result.error("Invalid Parameters", "authorEmail Invalid", null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
String cloneLocation = filesDir + "/" + folderName;
|
String cloneLocation = filesDir + "/" + folderName;
|
||||||
|
|
||||||
new GitPullTask(result).execute(cloneLocation, privateKeyPath);
|
new GitPullTask(result).execute(cloneLocation, publicKeyPath, privateKeyPath, authorName, authorEmail);
|
||||||
return;
|
return;
|
||||||
} else if (call.method.equals("gitPush")) {
|
} else if (call.method.equals("gitPush")) {
|
||||||
String folderName = call.argument("folderName");
|
String folderName = call.argument("folderName");
|
||||||
|
@ -98,11 +98,17 @@ GitException createGitException(String msg) {
|
|||||||
return GitException(msg);
|
return GitException(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future gitPull(String folderName) async {
|
Future gitPull({
|
||||||
|
String folderName,
|
||||||
|
String authorName,
|
||||||
|
String authorEmail,
|
||||||
|
}) async {
|
||||||
print("Going to git pull: $folderName");
|
print("Going to git pull: $folderName");
|
||||||
try {
|
try {
|
||||||
await _platform.invokeMethod('gitPull', {
|
await _platform.invokeMethod('gitPull', {
|
||||||
'folderName': folderName,
|
'folderName': folderName,
|
||||||
|
'authorName': authorName,
|
||||||
|
'authorEmail': authorEmail,
|
||||||
});
|
});
|
||||||
print("Done");
|
print("Done");
|
||||||
} on PlatformException catch (e) {
|
} on PlatformException catch (e) {
|
||||||
|
@ -61,7 +61,11 @@ class GitApp extends StatelessWidget {
|
|||||||
RaisedButton(
|
RaisedButton(
|
||||||
child: Text("Git Pull"),
|
child: Text("Git Pull"),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
gitPull(basePath);
|
gitPull(
|
||||||
|
folderName: basePath,
|
||||||
|
authorEmail: "noemail@example.com",
|
||||||
|
authorName: "Vishesh Handa",
|
||||||
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
RaisedButton(
|
RaisedButton(
|
||||||
|
@ -88,7 +88,11 @@ class GitNoteRepository implements NoteRepository {
|
|||||||
@override
|
@override
|
||||||
Future<bool> sync() async {
|
Future<bool> sync() async {
|
||||||
try {
|
try {
|
||||||
await gitPull(dirName);
|
await gitPull(
|
||||||
|
folderName: dirName,
|
||||||
|
authorEmail: Settings.instance.gitAuthorEmail,
|
||||||
|
authorName: Settings.instance.gitAuthor,
|
||||||
|
);
|
||||||
} on GitException catch (ex) {
|
} on GitException catch (ex) {
|
||||||
print(ex);
|
print(ex);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user