Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@pjlamb12

This comment has been minimized.

Copy link

@pjlamb12 pjlamb12 commented Sep 1, 2016

@wesbos: Have you done this same thing with TSLint? It seems like I should be able to basically do the same thing but replace eslint with tslint and be good to go.

---Update

It does work if you just replace eslint with tslint and .js with .ts.

@convexset

This comment has been minimized.

Copy link

@convexset convexset commented Sep 3, 2016

Made a little modification to it to allow someone who "knows what he's doing" to commit anyway. He just has to try within a pre-set time interval of the last fail.

https://gist.github.com/convexset/45048ea4458c7632cea5f70b4dd85c35

@broofa

This comment has been minimized.

Copy link

@broofa broofa commented Oct 28, 2016

I've have an improved(?) version with a couple tweaks:

  1. Calls eslint once for all files, instead of separately for each file
  2. Enables --fix option
  3. Re-adds files to commit, to include any "fix"ed files from previous step as part of the commit

P.S. @convexset: git commit --no-verify probably does what you're after already (allows committers to force a commit by suppressing the pre-commit hook)

@kingluddite

This comment has been minimized.

Copy link

@kingluddite kingluddite commented Dec 6, 2016

I had to fix permissions to get this to work:

chmod +x .git/hooks/post-commit

@drmercer

This comment has been minimized.

Copy link

@drmercer drmercer commented Apr 15, 2017

Super nifty! Thanks for this!

Not sure if this is what you meant for it to do, but I changed this line:

git show :$file | eslint $file

to this:

git show :$file | eslint --stdin

so that it actually lints the staged file, rather than the unstaged copy.

EDIT: This is even better; it shows the proper file name when an error is displayed:

git show :$file | eslint --stdin --stdin-filename "$file"
@JohannesFischer

This comment has been minimized.

Copy link

@JohannesFischer JohannesFischer commented May 9, 2017

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

This comment has been minimized.

Copy link

@luclucens luclucens commented Jul 6, 2017

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

@jskrzypek

This comment has been minimized.

Copy link

@jskrzypek jskrzypek commented Jul 30, 2017

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

@zerob4wl

This comment has been minimized.

Copy link

@zerob4wl zerob4wl commented Aug 20, 2017

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

@andrelandgraf

This comment has been minimized.

Copy link

@andrelandgraf 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

This comment has been minimized.

Copy link

@hkirsman 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