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


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

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

    
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-antrun-pluginartifactId>
                    <executions>
                        <execution>
                            <id>gen-gitbash-cleanid>
                            <phase>cleanphase>
                            <goals>
                                <goal>rungoal>
                            goals>
                        execution>
                        <execution>
                            <id>gen-gitbash-compileid>
                            <phase>compilephase>
                            <goals>
                                <goal>rungoal>
                            goals>
                        execution>
                    executions>
                    
                    <configuration>
                        <target>
                            
                            <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 & Usage${line.separator}commit_regex="^(\[[A-Za-z]+\-[0-9]+\]|merge|Merge)\s"${line.separator}error_message="Aborting commit. Commit message must start with ${yellow} 开发任务编号"${line.separator}usage="开发任务编号 Commit Message. \n${blue}示例: [TASK-101] 完成A功能开发""/>
                            <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 "$commit_regex" "$1"; then${line.separator}"/>
                            <echo file="${basedir}/.git/hooks/commit-msg" append="true"
                                  message="  printf "${red}$error_message\n"${line.separator}"/>
                            <echo file="${basedir}/.git/hooks/commit-msg" append="true"
                                  message="  printf "${yellow}Commit Regex: ${NC}${commit_regex}\n"${line.separator}"/>
                            <echo file="${basedir}/.git/hooks/commit-msg" append="true"
                                  message="  printf "${red}Usage: ${NC}${usage}\n"${line.separator}"/>
                            <echo file="${basedir}/.git/hooks/commit-msg" append="true"
                                  message="  exit 1${line.separator}fi"/>
    
                            
                            <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="./mvnw.cmd clean test"${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 "${red}Make sure successfully execute command: ${yellow}$command${red}."${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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

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


    (END)

  • 相关阅读:
    【MySQL】库的操作
    c++程序
    Java多线程及原理
    【Kafka】Kafka的重复消费和消息丢失问题
    【强化学习论文合集 | 2021年合集】七. ICRA-2021 强化学习论文
    Linux系统(Centos 8)环境安装
    OneFlow源码解析:静态图与运行时
    如何实现浏览器标签页之间的通信
    TypeScript的配置文件tsconfig.json
    集成底座方案演示说明
  • 原文地址:https://blog.csdn.net/catoop/article/details/134249549