Conditional pre-commit hook controlled from command line for GIT: Is it possible?

跟風遠走 提交于 2019-12-12 10:45:07

问题


We have a nice pre-commit hook for GIT, as well as a nice commit-msg. The pre-commit hook does a syntax validation and the commit-msg does some other business logic. It all works well.

However, I'd like to add Coding Standards validation to the pre-commit hook. In fact, it's already added. However, I don't want to strictly enforce our developers to match up with the coding standards, by default I'd like to validate the code for standards but if they would like to pass Coding Standards Validation, I'd like to let them pass by adding a parameter during commit.

Is it possible to capture/interpret any command line parameter which was given during commit to git at the pre-commit hook level in order to skip coding standard validation in the pre-commit hook (optionally?)

Or is it possible only in the pre-commit message hook by analyzing the commit message for a specific substring?

Please share your best practices about how (and where) to create command line controlled conditional code using git pre-commit hook (or other git hooks).


回答1:


A simple way of doing this would be to simply use an environment variable:

STANDARDS=no git commit

and then in the script (example in Bash, but you could read env vars in whatever language your hook is in):

if [ "$STANDARDS" != "no" ]; then
    ...check for code standards...
fi

Git doesn't normally pass information beyond what is listed in man githooks to each hook, so trying to do it with a "command line option" a la git commit --no-standards or some such wouldn't work.




回答2:


To add to what @Amber said, you should be able to do:

STANDARDS=no git commit -m "committing"

and have appropriate pre-commit hook which will see the environment variable and make decisions.

There is of course the --no-verify, but I suppose you don't want to skip the entire pre-commit:

-n
--no-verify

This option bypasses the pre-commit and commit-msg hooks. See also githooks(5).



来源:https://stackoverflow.com/questions/9730937/conditional-pre-commit-hook-controlled-from-command-line-for-git-is-it-possible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!