In my python project, I have pre-commit-config.YAML where I want to create my custom file.
The intention of this file is fail git commit if python lint errors are greater than certain numbers. The following command will be used to count lines
pylint api/ | wc -l
Can someone please suggest some approach. I am new to the MAC and Python ecosystem?
EDIT sh file looks like this.
#!/bin/sh
a=$(pylint source/ | wc -l)
b=20
errorsCount="$(echo "${a}" | tr -d '[:space:]')"
if [ $errorsCount -gt $b ]
then
exit 1
fi
I tried
repos:
- repo: local
hooks:
- id: custom-script-file
name: custom-script-file
entry: hooks/pre-commit.sh
language: script
types: [python]
pass_filenames: false
But it wouldn't worked.
Open a terminal window by using option + T in GitKraken Client. Once the terminal windows is open, change directory to . git/hooks . Then use the command chmod +x pre-commit to make the pre-commit file executable.
If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files . To run individual hooks use pre-commit run <hook_id> . The first time pre-commit runs on a file it will automatically download, install, and run the hook.
Use the --no-verify option to skip git commit hooks, e.g. git commit -m "commit message" --no-verify . When the --no-verify option is used, the pre-commit and commit-msg hooks are bypassed.
The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.
Here's what you could do to use inline bash command as pre-commit hook entry
- repo: local
hooks:
- id: pylint-error-count
name: pylint-error-count
entry: bash -c 'lines=$(pylint api/ | wc -l) && (( lines > 10)) && exit 1'
language: system
types: [python]
pass_filenames: false
You can also write a script and invoke it this way:
entry: path/relavite/to/repo/root/pylint_validator.sh
language: script
NOTE: wc -l
is not an accurate count of errors.
EDIT: adding more options
- repo: local
hooks:
- id: simple-pylint
name: simple-pylint
entry: pylint
args: ["api/"]
language: system
types: [python]
pass_filenames: false
- id: inline-pylint-with-bash
name: inline-pylint-with-bash
entry: bash -c 'lines=$(pylint api/ | wc -l) && (( lines > 10)) && exit 1'
language: system
types: [python]
pass_filenames: false
- id: custom-script-file
name: custom-script-file
entry: relative/path/to/repo/root/check_pylint.sh
language: script
types: [python]
pass_filenames: false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With