Git挂钩检查文件名

时间:2017-12-05 08:38:11

标签: git bash githooks

我正在研究一个新的预接收git挂钩,它检查提交的文件名,如果它们不遵守命名约定,则推送停止并且开发人员需要更改文件名。我尝试用这种方式更改文件名:

git mv oldName NewName
git commit --amend
git push -u origin master

这是钩子:

zero_commit="0000000000000000000000000000000000000000"
RED='\033[0;31m'

while read oldrev newrev refname
do

    if [ "$oldrev" = $zero_commit ]
    then
        list_commits=`git rev-list $newrev --not \ $(git for-each-ref refs/heads/ --format='%(refname)')`
    else
        list_commits=`git rev-list $oldrev..$newrev`
    fi

    for commit in $list_commits
    do


            list_files=`git diff-tree --no-commit-id --name-only -r $commit`
            branches=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
            for full_file in $list_files
                do

                file="${full_file##*/}" 
                extension="${file##*.}"
                file="${file%.*}"
                error_upper="${RED} Error: File $full_file  must be in upper case in commit : $commit "
                error_length="${RED} Error: File $full_file is over 40 character in commit : $commit"               
                    error_extension="${RED} Error: $full_file doesn't respect the naming convention please provide a valid file extension (.PSK | .PBK) in commit : $commit "
                    error_sqlext="${RED} Error: $full_file doesn't respect the naming convention please provide a valid file extension (.sql) in commit : $commit"
                    error_FK="${RED} Error: $full_file doesn't respect the naming convention , file sould start with NonFK_ in commit : $commit"            

                    if [[ $full_file == "DB/"* ]]
                    then                    
                    prefix="NonFK_"
                    if [[ $full_file == "DB/Dev_DB_Repository/004_Constraints_NonFK/"* ]] ; then                        
                        if [[ $file == ${file#$prefix} ]]
                        then
                        echo -e "$error_FK" >&2
                                    exit 1;
                        else file=${file#$prefix}
                        fi
                    fi

                    let "chrlen=${#file}"

                    if [ "$chrlen" -gt  "40" ] || [[ "$file" =~ [a-z] ]] 
                    then

                        if [ "$chrlen" -gt  "40" ]
                        then                        
                        echo -e "$error_length" >&2                     
                        fi
                        if [[ "$file" =~ [a-z] ]]
                        then
                        echo -e "$error_upper" >&2
                        fi
                        exit 1;
                    fi  
                        if [[ $full_file == "DB/Dev_DB_Repository/013_Packages/"* ]]  
                        then
                            if  [ "$extension" == "PSK" ] ||  [ "$extension" == "PBK"  ]
                            then
                            echo " Validating Files ..."
                            else 
                            echo -e "$error_extension" >&2
                                exit 1;
                            fi
                        elif [ "$extension" != "sql" ] 
                        then 
                                echo -e "$error_sqlext" >&2
                                exit 1;

                        fi
                    fi                      
                done        
        commit_msg=$(git log --format=%B -n 1 $commit)
        issue_key=$(curl -u name:mdp -X GET -H "Content-Type: application/json" http://127.0.0.1:8080/rest/api/2/issue/$commit_msg | grep -w $commit_msg)            
        issue_status=$(curl -u name:mdp -X GET -H "Content-Type: application/json" http://127.0.0.1:8080/rest/api/2/issue/$commit_msg?fields=status | grep -E "Open|Reopened")
        error_msg="${RED} ERROR: No issue with key '$commit_msg' has been found, please provide a commit message with a valid jira issue."
        status_error="${RED}ERROR: the issue $commit_msg isn't opened"
        if [ -z "$issue_key" ]
        then    
            echo -e "$error_msg" >&2
            exit 1;
        elif [ -z "$issue_status" ]
        then
            echo -e "$status_error" >&2
            exit 1;
        fi

    done

done

这适用于新文件(在重命名git将其视为新文件之后),但如果存储库中已存在我尝试更改挂钩的文件,则会停止它,因为git会考虑新名称和旧名称。有没有办法在Repository中更改文件名而不通过pre-receive hook停止它?

0 个答案:

没有答案