git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/2] send-email: add --header-cmd option

From
Maxim Cournoyer <maxim.cournoyer@gmail.com>
Date
Apr 25, 2023, 16:26 UTC
Message-ID
<20230425162631.13684-3-maxim.cournoyer@gmail.com>
In-Reply-To
<20230425162631.13684-1-maxim.cournoyer@gmail.com>

Sometimes, adding a header different than CC or TO is desirable; for example, when using Debbugs, it is best to use 'X-Debbugs-Cc' headers to keep people in CC; this is an example use case enabled by the new '--header-cmd' option.

The header folding logic is extracted to a subroutine so that it can be reused.

Signed-off-by: Maxim Cournoyer <maxim.cournoyer@gmail.com>
---
 Documentation/config/sendemail.txt |  1 +
 Documentation/git-send-email.txt   |  8 +++++
 git-send-email.perl                | 49 +++++++++++++++++++++++-------
 t/t9001-send-email.sh              | 21 +++++++++++--
 4 files changed, 66 insertions(+), 13 deletions(-)
diff --git a/Documentation/config/sendemail.txt b/Documentation/config/sendemail.txt
index 51da7088a8..92a9ebe98c 100644
--- a/Documentation/config/sendemail.txt
+++ b/Documentation/config/sendemail.txt
@@ -61,6 +61,7 @@ sendemail.ccCmd::
 sendemail.chainReplyTo::
 sendemail.envelopeSender::
 sendemail.from::
+sendemail.headerCmd::
 sendemail.signedoffbycc::
 sendemail.smtpPass::
 sendemail.suppresscc::
diff --git a/Documentation/git-send-email.txt b/Documentation/git-send-email.txt
index b0f438ec99..295a3dd67c 100644
--- a/Documentation/git-send-email.txt
+++ b/Documentation/git-send-email.txt
@@ -320,6 +320,14 @@ Automating
 	Output of this command must be single email address per line.
 	Default is the value of `sendemail.ccCmd` configuration value.
 
+--header-cmd=<command>::
+	Specify a command that is executed once per outgoing message
+	and output RFC 2822 style header lines to be inserted into
+	them. When the `sendemail.headerCmd` configuration variable is
+	set, its value is always used. When --header-cmd is provided
+	at the command line, its value takes precedence over the
+	`sendemail.headerCmd` configuration variable.
+
 --[no-]chain-reply-to::
 	If this is set, each email will be sent as a reply to the previous
 	email sent.  If disabled with "--no-chain-reply-to", all emails after
