简体   繁体   中英

Why do git commit-msg on Windows not match regex

I am trying to create a git commit-msg hook, which includes a simple regex to verify the log message. It is not working using the usual bash syntax.

I tried it using a very simple regex, and it is still not working

Any idea what I might be doing wrong?

Here is a simple example including an obvious match, but it is printing out "no match"

#!/bin/bash
message="[GLSD-0000]"
echo "Message: '$message'"
regex="\[GLSD-\w+\]"
if [[ $message =~ $regex ]]; then echo "yes";else echo "no match!"; fi

Using git version 2.18.0.windows.1

A few things to note from you attempt made,

  1. Your interpreter does #!/bin/sh does not support the regex operation in bash , even-though you have not used it. You should be using the bourne again shell bash ( #!/usr/bin/env bash) .
  2. The [[..]] with an = does a string match on left side operand with the right side operand. For regular expression support, you need to use the ~ operator
  3. The bash only supports a minimal version of ERE (Extended Regular expression) in which \\w (all words) is not supported.

So in a script targeted for a bash shell, you should be writing it as

#!/usr/bin/env bash

# equivalent of '\w' in the regex flavor supported by bash
re='\[GLSD-[A-Za-z0-9_]+\]'
msg='[GLSD-0000]'
[[ $msg =~ $re ]] && echo "yes" || echo "no match!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM