views:

459

answers:

2

Hi everyone,

I'm running into a bit of dilemma at the moment in that I need a script to run whenever the remote repository is updated (ie, whenever someone runs git push) that builds packages from the files in the repository. These packages are then placed into a directory on the git server that's exposed to clients over HTTP for future use.

The problem is, I'm not sure how to access the files in the repository in the post-update hook.

If anyone can give some insight, it would be much appreciated.

+2  A: 

First of all, you might want to use the post-receive hook instead of post-update. According to the githooks(5) man page, post-receive supersedes post-update.

That said, your hook script is executed in the .git/hooks subdirectory, so if you do a simple

cd ..

your script is in the working tree of the git repository. For instance, here's a tiny script which makes sure that the working tree of the remote git repository is updated whenever you push to the repository:

#!/bin/sh
export GIT_DIR=
cd ..
echo "Resetting working tree..."
git reset --hard
echo "Finished resetting working tree."

Note that you need to un-set the GIT_DIR environment variable; it's automatically set, and as long as its set all git commands will be run in that directory - no matter where you cd'ed to.

Frerich Raabe
Or use GIT_WORK_TREE enviromental variable, or --work-tree option to git...
Jakub Narębski
A: 

If your remote repsitory is a bare shared repo, then there is no copy of the files. you can change this, and then you'll just have to run an auto checkout.

if your packaging hte files, best to have the repo in a seperate directory too

I use the following for the exact purpose you've named

here is the blog post that showed me how to set it up http://toroid.org/ams/git-website-howto

here are my abbrieviated notes below

make a directory outside the repo and put the working tree there, then make it no longer a bare repo so there is a copy of the files, then run a before you run your packaging script

            # create the repo with files that live in a seperate folder
            cd /share/proj/all/$1/repo
            git --bare init --shared
            git config core.worktree ../actual
            git config core.bare false
            git config receive.denycurrentbranch ignore
            # add a hook to checkout the repo into the files directory automatically on push
            echo "#!/bin/sh" > hooks/post-receive
            echo "git checkout -f" >> hooks/post-receive
            chmod +x hooks/post-receive
Fire Crow