用百度搜了一遍hg的hook教程,发现真的是太少了。公司目前正要用到这个,正好本人负责,So。
百度是个坑,少有的几篇文章,再加上善于发现的眼睛,发现TortoiseHg的UI操作都会在控制台显示动作命令,结合之才有下面这个hook!
以上,是不是要转google?!
下面内容结合鄙人的这个篇文章[ 使用 PHP_CodeSniffer 检查 代码 是否 符合 编码规范 ],食用起来应该更美味!
1.编写如下sh脚本,并执行 chmod a+x 赋予执行权限:
#!/bin/bash echo -e "\n==========================Starting PHP Syntax Check==========================\n" #在/tmp目录下创建临时目录 TEMP_DIR=`mktemp -dt php_syntax_files.XXXXXX` #临时测试 #TEMP_DIR="/tmp/test/" #HG_NODE="d0a1ccf22a26dfe62ae1db932dcc4972509b8f0b" HG_BIN="/usr/bin/hg" CHECK_CMD="php /home/hg/php_codesniffer/scripts/phpcs --standard=Fenqile --tab-width=4 $TEMP_DIR"、 #输出临时目录 echo "temp dir : "$TEMP_DIR #导出当前至最新 #hg archive -r $HG_NODE:tip -t files /tmp/test #导出当前 #hg archive -r $HG_NODE -t files /tmp/test #全量导出 #hg archive -r tip -t files /tmp/test #导出修改部分的代码 echo $HG_BIN" archive -I \"set:added() or modified()\" -r "$HG_NODE":tip -t files "$TEMP_DIR $HG_BIN archive -I "set:added() or modified()" -r $HG_NODE:tip -t files $TEMP_DIR #检测代码,输出提示 echo "$CHECK_CMD" TEST_SYNTAX=`$CHECK_CMD` echo -e "$TEST_SYNTAX" #删除目录 echo "rm -rf "$TEMP_DIR rm -rf "$TEMP_DIR" if [ "0" == "${TEST_SYNTAX}" ];then echo -e "Through code detection, allowed to push!" exit 0 fi echo -e "\n\nServer detected the code has a problem, please check to submit the push again!" echo -e "\n===============================================================================\n\n" exit 1
2.在代码仓库所在目录下,编辑 [ 代码库/.hg/hgrc ] 文件,添加如下节点:
[hooks] pretxnchangegroup = /home/hg/php_codesniffer/scripts/hg_pretxnchangegroup_hook.sh
3. 在本地添加代码版本,推送代码(hg push),效果如下:
test% hg push % hg push 正在推到 ssh://[email protected]//home/hg/myproject 正在搜索修改 remote: adding changesets remote: adding manifests remote: adding file changes remote: added 3 changesets with 8 changes to 3 files remote: remote: ==========================Starting PHP Syntax Check========================== remote: remote: temp dir : /tmp/php_syntax_files.OxLrCU remote: /usr/bin/hg archive -I "set:added() or modified()" -r d0a1ccf22a26dfe62ae1db932dcc4972509b8f0b:tip -t files /tmp/php_syntax_files.OxLrCU remote: php /home/hg/php_codesniffer/scripts/phpcs --standard=Fenqile --tab-width=4 /tmp/php_syntax_files.OxLrCU remote: remote: FILE: /tmp/php_syntax_files.OxLrCU/test30000.php remote: ------------------------------------------------------------------------- remote: FOUND 1 ERROR AFFECTING 1 LINE remote: ------------------------------------------------------------------------- remote: 7 | ERROR | [x] A closing tag is not permitted at the end of a PHP file remote: ------------------------------------------------------------------------- remote: PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY remote: ------------------------------------------------------------------------- remote: remote: remote: FILE: /tmp/php_syntax_files.OxLrCU/test40000.php remote: ------------------------------------------------------------------------- remote: FOUND 1 ERROR AFFECTING 1 LINE remote: ------------------------------------------------------------------------- remote: 6 | ERROR | [x] A closing tag is not permitted at the end of a PHP file remote: ------------------------------------------------------------------------- remote: PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY remote: ------------------------------------------------------------------------- remote: remote: Time: 7ms; Memory: 2Mb remote: rm -rf /tmp/php_syntax_files.OxLrCU remote: remote: remote: Server detected the code has a problem, please check to submit the push again! remote: remote: =============================================================================== remote: remote: remote: transaction abort! remote: rollback completed remote: abort: pretxnchangegroup hook exited with status 1 [命令返回代码1 Wed Apr 20 12:27:12 2016] test%
到此配置完毕!
ps:
https://www.mercurial-scm.org/wiki/Hook#Hook_return_values
https://selenic.com/hg/help/config
http://pear.php.net/manual/en/package.php.php-codesniffer.svn-pre-commit.php
http://stackoverflow.com/questions/4162384/mercurial-pre-commit-hook
http://www.softwareprojects.com/resources/programming/t-mercurial-hook-php-syntax-check-1976.html
https://www.mercurial-scm.org/wiki/Hook
时间: 2024-11-04 05:34:52