{"thread":{"id":"60555","subject":"Running git(1) from within hooks/post-update","startedAt":"2023-11-24T14:00:26Z","lastAt":"2023-11-24T15:49:08Z","messageCount":3,"participants":["Alejandro Colomar"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"485123","messageId":"ZWCsd3cJZ3LAqOwg@debian","threadId":"60555","inReplyTo":null,"subject":"Running git(1) from within hooks/post-update","fromName":"Alejandro Colomar","fromEmail":"alx@kernel.org","sentAt":"2023-11-24T14:00:22Z","receivedAt":"2023-11-24T14:00:26Z","isPatch":false,"sender":{"key":"alx@kernel.org","avatar":null},"body":"Hi,\n\nI'm trying to set up a post-update hook to produce a build of the Linux\nman-pages PDF book after every push to my personal server to the 'main'\nbranch, so that I can serve man-pages-HEAD.pdf at some URL, for users to\nbe able to easily check the manual at git HEAD without having to clone\nthe repo.\n\nI thought of having a git non-bare repo where I build it, so the script\nwould be the following (the paths are tmp, because I'm still testing).\n\n\t$ cat post-update \n\t#!/bin/sh\n\n\ttest \"$1\" = \"refs/heads/main\" || exit 0;\n\n\tcd ~/tmp/man-pages/;\n\n\twhoami; pwd; ls -ld .git/;  # This is for debugging.\n\n\tgit fetch srv\t\t\t#>/dev/null 2>&1;\n\tgit reset srv/main --hard\t#>/dev/null 2>&1;\n\tgit clean -dffx\t\t\t#>/dev/null 2>&1;\n\tscripts/LinuxManBook/build.sh\t>~/tmp/LMB-HEAD.pdf &\n\nBut it's not working.  The git(1) calls are failing, saying it's not a\ngit repo, but it clearly is, and I have permissions, so I don't\nunderstand what's going wrong.  Here's the 'remote:' output of a push to\nthat testing server:\n\n\tremote: alx\n\tremote: /home/alx/tmp/man-pages\n\tremote: drwxr-xr-x 8 alx alx 4096 Nov 24 14:41 .git/\n\tremote: fatal: not a git repository: '.'\n\tremote: fatal: not a git repository: '.'\n\tremote: fatal: not a git repository: '.'\n\tremote: hooks/post-update: 12: ./scripts/LinuxManBook/build.sh: not found\n\nCan you please help?  :)\n\nThanks,\nAlex\n\n-- \n<https://www.alejandro-colomar.es/>\n"},{"id":"485129","messageId":"ZWDEWDTtiFYAYp2P@debian","threadId":"60555","inReplyTo":"ZWCsd3cJZ3LAqOwg@debian","subject":"Re: Running git(1) from within hooks/post-update","fromName":"Alejandro Colomar","fromEmail":"alx@kernel.org","sentAt":"2023-11-24T15:42:16Z","receivedAt":"2023-11-24T15:42:19Z","isPatch":false,"sender":{"key":"alx@kernel.org","avatar":null},"body":"On Fri, Nov 24, 2023 at 03:00:22PM +0100, Alejandro Colomar wrote:\n> Hi,\n> \n> I'm trying to set up a post-update hook to produce a build of the Linux\n> man-pages PDF book after every push to my personal server to the 'main'\n> branch, so that I can serve man-pages-HEAD.pdf at some URL, for users to\n> be able to easily check the manual at git HEAD without having to clone\n> the repo.\n> \n> I thought of having a git non-bare repo where I build it, so the script\n> would be the following (the paths are tmp, because I'm still testing).\n> \n> \t$ cat post-update \n> \t#!/bin/sh\n> \n> \ttest \"$1\" = \"refs/heads/main\" || exit 0;\n> \n> \tcd ~/tmp/man-pages/;\n> \n> \twhoami; pwd; ls -ld .git/;  # This is for debugging.\n> \n> \tgit fetch srv\t\t\t#>/dev/null 2>&1;\n> \tgit reset srv/main --hard\t#>/dev/null 2>&1;\n> \tgit clean -dffx\t\t\t#>/dev/null 2>&1;\n> \tscripts/LinuxManBook/build.sh\t>~/tmp/LMB-HEAD.pdf &\n\nThe script works fine when called manually.  It seems it's calling it\nas a hook that fails.  It seems it's running git(1) from within a\npost-update hook that is problematic.  Is that expected, or is it a bug,\nand can it be fixed?\n\n> \n> But it's not working.  The git(1) calls are failing, saying it's not a\n> git repo, but it clearly is, and I have permissions, so I don't\n> understand what's going wrong.  Here's the 'remote:' output of a push to\n> that testing server:\n> \n> \tremote: alx\n> \tremote: /home/alx/tmp/man-pages\n> \tremote: drwxr-xr-x 8 alx alx 4096 Nov 24 14:41 .git/\n> \tremote: fatal: not a git repository: '.'\n> \tremote: fatal: not a git repository: '.'\n> \tremote: fatal: not a git repository: '.'\n> \tremote: hooks/post-update: 12: ./scripts/LinuxManBook/build.sh: not found\n> \n> Can you please help?  :)\n> \n> Thanks,\n> Alex\n> \n> -- \n> <https://www.alejandro-colomar.es/>\n\n\n\n-- \n<https://www.alejandro-colomar.es/>\n"},{"id":"485130","messageId":"ZWDF8TBw_YM3UC6N@debian","threadId":"60555","inReplyTo":"ZWDEWDTtiFYAYp2P@debian","subject":"Re: Running git(1) from within hooks/post-update","fromName":"Alejandro Colomar","fromEmail":"alx@kernel.org","sentAt":"2023-11-24T15:49:05Z","receivedAt":"2023-11-24T15:49:08Z","isPatch":false,"sender":{"key":"alx@kernel.org","avatar":null},"body":"On Fri, Nov 24, 2023 at 04:42:16PM +0100, Alejandro Colomar wrote:\n> On Fri, Nov 24, 2023 at 03:00:22PM +0100, Alejandro Colomar wrote:\n> > Hi,\n> > \n> > I'm trying to set up a post-update hook to produce a build of the Linux\n> > man-pages PDF book after every push to my personal server to the 'main'\n> > branch, so that I can serve man-pages-HEAD.pdf at some URL, for users to\n> > be able to easily check the manual at git HEAD without having to clone\n> > the repo.\n> > \n> > I thought of having a git non-bare repo where I build it, so the script\n> > would be the following (the paths are tmp, because I'm still testing).\n> > \n> > \t$ cat post-update \n> > \t#!/bin/sh\n> > \n> > \ttest \"$1\" = \"refs/heads/main\" || exit 0;\n> > \n> > \tcd ~/tmp/man-pages/;\n> > \n> > \twhoami; pwd; ls -ld .git/;  # This is for debugging.\n> > \n> > \tgit fetch srv\t\t\t#>/dev/null 2>&1;\n> > \tgit reset srv/main --hard\t#>/dev/null 2>&1;\n> > \tgit clean -dffx\t\t\t#>/dev/null 2>&1;\n> > \tscripts/LinuxManBook/build.sh\t>~/tmp/LMB-HEAD.pdf &\n> \n> The script works fine when called manually.  It seems it's calling it\n> as a hook that fails.  It seems it's running git(1) from within a\n> post-update hook that is problematic.  Is that expected, or is it a bug,\n> and can it be fixed?\n\nAhh, after this I've found it documented in githooks(5):\n\n     Environment variables, such as GIT_DIR, GIT_WORK_TREE, etc., are\n     exported so that Git commands run by the hook can correctly\n     locate the repository. If your hook needs to invoke Git commands\n     in a foreign repository or in a different working tree of the\n     same repository, then it should clear these environment variables\n     so they do not interfere with Git operations at the foreign\n     location. For example:\n\n         local_desc=$(git describe)\n         foreign_desc=$(unset $(git rev-parse --local-env-vars); git -C ../foreign-repo describe)\n\nNever mind.\n\nThanks,\nAlex\n\n> \n> > \n> > But it's not working.  The git(1) calls are failing, saying it's not a\n> > git repo, but it clearly is, and I have permissions, so I don't\n> > understand what's going wrong.  Here's the 'remote:' output of a push to\n> > that testing server:\n> > \n> > \tremote: alx\n> > \tremote: /home/alx/tmp/man-pages\n> > \tremote: drwxr-xr-x 8 alx alx 4096 Nov 24 14:41 .git/\n> > \tremote: fatal: not a git repository: '.'\n> > \tremote: fatal: not a git repository: '.'\n> > \tremote: fatal: not a git repository: '.'\n> > \tremote: hooks/post-update: 12: ./scripts/LinuxManBook/build.sh: not found\n> > \n> > Can you please help?  :)\n> > \n> > Thanks,\n> > Alex\n> > \n> > -- \n> > <https://www.alejandro-colomar.es/>\n> \n> \n> \n> -- \n> <https://www.alejandro-colomar.es/>\n\n\n\n-- \n<https://www.alejandro-colomar.es/>\n"}]}