mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-28 09:47:35 +08:00
GitAdd: Use libgit2 instead of jgit
This doesn't seem to work properly, though. Not sure what I'm doing wrong.
This commit is contained in:
@ -46,6 +46,7 @@ set_target_properties(git2-lib
|
||||
add_library(native-lib
|
||||
SHARED
|
||||
keygen.c
|
||||
git.c
|
||||
)
|
||||
|
||||
# The order of these libraries is super dooper important
|
||||
|
77
android/app/src/main/cpp/git.c
Normal file
77
android/app/src/main/cpp/git.c
Normal file
@ -0,0 +1,77 @@
|
||||
#include <jni.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <android/log.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <git2.h>
|
||||
|
||||
int match_cb(const char *path, const char *spec, void *payload)
|
||||
{
|
||||
__android_log_print(ANDROID_LOG_ERROR, "GitAdd", "Match: %s\n", path);
|
||||
}
|
||||
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_io_gitjournal_gitjournal_Git_add(
|
||||
JNIEnv *env,
|
||||
jobject this_obj,
|
||||
jstring jni_git_base_path,
|
||||
jstring jni_add_pattern)
|
||||
{
|
||||
const char *git_base_path = (*env)->GetStringUTFChars(env, jni_git_base_path, 0);
|
||||
const char *add_pattern = (*env)->GetStringUTFChars(env, jni_add_pattern, 0);
|
||||
|
||||
int error;
|
||||
|
||||
// FIXME: Do this somewhere else
|
||||
git_libgit2_init();
|
||||
|
||||
git_repository *repo = NULL;
|
||||
error = git_repository_open(&repo, git_base_path);
|
||||
if (error < 0) {
|
||||
const git_error *e = giterr_last();
|
||||
__android_log_print(ANDROID_LOG_ERROR, "GitAdd", "Error %d/%d: %s\n", error, e->klass, e->message);
|
||||
return (*env)->NewStringUTF(env, "Error");
|
||||
}
|
||||
|
||||
git_index *idx = NULL;
|
||||
error = git_repository_index(&idx, repo);
|
||||
if (error < 0) {
|
||||
const git_error *e = giterr_last();
|
||||
__android_log_print(ANDROID_LOG_ERROR, "GitAdd", "Error %d/%d: %s\n", error, e->klass, e->message);
|
||||
return (*env)->NewStringUTF(env, "Error");
|
||||
}
|
||||
|
||||
const char *paths[] = {add_pattern};
|
||||
git_strarray pathspec = {paths, 1};
|
||||
|
||||
__android_log_print(ANDROID_LOG_ERROR, "GitAdd", "Add Pattern: %s", add_pattern);
|
||||
|
||||
error = git_index_add_all(idx, &pathspec, GIT_INDEX_ADD_DEFAULT, match_cb, NULL);
|
||||
if (error < 0) {
|
||||
const git_error *e = giterr_last();
|
||||
__android_log_print(ANDROID_LOG_ERROR, "GitAdd", "Error %d/%d: %s\n", error, e->klass, e->message);
|
||||
return (*env)->NewStringUTF(env, "Error");
|
||||
}
|
||||
|
||||
error = git_index_write(idx);
|
||||
if (error < 0) {
|
||||
const git_error *e = giterr_last();
|
||||
__android_log_print(ANDROID_LOG_ERROR, "GitAdd", "Error %d/%d: %s\n", error, e->klass, e->message);
|
||||
return (*env)->NewStringUTF(env, "Error");
|
||||
}
|
||||
|
||||
git_index_free(idx);
|
||||
if (error < 0) {
|
||||
const git_error *e = giterr_last();
|
||||
__android_log_print(ANDROID_LOG_ERROR, "GitAdd", "Error %d/%d: %s\n", error, e->klass, e->message);
|
||||
return (*env)->NewStringUTF(env, "Error");
|
||||
}
|
||||
|
||||
__android_log_print(ANDROID_LOG_ERROR, "GitAdd", "Everything seems fine");
|
||||
|
||||
return (*env)->NewStringUTF(env, "");
|
||||
}
|
@ -87,7 +87,7 @@ int generate_keys(const char* private_key_path,
|
||||
|
||||
void ssh_log_jni_callback(int priority, const char *function, const char *buffer, void *userdata)
|
||||
{
|
||||
__android_log_print(ANDROID_LOG_ERROR, "SSH_HOWDY", "P%d : %s : %s", priority, function, buffer);
|
||||
__android_log_print(ANDROID_LOG_ERROR, "SSH_HOWDY", "P%d : %s : %s\n", priority, function, buffer);
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
|
@ -6,4 +6,6 @@ public class Git {
|
||||
}
|
||||
|
||||
public native String generateKeys(String privateKeyPath, String publicKeyPath, String comment);
|
||||
|
||||
public native String add(String basePath, String pattern);
|
||||
}
|
||||
|
@ -3,14 +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.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> {
|
||||
@ -25,27 +17,12 @@ public class GitAddTask extends AsyncTask<String, Void, Void> {
|
||||
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.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();
|
||||
String errorStr = git.add(cloneDirPath, filePattern);
|
||||
if (!errorStr.isEmpty()) {
|
||||
result.error("FAILED", errorStr, null);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user