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:
Vishesh Handa
2019-05-16 12:23:27 +02:00
parent 8b1cafef2f
commit 64c8ddd7b0
6 changed files with 37 additions and 45 deletions

View File

@ -3,18 +3,6 @@ package io.gitjournal.gitjournal;
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.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;
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) {
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);
try {
Git git = Git.open(cloneDir);
PullCommand pullCommand = git.pull().setStrategy(MergeStrategy.THEIRS);
pullCommand.setTransportConfigCallback(new TransportConfigCallback() {
@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);
Git git = new Git();
git.setSshKeys(publicKeyPath, privateKeyPath, "");
String errorStr = git.pull(cloneDirPath, authorName, authorEmail);
if (!errorStr.isEmpty()) {
result.error("FAILED", errorStr, null);
return null;
}

View File

@ -3,8 +3,6 @@ package io.gitjournal.gitjournal;
import android.os.AsyncTask;
import android.util.Log;
import java.io.File;
import io.flutter.plugin.common.MethodChannel.Result;
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 privateKeyPath = params[2];
File cloneDir = new File(cloneDirPath);
Log.d("GitClone Directory", cloneDirPath);
Git git = new Git();

View File

@ -65,15 +65,25 @@ public class MainActivity extends FlutterActivity implements MethodCallHandler {
return;
} else if (call.method.equals("gitPull")) {
String folderName = call.argument("folderName");
String authorName = call.argument("authorName");
String authorEmail = call.argument("authorEmail");
if (folderName == null || folderName.isEmpty()) {
result.error("Invalid Parameters", "folderName Invalid", null);
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;
new GitPullTask(result).execute(cloneLocation, privateKeyPath);
new GitPullTask(result).execute(cloneLocation, publicKeyPath, privateKeyPath, authorName, authorEmail);
return;
} else if (call.method.equals("gitPush")) {
String folderName = call.argument("folderName");

View File

@ -98,11 +98,17 @@ GitException createGitException(String msg) {
return GitException(msg);
}
Future gitPull(String folderName) async {
Future gitPull({
String folderName,
String authorName,
String authorEmail,
}) async {
print("Going to git pull: $folderName");
try {
await _platform.invokeMethod('gitPull', {
'folderName': folderName,
'authorName': authorName,
'authorEmail': authorEmail,
});
print("Done");
} on PlatformException catch (e) {

View File

@ -61,7 +61,11 @@ class GitApp extends StatelessWidget {
RaisedButton(
child: Text("Git Pull"),
onPressed: () async {
gitPull(basePath);
gitPull(
folderName: basePath,
authorEmail: "noemail@example.com",
authorName: "Vishesh Handa",
);
},
),
RaisedButton(

View File

@ -88,7 +88,11 @@ class GitNoteRepository implements NoteRepository {
@override
Future<bool> sync() async {
try {
await gitPull(dirName);
await gitPull(
folderName: dirName,
authorEmail: Settings.instance.gitAuthorEmail,
authorName: Settings.instance.gitAuthor,
);
} on GitException catch (ex) {
print(ex);
}