简体   繁体   中英

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. 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? Part of my motivation is that a few of us use SmartGit, so a warning shows up there clearly in yellow.

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. The underlying git only returns zero for success and non-zero for failure.

Run this in git bash on windows, for example:

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

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. It removes the "warning:" text and prints everything after the ":" in "warning:" as the warning in their 'Output' window.

Not very sophisticated but at least it's documented for them now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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