git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v4 1/2] hooks: allow input from stdin for commit-related hooks

From
Orgad Shaneh <orgads@gmail.com>
Date
Nov 20, 2020, 05:23 UTC
Message-ID
<CAGHpTBKHmdjqrz1ABdGUUz7AwcixU_VBy1DQzybpFizqVo8C7A@mail.gmail.com>
In-Reply-To
<CAPig+cSN=-7KWgDcXM8po44PEKi27U6mJEEL0mj_wrTJBUf=WA@mail.gmail.com>
On Thu, Nov 19, 2020 at 11:23 PM Eric Sunshine <sunshine@sunshineco.com> wrote:
Show 29 quoted lines
>
> On Thu, Nov 19, 2020 at 3:57 PM Orgad Shaneh via GitGitGadget
> <gitgitgadget@gmail.com> wrote:
> > Let hooks receive user input if applicable.
> > [...]
> > This allows for example prompting the user to choose an issue
> > in prepare-commit-msg, and add "Fixes #123" to the commit message.
> >
> > Another possible use-case is running sanity test on pre-commit,
> > and having a prompt like "This and that issue were found in your
> > changes. Are you sure you want to commit? [Y/N]".
>
> These use-cases really help readers understand the motivation for this
> change. Good.
>
> > Allow stdin only for commit-related hooks. Some of the other
> > hooks pass their own input to the hook, so don't change them.
> >
> > Note: If pre-commit reads from stdin, and git commit is executed
> > with -F - (read message from stdin), the message is not read
> > correctly. This is fixed in the follow-up commit.
>
> Rather than making such a fundamental change and having to deal with
> the fallout by introducing complexity to handle various special-cases
> which pop up now and in the future, I wonder if it makes more sense to
> instead just update documentation to tell hook authors to read
> explicitly from the console rather than expecting stdin to be
> available (since stdin may already be consumed for other purposes when
> dealing with hooks or commands which invoke the hooks).

On the first revision I had several links in the commit message to users who solved it this way. This solution however is not optimal. I have a prepare-commit-msg hook that requires user interaction for choosing an issue. This hook must work from the terminal and also from GUI applications like IDE.

Currently the hook always pops a GUI window, but when using it from the terminal this is inconvenient (and when running over remote SSH without X forwarding it can't work), so I'd like it to be usable also from the terminal.

To achieve that, I created 2 classes - one for terminal and one for GUI, and trying to choose the correct class by checking if stdin is a tty. The condition looks like this (Ruby): client = STDIN.tty? ? Terminal.new : GUI.new

At this point I was surprised to discover that Git closes stdin, so the condition is never satisfied, and I always end up with GUI.

As I mentioned, I need it to work also when executed from GUI applications, so just reading from the console will not work in my case. I tried other ways to detect "running from terminal" without the tty condition, but couldn't. The environment variables are identical when running in a GUI terminal and in the IDE.

Can you suggest an alternative way to determine if I can accept user input from the console or not?

- Orgad
Previous: Junio C HamanoNext: Eric Sunshine
Message 12 of 22 in “hooks: allow input from stdin”
  1. hooks: allow input from stdinOrgad Shaneh via GitGitGadget, Nov 17, 2020
  2. Junio C HamanoNov 17, 2020
  3. hooks: allow input from stdinOrgad Shaneh via GitGitGadget, Nov 19, 2020
  4. hooks: allow input from stdin for commit-related hooksOrgad Shaneh via GitGitGadget, Nov 19, 2020
  5. Junio C HamanoNov 19, 2020
  6. Orgad ShanehNov 19, 2020
  7. 0/2 hooks: allow input from stdin for commit-related hooksOrgad Shaneh via GitGitGadget, Nov 19, 2020
  8. 2/2 commit: fix stdin conflict between message and hookOrgad Shaneh via GitGitGadget, Nov 19, 2020
  9. 1/2 hooks: allow input from stdin for commit-related hooksOrgad Shaneh via GitGitGadget, Nov 19, 2020
  10. Eric SunshineNov 19, 2020
  11. Junio C HamanoNov 19, 2020
  12. Orgad ShanehNov 20, 2020
  13. Eric SunshineNov 20, 2020
  14. Eric SunshineNov 20, 2020
  15. Orgad ShanehNov 20, 2020
  16. Junio C HamanoNov 20, 2020
  17. Ævar Arnfjörð BjarmasonNov 20, 2020
  18. Orgad ShanehNov 20, 2020
  19. 0/2 hooks: allow input from stdin for commit-related hooksOrgad Shaneh via GitGitGadget, Dec 9, 2020
  20. 1/2 hooks: lay foundations for passing stdin to hooksOrgad Shaneh via GitGitGadget, Dec 9, 2020
  21. 2/2 hooks: allow input from stdin for commit-related hooksOrgad Shaneh via GitGitGadget, Dec 9, 2020
  22. Junio C HamanoDec 9, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.