Git post-receive hook fails on hugo build step: not a git repository

Hugo fails to build website with error “not a git repository” when run from git post-receive hook. This is the output from git push to the remote host:

Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 8 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 1.12 KiB | 1.12 MiB/s, done.
Total 8 (delta 5), reused 0 (delta 0), pack-reused 0
remote: Cloning into ‘/home/dpirate/hugo/myhugoproject.com’…
remote: done.
remote: Start building sites …
remote: hugo v0.101.0+extended linux/amd64 BuildDate=unknown
remote: ERROR 2022/07/29 12:12:45 Failed to read Git log: fatal: not a git repository: ‘.’
remote: INFO 2022/07/29 12:12:45 syncing static files to /
remote: Error: Error building site: logged 1 error(s)
remote: Total in 52 ms
To ssh://host.myhugoproject.com/git/myhugoproject.com
1a30ca1…b86ecd6 master → master
branch ‘master’ set up to track ‘host:myhugoproject.com/master’.

The content of the post-receive script are:

#!/bin/bash                                                                      
                                                                                
set -e                                                                           
                                                                                 
PROJECT="myhugoproject.com"                                                        
GIT_REPO="/srv/git/${PROJECT}.git"                                               
WORKING_DIR="${HOME}/hugo"                                                                                                    
PUBLIC_WWW="/srv/http/${PROJECT}"                                                
                                                                               
/usr/bin/mkdir -p "${WORKING_DIR}"                                                                         
/usr/bin/git clone "${GIT_REPO}" "${WORKING_DIR}/${PROJECT}"                     
hugo -v -s "${WORKING_DIR}/${PROJECT}" -d "${PUBLIC_WWW}"                        
rm -rf "${WORKING_DIR}"

If I go from an interactive shell to ${WORKINGDIR}/${PROJECT} (which has not been deleted because bash bails early on errors here) and run hugo manually everything works fine. Running the post-receive script manually works fine too. So no permission problems, wrong paths, or obvious script problems. There is no firejail, SELinux and friends on the host. One thing that may be a problem is the account on the remote host is running fish as default shell, but I’m guessing it shouldn’t matter since there is the #!/bin/bash bang in the script. I’m pretty sure there must be a gotcha somewhere. Maybe a missing environment variable that must be set in bash ? But I can’t find it. Hopefully someone has already had this problem and can point me in the right direction.

I presume the error is triggered by enableGitInfo = true in your site configuration. Please confirm.

Yes definitely.

In the environment/context in which the Git post-receive hook runs, I believe that the GIT_DIR environment variable is set to “.”

I am unable to test this myself, but please edit your hook, placing this just before you run hugo:

unset GIT_DIR

I just tried with “unset GIT_DIR” just before starting Hugo and it does indeed fix the problem. Thanks for the help. It’s slightly strange though that this works as I had previously tried adding a pushd “${WORKINGDIR}/myhugoproject.com” just before starting hugo without “-s” and a corresponding “popd” after that.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.