typicode / husky

Git hooks made easy 🐶 woof!

Home Page:https://typicode.github.io/husky

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

stdin on hooks

rafamel opened this issue · comments

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

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

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": {
      // ...
    }
  }
}

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

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

commented

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

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

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

commented

@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?