不允许在git中删除Master分支

Tom*_*ick 8 git perl hook github githooks

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

提前致谢.

CB *_*ley 7

如果你感到快乐,禁止所有的分支机构通过"推"删除那么你可以设置配置变量receive.denyDeletestrue对你的资料库.

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

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


Gre*_*con 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;
Run Code Online (Sandbox Code Playgroud)