Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

regex - Parse branch name, initiate commit with name in the commit message

My team uses a common naming convention for branch names, which include the Jira task number in the branch name.

feature/ACD-1664_update-api-call

feature/VZ-1943_new-provider-template

hotfix/RV-977_fix-loading-issue


I want to create a git alias that will automatically stub out a commit message which includes the Jira task number. Ideally some bash script that will parse the branch name and echo out the commit -m command with the first part of the message pre-created.

  1. I need to regex out the commmit message.

I need to pull ACD-1664 from feature/ACD-1664_update-api-call

  1. Echo this string out into the terminal in a stubbed-out commit command like:

git commit -m "ACD-1664 | <cursor>"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Although this is not the solution you requested, I'd like to hint at another way to cover this, with a commit hook :

You can put in .git/hooks a commit-msg file with these contents :

#!/bin/bash
current_branch="$(git rev-parse --abbrev-ref HEAD)"
tmp=$(mktemp) || exit
echo "$current_branch $(cat "$1")" > "$tmp"
mv "$tmp" "$1"

(Thanks guys for the improvements in bash syntax made with your help here)

Then it would automatically prepend your commit messages with the branch name, which does the trick in JIRA.

For the rare occasions when you'd prefer NOT to trigger the hook, do this :

git commit -n -m"Your message"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...