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 
#      
# 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 '/\$/p' $commit_file >> $check_file
$SED -n '/\$/p' $commit_file >> $check_file
$SED -n '/\$/p' $commit_file >> $check_file
$SED -n '/\$/p' $commit_file >> $check_file
$SED -n '/\$/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




    :
  • http://gitbook.liuhui998.com/5_8.html
  • http://cppcheck.sourceforge.net/manual.pdf
  • http://www.douban.com/note/245204752/