head	1.2;
access;
symbols;
locks; strict;
comment	@# @;


1.2
date	2004.09.05.04.26.40;	author kuriyama;	state Exp;
branches;
next	1.1;

1.1
date	2004.03.30.00.18.16;	author kuriyama;	state Exp;
branches;
next	;


desc
@@


1.2
log
@Add failure checks of opening files and unlink.
@
text
@#!/usr/bin/perl -w
#
# $FreeBSD$

use strict;
use lib $ENV{CVSROOT};
use CVSROOT::cfg;
my $CVSROOT = $ENV{'CVSROOT'} || die "Can't determine \$CVSROOT!";
my $debug = $cfg::DEBUG;

my $BASE_FN       = "$cfg::TMPDIR/$cfg::FILE_PREFIX";
my $FILES_FILE = "$BASE_FN.files";

print "$$: pgrp: ", getpgrp(), "\n" if ($debug);
print "$$: committer: ", $cfg::COMMITTER, "\n" if ($debug);

foreach (@@ARGV) {
	print "$$: args: $_\n" if ($debug);
}

my $logfile = $ARGV[0];
my $log = "";
if (-r $logfile) {
	open(H, "<$logfile") or die;
	foreach (<H>) {
		print "$$: log: $_" if ($debug);
		$log .= $_;
	}
	close(H);
} else {
	print "$$: Cannot open logfile ($logfile)\n";
}

if (-r $FILES_FILE and open(FILES, $FILES_FILE)) {
	my %tag;
	while (<FILES>) {
		chomp;
		my ($tag, $file) = split(/\t/, $_, 2);
		print "$$: files_file: tag=$tag, file=$file\n" if ($debug);
		push @@{$tag{$tag}}, $file;
	}
	close(FILES);

	if (check_approvers($log, %tag)) {
		if (unlink($FILES_FILE) != 1) {
			print "$$: unlink failed ($!).\n";
		}
		exit 1;
	}
} else {
	print "$$: Cannot open \$FILES_FILE ($FILES_FILE)\n";
}
exit 0;

# ============================================================
sub read_approvers {
	my @@Approvers;
	my $approvers = "$CVSROOT/CVSROOT/approvers";
	if (-r $approvers) {
		print "$$: Read $approvers\n" if ($debug);
		if (not open(APP, "<$approvers")) {
			print "$$: Cannot open approvers file ($!).\n";
			return;
		}
		while (<APP>) {
			chomp;
			next if (/^#/);
			my ($mod, $tag, $rev) = split(/\t+/, $_, 3);
			if ($rev) {
				push @@Approvers, { mod => $mod,
						   tag => $tag,
						   app => $rev };
			}
		}
		close(APP);
	} else {
		print "$$: Cannot open approvers file.\n";
	}
	@@Approvers;
}

sub check_approvers {
	my ($log, %files) = @@_;
	my %tag;
	my @@Approvers = &read_approvers();
	foreach my $t (keys %files) {
		foreach (@@{$files{$t}}) {
			$tag{$t}->{$_}++;
		}
	}
	foreach my $r (@@Approvers) {
		printf "$$: checking: %s, %s, %s\n",
			$r->{mod}, $r->{tag}, $r->{app} if ($debug);
		foreach my $tag (sort keys %tag) {
			foreach my $file (sort keys %{$tag{$tag}}) {
				return 1 if (check_one($log, $r, $tag, $file));
			}
		}
	}
	0;
}

sub check_one {
	my ($log, $r, $tag, $file) = @@_;
	if ($file !~ m|$r->{mod}|) {
		printf "$$: file not matched (%s, %s)\n", $file, $r->{mod}
			if ($debug);
		return 0;
	}
	printf "$$: file matched (%s, %s)\n", $file, $r->{mod} if ($debug);
	if ($tag !~ m|$r->{tag}|) {
		printf "$$: tag not matched (%s, %s)\n",
			$tag, $r->{tag} if ($debug);
		return 0;
	}
	printf "$$: tag matched (%s, %s)\n", $tag, $r->{tag} if ($debug);
	if ($log =~ m|Approved by:[\t ]*$r->{app}|s) {
		printf "$$: log matched (%s, %s)\n", $log, $r->{app}
			if ($debug);
		return 0;
	}
	printf "$$: log not matched (%s, %s)\n", $log, $r->{app} if ($debug);
	printf "**** You need \"Approved by: %s\" line in your log entry.\n",
		$r->{app};
	return 1;
}
@


1.1
log
@Check whether this commit is approved by appropriate party or not in
specific modules/branches.

We hope this check reduces accidental commits to reserved branches
(such as security branches).

Reviewed by:	core
@
text
@d30 2
d45 3
a47 1
		unlink $FILES_FILE;
d50 2
d61 4
a64 1
		open(APP, "<$approvers") or return;
d76 2
@
