0

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
Cy Rossignol
  • 16,216
  • 4
  • 57
  • 83
kratos
  • 2,465
  • 2
  • 27
  • 45
  • On what OS do you try it... Can you share some part of your code? Are there any errors in your log file. What do you meen with Command line, bash is also CL. – Webdesigner Oct 10 '17 at 13:58
  • sorry added updates above – kratos Oct 10 '17 at 14:50
  • Do I understand you right, you want to do e.g. `git push production` from local host and git should respond with the command prompt from your artisan command done on your git hook? Or what you try to do? – Webdesigner Oct 10 '17 at 14:57
  • Check the command which is running this file. There might be for output redirection. – anwerj Oct 10 '17 at 15:18

1 Answers1

1

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.

Cy Rossignol
  • 16,216
  • 4
  • 57
  • 83