{"thread":{"id":"60088","subject":"git pre-push hook not getting the lines from STDIN","startedAt":"2023-08-08T22:32:34Z","lastAt":"2023-08-09T12:38:41Z","messageCount":3,"participants":["Wesley","Sean Allred"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"480343","messageId":"676330b6-720a-e262-d583-9012e549bba7@opperschaap.net","threadId":"60088","inReplyTo":null,"subject":"git pre-push hook not getting the lines from STDIN","fromName":"Wesley","fromEmail":"wesleys@opperschaap.net","sentAt":"2023-08-08T22:24:49Z","receivedAt":"2023-08-08T22:32:34Z","isPatch":false,"sender":{"key":"wesleys@opperschaap.net","avatar":"https://avatars.githubusercontent.com/u/6317502?v=4"},"body":"\nHello list,\n\nI'm trying to figure out how I can check which branches are used in a \ngit push action while using the pre-push hook. In the man page the \nfollowing is mentioned:\n\n----<\n\nInformation about what is to be pushed is provided on the hook’s \nstandard input with lines of the form:\n\n<local ref> SP <local object name> SP <remote ref> SP <remote object \nname> LF\n\nFor instance, if the command `git push origin master:foreign` were run \nthe hook would receive a line like the following:\n\n   refs/heads/master 67890 refs/heads/foreign 12345\n\n----<\n\nI cannot seem to reproduce this behavior with the push action. Only when \npushing to delete a remote branch or pushing to a new branch gets \ncreated yields any success. I went as far back as git 2.9.0 (I can't \nbuild older versions of git), to no avail. The line in the man page \nseems to indicate git v1.8.2-rc0 was the first tag to have it (ec55559f).\n\nCould someone verify that what I am seeing is correct behavior or that \nthis is incorrect?\n\nAs stated, I experience issues on git v2.9.0, 2.40.1 (Debian) and also \ndirectly from source 2.42.0.rc0.26.g6ac35453d6.\n\nThanks,\nWesley\n\n\n----< a new branch\n\n$ git push origin test:fofofo\n+.git/hooks/pre-push:5> remote=origin\n+.git/hooks/pre-push:6> url=git@gitlab.com:gitlabmeme/somerepo.git\n+.git/hooks/pre-push:8> success=128\n+.git/hooks/pre-push:10> z40=0000000000000000000000000000000000000000\n+.git/hooks/pre-push:11> IFS=' '\n+.git/hooks/pre-push:13> read LOCAL_REF LOCAL_SHA REMOTE_REF REMOTE_SHA\n+.git/hooks/pre-push:15> echo refs/heads/test\nrefs/heads/test\n+.git/hooks/pre-push:16> echo 24a3b84a02937df6040fecce2e0620e16c823b36\n24a3b84a02937df6040fecce2e0620e16c823b36\n+.git/hooks/pre-push:17> echo refs/heads/fofofo\nrefs/heads/fofofo\n+.git/hooks/pre-push:18> echo 0000000000000000000000000000000000000000\n0000000000000000000000000000000000000000\n+.git/hooks/pre-push:21> [ refs/heads/test '=' \n0000000000000000000000000000000000000000 ']'\n+.git/hooks/pre-push:23> [ refs/heads/test '=' '(delete)' ']'\n+.git/hooks/pre-push:30> exit 128\n\n----< deletion of a branch on the remote\n\n$ git push origin :development\n+.git/hooks/pre-push:5> remote=origin\n+.git/hooks/pre-push:6> url=git@gitlab.com:gitlabmeme/somerepo.git\n+.git/hooks/pre-push:8> success=128\n+.git/hooks/pre-push:10> z40=0000000000000000000000000000000000000000\n+.git/hooks/pre-push:11> IFS=' '\n+.git/hooks/pre-push:13> read LOCAL_REF LOCAL_SHA REMOTE_REF REMOTE_SHA\n+.git/hooks/pre-push:15> echo '(delete)'\n(delete)\n+.git/hooks/pre-push:16> echo 0000000000000000000000000000000000000000\n0000000000000000000000000000000000000000\n+.git/hooks/pre-push:17> echo refs/heads/development\nrefs/heads/development\n+.git/hooks/pre-push:18> echo 1762c08fd38c1137bbca27898df7c64ad846f877\n1762c08fd38c1137bbca27898df7c64ad846f877\n+.git/hooks/pre-push:21> [ '(delete)' '=' \n0000000000000000000000000000000000000000 ']'\n+.git/hooks/pre-push:23> [ '(delete)' '=' '(delete)' ']'\n+.git/hooks/pre-push:23> exit 128\n\n----<\n\nIf I push to an existing repo on my remote I see this:\n$ git push origin HEAD:development\n+.git/hooks/pre-push:5> remote=origin\n+.git/hooks/pre-push:6> url=git@gitlab.com:gitlabmeme/somerepo.git\n+.git/hooks/pre-push:8> success=128\n+.git/hooks/pre-push:10> z40=0000000000000000000000000000000000000000\n+.git/hooks/pre-push:11> IFS=' '\n+.git/hooks/pre-push:13> read LOCAL_REF LOCAL_SHA REMOTE_REF REMOTE_SHA\n+.git/hooks/pre-push:15> echo\n\n+.git/hooks/pre-push:16> echo\n\n+.git/hooks/pre-push:17> echo\n\n+.git/hooks/pre-push:18> echo\n\n+.git/hooks/pre-push:21> [ '' '=' \n0000000000000000000000000000000000000000 ']'\n+.git/hooks/pre-push:23> [ '' '=' '(delete)' ']'\n+.git/hooks/pre-push:28> exit 128\n\n----< The pre-push script\n\n#!/usr/bin/env zsh\n#\nset -x\n\nremote=\"$1\"\nurl=\"$2\"\n\nsuccess=128\n\nz40=0000000000000000000000000000000000000000\nIFS=' '\n\nread LOCAL_REF LOCAL_SHA REMOTE_REF REMOTE_SHA\n\necho $LOCAL_REF\necho $LOCAL_SHA\necho $REMOTE_REF\necho $REMOTE_SHA\n\n# deletion of remote branch\n[ \"$LOCAL_REF\" = $z40 ] && exit $success\n# git 2.40 at least does not have $z40 as a delete\n[ \"$LOCAL_REF\" = '(delete)' ] && exit $success\n\nexit $success\n\n\n\n\n-- \nWesley\n\nWhy not both?\n"},{"id":"480354","messageId":"m0wmy4lbd0.fsf@epic96565.epic.com","threadId":"60088","inReplyTo":"676330b6-720a-e262-d583-9012e549bba7@opperschaap.net","subject":"Re: git pre-push hook not getting the lines from STDIN","fromName":"Sean Allred","fromEmail":"allred.sean@gmail.com","sentAt":"2023-08-09T11:00:32Z","receivedAt":"2023-08-09T11:11:03Z","isPatch":false,"sender":{"key":"allred.sean@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2082195?v=4"},"body":"\nWesley <wesleys@opperschaap.net> writes:\n\n> Hello list,\n>\n> I'm trying to figure out how I can check which branches are used in a\n> git push action while using the pre-push hook.\n\n> ----< The pre-push script\n>\n> #!/usr/bin/env zsh\n> #\n> set -x\n>\n> remote=\"$1\"\n> url=\"$2\"\n>\n> success=128\n>\n> z40=0000000000000000000000000000000000000000\n> IFS=' '\n>\n> read LOCAL_REF LOCAL_SHA REMOTE_REF REMOTE_SHA\n>\n> echo $LOCAL_REF\n> echo $LOCAL_SHA\n> echo $REMOTE_REF\n> echo $REMOTE_SHA\n>\n> # deletion of remote branch\n> [ \"$LOCAL_REF\" = $z40 ] && exit $success\n> # git 2.40 at least does not have $z40 as a delete\n> [ \"$LOCAL_REF\" = '(delete)' ] && exit $success\n>\n> exit $success\n\nHave you looked at simplifying this script to the bare minimum to\nidentify the issue? I might suggest starting by just slurping stdin and\nwriting that to a file:\n\n    #!/bin/sh\n    echo \"$@\" >pre-push.$$.args\n    cat >>pre-push.$$.stdin\n    exit 1\n\n(The 'exit 1' here is to unconditionally prevent the push from actually\ngoing through during testing. Likely similar to your '128' value, since\nactual success of course should be exit code '0'.)\n\nFrom here, you can test your pre-push hook by using that file as stdin:\n\n    ./my-hook $(cat pre-push.$$.args) <pre-push.12345.stdin\n\nI'm not familiar with the particulars of Zsh scripting, but I suspect\nthere is a bug in your script. Zsh works with my script, too, so it\ndoesn't appear to be a problem with Zsh itself.\n\n--\nSean Allred\n"},{"id":"480356","messageId":"1eef1bff-8b65-abac-e75b-acd98b013e7b@opperschaap.net","threadId":"60088","inReplyTo":"m0wmy4lbd0.fsf@epic96565.epic.com","subject":"Re: git pre-push hook not getting the lines from STDIN","fromName":"Wesley","fromEmail":"wesleys@opperschaap.net","sentAt":"2023-08-09T12:38:25Z","receivedAt":"2023-08-09T12:38:41Z","isPatch":false,"sender":{"key":"wesleys@opperschaap.net","avatar":"https://avatars.githubusercontent.com/u/6317502?v=4"},"body":"\nHello Sean,\n\n> Have you looked at simplifying this script to the bare minimum to\n> identify the issue? I might suggest starting by just slurping stdin and\n> writing that to a file:\n> \n>      #!/bin/sh\n>      echo \"$@\" >pre-push.$$.args\n>      cat >>pre-push.$$.stdin\n>      exit 1\n >\n> I'm not familiar with the particulars of Zsh scripting, but I suspect\n> there is a bug in your script. Zsh works with my script, too, so it\n> doesn't appear to be a problem with Zsh itself.\n\nThis is the bare miminum. You can also take the example script from the \ngit sources which does it in bash[^1] and uses while read syntax. It \nproduces the same output, with the exception that you don't see the \nempty echo statements because it cannot read the input from STDIN and \nexits immediately.\n\nIt seems it works intermittently.\n\nIn my zsh repo for example with the following remotes:\n\norigin\tgit@gitlab.com:waterkip/zsh (fetch)\norigin\tgit@gitlab.com:waterkip/zsh (push)\nupstream\tgit://git.code.sf.net/p/zsh/code (fetch)\nupstream\tgit://git.code.sf.net/p/zsh/code (push)\n\nLocally my patchlevel-in_version branch is at commit b7f280ab5. If I \n`git pull --rebase upstream master` and than push it (`git push origin \nHEAD) with hook configured it doesn't show any of the things I expect in \nLOCAL_REF etc. This happens with my version of the script and the \nversion provided by git (in .git/hooks/pre-push.sample).\n\nCheers,\nWesley\n\n[^1]: \nhttps://github.com/git/git/blob/87c86dd14abe8db7d00b0df5661ef8cf147a72a3/templates/hooks--pre-push.sample\n\n-- \nWesley\n\nWhy not both?\n\n"}]}