clean up interface for refs_warn_dangling_symrefs

The refs_warn_dangling_symrefs interface is a bit fragile as it passes in printf-formatting strings with expectations about the number of arguments. This patch series made it worse by adding a 2nd positional argument. But there are only two call sites, and they both use almost identical display options. Make this safer by moving the format strings into the function that uses them to make it easier to see when the arguments don't match. Pass a prefix string and a dry_run flag so the decision logic can be handled where needed. Signed-off-by: Phil Hord <phil.hord@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phil Hord committed Jul 1, 2025 at 18:12 UTC 87d8d8c5d09b1ee52cdf472b53b370020a7cb41c
4 files changed +15 -15
builtin/fetch.c
+1 -4
@@ -1385,9 +1385,6 @@ static int prune_refs(struct display_state *display_state,
1385 struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);
1386 struct strbuf err = STRBUF_INIT;
1387 struct string_list refnames = STRING_LIST_INIT_NODUP;
1388 - const char *dangling_msg = dry_run
1389 - ? _(" %s will become dangling after %s is deleted")
1390 - : _(" %s has become dangling after %s was deleted");
1388
1389 for (ref = stale_refs; ref; ref = ref->next)
1390 string_list_append(&refnames, ref->name);
@@ -1418,7 +1415,7 @@ static int prune_refs(struct display_state *display_state,
1415 }
1416 string_list_sort(&refnames);
1417 refs_warn_dangling_symrefs(get_main_ref_store(the_repository),
1421 - stderr, dangling_msg, &refnames);
1418 + stderr, " ", dry_run, &refnames);
1419 }
1420
1421 cleanup:
builtin/remote.c
+1 -4
@@ -1515,9 +1515,6 @@ static int prune_remote(const char *remote, int dry_run)
1515 struct ref_states states = REF_STATES_INIT;
1516 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
1517 struct string_list_item *item;
1518 - const char *dangling_msg = dry_run
1519 - ? _(" %s will become dangling after %s is deleted!")
1520 - : _(" %s has become dangling after %s was deleted!");
1518
1519 get_remote_ref_states(remote, &states, GET_REF_STATES);
1520
@@ -1549,7 +1546,7 @@ static int prune_remote(const char *remote, int dry_run)
1546 }
1547
1548 refs_warn_dangling_symrefs(get_main_ref_store(the_repository),
1552 - stdout, dangling_msg, &refs_to_prune);
1549 + stdout, " ", dry_run, &refs_to_prune);
1550
1551 string_list_clear(&refs_to_prune, 0);
1552 free_remote_ref_states(&states);
refs.c
+11 -6
@@ -439,7 +439,8 @@ struct warn_if_dangling_data {
439 struct ref_store *refs;
440 FILE *fp;
441 const struct string_list *refnames;
442 - const char *msg_fmt;
442 + const char *indent;
443 + int dry_run;
444 };
445
446 static int warn_if_dangling_symref(const char *refname, const char *referent UNUSED,
@@ -447,7 +448,7 @@ static int warn_if_dangling_symref(const char *refname, const char *referent UNU
448 int flags, void *cb_data)
449 {
450 struct warn_if_dangling_data *d = cb_data;
450 - const char *resolves_to;
451 + const char *resolves_to, *msg;
452
453 if (!(flags & REF_ISSYMREF))
454 return 0;
@@ -458,19 +459,23 @@ static int warn_if_dangling_symref(const char *refname, const char *referent UNU
459 return 0;
460 }
461
461 - fprintf(d->fp, d->msg_fmt, refname, resolves_to);
462 - fputc('\n', d->fp);
462 + msg = d->dry_run
463 + ? _("%s%s will become dangling after %s is deleted\n")
464 + : _("%s%s has become dangling after %s was deleted\n");
465 + fprintf(d->fp, msg, d->indent, refname, resolves_to);
466 return 0;
467 }
468
469 void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp,
467 - const char *msg_fmt, const struct string_list *refnames)
470 + const char *indent, int dry_run,
471 + const struct string_list *refnames)
472 {
473 struct warn_if_dangling_data data = {
474 .refs = refs,
475 .fp = fp,
476 .refnames = refnames,
473 - .msg_fmt = msg_fmt,
477 + .indent = indent,
478 + .dry_run = dry_run,
479 };
480 refs_for_each_rawref(refs, warn_if_dangling_symref, &data);
481 }
refs.h
+2 -1
@@ -436,7 +436,8 @@ static inline const char *has_glob_specials(const char *pattern)
436 }
437
438 void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp,
439 - const char *msg_fmt, const struct string_list *refnames);
439 + const char *indent, int dry_run,
440 + const struct string_list *refnames);
441
442 /*
443 * Flags for controlling behaviour of pack_refs()