Dirty work state for cloning repo using arguments
authorSofian Brabez <sbz@6dev.net>
Tue, 14 Jul 2020 00:10:02 +0000 (01:10 +0100)
committerSofian Brabez <sbz@6dev.net>
Tue, 14 Jul 2020 00:14:04 +0000 (01:14 +0100)
gitlite.c

index 8ffc3d6fa9d4d8d21b623f2d3fc01ee7eaff1f7b..9cb131794624f4be1f02aba87a675c7a2f86f37b 100644 (file)
--- a/gitlite.c
+++ b/gitlite.c
@@ -1,5 +1,9 @@
+#include <libgen.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <sys/param.h>
+#include <unistd.h>
 
 #include <git2/global.h>
 #include <git2/clone.h>
@@ -23,13 +27,30 @@ git_bail_error(int rc)
     }
 }
 
+void
+util_stripext(char *path)
+{
+       char *end = path + strlen(path);
+
+       while (end > path && *end != '.') {
+               --end;
+       }
+
+       if (end > path) {
+               *end = '\0';
+       }
+}
+
 int
 main(int argc, char *argv[])
 {
        int rc;
-       const char *url = "https://github.com/freebsd/pkg.git";
        //const char *url = "https://github.com/freebsd/freebsd.git";
-       const char *local_path = "/home/sbz/git/src";
+       const char *url = "https://github.com/freebsd/pkg.git";
+       char buff[MAXPATHLEN];
+       char *ext = NULL;
+       char *local_path = NULL;
+
        git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
        git_checkout_options co_opts = GIT_CHECKOUT_OPTIONS_INIT;
        git_repository *cloned_repo = NULL;
@@ -46,6 +67,24 @@ main(int argc, char *argv[])
        rc = git_libgit2_init();
        git_bail_error(rc);
 
+       if (argc == 1) {
+               fprintf(stderr, "Usage: gitlite <git-url> [path]\n");
+               return -1;
+       }
+       
+       if (argv[1] != NULL) {
+               url = argv[1];
+       }
+
+       if (argv[2] != NULL) {
+               local_path = argv[2];
+       } else {
+               ext = strdup(basename(__DECONST(char*, url)));
+               util_stripext(__DECONST(char*, ext));
+               asprintf(&local_path, "%s/%s", getwd(buff), ext);
+       }
+       //printf("%s\n", local_path);
+
        rc = git_clone(&cloned_repo, url, local_path, &clone_opts);
        git_bail_error(rc);
 
@@ -55,5 +94,8 @@ main(int argc, char *argv[])
        rc = git_libgit2_shutdown();
        git_bail_error(rc);
 
+       if (ext != NULL)
+               free(ext);
+
     return (rc);
 }