Search code examples
githookpost-update

Git: Post-update hook that runs a script that needs access to all files in the repository


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.


Solution

  • 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.