如何防止使用 git hook 提交文件 (.json)?
我有一个 .json 文件仍在服务器上。所以我不能使用 gitignore。但现在我不想允许任何人更改此文件(提交它),因为它会破坏某些内容。我想使用本地钩子。
我怎样才能获得提交中的特殊文件?
您能给我一个如何实现这一目标的指示吗?
感谢您的帮助。
A pre-commit
sample:
#!/bin/bash
ipath="foo/bar.json"
git diff --cached --name-only | if grep -qE "^$ipath$";then
git reset HEAD -- "$ipath"
echo "Warning: $ipath is removed from the index. It's not allowed to be committed."
fi
git diff --cached --name-only
列出所有将要提交的已更改文件。如果foo/bar.json
在列表中找到,那么git reset HEAD -- foo/bar.json
将其从索引中删除,以便它不会被提交,而是保留在工作树中。
它对你来说效果很好。但是你不能确保它对其他人也有效。例如,其他贡献者可能会从其本地存储库中删除它。你需要的是一个pre-receive
挂钩在中央存储库中,即服务器端的存储库中。如果传入的提交触摸,它会拒绝任何推送foo/bar.json
.
A pre-receive
sample:
#!/bin/bash
ipath="foo/bar.json"
zero="0000000000000000000000000000000000000000"
while read old new name;do
if [ "$zero" == "$old" ];then
#creating new ref, do something here
continue
fi
if [ "$zero" == "$new" ];then
#deleting a ref, do something here
continue
fi
#updating a ref, check if the incoming commits touch `foo/bar.json`
git diff $old..$new --name-only | if grep -qE "^$ipath$";then
c=$(git log $old..$new --pretty=%H -- $ipath)
echo "Error: $ipath is changed in:"
echo $c
echo "Error: $ipath is not allowed to be committed and pushed."
exit 1
fi
done
贡献者收到错误消息后git push
。他们必须修改他们的提交并删除foo/bar.json
在再次尝试之前。在pre-receive
,你需要处理deleting a ref
and creating a ref
如果需要的话。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)