禁止在git中删除主分支

8

我正在尝试设置一个git钩子,以防止任何人删除我们仓库的masteralphabeta分支。有人能帮忙吗?我从未做过git钩子,所以我不想在没有一点帮助的情况下自己开发。

2个回答

8

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

7
如果您想通过“push”拒绝所有分支删除操作,您可以在存储库上将配置变量receive.denyDeletes设置为true
如果您需要更复杂的控制,我建议您查看git分发的contrib/hooks文件夹中的update-paranoid钩子。它允许您设置每个参考的acl,可以执行诸如拒绝非快进和通过推送拒绝删除等更复杂的行为。 update-paranoid应该能够满足您的所有需求,而无需编写自己的钩子。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接