Git fork

Merge branch 'ab/designated-initializers-more'

Code clean-up.

* ab/designated-initializers-more:
builtin/remote.c: add and use SHOW_INFO_INIT
builtin/remote.c: add and use a REF_STATES_INIT
urlmatch.[ch]: add and use URLMATCH_CONFIG_INIT
builtin/blame.c: refactor commit_info_init() to COMMIT_INFO_INIT macro
daemon.c: refactor hostinfo_init() to HOSTINFO_INIT macro

+82 -88
+13 -17
builtin/blame.c
··· 101 101 struct strbuf summary; 102 102 }; 103 103 104 + #define COMMIT_INFO_INIT { \ 105 + .author = STRBUF_INIT, \ 106 + .author_mail = STRBUF_INIT, \ 107 + .author_tz = STRBUF_INIT, \ 108 + .committer = STRBUF_INIT, \ 109 + .committer_mail = STRBUF_INIT, \ 110 + .committer_tz = STRBUF_INIT, \ 111 + .summary = STRBUF_INIT, \ 112 + } 113 + 104 114 /* 105 115 * Parse author/committer line in the commit object buffer 106 116 */ ··· 160 170 strbuf_add(name, namebuf, namelen); 161 171 } 162 172 163 - static void commit_info_init(struct commit_info *ci) 164 - { 165 - 166 - strbuf_init(&ci->author, 0); 167 - strbuf_init(&ci->author_mail, 0); 168 - strbuf_init(&ci->author_tz, 0); 169 - strbuf_init(&ci->committer, 0); 170 - strbuf_init(&ci->committer_mail, 0); 171 - strbuf_init(&ci->committer_tz, 0); 172 - strbuf_init(&ci->summary, 0); 173 - } 174 - 175 173 static void commit_info_destroy(struct commit_info *ci) 176 174 { 177 175 ··· 192 190 const char *subject, *encoding; 193 191 const char *message; 194 192 195 - commit_info_init(ret); 196 - 197 193 encoding = get_log_output_encoding(); 198 194 message = logmsg_reencode(commit, NULL, encoding); 199 195 get_ac_line(message, "\nauthor ", ··· 246 242 */ 247 243 static int emit_one_suspect_detail(struct blame_origin *suspect, int repeat) 248 244 { 249 - struct commit_info ci; 245 + struct commit_info ci = COMMIT_INFO_INIT; 250 246 251 247 if (!repeat && (suspect->commit->object.flags & METAINFO_SHOWN)) 252 248 return 0; ··· 440 436 int cnt; 441 437 const char *cp; 442 438 struct blame_origin *suspect = ent->suspect; 443 - struct commit_info ci; 439 + struct commit_info ci = COMMIT_INFO_INIT; 444 440 char hex[GIT_MAX_HEXSZ + 1]; 445 441 int show_raw_time = !!(opt & OUTPUT_RAW_TIMESTAMP); 446 442 const char *default_color = NULL, *color = NULL, *reset = NULL; ··· 630 626 if (longest_file < num) 631 627 longest_file = num; 632 628 if (!(suspect->commit->object.flags & METAINFO_SHOWN)) { 633 - struct commit_info ci; 629 + struct commit_info ci = COMMIT_INFO_INIT; 634 630 suspect->commit->object.flags |= METAINFO_SHOWN; 635 631 get_commit_info(suspect->commit, &ci, 1); 636 632 if (*option & OUTPUT_SHOW_EMAIL)
+1 -1
builtin/config.c
··· 575 575 int ret; 576 576 char *section_tail; 577 577 struct string_list_item *item; 578 - struct urlmatch_config config = { STRING_LIST_INIT_DUP }; 578 + struct urlmatch_config config = URLMATCH_CONFIG_INIT; 579 579 struct string_list values = STRING_LIST_INIT_DUP; 580 580 581 581 config.collect_fn = urlmatch_collect_fn;
+55 -56
builtin/remote.c
··· 347 347 int queried; 348 348 }; 349 349 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 + 350 358 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states) 351 359 { 352 360 struct ref *fetch_map = NULL, **tail = &fetch_map; ··· 358 366 die(_("Could not get fetch map for refspec %s"), 359 367 states->remote->fetch.raw[i]); 360 368 361 - states->new_refs.strdup_strings = 1; 362 - states->tracked.strdup_strings = 1; 363 - states->stale.strdup_strings = 1; 364 369 for (ref = fetch_map; ref; ref = ref->next) { 365 370 if (!ref->peer_ref || !ref_exists(ref->peer_ref->name)) 366 371 string_list_append(&states->new_refs, abbrev_branch(ref->name)); ··· 409 414 410 415 match_push_refs(local_refs, &push_map, &remote->push, MATCH_REFS_NONE); 411 416 412 - states->push.strdup_strings = 1; 413 417 for (ref = push_map; ref; ref = ref->next) { 414 418 struct string_list_item *item; 415 419 struct push_info *info; ··· 452 456 if (remote->mirror) 453 457 return 0; 454 458 455 - states->push.strdup_strings = 1; 456 459 if (!remote->push.nr) { 457 460 item = string_list_append(&states->push, _("(matching)")); 458 461 info = item->util = xcalloc(1, sizeof(struct push_info)); ··· 486 489 refspec.force = 0; 487 490 refspec.pattern = 1; 488 491 refspec.src = refspec.dst = "refs/heads/*"; 489 - states->heads.strdup_strings = 1; 490 492 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0); 491 493 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"), 492 494 fetch_map, 1); ··· 973 975 } 974 976 975 977 struct show_info { 976 - struct string_list *list; 977 - struct ref_states *states; 978 + struct string_list list; 979 + struct ref_states states; 978 980 int width, width2; 979 981 int any_rebase; 980 982 }; 981 983 984 + #define SHOW_INFO_INIT { \ 985 + .list = STRING_LIST_INIT_DUP, \ 986 + .states = REF_STATES_INIT, \ 987 + } 988 + 982 989 static int add_remote_to_show_info(struct string_list_item *item, void *cb_data) 983 990 { 984 991 struct show_info *info = cb_data; 985 992 int n = strlen(item->string); 986 993 if (n > info->width) 987 994 info->width = n; 988 - string_list_insert(info->list, item->string); 995 + string_list_insert(&info->list, item->string); 989 996 return 0; 990 997 } 991 998 992 999 static int show_remote_info_item(struct string_list_item *item, void *cb_data) 993 1000 { 994 1001 struct show_info *info = cb_data; 995 - struct ref_states *states = info->states; 1002 + struct ref_states *states = &info->states; 996 1003 const char *name = item->string; 997 1004 998 1005 if (states->queried) { ··· 1019 1026 static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data) 1020 1027 { 1021 1028 struct show_info *show_info = cb_data; 1022 - struct ref_states *states = show_info->states; 1029 + struct ref_states *states = &show_info->states; 1023 1030 struct branch_info *branch_info = branch_item->util; 1024 1031 struct string_list_item *item; 1025 1032 int n; ··· 1032 1039 if (branch_info->rebase >= REBASE_TRUE) 1033 1040 show_info->any_rebase = 1; 1034 1041 1035 - item = string_list_insert(show_info->list, branch_item->string); 1042 + item = string_list_insert(&show_info->list, branch_item->string); 1036 1043 item->util = branch_info; 1037 1044 1038 1045 return 0; ··· 1087 1094 show_info->width = n; 1088 1095 if ((n = strlen(push_info->dest)) > show_info->width2) 1089 1096 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); 1091 1098 item->util = push_item->util; 1092 1099 return 0; 1093 1100 } ··· 1215 1222 OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")), 1216 1223 OPT_END() 1217 1224 }; 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; 1221 1226 1222 1227 argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage, 1223 1228 0); ··· 1228 1233 if (!no_query) 1229 1234 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES); 1230 1235 1231 - memset(&states, 0, sizeof(states)); 1232 - memset(&info, 0, sizeof(info)); 1233 - info.states = &states; 1234 - info.list = &info_list; 1235 1236 for (; argc; argc--, argv++) { 1236 1237 int i; 1237 1238 const char **url; 1238 1239 int url_nr; 1239 1240 1240 - get_remote_ref_states(*argv, &states, query_flag); 1241 + get_remote_ref_states(*argv, &info.states, query_flag); 1241 1242 1242 1243 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; 1248 1249 } 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; 1251 1252 } 1252 1253 for (i = 0; i < url_nr; i++) 1253 1254 /* ··· 1260 1261 printf_ln(_(" Push URL: %s"), _("(no URL)")); 1261 1262 if (no_query) 1262 1263 printf_ln(_(" HEAD branch: %s"), _("(not queried)")); 1263 - else if (!states.heads.nr) 1264 + else if (!info.states.heads.nr) 1264 1265 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); 1267 1268 else { 1268 1269 printf(_(" HEAD branch (remote HEAD is ambiguous," 1269 1270 " 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); 1272 1273 } 1273 1274 1274 1275 /* remote branch info */ 1275 1276 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) 1280 1281 printf_ln(Q_(" Remote branch:%s", 1281 1282 " Remote branches:%s", 1282 - info.list->nr), 1283 + info.list.nr), 1283 1284 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); 1286 1287 1287 1288 /* git pull info */ 1288 1289 info.width = 0; 1289 1290 info.any_rebase = 0; 1290 1291 for_each_string_list(&branch_list, add_local_to_show_info, &info); 1291 - if (info.list->nr) 1292 + if (info.list.nr) 1292 1293 printf_ln(Q_(" Local branch configured for 'git pull':", 1293 1294 " 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); 1297 1298 1298 1299 /* git push info */ 1299 - if (states.remote->mirror) 1300 + if (info.states.remote->mirror) 1300 1301 printf_ln(_(" Local refs will be mirrored by 'git push'")); 1301 1302 1302 1303 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) 1306 1307 printf_ln(Q_(" Local ref configured for 'git push'%s:", 1307 1308 " Local refs configured for 'git push'%s:", 1308 - info.list->nr), 1309 + info.list.nr), 1309 1310 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); 1312 1313 1313 - free_remote_ref_states(&states); 1314 + free_remote_ref_states(&info.states); 1314 1315 } 1315 1316 1316 1317 return result; ··· 1337 1338 if (!opt_a && !opt_d && argc == 2) { 1338 1339 head_name = xstrdup(argv[1]); 1339 1340 } 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; 1342 1342 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES); 1343 1343 if (!states.heads.nr) 1344 1344 result |= error(_("Cannot determine remote HEAD")); ··· 1377 1377 static int prune_remote(const char *remote, int dry_run) 1378 1378 { 1379 1379 int result = 0; 1380 - struct ref_states states; 1380 + struct ref_states states = REF_STATES_INIT; 1381 1381 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP; 1382 1382 struct string_list_item *item; 1383 1383 const char *dangling_msg = dry_run 1384 1384 ? _(" %s will become dangling!") 1385 1385 : _(" %s has become dangling!"); 1386 1386 1387 - memset(&states, 0, sizeof(states)); 1388 1387 get_remote_ref_states(remote, &states, GET_REF_STATES); 1389 1388 1390 1389 if (!states.stale.nr) {
+1 -1
credential.c
··· 105 105 static void credential_apply_config(struct credential *c) 106 106 { 107 107 char *normalized_url; 108 - struct urlmatch_config config = { STRING_LIST_INIT_DUP }; 108 + struct urlmatch_config config = URLMATCH_CONFIG_INIT; 109 109 struct strbuf url = STRBUF_INIT; 110 110 111 111 if (!c->host)
+7 -12
daemon.c
··· 63 63 unsigned int hostname_lookup_done:1; 64 64 unsigned int saw_extended_args:1; 65 65 }; 66 + #define HOSTINFO_INIT { \ 67 + .hostname = STRBUF_INIT, \ 68 + .canon_hostname = STRBUF_INIT, \ 69 + .ip_address = STRBUF_INIT, \ 70 + .tcp_port = STRBUF_INIT, \ 71 + } 66 72 67 73 static void lookup_hostname(struct hostinfo *hi); 68 74 ··· 727 733 } 728 734 } 729 735 730 - static void hostinfo_init(struct hostinfo *hi) 731 - { 732 - memset(hi, 0, sizeof(*hi)); 733 - strbuf_init(&hi->hostname, 0); 734 - strbuf_init(&hi->canon_hostname, 0); 735 - strbuf_init(&hi->ip_address, 0); 736 - strbuf_init(&hi->tcp_port, 0); 737 - } 738 - 739 736 static void hostinfo_clear(struct hostinfo *hi) 740 737 { 741 738 strbuf_release(&hi->hostname); ··· 760 757 char *line = packet_buffer; 761 758 int pktlen, len, i; 762 759 char *addr = getenv("REMOTE_ADDR"), *port = getenv("REMOTE_PORT"); 763 - struct hostinfo hi; 760 + struct hostinfo hi = HOSTINFO_INIT; 764 761 struct strvec env = STRVEC_INIT; 765 - 766 - hostinfo_init(&hi); 767 762 768 763 if (addr) 769 764 loginfo("Connection from %s:%s", addr, port);
+1 -1
http.c
··· 990 990 char *low_speed_limit; 991 991 char *low_speed_time; 992 992 char *normalized_url; 993 - struct urlmatch_config config = { STRING_LIST_INIT_DUP }; 993 + struct urlmatch_config config = URLMATCH_CONFIG_INIT; 994 994 995 995 config.section = "http"; 996 996 config.key = NULL;
+4
urlmatch.h
··· 66 66 int (*fallback_match_fn)(const char *url, void *cb); 67 67 }; 68 68 69 + #define URLMATCH_CONFIG_INIT { \ 70 + .vars = STRING_LIST_INIT_DUP, \ 71 + } 72 + 69 73 int urlmatch_config_entry(const char *var, const char *value, void *cb); 70 74 71 75 #endif /* URL_MATCH_H */