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 <bebarino@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stephen Boyd committed
Mar 7, 2010 at 14:46 UTC
ca9e0a1b8738959f5bd5a17ec81fa3bd1a326c9d
1 file changed
+34
-39
builtin-log.c
+34
-39
@@ -458,35 +458,28 @@ static int auto_number = 1;
458
459
static char *default_attach = NULL;
460
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;
464
465
static void add_header(const char *value)
466
{
467
+ struct string_list_item *item;
468
int len = strlen(value);
469
while (len && value[len - 1] == '\n')
470
len--;
471
+
472
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);
480
}
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';
483
}
484
485
#define THREAD_SHALLOW 1
@@ -507,15 +500,13 @@ static int git_format_config(const char *var, const char *value, void *cb)
500
if (!strcmp(var, "format.to")) {
501
if (!value)
502
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);
504
return 0;
505
}
506
if (!strcmp(var, "format.cc")) {
507
if (!value)
508
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);
510
return 0;
511
}
512
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)
875
876
static int to_callback(const struct option *opt, const char *arg, int unset)
877
{
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);
879
return 0;
880
}
881
882
static int cc_callback(const struct option *opt, const char *arg, int unset)
883
{
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);
885
return 0;
886
}
887
@@ -972,6 +961,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
961
OPT_END()
962
};
963
964
+ extra_hdr.strdup_strings = 1;
965
+ extra_to.strdup_strings = 1;
966
+ extra_cc.strdup_strings = 1;
967
git_config(git_format_config, NULL);
968
init_revisions(&rev, prefix);
969
rev.commit_format = CMIT_FMT_EMAIL;
@@ -1008,29 +1000,29 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1000
add_signoff = xmemdupz(committer, endpos - committer + 1);
1001
}
1002
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);
1005
strbuf_addch(&buf, '\n');
1006
}
1007
1016
- if (extra_to_nr)
1008
+ if (extra_to.nr)
1009
strbuf_addstr(&buf, "To: ");
1018
- for (i = 0; i < extra_to_nr; i++) {
1010
+ for (i = 0; i < extra_to.nr; i++) {
1011
if (i)
1012
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)
1015
strbuf_addch(&buf, ',');
1016
strbuf_addch(&buf, '\n');
1017
}
1018
1027
- if (extra_cc_nr)
1019
+ if (extra_cc.nr)
1020
strbuf_addstr(&buf, "Cc: ");
1029
- for (i = 0; i < extra_cc_nr; i++) {
1021
+ for (i = 0; i < extra_cc.nr; i++) {
1022
if (i)
1023
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)
1026
strbuf_addch(&buf, ',');
1027
strbuf_addch(&buf, '\n');
1028
}
@@ -1239,6 +1231,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1231
fclose(stdout);
1232
}
1233
free(list);
1234
+ string_list_clear(&extra_to, 0);
1235
+ string_list_clear(&extra_cc, 0);
1236
+ string_list_clear(&extra_hdr, 0);
1237
if (ignore_if_in_upstream)
1238
free_patch_ids(&ids);
1239
return 0;