Cannot read from /dev/stdin in hooks

375 views
Skip to first unread message

Cesar Izurieta

unread,
Apr 8, 2022, 1:12:13 AM4/8/22
to git-for-windows
Hello,

I'm trying to read from /dev/stdin in a pre-push hook (to get the branches and references) but I get the following error:

I tried with a clean repository with the following minimal pre-push hook that reproduces the error:

#!/bin/bash -e

echo $(</dev/stdin)
exit 1

And the error I get is:

.git/hooks/pre-push: line 3: /dev/stdin: No such file or directory

Is this a bug? How can I read stdin on a hook on windows?

Version: git version 2.35.1.windows.2
Tried both on a WSL2 terminal and git bash terminal with the same output.

Warm greetings,
César Izurieta


Bryan Turner

unread,
Apr 8, 2022, 2:44:46 AM4/8/22
to Cesar Izurieta, git-for-windows
On Thu, Apr 7, 2022 at 10:12 PM Cesar Izurieta <ce...@ecuarock.net> wrote:
>
> Hello,
>
> I'm trying to read from /dev/stdin in a pre-push hook (to get the branches and references) but I get the following error:
>
> I tried with a clean repository with the following minimal pre-push hook that reproduces the error:
>
> #!/bin/bash -e
>
> echo $(</dev/stdin)
> exit 1
>
> And the error I get is:
>
> .git/hooks/pre-push: line 3: /dev/stdin: No such file or directory
>
> Is this a bug? How can I read stdin on a hook on windows?

In Bitbucket Server's hooks (which had to work on Windows), we used &0.

So you could do something like:
while read line
do
# Something with line
done < &0

Hope this helps!
Bryan

>
> Version: git version 2.35.1.windows.2
> Tried both on a WSL2 terminal and git bash terminal with the same output.
>
> Warm greetings,
> César Izurieta
>
>
> --
> You received this message because you are subscribed to the Google Groups "git-for-windows" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to git-for-windo...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/git-for-windows/eab04c14-26e4-4a4b-a5a5-3462a086b485n%40googlegroups.com.

Konstantin Khomoutov

unread,
Apr 8, 2022, 7:24:18 AM4/8/22
to Cesar Izurieta, git-for-windows
On Thu, Apr 07, 2022 at 11:42:42PM -0700, Bryan Turner wrote:

[...]
> > .git/hooks/pre-push: line 3: /dev/stdin: No such file or directory
> >
> > Is this a bug? How can I read stdin on a hook on windows?
>
> In Bitbucket Server's hooks (which had to work on Windows), we used &0.
>
> So you could do something like:
> while read line
> do
> # Something with line
> done < &0
>
> Hope this helps!

/dev/stdin is a Linux-specific thing, not mandated by POSIX, so you simply
cannot use it in portable POSIX shell scripts (by the way, the same applies
to the /proc/self/ hierarchy).

&0, which Bryan proposed, it s 100% correct thing as it's just a standard
way to refer to a file descriptor opened to the standard input stream (unless
reopened, but let's not digress).

I'd also hint to that `read` should read from FD 0 by default - there is
simply no need to explicitly redirect anything here.
I mean,

while read line; do ...; done < &0

is the same as unadorned

while read line; do ...; done

because FD 0 is what the standard input stream is opened to.

Reply all
Reply to author
Forward
0 new messages