Annotation of CVSROOT/loginfo.pl, revision 1.2
1.1 albertel 1: #!/usr/bin/perl -w
2: # include this script in your loginfo as:
3: # <modulename> /path/to/loginfo.pl sender@domain recipient@domain %{sVv}
4: #
5: # Copyright (c) 1999, 2000 Sascha Schumann <sascha@schumann.cx>
6:
7: # This makes some basic assumptions -- you are only checking
8: # in to a single CVS module.
9:
10: # This also doesn't like files or directories with spaces in them.
11:
12: use strict;
13:
14: use Socket;
15: use POSIX;
16:
17: $SIG{PIPE} = 'IGNORE';
18:
19: my $last_file = "/var/cvs/lastdir";
20: my $summary = "/var/cvs/summary";
21: my $smtpserver = "127.0.0.1";
22: my $smtpport = 25;
23: my $cvs = "/usr/bin/cvs";
24: my $cvsroot = $ENV{CVSROOT}."/";
25: # remove double trailing slash
26: $cvsroot =~ s/\/\/$/\//;
27: my $cvsusers = "/repository/CVSROOT/cvsusers";
28:
29: # get the id of this process group for use in figuring out
30: # whether this is the last directory with checkins or not
31: my $id = getpgrp();
32:
33: # the command line looks something like this for a normal commit:
34: # ("user@example.com", "cvsuser",
35: # "module changedfile,1.1,1.2 addedfile,NONE,1.1 removedfile,1.1,NONE")
36: my $mailfrom = shift;
37: my $mailto = $mailfrom;
38: my $envaddr = $mailto;
39:
40: my $cvsuser = shift;
41: my @args = split(" ", $ARGV[0]);
42: my $directory = shift @args;
43:
44: # extract just the module name from the directory
45: my $module = $directory;
46: $module =~ s/\/.+$//;
47:
48: if ($cvsuser eq "changelog" && $module ne "php-gtk") {
49: $envaddr = "php-cvs-daily-private\@lists.php.net";
50: $mailto = "php-cvs-daily\@lists.php.net";
51: }
52:
53: # bail when this is a new directory
54: &bail if $args[0] eq '-' && "$args[1] $args[2]" eq 'New directory';
55:
56: # bail if this is an import
57: &bail if $args[0] eq '-' && $args[1] eq 'Imported';
58:
59: # find out the last directory being processed
60: open FC, "$last_file.$id"
61: or die "last file does not exist";
62: my $last_directory = <FC>;
63: chop $last_directory;
64: close FC;
65: # remove the cvsroot from the front
66: $last_directory =~ s/^$cvsroot//;
67:
68: # add our changed files to the summary
69: open(FC, ">>$summary.$id") || die "cannot open summary file";
70: foreach my $arg (@args) {
71: print FC "$directory/$arg\n";
72: }
73: close(FC);
74:
75: # is this script already in the last changed directory?
76:
77: # exit if this isn't the last directory
78: &bail if($last_directory ne $directory);
79:
80: # get the log message and tag -- we throw away everything from STDIN
81: # before a line that begins with "Log Message"
82: my ($logmsg,$tag) = &get_log_message();
83:
84: # now we fork off into the background and generate the email
85: exit 0 if(fork() != 0);
86:
87: $| = 1;
88:
89: #print "Reading summary file\n";
90:
91: open(FC, "<$summary.$id");
92:
93: my (@added_files, @removed_files, @modified_files, @modified_files_info);
94: while (<FC>) {
95: chop;
96: my ($file, $old, $new) = split(",");
1.2 ! albertel 97: if ($file =~ m|TexConvert/tt.dynamic|) { next; }
1.1 albertel 98: if($old eq "NONE") {
99: push @added_files, $file;
100: } elsif($new eq "NONE") {
101: push @removed_files, $file;
102: } else {
103: push @modified_files, $file;
104: push @modified_files_info, [ $file, $old, $new ];
105: }
106: }
107: close FC;
108:
109: #print "Unlinking helper files\n";
110:
111: # clean up a little bit
112:
113: unlink("$summary.$id");
114: unlink("$last_file.$id");
115:
116: #print "Running rdiff\n";
117:
118: # build a diff (and new files) if necessary
119: my $diffmsg = '';
120:
121: foreach my $info (@modified_files_info) {
122: my ($file, $old, $new) = @$info;
123: open(LOG, "$cvs -Qn rdiff -r $old -r $new -u $file|") || die;
124: while(<LOG>) { s/\r\n/\n/; $diffmsg .= $_; }
125: close(LOG);
126: }
127:
128: # add the added files
129:
130: foreach my $file (@added_files) {
131: next if $file =~ /\.(gif|jpe|jpe?g|pdf|png|exe|class|tgz|tar.gz|jar)$/i
132: or $file !~ /\./;
133: $diffmsg .= "\nIndex: $file\n+++ $file\n";
134: open(LOG, "$cvs -Qn checkout -p -r1.1 $file |") || die;
135: while(<LOG>) { s/\r\n/\n/; $diffmsg .= $_; }
136: close(LOG);
137: }
138:
139: #print "Building commit email\n";
140:
141: my $subj_tag = $tag ? "($tag)" : '';
142: my $body_tag = $tag ? "(Branch: $tag)" : '';
143:
144: # build our email
145: my $msg = "";
146: if($#added_files ne -1) {
147: $msg .= "\n Added files: $body_tag";
148: $msg .= &build_list(@added_files);
149: $body_tag = '';
150: }
151: if($#removed_files ne -1) {
152: $msg .= "\n Removed files: $body_tag";
153: $msg .= &build_list(@removed_files);
154: $body_tag = '';
155: }
156: if($#modified_files ne -1) {
157: $msg .= "\n Modified files: $body_tag";
158: $msg .= &build_list(@modified_files);
159: $body_tag = '';
160: }
161:
162: my $subj = "";
163: my %dirfiles;
164: my @dirs = &get_dirs(@added_files, @removed_files, @modified_files);
165:
166: foreach my $dir (@dirs) {
167: $subj .= "$dir @{ $dirfiles{$dir} } ";
168: }
169:
170: my $msgid = "Message-ID: <cvs$cvsuser".time()."\@cvsserver>\n";
171:
172: my $from;
173: if (open FD, $cvsusers) {
174: while(<FD>) {
175: chop;
176: if (m/^$cvsuser:(.+?):(.+)$/) {
177: $from = "\"$1\" <$2>";
178: }
179: }
180: close(FD);
181: }
182:
183: $from ||= "$cvsuser <$mailfrom>";
184:
185: # "Reply-to: $mailto\n".
186: # "Date: ".localtime()."\n".
187: my (@DAYABBR) = qw(Sun Mon Tue Wed Thu Fri Sat);
188: my (@MONABBR) = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
189:
190: my (@gmtime) = gmtime();
191: my $rfc822date = sprintf("Date: %s, %02d %s %d %02d:%02d:%02d -0000\n",
192: $DAYABBR[$gmtime[6]], $gmtime[3], $MONABBR[$gmtime[4]],
193: $gmtime[5] + 1900, $gmtime[2], $gmtime[1], $gmtime[0]);
194:
195: no strict; # quiet warnings after here
196:
197: my $email;
198: my $common_header = "".
199: "From: $from\n".
200: "To: $mailto\n".
201: $msgid.
202: $rfc822date.
203: "Subject: cvs: $module$subj_tag $subj\n";
204:
205: my $common_body = "".
206: "$cvsuser\t\t".localtime()." EDT\n".
207: "$msg".
208: " Log:\n".
209: &indent($logmsg,2)."\n";
210:
211: my $boundary = $cvsuser.time();
212:
213: if (length($diffmsg) > 8000) {
214: my $now = POSIX::strftime("%Y%m%d%H%M%S", localtime);
215: $email = $common_header.
216: "MIME-Version: 1.0\n".
217: "Content-Type: multipart/mixed; boundary=\"$boundary\"\n".
218: "\n".
219: "This is a MIME encoded message\n\n".
220: "--$boundary\n".
221: "Content-Type: text/plain\n".
222: "\n".
223: $common_body.
224: "--$boundary\n".
225: "Content-Type: text/plain\n".
226: "Content-Disposition: attachment; filename=\"$cvsuser-$now.txt\"\n".
227: "\n".
228: "$diffmsg\n".
229: "--$boundary--\n";
230: } else {
231: $email = $common_header.
232: "\n".
233: $common_body.
234: "$diffmsg\n";
235: }
236:
237: $email =~ s/\r//g;
238: $email =~ s/\n/\r\n/g;
239:
240: # send our email
241:
242: print "Mailing the commit email to $mailto\n";
243:
244: #print $email;
245:
246: my $paddr = sockaddr_in($smtpport, inet_aton($smtpserver));
247: socket(SOCK, PF_INET, SOCK_STREAM, 0) || die "socket failed";
248: connect(SOCK, $paddr) || die "connect $smtpserver:$smtpport failed";
249: select(SOCK);
250: $|=1;
251:
252: print "HELO cvsserver\r\n".
253: "MAIL FROM:<this-will-bounce\@php.net>\r\n" .
254: "RCPT TO:<$envaddr>\r\n" .
255: "DATA\r\n".
256: "$email\r\n".
257: ".\r\n".
258: "QUIT\r\n";
259:
260: while(<SOCK>) { alarm(20); };
261:
262: close(SOCK);
263: exit 0;
264:
265: sub get_log_message {
266: my ($logmsg, $tag);
267: while (<STDIN>) {
268: $logmsg .= $_ if defined $logmsg;
269: if (/^Log Message/) { $logmsg = ""; }
270: if (/^\s+Tag:\s+(\w+)/) { $tag = $1; }
271: }
272: return ($logmsg, $tag);
273: }
274:
275: sub build_list {
276: my(@arr) = @_;
277: my($curdir, $curlen, $msg);
278:
279: $msg = "";
280: $curdir = "";
281: foreach (@arr) {
282: /^(.*)\/([^\/]+)$/;
283: my $dir = $1;
284: my $file = $2;
285: if($dir ne $curdir) {
286: $curdir = $dir;
287: $msg .= "\n /$curdir\t";
288: $curlen = length($curdir) + 5;
289: }
290: if(($curlen + length($file)) > 70) {
291: $msg .= "\n ".sprintf("%-".length($curdir)."s", "")."\t";
292: $curlen = length($curdir) + 5;
293: }
294: $msg .= $file." ";
295: $curlen += length($file) + 1;
296: }
297:
298: $msg .= "\n";
299:
300: return $msg;
301: }
302:
303: sub get_dirs {
304: my @files = sort @_;
305: foreach my $file (@files) {
306: (my $dir = $file) =~ s#[^/]+$##;
307: $dir =~ s/^$module//;
308: $dir =~ s/(.+)\//$1/;
309: $file =~ s#^.+/(.+)$#$1#;
310: push @{ $dirfiles{$dir} }, $file;
311: }
312: return sort keys %dirfiles;
313: }
314:
315: sub indent {
316: my ($msg,$nr) = @_;
317: my $s = " " x $nr;
318: $msg =~ s/\n/\n$s/g;
319: return $s.$msg;
320: }
321:
322: sub trim {
323: my ($x) = @_;
324: $x =~ s/^\s+//;
325: $x =~ s/\s+$//;
326: return $x;
327: }
328:
329: # eat STDIN (to avoid parent getting SIGPIPE) and exit with supplied exit code
330: sub bail {
331: my @toss = <STDIN>;
332: exit @_;
333: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>