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

[PATCH v4 6/6] send-email: add option --cite to quote the message body

From
SGSamuel GROOT <samuel.groot@grenoble-inp.org>
Date
Jun 8, 2016, 13:08 UTC
Message-ID
<20160608130833.32274-1-samuel.groot@grenoble-inp.org>
In-Reply-To
<20160608130142.29879-1-samuel.groot@grenoble-inp.org>

If used with `in-reply-to=<email_file>`, cite the message body of the given email file. Otherwise, do nothing.

If `--compose` is also set, quote the message body in the cover letter. Else, imply `--annotate` by default and quote the message body below the triple-dash section in the first patch only.

Signed-off-by: Tom RUSSELLO <tom.russello@grenoble-inp.org>
Signed-off-by: Samuel GROOT <samuel.groot@grenoble-inp.org>
Signed-off-by: Matthieu MOY <matthieu.moy@grenoble-inp.fr>
---
 Documentation/git-send-email.txt |  8 ++++
 git-send-email.perl              | 81 ++++++++++++++++++++++++++++++++++++++--
 t/t9001-send-email.sh            | 32 ++++++++++++++++
 3 files changed, 117 insertions(+), 4 deletions(-)
diff --git a/Documentation/git-send-email.txt b/Documentation/git-send-email.txt
index 21776f0..23cbd17 100644
--- a/Documentation/git-send-email.txt
+++ b/Documentation/git-send-email.txt
@@ -109,6 +109,14 @@ illustration below where `[PATCH v2 0/3]` is in reply to `[PATCH 0/2]`:
 Only necessary if --compose is also set.  If --compose
 is not set, this will be prompted for.
 
+--cite::
+	When used with `--in-reply-to=<email_file>`, quote the message body of the
+	given email file.
++
+If `--compose` is also set, the message cited will be in the cover letter. If
+`--compose` is not set, `--annotate` option is implied by default and the
+message body will be cited in the "below-triple-dash" section.
+
 --subject=<string>::
 	Specify the initial subject of the email thread.
 	Only necessary if --compose is also set.  If --compose
diff --git a/git-send-email.perl b/git-send-email.perl
index b444ea6..6877ea7 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -26,6 +26,7 @@ use Text::ParseWords;
 use Term::ANSIColor;
 use File::Temp qw/ tempdir tempfile /;
 use File::Spec::Functions qw(catfile);
+use File::Copy;
 use Error qw(:try);
 use Git;
 
@@ -56,6 +57,8 @@ git send-email --dump-aliases
     --subject               <str>  * Email "Subject:"
     --in-reply-to           <str>  * Email "In-Reply-To:"
     --in-reply-to          <file>  * Populate header fields appropriately.
+    --cite                         * Quote the message body in the cover if
+                                     --compose is set, else in the first patch.
     --[no-]xmailer                 * Add "X-Mailer:" header (default).
     --[no-]annotate                * Review each patch that will be sent in an editor.
     --compose                      * Open an editor for introduction.
@@ -161,7 +164,7 @@ my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/;
 
 # Variables we fill in automatically, or via prompting:
 my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh,
-	$initial_reply_to,$initial_references,$initial_subject,@files,
+	$initial_reply_to,$initial_references,$cite,$initial_subject,@files,
 	$author,$sender,$smtp_authpass,$annotate,$use_xmailer,$compose,$time);
 
 my $envelope_sender;
