#!/usr/bin/perl
#
# Check that the commit message contains a bug id, with a per-branch 
# policy.
#
# Usage:
#  In commitinfo:  check_for_bugid -l %r/%p %s
#  In verifymsg:   check_for_bugid -c %l
#
# NOTE: To disable this test, set disable to 1
#       To change the names of the branches tested, search for "reject"

my $disable = 0;
if ($disable eq 1) {
    exit 0;
}

my $uid=$ENV{"CVS_PID"};
my $debug = 0;

if (@ARGV[0] eq "-l") {
    $entries = "CVS/Entries";
    open(ENTRIES, $entries) || die("Cannot open $entries.\n");
    my $branch_name = "";
    while(<ENTRIES>) {
	chop;
	next if /^\s*$/;                    # Skip blank lines
	if (m|
	    /                             # 1st slash
	    ([\w.-]*)                     # file name -> $1
	    /                             # 2nd slash
	    .*                            # revision number
	    /                             # 3rd slash
	    .*                            # date and time
	    /                             # 4th slash
	    .*                            # keyword
	    /                             # 5th slash
	    T?                            # 'T' constant
	    (\w*)                         # branch    -> #2
	    |x) {
	    if ($2) {
		$branch_name = $2;
	    }
	}
    }
    print "This commit is using the branch: $branch_name\n" if $debug;
    close(ENTRIES);
    if ($branch_name) {
	system("touch /tmp/brxx.$uid.$branch_name.tmp");
    } else {
	# If we want to restrict the mainline then uncomment the next line:
	system("touch /tmp/brxx.$uid.HEAD.tmp");
    }
} elsif (@ARGV[0] eq "-c") {
    # Check for the required text in the commit message
    $message = @ARGV[1];
    open(MESSAGE, $message) || die("Cannot open $message.\n");
    my $bugid_found = 0;
    while(<MESSAGE>) {
# There is false positive on: bugS: a1234 asd asd, so check that $1 exists too
# TODO leading zeroes are not matched properly
	if (m/Bug[: ]*([\d]*)[\s]*/i) {
	    if ($1) {
		$bugid_found = 1;
		print "Found bug id $1 in the text '$_'\n" if $debug;
	    } else {
		print "Couldn't parse a bug id from the text '$_'\n" if $debug;
	    }
	}
    }
    close(MESSAGE);

    # See if we care that the text is present or not for the current branches
    my $reject = "";
    if (!$bugid_found) {
	if (-e "/tmp/brxx.$uid.FALCON_BRANCH.tmp") {
	    $reject = "FALCON_BRANCH";
	} elsif (-e "/tmp/brxx.$uid.DIAMOND_BRANCH.tmp") {
	    $reject = "DIAMOND_BRANCH";
# TODO this blocks all directories!
#	} elsif (-e "/tmp/brxx.$uid.HEAD.tmp") {
#	    $reject = "ruby";
	}
	
	if ($reject) {
	    print "Error: a bug id is required in commit messages for $reject\n";
	    
	    print " e.g. Bug: 1234 Your commit message goes here.\n";
	    print "(To change this requirement, edit the file 'CVSROOT/require_bugid')\n";
	    # Clean up the files that indicated which branches were
	    # active in this commit.
	    system("rm -f /tmp/brxx.$uid.*.tmp");
	    exit 1;
	}
    }

    # Clean up the files that indicated which branches were active in this 
    # commit. Do this after the final call to commit, or after rejection.
    # This is hard to do in cvs, so use crontab to remove the files each night.
    # system("rm -f /tmp/brxx.$uid.*.tmp");
} else {
    print ("Warning: require_bugid not invoked correctly\n");
}


