禁止删除 git 中的 master 分支

发布于 2024-08-17 06:36:56 字数 164 浏览 13 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛滥成性 2024-08-24 06:36:56

使用 pre-receive 钩子就可以直接实现。假设您使用的是裸露的中央存储库,请将以下代码放入 your-repo.git/hooks/pre-receive 中,并且不要忘记chmod +x your-repo。 git/hooks/预接收

#! /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;

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;
浅黛梨妆こ 2024-08-24 06:36:56

如果您愿意通过“推送”拒绝所有分支删除,那么您只需在存储库上将配置变量 receive.denyDeletes 设置为 true 即可。

如果您确实需要更复杂的控制,我建议您查看 git 发行版的 contrib/hooks 文件夹中的 update-paranoid 挂钩。它允许您设置每个引用 acl,它可以执行诸如拒绝非快进和拒绝通过推送删除以及一些更复杂的行为之类的操作。

update-paranoid 应该可以完成您需要的一切,而无需编写自己的钩子。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文