Use packet_reader instead of packet_read_line
[gitweb.git] / fetch-pack.c
index 79007f996c899272b6ae9044a9029776181f3438..86790b9bbad5a97c2dd4afaa42f9ccfca406b1b9 100644 (file)
@@ -76,8 +76,7 @@ struct alternate_object_cache {
        size_t nr, alloc;
 };
 
-static void cache_one_alternate(const char *refname,
-                               const struct object_id *oid,
+static void cache_one_alternate(const struct object_id *oid,
                                void *vcache)
 {
        struct alternate_object_cache *cache = vcache;
@@ -136,38 +135,42 @@ enum ack_type {
        ACK_ready
 };
 
-static void consume_shallow_list(struct fetch_pack_args *args, int fd)
+static void consume_shallow_list(struct fetch_pack_args *args,
+                                struct packet_reader *reader)
 {
        if (args->stateless_rpc && args->deepen) {
                /* If we sent a depth we will get back "duplicate"
                 * shallow and unshallow commands every time there
                 * is a block of have lines exchanged.
                 */
-               char *line;
-               while ((line = packet_read_line(fd, NULL))) {
-                       if (starts_with(line, "shallow "))
+               while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
+                       if (starts_with(reader->line, "shallow "))
                                continue;
-                       if (starts_with(line, "unshallow "))
+                       if (starts_with(reader->line, "unshallow "))
                                continue;
                        die(_("git fetch-pack: expected shallow list"));
                }
+               if (reader->status != PACKET_READ_FLUSH)
+                       die(_("git fetch-pack: expected a flush packet after shallow list"));
        }
 }
 
-static enum ack_type get_ack(int fd, struct object_id *result_oid)
+static enum ack_type get_ack(struct packet_reader *reader,
+                            struct object_id *result_oid)
 {
        int len;
-       char *line = packet_read_line(fd, &len);
        const char *arg;
 
-       if (!line)
+       if (packet_reader_read(reader) != PACKET_READ_NORMAL)
                die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
-       if (!strcmp(line, "NAK"))
+       len = reader->pktlen;
+
+       if (!strcmp(reader->line, "NAK"))
                return NAK;
-       if (skip_prefix(line, "ACK ", &arg)) {
+       if (skip_prefix(reader->line, "ACK ", &arg)) {
                if (!get_oid_hex(arg, result_oid)) {
                        arg += 40;
-                       len -= arg - line;
+                       len -= arg - reader->line;
                        if (len < 1)
                                return ACK;
                        if (strstr(arg, "continue"))
@@ -179,9 +182,9 @@ static enum ack_type get_ack(int fd, struct object_id *result_oid)
                        return ACK;
                }
        }
-       if (skip_prefix(line, "ERR ", &arg))
+       if (skip_prefix(reader->line, "ERR ", &arg))
                die(_("remote error: %s"), arg);
-       die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
+       die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
 }
 
 static void send_request(struct fetch_pack_args *args,
@@ -249,10 +252,14 @@ static int find_common(struct fetch_negotiator *negotiator,
        int got_ready = 0;
        struct strbuf req_buf = STRBUF_INIT;
        size_t state_len = 0;
+       struct packet_reader reader;
 
        if (args->stateless_rpc && multi_ack == 1)
                die(_("--stateless-rpc requires multi_ack_detailed"));
 
+       packet_reader_init(&reader, fd[0], NULL, 0,
+                          PACKET_READ_CHOMP_NEWLINE);
+
        if (!args->no_dependents) {
                mark_tips(negotiator, args->negotiation_tips);
                for_each_cached_alternate(negotiator, insert_one_alternate_object);
@@ -337,31 +344,30 @@ static int find_common(struct fetch_negotiator *negotiator,
        state_len = req_buf.len;
 
        if (args->deepen) {
-               char *line;
                const char *arg;
                struct object_id oid;
 
                send_request(args, fd[1], &req_buf);
-               while ((line = packet_read_line(fd[0], NULL))) {
-                       if (skip_prefix(line, "shallow ", &arg)) {
+               while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
+                       if (skip_prefix(reader.line, "shallow ", &arg)) {
                                if (get_oid_hex(arg, &oid))
-                                       die(_("invalid shallow line: %s"), line);
+                                       die(_("invalid shallow line: %s"), reader.line);
                                register_shallow(the_repository, &oid);
                                continue;
                        }
-                       if (skip_prefix(line, "unshallow ", &arg)) {
+                       if (skip_prefix(reader.line, "unshallow ", &arg)) {
                                if (get_oid_hex(arg, &oid))
-                                       die(_("invalid unshallow line: %s"), line);
+                                       die(_("invalid unshallow line: %s"), reader.line);
                                if (!lookup_object(the_repository, oid.hash))
-                                       die(_("object not found: %s"), line);
+                                       die(_("object not found: %s"), reader.line);
                                /* make sure that it is parsed as shallow */
                                if (!parse_object(the_repository, &oid))
-                                       die(_("error in object: %s"), line);
+                                       die(_("error in object: %s"), reader.line);
                                if (unregister_shallow(&oid))
-                                       die(_("no shallow found: %s"), line);
+                                       die(_("no shallow found: %s"), reader.line);
                                continue;
                        }
-                       die(_("expected shallow/unshallow, got %s"), line);
+                       die(_("expected shallow/unshallow, got %s"), reader.line);
                }
        } else if (!args->stateless_rpc)
                send_request(args, fd[1], &req_buf);
@@ -398,9 +404,9 @@ static int find_common(struct fetch_negotiator *negotiator,
                        if (!args->stateless_rpc && count == INITIAL_FLUSH)
                                continue;
 
-                       consume_shallow_list(args, fd[0]);
+                       consume_shallow_list(args, &reader);
                        do {
-                               ack = get_ack(fd[0], result_oid);
+                               ack = get_ack(&reader, result_oid);
                                if (ack)
                                        print_verbose(args, _("got %s %d %s"), "ack",
                                                      ack, oid_to_hex(result_oid));
@@ -470,9 +476,9 @@ static int find_common(struct fetch_negotiator *negotiator,
        strbuf_release(&req_buf);
 
        if (!got_ready || !no_done)
-               consume_shallow_list(args, fd[0]);
+               consume_shallow_list(args, &reader);
        while (flushes || multi_ack) {
-               int ack = get_ack(fd[0], result_oid);
+               int ack = get_ack(&reader, result_oid);
                if (ack) {
                        print_verbose(args, _("got %s (%d) %s"), "ack",
                                      ack, oid_to_hex(result_oid));
@@ -532,21 +538,14 @@ static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
                oidset_insert(oids, &refs->old_oid);
 }
 
-static int tip_oids_contain(struct oidset *tip_oids,
-                           struct ref *unmatched, struct ref *newlist,
-                           const struct object_id *id)
+static int is_unmatched_ref(const struct ref *ref)
 {
-       /*
-        * Note that this only looks at the ref lists the first time it's
-        * called. This works out in filter_refs() because even though it may
-        * add to "newlist" between calls, the additions will always be for
-        * oids that are already in the set.
-        */
-       if (!tip_oids->map.map.tablesize) {
-               add_refs_to_oidset(tip_oids, unmatched);
-               add_refs_to_oidset(tip_oids, newlist);
-       }
-       return oidset_contains(tip_oids, id);
+       struct object_id oid;
+       const char *p;
+       return  ref->match_status == REF_NOT_MATCHED &&
+               !parse_oid_hex(ref->name, &oid, &p) &&
+               *p == '\0' &&
+               oideq(&oid, &ref->old_oid);
 }
 
 static void filter_refs(struct fetch_pack_args *args,
@@ -559,6 +558,8 @@ static void filter_refs(struct fetch_pack_args *args,
        struct ref *ref, *next;
        struct oidset tip_oids = OIDSET_INIT;
        int i;
+       int strict = !(allow_unadvertised_object_request &
+                      (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
 
        i = 0;
        for (ref = *refs; ref; ref = next) {
@@ -595,23 +596,25 @@ static void filter_refs(struct fetch_pack_args *args,
                }
        }
 
+       if (strict) {
+               for (i = 0; i < nr_sought; i++) {
+                       ref = sought[i];
+                       if (!is_unmatched_ref(ref))
+                               continue;
+
+                       add_refs_to_oidset(&tip_oids, unmatched);
+                       add_refs_to_oidset(&tip_oids, newlist);
+                       break;
+               }
+       }
+
        /* Append unmatched requests to the list */
        for (i = 0; i < nr_sought; i++) {
-               struct object_id oid;
-               const char *p;
-
                ref = sought[i];
-               if (ref->match_status != REF_NOT_MATCHED)
-                       continue;
-               if (parse_oid_hex(ref->name, &oid, &p) ||
-                   *p != '\0' ||
-                   oidcmp(&oid, &ref->old_oid))
+               if (!is_unmatched_ref(ref))
                        continue;
 
-               if ((allow_unadvertised_object_request &
-                    (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)) ||
-                   tip_oids_contain(&tip_oids, unmatched, newlist,
-                                    &ref->old_oid)) {
+               if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
                        ref->match_status = REF_MATCHED;
                        *newtail = copy_ref(ref);
                        newtail = &(*newtail)->next;
@@ -1252,6 +1255,18 @@ static int process_acks(struct fetch_negotiator *negotiator,
            reader->status != PACKET_READ_DELIM)
                die(_("error processing acks: %d"), reader->status);
 
+       /*
+        * If an "acknowledgments" section is sent, a packfile is sent if and
+        * only if "ready" was sent in this section. The other sections
+        * ("shallow-info" and "wanted-refs") are sent only if a packfile is
+        * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
+        * otherwise.
+        */
+       if (received_ready && reader->status != PACKET_READ_DELIM)
+               die(_("expected packfile to be sent after 'ready'"));
+       if (!received_ready && reader->status != PACKET_READ_FLUSH)
+               die(_("expected no other sections to be sent after no 'ready'"));
+
        /* return 0 if no common, 1 if there are common, or 2 if ready */
        return received_ready ? 2 : (received_ack ? 1 : 0);
 }
@@ -1629,7 +1644,7 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
                parse_list_objects_filter(&args->filter_options, "blob:none");
        }
 
-       if (!ref) {
+       if (version != protocol_v2 && !ref) {
                packet_flush(fd[1]);
                die(_("no matching remote head"));
        }