]> git.ipfire.org Git - thirdparty/git.git/blobdiff - upload-pack.c
upload-pack: move rev-list code out of check_non_tip()
[thirdparty/git.git] / upload-pack.c
index b3f6653ffda1a3d6d319752676511307d9cd032b..93c05229cb2c451365b6c18be26af600d704b1f8 100644 (file)
@@ -276,7 +276,7 @@ static void create_pack_file(void)
        die("git upload-pack: %s", abort_msg);
 }
 
-static int got_sha1(char *hex, unsigned char *sha1)
+static int got_sha1(const char *hex, unsigned char *sha1)
 {
        struct object *o;
        int we_knew_they_have = 0;
@@ -382,6 +382,8 @@ static int get_common_commits(void)
 
        for (;;) {
                char *line = packet_read_line(0, NULL);
+               const char *arg;
+
                reset_timeout();
 
                if (!line) {
@@ -403,8 +405,8 @@ static int get_common_commits(void)
                        got_other = 0;
                        continue;
                }
-               if (starts_with(line, "have ")) {
-                       switch (got_sha1(line+5, sha1)) {
+               if (skip_prefix(line, "have ", &arg)) {
+                       switch (got_sha1(arg, sha1)) {
                        case -1: /* they have what we do not */
                                got_other = 1;
                                if (multi_ack && ok_to_give_up()) {
@@ -449,7 +451,7 @@ static int is_our_ref(struct object *o)
        return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
 }
 
-static void check_non_tip(void)
+static int has_unreachable(struct object_array *src)
 {
        static const char *argv[] = {
                "rev-list", "--stdin", NULL,
@@ -459,30 +461,22 @@ static void check_non_tip(void)
        char namebuf[42]; /* ^ + SHA-1 + LF */
        int i;
 
-       /*
-        * In the normal in-process case without
-        * uploadpack.allowReachableSHA1InWant,
-        * non-tip requests can never happen.
-        */
-       if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
-               goto error;
-
        cmd.argv = argv;
        cmd.git_cmd = 1;
        cmd.no_stderr = 1;
        cmd.in = -1;
        cmd.out = -1;
 
-       if (start_command(&cmd))
-               goto error;
-
        /*
-        * If rev-list --stdin encounters an unknown commit, it
-        * terminates, which will cause SIGPIPE in the write loop
+        * If the next rev-list --stdin encounters an unknown commit,
+        * it terminates, which will cause SIGPIPE in the write loop
         * below.
         */
        sigchain_push(SIGPIPE, SIG_IGN);
 
+       if (start_command(&cmd))
+               goto error;
+
        namebuf[0] = '^';
        namebuf[41] = '\n';
        for (i = get_max_object_index(); 0 < i; ) {
@@ -496,8 +490,8 @@ static void check_non_tip(void)
                        goto error;
        }
        namebuf[40] = '\n';
-       for (i = 0; i < want_obj.nr; i++) {
-               o = want_obj.objects[i].item;
+       for (i = 0; i < src->nr; i++) {
+               o = src->objects[i].item;
                if (is_our_ref(o))
                        continue;
                memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
@@ -505,8 +499,7 @@ static void check_non_tip(void)
                        goto error;
        }
        close(cmd.in);
-
-       sigchain_pop(SIGPIPE);
+       cmd.in = -1;
 
        /*
         * The commits out of the rev-list are not ancestors of
@@ -516,6 +509,7 @@ static void check_non_tip(void)
        if (i)
                goto error;
        close(cmd.out);
+       cmd.out = -1;
 
        /*
         * rev-list may have died by encountering a bad commit
@@ -525,19 +519,116 @@ static void check_non_tip(void)
        if (finish_command(&cmd))
                goto error;
 
+       sigchain_pop(SIGPIPE);
+
        /* All the non-tip ones are ancestors of what we advertised */
-       return;
+       return 0;
+
+error:
+       sigchain_pop(SIGPIPE);
+
+       if (cmd.in >= 0)
+               close(cmd.in);
+       if (cmd.out >= 0)
+               close(cmd.out);
+       return 1;
+}
+
+static void check_non_tip(void)
+{
+       int i;
+
+       /*
+        * In the normal in-process case without
+        * uploadpack.allowReachableSHA1InWant,
+        * non-tip requests can never happen.
+        */
+       if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
+               goto error;
+       if (!has_unreachable(&want_obj))
+               /* All the non-tip ones are ancestors of what we advertised */
+               return;
 
 error:
        /* Pick one of them (we know there at least is one) */
        for (i = 0; i < want_obj.nr; i++) {
-               o = want_obj.objects[i].item;
+               struct object *o = want_obj.objects[i].item;
                if (!is_our_ref(o))
                        die("git upload-pack: not our ref %s",
                            oid_to_hex(&o->oid));
        }
 }
 
+static void send_shallow(struct commit_list *result)
+{
+       while (result) {
+               struct object *object = &result->item->object;
+               if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
+                       packet_write(1, "shallow %s",
+                                    oid_to_hex(&object->oid));
+                       register_shallow(object->oid.hash);
+                       shallow_nr++;
+               }
+               result = result->next;
+       }
+}
+
+static void send_unshallow(const struct object_array *shallows)
+{
+       int i;
+
+       for (i = 0; i < shallows->nr; i++) {
+               struct object *object = shallows->objects[i].item;
+               if (object->flags & NOT_SHALLOW) {
+                       struct commit_list *parents;
+                       packet_write(1, "unshallow %s",
+                                    oid_to_hex(&object->oid));
+                       object->flags &= ~CLIENT_SHALLOW;
+                       /*
+                        * We want to _register_ "object" as shallow, but we
+                        * also need to traverse object's parents to deepen a
+                        * shallow clone. Unregister it for now so we can
+                        * parse and add the parents to the want list, then
+                        * re-register it.
+                        */
+                       unregister_shallow(object->oid.hash);
+                       object->parsed = 0;
+                       parse_commit_or_die((struct commit *)object);
+                       parents = ((struct commit *)object)->parents;
+                       while (parents) {
+                               add_object_array(&parents->item->object,
+                                                NULL, &want_obj);
+                               parents = parents->next;
+                       }
+                       add_object_array(object, NULL, &extra_edge_obj);
+               }
+               /* make sure commit traversal conforms to client */
+               register_shallow(object->oid.hash);
+       }
+}
+
+static void deepen(int depth, const struct object_array *shallows)
+{
+       if (depth == INFINITE_DEPTH && !is_repository_shallow()) {
+               int i;
+
+               for (i = 0; i < shallows->nr; i++) {
+                       struct object *object = shallows->objects[i].item;
+                       object->flags |= NOT_SHALLOW;
+               }
+       } else {
+               struct commit_list *result;
+
+               result = get_shallow_commits(&want_obj, depth,
+                                            SHALLOW, NOT_SHALLOW);
+               send_shallow(result);
+               free_commit_list(result);
+       }
+
+       send_unshallow(shallows);
+       packet_flush(1);
+}
+
 static void receive_needs(void)
 {
        struct object_array shallows = OBJECT_ARRAY_INIT;
@@ -550,14 +641,16 @@ static void receive_needs(void)
                const char *features;
                unsigned char sha1_buf[20];
                char *line = packet_read_line(0, NULL);
+               const char *arg;
+
                reset_timeout();
                if (!line)
                        break;
 
-               if (starts_with(line, "shallow ")) {
+               if (skip_prefix(line, "shallow ", &arg)) {
                        unsigned char sha1[20];
                        struct object *object;
-                       if (get_sha1_hex(line + 8, sha1))
+                       if (get_sha1_hex(arg, sha1))
                                die("invalid shallow line: %s", line);
                        object = parse_object(sha1);
                        if (!object)
@@ -570,19 +663,19 @@ static void receive_needs(void)
                        }
                        continue;
                }
-               if (starts_with(line, "deepen ")) {
-                       char *end;
-                       depth = strtol(line + 7, &end, 0);
-                       if (end == line + 7 || depth <= 0)
+               if (skip_prefix(line, "deepen ", &arg)) {
+                       char *end = NULL;
+                       depth = strtol(arg, &end, 0);
+                       if (!end || *end || depth <= 0)
                                die("Invalid deepen: %s", line);
                        continue;
                }
-               if (!starts_with(line, "want ") ||
-                   get_sha1_hex(line+5, sha1_buf))
+               if (!skip_prefix(line, "want ", &arg) ||
+                   get_sha1_hex(arg, sha1_buf))
                        die("git upload-pack: protocol error, "
                            "expected to get sha, not '%s'", line);
 
-               features = line + 45;
+               features = arg + 40;
 
                if (parse_feature_request(features, "multi_ack_detailed"))
                        multi_ack = 2;
@@ -630,53 +723,9 @@ static void receive_needs(void)
 
        if (depth == 0 && shallows.nr == 0)
                return;
-       if (depth > 0) {
-               struct commit_list *result = NULL, *backup = NULL;
-               int i;
-               if (depth == INFINITE_DEPTH && !is_repository_shallow())
-                       for (i = 0; i < shallows.nr; i++) {
-                               struct object *object = shallows.objects[i].item;
-                               object->flags |= NOT_SHALLOW;
-                       }
-               else
-                       backup = result =
-                               get_shallow_commits(&want_obj, depth,
-                                                   SHALLOW, NOT_SHALLOW);
-               while (result) {
-                       struct object *object = &result->item->object;
-                       if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
-                               packet_write(1, "shallow %s",
-                                               oid_to_hex(&object->oid));
-                               register_shallow(object->oid.hash);
-                               shallow_nr++;
-                       }
-                       result = result->next;
-               }
-               free_commit_list(backup);
-               for (i = 0; i < shallows.nr; i++) {
-                       struct object *object = shallows.objects[i].item;
-                       if (object->flags & NOT_SHALLOW) {
-                               struct commit_list *parents;
-                               packet_write(1, "unshallow %s",
-                                       oid_to_hex(&object->oid));
-                               object->flags &= ~CLIENT_SHALLOW;
-                               /* make sure the real parents are parsed */
-                               unregister_shallow(object->oid.hash);
-                               object->parsed = 0;
-                               parse_commit_or_die((struct commit *)object);
-                               parents = ((struct commit *)object)->parents;
-                               while (parents) {
-                                       add_object_array(&parents->item->object,
-                                                       NULL, &want_obj);
-                                       parents = parents->next;
-                               }
-                               add_object_array(object, NULL, &extra_edge_obj);
-                       }
-                       /* make sure commit traversal conforms to client */
-                       register_shallow(object->oid.hash);
-               }
-               packet_flush(1);
-       } else
+       if (depth > 0)
+               deepen(depth, &shallows);
+       else
                if (shallows.nr > 0) {
                        int i;
                        for (i = 0; i < shallows.nr; i++)
@@ -833,7 +882,7 @@ int main(int argc, char **argv)
        check_replace_refs = 0;
 
        for (i = 1; i < argc; i++) {
-               char *arg = argv[i];
+               const char *arg = argv[i];
 
                if (arg[0] != '-')
                        break;
@@ -849,8 +898,8 @@ int main(int argc, char **argv)
                        strict = 1;
                        continue;
                }
-               if (starts_with(arg, "--timeout=")) {
-                       timeout = atoi(arg+10);
+               if (skip_prefix(arg, "--timeout=", &arg)) {
+                       timeout = atoi(arg);
                        daemon_mode = 1;
                        continue;
                }