禁止在git中删除Master分支

时间:2010-01-07 17:58:38

标签: perl git github hook githooks

我正在尝试设置一个git挂钩,禁止任何人删除我们存储库的master,alpha和beta分支。有人能帮忙吗?我从来没有做过一个git hook,所以我不想在没有一点帮助的情况下尝试自己开发自己的运气。

提前致谢。

2 个答案:

答案 0 :(得分:7)

如果您乐意通过'push'拒绝所有分支删除,那么您只需将存储库中的配置变量receive.denyDeletes设置为true

如果您确实需要更复杂的控制,我建议您查看git发行版update-paranoid文件夹中的contrib/hooks挂钩。它允许你设置每个参考,它可以做一些事情,比如拒绝非快进和拒绝通过推送删除以及一些更复杂的行为。

update-paranoid应该做你需要的一切,而不必自己编写钩子。

答案 1 :(得分:7)

使用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;