docker环境 git 提交前验证php语法

  • 环境

    系统: linux
    php运行环境: docker(本地目录绑定)

  • 添加hook脚本 .git/hooks/pre-commit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 错误消息内容
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
echo $file

## 这里替换为docker镜像内路径/home/wwwroot/project_name
PHP_LINT=`docker exec -i myphp5.5 php -l /home/wwwroot/project_name/$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

net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_fin_timeout = 30