observing / pre-commit

Automatically installs a git pre-commit script in your git repository which runs your `npm test` on pre-commit
MIT License
1.88k stars 150 forks source link

Only run jshint on changed files #50

Closed shawn-simon closed 9 years ago

shawn-simon commented 9 years ago

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

mroderick commented 9 years ago

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!

shawn-simon commented 9 years ago

Omg! Thanks so much!

mroderick commented 9 years ago

Happy to help :)

lili21 commented 8 years ago

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

mroderick commented 8 years ago

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

lili21 commented 8 years ago

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

3rd-Eden commented 8 years ago

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.

okonet commented 8 years ago

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