You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.2 KiB
38 lines
1.2 KiB
4 years ago
|
---
|
||
4 years ago
|
title: "Git Tips - Lint + Test Pre-commit Hook"
|
||
4 years ago
|
date: 2020-01-01T21:00:39Z
|
||
4 years ago
|
tags: [git, bash, ]
|
||
4 years ago
|
---
|
||
|
|
||
4 years ago
|
One of my favorite inventions is a `pre-commit` hook that auto runs test and lint commands from a `makefile` or
|
||
4 years ago
|
`package.json` if they're found:
|
||
4 years ago
|
|
||
|
```sh
|
||
|
{{<highlight bash "linenos=table">}}
|
||
|
#!/usr/bin/env bash
|
||
|
|
||
|
if [ -f "$PWD/makefile" ] && [ ! -z "$(cat $PWD/makefile | grep '^lint:')" ]; then
|
||
|
echo "running make lint"
|
||
|
make lint
|
||
|
elif [ -f "$PWD/package.json" ] && [ ! -z "$(cat $PWD/package.json | grep "^\"lint\":")" ]; then
|
||
|
echo "running npm run lint"
|
||
|
npm run lint
|
||
|
fi
|
||
|
|
||
|
if [ -f "$PWD/makefile" ] && [ ! -z "$(cat $PWD/makefile | grep '^test:')" ]; then
|
||
|
echo "running make test"
|
||
|
make test
|
||
|
elif [ -f "$PWD/package.json" ] && [ ! -z "$(cat $PWD/package.json | grep "^\"test\":")" ]; then
|
||
|
echo "running npm run test"
|
||
|
npm run test
|
||
|
fi
|
||
|
{{</highlight>}}
|
||
|
```
|
||
|
|
||
4 years ago
|
The `/usr/bin/env bash` piece ensures that the script has access to all of the environment variables you expect in your
|
||
|
regular shell.
|
||
4 years ago
|
|
||
|
If the test or lint command fails then the `git commit` command fails. If I absolutely need to commit something in spite
|
||
4 years ago
|
of the lint/test results I can do `git commit --no-verify` to skip the `pre-commit` hook.
|
||
4 years ago
|
|