簡體   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