Subject: [PATCH] Added --export option to git-send-email.

From: Eduard - Gabriel Munteanu <[email-masked]>
Date: Thu, 10 Jul 2008 17:07:35 +0300

This option allows the user to process patches with git-send-email and then
import them into an email client, without having to send them directly. The
output format is mbox.

Signed-off-by: Eduard - Gabriel Munteanu <[email-masked]>
---
 Documentation/git-send-email.txt |    6 ++++++
 git-send-email.perl              |   19 +++++++++++++++++++
 2 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/Documentation/git-send-email.txt b/Documentation/git-send-email.txt
index afbb294..17a59dd 100644
--- a/Documentation/git-send-email.txt
+++ b/Documentation/git-send-email.txt
@@ -73,6 +73,12 @@ The --cc option must be repeated for each user you want on the cc list.
         Default is the value of 'sendemail.signedoffcc' configuration value;
         if that is unspecified, default to --signed-off-by-cc.
 
+--export::
+	Do not send anything, just export the emails in mbox format. You can
+	use this if you need to import the messages into your email client,
+	for example. Overwrites the target, which must not be one of the
+	input files.
+
 --quiet::
 	Make git-send-email less verbose.  One line per email should be
 	all that is output.
diff --git a/git-send-email.perl b/git-send-email.perl
index 3564419..53539cf 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -100,6 +100,9 @@ Options:
    --thread       Specify that the "In-Reply-To:" header should be set on all
                   emails. Defaults to on.
 
+   --export	  Don't actually send emails, just export them to a mbox file.
+		  Overwrites target, which must not be one of the input files.
+
    --quiet	  Make git-send-email less verbose.  One line per email
                   should be all that is output.
 
@@ -184,6 +187,7 @@ if ($@) {
 
 # Behavior modification variables
 my ($quiet, $dry_run) = (0, 0);
+my ($export_file);
 
 # Variables with corresponding config settings
 my ($thread, $chain_reply_to, $suppress_from, $signed_off_cc, $cc_cmd);
@@ -260,6 +264,7 @@ my $rc = GetOptions("sender|from=s" => \$sender,
 		    "suppress-from!" => \$suppress_from,
 		    "suppress-cc=s" => \@suppress_cc,
 		    "signed-off-cc|signed-off-by-cc!" => \$signed_off_cc,
+		    "export=s" => \$export_file,
 		    "dry-run" => \$dry_run,
 		    "envelope-sender=s" => \$envelope_sender,
 		    "thread!" => \$thread,
@@ -691,6 +696,7 @@ sub sanitize_address
 
 }
 
+my ($out);
 sub send_message
 {
 	my @recipients = unique_email_list(@to);
@@ -741,6 +747,10 @@ X-Mailer: git-send-email $gitversion
 
 	if ($dry_run) {
 		# We don't want to send the email.
+	} elsif (defined $export_file) {
+		my $mbox_from = "From $raw_from $date";
+		$message =~ s/^(>{0,})From/>$1From/m;
+		print $out "$mbox_from\n$header\n$message";
 	} elsif ($smtp_server =~ m#^/#) {
 		my $pid = open my $sm, '|-';
 		defined $pid or die $!;
@@ -835,6 +845,11 @@ $reply_to = $initial_reply_to;
 $references = $initial_reply_to || '';
 $subject = $initial_subject;
 
+if (defined $export_file) {
+	my $err = open $out, '>', $export_file;
+	defined $err or die "Can't open '$export_file': $!";
+}
+
 foreach my $t (@files) {
 	open(F,"<",$t) or die "can't open file $t";
 
@@ -979,6 +994,10 @@ foreach my $t (@files) {
 	$message_id = undef;
 }
 
+if (defined $export_file) {
+	close $out or warn $?;
+}
+
 if ($compose) {
 	cleanup_compose_files();
 }
-- 
1.5.6.1