r2 - 16 Apr 2009 - 10:14:28 - Main.yfangYou are here: YWiki >  MyTech Web > CVS中对提交内容检查的配置方法

CVS中对提交内容检查的配置方法

说明

  • 欢迎转载,请注明作者:杨方(yangfang@fudan.edu.cn)和原文URL,欢迎感兴趣的朋友一起研究技术
  • 测试环境:CentOS 5.3 + CVS 1.11.22 (server),客户端是TortoiseCVS 1.10.10,$ENV{CVSROOT}=/var/cvsroot

基本概念

  • CVS(Concurrent Versions System)就不多作介绍了,很著名的老牌配置管理工具,目前最流行的可能是svn或者git吧,不过因为各种原因,CVS还是占有不少份额。刚好最近需要控制CVS commit的格式,比如长度,内容什么的,就搭了一个简单的环境用来测试。环境搭建的过程比较简单,外面也有相当多中文英文的文档讲这个,我就不提了。其实这件事情我很久以前就在SVN下做过了,用pre-commit的hook做一下就行了,这回顺便看一下cvs的做法,逻辑上应该非常相似。
  • 查看了一些资料,CVS对于这样的需求是有专门的解决方案的,其实也很简单实现,就是自己一直缺乏概念,导致配置中碰到了很多问题。足够聪明的朋友直接看CVS手册就可以了。这里介绍一些简单而重要的概念。
    • 1. $ENV{CVSROOT}下存放了你的各个repo
root@localhost[0]cvsroot00:24:19# ls
CVSROOT  testproj  yfang testrepo

    • 2. $ENV{CVSROOT}下有一个特殊的目录叫做CVSROOT,这个目录下有很多文件,这些文件都是用来控制CVS服务器的行为的。
root@localhost[0]CVSROOT00:18:35# ls -a
.               commitinfo,v   editinfo    .#loginfo  notify,v   taginfo,v
..              config         .#editinfo  loginfo,v  passwd     val-tags
checkoutlist    .#config       editinfo,v  modules    rcsinfo    verifymsg
.#checkoutlist  config,v       Emptydir    .#modules  .#rcsinfo  .#verifymsg
checkoutlist,v  cvswrappers    file.txt,v  modules,v  rcsinfo,v  verifymsg,v
commitinfo      .#cvswrappers  history     notify     taginfo
.#commitinfo    cvswrappers,v  loginfo     .#notify   .#taginfo
    • 3. 注意到这里的文件名大多是3个一套的,比如loginfo, .#loginfo, loginfo,v,其中,第一个文件是核心配置文件,通常默认是readonly的,就是怕你不小心改错了,导致服务器工作异常。第二个.#开头的文件我不是很清楚,我的发现是,这个文件会保存最后一次相应文件变更前的版本。loginfo,v就和普通的cvs文件没什么区别了,里面存放了这个文件的版本信息和具体内容。
    • 4. 接下来介绍几个和CVS commit行为相关的支持文件,我们最终解决问题也就是靠这些

# 这里有三个和commit行为相关的支持文件,我现在就他们的功能和使用方法简单介绍一下:
#commitinfo
这个文件是用来检查提交文件所在的仓库和文件名列表。如果返回的是非零值,提交将失败。
#例子:
在commitinfo文件添加一行,用来针对testrepo这个项目进行检查,具体检查内容由自定义的脚本实现
^testrepo   /path/to/cvs_precommit_logcheck.pl
# 这里脚本后是不需要添加参数的,程序会自动获得参数
# 如果你提交的是testproj仓库中的 file1和file2,
# 那么你自动获得的参数列表就是 /var/cvsroot/testproj file1 file2
# 接下来的事情就是你如何针对这些参数写规则了,什么情况OK,什么情况不行。
# 脚本返回非零值,这次提交就会失败。否则提交成功。
# 说明一下,所有脚本中的输出都会在客户端的console上显示,
# 所以你知道该怎么根据你的脚本类型,pl,py,sh来写警告信息了吧。

#verifymsg
这个用来检验提交时候填写的comment/log内容和格式,
比如你可以检查是否有变更描述或者严格要求comment长度大于10个字等等。
文档上说偶尔与 rcsinfo 文件(用来指定日志模板,受到很多限制,我最后根本就没用它)组合在一起使用。
# 例子:
在verifymsg文件添加一行,用来针对testrepo这个项目进行检查,具体检查内容由自定义的脚本实现
^testrepo   /path/to/cvs_precommit_logcheck.pl
# 说明一下:一些文档提到要给/path/to/cvs_precommit_logcheck.pl传参数,
# 实验证明,至少现在的版本肯定是不需要的
# cvs_precommit_logcheck.pl会自动获得一个参数,
# 这个参数是一个存放当前comment/log 信息的临时文件,通常是/tmp/xxxxx
# 所以只要用脚本读取文件内容然后处理就行了。
# 警告信息的写法和commitinfo类似,不赘述。
# 脚本返回非零值,这次提交就会失败。否则提交成功。

