download cvslog.pl
Language: Perl
License: GPL
Copyright: Copyright 2001, 2003, 2004 Petr Baudis Copyright 1998 Board of Trustees, Leland Stanford Jr. University
LOC: 285
Project Info
Bee Bulletin Board(beebb)
Server: BerliOS
Type: cvs
BerliOS\b\beebb\beebb\CVSROOT\
   cvslog.pl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#!/usr/bin/perl -w
#
# cvslog -- Mail the CVS log message to a given address.
#
# Loosely based on cvslog by Russ Allbery <rra@stanford.edu>
# Copyright 1998  Board of Trustees, Leland Stanford Jr. University
#
# Copyright 2001, 2003, 2004  Petr Baudis <pasky@ucw.cz>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License version 2, as published by the
# Free Software Foundation.
#
# This program is designed to run from CVS's loginfo administrative file and
# takes a log message, massaging it and mailing it away. It's a modified
# version of the log script that comes with CVS, but tries to do less (it
# doesn't do cvs status).
#
# It should be run from loginfo with something like:
#
#       ALL         $CVSROOT/CVSROOT/cvslog %{sVv} $PROJECT $domain_from
#
# Note that it mails everything to the address configured at the top of this
# file.
#
# TODO: attach diffs (under some length, possibly a diffstat instead),
#	non-agressively reformat log messages
#
# $Id: cvslog.pl,v 1.1 2005/06/19 15:31:16 wilk2002 Exp $

use strict;
use vars qw ($cvs_root $project $repository $from_email $line @lista $dest_email $reply_email
		$CVS $diffstat $cvsweb_url $help_msg $sync_delay $max_diff_lines
		$show_diffstat $show_diff $login $subj_files $subj_maxlength
		$withthreading_email $messageid_email $mail_contenttype $mail_contenttransen);




### Configuration

$cvs_root = '/cvsroot/beebb/';

# Project name.
$project = $ARGV[1];

# The path to the repository.  If your platform or CVS implementation doesn't
# pass the full path to the cvslog script in $0 (or if your cvslog script
# isn't in the CVSROOT directory of your repository for some reason), you will
# need to explicitly set $REPOSITORY to the root directory of your repository
# (the same thing that you would set CVSROOT to).
$repository = $cvs_root; # ($0 =~ m#^(.*)/CVSROOT/cvslog$#);

# The from address in the generated mails.

$login = $ENV{'CVS_USER'} || $ENV{'CVS_USERNAME'} || $ENV{'CVSUSER'} ||
	getlogin || (getpwuid($<))[0] || "nobody";
$from_email = $login."<".$login."\@".$ARGV[2].">";  # "lupus-cvs<cvs\@wilk.waw.pl>";

# Mail all reports to this address.
#$dest_email = 'elinks-cvs@v.or.cz, pasky@pasky.ji.cz, fonseca@diku.dk, zas@norz.org';
$dest_email='';
open(F,$cvs_root."CVSROOT/mailing_list");
while($line = <F>)
    {
	if ( $line =~ m/\b$project\b/) 
	{
		($dest_email) = ($line =~ m|$project\s+(.*)$|);
	}
#        @lista = split(/\s+/, $line);
#	if ( $lista[0] =~ m/$project/)
#		{
#		    $dest_email=$lista[1];
#		}
    }
close F;

if (!($dest_email =~ m/\@/ ))
    {
	exit 0;
    }

# Email address all the replies should go at.
$reply_email = "no-reply\@".$ARGV[2]; # wilk.waw.pl';

# suffix of Message-ID in email header
$messageid_email = "\@".$ARGV[2]; # wilk.waw.pl';

# with threading?
$withthreading_email = 1;

# mail content-type
$mail_contenttype="text/plain; charset=UTF-8";

# mail content-transfer-encoding
$mail_contenttransen="8bit";

# The cvs binary location + name (full path to the executable). If in doubt,
# try just 'cvs' and hope. Otherwise, /usr/bin/cvs or /usr/local/bin/cvs could
# do.
$CVS = '/usr/bin/cvs';

# The diffstat binary location + name (full path to the executable) plus the
# additional arguments you want to pass to it. If in doubt, keep the default
# arguments and try just 'diffstat' and hope. Otherwise, /usr/bin/diffstat or
# /usr/local/bin/diffstat could do. Just comment it out if you don't have
# diffstat enabled.
$diffstat = '/home/users/wilk2002/diffstat -p0 -w 72';

