不允许在git中删除主分支 [英] Dissallow deletion of Master branch in git

查看:307
本文介绍了不允许在git中删除主分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



预先致谢。

解决方案

直接用预接收钩子。假设您使用的是中央资源库,请将以下代码放在 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; $(b
$ b while(<>){
chomp;
$ b $ next
除非m [^
([0-9a-f] +)#old SHA-1
\ s +
([0 -9a-f] +)#new SHA-1
\s +
refs / heads /(\S +)#ref
\s *
$
] x;
$ b $ my($ old,$ new,$ ref)=($ 1,$ 2,$ 3);

next除非$ ref =〜/ ^(master | alpha | beta)$ /;
$ b $ die$ 0:删除$ ref不允许!\\\

if $ new =〜/ ^ 0 + $ /;
}

exit $ errors == 0? 0:1;


I'm trying to setup a git hook that will disallow anyone to delete the master, alpha, and beta branches of our repository. Can anyone help with this? I have never done a git hook so i don't want to try my luck in developing my own without a little help.

Thanks in advance.

解决方案

Straightforward with a pre-receive hook. Assuming you're using a bare central repository, place the following code in your-repo.git/hooks/pre-receive, and don't forget to 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;

这篇关于不允许在git中删除主分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