WIP fetch command, very dirty master origin/HEAD origin/master
authorSofian Brabez <sbz@6dev.net>
Sun, 22 Nov 2020 23:13:56 +0000 (23:13 +0000)
committerSofian Brabez <sbz@6dev.net>
Sun, 22 Nov 2020 23:15:01 +0000 (23:15 +0000)
Makefile
callbacks.h [new file with mode: 0644]
gitlite.c

index ccdadda44f93ffa9db801479d79d99213dab4584..f75b9fbdfb98bae0bf108fc335fd0b746b4c5d7e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ MK_DEBUG_FILES=no
 
 SRC=   gitlite.c
 
-CFLAGS=        -I/usr/local/include -L/usr/local/lib -g
+CFLAGS=        -I/usr/local/include -L/usr/local/lib -g -O0
 LDFLAGS=-lgit2 -Wl
 
 .include <bsd.prog.mk>
diff --git a/callbacks.h b/callbacks.h
new file mode 100644 (file)
index 0000000..f26337f
--- /dev/null
@@ -0,0 +1,41 @@
+static int 
+progress_cb(const char *str, int len, void *data)
+{
+       (void)data;
+       printf("remote: %.*s", len, str);
+       fflush(stdout); /* We don't have the \n to force the flush */
+       return 0;
+}
+
+static int 
+update_cb(const char *refname, const git_oid *a, const git_oid *b, void *data)
+{
+       char a_str[GIT_OID_HEXSZ+1], b_str[GIT_OID_HEXSZ+1];
+       (void)data;
+
+       git_oid_fmt(b_str, b);
+       b_str[GIT_OID_HEXSZ] = '\0';
+
+       if (git_oid_is_zero(a)) {
+               printf("[new]     %.20s %s\n", b_str, refname);
+       } else {
+               git_oid_fmt(a_str, a);
+               a_str[GIT_OID_HEXSZ] = '\0';
+               printf("[updated] %.10s..%.10s %s\n", a_str, b_str, refname);
+       }
+
+       return 0;
+}
+
+static int 
+transfer_progress_cb(const git_indexer_progress *stats, void *payload)
+{
+       (void)payload;
+
+       if (stats->received_objects == stats->total_objects) {
+               printf("Resolving deltas %u/%u\r", stats->indexed_deltas, stats->total_deltas);
+       } else if (stats->total_objects > 0) {
+               printf("Received %u/%u objects (%u) in %" PRIuZ " bytes\r", stats->received_objects, stats->total_objects, stats->indexed_objects, stats->received_bytes);
+       }
+       return 0;
+}
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");