1

I have created comment-added Gerrit (v. 2.8) hook:

PROJECT="UNKNOWN"
AUTHOR="UNKNOWN"

while [[ $# > 1 ]]
do
key="$1"
shift

case $key in
    --project)
        PROJECT="$1"
        shift
    ;;
    --author)
        AUTHOR="$1"
        shift
    ;;
    *)
        # unknown option
    ;;
esac
done

# do something with the $PROJECT variable

The arguemts reading idea from this answer. According to the documentation this hook should be invoked with both project and author arguments. If I simulate invoke with the following command, everything works fine:

./commend-added --branch test --project testproject --author testauthor --whatever sth

However, when the hook is executed from Gerrit, both PROJECT and AUTHOR variables have default UNKNOWN value. Why?

I have inspected that the $# variable has value of 16 when called from Gerrit. I have also verified that the while loop never gets executed when the script is called from Gerrit.

I have also tried another loop with $# variable and it is neither executed:

for ((i=1; i<=$#; i++)); do
    # do something
done

However, I'm able to iterate over $@ variable. By using it, I also verified that the arguments supplied to the script by Gerrit are the same as in docs.

What am I doing wrong? How to read arguments when the hook is executed by Gerrit?

Community
  • 1
  • 1
fracz
  • 20,536
  • 18
  • 103
  • 149

1 Answers1

0

I suspect that gerrit execs the hook without parsing the command line.

If this happens, you may simply add at the beginning of your script:

set $*

This will parse the command line again.

Edouard Thiel
  • 5,878
  • 25
  • 33