contrib/git-svn: add Makefile, test, and associated ignores
[git/spearce.git] / git-send-email.perl
blob13b85dddd186b5b769fb8e9b88896b1f89ef7935
1 #!/usr/bin/perl -w
3 # Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4 # Copyright 2005 Ryan Anderson <ryan@michonline.com>
6 # GPL v2 (See COPYING)
8 # Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
10 # Sends a collection of emails to the given email addresses, disturbingly fast.
12 # Supports two formats:
13 # 1. mbox format files (ignoring most headers and MIME formatting - this is designed for sending patches)
14 # 2. The original format support by Greg's script:
15 # first line of the message is who to CC,
16 # and second line is the subject of the message.
19 use strict;
20 use warnings;
21 use Term::ReadLine;
22 use Mail::Sendmail qw(sendmail %mailcfg);
23 use Getopt::Long;
24 use Data::Dumper;
25 use Email::Valid;
27 sub unique_email_list(@);
28 sub cleanup_compose_files();
30 # Constants (essentially)
31 my $compose_filename = ".msg.$$";
33 # Variables we fill in automatically, or via prompting:
34 my (@to,@cc,@initial_cc,$initial_reply_to,$initial_subject,@files,$from,$compose);
36 # Behavior modification variables
37 my ($chain_reply_to, $smtp_server, $quiet, $suppress_from, $no_signed_off_cc) = (1, "localhost", 0, 0, 0);
39 # Example reply to:
40 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
42 my $term = new Term::ReadLine 'git-send-email';
44 # Begin by accumulating all the variables (defined above), that we will end up
45 # needing, first, from the command line:
47 my $rc = GetOptions("from=s" => \$from,
48 "in-reply-to=s" => \$initial_reply_to,
49 "subject=s" => \$initial_subject,
50 "to=s" => \@to,
51 "cc=s" => \@initial_cc,
52 "chain-reply-to!" => \$chain_reply_to,
53 "smtp-server=s" => \$smtp_server,
54 "compose" => \$compose,
55 "quiet" => \$quiet,
56 "suppress-from" => \$suppress_from,
57 "no-signed-off-cc" => \$no_signed_off_cc,
60 # Now, let's fill any that aren't set in with defaults:
62 open(GITVAR,"-|","git-var","-l")
63 or die "Failed to open pipe from git-var: $!";
65 my ($author,$committer);
66 while(<GITVAR>) {
67 chomp;
68 my ($var,$data) = split /=/,$_,2;
69 my @fields = split /\s+/, $data;
71 my $ident = join(" ", @fields[0...(@fields-3)]);
73 if ($var eq 'GIT_AUTHOR_IDENT') {
74 $author = $ident;
75 } elsif ($var eq 'GIT_COMMITTER_IDENT') {
76 $committer = $ident;
79 close(GITVAR);
81 my $prompting = 0;
82 if (!defined $from) {
83 $from = $author || $committer;
84 do {
85 $_ = $term->readline("Who should the emails appear to be from? ",
86 $from);
87 } while (!defined $_);
89 $from = $_;
90 print "Emails will be sent from: ", $from, "\n";
91 $prompting++;
94 if (!@to) {
95 do {
96 $_ = $term->readline("Who should the emails be sent to? ",
97 "");
98 } while (!defined $_);
99 my $to = $_;
100 push @to, split /,/, $to;
101 $prompting++;
104 if (!defined $initial_subject && $compose) {
105 do {
106 $_ = $term->readline("What subject should the emails start with? ",
107 $initial_subject);
108 } while (!defined $_);
109 $initial_subject = $_;
110 $prompting++;
113 if (!defined $initial_reply_to && $prompting) {
114 do {
115 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
116 $initial_reply_to);
117 } while (!defined $_);
119 $initial_reply_to = $_;
120 $initial_reply_to =~ s/(^\s+|\s+$)//g;
123 if (!defined $smtp_server) {
124 $smtp_server = "localhost";
127 if ($compose) {
128 # Note that this does not need to be secure, but we will make a small
129 # effort to have it be unique
130 open(C,">",$compose_filename)
131 or die "Failed to open for writing $compose_filename: $!";
132 print C "From $from # This line is ignored.\n";
133 printf C "Subject: %s\n\n", $initial_subject;
134 printf C <<EOT;
135 GIT: Please enter your email below.
136 GIT: Lines beginning in "GIT: " will be removed.
137 GIT: Consider including an overall diffstat or table of contents
138 GIT: for the patch you are writing.
141 close(C);
143 my $editor = $ENV{EDITOR};
144 $editor = 'vi' unless defined $editor;
145 system($editor, $compose_filename);
147 open(C2,">",$compose_filename . ".final")
148 or die "Failed to open $compose_filename.final : " . $!;
150 open(C,"<",$compose_filename)
151 or die "Failed to open $compose_filename : " . $!;
153 while(<C>) {
154 next if m/^GIT: /;
155 print C2 $_;
157 close(C);
158 close(C2);
160 do {
161 $_ = $term->readline("Send this email? (y|n) ");
162 } while (!defined $_);
164 if (uc substr($_,0,1) ne 'Y') {
165 cleanup_compose_files();
166 exit(0);
169 @files = ($compose_filename . ".final");
173 # Now that all the defaults are set, process the rest of the command line
174 # arguments and collect up the files that need to be processed.
175 for my $f (@ARGV) {
176 if (-d $f) {
177 opendir(DH,$f)
178 or die "Failed to opendir $f: $!";
180 push @files, grep { -f $_ } map { +$f . "/" . $_ }
181 sort readdir(DH);
183 } elsif (-f $f) {
184 push @files, $f;
186 } else {
187 print STDERR "Skipping $f - not found.\n";
191 if (@files) {
192 unless ($quiet) {
193 print $_,"\n" for (@files);
195 } else {
196 print <<EOT;
197 git-send-email [options] <file | directory> [... file | directory ]
198 Options:
199 --from Specify the "From:" line of the email to be sent.
201 --to Specify the primary "To:" line of the email.
203 --cc Specify an initial "Cc:" list for the entire series
204 of emails.
206 --compose Use \$EDITOR to edit an introductory message for the
207 patch series.
209 --subject Specify the initial "Subject:" line.
210 Only necessary if --compose is also set. If --compose
211 is not set, this will be prompted for.
213 --in-reply-to Specify the first "In-Reply-To:" header line.
214 Only used if --compose is also set. If --compose is not
215 set, this will be prompted for.
217 --chain-reply-to If set, the replies will all be to the previous
218 email sent, rather than to the first email sent.
219 Defaults to on.
221 --no-signed-off-cc Suppress the automatic addition of email addresses
222 that appear in a Signed-off-by: line, to the cc: list.
223 Note: Using this option is not recommended.
225 --smtp-server If set, specifies the outgoing SMTP server to use.
226 Defaults to localhost.
228 --suppress-from Supress sending emails to yourself if your address
229 appears in a From: line.
231 --quiet Make git-send-email less verbose. One line per email should be
232 all that is output.
234 Error: Please specify a file or a directory on the command line.
236 exit(1);
239 # Variables we set as part of the loop over files
240 our ($message_id, $cc, %mail, $subject, $reply_to, $message);
243 # Usually don't need to change anything below here.
245 # we make a "fake" message id by taking the current number
246 # of seconds since the beginning of Unix time and tacking on
247 # a random number to the end, in case we are called quicker than
248 # 1 second since the last time we were called.
250 # We'll setup a template for the message id, using the "from" address:
251 my $message_id_from = Email::Valid->address($from);
252 my $message_id_template = "<%s-git-send-email-$message_id_from>";
254 sub make_message_id
256 my $date = `date "+\%s"`;
257 chomp($date);
258 my $pseudo_rand = int (rand(4200));
259 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
260 #print "new message id = $message_id\n"; # Was useful for debugging
265 $cc = "";
267 sub send_message
269 my $to = join (", ", unique_email_list(@to));
271 %mail = ( To => $to,
272 From => $from,
273 CC => $cc,
274 Subject => $subject,
275 Message => $message,
276 'Reply-to' => $from,
277 'In-Reply-To' => $reply_to,
278 'Message-ID' => $message_id,
279 'X-Mailer' => "git-send-email",
282 $mail{smtp} = $smtp_server;
283 $mailcfg{mime} = 0;
285 #print Data::Dumper->Dump([\%mail],[qw(*mail)]);
287 sendmail(%mail) or die $Mail::Sendmail::error;
289 if ($quiet) {
290 printf "Sent %s\n", $subject;
291 } else {
292 print "OK. Log says:\n", $Mail::Sendmail::log;
293 print "\n\n"
298 $reply_to = $initial_reply_to;
299 make_message_id();
300 $subject = $initial_subject;
302 foreach my $t (@files) {
303 open(F,"<",$t) or die "can't open file $t";
305 @cc = @initial_cc;
306 my $found_mbox = 0;
307 my $header_done = 0;
308 $message = "";
309 while(<F>) {
310 if (!$header_done) {
311 $found_mbox = 1, next if (/^From /);
312 chomp;
314 if ($found_mbox) {
315 if (/^Subject:\s+(.*)$/) {
316 $subject = $1;
318 } elsif (/^(Cc|From):\s+(.*)$/) {
319 next if ($2 eq $from && $suppress_from);
320 printf("(mbox) Adding cc: %s from line '%s'\n",
321 $2, $_) unless $quiet;
322 push @cc, $2;
325 } else {
326 # In the traditional
327 # "send lots of email" format,
328 # line 1 = cc
329 # line 2 = subject
330 # So let's support that, too.
331 if (@cc == 0) {
332 printf("(non-mbox) Adding cc: %s from line '%s'\n",
333 $_, $_) unless $quiet;
335 push @cc, $_;
337 } elsif (!defined $subject) {
338 $subject = $_;
342 # A whitespace line will terminate the headers
343 if (m/^\s*$/) {
344 $header_done = 1;
346 } else {
347 $message .= $_;
348 if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
349 my $c = $1;
350 chomp $c;
351 push @cc, $c;
352 printf("(sob) Adding cc: %s from line '%s'\n",
353 $c, $_) unless $quiet;
357 close F;
359 $cc = join(", ", unique_email_list(@cc));
361 send_message();
363 # set up for the next message
364 if ($chain_reply_to || length($reply_to) == 0) {
365 $reply_to = $message_id;
367 make_message_id();
370 if ($compose) {
371 cleanup_compose_files();
374 sub cleanup_compose_files() {
375 unlink($compose_filename, $compose_filename . ".final");
381 sub unique_email_list(@) {
382 my %seen;
383 my @emails;
385 foreach my $entry (@_) {
386 my $clean = Email::Valid->address($entry);
387 next if $seen{$clean}++;
388 push @emails, $entry;
390 return @emails;