Git预提交钩子运行lint检查?

4

如何编写一个 Git pre-commit hook,以对更改/新增文件运行代码检查?

目前,我在提交之前运行 "./gradlew lint" 命令。如果有一种方法可以在提交到 Git 之前仅对新增/更改的文件运行检查,那将会更加方便。


嘿,你解决了吗? - Majkeee
1个回答

0
# 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

将此代码放入您的 .git/hooks/ 文件夹中,命名为 pre-commit.sh。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接