WIP fetch command, very dirty
[gitlite/.git] / gitlite.c
index 2396f9f612e08f05bb8059fc5fc97211404269f0..5e26dbe8eb37aef1da5da2ee700ec0e6ab78cafb 100644 (file)
--- a/gitlite.c
+++ b/gitlite.c
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/param.h>
+#include <sys/types.h>
 #include <sysexits.h>
 #include <unistd.h>
 
 #include <git2/errors.h>
 #include <git2/global.h>
 
+#define PRIuZ "zu"
+
+#include "callbacks.h"
+
+
 // Compile with:
 // clang90 -o gitlite gitlite.c -lgit2 -I/usr/local/include -L/usr/local/lib -g
 
@@ -27,6 +33,8 @@ git_bail_error(int rc) {
                else
                        printf("ERROR %d: cannot fetch error info\n", rc);
        }
+
+//    exit(rc);
 }
 
 void
@@ -122,7 +130,44 @@ main(int argc, char *argv[]) {
 
        } else if (do_fetch) {
                fprintf(stderr, "handle git fetch command\n");
-               return (-1);
+
+               rc = git_libgit2_init();
+               git_bail_error(rc);
+
+        git_repository *repo = NULL;
+
+        rc = git_repository_open(&repo, argv[0]);
+        git_bail_error(rc);
+
+
+        git_remote *remote = NULL;
+        rc = git_remote_lookup(&remote, repo, "origin");
+        git_bail_error(rc);
+
+
+        const git_indexer_progress *stats;
+        git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
+        fetch_opts.callbacks.update_tips = &update_cb;
+        fetch_opts.callbacks.sideband_progress = &progress_cb;
+        fetch_opts.callbacks.transfer_progress = transfer_progress_cb;
+        fetch_opts.callbacks.credentials = NULL;
+
+        //rc = git_remote_add_fetch(repo, "origin", "master");
+        //git_bail_error(rc);
+        printf("Fetching %s for repo %p\n", argv[0], repo);
+        rc = git_remote_fetch(remote, NULL, &fetch_opts, "fetch");
+        git_bail_error(rc);
+
+        __asm("int $3");
+        stats = git_remote_stats(remote);
+
+        if (stats->local_objects > 0) {
+            printf("\rReceived %u/%u objects in %" PRIuZ " bytes (used %u local objects)\n", stats->indexed_objects, stats->total_objects, stats->received_bytes, stats->local_objects);
+        } else{
+            printf("\rReceived %u/%u objects in %" PRIuZ "bytes\n", stats->indexed_objects, stats->total_objects, stats->received_bytes);
+        }
+
+        git_remote_free(remote);
 
        } else if (do_log) {
                fprintf(stderr, "handle git log command\n");