From 59ed5e35ec23d1564cfbc717c94220ff8e181a96 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Tue, 14 May 2019 16:51:56 +0200 Subject: [PATCH] GitTest: Refactor the code --- git_test/git.c | 71 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/git_test/git.c b/git_test/git.c index 92129d37..92db6249 100644 --- a/git_test/git.c +++ b/git_test/git.c @@ -4,13 +4,12 @@ #include -void handle_error(int err) +int handle_error(int err) { - if (err < 0) - { - const git_error *e = giterr_last(); - printf("Error %d/%d: %s\n", err, e->klass, e->message); - } + + const git_error *e = giterr_last(); + printf("Error %d/%d: %s\n", err, e->klass, e->message); + return err; } int match_cb(const char *path, const char *spec, void *payload) @@ -19,33 +18,53 @@ int match_cb(const char *path, const char *spec, void *payload) return 0; } -int main(int argc, char *argv[]) +int gj_git_add(char *git_base_path, char *add_pattern) { int err; + + git_repository *repo = NULL; + err = git_repository_open(&repo, git_base_path); + if (err < 0) + { + return handle_error(err); + } + + git_index *idx = NULL; + err = git_repository_index(&idx, repo); + if (err < 0) + { + return handle_error(err); + } + + char *paths[] = {add_pattern}; + git_strarray pathspec = {paths, 1}; + + err = git_index_add_all(idx, &pathspec, GIT_INDEX_ADD_DEFAULT, match_cb, NULL); + if (err < 0) + { + return handle_error(err); + } + + err = git_index_write(idx); + if (err < 0) + { + return handle_error(err); + } + + git_index_free(idx); + git_repository_free(repo); + + return 0; +} + +int main(int argc, char *argv[]) +{ char *git_base_path = "/tmp/journal_test"; char *clone_url = "git@github.com:GitJournal/journal_test.git"; char *add_pattern = "."; git_libgit2_init(); - git_repository *repo = NULL; - err = git_repository_open(&repo, git_base_path); - handle_error(err); - - git_index *idx = NULL; - err = git_repository_index(&idx, repo); - handle_error(err); - - char *paths[] = {add_pattern}; - git_strarray pathspec = {paths, 1}; - - err = git_index_add_all(idx, &pathspec, GIT_INDEX_ADD_DEFAULT, match_cb, NULL); - handle_error(err); - - err = git_index_write(idx); - handle_error(err); - - git_index_free(idx); - + git_libgit2_shutdown(); return 0; }