Skip to content

Commit 87d8d8c

Browse files
phordgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0f84695 commit 87d8d8c

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

builtin/fetch.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,9 +1385,6 @@ static int prune_refs(struct display_state *display_state,
13851385
struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);
13861386
struct strbuf err = STRBUF_INIT;
13871387
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");
13911388

13921389
for (ref = stale_refs; ref; ref = ref->next)
13931390
string_list_append(&refnames, ref->name);
@@ -1418,7 +1415,7 @@ static int prune_refs(struct display_state *display_state,
14181415
}
14191416
string_list_sort(&refnames);
14201417
refs_warn_dangling_symrefs(get_main_ref_store(the_repository),
1421-
stderr, dangling_msg, &refnames);
1418+
stderr, " ", dry_run, &refnames);
14221419
}
14231420

14241421
cleanup:

builtin/remote.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,9 +1515,6 @@ static int prune_remote(const char *remote, int dry_run)
15151515
struct ref_states states = REF_STATES_INIT;
15161516
struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
15171517
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!");
15211518

15221519
get_remote_ref_states(remote, &states, GET_REF_STATES);
15231520

@@ -1549,7 +1546,7 @@ static int prune_remote(const char *remote, int dry_run)
15491546
}
15501547

15511548
refs_warn_dangling_symrefs(get_main_ref_store(the_repository),
1552-
stdout, dangling_msg, &refs_to_prune);
1549+
stdout, " ", dry_run, &refs_to_prune);
15531550

15541551
string_list_clear(&refs_to_prune, 0);
15551552
free_remote_ref_states(&states);

refs.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,15 +439,16 @@ struct warn_if_dangling_data {
439439
struct ref_store *refs;
440440
FILE *fp;
441441
const struct string_list *refnames;
442-
const char *msg_fmt;
442+
const char *indent;
443+
int dry_run;
443444
};
444445

445446
static int warn_if_dangling_symref(const char *refname, const char *referent UNUSED,
446447
const struct object_id *oid UNUSED,
447448
int flags, void *cb_data)
448449
{
449450
struct warn_if_dangling_data *d = cb_data;
450-
const char *resolves_to;
451+
const char *resolves_to, *msg;
451452

452453
if (!(flags & REF_ISSYMREF))
453454
return 0;
@@ -458,19 +459,23 @@ static int warn_if_dangling_symref(const char *refname, const char *referent UNU
458459
return 0;
459460
}
460461

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);
463466
return 0;
464467
}
465468

466469
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)
468472
{
469473
struct warn_if_dangling_data data = {
470474
.refs = refs,
471475
.fp = fp,
472476
.refnames = refnames,
473-
.msg_fmt = msg_fmt,
477+
.indent = indent,
478+
.dry_run = dry_run,
474479
};
475480
refs_for_each_rawref(refs, warn_if_dangling_symref, &data);
476481
}

refs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,8 @@ static inline const char *has_glob_specials(const char *pattern)
436436
}
437437

438438
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);
440441

441442
/*
442443
* Flags for controlling behaviour of pack_refs()

0 commit comments

Comments
 (0)