简体   繁体   English

在 bash 上使用正则表达式的预钩子 git 消息验证失败失败

[英]Pre-hook git message validation fails using regex on bash fails

I have to validate the structure of a commit message to be consistent with the following structure:我必须验证提交消息的结构与以下结构一致:

<type>(optional description): <subject>

<BLANK LINE>

<optional body>

<BLANK LINE>

<footer>

Type, subject and footer are required.类型、主题和页脚是必需的。

And 'footer' (mandatory) has a structure like:并且“页脚”(强制)具有如下结构:

Closes #S2169505 (https://rally1.rallydev.com/#/?detail=/userstory/123456)

And additionally can have BREAKING CHANGES(optional) like:并且还可以有 BREAKING CHANGES(可选),例如:

BREAKING CHANGE: Something is breaking thanks to this commit.

Examples:例子:

Valid commit message有效的提交信息

feat: Implement retry logic for outbound requests

If outbound requests fail with a transient exception, they will be automatically
retried for a configured number of times. If the exception is not transient, the
request is not retried and the error is returned to the controller.

This should ensure that temporary failures will more likely return success. 

Closes #S2169505 (https://rally1.rallydev.com/#/?detail=/userstory/123456)
BREAKING CHANGE: Something is breaking thanks to this commit.

Another valid commit message:另一个有效的提交信息:

test(some optional thing): Implement retry logic for outbound requests
Closes #S2169505 (https://rally1.rallydev.com/#/?detail=/userstory/123456)
BREAKING CHANGE: Something is breaking thanks to this commit.

Invalid commit messages:无效的提交消息:

feat: Implement retry logic for outbound requests

If outbound requests fail with a transient exception, they will be automatically
retried for a configured number of times. If the exception is not transient, the
request is not retried and the error is returned to the controller.

This should ensure that temporary failures will more likely return success. 

Closes #S2169505 (https://rally1.rallydev.com/#/?detail=/userstory/123456)
THIS TEXT SHOULD NOT BE HERE

Another invalid commit message(it has no footer):另一个无效的提交消息(它没有页脚):

feat: Implement retry logic for outbound requests

I created a #bash script to validate the message:我创建了一个#bash 脚本来验证消息:

#!/bin/sh

msg="" #THE MENTIONED COMMIT MESSAGE
regExp='^(test|feat)(\(.+\))?:(.+)((.|\n)*)?(Closes #.+ \(.+\))([\n]*BREAKING CHANGE:.+)?$'

if [[ $msg =~ $regExp ]];
then
    echo "cool"
else
    echo "NOT cool"
fi

exit 1

my regex contains the following structure:我的正则表达式包含以下结构:

^ Init of the message ^消息的初始化

(test|feat) 'type' (test|feat) “类型”

(\(.+\))? optional message in parenthesis括号中的可选消息

: colon separator between 'type' and 'subject' : “类型”和“主题”之间的冒号分隔符

(.+) subject (.+)主题

((.|\n)*)? optional 'body'可选的“身体”

(Closes #.+ \(.+\)) mandatory footer (Closes #.+ \(.+\))强制页脚

([\n]*BREAKING CHANGE:.+)? optional extra footer可选的额外页脚

$ End of the message $消息结束

Unfortunately my script is not working(printing NOT cool ).不幸的是我的脚本不工作(打印NOT cool )。 If I omit the end of the message symbol on the regex ( $ ) the script works (prints 'cool') but then it returns also success for the 3rd example which is invalid since after the Closes footer cannot exist more content (unless is a breaking change).如果我在正则表达式( $ )上省略了消息符号的结尾,则脚本可以工作(打印“酷”),但它也会返回第三个示例的成功,这是无效的,因为在Closes页脚之后不能存在更多内容(除非是突破性变化)。

Additionally : I'm testing this running the script on the console, is there any way to print what the regex is selecting(and not selecting) from the provided message?另外:我正在测试这个在控制台上运行脚本,有没有办法从提供的消息中打印正则表达式正在选择(而不是选择)的内容?

I found out the solution;我找到了解决方案; Bash/shell uses 'ERE' and instead of using character like ' ' or '\n' I replaced them by [:blank:] [:space:] and others, like specified here Bash/shell 使用 'ERE' 而不是使用像 ' ' 或 '\n' 这样的字符,我将它们替换为[:blank:] [:space:]和其他字符,如指定here

Special thanks to @markp-fuso, sir, your response indicating how to check the matched regex solved all my issues (I was able to "debug" my regex)特别感谢@markp-fuso,先生,您的回复表明如何检查匹配的正则表达式解决了我所有的问题(我能够“调试”我的正则表达式)

My final regex is:我的最终正则表达式是:

regExp='^(build|ci|docs|feat|fix|perf|refactor|style|test)(\(.+\))?:[[:blank:]]([[:alnum:][:blank:][:punct:]]+)[[:space:]]+([[:space:][:punct:][:alnum:]]*)(Closes[[:blank:]]#[[:alnum:]]+[[:blank:]]\([[:alnum:][:punct:]]+)[[:space:]]?(BREAKING[[:blank:]]CHANGE:[[:blank:]][[:blank:][:alnum:][:punct:]]+)?$'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM