Command-Line Conditional Pre-Clamp for GIT: Is this Possible?

We have a good pre-commit hook for GIT, as well as a good commit-msg. The pre-commit hook performs syntax checking, and commit-msg does some other business logic. Everything works well.

However, I would like to add a coding standard check on the pre-hook. In fact, this has already been added. However, I do not want to strictly monitor that our developers comply with the coding standards. By default, I would like to check the code for the standards, but if they would like to pass a check on the coding standards, I would like them to pass by adding a parameter to fixing time.

Is it possible to capture / interpret any command line parameter that was set during commit on git at the capture level before commit in order to skip the standard encoding check in the pre-commit hook (optional?)

Or is this only possible when intercepting a message before committing by analyzing the commit message for a specific substring?

Share your recommendations on how (and where) to create conditional code with a controlled command line using the git pre-commit hook (or other git hooks).

+6
source share
2 answers

An easy way to do this would be to simply use an environment variable:

STANDARDS=no git commit 

and then in the script (example in Bash, but you can read env vars in any language your hook is in):

 if [ "$STANDARDS" != "no" ]; then ...check for code standards... fi 

Git usually does not pass information beyond the limits specified in man githooks to every attempt, so try to do this using the command line option a la git commit --no-standards or some of them will not work.

+10
source

To add to what @Amber said, you should be able to:

 STANDARDS=no git commit -m "committing" 

and you have the corresponding pre-commit hook that will see the environment variable and make decisions.

Of course there is --no-verify , but I suppose you don't want to skip all the pre-commit:

-P
--no-verify

This option bypasses the pre-commit and commit-msg hooks. See also githooks (5).

+3
source

Source: https://habr.com/ru/post/910862/


All Articles