Git中代码预检查
版权声明:本文系作者原创。未经许可,不得转载。
在git中,有一个脚本pre-commit可以在git comit之前,进行一些操作,比如对代码进行预检查等。
本脚本,利用ubuntu自带的cppcheck对代码进行提交前预检查。检查有误,则不能提交。
使用方式如下:
1、安装cppcheck
sudo apt-get install cppcheck
2、将附件复制到项目中.git/hooks目录下并修改权限。 以cmos-qmlplugin-multimedia-photos为例,执行:
cp cmos-pre-commit 目录/cmos-qmlplugin-multimedia-photos/.git/hooks/
chmod 755 目录/cmos-qmlplugin-multimedia-photos/.git/hooks/cmos-pre-commit
3、将git自带的pre-commit.sample复制一份,并添加一行代码:
cp目录/cmos-qmlplugin-multimedia-photos/.git/hooks/pre-commit.sample 目录/cmos-qmlplugin-multimedia-photos/.git/hooks/pre-commit
echo "./cmos-pre-commit" >>目录/cmos-qmlplugin-multimedia-photos/.git/hooks/pre-commit
cmos-pre-commit的源码如下:
#
# This file is part of cmos git
#
# Copyright (C) 2014 Beijing Yuan Xin Technology Co.,Ltd. All rights reserved.
#
# Authors:
# Peng Huaping <penghuaping@syberos.com>
#
# This software, including documentation, is protected by copyright controlled
# by Beijing Yuan Xin Technology Co.,Ltd. All rights are reserved.
#
#!/bin/sh
DATE=/bin/date
GIT=/usr/bin/git
SED=/bin/sed
AWK=/usr/bin/awk
TOUCH=/usr/bin/touch
CPPCHECK=/usr/bin/cppcheck
CAT=/bin/cat
ECHO=/bin/echo
RM=/bin/rm
WC=/usr/bin/wc
current_date=`$DATE +%Y%m%d%H%M%S`
commit_file=/tmp/file_$current_date.tmp
check_file=/tmp/fliter_file_$current_date.tmp
check_log_file=/tmp/precheck_$current_date.log
$TOUCH $commit_file
$GIT status -s | $SED -n '/^[M,A]/p' | $AWK '{print $2}' > $commit_file
$TOUCH $check_file
#只对扩展名为cpp、h、cc、hpp、c进行检查
$SED -n '/\<cpp\>$/p' $commit_file >> $check_file
$SED -n '/\<c\>$/p' $commit_file >> $check_file
$SED -n '/\<cc\>$/p' $commit_file >> $check_file
$SED -n '/\<h\>$/p' $commit_file >> $check_file
$SED -n '/\<hpp\>$/p' $commit_file >> $check_file
check_count=`$CAT $check_file | $WC -l`
if [ $check_count != 0 ]; then
$TOUCH $check_log_file
for file in `$CAT $check_file`
do
$CPPCHECK --enable=style,performance,portability,unusedFunction --quiet --template=gcc $file 2>> $check_log_file
done
precheck_count=`$CAT $check_log_file | $WC -l`
if [ $precheck_count = 0 ]; then
$ECHO "代码检查通过!"
else
$ECHO "代码检查没有通过!"
$ECHO "原因如下:"
$CAT $check_log_file
exit 1
fi
fi
$RM -f $commit_file
$RM -f $check_file
$RM -f $check_log_file
参考资料:
辛巴
it男
256 声望
4 粉丝
推荐阅读
mac下编译go-ethereum
macos: 10.14.4 下载go-ethereum源码,按照文档执行命令编译: $ make all 报错:fatal error: 'stdlib.h' file not found /usr/include 查看xcode是否安装: $ xcode-select --install xcode-select: error: com...
lansheng228赞 2阅读 3.3k
git 常用命令 记录
有错误和改进的地方望留言,谢谢 : )顺便推荐一下,git入门教程Git 教程图解 GitPro Git 2(中文版)Git 使用Git 使用规范流程Git 工作流程Commit message 和 Change log 编写指南全局设置git config --system //...
Luff阅读 3.2k
git/pip/easy_install/apt代理/源设置
在公司内网环境中,访问公网往往需要经过公司的代理,对于浏览器、IDE等开发工具,都提供了设置代理的配置,而git、pip、easy_install等CLI工具,则需要通过命令或配置文件进行代理设置;对于Python、Ubuntu、Mav...
乘着风赞 1阅读 5.5k
低代码开发重要工具:jvs-logic(逻辑引擎)2.1.6版本功能清单
逻辑引擎是一种能够处理逻辑表达式的程序,它能够根据用户输入的表达式计算出表达式的值。在实际应用中,逻辑引擎通常被用于处理规则引擎、决策系统、业务规则配置等领域,具有广泛的应用前景。前面几期我们了解...
软件部长赞 2阅读 419
被吐槽 GitHub仓 库太大,直接 600M 瘦身到 6M,这下舒服了
忙里偷闲学习了点技术写了点demo代码,打算提交到我那 2000Star 的Github仓库上,居然发现有5个Issues,最近的一条日期已经是2022/8/1了,以前我还真没留意过这些,我这人懒得很,本地代码提交成功基本就不管了。
程序员小富赞 2阅读 1.4k评论 1
书写友好的提交信息
其中开头部分表明了本次提交的类型(是添加功能还是修复错误等),范围则描述了修改的影响面,然后是一段简要的描述,更多详细的信息则可以在正文中进行描述。
Kisstar阅读 3k
Coding 官方推出网站服务状态页面啦!
第一部分是系统运行状态。如下图,提示所有系统的运行状态,如 Coding 网站访问状态, Git 服务,QC 服务和 Paas 服务等:第二部分是系统指标。System Metrics 两个图表示的分别是 Coding 官方网站的实时响应时间...
CODING阅读 2.8k
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。