observing/pre-commit

Only run jshint on changed files

Closed this issue · 8 comments

Is there any way to only run the hook (jshint in my case) on the files being committed? It's quite slow on my large repo

Yes, you can create a script that pre-commit can run for you

This assumes that you've installed jshint into your project (which you'll want to ensure consistent results for everyone).

This script only validates staged changes.

scripts/jshint-pre-commit.sh

#!/bin/bash

git stash -q --keep-index

git diff-index --cached HEAD --name-only --diff-filter ACMR | egrep '.js$' | xargs $(npm bin)/jshint
RESULT=$?

git stash pop -q

[ $RESULT -ne 0 ] && exit 1
exit 0

In package.json you'll want to regsiter it:

{
  "scripts": {
    "jshint-pre-commit": "./scripts/jshint-pre-commit.sh"
  },
  "pre-commit": ["jshint-pre-commit"]
}

Enjoy!

Omg! Thanks so much!

Happy to help :)

I think that script should be written in pre-commit hook.

@lili21 I don't follow, can you elaborate?

@mroderick I mean this behavior should be default.

Let's say I got this config in my package.json file.

...
"scripts": {
  "foo": "commond1/to/run",
  "bar": "commond2/to/run"
},
...
"pre-commit": ['foo', 'bar']

pre-commit file within .git/hooks should run foo, bar on changed files by default。 that would be more reasonable , right ?

I'm more than happy to accept pull requests that can send the changed files as additional arguments to scripts. But a feel like this should be optional behavior because if you have your test specified and change an index.js you don't want to run index.js but your actual test file.

I've created a module that solves that: https://github.com/okonet/lint-staged Thoughts?