| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# CVS loginfo 2 mail |
| 4 |
# Copyright (c) 2001-2002 SATOH Fumiyasu. All rights reserved. |
| 5 |
# |
| 6 |
# Date: 2002-01-31, since 2001-10-29 |
| 7 |
# License: GNU General Public License ver.2 |
| 8 |
# |
| 9 |
# Required external commands: |
| 10 |
# sendmail |
| 11 |
# rcsdiff |
| 12 |
# |
| 13 |
# $Id: loginfo2mail.pl,v 1.1 2004/06/18 07:58:20 orrisroot Exp $ |
| 14 |
|
| 15 |
# FIXME: Read configurations from external file |
| 16 |
# FIXME: Print `cvs rdiff' command line to generate diff output |
| 17 |
# [namazu-devel-ja 2164] [namazu-devel-ja 2166] |
| 18 |
# FIXME: Support to customize sender address |
| 19 |
# FIXME: Support diff(1) options for rcsdiff(1) |
| 20 |
# FIXME: Support chroot(2)-ed environment |
| 21 |
|
| 22 |
use strict; |
| 23 |
use English; |
| 24 |
use FindBin; |
| 25 |
use Getopt::Std; |
| 26 |
use IO::File; |
| 27 |
|
| 28 |
$ENV{'PATH'} = "/usr/bin:/usr/sbin:/usr/lib:$ENV{'PATH'}"; |
| 29 |
|
| 30 |
my $CMD_NAME ="cvs-$FindBin::Script"; |
| 31 |
my $CMD_VERSION = "0.1.7"; |
| 32 |
|
| 33 |
# Options |
| 34 |
# ====================================================================== |
| 35 |
|
| 36 |
# CVS root |
| 37 |
my $cvs_root = $ENV{'CVSROOT'}; |
| 38 |
|
| 39 |
# URL for CVS web interface by cvsweb or ViewCVS |
| 40 |
my $cvsweb_url = 'http://cvs.sourceforge.jp/cgi-bin/viewcvs.cgi/samurai-graph/'; |
| 41 |
# Type of CVS web interface: 'cvsweb' or 'viewcvs' |
| 42 |
my $cvsweb_type = 'viewcvs'; |
| 43 |
|
| 44 |
# Maximum line number of rcsdiff(1) output (0< mean `no limit') |
| 45 |
my $diff_maxline = -1; |
| 46 |
|
| 47 |
# Content language |
| 48 |
my $lang = 'NONE'; |
| 49 |
|
| 50 |
my $usage = <<EOF_USAGE; |
| 51 |
Usage: in \$CVSROOT/CVSROOT/loginfo |
| 52 |
^module \$CVSROOT/CVSROOT/$CMD_NAME [options] %{sVv} address ... |
| 53 |
|
| 54 |
Options: |
| 55 |
-d cvsroot CVS root directory [\$CVSROOT] |
| 56 |
-w webtype Type of web interface [$cvsweb_type] |
| 57 |
-W url URL for CVS web interface [$cvsweb_url] |
| 58 |
-f address Envelope sender address (not implemented yet) |
| 59 |
-D number Max line number of rcsdiff(1) output [$diff_maxline] |
| 60 |
-L language Content language [$lang] |
| 61 |
|
| 62 |
%{sVv} CVS loginfo |
| 63 |
address Mail CVS loginfo to this address(es) |
| 64 |
|
| 65 |
Type of web interface: |
| 66 |
viewcvs ViewCVS - http://viewcvs.sourceforge.net/ |
| 67 |
cvsweb cvsweb - http://stud.fh-heilbronn.de/~zeller/cgi/cvsweb.cgi/ |
| 68 |
|
| 69 |
Content Language: |
| 70 |
ja Japanese (ISO-2022-JP) |
| 71 |
EOF_USAGE |
| 72 |
|
| 73 |
# Command-line options |
| 74 |
# ---------------------------------------------------------------------- |
| 75 |
|
| 76 |
use vars qw($opt_d $opt_w $opt_W $opt_f $opt_D $opt_L); |
| 77 |
if (!getopts('d:w:W:f:D:L:') || @ARGV < 2) { |
| 78 |
print($usage); |
| 79 |
exit(1); |
| 80 |
} |
| 81 |
|
| 82 |
$cvs_root = $opt_d if (defined($opt_d)); |
| 83 |
$cvsweb_type = $opt_w if (defined($opt_w)); |
| 84 |
$cvsweb_url = $opt_W if (defined($opt_W)); |
| 85 |
$diff_maxline = $opt_D if (defined($opt_D)); |
| 86 |
$lang = $opt_L if (defined($opt_L)); |
| 87 |
|
| 88 |
my $cvs_info = shift(@ARGV); |
| 89 |
my @recip = @ARGV; |
| 90 |
|
| 91 |
# Check options |
| 92 |
# ---------------------------------------------------------------------- |
| 93 |
|
| 94 |
if (!defined($cvs_root)) { |
| 95 |
die "\$CVSROOT not defined.\n"; |
| 96 |
} |
| 97 |
|
| 98 |
# Language specific? |
| 99 |
# ====================================================================== |
| 100 |
|
| 101 |
my $ja_convert = undef; |
| 102 |
if ($lang eq 'ja') { |
| 103 |
eval('use Jcode'); |
| 104 |
if ($@) { |
| 105 |
eval('require "$cvs_root/CVSROOT/scripts/jcode.pl"'); |
| 106 |
if ($@) { |
| 107 |
die "Jcode.pm or jcode.pl required to process Japanese.\n"; |
| 108 |
} |
| 109 |
$ja_convert = sub { |
| 110 |
jcode::convert($_[0], 'jis'); |
| 111 |
}; |
| 112 |
} else { |
| 113 |
$ja_convert = sub { |
| 114 |
Jcode::convert($_[0], 'jis'); |
| 115 |
}; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
# Parse %{sVv} |
| 120 |
# ====================================================================== |
| 121 |
|
| 122 |
$cvs_info =~ s/ - (New directory|Imported sources)//g; |
| 123 |
my ($cvs_module, @cvs_fileinfo) = split(/ /, $cvs_info); |
| 124 |
|
| 125 |
my @cvs_file = my %cvs_rev_old = my %cvs_rev_new = (); |
| 126 |
foreach my $cvs_fileinfo (@cvs_fileinfo) { |
| 127 |
my ($file, $rev_old, $rev_new) = split(/,/, $cvs_fileinfo); |
| 128 |
push(@cvs_file, $file); |
| 129 |
$cvs_rev_old{$file} = $rev_old; |
| 130 |
$cvs_rev_new{$file} = $rev_new; |
| 131 |
} |
| 132 |
|
| 133 |
# Get CVS username |
| 134 |
# ====================================================================== |
| 135 |
|
| 136 |
my $cvs_user = $ENV{'CVS_USER'}; |
| 137 |
$cvs_user = getpwuid($EUID) if (!defined($cvs_user)); |
| 138 |
$cvs_user = $EUID if (!defined($cvs_user)); |
| 139 |
|
| 140 |
# Make mail header |
| 141 |
# ====================================================================== |
| 142 |
|
| 143 |
my $header = <<EOF_HEADER; |
| 144 |
Subject: $cvs_module committed by $cvs_user |
| 145 |
X-CVS-LogInfo: $CMD_NAME/$CMD_VERSION |
| 146 |
X-CVS-Root: $cvs_root |
| 147 |
X-CVS-User: $cvs_user |
| 148 |
EOF_HEADER |
| 149 |
|
| 150 |
$header .= "To: "; |
| 151 |
$header .= join(",\n ", @recip) . "\n"; |
| 152 |
$header .= 'Content-Type: text/plain'; |
| 153 |
$header .= '; charset=ISO-2022-JP' if ($opt_L eq 'ja'); |
| 154 |
$header .= "\n"; |
| 155 |
$header .= "MIME-Version: 1.0\n" if ($opt_L ne 'NONE'); |
| 156 |
|
| 157 |
foreach my $cvs_file (@cvs_file) { |
| 158 |
my $file = "$cvs_module/$cvs_file"; |
| 159 |
my $rev_old = $cvs_rev_old{$cvs_file}; |
| 160 |
my $rev_new = $cvs_rev_new{$cvs_file}; |
| 161 |
|
| 162 |
if ($rev_old eq 'NONE') { |
| 163 |
$header .= "X-CVS-Added"; |
| 164 |
} elsif ($rev_new eq 'NONE') { |
| 165 |
$header .= "X-CVS-Removed"; |
| 166 |
} else { |
| 167 |
$header .= "X-CVS-Modified"; |
| 168 |
} |
| 169 |
$header .= ": $file $rev_old->$rev_new\n"; |
| 170 |
} |
| 171 |
|
| 172 |
# Send mail |
| 173 |
# ====================================================================== |
| 174 |
|
| 175 |
my $fh_sendmail = new IO::File; |
| 176 |
my $pid_sendmail = $fh_sendmail->open('|-'); |
| 177 |
if (!defined($pid_sendmail)) { # fork failed |
| 178 |
print "Cannot fork child or open pipe: $!\n"; |
| 179 |
exit(1); |
| 180 |
} elsif ($pid_sendmail == 0) { # child process |
| 181 |
if (!exec('sendmail', '-t', '-oi')) { |
| 182 |
print "Cannot exec sendmail: $!\n"; |
| 183 |
exit(1); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
# Print header fields and log from cvs command |
| 188 |
# ---------------------------------------------------------------------- |
| 189 |
|
| 190 |
$fh_sendmail->print($header); |
| 191 |
$fh_sendmail->print("\n"); |
| 192 |
while (defined(my $line = STDIN->getline())) { |
| 193 |
if ($opt_L eq 'ja') { |
| 194 |
$ja_convert->(\$line); |
| 195 |
} |
| 196 |
$fh_sendmail->print($line); |
| 197 |
} |
| 198 |
$fh_sendmail->print("\n"); |
| 199 |
|
| 200 |
# Print commitment type and URL for web interface |
| 201 |
# ---------------------------------------------------------------------- |
| 202 |
|
| 203 |
foreach my $cvs_file (@cvs_file) { |
| 204 |
my $file = "$cvs_module/$cvs_file"; |
| 205 |
my $rev_old = $cvs_rev_old{$cvs_file}; |
| 206 |
my $rev_new = $cvs_rev_new{$cvs_file}; |
| 207 |
|
| 208 |
$fh_sendmail->print("$file $rev_old -> $rev_new ("); |
| 209 |
if ($rev_old eq 'NONE') { |
| 210 |
$fh_sendmail->print('added'); |
| 211 |
} elsif ($rev_new eq 'NONE') { |
| 212 |
$fh_sendmail->print('removed'); |
| 213 |
} else { |
| 214 |
$fh_sendmail->print('modified'); |
| 215 |
} |
| 216 |
$fh_sendmail->print(")\n"); |
| 217 |
$fh_sendmail->print("$cvsweb_url/$file"); |
| 218 |
|
| 219 |
if ($rev_old eq 'NONE' || $rev_new eq 'NONE') { |
| 220 |
$fh_sendmail->print("?rev="); |
| 221 |
$fh_sendmail->print($rev_old eq 'NONE' ? $rev_new : $rev_old); |
| 222 |
$fh_sendmail->print('&content-type=text/'); |
| 223 |
if ($cvsweb_type eq 'cvsweb') { |
| 224 |
$fh_sendmail->print('x-cvsweb-markup'); |
| 225 |
} else { |
| 226 |
$fh_sendmail->print('vnd.viewcvs-markup'); |
| 227 |
} |
| 228 |
$fh_sendmail->print("\n"); |
| 229 |
} else { |
| 230 |
$fh_sendmail->print(".diff?r1=$rev_old&r2=$rev_new\n"); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
# Print diff by rcsdiff |
| 235 |
# ---------------------------------------------------------------------- |
| 236 |
|
| 237 |
if ($diff_maxline == 0) { |
| 238 |
exit(0); |
| 239 |
} |
| 240 |
|
| 241 |
chdir($cvs_root) || die "Cannot change working directory: $!\n"; |
| 242 |
my $diff_line = 0; |
| 243 |
foreach my $cvs_file (@cvs_file) { |
| 244 |
if ($diff_line == $diff_maxline) { |
| 245 |
$fh_sendmail->print("\n\n---------- Diff output was snipped here.\n"); |
| 246 |
last; |
| 247 |
} |
| 248 |
|
| 249 |
my $file = "$cvs_module/$cvs_file,v"; |
| 250 |
my $rev_old = $cvs_rev_old{$cvs_file}; |
| 251 |
my $rev_new = $cvs_rev_new{$cvs_file}; |
| 252 |
next if ($rev_new eq 'NONE' || $rev_old eq 'NONE'); |
| 253 |
|
| 254 |
$fh_sendmail->print("\n"); |
| 255 |
|
| 256 |
my $fh_rcsdiff = new IO::File; |
| 257 |
my $pid_rcsdiff = $fh_rcsdiff->open('-|'); |
| 258 |
if (!defined($pid_rcsdiff)) { # fork failed |
| 259 |
print "Cannot fork child or open pipe: $!\n"; |
| 260 |
exit(1); |
| 261 |
} elsif ($pid_rcsdiff == 0) { # child process |
| 262 |
open(STDERR, '>&STDOUT') || warn "Cannot duplicate stdout: $!\n"; |
| 263 |
if (!exec('rcsdiff', '-u', '-kk', "-r$rev_old", "-r$rev_new", $file)) { |
| 264 |
print "Cannot exec rcsdiff: $!\n"; |
| 265 |
exit(1); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
while (defined(my $line = $fh_rcsdiff->getline())) { |
| 270 |
if ($opt_L eq 'ja') { |
| 271 |
$ja_convert->(\$line); |
| 272 |
} |
| 273 |
$fh_sendmail->print($line); |
| 274 |
|
| 275 |
if ($diff_maxline > 0 && ++$diff_line == $diff_maxline) { |
| 276 |
last; |
| 277 |
} |
| 278 |
} |
| 279 |
# FIXME: Need close and wait for child? |
| 280 |
} |
| 281 |
|
| 282 |
# FIXME: Check sendmail exit code. |
| 283 |
|
| 284 |
# Done |
| 285 |
# ====================================================================== |
| 286 |
|
| 287 |
exit(0); |
| 288 |
|