{"thread":{"id":"60775","subject":"Git pre-received hook not failing with exit code 1 correcly","startedAt":"2024-01-21T01:56:32Z","lastAt":"2024-01-21T18:06:03Z","messageCount":2,"participants":["Marc C","brian m. carlson"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"487162","messageId":"CYK0FIQPVI22.2RHKOY4L00FZS@mccd.space","threadId":"60775","inReplyTo":null,"subject":"Git pre-received hook not failing with exit code 1 correcly","fromName":"Marc C","fromEmail":"marc@mccd.space","sentAt":"2024-01-21T01:57:28Z","receivedAt":"2024-01-21T01:56:32Z","isPatch":false,"sender":{"key":"marc@mccd.space","avatar":null},"body":"Heya!\n\nI am trying to understand why my pre-publish hook does not exit with the\ncorrect status code. \n\nI have a pre-publish script\n\n```\n#!/bin/sh\nset -euo pipefail\necho \"Testing nixos config\"\nnixos-rebuild dry-build\necho \"Success\"\n```\n\nRunning it directly in my CLI, it acts like I would expect and return\nexit code 1.\n\n```\n$ ./.git/hooks/pre-receive\nTesting nixos config\nbuilding the system configuration...\nerror:\n       … while calling the 'seq' builtin\n       ....\n       ...\n\n$ echo \"$?\"\n1\n```\n\nHowever, when running the script as a pre-receive hook, it is not\nrunning the commands correctly and returns the wrong exit code. I get\nthe following:\n\n```\nremote: Testing nixos config\nremote: building the system configuration...\nremote: Success <-- ????\nremote: error:\nremote:        … while calling the 'seq' builtin\n...\nTo myserver:/myrepo\n   bffa94e..a14b3f6  main -> main\n```\n\nAny clue what I am missing? When running it as a pre-receive hook, the\nfailing command returns exit code 0. Running it in the CLI, it returns\nexit code 1. It is Schrodinger's exit code.\n\nThank you so much for your help.\n\nSincerely,\nMarc\n\n"},{"id":"487181","messageId":"Za1dAaKO_IlT3nuM@tapette.crustytoothpaste.net","threadId":"60775","inReplyTo":"CYK0FIQPVI22.2RHKOY4L00FZS@mccd.space","subject":"Re: Git pre-received hook not failing with exit code 1 correcly","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2024-01-21T18:05:53Z","receivedAt":"2024-01-21T18:06:03Z","isPatch":false,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2024-01-21 at 01:57:28, Marc C wrote:\n> Heya!\n\nHey,\n\n> I am trying to understand why my pre-publish hook does not exit with the\n> correct status code. \n> \n> I have a pre-publish script\n> \n> ```\n> #!/bin/sh\n> set -euo pipefail\n\nOne note about this is that `-o pipefail` is a bash- and zsh-specific\noption.  (It's also present in some ksh variants.)  It isn't specified\nby POSIX, so you'd probably want to use `#!/bin/bash` as your shebang\ninstead.\n\nEven OSes such as Debian which specify certain extensions required for\n`/bin/sh` (notably `local`) don't require this of it.\n\n> However, when running the script as a pre-receive hook, it is not\n> running the commands correctly and returns the wrong exit code. I get\n> the following:\n> \n> ```\n> remote: Testing nixos config\n> remote: building the system configuration...\n> remote: Success <-- ????\n> remote: error:\n> remote:        … while calling the 'seq' builtin\n> ...\n> To myserver:/myrepo\n>    bffa94e..a14b3f6  main -> main\n> ```\n> \n> Any clue what I am missing? When running it as a pre-receive hook, the\n> failing command returns exit code 0. Running it in the CLI, it returns\n> exit code 1. It is Schrodinger's exit code.\n\nI know nothing about nixOS, but I'm wondering if maybe `nixos-rebuild\ndry-build` forks in some cases, specifically when it's detached from the\nterminal.  If that happened, then you'd see the above, where the child\nprocess continued but the parent process exited, and then the error\nmessage would get printed at the end.\n\nYou'd also see this if you had an `&` at the end of `nixos-rebuild\ndry-build`, but you don't seem to have that here.\n\nYou could try something like `(cat | ./.git/hooks/pre-receive 2>&1 | cat)`\nto see if the problem is the lack of terminal.\n\nHopefully that gives you a helpful head start on the problem.  I\napologize for not being able to help more.\n-- \nbrian m. carlson (he/him or they/them)\nToronto, Ontario, CA\n"}]}