【问题标题】:Git commit-msg hook: prefix commit based on part of branch nameGit commit-msg hook:基于部分分支名称的前缀提交
【发布时间】:2017-03-16 00:34:10
【问题描述】:

我的分支名称模式如下 ticketId_ticketDescription 例如 MIT-1018-make-goto-redirection-smarter

所以我希望每个提交都以ticketId为前缀,在这种情况下它是 [MIT-1018]

所以如果我提交

git commit -am"这是一条提交信息" 所以消息应该是“MIT-1018: This is a commit message

这是我的尝试

#!/bin/bash
ticket=`git rev-parse --abbrev-ref HEAD | sed -e 's/MIT-[0-9]+//'`
echo $ticket
comment=`cat $1`
search=`grep "$ticket" $1`
if [ -n "$ticket" ] && [ -z "$search" ]
then
echo "$ticket: $comment" > $1
fi

但这会将整个分支名称添加为前缀,而不仅仅是 TicketId

【问题讨论】:

  • 你的例子不清楚,你能清楚地说明你需要的样本输入和预期输出吗?你对脚本“$1”的论点是什么?
  • TicketId 字符串是否有固定长度的字符? 3 字符后跟4 数字?
  • @Inian 我刚刚在描述中突出显示了它 git commit -am"This is a commit message" 所以消息应该是 "MIT-1018: This is a commit message"
  • @Inian 是的,它总是以 开头,然后是任意数量的 4 或更多或更少的数字
  • 请参考下面我的回答,一旦您发现它解决了您的问题,请不要忘记接受/支持该解决方案。

标签: regex git shell githooks


【解决方案1】:

您可以将regex in bash 与字符类、[[:alnum:]][[:digit:]] 一起使用,以根据需要提取票证标识符。

$ ticket="$(git rev-parse --abbrev-ref HEAD)"
$ commitMessage="This is a commit message"

# Assuming the variable has the string "MIT-1018-make-goto-redirection-smarter"
# ticket="MIT-1018-make-goto-redirection-smarter"

$ [[ $ticket =~ (([[:alnum:]]{3})-([[:digit:]]{3,})).* ]] && ticketID=${BASH_REMATCH[1]}
$ printf "%s\n" "$ticketID: $commitMessage"
MIT-1018: This is a commit message

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2019-11-09
    • 1970-01-01
    • 2018-10-27
    • 2012-08-05
    • 2014-02-10
    • 2021-08-07
    相关资源
    最近更新 更多