且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

禁用标签删除

更新时间:2023-12-05 10:32:16

git help hooks 包含有关 hooks 的文档.当 Git 即将创建/移动/删除引用时,会调用 update 挂钩.每个要更新的引用调用一次,并给出:

git help hooks contains documentation about the hooks. The update hook is invoked when Git is about to create/move/delete a reference. It is called once per reference to be updated, and is given:

  • 第一个参数:引用名称(例如,refs/tags/v1.0)
  • 第二个参数:引用当前指向的对象的 SHA1(如果引用当前不存在,则全为零)
  • 第三个参数:用户希望引用指向的对象的 SHA1(如果要删除引用,则全为零).

如果钩子以非零退出代码退出,git 不会更新引用并且用户会收到错误.

If the hook exits with a non-zero exit code, git won't update the reference and the user will get an error.

因此,为了解决您的特定问题,您可以将以下内容添加到您的 update 挂钩中:

So to address your particular problem, you can add the following to your update hook:

#!/bin/sh

log() { printf '%s
' "$*"; }
error() { log "ERROR: $*" >&2; }
fatal() { error "$*"; exit 1; }

case $1 in
    refs/tags/*)
        [ "$3" != 0000000000000000000000000000000000000000 ] 
            || fatal "you're not allowed to delete tags"
        [ "$2" = 0000000000000000000000000000000000000000 ] 
            || fatal "you're not allowed to move tags"
        ;;
esac