# Git pre-received hook not failing with exit code 1 correcly

2 messages from 2024-01-21 to 2024-01-21. Participants: Marc C, brian m. carlson.
Thread: https://gitlist.dev/t/60775

## Marc C, 2024-01-21 01:57

Subject: Git pre-received hook not failing with exit code 1 correcly
Message-ID: <CYK0FIQPVI22.2RHKOY4L00FZS@mccd.space>
URL: https://gitlist.dev/e/CYK0FIQPVI22.2RHKOY4L00FZS%40mccd.space

```
Heya!

I am trying to understand why my pre-publish hook does not exit with the
correct status code. 

I have a pre-publish script

ˋˋˋ
#!/bin/sh
set -euo pipefail
echo "Testing nixos config"
nixos-rebuild dry-build
echo "Success"
ˋˋˋ

Running it directly in my CLI, it acts like I would expect and return
exit code 1.

ˋˋˋ
$ ./.git/hooks/pre-receive
Testing nixos config
building the system configuration...
error:
       … while calling the 'seq' builtin
       ....
       ...

$ echo "$?"
1
ˋˋˋ

However, when running the script as a pre-receive hook, it is not
running the commands correctly and returns the wrong exit code. I get
the following:

ˋˋˋ
remote: Testing nixos config
remote: building the system configuration...
remote: Success <-- ????
remote: error:
remote:        … while calling the 'seq' builtin
...
To myserver:/myrepo
   bffa94e..a14b3f6  main -> main
ˋˋˋ

Any clue what I am missing? When running it as a pre-receive hook, the
failing command returns exit code 0. Running it in the CLI, it returns
exit code 1. It is Schrodinger's exit code.

Thank you so much for your help.

Sincerely,
Marc


```

## brian m. carlson, 2024-01-21 18:05

Subject: Re: Git pre-received hook not failing with exit code 1 correcly
Message-ID: <Za1dAaKO_IlT3nuM@tapette.crustytoothpaste.net>
URL: https://gitlist.dev/e/Za1dAaKO_IlT3nuM%40tapette.crustytoothpaste.net
In-Reply-To: <CYK0FIQPVI22.2RHKOY4L00FZS@mccd.space>

```
On 2024-01-21 at 01:57:28, Marc C wrote:
> Heya!

Hey,

> I am trying to understand why my pre-publish hook does not exit with the
> correct status code. 
> 
> I have a pre-publish script
> 
> ˋˋˋ
> #!/bin/sh
> set -euo pipefail

One note about this is that `-o pipefail` is a bash- and zsh-specific
option.  (It's also present in some ksh variants.)  It isn't specified
by POSIX, so you'd probably want to use `#!/bin/bash` as your shebang
instead.

Even OSes such as Debian which specify certain extensions required for
`/bin/sh` (notably `local`) don't require this of it.

> However, when running the script as a pre-receive hook, it is not
> running the commands correctly and returns the wrong exit code. I get
> the following:
> 
> ˋˋˋ
> remote: Testing nixos config
> remote: building the system configuration...
> remote: Success <-- ????
> remote: error:
> remote:        … while calling the 'seq' builtin
> ...
> To myserver:/myrepo
>    bffa94e..a14b3f6  main -> main
> ˋˋˋ
> 
> Any clue what I am missing? When running it as a pre-receive hook, the
> failing command returns exit code 0. Running it in the CLI, it returns
> exit code 1. It is Schrodinger's exit code.

I know nothing about nixOS, but I'm wondering if maybe `nixos-rebuild
dry-build` forks in some cases, specifically when it's detached from the
terminal.  If that happened, then you'd see the above, where the child
process continued but the parent process exited, and then the error
message would get printed at the end.

You'd also see this if you had an `&` at the end of `nixos-rebuild
dry-build`, but you don't seem to have that here.

You could try something like `(cat | ./.git/hooks/pre-receive 2>&1 | cat)`
to see if the problem is the lack of terminal.

Hopefully that gives you a helpful head start on the problem.  I
apologize for not being able to help more.
-- 
brian m. carlson (he/him or they/them)
Toronto, Ontario, CA

```
