不允许在git中删除主分支

问题描述 投票:8回答:2

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

git perl githooks
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发行版的update-paranoid文件夹中的contrib/hooks钩子。它允许您设置每个引用访问权限,可以执行诸如拒绝非快速转发和通过推送以及某些更复杂行为拒绝删除的操作。

update-paranoid应该完成您需要的所有事情,而不必编写自己的钩子。

© www.soinside.com 2019 - 2024. All rights reserved.