I'm trying to use a pre-push
Git hook that prompts the user for confirmation before proceeding. However, when executed as a Git hook, the read
command does not wait for user input and immediately proceeds as if an empty response was given.
Here's a minimal script reproducing the issue (.git/hooks/pre-push):
#!/usr/bin/sh
echo "Do you want to continue (o/n) ?"
read -r response
if [[ "$response" =~ ^[oO]$ ]]; then
echo "Continue ...."
else
echo "Stop !!!!"
exit 1
fi
exit 0
Expected Behavior
- The script should wait for user input before proceeding.
Actual Behavior
- The script does not wait for input when executed as a Git hook and immediately treats the response as empty, leading to the "Stop !!!!" message.
What I've Tried
- Running the script manually works fine (it waits for input).
- Using
/bin/bash
instead of/usr/bin/sh
. - Running
read -p
directly. - Redirecting input with
exec < /dev/tty
, but the behavior remains the same in the hook context.
Question
- Why does
read
get skipped when executed inside a Git hook? - How can I properly prompt for user input in a Git hook?
Thanks for any insights!