observing/pre-commit

Pass list of commiting files as parameter to npm script

Opened this issue · 2 comments

Hello, thanks for good module!
But one thing is really needed.
For example, I have lint script on npm, that run linting gulp lint task. And in this task I can get list of all changed files from last commit through gulp-git to lint only them like this

gulp.task('git-diff', callback => {
  git.exec({args: `diff-index --name-only HEAD | grep .*\\.js | grep -v json`, quiet: true}, (err, stdout) => {
    .....
  });
});

But if I commit only some files this task anyway will take every changed, i.e. some files that I don't want to commit right now may have broken lint rules (for examples this files in deep development in that moment).
It would be great, if pre-commit passed list of files to npm script, for example if we have

"scripts": {
   "lint": "gulp lint"
},
"pre-commit": [
  "lint"
]

And pre-commit can call npm run lint -- --file path1 [--file path2 [--file path3]] where pathN is path to commiting files. In this case I can read in gulp array of passed files and lint only them.

you could use the --cached flag of git diff-index to only match the files that are actually staged.

For instance, I use in my package.json the following script:

eslint $(git diff-index --name-only HEAD --cached | grep \".js$\シ

to lint all the staged files. You could do:

git.exec({args: `diff-index --name-only --cached HEAD | grep .*\\.js | grep -v json`, //...

Check out https://github.com/okonet/lint-staged. It solves exactly this problem.