builtin/remote.c: add and use a REF_STATES_INIT

Use a new REF_STATES_INIT designated initializer instead of assigning to the "strdup_strings" member of the previously memzero()'d version of this struct. The pattern of assigning to "strdup_strings" dates back to 211c89682ee (Make git-remote a builtin, 2008-02-29) (when it was "strdup_paths"), i.e. long before we used anything like our current established *_INIT patterns consistently. Then in e61e0cc6b70 (builtin-remote: teach show to display remote HEAD, 2009-02-25) and e5dcbfd9ab7 (builtin-remote: new show output style for push refspecs, 2009-02-25) we added some more of these. As it turns out we only initialized this struct three times, all the other uses were of pointers to those initialized structs. So let's initialize it in those three places, skip the memset(), and pass those structs down appropriately. This would be a behavior change if we had codepaths that relied say on implicitly having had "new_refs" initialized to STRING_LIST_INIT_NODUP with the memset(), but only set the "strdup_strings" on some other struct, but then called string_list_append() on "new_refs". There isn't any such codepath, all of the late assignments to "strdup_strings" assigned to those structs that we'd use for those codepaths. So just initializing them all up-front makes for easier to understand code, i.e. in the pre-image it looked as though we had that tricky edge case, but we didn't. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Oct 1, 2021 at 12:27 UTC 0bc7787ca9ea123e28769f728d77261cad1f1557
1 file changed +11 -12
builtin/remote.c
+11 -12
@@ -344,6 +344,14 @@ struct ref_states {
344 int queried;
345 };
346
347 +#define REF_STATES_INIT { \
348 + .new_refs = STRING_LIST_INIT_DUP, \
349 + .stale = STRING_LIST_INIT_DUP, \
350 + .tracked = STRING_LIST_INIT_DUP, \
351 + .heads = STRING_LIST_INIT_DUP, \
352 + .push = STRING_LIST_INIT_DUP, \
353 +}
354 +
355 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
356 {
357 struct ref *fetch_map = NULL, **tail = &fetch_map;
@@ -355,9 +363,6 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
363 die(_("Could not get fetch map for refspec %s"),
364 states->remote->fetch.raw[i]);
365
358 - states->new_refs.strdup_strings = 1;
359 - states->tracked.strdup_strings = 1;
360 - states->stale.strdup_strings = 1;
366 for (ref = fetch_map; ref; ref = ref->next) {
367 if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
368 string_list_append(&states->new_refs, abbrev_branch(ref->name));
@@ -406,7 +411,6 @@ static int get_push_ref_states(const struct ref *remote_refs,
411
412 match_push_refs(local_refs, &push_map, &remote->push, MATCH_REFS_NONE);
413
409 - states->push.strdup_strings = 1;
414 for (ref = push_map; ref; ref = ref->next) {
415 struct string_list_item *item;
416 struct push_info *info;
@@ -449,7 +453,6 @@ static int get_push_ref_states_noquery(struct ref_states *states)
453 if (remote->mirror)
454 return 0;
455
452 - states->push.strdup_strings = 1;
456 if (!remote->push.nr) {
457 item = string_list_append(&states->push, _("(matching)"));
458 info = item->util = xcalloc(1, sizeof(struct push_info));
@@ -483,7 +486,6 @@ static int get_head_names(const struct ref *remote_refs, struct ref_states *stat
486 refspec.force = 0;
487 refspec.pattern = 1;
488 refspec.src = refspec.dst = "refs/heads/*";
486 - states->heads.strdup_strings = 1;
489 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
490 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
491 fetch_map, 1);
@@ -1212,7 +1214,7 @@ static int show(int argc, const char **argv)
1214 OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
1215 OPT_END()
1216 };
1215 - struct ref_states states;
1217 + struct ref_states states = REF_STATES_INIT;
1218 struct string_list info_list = STRING_LIST_INIT_NODUP;
1219 struct show_info info;
1220
@@ -1225,7 +1227,6 @@ static int show(int argc, const char **argv)
1227 if (!no_query)
1228 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1229
1228 - memset(&states, 0, sizeof(states));
1230 memset(&info, 0, sizeof(info));
1231 info.states = &states;
1232 info.list = &info_list;
@@ -1334,8 +1335,7 @@ static int set_head(int argc, const char **argv)
1335 if (!opt_a && !opt_d && argc == 2) {
1336 head_name = xstrdup(argv[1]);
1337 } else if (opt_a && !opt_d && argc == 1) {
1337 - struct ref_states states;
1338 - memset(&states, 0, sizeof(states));
1338 + struct ref_states states = REF_STATES_INIT;
1339 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1340 if (!states.heads.nr)
1341 result |= error(_("Cannot determine remote HEAD"));
@@ -1374,14 +1374,13 @@ static int set_head(int argc, const char **argv)
1374 static int prune_remote(const char *remote, int dry_run)
1375 {
1376 int result = 0;
1377 - struct ref_states states;
1377 + struct ref_states states = REF_STATES_INIT;
1378 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
1379 struct string_list_item *item;
1380 const char *dangling_msg = dry_run
1381 ? _(" %s will become dangling!")
1382 : _(" %s has become dangling!");
1383
1384 - memset(&states, 0, sizeof(states));
1384 get_remote_ref_states(remote, &states, GET_REF_STATES);
1385
1386 if (!states.stale.nr) {