git-grep: clarification on parameters.
[git/gitweb.git] / git-send-email.perl
blobec1428d961206f12e195274aea6ea3ae52374898
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_reply_to,$initial_subject,@files,$from,$compose);
36 # Behavior modification variables
37 my ($chain_reply_to, $smtp_server) = (1, "localhost");
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 "chain-reply-to!" => \$chain_reply_to,
52 "smtp-server=s" => \$smtp_server,
53 "compose" => \$compose,
56 # Now, let's fill any that aren't set in with defaults:
58 open(GITVAR,"-|","git-var","-l")
59 or die "Failed to open pipe from git-var: $!";
61 my ($author,$committer);
62 while(<GITVAR>) {
63 chomp;
64 my ($var,$data) = split /=/,$_,2;
65 my @fields = split /\s+/, $data;
67 my $ident = join(" ", @fields[0...(@fields-3)]);
69 if ($var eq 'GIT_AUTHOR_IDENT') {
70 $author = $ident;
71 } elsif ($var eq 'GIT_COMMITTER_IDENT') {
72 $committer = $ident;
75 close(GITVAR);
77 my $prompting = 0;
78 if (!defined $from) {
79 $from = $author || $committer;
80 do {
81 $_ = $term->readline("Who should the emails appear to be from? ",
82 $from);
83 } while (!defined $_);
85 $from = $_;
86 print "Emails will be sent from: ", $from, "\n";
87 $prompting++;
90 if (!@to) {
91 do {
92 $_ = $term->readline("Who should the emails be sent to? ",
93 "");
94 } while (!defined $_);
95 my $to = $_;
96 push @to, split /,/, $to;
97 $prompting++;
100 if (!defined $initial_subject && $compose) {
101 do {
102 $_ = $term->readline("What subject should the emails start with? ",
103 $initial_subject);
104 } while (!defined $_);
105 $initial_subject = $_;
106 $prompting++;
109 if (!defined $initial_reply_to && $prompting) {
110 do {
111 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
112 $initial_reply_to);
113 } while (!defined $_);
115 $initial_reply_to = $_;
116 $initial_reply_to =~ s/(^\s+|\s+$)//g;
119 if (!defined $smtp_server) {
120 $smtp_server = "localhost";
123 if ($compose) {
124 # Note that this does not need to be secure, but we will make a small
125 # effort to have it be unique
126 open(C,">",$compose_filename)
127 or die "Failed to open for writing $compose_filename: $!";
128 print C "From \n";
129 printf C "Subject: %s\n\n", $initial_subject;
130 printf C <<EOT;
131 GIT: Please enter your email below.
132 GIT: Lines beginning in "GIT: " will be removed.
133 GIT: Consider including an overall diffstat or table of contents
134 GIT: for the patch you are writing.
137 close(C);
139 my $editor = $ENV{EDITOR};
140 $editor = 'vi' unless defined $editor;
141 system($editor, $compose_filename);
143 open(C2,">",$compose_filename . ".final")
144 or die "Failed to open $compose_filename.final : " . $!;
146 open(C,"<",$compose_filename)
147 or die "Failed to open $compose_filename : " . $!;
149 while(<C>) {
150 next if m/^GIT: /;
151 print C2 $_;
153 close(C);
154 close(C2);
156 do {
157 $_ = $term->readline("Send this email? (y|n) ");
158 } while (!defined $_);
160 if (uc substr($_,0,1) ne 'Y') {
161 cleanup_compose_files();
162 exit(0);
165 @files = ($compose_filename . ".final");
169 # Now that all the defaults are set, process the rest of the command line
170 # arguments and collect up the files that need to be processed.
171 for my $f (@ARGV) {
172 if (-d $f) {
173 opendir(DH,$f)
174 or die "Failed to opendir $f: $!";
176 push @files, grep { -f $_ } map { +$f . "/" . $_ }
177 sort readdir(DH);
179 } elsif (-f $f) {
180 push @files, $f;
182 } else {
183 print STDERR "Skipping $f - not found.\n";
187 if (@files) {
188 print $_,"\n" for @files;
189 } else {
190 print <<EOT;
191 git-send-email [options] <file | directory> [... file | directory ]
192 Options:
193 --from Specify the "From:" line of the email to be sent.
195 --to Specify the primary "To:" line of the email.
197 --compose Use \$EDITOR to edit an introductory message for the
198 patch series.
200 --subject Specify the initial "Subject:" line.
201 Only necessary if --compose is also set. If --compose
202 is not set, this will be prompted for.
204 --in-reply-to Specify the first "In-Reply-To:" header line.
205 Only used if --compose is also set. If --compose is not
206 set, this will be prompted for.
208 --chain-reply-to If set, the replies will all be to the previous
209 email sent, rather than to the first email sent.
210 Defaults to on.
212 --smtp-server If set, specifies the outgoing SMTP server to use.
213 Defaults to localhost.
215 Error: Please specify a file or a directory on the command line.
217 exit(1);
220 # Variables we set as part of the loop over files
221 our ($message_id, $cc, %mail, $subject, $reply_to, $message);
224 # Usually don't need to change anything below here.
226 # we make a "fake" message id by taking the current number
227 # of seconds since the beginning of Unix time and tacking on
228 # a random number to the end, in case we are called quicker than
229 # 1 second since the last time we were called.
231 # We'll setup a template for the message id, using the "from" address:
232 my $message_id_from = Email::Valid->address($from);
233 my $message_id_template = "<%s-git-send-email-$message_id_from>";
235 sub make_message_id
237 my $date = `date "+\%s"`;
238 chomp($date);
239 my $pseudo_rand = int (rand(4200));
240 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
241 #print "new message id = $message_id\n"; # Was useful for debugging
246 $cc = "";
248 sub send_message
250 my $to = join (", ", unique_email_list(@to));
252 %mail = ( To => $to,
253 From => $from,
254 CC => $cc,
255 Subject => $subject,
256 Message => $message,
257 'Reply-to' => $from,
258 'In-Reply-To' => $reply_to,
259 'Message-ID' => $message_id,
260 'X-Mailer' => "git-send-email",
263 $mail{smtp} = $smtp_server;
264 $mailcfg{mime} = 0;
266 #print Data::Dumper->Dump([\%mail],[qw(*mail)]);
268 sendmail(%mail) or die $Mail::Sendmail::error;
270 print "OK. Log says:\n", $Mail::Sendmail::log;
271 print "\n\n"
275 $reply_to = $initial_reply_to;
276 make_message_id();
277 $subject = $initial_subject;
279 foreach my $t (@files) {
280 my $F = $t;
281 open(F,"<",$t) or die "can't open file $t";
283 @cc = ();
284 my $found_mbox = 0;
285 my $header_done = 0;
286 $message = "";
287 while(<F>) {
288 if (!$header_done) {
289 $found_mbox = 1, next if (/^From /);
290 chomp;
292 if ($found_mbox) {
293 if (/^Subject:\s+(.*)$/) {
294 $subject = $1;
296 } elsif (/^(Cc|From):\s+(.*)$/) {
297 printf("(mbox) Adding cc: %s from line '%s'\n",
298 $2, $_);
299 push @cc, $2;
302 } else {
303 # In the traditional
304 # "send lots of email" format,
305 # line 1 = cc
306 # line 2 = subject
307 # So let's support that, too.
308 if (@cc == 0) {
309 printf("(non-mbox) Adding cc: %s from line '%s'\n",
310 $_, $_);
312 push @cc, $_;
314 } elsif (!defined $subject) {
315 $subject = $_;
319 # A whitespace line will terminate the headers
320 if (m/^\s*$/) {
321 $header_done = 1;
323 } else {
324 $message .= $_;
325 if (/^Signed-off-by: (.*)$/i) {
326 my $c = $1;
327 chomp $c;
328 push @cc, $c;
329 printf("(sob) Adding cc: %s from line '%s'\n",
330 $c, $_);
334 close F;
336 $cc = join(", ", unique_email_list(@cc));
338 send_message();
340 # set up for the next message
341 if ($chain_reply_to || length($reply_to) == 0) {
342 $reply_to = $message_id;
344 make_message_id();
347 if ($compose) {
348 cleanup_compose_files();
351 sub cleanup_compose_files() {
352 unlink($compose_filename, $compose_filename . ".final");
358 sub unique_email_list(@) {
359 my %seen;
360 my @emails;
362 foreach my $entry (@_) {
363 my $clean = Email::Valid->address($entry);
364 next if $seen{$clean}++;
365 push @emails, $entry;
367 return @emails;