Skip to content

Commit ca9e0a1

Browse files
bebarinogitster
authored andcommitted
format-patch: use a string_list for headers
In the next patch we'll need to clear the header lists if the user specifies --no-add-headers or --no-to or --no-cc. This actually cuts down on the code a bit too. Signed-off-by: Stephen Boyd <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ae6c098 commit ca9e0a1

File tree

1 file changed

+34
-39
lines changed

1 file changed

+34
-39
lines changed

builtin-log.c

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -458,35 +458,28 @@ static int auto_number = 1;
458458

459459
static char *default_attach = NULL;
460460

461-
static char **extra_hdr;
462-
static int extra_hdr_nr;
463-
static int extra_hdr_alloc;
464-
465-
static char **extra_to;
466-
static int extra_to_nr;
467-
static int extra_to_alloc;
468-
469-
static char **extra_cc;
470-
static int extra_cc_nr;
471-
static int extra_cc_alloc;
461+
static struct string_list extra_hdr;
462+
static struct string_list extra_to;
463+
static struct string_list extra_cc;
472464

473465
static void add_header(const char *value)
474466
{
467+
struct string_list_item *item;
475468
int len = strlen(value);
476469
while (len && value[len - 1] == '\n')
477470
len--;
471+
478472
if (!strncasecmp(value, "to: ", 4)) {
479-
ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
480-
extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
481-
return;
473+
item = string_list_append(value + 4, &extra_to);
474+
len -= 4;
475+
} else if (!strncasecmp(value, "cc: ", 4)) {
476+
item = string_list_append(value + 4, &extra_cc);
477+
len -= 4;
478+
} else {
479+
item = string_list_append(value, &extra_hdr);
482480
}
483-
if (!strncasecmp(value, "cc: ", 4)) {
484-
ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
485-
extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
486-
return;
487-
}
488-
ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
489-
extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
481+
482+
item->string[len] = '\0';
490483
}
491484

492485
#define THREAD_SHALLOW 1
@@ -507,15 +500,13 @@ static int git_format_config(const char *var, const char *value, void *cb)
507500
if (!strcmp(var, "format.to")) {
508501
if (!value)
509502
return config_error_nonbool(var);
510-
ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
511-
extra_to[extra_to_nr++] = xstrdup(value);
503+
string_list_append(value, &extra_to);
512504
return 0;
513505
}
514506
if (!strcmp(var, "format.cc")) {
515507
if (!value)
516508
return config_error_nonbool(var);
517-
ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
518-
extra_cc[extra_cc_nr++] = xstrdup(value);
509+
string_list_append(value, &extra_cc);
519510
return 0;
520511
}
521512
if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
@@ -884,15 +875,13 @@ static int header_callback(const struct option *opt, const char *arg, int unset)
884875

885876
static int to_callback(const struct option *opt, const char *arg, int unset)
886877
{
887-
ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
888-
extra_to[extra_to_nr++] = xstrdup(arg);
878+
string_list_append(arg, &extra_to);
889879
return 0;
890880
}
891881

892882
static int cc_callback(const struct option *opt, const char *arg, int unset)
893883
{
894-
ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
895-
extra_cc[extra_cc_nr++] = xstrdup(arg);
884+
string_list_append(arg, &extra_cc);
896885
return 0;
897886
}
898887

@@ -972,6 +961,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
972961
OPT_END()
973962
};
974963

964+
extra_hdr.strdup_strings = 1;
965+
extra_to.strdup_strings = 1;
966+
extra_cc.strdup_strings = 1;
975967
git_config(git_format_config, NULL);
976968
init_revisions(&rev, prefix);
977969
rev.commit_format = CMIT_FMT_EMAIL;
@@ -1008,29 +1000,29 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
10081000
add_signoff = xmemdupz(committer, endpos - committer + 1);
10091001
}
10101002

1011-
for (i = 0; i < extra_hdr_nr; i++) {
1012-
strbuf_addstr(&buf, extra_hdr[i]);
1003+
for (i = 0; i < extra_hdr.nr; i++) {
1004+
strbuf_addstr(&buf, extra_hdr.items[i].string);
10131005
strbuf_addch(&buf, '\n');
10141006
}
10151007

1016-
if (extra_to_nr)
1008+
if (extra_to.nr)
10171009
strbuf_addstr(&buf, "To: ");
1018-
for (i = 0; i < extra_to_nr; i++) {
1010+
for (i = 0; i < extra_to.nr; i++) {
10191011
if (i)
10201012
strbuf_addstr(&buf, " ");
1021-
strbuf_addstr(&buf, extra_to[i]);
1022-
if (i + 1 < extra_to_nr)
1013+
strbuf_addstr(&buf, extra_to.items[i].string);
1014+
if (i + 1 < extra_to.nr)
10231015
strbuf_addch(&buf, ',');
10241016
strbuf_addch(&buf, '\n');
10251017
}
10261018

1027-
if (extra_cc_nr)
1019+
if (extra_cc.nr)
10281020
strbuf_addstr(&buf, "Cc: ");
1029-
for (i = 0; i < extra_cc_nr; i++) {
1021+
for (i = 0; i < extra_cc.nr; i++) {
10301022
if (i)
10311023
strbuf_addstr(&buf, " ");
1032-
strbuf_addstr(&buf, extra_cc[i]);
1033-
if (i + 1 < extra_cc_nr)
1024+
strbuf_addstr(&buf, extra_cc.items[i].string);
1025+
if (i + 1 < extra_cc.nr)
10341026
strbuf_addch(&buf, ',');
10351027
strbuf_addch(&buf, '\n');
10361028
}
@@ -1239,6 +1231,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
12391231
fclose(stdout);
12401232
}
12411233
free(list);
1234+
string_list_clear(&extra_to, 0);
1235+
string_list_clear(&extra_cc, 0);
1236+
string_list_clear(&extra_hdr, 0);
12421237
if (ignore_if_in_upstream)
12431238
free_patch_ids(&ids);
12441239
return 0;

0 commit comments

Comments
 (0)