Make fetch-pack a builtin with an internal API
authorDaniel Barkalow <barkalow@iabervon.org>
Tue, 11 Sep 2007 03:03:00 +0000 (23:03 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 19 Sep 2007 10:22:30 +0000 (03:22 -0700)
Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Makefile
builtin-fetch-pack.c [moved from fetch-pack.c with 93% similarity]
builtin.h
fetch-pack.h [new file with mode: 0644]
git.c

index 3436a2326a3f98c5af86b009efb694a600147517..9ce6a9ba310d1c6521f48039c360c3c683e06e1a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -331,6 +331,7 @@ BUILTIN_OBJS = \
        builtin-diff-files.o \
        builtin-diff-index.o \
        builtin-diff-tree.o \
+       builtin-fetch-pack.o \
        builtin-fetch--tool.o \
        builtin-fmt-merge-msg.o \
        builtin-for-each-ref.o \
similarity index 93%
rename from fetch-pack.c
rename to builtin-fetch-pack.c
index 9c81305be5f4c1a78794c9d5397bd828ba705fcc..3b217d96f074c1af1429ccdc90499338cfee04ea 100644 (file)
@@ -6,6 +6,7 @@
 #include "exec_cmd.h"
 #include "pack.h"
 #include "sideband.h"
+#include "fetch-pack.h"
 
 static int keep_pack;
 static int transfer_unpack_limit = -1;
@@ -573,7 +574,7 @@ static int get_pack(int xd[2])
        die("%s died of unnatural causes %d", argv[0], status);
 }
 
-static int fetch_pack(int fd[2], int nr_match, char **match)
+static struct ref *do_fetch_pack(int fd[2], int nr_match, char **match)
 {
        struct ref *ref;
        unsigned char sha1[20];
@@ -615,12 +616,7 @@ static int fetch_pack(int fd[2], int nr_match, char **match)
                die("git-fetch-pack: fetch failed.");
 
  all_done:
-       while (ref) {
-               printf("%s %s\n",
-                      sha1_to_hex(ref->old_sha1), ref->name);
-               ref = ref->next;
-       }
-       return 0;
+       return ref;
 }
 
 static int remove_duplicates(int nr_heads, char **heads)
@@ -663,15 +659,28 @@ static int fetch_pack_config(const char *var, const char *value)
 
 static struct lock_file lock;
 
