Skip to content

Instantly share code, notes, and snippets.

@jhartikainen
Created February 6, 2015 17:46
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save jhartikainen/36a955f3bfe06557e16e to your computer and use it in GitHub Desktop.
Save jhartikainen/36a955f3bfe06557e16e to your computer and use it in GitHub Desktop.
ESLint git commit hook
#!/bin/bash
files=$(git diff --cached --name-only | grep '\.js$')
# Prevent ESLint help message if no files matched
if [[ $files = "" ]] ; then
exit 0
fi
echo $files | xargs eslint
rc=$?
if [[ $rc != 0 ]] ; then
echo "ESLint check failed, commit denied"
exit $rc
fi
@shorinji
Copy link

I think you could use git diff --staged --diff-filter=AM --name-only to shorten down file status selection

@enapupe
Copy link

enapupe commented Feb 7, 2017

One liner for using at package.json:

"lint-staged": "eslint `git diff --staged --diff-filter=AM --name-only | grep .js$ | tr '\n' ' '`"

It runs like eslint [file1] [file2] [file3] [etc] for all staged added/modified files

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