@matheusbsbs / matheus-bagliano / commits / 3ee79d9f59
Fork 0

Combine To: and Cc: headers

RFC 2822 only permits a single To: header and a single Cc: header, so we need to turn multiple values of each of these into a list. This will be particularly significant with a command-line option to add Cc: headers, where the user can't make sure to configure valid header sets in any easy way. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Daniel Barkalow committed Feb 19, 2008 at 02:40 UTC 3ee79d9f59684778151804c02cc6ad155b30efde
2 files changed +65 -15
builtin-log.c
+64 -14
@@ -411,28 +411,47 @@ static int istitlechar(char c)
411 (c >= '0' && c <= '9') || c == '.' || c == '_';
412 }
413
414 -static char *extra_headers = NULL;
415 -static int extra_headers_size = 0;
414 static const char *fmt_patch_suffix = ".patch";
415 static int numbered = 0;
416 static int auto_number = 0;
417
418 +static char **extra_hdr;
419 +static int extra_hdr_nr;
420 +static int extra_hdr_alloc;
421 +
422 +static char **extra_to;
423 +static int extra_to_nr;
424 +static int extra_to_alloc;
425 +
426 +static char **extra_cc;
427 +static int extra_cc_nr;
428 +static int extra_cc_alloc;
429 +
430 +static void add_header(const char *value)
431 +{
432 + int len = strlen(value);
433 + while (value[len - 1] == '\n')
434 + len--;
435 + if (!strncasecmp(value, "to: ", 4)) {
436 + ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
437 + extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
438 + return;
439 + }
440 + if (!strncasecmp(value, "cc: ", 4)) {
441 + ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
442 + extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
443 + return;
444 + }
445 + ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
446 + extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
447 +}
448 +
449 static int git_format_config(const char *var, const char *value)
450 {
451 if (!strcmp(var, "format.headers")) {
423 - int len;
424 -
452 if (!value)
453 die("format.headers without value");
427 - len = strlen(value);
428 - while (value[len - 1] == '\n')
429 - len--;
430 - extra_headers_size += len + 2;
431 - extra_headers = xrealloc(extra_headers, extra_headers_size);
432 - extra_headers[extra_headers_size - len - 2] = 0;
433 - strcat(extra_headers, value);
434 - extra_headers[extra_headers_size - 2] = '\n';
435 - extra_headers[extra_headers_size - 1] = 0;
454 + add_header(value);
455 return 0;
456 }
457 if (!strcmp(var, "format.suffix")) {
@@ -712,6 +731,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
731 const char *in_reply_to = NULL;
732 struct patch_ids ids;
733 char *add_signoff = NULL;
734 + struct strbuf buf;
735
736 git_config(git_format_config);
737 init_revisions(&rev, prefix);
@@ -724,7 +744,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
744 DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
745
746 rev.subject_prefix = fmt_patch_subject_prefix;
727 - rev.extra_headers = extra_headers;
747
748 /*
749 * Parse the arguments before setup_revisions(), or something
@@ -815,6 +834,37 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
834 }
835 argc = j;
836
837 + strbuf_init(&buf, 0);
838 +
839 + for (i = 0; i < extra_hdr_nr; i++) {
840 + strbuf_addstr(&buf, extra_hdr[i]);
841 + strbuf_addch(&buf, '\n');
842 + }
843 +
844 + if (extra_to_nr)
845 + strbuf_addstr(&buf, "To: ");
846 + for (i = 0; i < extra_to_nr; i++) {
847 + if (i)
848 + strbuf_addstr(&buf, " ");
849 + strbuf_addstr(&buf, extra_to[i]);
850 + if (i + 1 < extra_to_nr)
851 + strbuf_addch(&buf, ',');
852 + strbuf_addch(&buf, '\n');
853 + }
854 +
855 + if (extra_cc_nr)
856 + strbuf_addstr(&buf, "Cc: ");
857 + for (i = 0; i < extra_cc_nr; i++) {
858 + if (i)
859 + strbuf_addstr(&buf, " ");
860 + strbuf_addstr(&buf, extra_cc[i]);
861 + if (i + 1 < extra_cc_nr)
862 + strbuf_addch(&buf, ',');
863 + strbuf_addch(&buf, '\n');
864 + }
865 +
866 + rev.extra_headers = strbuf_detach(&buf, 0);
867 +
868 if (start_number < 0)
869 start_number = 1;
870 if (numbered && keep_subject)
t/t4014-format-patch.sh
+1 -1
@@ -112,7 +112,7 @@ test_expect_success 'extra headers without newlines' '
112
113 '
114
115 -test_expect_failure 'extra headers with multiple To:s' '
115 +test_expect_success 'extra headers with multiple To:s' '
116
117 git config --replace-all format.headers "To: R. E. Cipient <rcipient@example.com>" &&
118 git config --add format.headers "To: S. E. Cipient <scipient@example.com>" &&