Reputation: 2089
I'm trying to create a pre-commit hook. And I want it to interract with user. So, I've found that I can use
read -p "Enter info: " info
Or just
read info
I created a file:
pre-commit:
#!/bin/sh
read -r input
echo $input
It just should read variable and output it. But it doesn't work. I mean it doesn't work as hook. If I run it using terminal with ./.githooks/pre-commit
, everything is okay. But when I use git commit -am "Hook"
, it echos empty string and doesn't read anything. Am I doing something wrong?
Git version is 2.28.0.windows.1
Upvotes: 7
Views: 1828
Reputation: 1324577
As in this gist, you might need to take into account stderr (used by Git commands, as for instance here)
#!/bin/sh
# Redirect output to stderr.
exec 1>&2
# enable user input
exec < /dev/tty
Example of a script following those lines:
consoleregexp='console.log'
# CHECK
if test $(git diff --cached | grep $consoleregexp | wc -l) != 0
then
exec git diff --cached | grep -ne $consoleregexp
read -p "There are some occurrences of console.log at your modification. Are you sure want to continue? (y/n)" yn
echo $yn | grep ^[Yy]$
if [ $? -eq 0 ]
then
exit 0; #THE USER WANTS TO CONTINUE
else
exit 1; # THE USER DONT WANT TO CONTINUE SO ROLLBACK
fi
fi
Upvotes: 9