git commit 时自动对所有 php 文件执行语法错误检查

使用 Shell 编写 hooks 下的 pre-commit 钩子,实现在 git commit 时检查所有的 .php 文件(忽略所有删除状态的文件)是否存在语法错误,如果存在错误,则终止提交,并输出相关错误信息。

#!/bin/bash

# @auth 后三排
# @site https://housanpai.com

# 错误消息内容
IS_ERROR_MESSAGE=()

while read st file
do

    # 文件状态为 D 时跳出本次循环
    if [ 'D' == "$st" ]
    then
        echo $file
        continue
    fi

    # 文件末为不是 .php 时输出文件,并跳出本次循环
    if [[ ! "$file"  =~ (\.php$) ]]
    then
        echo $file
        continue
    fi

    PHP_LINT=`php -l $file`

    # 本文件不存在语法错误,输出结果,并跳出本次循环
    if [ 0 -eq $? ]
    then
        echo $PHP_LINT
        continue
    fi

    # 统计错误消息内容的数据个数
    ERROR_COUNT=${#IS_ERROR_MESSAGE[@]}

    # 将错误的存放到数组里面
    IS_ERROR_MESSAGE[${ERROR_COUNT}]=$PHP_LINT

done <<EOF
`git diff --cached --name-status`
EOF

if [ -n "${IS_ERROR_MESSAGE}" ]
then

    # 循环输出错误消息,并且指定文字颜色为红色
    for ((i=0;i<${#IS_ERROR_MESSAGE[@]};i++))
    do
        echo -e "\033[31m ${IS_ERROR_MESSAGE[$i]} \033[0m"
    done

    exit 1

fi

exit 0

GitHub 源码地址

知识共享许可协议
本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。
随喜
支付宝随喜
微信随喜

a'

“Look, I’m not going to judge whether or not she thought it was sufficient,” said Biden. “I said privately what I said publicly: I’m sorry she was treated the way she was treated, I wish we could have figured out a better way to get this thing done, I did everything in my power to do what I thought was within the rules to be able to stop things.” cheap jerseys http://www.cheapjerseysfromchina.us/

Federal health officials say the number of confirmed measles patients in the United States — 626 in 22 states — is on pace to surpass 2014's record 667 cases. One in 1,000 children who contract the disease will die, according to the CDC. Cowboys Jerseys http://www.dallascowboysjerseyscheap.us/

Raja's elder brother,Jordan who continues to work as a police officer,Jordan apologized to the Jones family and complained that Raja had been unfairly treated for what he called "a bad decision." Travis Scott Jordan 1 http://www.travisscottjordan1.us.com/

Buy Kamagra Without Prescription Viagra Sin Receta En Sevilla Clomid Et Bb <a href=http://viaacost.com>buy viagra</a> Propecia Recuperarpelo Cytotec Apres Ivg

Viagra Toronto Store Viagra Hollande Amoxil Powered By Phpbb <a href=http://cthosts.net>buy generic cialis online</a> Cialis 20 Mg Vendita

Cheap Linisopril Comprar Cialis Espana Sin Receta [url=http://lowpricecial.com]cialis 40 mg[/url] Buy Doxycycline In Canada Online Drugstore Canada