开发者

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

开发者 https://www.devze.com 2022-12-08 22:06 出处:网络
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 th

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 fi开发者_JS百科les in the repository in the post-update hook.

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


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消