Using Git Hooks

Adding a Simple Hook

Use the code to get started:

Copy
$ cd your-repo
$ echo '$HOME/.spectral/spectral scan --unstaged' > .git/hooks/pre-commit
$ chmod u+x .git/hooks/pre-commit

This requires Code Security installed globally for the user.

Each time you perform a commit, Code Security scans for issues.

To remove the hook, delete the file:

$ rm .git/hooks/pre-commit

Notes:

  • As this repo is stored locally on your disk, Code Security cannot enforce the scan on the central server-hosted repo or on other computers.

  • You can only have one running hook at a time.

Integrating Husky

You can integrate Code Security and Husky in two ways:

  • Pick a hook manager for your own language.

  • Pick a hook manager using for Node.js.

To integrate:

Copy
$ yarn init
$ yarn add --dev husky
$ npm set-script prepare "husky install"
$ npm run prepare
$ npx husky add .husky/pre-commit "$HOME/.spectral/spectral scan --unstaged"
$ git add .husky/pre-commit
$ git commit -m "Keep calm and commit"

Integrating other Hook Managers

If you prefer using a manager that is independent of a programming language, or if you prefer to use a manager specific to your programming language, refer to this website and select an applicable hooks manager.