Bash filter out files with a given extension
I've added a pre-commit hook to run Rubocop against any files that are being staged for commit. However, Rubocop errors out when you give it a .png or .svg file. I've added those file extensions to the
exclude block in
.rubocop.yml but because I'm actually explicitly supplying files by name that doesn't seem to do the trick.
Here's my pre-commit script:
#!/usr/bin/env bash set -e git diff --staged --diff-filter=d --name-only | xargs bundle exec rubocop
I think the approach is grabbing the list of files from that git command and looping through them, filtering out any that are .png or .svg, but honestly I don't know how to do that. Any suggestions on filtering the files by extension?
One idea might be to filter out those extensions using
git diff --staged --diff-filter=d --name-only \ | grep --invert-match '\.\(png\|svg\)$' \ | xargs bundle exec rubocop
There is a flag
--only-recognized-file-typeswhich I think will do what you need.
git diff --staged --diff-filter=d --name-only -- ':!*.png' ':!*.svg'
or for easier control you could check attributes and set up the matching just in
git help glossaryand hunt up
pathspec, there's probably a quicker route to find the docs for it but that's the one I know.