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

偶尔善良 提交于 2019-12-11 01:29:30

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/1558036/git-post-update-hook-that-runs-a-script-that-needs-access-to-all-files-in-the-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!