What are Git hooks?
Git hooks are scripts you execute before an action is performed. It can be
pre-rebase. Often, developers use them to perform
some checks on the commit being pushed, either on the commit title, message
or the code itself.
Why using hooks with Code Inspector?
Using hooks with Code Inspector, you can check your code before pushing it. You invoke Code Inspector to check your code on what has been modified.
It shows you all errors before you push your code and send it for review. That way, no error or warnings are triggered by Code Inspector during the Code Review since you checked everything before pushing the code! In other words, it helps you doing faster code reviews.
The first step is to install our
that is distributed in the
code-inspector Python package. To install it,
just run the following command:
pip install code-inspector
In order to use the tool, you need to define the following variables:
CODE_INSPECTOR_ACCESS_KEY: your API access key
CODE_INSPECTOR_SECRET_KEY: your API secret key
These API keys are available in your profile.
Configure your Git hooks
You need to add a script in your Git repository to integrate with Code Inspector and check for violations at each push.
.git/hooks/pre-push in your Git repository and make sure
to make it executable (
chmod a+x .git/hooks/pre-push).
The tool is invoked with the local and remote SHA. It also needs the name
of the corresponding project on Code Inspector (the argument
<project-name> with the name of your project on Code Inspector so that
it will filter violations according to your project configuration.
"$1" url="$2" z40=0000000000000000000000000000000000000000 while read local_ref local_sha remote_ref remote_sha do code-inspector-pre-hook-check --project-name "<project-name>" --remote-sha $remote_sha --local-sha $local_sha if [ "$?" -ne "0" ]; then echo "Code Inspector found errors" exit 1 fi done exit 0remote=
You can use some additional options with
code-inspector-pre-hook-check to filter
some violations or categories.
--exclude-categories: exclude categories of violations. Separate multiple categories using a comma. For example
--exclude-categories=Performance,Code_Style,Designwill reports all issues in the code being pushed but the one that are classified under the performance, code style or design categories.
--exclude-severities: exclude categories of severities. Separate severities are specified using a comma. Severities are specified by number, 4 being the lowest and 1 the highest severity. The option
--exclude-severities=3,4reports all violations but the one with the severity 3 and 4.
When using the
pre-push script, your code is being sent to the Code Inspector
servers for analysis. The analysis is not local, it is done directly
on Code Inspector servers to avoid any load on your local machine.
If you have any issue with the
pre-push script, please open
an issue on our GitHub project.