Git - Git hooks 钩子

文章目录

  • 执行顺序
  • 编写脚本
    • 1、pre-commit
    • 2、commit-msg
  • 验证功能

最近在搭建项目工程,为了方便的规范项目成员的commit-msg格式,就简单研究了一下 git hooks
关于钩子的概念,网上很多,可以自行搜索学习,例如:官方文档 自定义 Git - Git 钩子
钩子目录: /.git/hooks

执行顺序

简单打印log信息,查看了一下钩子的执行顺序
Git - Git hooks 钩子_第1张图片

序号 hooks 简述
1 pre-commit 在键入提交信息前运行
2 prepare-commit-msg 在启动提交信息编辑器之前,默认信息被创建之后运行
3 commit-msg 可以用来在提交成功前,验证项目状态或提交信息
4 post-commit 在整个提交过程完成后运行

编写脚本

  • 项目里没有做过多的严格控制,主要是校验团队成员的提交msg规范,方便同组测试、打包成员快速了解代码变更
  • git自带的钩子*.sample模板都是#!/bin/sh,这里自己改成了#!/usr/bin/env bash

1、pre-commit

  • 设计的逻辑:是为了团队成员只需要安装一次pre-commit钩子,以后每次提交前commit-msg都能保证是最新的校验规则
#!/usr/bin/env bash
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

pwd
echo "开始执行:pre-commit"

# 更新commit-msg钩子
# 检查源文件
if [[ ! -f "./pm/git-hooks/commit-msg" ]]; then
    echo "./pm/git-hooks/commit-msg缺失,请检查文件完整性!"
    exit 1
fi

# 检查目标文件
if [[ -f "./.git/hooks/commit-msg" ]]; then
    # 如果文件存在,则覆盖成最新校验规则
    cat ./pm/git-hooks/commit-msg >./.git/hooks/commit-msg
    echo "commit-msg覆盖成功"
else
    # 如果不存在,则复制最新校验规则
    cp ./pm/git-hooks/commit-msg -R ./.git/hooks/
    echo "commit-msg复制成功"
fi

# 提交之前测试打包是否异常
# -Dmaven.test.skip=true 跳过测试用例
mvn -Dmaven.test.skip=true clean package
#判断执行上面mvn操作的返回值是否为0
if [[ $? -ne 0 ]]; then
    echo "打包异常,请检查代码问题!"
    exit 1
fi

2、commit-msg

  • 返回状态非0,则git就会终止commit动作
#!/usr/bin/env bash
#
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message.  The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit.  The hook is allowed to edit the commit message file.
#
# To enable this hook, rename this file to "commit-msg".

# Uncomment the below to add a Signed-off-by line to the message.
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
# hook is more suited to it.
#
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"

# This example catches duplicate Signed-off-by lines.

#test "" = "$(grep '^Signed-off-by: ' "$1" |
#	 sort | uniq -c | sed -e '/^[ 	]*1[ 	]/d')" || {
#	echo >&2 Duplicate Signed-off-by lines.
#	exit 1
#}

echo "开始执行:commit-msg"

# 更新pre-commit钩子
# 检查源文件
if [[ ! -f "./pm/git-hooks/pre-commit" ]]; then
    echo "./pm/git-hooks/pre-commit缺失,请检查文件完整性!"
    exit 1
fi

# 检查目标文件
if [[ -f "./.git/hooks/pre-commit" ]]; then
    # 如果文件存在,则覆盖成最新校验规则
    cat ./pm/git-hooks/pre-commit >./.git/hooks/pre-commit
    echo "pre-commit覆盖成功"
else
    # 如果不存在,则复制最新校验规则
    cp ./pm/git-hooks/pre-commit -R ./.git/hooks/
    echo "pre-commit复制成功"
fi

# 校验提交信息
msg=`awk '{printf("%s",$0)}' $1`

if [[ ${#msg} -lt 16 ]] 
then
    echo "${#msg}个字符太短,不能小于16个, 本次提交失败,请完善commit message再提交"
    exit 1
fi

if [[ ${#msg} -gt 128 ]]  
then
    echo "${#msg}个字符太长,不能大于128个, 本次提交失败,请完善commit message再提交"
    exit 1
fi

caseMsg=$(echo $msg | tr [A-Z] [a-z]) # 大写转小写
msgRE="^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert|polish|types|workflows|merge)"

if [[ ! (${caseMsg} =~ ${msgRE}) ]]
then
    echo "不符合格式规范, 本次提交失败,请参考README.md完善commit message再提交"
    exit 1
fi

验证功能

提交测试一下,执行成功perfect

/home/nangy/IdeaProjects/TestGit
开始执行:pre-commit
commit-msg覆盖成功
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< com.nangy.test:TestGit >----------------------
[INFO] Building TestGit 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
......
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ TestGit ---
[INFO] Building jar: /home/nangy/IdeaProjects/TestGit/target/TestGit-20200512.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  13.553 s
[INFO] Finished at: 2020-05-13T07:57:52+08:00
[INFO] ------------------------------------------------------------------------
开始执行:commit-msg
pre-commit覆盖成功
[master 2273e3c] fix branch 'test' 测试钩子功能

你可能感兴趣的:(Linux)