在git hooks中添加grunt和utf8编码检测脚本

使用grunt可以配置一些代码检查的工具,但需要命令行方式运行,因此如果能够在git中提交代码时自动运行会方便很多。那么git提供了hooks,位于项目根目录下.git/hooks文件夹下,在hooks中有多个脚本可供选择,那么用于提交,我们只需要编写pre-commit脚本置于此处,便可在每次提交代码时自动运行。基于这个方便的脚本,那么我们也可以增加一些其他功能,比如代码文件编码检查或转换。

检测utf8编码和自动运行grunt的脚本如下:

#!/bin/sh

grunt --force

git ls-files -z --  |
xargs -0 sh -c '

    e=""
    for f; do
        if ! git show :"$f" |
             iconv -f UTF-8 -t UTF-8 >/dev/null 2>&1; then
            e=1
            echo "Not UTF-8: $f"
            #exit 255 # to abort after first non-UTF-8 file
        fi
    done
    test -z "$e"

' -

*配置为需要检测的文件。

你可能感兴趣的:(在git hooks中添加grunt和utf8编码检测脚本)