@@ -347,6 +347,14 @@ struct ref_states {
347347 int queried ;
348348};
349349
350+ #define REF_STATES_INIT { \
351+ .new_refs = STRING_LIST_INIT_DUP, \
352+ .stale = STRING_LIST_INIT_DUP, \
353+ .tracked = STRING_LIST_INIT_DUP, \
354+ .heads = STRING_LIST_INIT_DUP, \
355+ .push = STRING_LIST_INIT_DUP, \
356+ }
357+
350358static int get_ref_states (const struct ref * remote_refs , struct ref_states * states )
351359{
352360 struct ref * fetch_map = NULL , * * tail = & fetch_map ;
@@ -358,9 +366,6 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
358366 die (_ ("Could not get fetch map for refspec %s" ),
359367 states -> remote -> fetch .raw [i ]);
360368
361- states -> new_refs .strdup_strings = 1 ;
362- states -> tracked .strdup_strings = 1 ;
363- states -> stale .strdup_strings = 1 ;
364369 for (ref = fetch_map ; ref ; ref = ref -> next ) {
365370 if (!ref -> peer_ref || !ref_exists (ref -> peer_ref -> name ))
366371 string_list_append (& states -> new_refs , abbrev_branch (ref -> name ));
@@ -409,7 +414,6 @@ static int get_push_ref_states(const struct ref *remote_refs,
409414
410415 match_push_refs (local_refs , & push_map , & remote -> push , MATCH_REFS_NONE );
411416
412- states -> push .strdup_strings = 1 ;
413417 for (ref = push_map ; ref ; ref = ref -> next ) {
414418 struct string_list_item * item ;
415419 struct push_info * info ;
@@ -452,7 +456,6 @@ static int get_push_ref_states_noquery(struct ref_states *states)
452456 if (remote -> mirror )
453457 return 0 ;
454458
455- states -> push .strdup_strings = 1 ;
456459 if (!remote -> push .nr ) {
457460 item = string_list_append (& states -> push , _ ("(matching)" ));
458461 info = item -> util = xcalloc (1 , sizeof (struct push_info ));
@@ -486,7 +489,6 @@ static int get_head_names(const struct ref *remote_refs, struct ref_states *stat
486489 refspec .force = 0 ;
487490 refspec .pattern = 1 ;
488491 refspec .src = refspec .dst = "refs/heads/*" ;
489- states -> heads .strdup_strings = 1 ;
490492 get_fetch_map (remote_refs , & refspec , & fetch_map_tail , 0 );
491493 matches = guess_remote_head (find_ref_by_name (remote_refs , "HEAD" ),
492494 fetch_map , 1 );
@@ -973,26 +975,31 @@ static int get_remote_ref_states(const char *name,
973975}
974976
975977struct show_info {
976- struct string_list * list ;
977- struct ref_states * states ;
978+ struct string_list list ;
979+ struct ref_states states ;
978980 int width , width2 ;
979981 int any_rebase ;
980982};
981983
984+ #define SHOW_INFO_INIT { \
985+ .list = STRING_LIST_INIT_DUP, \
986+ .states = REF_STATES_INIT, \
987+ }
988+
982989static int add_remote_to_show_info (struct string_list_item * item , void * cb_data )
983990{
984991 struct show_info * info = cb_data ;
985992 int n = strlen (item -> string );
986993 if (n > info -> width )
987994 info -> width = n ;
988- string_list_insert (info -> list , item -> string );
995+ string_list_insert (& info -> list , item -> string );
989996 return 0 ;
990997}
991998
992999static int show_remote_info_item (struct string_list_item * item , void * cb_data )
9931000{
9941001 struct show_info * info = cb_data ;
995- struct ref_states * states = info -> states ;
1002+ struct ref_states * states = & info -> states ;
9961003 const char * name = item -> string ;
9971004
9981005 if (states -> queried ) {
@@ -1019,7 +1026,7 @@ static int show_remote_info_item(struct string_list_item *item, void *cb_data)
10191026static int add_local_to_show_info (struct string_list_item * branch_item , void * cb_data )
10201027{
10211028 struct show_info * show_info = cb_data ;
1022- struct ref_states * states = show_info -> states ;
1029+ struct ref_states * states = & show_info -> states ;
10231030 struct branch_info * branch_info = branch_item -> util ;
10241031 struct string_list_item * item ;
10251032 int n ;
@@ -1032,7 +1039,7 @@ static int add_local_to_show_info(struct string_list_item *branch_item, void *cb
10321039 if (branch_info -> rebase >= REBASE_TRUE )
10331040 show_info -> any_rebase = 1 ;
10341041
1035- item = string_list_insert (show_info -> list , branch_item -> string );
1042+ item = string_list_insert (& show_info -> list , branch_item -> string );
10361043 item -> util = branch_info ;
10371044
10381045 return 0 ;
@@ -1087,7 +1094,7 @@ static int add_push_to_show_info(struct string_list_item *push_item, void *cb_da
10871094 show_info -> width = n ;
10881095 if ((n = strlen (push_info -> dest )) > show_info -> width2 )
10891096 show_info -> width2 = n ;
1090- item = string_list_append (show_info -> list , push_item -> string );
1097+ item = string_list_append (& show_info -> list , push_item -> string );
10911098 item -> util = push_item -> util ;
10921099 return 0 ;
10931100}
@@ -1215,9 +1222,7 @@ static int show(int argc, const char **argv)
12151222 OPT_BOOL ('n' , NULL , & no_query , N_ ("do not query remotes" )),
12161223 OPT_END ()
12171224 };
1218- struct ref_states states ;
1219- struct string_list info_list = STRING_LIST_INIT_NODUP ;
1220- struct show_info info ;
1225+ struct show_info info = SHOW_INFO_INIT ;
12211226
12221227 argc = parse_options (argc , argv , NULL , options , builtin_remote_show_usage ,
12231228 0 );
@@ -1228,26 +1233,22 @@ static int show(int argc, const char **argv)
12281233 if (!no_query )
12291234 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES );
12301235
1231- memset (& states , 0 , sizeof (states ));
1232- memset (& info , 0 , sizeof (info ));
1233- info .states = & states ;
1234- info .list = & info_list ;
12351236 for (; argc ; argc -- , argv ++ ) {
12361237 int i ;
12371238 const char * * url ;
12381239 int url_nr ;
12391240
1240- get_remote_ref_states (* argv , & states , query_flag );
1241+ get_remote_ref_states (* argv , & info . states , query_flag );
12411242
12421243 printf_ln (_ ("* remote %s" ), * argv );
1243- printf_ln (_ (" Fetch URL: %s" ), states .remote -> url_nr > 0 ?
1244- states .remote -> url [0 ] : _ ("(no URL)" ));
1245- if (states .remote -> pushurl_nr ) {
1246- url = states .remote -> pushurl ;
1247- url_nr = states .remote -> pushurl_nr ;
1244+ printf_ln (_ (" Fetch URL: %s" ), info . states .remote -> url_nr > 0 ?
1245+ info . states .remote -> url [0 ] : _ ("(no URL)" ));
1246+ if (info . states .remote -> pushurl_nr ) {
1247+ url = info . states .remote -> pushurl ;
1248+ url_nr = info . states .remote -> pushurl_nr ;
12481249 } else {
1249- url = states .remote -> url ;
1250- url_nr = states .remote -> url_nr ;
1250+ url = info . states .remote -> url ;
1251+ url_nr = info . states .remote -> url_nr ;
12511252 }
12521253 for (i = 0 ; i < url_nr ; i ++ )
12531254 /*
@@ -1260,57 +1261,57 @@ static int show(int argc, const char **argv)
12601261 printf_ln (_ (" Push URL: %s" ), _ ("(no URL)" ));
12611262 if (no_query )
12621263 printf_ln (_ (" HEAD branch: %s" ), _ ("(not queried)" ));
1263- else if (!states .heads .nr )
1264+ else if (!info . states .heads .nr )
12641265 printf_ln (_ (" HEAD branch: %s" ), _ ("(unknown)" ));
1265- else if (states .heads .nr == 1 )
1266- printf_ln (_ (" HEAD branch: %s" ), states .heads .items [0 ].string );
1266+ else if (info . states .heads .nr == 1 )
1267+ printf_ln (_ (" HEAD branch: %s" ), info . states .heads .items [0 ].string );
12671268 else {
12681269 printf (_ (" HEAD branch (remote HEAD is ambiguous,"
12691270 " may be one of the following):\n" ));
1270- for (i = 0 ; i < states .heads .nr ; i ++ )
1271- printf (" %s\n" , states .heads .items [i ].string );
1271+ for (i = 0 ; i < info . states .heads .nr ; i ++ )
1272+ printf (" %s\n" , info . states .heads .items [i ].string );
12721273 }
12731274
12741275 /* remote branch info */
12751276 info .width = 0 ;
1276- for_each_string_list (& states .new_refs , add_remote_to_show_info , & info );
1277- for_each_string_list (& states .tracked , add_remote_to_show_info , & info );
1278- for_each_string_list (& states .stale , add_remote_to_show_info , & info );
1279- if (info .list -> nr )
1277+ for_each_string_list (& info . states .new_refs , add_remote_to_show_info , & info );
1278+ for_each_string_list (& info . states .tracked , add_remote_to_show_info , & info );
1279+ for_each_string_list (& info . states .stale , add_remote_to_show_info , & info );
1280+ if (info .list . nr )
12801281 printf_ln (Q_ (" Remote branch:%s" ,
12811282 " Remote branches:%s" ,
1282- info .list -> nr ),
1283+ info .list . nr ),
12831284 no_query ? _ (" (status not queried)" ) : "" );
1284- for_each_string_list (info .list , show_remote_info_item , & info );
1285- string_list_clear (info .list , 0 );
1285+ for_each_string_list (& info .list , show_remote_info_item , & info );
1286+ string_list_clear (& info .list , 0 );
12861287
12871288 /* git pull info */
12881289 info .width = 0 ;
12891290 info .any_rebase = 0 ;
12901291 for_each_string_list (& branch_list , add_local_to_show_info , & info );
1291- if (info .list -> nr )
1292+ if (info .list . nr )
12921293 printf_ln (Q_ (" Local branch configured for 'git pull':" ,
12931294 " Local branches configured for 'git pull':" ,
1294- info .list -> nr ));
1295- for_each_string_list (info .list , show_local_info_item , & info );
1296- string_list_clear (info .list , 0 );
1295+ info .list . nr ));
1296+ for_each_string_list (& info .list , show_local_info_item , & info );
1297+ string_list_clear (& info .list , 0 );
12971298
12981299 /* git push info */
1299- if (states .remote -> mirror )
1300+ if (info . states .remote -> mirror )
13001301 printf_ln (_ (" Local refs will be mirrored by 'git push'" ));
13011302
13021303 info .width = info .width2 = 0 ;
1303- for_each_string_list (& states .push , add_push_to_show_info , & info );
1304- QSORT (info .list -> items , info .list -> nr , cmp_string_with_push );
1305- if (info .list -> nr )
1304+ for_each_string_list (& info . states .push , add_push_to_show_info , & info );
1305+ QSORT (info .list . items , info .list . nr , cmp_string_with_push );
1306+ if (info .list . nr )
13061307 printf_ln (Q_ (" Local ref configured for 'git push'%s:" ,
13071308 " Local refs configured for 'git push'%s:" ,
1308- info .list -> nr ),
1309+ info .list . nr ),
13091310 no_query ? _ (" (status not queried)" ) : "" );
1310- for_each_string_list (info .list , show_push_info_item , & info );
1311- string_list_clear (info .list , 0 );
1311+ for_each_string_list (& info .list , show_push_info_item , & info );
1312+ string_list_clear (& info .list , 0 );
13121313
1313- free_remote_ref_states (& states );
1314+ free_remote_ref_states (& info . states );
13141315 }
13151316
13161317 return result ;
@@ -1337,8 +1338,7 @@ static int set_head(int argc, const char **argv)
13371338 if (!opt_a && !opt_d && argc == 2 ) {
13381339 head_name = xstrdup (argv [1 ]);
13391340 } else if (opt_a && !opt_d && argc == 1 ) {
1340- struct ref_states states ;
1341- memset (& states , 0 , sizeof (states ));
1341+ struct ref_states states = REF_STATES_INIT ;
13421342 get_remote_ref_states (argv [0 ], & states , GET_HEAD_NAMES );
13431343 if (!states .heads .nr )
13441344 result |= error (_ ("Cannot determine remote HEAD" ));
@@ -1377,14 +1377,13 @@ static int set_head(int argc, const char **argv)
13771377static int prune_remote (const char * remote , int dry_run )
13781378{
13791379 int result = 0 ;
1380- struct ref_states states ;
1380+ struct ref_states states = REF_STATES_INIT ;
13811381 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP ;
13821382 struct string_list_item * item ;
13831383 const char * dangling_msg = dry_run
13841384 ? _ (" %s will become dangling!" )
13851385 : _ (" %s has become dangling!" );
13861386
1387- memset (& states , 0 , sizeof (states ));
13881387 get_remote_ref_states (remote , & states , GET_REF_STATES );
13891388
13901389 if (!states .stale .nr ) {
0 commit comments