我正在尝试设置一个git钩子,以防止任何人删除我们仓库的master
、alpha
和beta
分支。有人能帮忙吗?我从未做过git钩子,所以我不想在没有一点帮助的情况下自己开发。
使用 pre-receive
钩子非常简单。假设您正在使用一个裸仓库,将以下代码放置在 your-repo.git/hooks/pre-receive
中,并不要忘记运行 chmod +x your-repo.git/hooks/pre-receive
。
#! /usr/bin/perl
# create: 00000... 51b8d... refs/heads/topic/gbacon
# delete: 51b8d... 00000... refs/heads/topic/gbacon
# update: 51b8d... d5e14... refs/heads/topic/gbacon
my $errors = 0;
while (<>) {
chomp;
next
unless m[ ^
([0-9a-f]+) # old SHA-1
\s+
([0-9a-f]+) # new SHA-1
\s+
refs/heads/(\S+) # ref
\s*
$
]x;
my($old,$new,$ref) = ($1,$2,$3);
next unless $ref =~ /^(master|alpha|beta)$/;
die "$0: deleting $ref not permitted!\n"
if $new =~ /^0+$/;
}
exit $errors == 0 ? 0 : 1;
receive.denyDeletes
设置为true
。contrib/hooks
文件夹中的update-paranoid
钩子。它允许您设置每个参考的acl,可以执行诸如拒绝非快进和通过推送拒绝删除等更复杂的行为。
update-paranoid
应该能够满足您的所有需求,而无需编写自己的钩子。