We've had a few instances where a lazy fetch in a partial clone fails, leading to a fatal error, when the calling code could have easily recovered - in other words, the severity of the bug should have just a wasted fetch instead of stopping the whole command. Part of the issue (and possibly the whole issue - I haven't looked at this beyond fetch-pack yet) is that fetch-pack dies whenever it encounters an error, so I took a look at fixing that. (Note that fetch-pack is sometimes run through a remote helper, meaning that we could leave the die() invocations in and just make sure that we handle failure in the separate process correctly. But when the promisor remote is HTTP protocol v2 or SSH protocol v0/v2, this is not true - fetch_pack() is run in-process.) I think the best way for easy authorship and review is to convert each possibly-dying function into a function that either returns a possibly-null error string or returns success/failure and writes the error string into an "out" parameter. In this way, the change is rather mechanical and should be easy to review. In the patch below I chose the former approach, and I modified 2 functions (one that returns no value and one that returns a value) to demonstrate what it would look like. We could also take this further and have a "struct error" for type safety and macros - e.g. THROW() to return a "struct error", TRY() to execute what's inside the parentheses and return the error if there is one, and OR_DIE() to execute what's inside the parentheses and die if there is an error. Any opinions before I continue working on this? Signed-off-by: Jonathan Tan <jonathantanmy@xxxxxxxxxx> --- fetch-pack.c | 78 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/fetch-pack.c b/fetch-pack.c index 80fb3bd899..20a7e05ea8 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -24,6 +24,8 @@ #include "fsck.h" #include "shallow.h" +typedef char * error_string; + static int transfer_unpack_limit = -1; static int fetch_unpack_limit = -1; static int unpack_limit = 100; @@ -136,8 +138,8 @@ enum ack_type { ACK_ready }; -static void consume_shallow_list(struct fetch_pack_args *args, - struct packet_reader *reader) +static error_string 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" @@ -149,41 +151,54 @@ static void consume_shallow_list(struct fetch_pack_args *args, continue; if (starts_with(reader->line, "unshallow ")) continue; - die(_("git fetch-pack: expected shallow list")); + return xstrdup(_("git fetch-pack: expected shallow list")); } if (reader->status != PACKET_READ_FLUSH) - die(_("git fetch-pack: expected a flush packet after shallow list")); + return xstrdup(_("git fetch-pack: expected a flush packet after shallow list")); } + return NULL; } -static enum ack_type get_ack(struct packet_reader *reader, - struct object_id *result_oid) +static error_string get_ack(struct packet_reader *reader, + enum ack_type *result_ack, + struct object_id *result_oid) { int len; const char *arg; if (packet_reader_read(reader) != PACKET_READ_NORMAL) - die(_("git fetch-pack: expected ACK/NAK, got a flush packet")); + return xstrdup(_("git fetch-pack: expected ACK/NAK, got a flush packet")); len = reader->pktlen; - if (!strcmp(reader->line, "NAK")) - return NAK; + if (!strcmp(reader->line, "NAK")) { + *result_ack = NAK; + return NULL; + } if (skip_prefix(reader->line, "ACK ", &arg)) { const char *p; if (!parse_oid_hex(arg, result_oid, &p)) { len -= p - reader->line; - if (len < 1) - return ACK; - if (strstr(p, "continue")) - return ACK_continue; - if (strstr(p, "common")) - return ACK_common; - if (strstr(p, "ready")) - return ACK_ready; - return ACK; + if (len < 1) { + *result_ack = ACK; + return NULL; + } + if (strstr(p, "continue")) { + *result_ack = ACK_continue; + return NULL; + } + if (strstr(p, "common")) { + *result_ack = ACK_common; + return NULL; + } + if (strstr(p, "ready")) { + *result_ack = ACK_ready; + return NULL; + } + *result_ack = ACK; + return NULL; } } - die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line); + return xstrfmt(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line); } static void send_request(struct fetch_pack_args *args, @@ -394,7 +409,8 @@ static int find_common(struct fetch_negotiator *negotiator, print_verbose(args, "have %s", oid_to_hex(oid)); in_vain++; if (flush_at <= ++count) { - int ack; + enum ack_type ack; + error_string err; packet_buf_flush(&req_buf); send_request(args, fd[1], &req_buf); @@ -409,9 +425,11 @@ static int find_common(struct fetch_negotiator *negotiator, if (!args->stateless_rpc && count == INITIAL_FLUSH) continue; - consume_shallow_list(args, &reader); + if ((err = consume_shallow_list(args, &reader))) + die("%s", err); do { - ack = get_ack(&reader, result_oid); + if ((err = get_ack(&reader, &ack, result_oid))) + die("%s", err); if (ack) print_verbose(args, _("got %s %d %s"), "ack", ack, oid_to_hex(result_oid)); @@ -457,6 +475,9 @@ static int find_common(struct fetch_negotiator *negotiator, got_ready = 1; break; } + case NAK: + /* nothing */ + break; } } while (ack); flushes--; @@ -481,10 +502,17 @@ static int find_common(struct fetch_negotiator *negotiator, } strbuf_release(&req_buf); - if (!got_ready || !no_done) - consume_shallow_list(args, &reader); + if (!got_ready || !no_done) { + error_string err; + if ((err = consume_shallow_list(args, &reader))) + die("%s", err); + } while (flushes || multi_ack) { - int ack = get_ack(&reader, result_oid); + error_string err; + enum ack_type ack; + + if ((err = get_ack(&reader, &ack, result_oid))) + die("%s", err); if (ack) { print_verbose(args, _("got %s (%d) %s"), "ack", ack, oid_to_hex(result_oid)); -- 2.28.0.rc0.142.g3c755180ce-goog