Reputation: 1505
I'm working on an interactive pre-commit Git Hook to ensure that a certain set of criteria have been met prior to the commit (eg. updating particular files in the repo). I've been basing my checklist off of the example here. My hook has identical implementation but has different question text. Currently, it works perfectly in Git Bash.
However, for a team-wide implementation I need to have it working within IntelliJ, as most developers use Git functionality within the IDE. Using the example hook linked above, my commits fail with the error:
0 files committed, 1 file failed to commit: COMMIT TEST MESSAGE Would you like to play a game? .git/hooks/pre-commit: line 6: /dev/tty: No such device or address
I would like to have the complete interaction contained within IntelliJ, if possible. If this is not possible, I would begin looking into IntelliJ plugin development in order to implement this within organizational restrictions.
For convenience, the Git hook linked above is pasted below.
#!/bin/sh
echo "Would you like to play a game?"
# Read user input, assign stdin to keyboard
exec < /dev/tty
while read -p "Have you double checked that only relevant files were added? (Y/n) " yn; do
case $yn in
[Yy] ) break;;
[Nn] ) echo "Please ensure the right files were added!"; exit 1;;
* ) echo "Please answer y (yes) or n (no):" && continue;
esac
done
while read -p "Has the documentation been updated? (Y/n) " yn; do
case $yn in
[Yy] ) break;;
[Nn] ) echo "Please add or update the docs!"; exit 1;;
* ) echo "Please answer y (yes) or n (no):" && continue;
esac
done
while read -p "Do you know which issue or PR numbers to reference? (Y/n) " yn; do
case $yn in
[Yy] ) break;;
[Nn] ) echo "Better go check those tracking numbers!"; exit 1;;
* ) echo "Please answer y (yes) or n (no):" && continue;
esac
done
exec <&-
Upvotes: 5
Views: 4678
Reputation: 7548
It is not possible, IDE is not a terminal, and does not provide any tty you can use interactively. You need either to make you hook show a GUI prompt, or implement what you want in a different way.
There are plugins already implementing pre-commit hooks, e.g. https://plugins.jetbrains.com/plugin/9278-pre-commit-hook-plugin
Upvotes: 2