diff --git a/git-send-email.perl b/git-send-email.perl
index b8d77ad214..906fc1964d 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -88,8 +88,9 @@ sub usage {
 
   Automating:
     --identity              <str>  * Use the sendemail.<id> options.
-    --to-cmd                <str>  * Email To: via `<str> \$patch_path`
-    --cc-cmd                <str>  * Email Cc: via `<str> \$patch_path`
+    --to-cmd                <str>  * Email To: via `<str> \$patch_path`.
+    --cc-cmd                <str>  * Email Cc: via `<str> \$patch_path`.
+    --header-cmd            <str>  * Add headers via `<str> \$patch_path`.
     --suppress-cc           <str>  * author, self, sob, cc, cccmd, body, bodycc, misc-by, all.
     --[no-]cc-cover                * Email Cc: addresses in the cover letter.
     --[no-]to-cover                * Email To: addresses in the cover letter.
@@ -270,7 +271,7 @@ sub do_edit {
 # Variables with corresponding config settings
 my ($suppress_from, $signed_off_by_cc);
 my ($cover_cc, $cover_to);
-my ($to_cmd, $cc_cmd);
+my ($to_cmd, $cc_cmd, $header_cmd);
 my ($smtp_server, $smtp_server_port, @smtp_server_options);
 my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
 my ($batch_size, $relogin_delay);
@@ -319,6 +320,7 @@ sub do_edit {
     "tocmd" => \$to_cmd,
     "cc" => \@config_cc,
     "cccmd" => \$cc_cmd,
+    "headercmd" => \$header_cmd,
     "aliasfiletype" => \$aliasfiletype,
     "bcc" => \@config_bcc,
     "suppresscc" => \@suppress_cc,
@@ -520,6 +522,7 @@ sub config_regexp {
 		    "compose" => \$compose,
 		    "quiet" => \$quiet,
 		    "cc-cmd=s" => \$cc_cmd,
+		    "header-cmd=s" => \$header_cmd,
 		    "suppress-from!" => \$suppress_from,
 		    "no-suppress-from" => sub {$suppress_from = 0},
 		    "suppress-cc=s" => \@suppress_cc,
@@ -1766,17 +1769,15 @@ sub process_file {
 	$subject = $initial_subject;
 	$message = "";
 	$message_num++;
-	# First unfold multiline header fields
+	# Retrieve and fold header fields.
+	my @header_lines = ();
 	while(<$fh>) {
 		last if /^\s*$/;
-		if (/^\s+\S/ and @header) {
-			chomp($header[$#header]);
-			s/^\s+/ /;
-			$header[$#header] .= $_;
-	    } else {
-			push(@header, $_);
-		}
+		push(@header_lines, $_);
 	}
+	@header = fold_headers(@header_lines);
+	# Add computed headers, if applicable.
+	push @header, invoke_header_cmd($header_cmd, $t) if defined $header_cmd;
 	# Now parse the header
 	foreach(@header) {
 		if (/^From /) {
@@ -2022,6 +2023,32 @@ sub execute_cmd {
 	return @lines;
 }
 
+# Process headers lines, folding multiline headers as defined by RFC
+# 2822.
+sub fold_headers {
+	my @headers;
+	foreach(@_) {
+		last if /^\s*$/;
+		if (/^\s+\S/ and @headers) {
+			chomp($headers[$#headers]);
+			s/^\s+/ /;
+			$headers[$#headers] .= $_;
+		} else {
+			push(@headers, $_);
+		}
+	}
+	return @headers;
+}
+
+# Invoke the provided CMD with FILE as an argument, which should
+# output RFC 2822 email headers. Fold multiline headers and return the
+# headers as an array.
+sub invoke_header_cmd {
+	my ($cmd, $file) = @_;
+	my @lines = execute_cmd("header-cmd", $header_cmd, $file);
+	return fold_headers(@lines);
+}
+
 # Execute a command (e.g. $to_cmd) to get a list of email addresses
 # and return a results array
 sub recipients_cmd {
diff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh
index 0de83b5d2b..3393725107 100755
--- a/t/t9001-send-email.sh
+++ b/t/t9001-send-email.sh
@@ -374,13 +374,16 @@ test_expect_success $PREREQ,!AUTOIDENT 'broken implicit ident aborts send-email'
 	)
 '
 
-test_expect_success $PREREQ 'setup tocmd and cccmd scripts' '
+test_expect_success $PREREQ 'setup cmd scripts' '
 	write_script tocmd-sed <<-\EOF &&
 	sed -n -e "s/^tocmd--//p" "$1"
 	EOF
-	write_script cccmd-sed <<-\EOF
+	write_script cccmd-sed <<-\EOF &&
 	sed -n -e "s/^cccmd--//p" "$1"
 	EOF
+	write_script headercmd-sed <<-\EOF
+	sed -n -e "s/^headercmd--//p" "$1"
+	EOF
 '
 
 test_expect_success $PREREQ 'tocmd works' '
@@ -410,6 +413,20 @@ test_expect_success $PREREQ 'cccmd works' '
 	grep "^	cccmd@example.com" msgtxt1
 '
 
+test_expect_success $PREREQ 'headercmd works' '
+	clean_fake_sendmail &&
+	cp $patches headercmd.patch &&
+	echo "headercmd--X-Debbugs-CC: dummy@example.com" >>headercmd.patch &&
+	git send-email \
+		--from="Example <nobody@example.com>" \
+		--to=nobody@example.com \
+		--header-cmd=./headercmd-sed \
+		--smtp-server="$(pwd)/fake.sendmail" \
+		headercmd.patch \
+		&&
+	grep "^X-Debbugs-CC: dummy@example.com" msgtxt1
+'
+
 test_expect_success $PREREQ 'reject long lines' '
 	z8=zzzzzzzz &&
 	z64=$z8$z8$z8$z8$z8$z8$z8$z8 &&
-- 
2.39.2
Previous: Felipe ContrerasNext: Junio C Hamano
Message 28 of 30 in “send-email: add --header-cmd option”
  1. 0/2 send-email: add --header-cmd optionMaxim Cournoyer, Apr 23, 2023
  2. 1/2 send-email: extract execute_cmd from recipients_cmdMaxim Cournoyer, Apr 23, 2023
  3. Junio C HamanoApr 24, 2023
  4. Maxim CournoyerApr 25, 2023
  5. 2/2 send-email: add --header-cmd optionMaxim Cournoyer, Apr 23, 2023
  6. Junio C HamanoApr 24, 2023
  7. Maxim CournoyerApr 25, 2023
  8. Junio C HamanoApr 25, 2023
  9. 0/3 send-email: add --header-cmd optionMaxim Cournoyer, Apr 25, 2023
  10. 1/3 send-email: extract execute_cmd from recipients_cmdMaxim Cournoyer, Apr 25, 2023
  11. 2/3 send-email: add --header-cmd optionMaxim Cournoyer, Apr 25, 2023
  12. 3/3 send-email: detect empty blank lines in command outputMaxim Cournoyer, Apr 25, 2023
  13. Maxim CournoyerApr 25, 2023
  14. 0/3 send-email: add --header-cmd optionMaxim Cournoyer, May 1, 2023
  15. 1/3 send-email: extract execute_cmd from recipients_cmdMaxim Cournoyer, May 1, 2023
  16. 2/3 send-email: add --header-cmd, --no-header-cmd optionsMaxim Cournoyer, May 1, 2023
  17. 3/3 send-email: detect empty blank lines in command outputMaxim Cournoyer, May 1, 2023
  18. Maxim CournoyerMay 8, 2023
  19. Eric SunshineMay 8, 2023
  20. Maxim CournoyerMay 8, 2023
  21. 0/2 send-email: add --header-cmd optionMaxim Cournoyer, Apr 25, 2023
  22. 1/2 send-email: extract execute_cmd from recipients_cmdMaxim Cournoyer, Apr 25, 2023
  23. Eric SunshineApr 25, 2023
  24. Maxim CournoyerApr 25, 2023
  25. Felipe ContrerasMay 2, 2023
  26. Maxim CournoyerMay 2, 2023
  27. Felipe ContrerasMay 2, 2023
  28. 2/2 send-email: add --header-cmd optionMaxim Cournoyer, Apr 25, 2023
  29. Junio C HamanoApr 24, 2023
  30. Maxim CournoyerApr 25, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.