Code

[PATCH] Add "--chain-reply-to" to git-send-email-script, to control whether or not the
[git.git] / git-send-email-script
1 #!/usr/bin/perl -w
2 #
3 # Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4 # Copyright 2005 Ryan Anderson <ryan@michonline.com>
5 #
6 # GPL v2 (See COPYING)
7
8 # Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
9 #
10 # Sends a collection of emails to the given email addresses, disturbingly fast.
11
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.
17
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 # Variables we fill in automatically, or via prompting:
28 my (@to,@cc,$initial_reply_to,$initial_subject,@files,$from);
30 # Behavior modification variables
31 my ($chain_reply_to) = (1);
33 # Example reply to:
34 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
36 my $term = new Term::ReadLine 'git-send-email';
38 # Begin by accumulating all the variables (defined above), that we will end up
39 # needing, first, from the command line:
41 my $rc = GetOptions("from=s" => \$from,
42                     "in-reply-to=s" => \$initial_reply_to,
43                     "subject=s" => \$initial_subject,
44                     "to=s" => \@to,
45                     "chain-reply-to!" => \$chain_reply_to,
46          );
48 # Now, let's fill any that aren't set in with defaults:
50 open(GITVAR,"-|","git-var","-l")
51         or die "Failed to open pipe from git-var: $!";
53 my ($author,$committer);
54 while(<GITVAR>) {
55         chomp;
56         my ($var,$data) = split /=/,$_,2;
57         my @fields = split /\s+/, $data;
59         my $ident = join(" ", @fields[0...(@fields-3)]);
61         if ($var eq 'GIT_AUTHOR_IDENT') {
62                 $author = $ident;
63         } elsif ($var eq 'GIT_COMMITTER_IDENT') {
64                 $committer = $ident;
65         }
66 }
67 close(GITVAR);
70 if (!defined $from) {
71         $from = $author || $committer;
72         1 while (!defined ($_ = $term->readline("Who should the emails appear to be from? ", 
73                                 $from)));
74         $from = $_;
75         print "Emails will be sent from: ", $from, "\n";
76 }
78 if (!@to) {
79         1 while (!defined ($_ = $term->readline("Who should the emails be sent to? ", 
80                                 "")));
81         my $to = $_;
82         push @to, split /,/, $to;
83 }
85 if (!defined $initial_subject) {
86         1 while (!defined ($_ = 
87                 $term->readline("What subject should the emails start with? ", 
88                         $initial_subject)));
89         $initial_subject = $_;
90 }
92 if (!defined $initial_reply_to) {
93         1 while (!defined ($_ = 
94                 $term->readline("Message-ID to be used as In-Reply-To? ", 
95                         $initial_reply_to)));
96         $initial_reply_to = $_;
97         $initial_reply_to =~ s/(^\s+|\s+$)//g;
98 }
100 # Now that all the defaults are set, process the rest of the command line
101 # arguments and collect up the files that need to be processed.
102 for my $f (@ARGV) {
103         if (-d $f) {
104                 opendir(DH,$f)
105                         or die "Failed to opendir $f: $!";
107                 push @files, map { +$f . "/" . $_ } grep !/^\.{1,2}$/,
108                         sort readdir(DH);
109         } elsif (-f $f) {
110                 push @files, $f;
112         } else {
113                 print STDERR "Skipping $f - not found.\n";
114         }
117 if (@files) {
118         print $_,"\n" for @files;
119 } else {
120         print <<EOT;
121 git-send-email-script [options] <file | directory> [... file | directory ]
122 Options:
123    --from         Specify the "From:" line of the email to be sent.
124    --to           Specify the primary "To:" line of the email.
125    --subject      Specify the initial "Subject:" line.
126    --in-reply-to  Specify the first "In-Reply-To:" header line.
127    --chain-reply-to If set, the replies will all be to the first
128                   email sent, rather than to the last email sent.
130 Error: Please specify a file or a directory on the command line.
131 EOT
132         exit(1);
135 # Variables we set as part of the loop over files
136 our ($message_id, $cc, %mail, $subject, $reply_to, $message);
139 # Usually don't need to change anything below here.
141 # we make a "fake" message id by taking the current number
142 # of seconds since the beginning of Unix time and tacking on
143 # a random number to the end, in case we are called quicker than
144 # 1 second since the last time we were called.
145 sub make_message_id
147         my $date = `date "+\%s"`;
148         chomp($date);
149         my $pseudo_rand = int (rand(4200));
150         $message_id = "<$date$pseudo_rand\@foobar.com>";
151         print "new message id = $message_id\n";
156 $cc = "";
158 sub send_message
160         my %to;
161         $to{lc(Email::Valid->address($_))}++ for (@to);
163         my $to = join(",", keys %to);
165         %mail = (       To      =>      $to,
166                         From    =>      $from,
167                         CC      =>      $cc,
168                         Subject =>      $subject,
169                         Message =>      $message,
170                         'Reply-to'      =>      $from,
171                         'In-Reply-To'   =>      $reply_to,
172                         'Message-ID'    =>      $message_id,
173                         'X-Mailer'      =>      "git-send-email-script",
174                 );
176         $mail{smtp} = 'localhost';
177         $mailcfg{mime} = 0;
179         #print Data::Dumper->Dump([\%mail],[qw(*mail)]);
181         sendmail(%mail) or die $Mail::Sendmail::error;
183         print "OK. Log says:\n", $Mail::Sendmail::log;
184         print "\n\n"
188 $reply_to = $initial_reply_to;
189 make_message_id();
190 $subject = $initial_subject;
192 foreach my $t (@files) {
193         my $F = $t;
194         open(F,"<",$t) or die "can't open file $t";
196         @cc = ();
197         my $found_mbox = 0;
198         my $header_done = 0;
199         $message = "";
200         while(<F>) {
201                 if (!$header_done) {
202                         $found_mbox = 1, next if (/^From /);
203                         chomp;
205                         if ($found_mbox) {
206                                 if (/^Subject:\s+(.*)$/) {
207                                         $subject = $1;
209                                 } elsif (/^(Cc|From):\s+(.*)$/) {
210                                         printf("(mbox) Adding cc: %s from line '%s'\n",
211                                                 $2, $_);
212                                         push @cc, $2;
213                                 }
215                         } else {
216                                 # In the traditional
217                                 # "send lots of email" format,
218                                 # line 1 = cc
219                                 # line 2 = subject
220                                 # So let's support that, too.
221                                 if (@cc == 0) {
222                                         printf("(non-mbox) Adding cc: %s from line '%s'\n",
223                                                 $_, $_);
225                                         push @cc, $_;
227                                 } elsif (!defined $subject) {
228                                         $subject = $_;
229                                 }
230                         }
231                         
232                         # A whitespace line will terminate the headers
233                         if (m/^\s*$/) {
234                                 $header_done = 1;
235                         }
236                 } else {
237                         $message .=  $_;
238                         if (/^Signed-off-by: (.*)$/i) {
239                                 my $c = $1;
240                                 chomp $c;
241                                 push @cc, $c;
242                                 printf("(sob) Adding cc: %s from line '%s'\n",
243                                         $c, $_);
244                         }
245                 }
246         }
247         close F;
249         my %clean_ccs;
250         $clean_ccs{lc(Email::Valid->address($_))}++ for @cc;
252         $cc = join(",", keys %clean_ccs);
254         send_message();
256         # set up for the next message
257         if ($chain_reply_to || length($reply_to) == 0) {
258                 $reply_to = $message_id;
259         }
260         make_message_id();
261 #       $subject = "Re: ".$initial_subject;