1

I have set up a git repository for my webpage (it's simple HTML, generated from markdown by Pandoc). The repository will only be updated remotely by me, and I have successfully configured it to work with HTTPS.

Now, This is not a bare repository -- nobody will update the repository or the working tree locally on the server, so I just keep a working copy there, and make it available for the web server to publish.

It all works fine, but -- I need the working tree to be automatically updated every time I push changes to the server, so I have written the following post-update script (in the example, the working tree is in /path/git/me, and the repository in /path/git/me/.git):

#!/bin/bash

(cd /path/git/me/ && echo "running git reset --hard on `pwd`" && git reset --hard)

(cd /path/git/me/ && pandoc -s -o index.html index.md)

And when I run git push, the hook is executed, but something really strange happens:

  • git complains that '.' is not a repository, and does not run the git reset --hard command
  • however, pandoc does run without problems!

Below is the transcript of a push:

$ GIT_TRACE=1 git push
12:21:12:21:27.088581 run-command.c:347       trace: run_command: 'git-remote-https' 'origin' 'https://server.org/git/me'
Username for 'https://server.org': me
Password for 'https://me@server.org': 
12:21:32.624468 run-command.c:347       trace: run_command: 'send-pack' '--stateless-rpc' '--helper-status' '--thin' '--progress' 'https://server.org/git/me/' '--stdin'
12:21:32.627833 exec_cmd.c:128          trace: exec: 'git' 'send-pack' '--stateless-rpc' '--helper-status' '--thin' '--progress' 'https://server.org/git/me/' '--stdin'
12:21:32.633026 git.c:348               trace: built-in: git 'send-pack' '--stateless-rpc' '--helper-status' '--thin' '--progress' 'https://server.org/git/me/' '--stdin'
12:21:32.634024 run-command.c:347       trace: run_command: 'pack-objects' '--all-progress-implied' '--revs' '--stdout' '--thin' '--delta-base-offset' '--progress'
12:21:32.634538 exec_cmd.c:128          trace: exec: 'git' 'pack-objects' '--all-progress-implied' '--revs' '--stdout' '--thin' '--delta-base-offset' '--progress'
12:21:32.636701 git.c:348               trace: built-in: git 'pack-objects' '--all-progress-implied' '--revs' '--stdout' '--thin' '--delta-base-offset' '--progress'
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 223 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: running git reset --hard on /path/git/me
remote: fatal: Not a git repository: '.'
To https://server.org/git/me
   93be2f2..9cc7a28  master -> master

What did I do wrong here, and how would I get the hook to run the commands as intended?

Jay
  • 153
  • 1
  • 7
  • 1
    Why do you need git reset --hard? whats the status of repository before git reset --hard? Try to add HEAD after --hard, so you command will look like: "git reset --hard HEAD" – Navern Aug 24 '15 at 16:23
  • Navern: I have tried adding `HEAD` but it didn't help... I run `git reset --hard` so the working tree will be a checkout of the latest version. – Jay Aug 24 '15 at 16:27

1 Answers1

3

I have found the problem!

During execution of the hook, the variables GIT_DIR and GIT_WORK_TREE are set, and git will ignore the current directory. And it seems that I can pass the directories explicitly to git:

git --git-dir /path/git/me/.git --work-tree /path/git/me/ reset --hard

Everything works now!

Jay
  • 153
  • 1
  • 7
  • 1
    Wow really? You helped me a lot. I face exactly the same problem. In fact triggering the post-receive hook via the server GUI went OK. But when I dit a `git push` locally git indeed complains about '.' is not a git directory: fatal: Not a git repository: '.'. You could also try to unset the variables. unset GIT_DIR. See: http://stackoverflow.com/questions/9905882/git-post-receive-not-working-correctly – Melroy van den Berg Oct 28 '16 at 14:59