当前位置:首页 > 编程笔记 > 正文
已解决

maven 项目添加 git-hook 脚本,约束提交内容格式

来自网友在路上 181881提问 提问时间:2023-11-08 18:36:12阅读次数: 81

最佳答案 问答题库818位专家为你答疑解惑

git 提交代码,推送代码,可以通过在 .git/hooks 目录中的 bash 脚本来做一定的验证工作。

本例使用插件 maven-antrun-plugin 自动输出脚本至 .git/hooks 目录中,在 pom.xml 中的使用示例如下:

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><executions><execution><id>gen-gitbash-clean</id><phase>clean</phase><goals><goal>run</goal></goals></execution><execution><id>gen-gitbash-compile</id><phase>compile</phase><goals><goal>run</goal></goals></execution></executions><!-- configuration没有放在execution里面是让多个execution都能执行 --><configuration><target><!-- 生成commit-msg文件 --><echo file="${basedir}/.git/hooks/commit-msg"message="#!/usr/bin/env bash${line.separator}${line.separator}"/><echo file="${basedir}/.git/hooks/commit-msg" append="true"message="# Color codes${line.separator}red='\033[0;31m'${line.separator}yellow='\033[0;33m'${line.separator}blue='\033[0;34m'${line.separator}NC='\033[0m' # No colors"/><echo file="${basedir}/.git/hooks/commit-msg" append="true"message="${line.separator}${line.separator}"/><echo file="${basedir}/.git/hooks/commit-msg" append="true"message="# Regex &amp; Usage${line.separator}commit_regex=&quot;^(\[[A-Za-z]+\-[0-9]+\]|merge|Merge)\s&quot;${line.separator}error_message=&quot;Aborting commit. Commit message must start with ${yellow} 开发任务编号&quot;${line.separator}usage=&quot;开发任务编号 Commit Message. \n${blue}示例: [TASK-101] 完成A功能开发&quot;"/><echo file="${basedir}/.git/hooks/commit-msg" append="true"message="${line.separator}${line.separator}"/><echo file="${basedir}/.git/hooks/commit-msg" append="true"message="if ! grep -qE &quot;$commit_regex&quot; &quot;$1&quot;; then${line.separator}"/><echo file="${basedir}/.git/hooks/commit-msg" append="true"message="  printf &quot;${red}$error_message\n&quot;${line.separator}"/><echo file="${basedir}/.git/hooks/commit-msg" append="true"message="  printf &quot;${yellow}Commit Regex: ${NC}${commit_regex}\n&quot;${line.separator}"/><echo file="${basedir}/.git/hooks/commit-msg" append="true"message="  printf &quot;${red}Usage: ${NC}${usage}\n&quot;${line.separator}"/><echo file="${basedir}/.git/hooks/commit-msg" append="true"message="  exit 1${line.separator}fi"/><!-- 生成pre-push文件 --><echo file="${basedir}/.git/hooks/pre-push"message="#!/usr/bin/env bash${line.separator}${line.separator}"/><echo file="${basedir}/.git/hooks/pre-push" append="true"message="# Color codes${line.separator}red='\033[0;31m'${line.separator}yellow='\033[0;33m'"/><echo file="${basedir}/.git/hooks/pre-push" append="true"message="${line.separator}${line.separator}"/><echo file="${basedir}/.git/hooks/pre-push" append="true"message="command=&quot;./mvnw.cmd clean test&quot;${line.separator}${command}"/><echo file="${basedir}/.git/hooks/pre-push" append="true"message="${line.separator}${line.separator}"/><echo file="${basedir}/.git/hooks/pre-push" append="true"message="if ! [ $? -eq 0 ]; then${line.separator}"/><echo file="${basedir}/.git/hooks/pre-push" append="true"message="  printf &quot;${red}Make sure successfully execute command: ${yellow}$command${red}.&quot;${line.separator}"/><echo file="${basedir}/.git/hooks/pre-push" append="true"message="  exit 1${line.separator}fi"/><!-- 设置权限 --><chmod dir="${basedir}/.git/hooks" perm="755" includes="**/*"/></target></configuration></plugin>

在执行maven的 clean 或者 compile 时候,插件会自动输出 commit-msgpre-push 两个脚本文件


(END)

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"maven 项目添加 git-hook 脚本,约束提交内容格式":http://eshow365.cn/6-35494-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!