#loginfo
在提交之后被调用。它接受日志消息及其他额外信息,并能将日志消息存储到一个文件,或者发邮件给特定的人
#   s = file name
#   V = old version number (pre-checkin)
#   v = new version number (post-checkin)
#   t = tag or branch name
#
# For example:
#DEFAULT (echo ""; id; echo %s; date; cat) >> $CVSROOT/CVSROOT/commitlog
# or
#DEFAULT (echo ""; id; echo %{sVv}; date; cat) >> $CVSROOT/CVSROOT/commitlog
# or
#DEFAULT (echo ""; id; echo %s; date; cat) | mail -s "commit Log" yangfang@fudan.edu.cn
    • 5. 以上控制文件中都支持最简单得正则写法,支持^ . * $,除此以外有些还支持DEFAULT和ALL,分别表示"如果以上均不匹配则使用"和"所有都要经过这个检查"

实现过程

  • 言归正传,现在来看看我们的原始需求如何实现吧。因为是对comment,也就是log进行格式判断,这里我们只需要修改verifymsg就行了。
  • 1. 如何修改默认的verifymsg呢?
    • 方案一:强行chmod u+w verifymsg,然后编辑保存。
    • 方案二:把CVSROOT当作一个普通的仓库checkout出来,然后编辑里面的verifymsg文件,保存提交。CVS服务器会神奇的帮你更新服务器上的verifymsg文件,这可是一般仓库绝对不会有的特性哦。
    • 如果服务器管理员只有你一个,你选用任何一个都无所谓。如果你想在平时使用的时候混用两个方案,那么请尽量放弃方案一,因为CVS服务器的特性是认为提交上来的才是可信的,方案一的行为更多被认为是hack行为。
  • 2. 内容方面,按照我的要求,只要在vertifymsg中加入一句话就行了,特别强调一下,vertifymsg脚本重视不支持ALL的
root@localhost[0]CVSROOT00:47:10# cat verifymsg
# The "verifymsg" file is used to allow verification of logging
# information.  It works best when a template (as specified in the
# rcsinfo file) is provided for the logging procedure.  Given a
# template with locations for, a bug-id number, a list of people who
# reviewed the code before it can be checked in, and an external
# process to catalog the differences that were code reviewed, the
# following test can be applied to the code:
#
#   Making sure that the entered bug-id number is correct.
#   Validating that the code that was reviewed is indeed the code being
#       checked in (using the bug-id number or a seperate review
#       number to identify this particular code set.).
#
# If any of the above test failed, then the commit would be aborted.
#
# Actions such as mailing a copy of the report to each reviewer are
# better handled by an entry in the loginfo file.
#
# One thing that should be noted is the the ALL keyword is not
# supported.  There can be only one entry that matches a given
# repository.
^testrepo   /path/to/cvs_precommit_logcheck.pl
#^.* /path/to/cvs_precommit_logcheck.pl  #如果想对所有仓库生效,使用这行
  • 3. 编写cvs_precommit_logcheck.pl,放到一个好位置,我是放到/usr/local/bin/下的
#!/usr/bin/perl

# cvs_precommit_logcheck.pl
# Author: Yang Fang
# Email: yangfang@fudan.edu.cn
# Date: Apr. 15th 2009
# Description: This script will help checking the commit log format.
# Usage: Append a line to $ENV{CVSROOT}/CVSROOT/verifymsg, like
#        ^testrepo  PATH/TO/cvs_precommit_logcheck.pl
#        This sample will affect all commit in 'testrepo'
#        ^.*    PATH/TO/cvs_precommit_logcheck.pl
#        This sample will affect all commit in every repo


sub read_file
{
        my ( $f ) = @_;
        open F, "< $f" or die "Can't open $f : $!";
        my @f = <F>;
        close F;
        return wantarray ? @f : join('',@f);
}

my $log = read_file($ARGV[0]);

# trim log
$log =~ s/^\s*//;
$log =~ s/\s*$//;

# check log format
$ok = $log =~ /^Bug\s+number\s*:.*\n
Modifier\s*:.*\n
Reviewer\s*:.*\n
Description\s*:.{10,}
/x;

unless($ok) {
        $msg = "Invalid Format,comment must in the following format:\n".
        "Bug number:\n".
        "Modifier:\n".
        "Reviewer:\n".
        "Description: more than 10 characters are required\n";
        die($msg);
}

exit(0);

参考资料

Edit | WYSIWYG | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r2 < r1 | More topic actions
 
Powered by YWiki
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding YWiki? Send feedback