仅在修改某些目录中的文件时才执行git pre-commit hook


11

我正在尝试为git pre-commit钩子创建bash脚本。每当进行提交时,我都想检查某个目录中的文件是否是已暂存文件的一部分,如果是,请运行命令(Grunt任务)。到目前为止,我一切正常,除了只有在修改某些文件后才能运行。我正在尝试使它起作用:

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

不幸的是,这给了我错误。

Answers:


9

尝试,

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

快乐黑客


8

像这样尝试:

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

这看起来很像被接受的答案,但是那是错误的。在bash / sh中,管道中的每个命令都在单独的子shell中执行,因此then部分中的退出将仅退出子shell,fi之后的任何代码仍将执行!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.