使用部分分支名称预先安装Git commit消息 [英] Prepend Git commit message with partial branch name
问题描述
我当前的分支命名约定是这样的:
ticket-45-my-new-feature-branch-description
我目前在我的.git / hooks / prepare-commit-msg文件中使用这段代码,使用分支名称提交消息:
BRANCH_NAME = $(git branch 2> / dev / null | grep -e ^ * | tr -d'*')
if [-n$ BRANCH_NAME]&& [$ BRANCH_NAME!=master];然后
回显[$ BRANCH_NAME] $(cat $ 1)>
$ b
最终结果:
[ticket-45-my-new-feature-branch-description] test commit
我试图完成的输出是这样的:
[ticket-45]测试提交
布朗尼指出,如果我们能够利用它:
test
$ b我会爱保留我的描述性分支名称,但在提交消息中截断前缀文本。我相信我必须使用一些正则表达式,但我真的不知道如何实现这一点。我应该提到我们有几个项目同时进行,所以分支名称是不同的,如下所示:
ticket-123-分行名称
abc-22-my-branch
ua-11-my-feature
$ b $
任何帮助都非常感谢!!!
好吧,首先,这是:
BRANCH_NAME = $(git branch 2> / dev / null | grep -e ^ * | tr -d'*')
$
$ b
branch = $(git symbolic-ref --short HEAD)
是主要的矫枉过正:-)使用:
)|| ...
获取当前分支名称。 ||
之后的部分是如果你不在分支上怎么办(即,如果你处于分离头部模式) - 你会必须自己决定。 (您的当前代码将BRANCH_NAME设置为空字符串;要做到这一点,您甚至不需要 ||
部分,但可能需要添加 -q
或者 2> / dev / null
,以避免来自symbolic-ref的fatal:消息。)
剩下的只是基本的脚本。在bash中,您可以直接使用regex,在旧的sh中您可以调用 expr
或 sed
。 sed
和 tr
都可以大写,但sed也可以做一个正则表达式,所以它看起来像一个好的候选人:
$ trimmed = $(echo $ branch | sed -e's:^ \([^ - ] * - [^ - ] * \) - 。*:\ 1:'-e \
'y / abcdefghijklmnopqrstuvwxyz / ABCDEFGHIJKLMNOPQRSTUVWXYZ /')
$ echo $ trimmed
TICKET- 45
最后,这样做有点危险:
echosome stuff $(cat $ 1)> $ 1
当你依赖shell来扩展 $(cat $ 1)
,然后截断>的输出文件。 $ 1
部分。 (显然它可以工作,但是你会受到shell变幻莫测的影响。)最好使用一个临时文件,或者可能是另一个 sed
,但是就地:
sed -i .bak -e1s:^:[$ trimmed]:$ 1
#或使用-i'',但注意在sed手册页中的次要警告
以上只是零碎的测试,但应该工作。 >
My current branch naming convention is as such:
ticket-45-my-new-feature-branch-description
I am currently using this code in my .git/hooks/prepare-commit-msg file to prepend every commit message with the branch name as such:
BRANCH_NAME=$(git branch 2>/dev/null | grep -e ^* | tr -d ' *')
if [ -n "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "master" ]; then
echo "[$BRANCH_NAME] $(cat $1)" > $1
fi
End result:
[ticket-45-my-new-feature-branch-description] test commit
What I'm trying to accomplish is output like this:
[ticket-45] test commit
Brownie points if we can capitalize it:
[TICKET-45] test commit
I would love to keep my descriptive branch names, but truncate the prepended text in commit messages. I'm sure I have to use some regex, but I really don't know how to accomplish that. I should mention that we have several projects going on at once, so branch names are different, like so:
ticket-123-branch-name
abc-22-my-branch
ua-11-my-feature
The only thing in common is the fact that I need everything before the second '-'.
Any help is greatly appreciated!!!
Ok, first, this:
BRANCH_NAME=$(git branch 2>/dev/null | grep -e ^* | tr -d ' *')
is major overkill :-) Use:
branch=$(git symbolic-ref --short HEAD) || ...
to get the current branch name. The part after ||
is "what to do if you're not on a branch" (i.e., if you're in "detached head" mode)—you'll have to decide that for yourself. (Your current code sets BRANCH_NAME to the empty string; to do that, you don't even need the ||
part, but you may want to add -q
, or a 2>/dev/null
, to avoid the "fatal:" message from symbolic-ref.)
The rest is just basic scripting. In bash you can use regex's directly, in old sh you can invoke expr
or sed
. Both sed
and tr
can upper-case-ify, but sed can do a regex too, so it looks like a good candidate:
$ trimmed=$(echo $branch | sed -e 's:^\([^-]*-[^-]*\)-.*:\1:' -e \
'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/')
$ echo $trimmed
TICKET-45
Last, it's slightly dangerous to do:
echo "some stuff $(cat $1)" > $1
as you're depending on the shell to expand the $(cat $1)
before it truncates the output file for the > $1
part. (Obviously it works, but you're subject to shell vagaries.) Better to use a temporary file, or maybe another sed
but in-place:
sed -i .bak -e "1s:^:[$trimmed] :" $1
# or use -i '', but note minor warning in sed man pages
The above is only tested piecemeal, but should work.
这篇关于使用部分分支名称预先安装Git commit消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!