Skip to content

Instantly share code, notes, and snippets.

@baranga
Created June 21, 2017 09:32
Show Gist options
  • Save baranga/57925634f0db318b8f94915cee055bc4 to your computer and use it in GitHub Desktop.
Save baranga/57925634f0db318b8f94915cee055bc4 to your computer and use it in GitHub Desktop.
eslint as git pre commit hook
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
eslint=$(PATH="./node_modules/.bin:$PATH" which eslint)
if [[ -e "$eslint" ]]
then
jsfiles=$(git diff-index --cached --name-only --diff-filter=ACM $against | grep '.js$')
if [[ -n "$jsfiles" ]]
then
eslintOutput=$("$eslint" -f stylish --color -o .eslint.log $jsfiles)
if [[ $? != 0 ]]
then
cat .eslint.log
exit 1
fi
fi
fi
@derlarsen
Copy link

derlarsen commented Jun 21, 2017

For all fans of git UIs like myself this gist may suit you better: https://gist.github.com/derlarsen/1de6f1fcd6d496e4208ff56c7eefa58d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment