Skip to content

Hook_commit msg

IceNature edited this page Jul 15, 2016 · 3 revisions

Commit 验证钩子

将下面的代码复制到:<项目根目录>/.git/hooks/commit-msg 文件中(没有则创建)

#!/bin/bash

commit_message=$(cat $1)
commit_files=($(git diff --name-only --cached))
if [ ${#commit_files[@]} -ne 1 ]; then
    echo -e "Add more than one files!\nAbort commit!"
    exit 1
fi

commit_file=`basename ${commit_files[0]}`
regex="^Update [^ ]+ ~ ${commit_file}$"

if [[ "${commit_message}" =~ ${regex} ]]; then
    exit 0
fi
echo -e "Commit messgae format is incorrect!\nAbort commit!"
exit 1

钩子将会在提交时检查提交信息(格式参见 README)以及提交包含的文件个数。若文件个数大于 1 或提交信息格式不正确,则会终止提交
注:如果需要使用非标准提交信息格式,请在提交时添加 --no-verify 参数。

Clone this wiki locally