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>
                    <phase>generate-sourcesphase>
                    <goals>
                        <goal>rungoal>
                    goals>
                    <configuration>
                        <tasks>
                            
                            <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="**/*"/>
                        tasks>
                    configuration>
                execution>
            executions>
        plugin>

插件会自动输出 commit-msgpre-push 两个脚本文件


(END)

你可能感兴趣的:(maven,git)