-int main(int argc, char **argv)
+void setup_fetch_pack(struct fetch_pack_args *args)
+{
+       uploadpack = args->uploadpack;
+       quiet = args->quiet;
+       keep_pack = args->keep_pack;
+       if (args->unpacklimit >= 0)
+               unpack_limit = args->unpacklimit;
+       if (args->keep_pack)
+               unpack_limit = 0;
+       use_thin_pack = args->use_thin_pack;
+       fetch_all = args->fetch_all;
+       verbose = args->verbose;
+       depth = args->depth;
+       no_progress = args->no_progress;
+}
+
+int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
 {
        int i, ret, nr_heads;
+       struct ref *ref;
        char *dest = NULL, **heads;
-       int fd[2];
-       pid_t pid;
-       struct stat st;
 
-       setup_git_directory();
        git_config(fetch_pack_config);
 
        if (0 <= transfer_unpack_limit)
@@ -682,7 +691,7 @@ int main(int argc, char **argv)
        nr_heads = 0;
        heads = NULL;
        for (i = 1; i < argc; i++) {
-               char *arg = argv[i];
+               const char *arg = argv[i];
 
                if (*arg == '-') {
                        if (!prefixcmp(arg, "--upload-pack=")) {
@@ -716,8 +725,6 @@ int main(int argc, char **argv)
                        }
                        if (!prefixcmp(arg, "--depth=")) {
                                depth = strtol(arg + 8, NULL, 0);
-                               if (stat(git_path("shallow"), &st))
-                                       st.st_mtime = 0;
                                continue;
                        }
                        if (!strcmp("--no-progress", arg)) {
@@ -726,22 +733,52 @@ int main(int argc, char **argv)
                        }
                        usage(fetch_pack_usage);
                }
-               dest = arg;
-               heads = argv + i + 1;
+               dest = (char *)arg;
+               heads = (char **)(argv + i + 1);
                nr_heads = argc - i - 1;
                break;
        }
        if (!dest)
                usage(fetch_pack_usage);
-       pid = git_connect(fd, dest, uploadpack, verbose ? CONNECT_VERBOSE : 0);
+
+       ref = fetch_pack(dest, nr_heads, heads);
+
+       ret = !ref;
+
+       while (ref) {
+               printf("%s %s\n",
+                      sha1_to_hex(ref->old_sha1), ref->name);
+               ref = ref->next;
+       }
+
+       return ret;
+}
+
+struct ref *fetch_pack(const char *dest, int nr_heads, char **heads)
+{
+       int i, ret;
+       int fd[2];
+       pid_t pid;
+       struct ref *ref;
+       struct stat st;
+
+       if (depth > 0) {
+               if (stat(git_path("shallow"), &st))
+                       st.st_mtime = 0;
+       }
+
+       printf("connect to %s\n", dest);
+
+       pid = git_connect(fd, (char *)dest, uploadpack,
+                          verbose ? CONNECT_VERBOSE : 0);
        if (pid < 0)
-               return 1;
+               return NULL;
        if (heads && nr_heads)
                nr_heads = remove_duplicates(nr_heads, heads);
-       ret = fetch_pack(fd, nr_heads, heads);
+       ref = do_fetch_pack(fd, nr_heads, heads);
        close(fd[0]);
        close(fd[1]);
-       ret |= finish_connect(pid);
+       ret = finish_connect(pid);
 
        if (!ret && nr_heads) {
                /* If the heads to pull were given, we should have
@@ -785,5 +822,8 @@ int main(int argc, char **argv)
                }
        }
 
-       return !!ret;
+       if (ret)
+               ref = NULL;
+
+       return ref;
 }
index 1fa1444e33e3a687442d1fbab67cc9bdfacd2726..d424bfdeb17c2b84fc052e54b0e55905379a5939 100644 (file)
--- a/builtin.h
+++ b/builtin.h
@@ -31,6 +31,7 @@ extern int cmd_diff_files(int argc, const char **argv, const char *prefix);
 extern int cmd_diff_index(int argc, const char **argv, const char *prefix);
 extern int cmd_diff(int argc, const char **argv, const char *prefix);
 extern int cmd_diff_tree(int argc, const char **argv, const char *prefix);
+extern int cmd_fetch_pack(int argc, const char **argv, const char *prefix);
 extern int cmd_fetch__tool(int argc, const char **argv, const char *prefix);
 extern int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix);
 extern int cmd_for_each_ref(int argc, const char **argv, const char *prefix);
diff --git a/fetch-pack.h b/fetch-pack.h
new file mode 100644 (file)
index 0000000..e06bf5b
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef FETCH_PACK_H
+#define FETCH_PACK_H
+
+struct fetch_pack_args
+{
+       const char *uploadpack;
+       int quiet;
+       int keep_pack;
+       int unpacklimit;
+       int use_thin_pack;
+       int fetch_all;
+       int verbose;
+       int depth;
+       int no_progress;
+};
+
+void setup_fetch_pack(struct fetch_pack_args *args);
+
+struct ref *fetch_pack(const char *dest, int nr_heads, char **heads);
+
+#endif
diff --git a/git.c b/git.c
index 41fe73eff9dd17d1b6bd7c92510870b5fd081e2d..6ca36c02650bdebef6c015328231f50b9edf3cef 100644 (file)
--- a/git.c
+++ b/git.c
@@ -334,6 +334,7 @@ static void handle_internal_command(int argc, const char **argv)
                { "diff-files", cmd_diff_files },
                { "diff-index", cmd_diff_index, RUN_SETUP },
                { "diff-tree", cmd_diff_tree, RUN_SETUP },
+               { "fetch-pack", cmd_fetch_pack, RUN_SETUP },
                { "fetch--tool", cmd_fetch__tool, RUN_SETUP },
                { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
                { "for-each-ref", cmd_for_each_ref, RUN_SETUP },