Git hook to reject commits where files contain a specific string

前端 未结 3 510
花落未央
花落未央 2020-12-16 16:37

I am using Guard with Rspec; I use focus: true to force it run only tests I am working on. But sometimes I forget to remove focu

3条回答
  •  被撕碎了的回忆
    2020-12-16 17:33

    A good source of information is the book at git-scm.

    You want the pre-commit hook. To return a non-zero value (and thus abort the commit), you'd want something along these lines:

    FILES_PATTERN='\.rb(\..+)?$'
    FORBIDDEN='focus: true'
    git diff --cached --name-only | \
      grep -spec/ | \
      grep -E $FILES_PATTERN | \
      xargs grep --with-filename -n $FORBIDDEN && echo "COMMIT REJECTED Found '$FORBIDDEN' references. Please remove them before commiting" && exit 1
    

    That's lifted from this rather good tips site. I haven't tested the tweaks I made.

提交回复
热议问题