0

I have the following bash script.

The problem I try to solve is really easy. When a commit in Git no starts with gh-1234 ... it should fail.

What is wrong with this bash script?

commit_regex='(gh-[0-9]+|merge)'

error_msg="Aborting commit. Your commit message is missing either a Github Issue ('gh-1111') or 'Merge'."

if ! grep -q "$commit_regex" <<< "$1"; then
    echo "$error_msg" >&2
    exit 1
fi
1
  • 2
    I think you want grep -E? Commented Sep 29, 2016 at 10:37

2 Answers 2

1

As mentioned in the comments, you need to do grep -E "$commit_regex"

From the man grep page-

-E, --extended- Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)

That should solve your problem since it forces grep to expand the variable.

Sign up to request clarification or add additional context in comments.

Comments

0

Also another way to achieve the same without grep :

if [[ $git_message=~ $commit_regex ]] 
then
    echo "$error_msg"
    exit 1;
fi

Also : If the commit it "gh-1234" is mandated to be at the start of the message , you should add ^ in your regex.

2 Comments

O, but I don't want to check the $error_msg agains the regex. I would like to check the git commit
Yeah , of course , it would work against the git commit also if the commit has chars in the form of gh-1234.I just took the error_msg as an example as it too had the "gh-1111" entry.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.