繁体   English   中英

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

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

我必须验证提交消息的结构与以下结构一致:

<type>(optional description): <subject>

<BLANK LINE>

<optional body>

<BLANK LINE>

<footer>

类型、主题和页脚是必需的。

并且“页脚”(强制)具有如下结构:

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

并且还可以有 BREAKING CHANGES(可选),例如:

BREAKING CHANGE: Something is breaking thanks to this commit.

例子:

有效的提交信息

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.

另一个有效的提交信息:

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.

无效的提交消息:

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

另一个无效的提交消息(它没有页脚):

feat: Implement retry logic for outbound requests

我创建了一个#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

我的正则表达式包含以下结构:

^消息的初始化

(test|feat) “类型”

(\(.+\))? 括号中的可选消息

: “类型”和“主题”之间的冒号分隔符

(.+)主题

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

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

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

$消息结束

不幸的是我的脚本不工作(打印NOT cool )。 如果我在正则表达式( $ )上省略了消息符号的结尾,则脚本可以工作(打印“酷”),但它也会返回第三个示例的成功,这是无效的,因为在Closes页脚之后不能存在更多内容(除非是突破性变化)。

另外:我正在测试这个在控制台上运行脚本,有没有办法从提供的消息中打印正则表达式正在选择(而不是选择)的内容?

我找到了解决方案; Bash/shell 使用 'ERE' 而不是使用像 ' ' 或 '\n' 这样的字符,我将它们替换为[:blank:] [:space:]和其他字符,如指定here

特别感谢@markp-fuso,先生,您的回复表明如何检查匹配的正则表达式解决了我所有的问题(我能够“调试”我的正则表达式)

我的最终正则表达式是:

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