Merge branch 'jc/nostat'
[gitweb.git] / clone-pack.c
index c102ca885bc3d6700bea82fba5d4c97ab6a93db7..f634431be1f6ba3fa518d987a227c33f19543fb6 100644 (file)
@@ -1,10 +1,9 @@
 #include "cache.h"
 #include "refs.h"
 #include "pkt-line.h"
-#include <sys/wait.h>
 
-static int quiet;
-static const char clone_pack_usage[] = "git-clone-pack [-q] [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*";
+static const char clone_pack_usage[] =
+"git-clone-pack [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*";
 static const char *exec = "git-upload-pack";
 
 static void clone_handshake(int fd[2], struct ref *ref)
@@ -34,6 +33,12 @@ static void write_one_ref(struct ref *ref)
        int fd;
        char *hex;
 
+       if (!strncmp(ref->name, "refs/", 5) &&
+           check_ref_format(ref->name + 5)) {
+               error("refusing to create funny ref '%s' locally", ref->name);
+               return;
+       }
+
        if (safe_create_leading_directories(path))
                die("unable to create leading directory for %s", ref->name);
        fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666);
@@ -110,43 +115,28 @@ static int clone_pack(int fd[2], int nr_match, char **match)
 {
        struct ref *refs;
        int status;
-       pid_t pid;
 
-       get_remote_heads(fd[0], &refs, nr_match, match);
+       get_remote_heads(fd[0], &refs, nr_match, match, 1);
        if (!refs) {
                packet_flush(fd[1]);
                die("no matching remote head");
        }
        clone_handshake(fd, refs);
-       pid = fork();
-       if (pid < 0)
-               die("git-clone-pack: unable to fork off git-unpack-objects");
-       if (!pid) {
-               dup2(fd[0], 0);
-               close(fd[0]);
-               close(fd[1]);
-               execlp("git-unpack-objects", "git-unpack-objects",
-                       quiet ? "-q" : NULL, NULL);
-               die("git-unpack-objects exec failed");
-       }
-       close(fd[0]);
-       close(fd[1]);
-       while (waitpid(pid, &status, 0) < 0) {
-               if (errno != EINTR)
-                       die("waiting for git-unpack-objects: %s", strerror(errno));
-       }
-       if (WIFEXITED(status)) {
-               int code = WEXITSTATUS(status);
-               if (code)
-                       die("git-unpack-objects died with error code %d", code);
-               write_refs(refs);
-               return 0;
-       }
-       if (WIFSIGNALED(status)) {
-               int sig = WTERMSIG(status);
-               die("git-unpack-objects died of signal %d", sig);
+
+       status = receive_keep_pack(fd, "git-clone-pack");
+
+       if (!status) {
+               if (nr_match == 0)
+                       write_refs(refs);
+               else
+                       while (refs) {
+                               printf("%s %s\n",
+                                      sha1_to_hex(refs->old_sha1),
+                                      refs->name);
+                               refs = refs->next;
+                       }
        }
-       die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status);
+       return status;
 }
 
 int main(int argc, char **argv)
@@ -156,16 +146,16 @@ int main(int argc, char **argv)
        int fd[2];
        pid_t pid;
 
+       setup_git_directory();
+
        nr_heads = 0;
        heads = NULL;
        for (i = 1; i < argc; i++) {
                char *arg = argv[i];
 
                if (*arg == '-') {
-                       if (!strcmp("-q", arg)) {
-                               quiet = 1;
+                       if (!strcmp("-q", arg))
                                continue;
-                       }
                        if (!strncmp("--exec=", arg, 7)) {
                                exec = arg + 7;
                                continue;