#! /usr/bin/perl -w
#
# $FreeBSD$
#
# This hack is to sanitise the results of what the user may have
# "done" while editing the commit log message.. :-)   Peter Wemm.
#
# Note: this uses an enhancement to cvs's verifymsg functionality.
# Normally, the check is advisory only, the FreeBSD version reads
# back the file after the verifymsg file so that this script can
# make changes.
#

use strict;
use lib $ENV{CVSROOT};
use CVSROOT::cfg;


#############################################################
#
# Main Body
#
############################################################

my $filename = shift;
die "Usage: logcheck filename\n" unless $filename;




# Read the log file in, stripping 'CVS:' lines and removing trailing
# white spaces.
open IN, "< $filename" or
    die "logcheck: Cannot open for reading: $filename: $!\n";
my @log_in = map { s/^(.*?)\s*$/$1/; $1 } grep { !/^CVS:/ } <IN>;
close IN;

# Remove duplicate blank lines.
my $i = 0;
while ($i < scalar(@log_in) - 1) {
	if ($log_in[$i] eq "" && $log_in[$i + 1] eq "") {
		splice(@log_in, $i, 1);
		next;
        }
        ++$i;
}

# Remove leading and trailing blank lines.  (There will be at most
# one because of the duplicate removal above).
shift @log_in if $log_in[0] eq "";
pop @log_in if $log_in[-1] eq "";

# Scan through the commit message looking for templated headers
# as defined in the configuration file, and rcstemplate.
# Assume that these only exist in the last paragraph.
# Filter out blank entries, and type check if necessary.
my $j = $#log_in;	# The index of the last entry in the commit msg.
my $error = 0;
while ($j >= 0) {
	my $logline = $log_in[$j];

	--$j;

	# Hitting a blank line means that we've seen all of the last paragraph.
	last if $logline eq "";

	unless ($logline =~ /^(.*?):\s*(.*)$/) {
		### XXX
		# We're here because we saw a line that didn't match
		# a template header (no ':').  This could be a continuation
		# line from the previous header, or the log message proper.
		# We don't know, so run the risk of checking the last paragraph
		# of the log message for headers.
		next;
	}

	my $header = $1;
	my $value = $2;
	my $pattern = $cfg::TEMPLATE_HEADERS{$header};

	# Ignore unrecognised headers.
	unless (defined($pattern)) {
	### 	print "Warning: unknown template header: $header\n";
		next;
	}

	# Filter out the template header if it's blank.
	if ($value eq "") {
		splice(@log_in, $j + 1, 1);
		next;
	}

	# Type check the header
	unless ($value =~ /^$pattern$/) {
		print "Error: $header: should match '$pattern'.\n";
		++$error;
		next;
	}
}


# Make sure that there is some content in the log message.
# XXX Note that logcheck isn't evoked if the log message is
# completely empty.  This is a bug in cvs.
my $log = "@log_in";
die "Log message contains no content!\n" if $log =~ /^\s*$/;


# Write the modified log file back out.
my $tmpfile = $filename . "tmp";
open OUT, "> $tmpfile" or
    die "logcheck: Cannot open for writing: $tmpfile: $!\n";
print OUT map { "$_\n" } @log_in;
close OUT;


# Stop the commit if there was a problem with the template headers.
if ($error) {
	print "There were $error errors in the template headers.\n";
	print "Please fix the log message and commit again.\n";
	print "A copy of your log message was saved in $tmpfile.\n";
	exit 1;
}



# Nuke likely editor backups.
unlink "$filename.~";
unlink "$filename.bak";


# Overwrite the log message with our sanitised one.  (See the comment
# block at the top of this script for an explaination of why.)
rename($tmpfile, $filename) or
    die "logcheck: Could not rename $tmpfile to $filename: $!";

exit 0;
17.19'>dependabot/npm_and_yarn/devel/electron6/files/lodash-4.17.19</option>
<option value='dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2'>dependabot/npm_and_yarn/devel/electron6/files/lodash.merge-4.6.2</option>
<option value='dependabot/npm_and_yarn/devel/electron6/files/node-fetch-2.6.1'>dependabot/npm_and_yarn/devel/electron6/files/node-fetch-2.6.1</option>
<option value='dependabot/npm_and_yarn/devel/electron6/files/serve-10.1.2'>dependabot/npm_and_yarn/devel/electron6/files/serve-10.1.2</option>
<option value='gnome-3.22'>gnome-3.22</option>
<option value='gnome-3.24'>gnome-3.24</option>
<option value='gnome-3.26'>gnome-3.26</option>
<option value='gnome-3.28' selected='selected'>gnome-3.28</option>
<option value='gnome-3.32'>gnome-3.32</option>
<option value='gnome-3.36'>gnome-3.36</option>
<option value='gstreamer'>gstreamer</option>
<option value='gstreamer-1.16'>gstreamer-1.16</option>
<option value='gstreamer0.10-removal'>gstreamer0.10-removal</option>
<option value='main'>main</option>
<option value='master'>master</option>
<option value='mate-1.16'>mate-1.16</option>
<option value='mate-1.18'>mate-1.18</option>
<option value='mate-1.20'>mate-1.20</option>
<option value='mate-1.22'>mate-1.22</option>
</select> <input type='submit' value='switch'/></form></td></tr>
<tr><td class='sub'>FreeBSD GNOME current development ports (https://github.com/freebsd/freebsd-ports-gnome)</td><td class='sub right'></td></tr></table>
<table class='tabs'><tr><td>
<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/about/?h=gnome-3.28'>about</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/?h=gnome-3.28'>summary</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/refs/?h=gnome-3.28&amp;id=1bd10d164226c81de3cd6c05d951279736f69ec2'>refs</a><a class='active' href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/www/zope-validation?h=gnome-3.28'>log</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/tree/www/zope-validation?h=gnome-3.28&amp;id=1bd10d164226c81de3cd6c05d951279736f69ec2'>tree</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/www/zope-validation?h=gnome-3.28&amp;id=1bd10d164226c81de3cd6c05d951279736f69ec2'>commit</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/diff/www/zope-validation?h=gnome-3.28&amp;id=1bd10d164226c81de3cd6c05d951279736f69ec2'>diff</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/stats/www/zope-validation?h=gnome-3.28'>stats</a></td><td class='form'><form class='right' method='get' action='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/www/zope-validation'>
<input type='hidden' name='h' value='gnome-3.28'/><input type='hidden' name='id' value='1bd10d164226c81de3cd6c05d951279736f69ec2'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/?h=gnome-3.28&amp;id=1bd10d164226c81de3cd6c05d951279736f69ec2'>root</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/www?h=gnome-3.28&amp;id=1bd10d164226c81de3cd6c05d951279736f69ec2'>www</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/www/zope-validation?h=gnome-3.28&amp;id=1bd10d164226c81de3cd6c05d951279736f69ec2'>zope-validation</a></div><div class='content'><table class='list nowrap'><tr class='nohover'><th></th><th class='left'>Commit message (<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/www/zope-validation?h=gnome-3.28&amp;id=1bd10d164226c81de3cd6c05d951279736f69ec2&amp;showmsg=1'>Expand</a>)</th><th class='left'>Author</th><th class='left'>Age</th><th class='left'>Files</th><th class='left'>Lines</th></tr>