I am trying to atomatize my git commits with bash functions so that they add a text before each commit if they detect that they are on a branch with a jira ticket.
For example
I am working on a new issue and the branch is called
bugfix/MR2-71-sidebar-modification
where MR2-71
is the jira ticket number. I would like to find a general way to find as many cases as possible.
The bash functions I have are as follows
function getIssueName() {
local string="$(git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')"
# Extract the issue name from the branch name with regex without perl
# For example extract MR2-16 of the string feature/MR2-16-requests-list
# string variable will be feature/MR2-16-requests-list
# issueName variable will be MR2-123 only
local issueName="$(echo "$string" | sed -e 's/feature\/\(.*\)-.*/\1/' -e 's/bugfix\/\(.*\)-.*/\1/' -e 's/hotfix\/\(.*\)-.*/\1/' -e 's/release\/\(.*\)-.*/\1/' -e 's/\(.*\)-.*/\1/')"
# if issueName is empty or equal to string then return empty
if [ -z "$issueName" ] || [ "$issueName" = "$string" ]; then
echo ""
else
echo "$issueName"
fi
}
but issueName
variable does not seem to have the ticket number correctly, as it sometimes brings extra string.
for example MR2-71-sidebar-mod
or MR2-75-table
Example content of the string
variable (one for each line):
bugfix/MR2-38-refactor-routes-for-requests-list
bugfix/MR2-39-default-sorting-order-in-requests-list
feature/MR2-17-feature-clients-list
feature/MR2-34-laravel-9-upgrade
Example of the result that the function getIssueName
should return (one for each line):
MR2-38
MR2-39
MR2-17
MR2-34
Any idea how to extract the issue number and make the function work in any case?
For example something like [aA-zZZ]-[0-9]
(sorry I know almost nothing about regex)
Using sed
$ sed 's|[^/]*/\([^-]*-[^-]*\).*|\1|' <<< "$string"
MR2-38
MR2-39
MR2-17
MR2-34