Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

githooks

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

like image 507
Tamas Kalman Avatar asked Mar 16 '12 02:03

Tamas Kalman


2 Answers

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.

like image 171
Amber Avatar answered Sep 18 '22 11:09

Amber


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

like image 27
manojlds Avatar answered Sep 17 '22 11:09

manojlds