Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdin on hooks #442

Closed
rafamel opened this issue Jan 12, 2019 · 8 comments
Closed

stdin on hooks #442

rafamel opened this issue Jan 12, 2019 · 8 comments

Comments

@rafamel
Copy link

rafamel commented Jan 12, 2019

Hi, I found myself adding some stdin dependent hooks and just noticed 1.3.1 disables it due to a bug on Windows. Is there a fix planned in the near future? Otherwise, would it be sensible to enable it for other OS and leave it disabled on Windows until a fix is available?

Thanks for the amazing work!
Rafa

@typicode
Copy link
Owner

Hi @rafamel,

Is there a fix planned in the near future?

Maybe. Not sure, if it should be automatic or not.

That said, you can enable it locally by creating a ~/.huskyrc file and adding this line:

# ~/.huskyrc
exec < /dev/tty

@rafamel
Copy link
Author

rafamel commented Jan 18, 2019

Thanks for getting back @typicode !

Since we're already defining a husky property @ package.json, wouldn't it make sense to, if it's not enabled by default, allowing to enable the option there within an interactive or stdin key?

{
  // ...
  "husky": {
    "interactive": true,
    "hooks": {
      // ...
    }
  }
}

@fjcaetano
Copy link

I believe this is because husky is always calling execa.shellSync with stdio: 'inherit'. I even tried to pipe my input into git commit but it failed nonetheless.

@typicode's workaround solved it for me, but I'm left with the same question as @rafamel: this is something that could be configurable per project

@kg-currenxie
Copy link

Ye, doesn't make sense for every developer to add a ~/.huskyrc file manually.

@dosentmatter
Copy link

dosentmatter commented Jul 27, 2019

Hello, I wrote an npm package, force-stdin-tty to enable stdin in node. Instructions are in the README.

I guess you could also have husky run a shell script that does exec < /dev/tty before running a node script so each developer doesn't have to set up ~/.huskyrc. force-stdin-tty just makes it easier since everything is in javascript.

File issues or contribute to the GitHub repo

@jagu-sayan
Copy link

Related to #597 (comment)

This work with GitKraken, SourceTree, Atom, VisualStudio Code :

# Used to fix GitKraken bug that launch git hook in terminal
PARENT_COMMAND="$(basename "$(ps -o comm= $PPID)")"

# Check if stdout is a terminal and parent command is `git`
if [ -t 1 ] && [ "$PARENT_COMMAND" == "git" ]; then
  # Exec node binary with /dev/tty (current terminal) as stdin
  exec < /dev/tty
else
```

And maybe we can add the `HUSKY_GIT_STDIN=1` option

orgads added a commit to orgads/git that referenced this issue Nov 17, 2020
Let hooks receive user input if applicable.

Closing stdin originates in f5bbc32 (Port git commit to C,
2007). Looks like the original shell implementation did have
stdin open. Not clear why the author chose to close it on
the C port (maybe copy&paste).

Some references of users requesting this feature. Some of
them use acrobatics to gain access to stdin:
[1] https://stackoverflow.com/q/1067874/764870
[2] https://stackoverflow.com/q/47477766/764870
[3] https://stackoverflow.com/q/3417896/764870
[4] PHP-CS-Fixer/PHP-CS-Fixer#3165
[5] typicode/husky#442
orgads added a commit to orgads/git that referenced this issue Nov 17, 2020
Let hooks receive user input if applicable.

Closing stdin originates in f5bbc32 (Port git commit to C,
2007). Looks like the original shell implementation did have
stdin open. Not clear why the author chose to close it on
the C port (maybe copy&paste).

The only hook that passes internal information to the hook
via stdin is pre-push, which has its own logic.

Some references of users requesting this feature. Some of
them use acrobatics to gain access to stdin:
[1] https://stackoverflow.com/q/1067874/764870
[2] https://stackoverflow.com/q/47477766/764870
[3] https://stackoverflow.com/q/3417896/764870
[4] PHP-CS-Fixer/PHP-CS-Fixer#3165
[5] typicode/husky#442
orgads added a commit to orgads/git that referenced this issue Nov 17, 2020
Let hooks receive user input if applicable.

Closing stdin originates in f5bbc32 (Port git commit to C,
2007). Looks like the original shell implementation did have
stdin open. Not clear why the author chose to close it on
the C port (maybe copy&paste).

The only hook that passes internal information to the hook
via stdin is pre-push, which has its own logic.

Some references of users requesting this feature. Some of
them use acrobatics to gain access to stdin:
[1] https://stackoverflow.com/q/1067874/764870
[2] https://stackoverflow.com/q/47477766/764870
[3] https://stackoverflow.com/q/3417896/764870
[4] PHP-CS-Fixer/PHP-CS-Fixer#3165
[5] typicode/husky#442

Signed-off-by: Orgad Shaneh <orgads@gmail.com>
@typicode
Copy link
Owner

Closing as husky 5 is working differently and may not have this issue

@eddie-dunn
Copy link

@typicode

I upgraded to husky 7 hoping it would make interactive hooks work, but it seems all calls to read -p "Question" choice are ignored unless I add exec < /dev/tty to, e.g., ./husky/pre-push.

Is there a better solution? Either way, finding out how to add interactive hooks wasn't trivial, maybe it should be added to the docs somewhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants