0

I want to ignore any dist and node_modules directories in my git commit hook. I've tried this:

git diff --cached --name-only --diff-filter=ACM | find . -path ./node_modules -prune -o -path './**/dist -prune -o -name '*.js'

But it doesn't work. It doesn't seem like the find is accepting the files found from the git diff...

If I run:

git diff --cached --name-only --diff-filter=ACM

I correctly get the staged files:

./dist/some-file.js

And if I run:

find . -path ./node_modules -prune -o -path './**/dist' -prune -o -name '*.js' -print

I correctly get a list of files that don't include any that are in dist or node_modules directories.

How can I combine these do that my git hook doesn't pick up staged files in dist directories.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Mike Rifgin
  • 10,409
  • 21
  • 75
  • 111

1 Answers1

2

find doesn't process its standard input, so it can't be used after |

grep can be used instead

| grep -v '/dist/' | grep -v '/node_modules/'

or using one grep process

| grep -Ev '/dist/|/node_modules/'
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
  • Thanks for the speedy reply. How do I use this along with looking for .js files? Another pipe ? | grep -Ev '/dist/|/node_modules/' | grep ‘.js$’ ? – Mike Rifgin Dec 13 '17 at 16:48
  • yes, `grep -v` is an exclude filter, `grep` without `-v` include filter, `-E` is for extended regexp, see also grep --help – Nahuel Fouilleul Dec 13 '17 at 16:51
  • I have multiple dist folders which are sometimes nested. I’ve tried /**/dist/|/node_modules/ but that just gives me “repititon operator operand invalid “ – Mike Rifgin Dec 14 '17 at 09:31
  • It’s ok I think I need /*dist/|node_modules/ – Mike Rifgin Dec 14 '17 at 09:35
  • grep operates only over intput stream (strings) the pattern is a regexp pattern not a glob `/dist/` matches any path which contains `/dist/`, see grep documentation and regular expressions for more information – Nahuel Fouilleul Dec 14 '17 at 11:04