Skip to content

Instantly share code, notes, and snippets.

@wesbos
Created July 4, 2016 18:55
Show Gist options
  • Star 50 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save wesbos/8aec9d2ff7f7cf9dd65ca2c20d5dfc23 to your computer and use it in GitHub Desktop.
Save wesbos/8aec9d2ff7f7cf9dd65ca2c20d5dfc23 to your computer and use it in GitHub Desktop.
ESLint 3.0 Git Pre Commit Hook
#!/bin/bash
files=$(git diff --cached --name-only | grep '\.jsx\?$')
# Prevent ESLint help message if no files matched
if [[ $files = "" ]] ; then
exit 0
fi
failed=0
for file in ${files}; do
git show :$file | eslint $file
if [[ $? != 0 ]] ; then
failed=1
fi
done;
if [[ $failed != 0 ]] ; then
echo "🚫🚫🚫 ESLint failed, git commit denied!"
exit $failed
fi
@JohannesFischer
Copy link

Neat, thanks a lot! Additionally using --diff-filter=M in the diff command to skip renamed and deleted files and test modified files only.

@luclucens
Copy link

Not working for me:
.git/hooks/commit-msg: line 11: eslint: command not found
ESLint failed, git commit denied!

@jskrzypek
Copy link

@luclucens you need to have eslint installed globally for that to work

@zerob4wl
Copy link

@luclucens you can install eslint locally and user ./node_modules/.bin/eslint instance of eslint

@andrelandgraf
Copy link

andrelandgraf commented Aug 3, 2018

@zerob4wl this does not work for me:
Any workaround as I really do not want to install eslint globally?

#!/bin/bash
files=$(git diff --cached --name-only | grep '\.jsx\?$')

# Prevent ESLint help message if no files matched
if [[ $files = "" ]] ; then
  exit 0
fi

failed=0
for file in ${files}; do
  git show :$file | ./node_modules/.bin/eslint $file
  if [[ $? != 0 ]] ; then
    failed=1
  fi
done;

if [[ $failed != 0 ]] ; then
  echo "ESLint failed, git commit denied!"
  exit $failed
fi

EDIT: made it work now. I will not delete this post and instead post the solution if you might have the same problem as me:
You have to make sure that the hook is exeutable like follows:
chmod +x commit-msg

@hkirsman
Copy link

hkirsman commented Jan 21, 2019

I've added this to my composer.json so when doing 'composer install', everybody in team would get the script:

"scripts": {
        "pre-install-cmd": [
            "[[ -f .git/hooks/commit-msg ]] || curl https://gist.githubusercontent.com/wesbos/8aec9d2ff7f7cf9dd65ca2c20d5dfc23/raw/f662c8b15515180c8310e2d20d192dd3d92a7d28/commit-msg --output .git/hooks/commit-msg && chmod +x .git/hooks/commit-msg"
        ]
    }

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