# URL of cvsweb. Just comment out if you don't have any.
$cvsweb_url = 'http://cvs.berlios.de/cgi-bin/viewcvs.cgi/beebb/';

# The leading message of the mail:
$help_msg = "To jest wiadomosc powiadamiajaca o zmianie w projekcie $project w jego drzewie CVS.";

# Number of seconds to wait for possible concurrent instances. CVS calls up
# this script for each involved directory separately and this is the sync
# delay. 5s looks as a safe value, but feel free to increase if you are running
# this on a slower (or overloaded) machine or if you have really a lot of
# directories.
$sync_delay = 5;

# Maximal number of lines the diff can contain. If it will be longer, it is
# going to be trimmed to this length.
# Assuming that each line is avg. 60 lines long and we don't want to have mails
# bigger than 35kb, the maximal number of lines would be 597.
$max_diff_lines = 597;

# Whether you want the diffstat of changes to be sent in the message.
$show_diffstat = 1;

# Whether you want the diff of changes to be sent in the message.
$show_diff = 1;

# Whether you want the affected files list in subject
$subj_files=1;

# How long the subject can be
$subj_maxlength=78;

### The code itself

use vars qw (@dirs $module $user $tag $htag $logmsg);


$| = 1;
print "Prosze czekac trwa wysylanie powiadomien do programistow ...\n";


### Load input data

my (@files, %files); # two ways of accessing the same records


# The arguments are from %{sVv}; first the relative path in the repository
# and then the list of files modified.

my @preinput = split (' ', ($ARGV[0] or ''),2);
$dirs[0]->{name} = shift @preinput or die "$0: no directory specified\n";
# allowing files with spaces, but forbiding files starting with space
my @input = split(/([^,]*,[0-9.NOE]*,[0-9.NOE]*) */,$preinput[0]);

if ("@input" eq '- New directory') {
  $dirs[0]->{type} = 'directory';

} else {
  $dirs[0]->{type} = 'checkin';

  foreach (@input) {
    my ($file);
    # regexp, which allows spaces in files, makes empty lines
    if ($_ eq "") {
    	next;
    }

    ($file->{name}, $file->{oldrev}, $file->{newrev}) = split (',');
#    this is setting up later... but later it is imposiible to determine which filenames contain spaces and which not
#    $file->{op} = '?';
    $file->{op}="modify";
    $file->{op}="add"		if($file->{oldrev} eq "NONE");
    $file->{op}="remove"	if($file->{newrev} eq "NONE");
    $file->{op}="?"		if($file->{oldrev} eq "NONE" and $file->{newrev} eq "NONE");

    push (@files, $file);
    $files{$file->{name}} = $file;

    push (@{$dirs[0]->{commits}}, $file);
  }
}


# Guess module name.

$module = $dirs[0]->{name}; $module =~ s#/.*##;


# Figure out who is doing the update.

$user = $login; # $ARGV[1];


# Parse stdin

my $state = 0;
my @op = ('add', 'modify', 'remove');

while (<STDIN>) {
  $tag = $1 if (/^\s*Tag: ([a-zA-Z0-9_-]+)/);
  $state = 1 if /^Added Files:/;
  $state = 2 if /^Modified Files:/;
  $state = 3 if /^Removed Files:/;
  last if /^Log Message/;
  next unless $state;
  foreach (split) {
    $files{$_}->{op} = $op[$state-1];
  }
}

$htag = $tag ? $tag : "HEAD";

while (<STDIN>) {
  $logmsg .= $_;
}


### Check if we want to waste time at this whole thing at all


# The following is an elinks-specific hack, as we don't want to send
# notifications about this file being changed :).

exit if ($files[0]->{name} and $files[0]->{name} eq "ChangeLog");



### Sync between the multiple instances potentially being ran simultaneously

my $sum; # _VERY_ simple hash of the log message. It is really weak, but I'm
         # lazy and it's really sorta exceptional to even get more commits
         # running simultaneously anyway.
