【问题标题】:Bypass pre-receive hook if branch already exist (pushed)如果分支已经存在,则绕过预接收钩子(推送)
【发布时间】:2020-08-06 16:35:28
【问题描述】:

我有一个 pre-receive 挂钩来检查运行 Git 项目的命名约定:

valid_branch_regex="^(master|release-[0-9]{4}-[0-9]{2}|[A-Z|0-9]{3,6}-[0-9]+-.*)"

while read oldrev newrev refname; do
    echo "$refname : $oldrev ~ $newrev"
    current_branch=$refname
    short_current_branch="$(echo $current_branch | sed 's/refs\/heads\///g')"
done

message="There is something wrong with your branch name. Branch names in this project must adhere to this contract:\
 $valid_branch_regex. Your commit will be rejected. You should rename your branch to a valid name and try again."

if [[ ! $short_current_branch =~ $valid_branch_regex ]]
then
    echo "$message"
    exit 1
fi

exit 0

问题是我想绕过在应用脚本之前推送的分支。有什么想法可以改善我目前的逻辑吗?

谢谢!

【问题讨论】:

    标签: bash github git-bash


    【解决方案1】:

    这可能是这个问题的解决方案:

    valid_branch_regex="^(master|release-[0-9]{4}-[0-9]{2}|[A-Z|0-9]{3,6}-[0-9]+-.*)"
    
    zero_commit="0000000000000000000000000000000000000000"
    message="There is something wrong with your branch name. Branch names in this project must adhere to this contract: \
    $valid_branch_regex. Your commit will be rejected. You should rename your branch to a valid name and try again."
    while read oldrev newrev refname; do
        echo "$refname : $oldrev ~ $newrev"
        current_branch=$refname
        short_current_branch="$(echo $current_branch | sed 's/refs\/heads\///g')"
    done
    
    message="There is something wrong with your branch name. Branch names in this project must adhere to this contract:\
     $valid_branch_regex. Your commit will be rejected. You should rename your branch to a valid name and try again."
        # Check for new branch or tag
        if [ "$oldrev" == "$zero_commit" ]; then
            short_current_branch="$(echo $current_branch | sed 's/refs\/heads\///g')"
        else
            echo "This is an existing branch, wont check the naming convention. \
    Please be aware that the branch name should follow this contract: $valid_branch_regex"
            exit 0
        fi
    
    if [[ ! $short_current_branch =~ $valid_branch_regex ]]
    then
        echo "$message"
        exit 1
    fi
    
    exit 0 
        if [[ ! $short_current_branch =~ $valid_branch_regex ]]
        then
            echo "$message"
            exit 1
        fi
    
    done
    exit 0

    【讨论】:

      猜你喜欢
      • 2014-10-15
      • 2014-03-15
      • 2023-01-12
      • 2019-05-04
      • 1970-01-01
      • 2015-04-03
      • 2016-05-30
      • 1970-01-01
      • 2022-08-17
      相关资源
      最近更新 更多