问题描述
我最近在github上启动了一个。
每次使用Travis提交后,我都设法设置自动测试。但是现在我想用jshint设置一个预先提交的钩子。所以如果jshint报告错误,提交应该失败。但是,这是可能的,如果是这样,如何做到这一点?
I recently started a project on github.I've managed to setup automatic testing after each commit using Travis. But now I would like to setup a pre-commit hook with jshint too. So if jshint reports errors, the commit should fail. But is this possible, and if so, how to do this ?
推荐答案
是的!这个有可能。我最近写了。请注意,它不是特定于GitHub,通常只是Git - 因为它是一个预提交钩子,它在之前运行任何数据发送到GitHub。
Yes! This is possible. I recently wrote about it. Note that it's not specific to GitHub, just Git in general - as it's a pre-commit hook, it runs before any data is sent to GitHub.
仓库的 /。git / hooks 目录中的任何适当命名的可执行文件都将作为钩子运行。默认情况下,可能会有一堆示例挂钩。 ,我将其用作JSLint预提交钩子(您可以非常轻松地修改它以使用JSHint ):
Any appropriately-named executable files in the /.git/hooks directory of your repository will be run as hooks. There will likely be a bunch of example hooks in there already by default. Here's a simple shell script that I use as a JSLint pre-commit hook (you could modify it very easily to work with JSHint instead):
#!/bin/sh
files=$(git diff --cached --name-only --diff-filter=ACM | grep "\.js$")
if [ "$files" = "" ]; then
exit 0
fi
pass=true
echo "\nValidating JavaScript:\n"
for file in ${files}; do
result=$(jslint ${file} | grep "${file} is OK")
if [ "$result" != "" ]; then
echo "\t\033[32mJSLint Passed: ${file}\033[0m"
else
echo "\t\033[31mJSLint Failed: ${file}\033[0m"
pass=false
fi
done
echo "\nJavaScript validation complete\n"
if ! $pass; then
echo "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass JSLint but do not. Please fix the JSLint errors and try again.\n"
exit 1
else
echo "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi
您可以简单地将它放入Git钩子目录中名为 pre-commit 的可执行文件中,并在每次提交之前运行。
You can simply put that in an executable file named pre-commit in your Git hooks directory, and it will run before every commit.
这篇关于设置预先提交钩子jshint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!