Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run custom shell script file before pre commit hook

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.

like image 786
Kedar Shinde Avatar asked Dec 27 '19 10:12

Kedar Shinde


People also ask

How do I run a git script before committing?

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.

How do you run pre-commit before commit?

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.

How do you bypass pre-commit 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.

How does git pre-commit hook work?

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.


1 Answers

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

like image 57
RafalS Avatar answered Oct 23 '22 02:10

RafalS