我想在Git中设置一个自定义过滤器,这样js文件就会自动被美化。我试着创建一个预提交钩子,运行jshint和js-美化。这是我创建的脚本:
#!/bin/sh
files=$(git diff --cached --name-only --diff-filter=ACM | grep ".js$")
if [ "$files" = "" ]; then
exit 0
fi
pass=true
echo "Javascript validation with JSHint:"
for file in ${files}; do
result=$(jshint ${file} | egrep "error")
if [ "$result" != "" ]; then
echo "JSHint error: ${file}"
echo "$(jshint ${file})"
pass=false
else
echo "JSHint ok: ${file}"
beautify=$(js-beautify ${file} -r -P --config .beautifyrc)
fi
done
echo "Validation complete"
if ! $pass; then
echo "Aborting commit."
exit 1
else
echo "Commit ok."
exit 0
fi这样做,文件是被提交的,但是js所做的更改并不包括在提交中,而且文件更改是挂起的(所以我需要进行第二次提交)。我读过关于设置过滤器的文章,但是我不知道我怎么能运行自定义的shell脚本。我希望设置这个过滤器:
*.js js-beautify ${file} -r -P --config .beautifyrc非常感谢,最诚挚的问候。
发布于 2015-07-06 08:47:14
您需要对您更改的文件运行git add,以便将它们记录下来。您修改的文件仅在工作副本中更改;它们在索引上没有更改,这是Git在创建commit时使用的。
请注意,这种方法在部分git添加时失败(在这里,您正在暂存要提交的文件的一部分,而不是整个文件)。
https://stackoverflow.com/questions/31241092
复制相似问题