Skip to content

Commit

Permalink
Linting pre-commit hook (#787)
Browse files Browse the repository at this point in the history
* pre-commit hook and reqs

* Updated pre-commit file

* Added hook setup file

* Updated linting step to only use yapf and only lint staged files

* Removed redundant yapf from environment

* Updated developer readme on github

* Edited dev readme

* Updated web documentation

* Updated installation.rst

* Deleted extra whitespace

* Added hook_setup confirmation and updated documentation

* Standardized shell files

* Updated documentation

* updated pre-commit hook

Co-authored-by: Priti Ashvin Shah <[email protected]>
  • Loading branch information
ismirlia and priti-ashvin-shah-ibm authored May 9, 2022
1 parent 3c78735 commit 2f85eeb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README_developers.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,13 @@ If you are planning to develop the qiskit metal codebase, you need extra package
```
python -m pip install -r requirements-dev.txt
```
You'll want to also use these instructions to [setup user environment](/docs/NEW_DEVELOPER_SETUP.md)
You may also want to also use these instructions to [setup user environment](/docs/NEW_DEVELOPER_SETUP.md)

#### Setting up precommit hooks

If are planning on committing, you can run the following in the root of your project to link the available precommit hooks.
```
./hook_setup
```
Please make sure the command is run from the same shell you plan on using to commit. If running on Windows, please make sure that this script is run from git-bash or another Linux-style shell. Currently, the precommit hook will check for yapf formatting.

11 changes: 11 additions & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ Here are some things to consider when setting up a development environment:

* Library errors when activating conda environments, or initializing jupyter notebook/lab, indicate a conflict between python libraries in the base and sub environments. Go ahead and manually delete the library from the base environment `site-packages` folder, shows in the error message. You might need to reinstall them in the sub environment, or create a new one.

--------------------------
Setting up precommit hooks
--------------------------

If are planning on committing, you can run the following in the root of your project to link the available precommit hooks.
::

./hook_setup

Please make sure the command is run from the same shell you plan on using to commit. If running on Windows, please make sure that this script is run from git-bash or another Linux-style shell. Currently, the precommit hook will check for yapf formatting.

=============
Common Issues
=============
Expand Down
9 changes: 9 additions & 0 deletions hook_setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

ln -s -f ../../hooks/pre-commit .git/hooks/pre-commit
ret=$?
if [[ $ret != 0 ]]; then
echo "Linking hooks failed with rc $ret"
else
echo "Linking hooks succeeded!"
fi
33 changes: 33 additions & 0 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

echo "Checking dependencies"
which yapf &> /dev/null
ret=$?
if [[ $ret != 0 ]]; then
echo "Please install yapf to use pre-commit hook"
exit 1
else
echo "All dependencies present"
fi

echo "Begin linting step using yapf formatting"
linterrorfound=0
for stagedfile in $(git diff --name-only --cached | grep .*.py)
do
yapf --diff --style .style.yapf $stagedfile
ret=$?
if [[ $ret != 0 ]]; then
linterrorfound=1
echo ">>> Errors in file $stagedfile"
fi
done

if [[ $linterrorfound == 1 ]]; then
echo "Please fix linting errors before committing"
exit 1
else
echo "Linting successful"
fi

echo "Python linting validation completed!"
exit 0

0 comments on commit 2f85eeb

Please sign in to comment.