git 设置预提交钩子 jshint
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15703065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setup pre-commit hook jshint
提问by Jeanluca Scaljeri
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 上开始了一个项目。我设法在每次使用 Travis 提交后设置自动测试。但是现在我也想用 jshint 设置一个预提交钩子。因此,如果 jshint 报告错误,则提交应该失败。但这是可能的,如果是这样,如何做到这一点?
回答by nelsonic
There's an easier wayof doing pre-commit checks (e.g. JSHint) in your Node.js workflow:
在您的 Node.js 工作流中,有一种更简单的方法可以进行预提交检查(例如 JSHint):
Install jshintfrom NPM:
从 NPM安装jshint:
npm install jshint
npm install jshint
Next create a .jshintrcfile in your project if you don't already have one. e.g: https://github.com/nelsonic/learn-jshint/blob/master/.jshintrc
接下来在你的项目中创建一个.jshintrc文件,如果你还没有的话。例如:https: //github.com/nelsonic/learn-jshint/blob/master/.jshintrc
Now install pre-commitmodule (and save it as a dev dependency):
现在安装预提交模块(并将其保存为开发依赖项):
npm install pre-commit --save-dev
npm install pre-commit --save-dev
Next you will need to define the task (script) that will be run for JSHint in your package.json
接下来,您需要在package.json 中定义将为 JSHint 运行的任务(脚本)
e.g:
例如:
{ "scripts": { "jshint": "jshint -c .jshintrc --exclude-path .gitignore ." } }
{ "scripts": { "jshint": "jshint -c .jshintrc --exclude-path .gitignore ." } }
then you register the scripts you want to be run pre-commit (also in package.json) e.g:
然后你注册你想要预提交运行的脚本(也在 package.json 中),例如:
"pre-commit": [ "jshint", "coverage", "etc" ]
"pre-commit": [ "jshint", "coverage", "etc" ]
This allows you to have more than just one check in your pre-commit workflow. (We have checks to ensure team members code complies with JSHint, Code Style and Test Coverage is 100%)
这使您可以在预提交工作流程中进行不止一项检查。(我们有检查以确保团队成员的代码符合 JSHint,代码风格和测试覆盖率为 100%)
For a more detailed tutorial you can share with your team see: https://github.com/nelsonic/learn-pre-commit
如需更详细的教程,您可以与您的团队分享,请参阅:https: //github.com/nelsonic/learn-pre-commit
回答by James Allardice
But is this possible...
但这可能吗……
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 beforeany data is sent to GitHub.
是的!这个有可能。我最近写了一篇关于它的文章。请注意,它不是特定于 GitHub 的,只是一般的 Git - 因为它是一个预提交钩子,它在任何数据发送到 GitHub之前运行。
Any appropriately-named executable files in the /.git/hooksdirectory 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 scriptthat I use as a JSLint pre-commit hook (you could modify it very easily to work with JSHint instead):
存储库的/.git/hooks目录中任何适当命名的可执行文件都将作为钩子运行。默认情况下,那里可能已经有一堆示例钩子。这是一个简单的 shell 脚本,我将其用作 JSLint 预提交钩子(您可以很容易地修改它以使用 JSHint):
#!/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 "\t3[32mJSLint Passed: ${file}3[0m"
else
echo "\t3[31mJSLint Failed: ${file}3[0m"
pass=false
fi
done
echo "\nJavaScript validation complete\n"
if ! $pass; then
echo "3[41mCOMMIT FAILED:3[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 "3[42mCOMMIT SUCCEEDED3[0m\n"
fi
You can simply put that in an executable file named pre-commitin your Git hooks directory, and it will run before every commit.
你可以简单地把它放在你的 Git hooks 目录中一个名为pre-commit的可执行文件中,它会在每次提交之前运行。
回答by igor
Some changes to @James Allardice script to accommodate JSHint. Thanks for the original code.
某些更改@詹姆斯阿勒代斯脚本来满足JSHint。感谢您提供原始代码。
#!/bin/sh
#
# Run JSHint validation before commit.
files=$(git diff --cached --name-only --diff-filter=ACMR -- *.js **/*.js)
pass=true
if [ "$files" != "" ]; then
for file in ${files}; do
result=$(jshint ${file})
if [ "$result" != "" ]; then
echo "$result"
echo "\n"
pass=false
fi
done
fi
if $pass; then
exit 0
else
echo ""
echo "COMMIT FAILED:"
echo "Some JavaScript files are invalid. Please fix errors and try committing again."
exit 1
fi
回答by EnriMR
A similar script to the @igor's one with some improvements:
与@igor 类似的脚本,但有一些改进:
- color indicators
- no --diff-filter, grep used insead
- help message (git style) to avoid pre-commit call
- 颜色指标
- 没有 --diff-filter,grep 使用 insead
- 帮助消息(git 样式)以避免预提交调用
#!/bin/sh
#
# Run JSHint validation before commit.
RED='3[0;31m'
REDBOLD='3[1;31m'
ORANGE='3[0;33m'
NC='3[0m' # No Color
files=$(git diff --cached --name-only | grep .js)
pass=true
totalErrors=0
if [ "$files" != "" ]; then
for file in ${files}; do
result=$(jshint ${file})
if [ "$result" != "" ]; then
echo "${RED}$result${NC}"
pass=false
totalErrors=$((totalErrors+1))
fi
echo ""
done
fi
if $pass; then
exit 0
else
echo "${ORANGE}===== ${totalErrors} JSHint Error${NC}"
echo ""
echo "${REDBOLD}COMMIT FAILED: Some JavaScript files are invalid. Please fix errors and try committing again.${NC}"
echo ""
echo " (use -n option \"git commit -n -m <message>\" to avoid call pre-commit hook and JSHint check)"
echo ""
exit 1
fi