On Thu, 7 Nov 2013 12:14:34 -0500
lingfei ouyang <oylf1...@gmail.com> wrote:

> I'm trying to create an Git-extenstion pre-commit hooks and below is
> what I got:
> ~~~~
> #!/bin/bash -xv
> refname="$1"
> oldrev="$2"
> newrev="$3"
> pattern="[A-Z]-[0-9]{5}\:"
> echo $newrev
> newrev_subject=$(git show -s --pretty=format:$s $newrev)
> echo $newrev_subject
> if [[ $newrev_subject =~ $pattern ]]; then
>                 echo "Processing..."
> else
>      echo "error: commit subject comment's not include VersionOne
> stories,defect number"
>      exit 1
> fi
> ~~~~
> but when I try to commit the changes and I got below output error:
> "C:\Program Files\Git\bin\git.exe" commit -F
> "C:\JICore\git-clones\Lingfei_test1\Lingfei_test\.git\\COMMITMESSAGE"
> .git/hooks/pre-commit: line 14: conditional binary operator expected
> .git/hooks/pre-commit: line 14: syntax error near `=~'
> .git/hooks/pre-commit: line 14: `if [[ $newrev_subject =~
> $pattern ]]; then' Done
> So could you let me know if anything i did wrong here? Git extentions
> doesn't know bash?

Bash tells you it doesn't understand the token =~ in a place where it
expects a binary operator (applicable inside [[ ... ]]).  Reading
through the bash release notes, I gather than bash acquired this binary
operator in version 3.0 (and it has been subsequently further refined
in 3.1 and 3.2).  Git bash I have installed as part of Git for Windows has the version 3.1.0 (you can know it by
hitting Ctrl-x + Ctrl-v while in the Git bash window).  So technically
the feature should be supported.  Though it does not work for me:

  $ if [[ $x =~ .*b.* ]]; then echo OK; else echo FAIL; fi
  sh.exe": conditional binary operator expected
  sh.exe": syntax error near `=~'

On a Debian machine running bash 3.2 this works:

  $ x="abcd1234abcd"
  $ if [[ $x =~ .*a.* ]]; then echo OK; else echo FAIL; fi

So while I don't know why this binop does not work in Git bash, I'd say
just rewrite the script.  From `man bash` I gather, than

if [[ $s =~ $pattern ]]; then echo OK; fi

is roughly the same as

if echo "$s" | grep -q "$pattern" 2>/dev/null; the echo OK; fi

Also note that, as indicated by someone else in this thread, a Git hook
might be written in any language.  The only twist is that on Windows,
to be executable, a file must have one of special file name extensions
recognised by the system, and a Git hook script must have no extension.
The solution to this is to make the hook screen a mere wrapper around
some other program which does the actual heavy-lifting.
For instance, you might write the hook code in VBS, and then call it in
the "bootstrap" hook script like this (untested):

exec cscript.exe /b c:/path/to/your/actual/hook/script.vbs

You received this message because you are subscribed to the Google Groups "Git 
for human beings" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to git-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to