0

I am running a shell script from another shell script which is a git-hook pre-push.

This is the content of .git/hooks/pre-push:

#!/usr/bin/env bash

protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

if [ $protected_branch = $current_branch ]; then
    sh test.sh
    if [ $? != 0 ]; then
        echo "Error"
        exit 1
    fi
else
    exit 0
fi

This is the content of test.sh:

#!/bin/bash
run_base=1
    run_test () {
        read -p "enter varname: " varname
        echo $varname
    }
time {
        if [ "$run_base" = "0" ] ; then 
            echo "skipped"
        else 
            time { run_test ; }
            echo "run_test done";
        fi
}

If I run pre-push script directly, then it works fine, but it doesn't work when I execute git push origin master and pre-push gets triggered automatically.

read in test.sh is not being executed when I trigger the pre-push hook script. Do I need to do anything specific in order to execute read in test.sh which is called from pre-push?

npatel
  • 1,081
  • 2
  • 13
  • 21

2 Answers2

0

I just test it on my computer and it works perfectly, isoto@hal9014-2 ~/w/test> ./pre-push enter varname: asd asd

real 0m1.361s user 0m0.000s sys 0m0.000s run_test done

real 0m1.361s user 0m0.000s sys 0m0.000s

So, the only thing that I did was to add executable permissions, chmod +x * And also I put both scripts in the same directory, besides everything should work.

israelss
  • 322
  • 3
  • 6
  • If I execute pre-push script directly, then I do not see any issue, I see this issue when I execute `git push origin master` which triggers pre-push script automatically. – npatel Jan 24 '19 at 18:48
0

Found the answer, I had to add < /dev/tty at the end of read:

read -p "enter varname: " varname < /dev/tty
echo $varname
npatel
  • 1,081
  • 2
  • 13
  • 21