Take an email message file, parse it and fill the "From", "To", "Cc",
"In-reply-to", "References" fields appropriately.

If `--compose` option is set, it will also fill the subject field with
`Re: [<email_file>'s subject]` in the introductory message.

Signed-off-by: Tom RUSSELLO <tom.russe...@grenoble-inp.org>
Signed-off-by: Samuel GROOT <samuel.gr...@grenoble-inp.org>
Signed-off-by: Matthieu MOY <matthieu....@grenoble-inp.fr>
---
 Documentation/git-send-email.txt |  9 +++--
 git-send-email.perl              | 51 +++++++++++++++++++++++-
 t/t9001-send-email.sh            | 83 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 138 insertions(+), 5 deletions(-)

diff --git a/Documentation/git-send-email.txt b/Documentation/git-send-email.txt
index edbba3a..21776f0 100644
--- a/Documentation/git-send-email.txt
+++ b/Documentation/git-send-email.txt
@@ -84,13 +84,16 @@ See the CONFIGURATION section for 'sendemail.multiEdit'.
        the value of GIT_AUTHOR_IDENT, or GIT_COMMITTER_IDENT if that is not
        set, as returned by "git var -l".
 
---in-reply-to=<identifier>::
+--in-reply-to=<Message-Id|email_file>::
        Make the first mail (or all the mails with `--no-thread`) appear as a
-       reply to the given Message-Id, which avoids breaking threads to
-       provide a new patch series.
+       reply to the given Message-Id (given directly by argument or via the 
email
+       file), which avoids breaking threads to provide a new patch series.
        The second and subsequent emails will be sent as replies according to
        the `--[no]-chain-reply-to` setting.
 +
+Furthermore, if the argument is an email file, parse it and populate header
+fields appropriately for the reply.
++
 So for example when `--thread` and `--no-chain-reply-to` are specified, the
 second and subsequent patches will be replies to the first one like in the
 illustration below where `[PATCH v2 0/3]` is in reply to `[PATCH 0/2]`:
diff --git a/git-send-email.perl b/git-send-email.perl
index 9b51062..b444ea6 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -55,6 +55,7 @@ git send-email --dump-aliases
     --[no-]bcc              <str>  * Email Bcc:
     --subject               <str>  * Email "Subject:"
     --in-reply-to           <str>  * Email "In-Reply-To:"
+    --in-reply-to          <file>  * Populate header fields appropriately.
     --[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.
@@ -160,7 +161,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_subject,@files,
+       $initial_reply_to,$initial_references,$initial_subject,@files,
        $author,$sender,$smtp_authpass,$annotate,$use_xmailer,$compose,$time);
 
 my $envelope_sender;
@@ -639,6 +640,52 @@ if (@files) {
        usage();
 }
 
+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"
+               if $error;
+
+       open my $fh, "<", $initial_reply_to or die "can't open file 
$initial_reply_to";
+       my $mail = Git::parse_email($fh);
+       close $fh;
+
+       my $initial_sender = $sender || $repoauthor || $repocommitter || '';
+
+       my $prefix_re = "";
+       my $subject_re = $mail->{"subject"}[0];
+       if ($subject_re =~ /^[^Re:]/) {
+               $prefix_re = "Re: ";
+       }
+       $initial_subject = $prefix_re . $subject_re;
+
+       push @initial_to, $mail->{"from"}[0];
+
+       foreach my $to_addr (parse_address_line(join ",", @{$mail->{"to"}})) {
+               if (!($to_addr eq $initial_sender)) {
+                       push @initial_cc, $to_addr;
+               }
+       }
+
+       if (defined $mail->{"cc"}) {
+               foreach my $cc_addr (parse_address_line(join ",", 
@{$mail->{"cc"}})) {
+                       my $qaddr = unquote_rfc2047($cc_addr);
+                       my $saddr = sanitize_address($qaddr);
+                       if ($saddr eq $initial_sender) {
+                               next if ($suppress_cc{'self'});
+                       } else {
+                               next if ($suppress_cc{'cc'});
+                       }
+                       push @initial_cc, $cc_addr;
+               }
+       }
+
+       $initial_reply_to = $mail->{"message-id"}[0];
+       if ($mail->{"references"}) {
+               $initial_references = join("", @{$mail->{"references"}}) .
+                       " " . $initial_reply_to;
+       }
+}
+
 sub get_patch_subject {
        my $fn = shift;
        open (my $fh, '<', $fn);
@@ -1426,7 +1473,7 @@ Message-Id: $message_id
 }
 
 $reply_to = $initial_reply_to;
-$references = $initial_reply_to || '';
+$references = $initial_references || $initial_reply_to || '';
 $subject = $initial_subject;
 $message_num = 0;
 
