1

I'm trying to implement an option to skip a pre-commit hook (which will be considered pass and perform the commit) in a pre-commit hook.

Everything is running fine except the prompt which reads an empty string ("") and thus the switch is going to the default "invalid argument" which leads into an infinite loop.

I guess there is something I don't understand about how pre-commit works which result in this behavior. Could you please illuminate me and if possible hint at a solution?

while true; do
  read -p "Do you want to run pre-commit hook? (N=pass) [Y/N]: " choice
  
  #tried alternative approach, but no success.
  #echo "Do you want to run pre-commit hook? (N=pass) [Y/N]: "
  #read choice
  echo "Debug: response=\"$choice\""
  case $choice in
    [Yy]* )
      echo "Running pre-commit hook...";
      break;;
    [Nn]* )
      echo "Skipping pre-commit hook. This is evaluated as passed.";
      exit 0;;
    * )
      echo "Invalid input. Please enter 'y' or 'n'.";;
  esac
done
2
  • Which shell is executing this? Commented Jul 17, 2023 at 12:56
  • I would discourage the use of interactive prompts in Git hooks. My coworker tried the /dev/tty trick in a hook. You get strange errors as soon as you try to run it in some non-shell like an IDE. Commented Dec 7, 2024 at 20:35

1 Answer 1

1

Looks like this is expected behavior, since Git does not run hooks in an interactive environment.

Some quick Googling found this question, which seems nearly identical. For ease of reference, the accepted answer there notes that you can open standard input by running exec < /dev/tty as part of your hook. This should be done before the existing read command, allowing it to read from stdin as you'd expect.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.