简体   繁体   English

在预提交钩子中引发git警告

[英]Raise git warning in pre-commit hook

I've been using the pre-commit hooks in git to check for problems when I or someone else commits to a project at work. 当我或其他人在工作中提交项目时,我一直在使用git中的预提交钩子来检查问题。 I can exit with a non-zero status to indicate that the commit has serious problems that should be fixed immediately. 我可以以非零状态退出,以指示提交存在应立即修复的严重问题。

However, sometimes the problem is only something that the programmer needs a warning about (such as they need to update the help file, based on their change, for example). 但是,有时问题只是程序员需要警告的事情(例如,他们需要根据他们的更改来更新帮助文件)。 Is there any way for me to raise a git warning like the one you get with "LF will be replaced by CRLF" for example? 有没有办法让我提出一个git警告,例如你所获得的“LF将被CRLF取代”? Part of my motivation is that a few of us use SmartGit, so a warning shows up there clearly in yellow. 我的部分动机是我们中的一些人使用SmartGit,因此警告显示为黄色。

The post-commit hook looked promising but after trying different return values it never really gave me a warning either. 后提交钩子看起来很有希望,但在尝试不同的返回值后,它从未真正给我一个警告。 It would be nice to keep everything in the pre-commit hook, and I'm sure there are better ways to do this. 将所有内容保留在预提交钩子中会很好,我相信有更好的方法可以做到这一点。 Thanks for any tips you can give! 感谢您提供的任何提示!

It seems that the idea of being able to return a warning return code was just something that was planted in my head by SmartGit. 似乎能够返回警告返回码的想法只是SmartGit在我脑海中种植的东西。 The underlying git only returns zero for success and non-zero for failure. 底层git仅为成功返回零,为失败返回非零。

Run this in git bash on windows, for example: 在Windows上的git bash中运行它,例如:

touch tmp.txt
echo "lf line endings" > tmp.txt
git add tmp.txt
echo $?

You should get zero as the last return code, even though a "warning" just appeared after your last call to git (and SmartGit recognized it as a warning and probably gave you a nice yellow triangle). 你应该得到零作为最后一个返回代码,即使在你最后一次调用git之后出现“警告”(并且SmartGit将其识别为警告并且可能给你一个漂亮的黄色三角形)。

By experimenting with it (ie the hard way) I realized SmartGit just searches for lines that begin with "warning:" and sucks up the "warning:" part of the line to trigger the nice yellow triangle. 通过试验(即艰难的方式),我意识到SmartGit只是搜索以“warning:”开头的行,然后吸收行的“警告:”部分来触发漂亮的黄色三角形。 It removes the "warning:" text and prints everything after the ":" in "warning:" as the warning in their 'Output' window. 它删除了“warning:”文本,并将“warning:”中“:”后的所有内容打印为“输出”窗口中的警告。

Not very sophisticated but at least it's documented for them now. 不是很复杂,但至少现在已经为他们记录了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM