Git tips - lint + test pre-commit hook
A nice last minute sanity check I use is a pre-commit
hook that auto runs test and lint commands from a makefile
or
package.json
if they’re found:
1#!/usr/bin/env bash
2
3if [ -f "$PWD/makefile" ] && [ ! -z "$(cat $PWD/makefile | grep '^lint:')" ]; then
4 echo "running make lint"
5 make lint
6elif [ -f "$PWD/package.json" ] && [ ! -z "$(cat $PWD/package.json | grep "^\"lint\":")" ]; then
7 echo "running npm run lint"
8 npm run lint
9fi
10
11if [ -f "$PWD/makefile" ] && [ ! -z "$(cat $PWD/makefile | grep '^test:')" ]; then
12 echo "running make test"
13 make test
14elif [ -f "$PWD/package.json" ] && [ ! -z "$(cat $PWD/package.json | grep "^\"test\":")" ]; then
15 echo "running npm run test"
16 npm run test
17fi
If the test or lint command fails then the git commit
command fails. If I absolutely need to commit something in spite
of the lint/test results I can do git commit --no-verify
to skip the pre-commit
hook.