»
(008) CubeIDE Implementation of USB Peripherals*
(018) C99 pointer*
(019) C99 With only value assignment*
(022) POSIX pthread multi-threads mutex*
(023) POSIX pthread multi-threads programming*
(027) C99 time and wait*
(028) High-precision calculation with GMP*
(029) Web Service Lib*
(030) International Components for Unicode (ICU)*
(031) Performance of C is higher than C++, because...*
🍓(014) Ban Git reset --hard
Git禁止reset --hard
cd /path/to/repo.git/hooks
touch pre-receive
chmod +x pre-receive
#!/bin/bash
#oldrev是服务器(远程仓库)中当前的分支的当前提交哈希
#newrev是推送者希望远程更新到仓库中去的提交哈希
# 读取标准输入,获取引用更新信息
while read old_ref new_ref ref_name; do
echo "Received push:"
echo "Old ref: $old_ref"
echo "New ref: $new_ref"
echo "Ref name: $ref_name"
# 检查是否是为第一次推送(oldrev 为 0000000000000000000000000000000000000000)
if [ "$old_ref" = "0000000000000000000000000000000000000000" ]; then
echo "Allow first push: $refname"
exit 0
fi
# 检查是否是删除分支(oldrev 存在,但 newrev 为 0000000000000000000000000000000000000000)
if [ "$new_ref" = "0000000000000000000000000000000000000000" ]; then
echo "ERROR: Deleting branches is not allowed."
exit 1
fi
# 检查是否是强制推送(newrev 存在但 oldrev 不是它的祖先)
if ! git merge-base --is-ancestor "$old_ref" "$new_ref"; then
echo "force push is not allowed: $refname"
exit 1
fi
# 检查是否包含 reset 操作
if [[ "$old_ref" != "0000000000000000000000000000000000000000" && "$new_ref" != "0000000000000000000000000000000000000000" ]]; then
commits=$(git rev-list $old_ref..$new_ref)
for commit in $commits; do
if git log -1 --pretty=%B $commit | grep -q "git reset"; then
echo "ERROR: Push contains 'git reset' operation in commit $commit."
exit 1
fi
done
fi
done
# 如果未检测到不允许的情况,则允许推送
exit 0
来自:DeepSeek综合ChatGPT