Execute git pre-commit hook only if files in certain directory modified

bashgit

I am trying to create a bash script for a git pre-commit hook. Whenever a commit is made, I want to check if files in a certain directory are part of the staged files and if so, run a command (Grunt task). So far I have everything working, except for only running if certain files are modified. I'm trying to get this to work:

SRC_PATTERN="site/assets/js/"
if [ grep --quiet $SRC_PATTERN `git diff --cached --name-only` ]
then
  echo "none"
  exit 0
fi

This is unfortunately giving me errors.

Best Answer

try,

git diff --cached --name-only | if grep --quiet "$SRC_PATTERN"
then
    ...

Happy hacking

Related Question