Skip to content

Commit 0bc7787

Browse files
avargitster
authored andcommitted
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 211c896 (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 e61e0cc (builtin-remote: teach show to display remote HEAD, 2009-02-25) and e5dcbfd (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 73ee449 commit 0bc7787

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

builtin/remote.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,14 @@ struct ref_states {
344344
int queried;
345345
};
346346

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+
347355
static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
348356
{
349357
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
355363
die(_("Could not get fetch map for refspec %s"),
356364
states->remote->fetch.raw[i]);
357365

358-
states->new_refs.strdup_strings = 1;
359-
states->tracked.strdup_strings = 1;
360-
states->stale.strdup_strings = 1;
361366
for (ref = fetch_map; ref; ref = ref->next) {
362367
if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
363368
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,
406411

407412
match_push_refs(local_refs, &push_map, &remote->push, MATCH_REFS_NONE);
408413

409-
states->push.strdup_strings = 1;
410414
for (ref = push_map; ref; ref = ref->next) {
411415
struct string_list_item *item;
412416
struct push_info *info;
@@ -449,7 +453,6 @@ static int get_push_ref_states_noquery(struct ref_states *states)
449453
if (remote->mirror)
450454
return 0;
451455

452-
states->push.strdup_strings = 1;
453456
if (!remote->push.nr) {
454457
item = string_list_append(&states->push, _("(matching)"));
455458
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
483486
refspec.force = 0;
484487
refspec.pattern = 1;
485488
refspec.src = refspec.dst = "refs/heads/*";
486-
states->heads.strdup_strings = 1;
487489
get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
488490
matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
489491
fetch_map, 1);
@@ -1212,7 +1214,7 @@ static int show(int argc, const char **argv)
12121214
OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
12131215
OPT_END()
12141216
};
1215-
struct ref_states states;
1217+
struct ref_states states = REF_STATES_INIT;
12161218
struct string_list info_list = STRING_LIST_INIT_NODUP;
12171219
struct show_info info;
12181220

@@ -1225,7 +1227,6 @@ static int show(int argc, const char **argv)
12251227
if (!no_query)
12261228
query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
12271229

1228-
memset(&states, 0, sizeof(states));
12291230
memset(&info, 0, sizeof(info));
12301231
info.states = &states;
12311232
info.list = &info_list;
@@ -1334,8 +1335,7 @@ static int set_head(int argc, const char **argv)
13341335
if (!opt_a && !opt_d && argc == 2) {
13351336
head_name = xstrdup(argv[1]);
13361337
} 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;
13391339
get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
13401340
if (!states.heads.nr)
13411341
result |= error(_("Cannot determine remote HEAD"));
@@ -1374,14 +1374,13 @@ static int set_head(int argc, const char **argv)
13741374
static int prune_remote(const char *remote, int dry_run)
13751375
{
13761376
int result = 0;
1377-
struct ref_states states;
1377+
struct ref_states states = REF_STATES_INIT;
13781378
struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
13791379
struct string_list_item *item;
13801380
const char *dangling_msg = dry_run
13811381
? _(" %s will become dangling!")
13821382
: _(" %s has become dangling!");
13831383

1384-
memset(&states, 0, sizeof(states));
13851384
get_remote_ref_states(remote, &states, GET_REF_STATES);
13861385

13871386
if (!states.stale.nr) {

0 commit comments

Comments
 (0)