string_list API users: use string_list_init_{no,}dup

Follow-up on the introduction of string_list_init_nodup() and string_list_init_dup() in the series merged in bd4232fac33 (Merge branch 'ab/struct-init', 2021-07-16) and convert code that implicitly relied on xcalloc() being equivalent to the initializer to use xmalloc() and string_list_init_{no,}dup() instead. In the case of get_unmerged() in merge-recursive.c we used the combination of xcalloc() and assigning "1" to "strdup_strings" to get what we'd get via string_list_init_dup(), let's use that instead. Adjacent code in cmd_format_patch() will be changed in a subsequent commit, since we're changing that let's change the other in-tree patterns that do the same. Let's also convert a "x == NULL" to "!x" per our CodingGuidelines, as we need to change the "if" line anyway. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Apr 13, 2022 at 22:01 UTC f2605051427af9fceaacb4a9d0effc6e2b0af227
3 files changed +12 -7
builtin/log.c
+6 -3
@@ -231,7 +231,8 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
231 }
232
233 if (mailmap) {
234 - rev->mailmap = xcalloc(1, sizeof(struct string_list));
234 + rev->mailmap = xmalloc(sizeof(struct string_list));
235 + string_list_init_nodup(rev->mailmap);
236 read_mailmap(rev->mailmap);
237 }
238
@@ -2173,8 +2174,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
2174 prepare_bases(&bases, base, list, nr);
2175 }
2176
2176 - if (in_reply_to || thread || cover_letter)
2177 - rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
2177 + if (in_reply_to || thread || cover_letter) {
2178 + rev.ref_message_ids = xmalloc(sizeof(*rev.ref_message_ids));
2179 + string_list_init_nodup(rev.ref_message_ids);
2180 + }
2181 if (in_reply_to) {
2182 const char *msgid = clean_message_id(in_reply_to);
2183 string_list_append(rev.ref_message_ids, msgid);
builtin/shortlog.c
+4 -2
@@ -81,8 +81,10 @@ static void insert_one_record(struct shortlog *log,
81 format_subject(&subject, oneline, " ");
82 buffer = strbuf_detach(&subject, NULL);
83
84 - if (item->util == NULL)
85 - item->util = xcalloc(1, sizeof(struct string_list));
84 + if (!item->util) {
85 + item->util = xmalloc(sizeof(struct string_list));
86 + string_list_init_nodup(item->util);
87 + }
88 string_list_append(item->util, buffer);
89 }
90 }
merge-recursive.c
+2 -2
@@ -522,10 +522,10 @@ static struct stage_data *insert_stage_data(struct repository *r,
522 */
523 static struct string_list *get_unmerged(struct index_state *istate)
524 {
525 - struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
525 + struct string_list *unmerged = xmalloc(sizeof(struct string_list));
526 int i;
527
528 - unmerged->strdup_strings = 1;
528 + string_list_init_dup(unmerged);
529
530 /* TODO: audit for interaction with sparse-index. */
531 ensure_full_index(istate);