如果我要提交的更改包含特定字符串(例如,@todo
或@hack
).
有人可以告诉我如何实现吗?
† 或警告。
‡ 或提交时。
最佳答案
检查字符串 '@todo' 是否被添加的简单预提交 Hook 可能如下所示:
#!/bin/sh
. git-sh-setup # for die
if git-diff-index -p -M --cached HEAD -- \
| grep '^+' \
| grep @todo; then
die Blocking commit because string @todo detected in patch
fi
如果这是 .git/hooks/pre-commit
的内容并且是可执行的,
任何添加字符串 @todo
的补丁都将被拒绝。
关于如果字符串出现在源代码(或差异)中,Git 在提交之前发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7291186/