@@ -305,6 +308,7 @@ $rc = GetOptions(
 		    "sender|from=s" => \$sender,
                     "in-reply-to=s" => \$initial_reply_to,
 		    "subject=s" => \$initial_subject,
+		    "cite" => \$cite,
 		    "to=s" => \@initial_to,
 		    "to-cmd=s" => \$to_cmd,
 		    "no-to" => \$no_to,
@@ -640,6 +644,7 @@ if (@files) {
 	usage();
 }
 
+my $message_cited;
 if ($initial_reply_to && -f $initial_reply_to) {
 	my $error = validate_patch($initial_reply_to);
 	die "fatal: $initial_reply_to: $error\nwarning: no patches were sent\n"
@@ -658,7 +663,8 @@ if ($initial_reply_to && -f $initial_reply_to) {
 	}
 	$initial_subject = $prefix_re . $subject_re;
 
-	push @initial_to, $mail->{"from"}[0];
+	my $recipient = $mail->{"from"}[0];
+	push @initial_to, $recipient;
 
 	foreach my $to_addr (parse_address_line(join ",", @{$mail->{"to"}})) {
 		if (!($to_addr eq $initial_sender)) {
@@ -684,6 +690,25 @@ if ($initial_reply_to && -f $initial_reply_to) {
 		$initial_references = join("", @{$mail->{"references"}}) .
 			" " . $initial_reply_to;
 	}
+
+	if ($cite) {
+		my $date = $mail->{"date"}[0];
+		my $tpl_date =  $date && "On $date, " || '';
+		$message_cited = $tpl_date . $recipient . " wrote:\n";
+
+		# Quote the message body
+		foreach (@{$mail->{"body"}}) {
+			my $space = "";
+			if (/^[^>]/) {
+				$space = " ";
+			}
+			$message_cited .= ">" . $space . $_;
+		}
+
+		if (!$compose) {
+			$annotate = 1;
+		}
+	}
 }
 
 sub get_patch_subject {
@@ -711,6 +736,9 @@ if ($compose) {
 	my $tpl_sender = $sender || $repoauthor || $repocommitter || '';
 	my $tpl_subject = $initial_subject || '';
 	my $tpl_reply_to = $initial_reply_to || '';
+	my $tpl_quote = $message_cited &&
+		"\nGIT: Please, trim down irrelevant sections in the cited message\n".
+		"GIT: to keep your email concise.\n" . $message_cited || '';
 
 	print $c <<EOT;
 From $tpl_sender # This line is ignored.
@@ -722,7 +750,7 @@ GIT: Clear the body content if you don't wish to send a summary.
 From: $tpl_sender
 Subject: $tpl_subject
 In-Reply-To: $tpl_reply_to
-
+$tpl_quote
 EOT
 	for my $f (@files) {
 		print $c get_patch_subject($f);
@@ -787,7 +815,52 @@ EOT
 		$compose = -1;
 	}
 } elsif ($annotate) {
-	do_edit(@files);
+	if ($message_cited) {
+		my $cite_email_filename = ($repo ?
+			tempfile(".gitsendemail.msg.XXXXXX",
+				DIR => $repo->repo_path()) :
+			tempfile(".gitsendemail.msg.XXXXXX",
+				DIR => "."))[1];
+
+		# Insertion in a temporary file to keep the original file clean
+		# in case of cancellation/error.
+		do_insert_cited_message($cite_email_filename, $files[0]);
+
+		my $tmp = $files[0];
+		$files[0] = $cite_email_filename;
+
+		do_edit(@files);
+
+		# Erase the original patch if the edition went well
+		move($cite_email_filename, $tmp);
+		$files[0] = $tmp;
+	} else {
+		do_edit(@files);
+	}
+}
+
+sub do_insert_cited_message {
+	my $tmp_file = shift;
+	my $original_file = shift;
+
+	open my $c, "<", $original_file
+	or die "Failed to open $original_file: " . $!;
+
+	open my $c2, ">", $tmp_file
+		or die "Failed to open $tmp_file: " . $!;
+
+	# Insertion after the triple-dash
+	while (<$c>) {
+		print $c2 $_;
+		last if (/^---$/);
+	}
+	print $c2 $message_cited;
+	while (<$c>) {
+		print $c2 $_;
+	}
+
+	close $c;
+	close $c2;
 }
 
 sub ask {
diff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh
index 7591342..29e28f2 100755
--- a/t/t9001-send-email.sh
+++ b/t/t9001-send-email.sh
@@ -1915,6 +1915,7 @@ test_expect_success $PREREQ 'Fields with --in-reply-to are correct' '
 	git send-email \
 		--in-reply-to=email \
 		--from="Example <nobody@example.com>" \
+		--cite \
 		--smtp-server="$(pwd)/fake.sendmail" \
 		-2 \
 		2>errors &&
@@ -1936,10 +1937,22 @@ test_expect_success $PREREQ 'Fields with --in-reply-to are correct' '
 	echo "$ref_adr" | grep -v "References: <author_123456@example.com>"
 '
 
+test_expect_success $PREREQ 'correct cited message with --in-reply-to' '
+	msg_cited=$(grep -A 3 "^---$" msgtxt1) &&
+	echo "$msg_cited" | grep "On Sat, 12 Jun 2010 15:53:58 +0200, author@example.com wrote:" &&
+	echo "$msg_cited" | grep "> Have you seen my previous email?" &&
+	echo "$msg_cited" | grep ">> Previous content"
+'
+
+test_expect_success $PREREQ 'second patch body is not modified by --in-reply-to' '
+	! grep "Have you seen my previous email?" msgtxt2
+'
+
 test_expect_success $PREREQ 'Fields with --in-reply-to and --compose are correct' '
 	clean_fake_sendmail &&
 	git send-email \
 		--in-reply-to=email \
+		--cite \
 		--compose \
 		--from="Example <nobody@example.com>" \
 		--smtp-server="$(pwd)/fake.sendmail" \
@@ -1967,6 +1980,7 @@ test_expect_success $PREREQ 'Fields with --in-reply-to and --compose are correct
 test_expect_success $PREREQ 'Re: written only once with --in-reply-to and --compose ' '
 	git send-email \
 		--in-reply-to=msgtxt1 \
+		--cite \
 		--compose \
 		--from="Example <nobody@example.com>" \
 		--smtp-server="$(pwd)/fake.sendmail" \
@@ -1975,4 +1989,22 @@ test_expect_success $PREREQ 'Re: written only once with --in-reply-to and --comp
 	grep "Subject: Re: subject goes here" msgtxt3
 '
 
+test_expect_success $PREREQ 'correct cited message with --in-reply-to and --compose' '
+	grep "> On Sat, 12 Jun 2010 15:53:58 +0200, author@example.com wrote:" msgtxt3 &&
+	grep ">> Have you seen my previous email?" msgtxt3 &&
+	grep ">>> Previous content" msgtxt3
+'
+
+test_expect_success $PREREQ 'Message is not cited with only --in-reply-to' '
+	clean_fake_sendmail &&
+	git send-email \
+		--in-reply-to=email \
+		--compose \
+		--from="Example <nobody@example.com>" \
+		--smtp-server="$(pwd)/fake.sendmail" \
+		-1 \
+		2>errors &&
+	! grep "Have you seen my previous email?" msgtxt1
+'
+
 test_done
-- 
2.8.2.537.gb153d2a
Previous: Samuel GROOTNext: Matthieu Moy
Message 93 of 96 in “send-email: new --quote-mail option”
  1. 0/2 send-email: new --quote-mail optionTom Russello, May 23, 2016
  2. 1/2 send-email: new option to quote an email and reply toTom Russello, May 23, 2016
  3. Eric WongMay 23, 2016
  4. Matthieu MoyMay 23, 2016
  5. Samuel GROOTMay 23, 2016
  6. Samuel GROOTMay 24, 2016
  7. Matthieu MoyMay 24, 2016
  8. Aaron SchrabMay 24, 2016
  9. Tom RusselloMay 25, 2016
  10. Eric WongMay 24, 2016
  11. Matthieu MoyMay 23, 2016
  12. Samuel GROOTMay 24, 2016
  13. Matthieu MoyMay 25, 2016
  14. Junio C HamanoMay 25, 2016
  15. Matthieu MoyMay 25, 2016
  16. Junio C HamanoMay 25, 2016
  17. Matthieu MoyMay 25, 2016
  18. Samuel GROOTMay 26, 2016
  19. Matthieu MoyMay 27, 2016
  20. 2/2 t9001: adding --quote-mail option testTom Russello, May 23, 2016
  21. Matthieu MoyMay 23, 2016
  22. Matthieu MoyMay 23, 2016
  23. Samuel GROOTMay 23, 2016
  24. 0/2 send-email: new --quote-email optionTom Russello, May 27, 2016
  25. 1/2 send-email: quote-email populates the fieldsTom Russello, May 27, 2016
  26. Matthieu MoyMay 28, 2016
  27. Tom RusselloMay 29, 2016
  28. 2/2 send-email: quote-email quotes the message bodyTom Russello, May 27, 2016
  29. Matthieu MoyMay 28, 2016
  30. Tom RusselloMay 29, 2016
  31. 0/6 send-email: cleaner tests and quote emailTom Russello, Jun 7, 2016
  32. 1/6 t9001: non order-sensitive file comparisonTom Russello, Jun 7, 2016
  33. Junio C HamanoJun 8, 2016
  34. Samuel GROOTJun 8, 2016
  35. Junio C HamanoJun 8, 2016
  36. Samuel GROOTJun 8, 2016
  37. Matthieu MoyJun 9, 2016
  38. Samuel GROOTJun 13, 2016
  39. Matthieu MoyJun 9, 2016
  40. Tom RusselloJun 9, 2016
  41. 2/6 t9001: check email address is in Cc: fieldTom Russello, Jun 7, 2016
  42. Matthieu MoyJun 9, 2016
  43. Samuel GROOTJun 13, 2016
  44. 3/6 t9001: shorten send-email's outputTom Russello, Jun 7, 2016
  45. Eric WongJun 8, 2016
  46. Samuel GROOTJun 8, 2016
  47. Matthieu MoyJun 9, 2016
  48. 4/6 send-email: create email parser subroutineTom Russello, Jun 7, 2016
  49. 5/6 send-email: --in-reply-to=<file> populates the fieldsTom Russello, Jun 7, 2016
  50. 6/6 send-email: add option --cite to quote the message bodyTom Russello, Jun 7, 2016
  51. (unknown)Samuel GROOT, Jun 8, 2016
  52. 1/6 t9001: non order-sensitive file comparisonSamuel GROOT, Jun 8, 2016
  53. Remi Galan AlfonsoJun 8, 2016
  54. Samuel GROOTJun 8, 2016
  55. Junio C HamanoJun 8, 2016
  56. Samuel GROOTJun 8, 2016
  57. Junio C HamanoJun 8, 2016
  58. Samuel GROOTJun 8, 2016
  59. 2/6 t9001: check email address is in Cc: fieldSamuel GROOT, Jun 8, 2016
  60. Junio C HamanoJun 8, 2016
  61. Samuel GROOTJun 8, 2016
  62. 3/6 send-email: shorten send-email's outputSamuel GROOT, Jun 8, 2016
  63. Junio C HamanoJun 8, 2016
  64. Samuel GROOTJun 8, 2016
  65. Junio C HamanoJun 8, 2016
  66. Samuel GROOTJun 8, 2016
  67. Matthieu MoyJun 9, 2016
  68. Samuel GROOTJun 13, 2016
  69. 4/6 send-email: create email parser subroutineSamuel GROOT, Jun 8, 2016
  70. Junio C HamanoJun 8, 2016
  71. Eric SunshineJun 8, 2016
  72. Junio C HamanoJun 8, 2016
  73. Samuel GROOTJun 8, 2016
  74. Junio C HamanoJun 8, 2016
  75. Samuel GROOTJun 8, 2016
  76. Samuel GROOTJun 8, 2016
  77. Eric SunshineJun 8, 2016
  78. Junio C HamanoJun 8, 2016
  79. Samuel GROOTJun 8, 2016
  80. Eric WongJun 9, 2016
  81. Samuel GROOTJun 13, 2016
  82. Eric WongJun 13, 2016
  83. Samuel GROOTJun 14, 2016
  84. Eric SunshineJun 9, 2016
  85. Samuel GROOTJun 13, 2016
  86. Samuel GROOTJun 8, 2016
  87. Eric WongJun 8, 2016
  88. 5/6 send-email: --in-reply-to=<file> populate header fieldsSamuel GROOT, Jun 8, 2016
  89. Junio C HamanoJun 8, 2016
  90. Samuel GROOTJun 14, 2016
  91. Matthieu MoyJun 9, 2016
  92. Samuel GROOTJun 14, 2016
  93. 6/6 send-email: add option --cite to quote the message bodySamuel GROOT, Jun 8, 2016
  94. Matthieu MoyJun 9, 2016
  95. Samuel GROOTJun 14, 2016
  96. Tom RusselloJun 15, 2016

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.