map { $sum += ord $_ } split (//, $logmsg);

my $syncfile; # Name of the file used for syncing
$syncfile = "/tmp/cvslog.$project.$module.$sum";


if (-f $syncfile and -w $syncfile) {
  # The synchronization file for this file already exists, so we are not the
  # first ones. So let's just dump what we know and exit.

  open (FF, ">>$syncfile") or die "aieee... can't log, can't log! $syncfile blocked!";

  {
    my @t;
    foreach (@files) {
      push (@t, join(',', $_->{name}, $_->{oldrev}, $_->{newrev}, $_->{op}));
    }

    print FF join("\t", $dirs[0]->{name}, $dirs[0]->{type}, @t) . "\n";
  }

  close (FF);
  exit;

} else {
  # We are the first one! Thus, we'll fork, exit the original instance, and
  # wait a bit with the new one. Then we'll grab what the others collected and
  # go on.

  # We don't need to care about permissions since all the instances of the one
  # commit will obviously live as the same user.

  # system("touch") in a different way
  open (FF, ">>$syncfile") or die "aieee... can't log, can't log! $syncfile blocked!";
  close (FF);

  exit if (fork);
  sleep ($sync_delay);

  open (FF, $syncfile);
  my ($i) = 1;
  while (<FF>) {
    chomp;

    my ($zdir, $ztype, @zfiles) = split ("\t");
    $dirs[$i]->{name} = $zdir;
    $dirs[$i]->{type} = $ztype;

    foreach (@zfiles) {
      my ($commit);
      ($commit->{name}, $commit->{oldrev}, $commit->{newrev}, $commit->{op}) = split (',');
      push (@{$dirs[$i]->{commits}}, $commit);
    }

    $i++;
  }
  close (FF);

  unlink ($syncfile);
}



### Send the mail


# Open our mail program

open (MAIL, '| /usr/lib/sendmail -t -oi -oem')
    or die "$0: cannot fork sendmail: $!\n";


# Fill in date

my ($date);
$date = scalar gmtime;




# Compose the mail

# TODO: Use CVSROOT/users to determine the committer's realname and email and
# add it to the reply-to / mail-followup-to list. --pasky

my ($VERSION) = '$Revision: 1.1 $' =~ / (\d+\.\d+) /;


# Subject files, references and 
# future "files affected part" (List the files being changed, plus the cvsweb URLs)


my $files_affected_part="";
my $subj_file_list="";
my $references="";
my $messageid="";
my $cvsweb_part="";
for (my $i = 0; $i < @dirs; $i++) {
  my $dirs = $dirs[$i];
  my $dir = $dirs->{name};
  my $subdir = $dir;
  $subdir =~ s!^[^/]*/?!!;
  $subdir .= '/' if $subdir ne '';

  $files_affected_part .= "\n$dir:\n   ";

  if ($dirs[$i]->{type} eq 'directory') {
    $files_affected_part .= "New directory\n";

  } else {
    my $commits = $dirs->{commits};

    for (my $j = 0; $j < @$commits; $j++) {
      my $ref;
      my $commit = $commits->[$j];
      my ($name, $oldrev, $newrev, $op) = ("NAME","NONE","NONE",-1);
      ($name, $oldrev, $newrev, $op) = ($commit->{name}, $commit->{oldrev}, $commit->{newrev}, $commit->{op});
      $subj_file_list .= "$subdir$name";
      $subj_file_list .= " (NEW)" if ($op eq 'add');
      $subj_file_list .= " (REMOVED)" if ($op eq 'remove');
      $subj_file_list .= ", ";
      $files_affected_part .= "$name ($oldrev -> $newrev) ";
      $files_affected_part .= " (NEW)" if ($op eq 'add');
      $files_affected_part .= " (REMOVED)" if ($op eq 'remove');
      $files_affected_part .= " (?! contact devils)" if ($op eq '?');
      $files_affected_part .= ", " if $j < @$commits-1;
#      $files_affected_part .= "\n";
      $cvsweb_part .= "    $cvsweb_url/$dir/$name?r1=$oldrev&r2=$newrev&f=h\n"
        if defined $cvsweb_url and $op ne 'add' and $op ne 'remove';
      $ref=$dir."_".$name.".".$newrev;
      $ref =~ s/[^a-zA-Z0-9.]/_/g;
      $messageid="<".$ref.$messageid_email.">";
      $ref=$dir."_".$name.".".$oldrev;
      $ref =~ s/[^a-zA-Z0-9.]/_/g;
      $references.=" <". $ref. $messageid_email .">";
    }
  }
}

# Fill in subj and possibly cut it

my ($subj);
$subj_file_list =~ s/, $/ /;
$subj = "Subject: $module".($tag?" ($tag)":"").": ".($subj_files?$subj_file_list:"").$logmsg;
$subj =~ s/\n/ /g; $subj =~ s/ *$//;
$subj = substr($subj, 0, $subj_maxlength-3) . '...' if (length($subj) > $subj_maxlength);
my $keywords="DEV-$user, MOD-$module, TAG-".($tag?$tag:"HEAD").", ". $subj_file_list;

print MAIL <<EOM;
From: $from_email
To: $dest_email
Reply-To: $reply_email
Mail-Followup-To: $reply_email
X-CVS: $user\@$project:$module
X-CVS-Module: $module
User-Agent: cvslog.pl/$VERSION
$subj
Message-ID: $messageid
MIME-Version: 1.0
Content-Transfer-Encoding: $mail_contenttransen
Content-Type: $mail_contenttype
Keywords: $keywords
EOM

if ($withthreading_email) {
  print MAIL <<EOM;
References: $references
In-Reply-To: $references
EOM
}

my $msgheader = sprintf ("%-36s %s\n%-36s %s", "Author: $user", "Date: $date GMT","Module: $module"," Tag: $htag");
$logmsg =~ s/\n+/\n/gm;

print MAIL <<EOM;

$msgheader
EOM

print MAIL <<EOM;
---- Log message:
$logmsg
EOM

print MAIL <<EOM;
---- Files affected:$files_affected_part
EOM


goto end_diff unless $show_diff or $show_diffstat;

print MAIL <<EOM;

---- Diffs:
EOM

# And now the diffs!

my @diff;

for (my $i = 0; $i < @dirs; $i++) {
  my $dirs = $dirs[$i];
  my $dir = $dirs->{name};

  next if ($dirs[$i]->{type} ne 'checkin');

  my $commits = $dirs->{commits};

  for (my $j = 0; $j < @$commits; $j++) {
    my $commit = $commits->[$j];

    my $oldrev = $commit->{oldrev}; $oldrev = '0.0' if ($oldrev eq 'NONE');
    my $newrev = $commit->{newrev};
    my $name = $commit->{name};

    # Do not print diffs of removed files. Too boring.
    next if ($newrev eq 'NONE');

    my @difflines;

    my $pid = open (CVS, '-|');
    if (!defined $pid) {
      die "$0: can't fork cvs: $!\n";
    } elsif ($pid == 0) {
      open (STDERR, '>&STDOUT') or die "$0: can't reopen stderr: $!\n";
      exec ($CVS, '-fnQq', '-d', $repository, 'rdiff', '-kk', '-u',
            '-r', $oldrev, '-r', $newrev, $dir.'/'.$name)
		or die "$0: can't fork cvs: $!\n";
    } else {
      @difflines = <CVS>;
      close CVS;
      if (@difflines > 1 and $difflines[1] =~ /failed to read diff file header/) {
        @difflines = ($difflines[0], "<<Binary file>>\n");
      }
    }

    push (@diff, "\n" . ("=" x 64) . "\n");
    push (@diff, @difflines);
  }
}

push (@diff, ("=" x 64) . "\n");

# Diffstat

my $dstmp = '/tmp/cvslog.diffstat.'.$$;
if ($diffstat and $show_diffstat and open (DSTMP, '>' . $dstmp)) {
  print DSTMP @diff;
  close DSTMP;
  my $pid = open (DIFFSTAT, '-|');
  if (!defined $pid) {
    die "$0: can't fork diffstat: $!\n";
  } elsif ($pid == 0) {
    open (STDERR, '>&STDOUT') or die "$0: can't reopen stderr: $!\n";
    exec (split(/\s+/, $diffstat), $dstmp)
      	or die "$0: can't fork diffstat: $!\n";
  } else {
    my @diffstat = <DIFFSTAT>;
    close DIFFSTAT;
    print MAIL @diffstat;
    print MAIL "\n";
  }
  unlink ($dstmp);
}

goto end_diff unless $show_diff;

if (@diff > $max_diff_lines) {
  @diff = splice(@diff, 0, $max_diff_lines);
  print MAIL @diff;
  print MAIL "<<Diff was trimmed, longer than $max_diff_lines lines>>\n";
} else {
  print MAIL @diff;
}

end_diff:

if ($cvsweb_part ne ""){
  print MAIL <<EOM;

---- CVS-web:
$cvsweb_part
EOM
}

print MAIL <<EOM;
---- INFO:
Nie chcesz dostawac takich maili ? Powiedz mi o tym: wilk\@dickensa.waw.pl
EOM


# Send it to the world

close MAIL;
die "$0: sendmail exit status " . $? >> 8 . "\n" unless ($? == 0);


# vi: set sw=2:

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us