Force a Commit Comment
Even though in most cases it is better to educate users about the proper use of commit comments, in some cases it may make sense to enforce proper comments. The scripts below can serve as a starting point.
All scripts are based on the
verifymsg commit support file. They require an entry like this in CVSROOT/verifymsg :
DEFAULT <executable>
Where <executable> may be one of the following:
<server_local_absolute_path>\verifymsg.exe (for the C++ solution)
</path/to/python> <path/to/verifymsg.py> (for the Python solution
Python
Gabriel Genellina published on 2007-06-27 in the CVSNT support group his Python script verifymsg.py:
#!/usr/bin/env python
"""Enforce some minimum restrictions on commit messages"""
# compatible with any Python version 2.1 and up
import sys
class Error(Exception): pass
class AbnormalError(Error): pass
def process(filename):
# read message from file
try:
f = open(filename, "r")
try:
msg = f.read()
finally:
f.close()
except:
raise AbnormalError, "can't read commit message. %s: %s" % sys.exc_info()[:2]
# ignore leading and trailing whitespace
msg = msg.strip()
# at least two words separated by any whitespace
words = msg.split()
if len(words)<2:
raise Error, "Commit messages must be more meaningful."
# at least 6 chars, not counting whitespace nor linefeeds and such
nonws = reduce(lambda sum,word: sum+len(word), words, 0)
if nonws<6:
raise Error, "Commit messages must be more meaningful."
# 0 means OK
return 0
def main(argv):
try:
if len(argv)<2:
raise AbnormalError, "missing argument: filename"
return process(argv[1])
except Error,e:
print "%s: %s" % (e.__class__.__name__, " ".join(e.args))
return 1
except:
print "%s: %s" % sys.exc_info()[:2]
return 1
if __name__=='__main__':
sys.exit(main(sys.argv))
C++
Jan Prochazka published on 2007-06-25 in the CVSNT support group his C++ program. Compile it to verifymsg.exe:
// verifymsg.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <stdlib.h>
#include <direct.h>
#include <errno.h>
#include <tchar.h>
#include <atlstr.h>
int _tmain(int argc, _TCHAR* argv[])
{
char* buffer;
CString msg, msgOrig="";
FILE* msgFile;
long lSize;
//something is wrong (file name should be here)
if (argc <= 1){
printf("\nSomething ir really wrong, file name is missing.\n\n");
return 2;
}
if (NULL==(msgFile = fopen(argv[1],"rt"))){
printf("\nSomething ir really wrong, can not open message file.\n\n");
return 2;
}
//jump to C
// obtain file size.
fseek(msgFile , 0 , SEEK_END);
lSize = ftell(msgFile);
rewind(msgFile);
// allocate memory to contain the whole file.
buffer = (char*) malloc(lSize);
if (buffer == NULL){
printf("\nSomething ir really wrong, can not allocate message buffer.\n\n");
fclose(msgFile);
return 3;
}
// copy the file into the buffer.
fread(buffer,1,lSize,msgFile);
/*** the whole file is loaded in the buffer. ***/
fclose(msgFile);
//add end of string
buffer[lSize-1] = '\0';
//jump back to C++
msgOrig = msg = buffer;
free(buffer);
msg.Trim();
//at least 1 space in the message
if (msg.Find(" ") == -1){
printf("\nPlease provide a more substantial commit message then \n\n");
printf("'%s'\n\n",msgOrig);
return 1;
}
//at least 6 chaqracters
msg.Remove(' ');
msg.Remove('\t');
msg.Remove('\n');
if (msg.GetLength() < 6){
printf("\nPlease provide a more substantial commit message then \n\n");
printf("'%s'\n\n",msgOrig);
return 1;
}
//OK
return 0;
}

