Linux之svn强制有注释才能提交

摘要

不少开发员提交修改的时候都不写注释,导致查看历史时很费劲,也不太符合规范。有的公司要求每次提交修改时都写上 bug号或者任务描述,那么如何在工具上防止开发员们不写注释呢?

在hooks目录下添加一个脚本就ok

#!/usr/bin/env python
#-*- encoding: utf-8 -*-
#########################################################################
# File Name: pre-commit.py
# Author: LookBack
# Email: admin#dwhd.org
# Version:
# Created Time: 2015年09月28日 星期一 18时53分18秒
#########################################################################
#SVN提交前检查钩子
#功能:
#	1、强制填写提交注释,内容10字节以上
#	2、强制注释格式为:xxx:xxx
#	3、提交文件检查,过滤不允许提交的文件
#########################################################################

import sys
import os
import re

def main(argv):
	(repos, txn) = argv
	badlist = (".*config\.php$", ".*/php/cache", ".*test", "config\.js$","^.*\.db$")
	message = "".join(os.popen("/usr/bin/svnlook log '%s' -t '%s'" % (repos, txn)).readlines()).strip()
	if len(message) < 10:
		sys.stderr.write("请输入本次提交的修改内容,10字节以上.\n");
		sys.exit(1)
	if message.find(':') < 1:
		sys.stderr.write("请按规范填写注释,格式为:功能名: 修改说明.\n");
		sys.exit(1)

	changelist = os.popen("/usr/bin/svnlook changed '%s' -t '%s'" % (repos, txn)).readlines()
	for line in changelist:
		for pattern in badlist:
			if re.search(pattern, line):
				sys.stderr.write("请不要把 %s 加入版本库.\n" % line[1:].strip());
				sys.exit(1)
	sys.exit(0)

if __name__ == "__main__":
	main(sys.argv[1:])

根据系统自带的pre-commit.teml中的内容弄清楚这段脚本的含义:
The pre-commit hook is invoked before a Subversion txn is
# committed. Subversion runs this hook by invoking a program
# (script, executable, binary, etc.) named 'pre-commit' (for which
# this file is a template), with the following ordered arguments:
#
# [1] REPOS-PATH (the path to this repository)
# [2] TXN-NAME (the name of the txn about to be committed)
#
# [STDIN] LOCK-TOKENS ** the lock tokens are passed via STDIN.
#
# If STDIN contains the line "LOCK-TOKENS:\n" (the "\n" denotes a
# single newline), the lines following it are the lock tokens for
# this commit. The end of the list is marked by a line containing
# only a newline character.

  • 本文由 发表于 2015年9月28日12:17:08
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: