Dissallow deletion of Master branch in git

送分小仙女□ 提交于 2019-12-04 11:08:00

问题


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.


回答1:


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;



回答2:


If you're happy to deny all branch deletes via 'push' then you can just set the config variable receive.denyDeletes to true on your repository.

If you do need more sophisticated control I recommend that you take a look at the update-paranoid hook from the git distribution's contrib/hooks folder. It allows you to set up per ref acls which can do things like deny non fast-forwards and deny deletes via push as well as some more sophisticated behaviours.

update-paranoid should do everything you need without you having to write your own hook.



来源:https://stackoverflow.com/questions/2022438/dissallow-deletion-of-master-branch-in-git

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!