diff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh
index aca7d5c..7591342 100755
--- a/t/t9001-send-email.sh
+++ b/t/t9001-send-email.sh
@@ -1892,4 +1892,87 @@ test_expect_success $PREREQ 'leading and trailing 
whitespaces are removed' '
        test_cmp expected-list actual-list
 '
 
+test_expect_success $PREREQ 'setup expect' '
+       cat >email <<-\EOF
+       Subject: subject goes here
+       From: aut...@example.com
+       To: t...@example.com
+       Cc: c...@example.com, c...@example.com,
+     c...@example.com
+       Date: Sat, 12 Jun 2010 15:53:58 +0200
+       Message-Id: <author_123...@example.com>
+       References: <firstauthor_654...@example.com>
+        <secondauthor_01546...@example.com>
+        <thirdauthor_1395...@example.com>
+
+       Have you seen my previous email?
+       > Previous content
+       EOF
+'
+
+test_expect_success $PREREQ 'Fields with --in-reply-to are correct' '
+       clean_fake_sendmail &&
+       git send-email \
+               --in-reply-to=email \
+               --from="Example <nob...@example.com>" \
+               --smtp-server="$(pwd)/fake.sendmail" \
+               -2 \
+               2>errors &&
+       grep "From: Example <nob...@example.com>" msgtxt1 &&
+       grep "In-Reply-To: <author_123...@example.com>" msgtxt1 &&
+       to_adr=$(awk "/^To: /{flag=1}/^Cc: /{flag=0} flag {print}" msgtxt1) &&
+       cc_adr=$(awk "/^Cc: /{flag=1}/^Subject: /{flag=0} flag {print}" 
msgtxt1) &&
+       ref_adr=$(awk "/^References: /{flag=1}/^MIME-Version: /{flag=0} flag 
{print}" \
+               msgtxt1) &&
+       echo "$to_adr" | grep aut...@example.com &&
+       echo "$cc_adr" | grep t...@example.com &&
+       echo "$cc_adr" | grep c...@example.com &&
+       echo "$cc_adr" | grep c...@example.com &&
+       echo "$cc_adr" | grep c...@example.com &&
+       echo "$ref_adr" | grep "<firstauthor_654...@example.com>" &&
+       echo "$ref_adr" | grep "<secondauthor_01546...@example.com>" &&
+       echo "$ref_adr" | grep "<thirdauthor_1395...@example.com>" &&
+       echo "$ref_adr" | grep "<author_123...@example.com>" &&
+       echo "$ref_adr" | grep -v "References: <author_123...@example.com>"
+'
+
+test_expect_success $PREREQ 'Fields with --in-reply-to and --compose are 
correct' '
+       clean_fake_sendmail &&
+       git send-email \
+               --in-reply-to=email \
+               --compose \
+               --from="Example <nob...@example.com>" \
+               --smtp-server="$(pwd)/fake.sendmail" \
+               -1 \
+               2>errors &&
+       grep "From: Example <nob...@example.com>" msgtxt1 &&
+       grep "In-Reply-To: <author_123...@example.com>" msgtxt1 &&
+       grep "Subject: Re: subject goes here" msgtxt1 &&
+       to_adr=$(awk "/^To: /{flag=1}/^Cc: /{flag=0} flag {print}" msgtxt1) &&
+       cc_adr=$(awk "/^Cc: /{flag=1}/^Subject: /{flag=0} flag {print}" 
msgtxt1) &&
+       ref_adr=$(awk "/^References: /{flag=1}/^MIME-Version: /{flag=0} flag 
{print}" \
+               msgtxt1) &&
+       echo "$to_adr" | grep aut...@example.com &&
+       echo "$cc_adr" | grep t...@example.com &&
+       echo "$cc_adr" | grep c...@example.com &&
+       echo "$cc_adr" | grep c...@example.com &&
+       echo "$cc_adr" | grep c...@example.com &&
+       echo "$ref_adr" | grep "<firstauthor_654...@example.com>" &&
+       echo "$ref_adr" | grep "<secondauthor_01546...@example.com>" &&
+       echo "$ref_adr" | grep "<thirdauthor_1395...@example.com>" &&
+       echo "$ref_adr" | grep "<author_123...@example.com>" &&
+       echo "$ref_adr" | grep -v "References: <author_123...@example.com>"
+'
+
+test_expect_success $PREREQ 'Re: written only once with --in-reply-to and 
--compose ' '
+       git send-email \
+               --in-reply-to=msgtxt1 \
+               --compose \
+               --from="Example <nob...@example.com>" \
+               --smtp-server="$(pwd)/fake.sendmail" \
+               -1 \
+               2>errors &&
+       grep "Subject: Re: subject goes here" msgtxt3
+'
+
 test_done
-- 
2.8.2.537.gb153d2a

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to