如何编写一个 Git pre-commit hook
,以对更改/新增文件运行代码检查?
目前,我在提交之前运行 "./gradlew lint" 命令。如果有一种方法可以在提交到 Git 之前仅对新增/更改的文件运行检查,那将会更加方便。
# Get custom info
dirToLint=$(git config hooks.lintTargetDirectory)
lintArgs=$(git config hooks.lintArgs)
# If user has not defined a preferred directory to lint against, make it .
if [ -z "$dirToLint"]
then
dirToLint="."
fi
# Perform lint check
echo "Performing pre-commit lint check of ""$dirToLint"
lint $lintArgs "--exitcode" $dirToLint
lintStatus=$?
if [ $lintStatus -ne 0 ]
then
echo "Lint failure."
exit 1
fi
exit $lintStatus