有条件的预提交钩子从命令行控制GIT:是否有可能? [英] Conditional pre-commit hook controlled from command line for GIT: Is it possible?

查看:147
本文介绍了有条件的预提交钩子从命令行控制GIT:是否有可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们对GIT有一个很好的提交前钩子,还有一个很好的commit-msg。预提交钩子进行语法验证,并且提交消息执行一些其他业务逻辑。这一切都很好。



但是,我想将编码标准验证添加到预提交钩子。事实上,它已经被添加了。但是,我不想严格执行我们的开发人员来匹配编码标准,默认情况下我想验证代码的标准,但如果他们想通过编码标准验证,我想让他们通过在提交期间添加参数。

是否有可能捕获/解释在commit-commit级别提交git期间给出的任何命令行参数,以便跳过pre中的编码标准验证-commit hook(可选?)



或者只有在预先提交的消息钩子中,通过分析特定子字符串的提交消息才有可能?



请使用git pre-commit hook(或其他git钩子)分享您的最佳实践,以了解如何(以及在​​哪里)创建命令行控制的条件代码。



 <$ 

c $ c> STANDARDS = no git commit

然后在脚本中(例如Bash,但是你可以用你的钩子的任何语言阅读env vars):

  if [$ STANDARDS!=no];然后
...检查代码标准...
fi






Git通常不会将超出 man githooks 中列出的信息传递给每个钩子,因此尝试使用command行选项a la git commit --no-standards 或者其他一些行不通。


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).

解决方案

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.

这篇关于有条件的预提交钩子从命令行控制GIT:是否有可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