美文网首页
在git hooks中添加grunt和utf8编码检测脚本

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

作者: 驳斑 | 来源:发表于2016-11-22 13:40 被阅读0次

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

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

#!/bin/sh

grunt --force

git ls-files -z -- <files> |
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"

' -

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

相关文章

网友评论

      本文标题:在git hooks中添加grunt和utf8编码检测脚本

      本文链接:https://www.haomeiwen.com/subject/yxbppttx.html