I have a Laravel Artisan command that works great and has some interactive-type questions. It works great when run from the command line.
However, when invoked from a Git hook (a Bash script), it doesn't display the interactive questions like "confirm" or "ask", etc. I just need to know in which context the Artisan command is being run and whether I will be able to display my "confirm" or other interactive questions. Is it possible?
My code below:
#!/bin/bash
php artisan some:command
Git executes the hook scripts in a non-interactive shell, so the script's stdin isn't connected to the terminal. We can redirect the terminal to the hook script's process so we can interact with the commands:
#!/bin/sh
# This is a git hook.
# Connect terminal to STDIN...
exec < /dev/tty
# Run any interactive commands...
php artisan some:command
# Close STDIN...
exec <&-
We use exec
in this context to control the file descriptors available to the script. See this question or the POSIX spec for more information about exec
.
This technique should work fine from the command-line. We may encounter issues if we use other tools that wrap or interact with git.