{"thread":{"id":"13545","subject":"[PATCH] git-send-email: add option for output in mbox format","startedAt":"2008-05-16T15:23:20Z","lastAt":"2008-05-16T15:23:20Z","messageCount":1,"participants":["Robert Richter"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"77146","messageId":"20080516152320.803658050@erda.amd.com","threadId":"13545","inReplyTo":null,"subject":"[PATCH] git-send-email: add option for output in mbox format","fromName":"Robert Richter","fromEmail":"robert.richter@amd.com","sentAt":"2008-05-16T15:23:20Z","receivedAt":"2008-05-16T15:23:20Z","isPatch":true,"sender":{"key":"robert.richter@amd.com","avatar":null},"body":"This patch adds an option for output in mbox format. Instead of\nsending, all messages are printed to standard out in mbox format.\n\nExample:\n\ngit-send-email --mbox --compose --no-chain-reply-to patches > patches.mbox\n\nSigned-off-by: Robert Richter <robert.richter@amd.com>\n---\n Documentation/git-send-email.txt |    4 ++\n git-send-email.perl              |   57 ++++++++++++++++++++++++++++++++------\n 2 files changed, 52 insertions(+), 9 deletions(-)\n\ndiff --git a/Documentation/git-send-email.txt b/Documentation/git-send-email.txt\nindex 9d0a10c..1f1a06f 100644\n--- a/Documentation/git-send-email.txt\n+++ b/Documentation/git-send-email.txt\n@@ -81,6 +81,10 @@ The --cc option must be repeated for each user you want on the cc list.\n \tvalues in the 'sendemail' section. The default identity is\n \tthe value of 'sendemail.identity'.\n \n+--mbox::\n+\tPrint all messages to the standard output in mbox format,\n+\tinstead of sending each one.\n+\n --smtp-server::\n \tIf set, specifies the outgoing SMTP server to use (e.g.\n \t`smtp.example.com` or a raw IP address).  Alternatively it can\ndiff --git a/git-send-email.perl b/git-send-email.perl\nindex 1e1d986..48ac994 100755\n--- a/git-send-email.perl\n+++ b/git-send-email.perl\n@@ -7,6 +7,8 @@\n #\n # Ported to support git \"mbox\" format files by Ryan Anderson <ryan@michonline.com>\n #\n+# Support of mbox output format by Robert Richter <robert.richter@amd.com>\n+#\n # Sends a collection of emails to the given email addresses, disturbingly fast.\n #\n # Supports two formats:\n@@ -74,6 +76,9 @@ Options:\n    --identity     The configuration identity, a subsection to prioritise over\n                   the default section.\n \n+   --mbox         Print all messages to the standard output in mbox format,\n+                  instead of sending each one.\n+\n    --smtp-server  If set, specifies the outgoing SMTP server to use.\n                   Defaults to localhost.  Port number can be specified here with\n                   hostname:port format or by using --smtp-server-port option.\n@@ -149,6 +154,12 @@ sub format_2822_time {\n \t\t       );\n }\n \n+sub format_2822_to_ctime {\n+\tmy $date = shift;\n+\t$date =~ s/(\\w{3}), (\\d{2}) (\\w{3}) (\\d{4}) (\\d\\d:\\d\\d:\\d\\d)/$1 $3 $2 $5 $4/;\n+\treturn $date;\n+}\n+\n my $have_email_valid = eval { require Email::Valid; 1 };\n my $smtp;\n my $auth;\n@@ -180,7 +191,7 @@ if ($@) {\n }\n \n # Behavior modification variables\n-my ($quiet, $dry_run) = (0, 0);\n+my ($mbox, $quiet, $dry_run) = (0, 0, 0);\n \n # Variables with corresponding config settings\n my ($thread, $chain_reply_to, $suppress_from, $signed_off_cc, $cc_cmd);\n@@ -244,6 +255,7 @@ my $rc = GetOptions(\"sender|from=s\" => \\$sender,\n \t\t    \"cc=s\" => \\@initial_cc,\n \t\t    \"bcc=s\" => \\@bcclist,\n \t\t    \"chain-reply-to!\" => \\$chain_reply_to,\n+\t\t    \"mbox\" => \\$mbox,\n \t\t    \"smtp-server=s\" => \\$smtp_server,\n \t\t    \"smtp-server-port=s\" => \\$smtp_server_port,\n \t\t    \"smtp-user=s\" => \\$smtp_authuser,\n@@ -408,7 +420,7 @@ if (!$no_validate) {\n }\n \n if (@files) {\n-\tunless ($quiet) {\n+\tunless ($quiet || $mbox) {\n \t\tprint $_,\"\\n\" for (@files);\n \t}\n } else {\n@@ -427,7 +439,7 @@ if (!defined $sender) {\n \t}\n \n \t$sender = $_ if ($_);\n-\tprint \"Emails will be sent from: \", $sender, \"\\n\";\n+\tprint (\"Emails will be sent from: \", $sender, \"\\n\") unless $mbox;\n \t$prompting++;\n }\n \n@@ -512,8 +524,20 @@ GIT: for the patch you are writing.\n EOT\n \tclose(C);\n \n+\t# Open editor in console\n \tmy $editor = $ENV{GIT_EDITOR} || Git::config(@repo, \"core.editor\") || $ENV{VISUAL} || $ENV{EDITOR} || \"vi\";\n+\tmy ($console, $consoleOUT) = $term->findConsole;\n+\topen(COPYIN,  \"<&STDIN\");\n+\topen(COPYOUT, \">&STDOUT\");\n+\topen(STDIN,   \"<$console\");\n+\topen(STDOUT,  \">$consoleOUT\");\n \tsystem('sh', '-c', $editor.' \"$@\"', $editor, $compose_filename);\n+\tclose(STDOUT);\n+\tclose(STDIN);\n+\topen(STDIN,  \"<&COPYIN\");\n+\topen(STDOUT, \">&COPYOUT\");\n+\tclose(COPYOUT);\n+\tclose(COPYIN);\n \n \topen(C2,\">\",$compose_filename . \".final\")\n \t\tor die \"Failed to open $compose_filename.final : \" . $!;\n@@ -692,7 +716,12 @@ X-Mailer: git-send-email $gitversion\n \tunshift (@sendmail_parameters,\n \t\t\t'-f', $raw_from) if(defined $envelope_sender);\n \n-\tif ($dry_run) {\n+\tif ($mbox) {\n+\t\t# print to stdout in mbox format\n+\t\tprintf (\"From %s %s\\n\", $raw_from, format_2822_to_ctime($date));\n+\t\tprint (\"$header\\n\");\n+\t\tprint escape_from($message);\n+\t} elsif ($dry_run) {\n \t\t# We don't want to send the email.\n \t} elsif ($smtp_server =~ m#^/#) {\n \t\tmy $pid = open my $sm, '|-';\n@@ -751,7 +780,10 @@ X-Mailer: git-send-email $gitversion\n \t\t$smtp->dataend() or die $smtp->message;\n \t\t$smtp->ok or die \"Failed to send $subject\\n\".$smtp->message;\n \t}\n-\tif ($quiet) {\n+\tif ($mbox) {\n+\t\t# Nop\n+\t\t;\n+\t} elsif ($quiet) {\n \t\tprintf (($dry_run ? \"Dry-\" : \"\").\"Sent %s\\n\", $subject);\n \t} else {\n \t\tprint (($dry_run ? \"Dry-\" : \"\").\"OK. Log says:\\n\");\n@@ -815,7 +847,7 @@ foreach my $t (@files) {\n \t\t\t\t\t\tnext if ($suppress_cc{'cc'});\n \t\t\t\t\t}\n \t\t\t\t\tprintf(\"(mbox) Adding cc: %s from line '%s'\\n\",\n-\t\t\t\t\t\t$2, $_) unless $quiet;\n+\t\t\t\t\t\t$2, $_) unless ($quiet || $mbox);\n \t\t\t\t\tpush @cc, $2;\n \t\t\t\t}\n \t\t\t\telsif (/^Content-type:/i) {\n@@ -841,7 +873,7 @@ foreach my $t (@files) {\n \t\t\t\t$input_format = 'lots';\n \t\t\t\tif (@cc == 0 && !$suppress_cc{'cc'}) {\n \t\t\t\t\tprintf(\"(non-mbox) Adding cc: %s from line '%s'\\n\",\n-\t\t\t\t\t\t$_, $_) unless $quiet;\n+\t\t\t\t\t\t$_, $_) unless ($quiet || $mbox);\n \n \t\t\t\t\tpush @cc, $_;\n \n@@ -864,7 +896,7 @@ foreach my $t (@files) {\n \t\t\t\tnext if ($c eq $sender and $suppress_cc{'self'});\n \t\t\t\tpush @cc, $c;\n \t\t\t\tprintf(\"(sob) Adding cc: %s from line '%s'\\n\",\n-\t\t\t\t\t$c, $_) unless $quiet;\n+\t\t\t\t\t$c, $_) unless ($quiet || $mbox);\n \t\t\t}\n \t\t}\n \t}\n@@ -880,7 +912,7 @@ foreach my $t (@files) {\n \t\t\tnext if ($c eq $sender and $suppress_from);\n \t\t\tpush @cc, $c;\n \t\t\tprintf(\"(cc-cmd) Adding cc: %s from: '%s'\\n\",\n-\t\t\t\t$c, $cc_cmd) unless $quiet;\n+\t\t\t\t$c, $cc_cmd) unless ($quiet || $mbox);\n \t\t}\n \t\tclose F\n \t\t\tor die \"(cc-cmd) failed to close pipe to '$cc_cmd'\";\n@@ -959,3 +991,10 @@ sub validate_patch {\n \t}\n \treturn undef;\n }\n+\n+# credits to Mail::Internet\n+sub escape_from {\n+    my $body = shift;\n+    $body =~ s/\\A(>*From) />$1 /og;\n+    return $body;\n+}\n-- \n1.5.3.7\n"}]}