Skip to content

Commit 973703e

Browse files
remote-curl: introduce show_http_message_fatal() helper
Several code paths in remote-curl.c follow the same pattern of calling show_http_message() to display server error messages followed by die() to terminate with an error. This duplication makes the code more verbose and harder to maintain. Introduce a new show_http_message_fatal() helper function that combines these two operations. This function: 1. Displays any HTTP error message from the server via show_http_message() 2. Calls die() with the provided error message 3. Returns NORETURN to help the compiler with control flow analysis Refactor existing call sites in remote-curl.c to use this new helper, reducing code duplication and improving readability. This pattern will also be used by upcoming HTTP 429 rate limiting support. Suggested-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Vaidas Pilkauskas <vaidas.pilkauskas@shopify.com>
1 parent 3e0b78c commit 973703e

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

remote-curl.c

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -367,31 +367,42 @@ static void free_discovery(struct discovery *d)
367367
}
368368
}
369369

370-
static int show_http_message(struct strbuf *type, struct strbuf *charset,
371-
struct strbuf *msg)
370+
static NORETURN void show_http_message_fatal(struct strbuf *type, struct strbuf *charset,
371+
struct strbuf *msg, const char *fmt, ...)
372372
{
373373
const char *p, *eol;
374+
va_list ap;
375+
report_fn die_message_routine = get_die_message_routine();
374376

375377
/*
376378
* We only show text/plain parts, as other types are likely
377379
* to be ugly to look at on the user's terminal.
378380
*/
379381
if (strcmp(type->buf, "text/plain"))
380-
return -1;
382+
goto out;
381383
if (charset->len)
382384
strbuf_reencode(msg, charset->buf, get_log_output_encoding());
383385

384386
strbuf_trim(msg);
385387
if (!msg->len)
386-
return -1;
388+
goto out;
387389

388390
p = msg->buf;
389391
do {
390392
eol = strchrnul(p, '\n');
391393
fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
392394
p = eol + 1;
393395
} while(*eol);
394-
return 0;
396+
397+
out:
398+
strbuf_release(type);
399+
strbuf_release(charset);
400+
strbuf_release(msg);
401+
402+
va_start(ap, fmt);
403+
die_message_routine(fmt, ap);
404+
va_end(ap);
405+
exit(128);
395406
}
396407

397408
static int get_protocol_http_header(enum protocol_version version,
@@ -518,21 +529,21 @@ static struct discovery *discover_refs(const char *service, int for_push)
518529
case HTTP_OK:
519530
break;
520531
case HTTP_MISSING_TARGET:
521-
show_http_message(&type, &charset, &buffer);
522-
die(_("repository '%s' not found"),
523-
transport_anonymize_url(url.buf));
532+
show_http_message_fatal(&type, &charset, &buffer,
533+
_("repository '%s' not found"),
534+
transport_anonymize_url(url.buf));
524535
case HTTP_NOAUTH:
525-
show_http_message(&type, &charset, &buffer);
526-
die(_("Authentication failed for '%s'"),
527-
transport_anonymize_url(url.buf));
536+
show_http_message_fatal(&type, &charset, &buffer,
537+
_("Authentication failed for '%s'"),
538+
transport_anonymize_url(url.buf));
528539
case HTTP_NOMATCHPUBLICKEY:
529-
show_http_message(&type, &charset, &buffer);
530-
die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
531-
transport_anonymize_url(url.buf), curl_errorstr);
540+
show_http_message_fatal(&type, &charset, &buffer,
541+
_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
542+
transport_anonymize_url(url.buf), curl_errorstr);
532543
default:
533-
show_http_message(&type, &charset, &buffer);
534-
die(_("unable to access '%s': %s"),
535-
transport_anonymize_url(url.buf), curl_errorstr);
544+
show_http_message_fatal(&type, &charset, &buffer,
545+
_("unable to access '%s': %s"),
546+
transport_anonymize_url(url.buf), curl_errorstr);
536547
}
537548

538549
if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {

0 commit comments

Comments
 (0)