Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raise git warning in pre-commit hook

Tags:

git

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!

like image 839
davidvandebunte Avatar asked Nov 03 '22 04:11

davidvandebunte


1 Answers

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 (i.e. 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.

like image 102
davidvandebunte Avatar answered Nov 08 '22 16:11

davidvandebunte