I created a bare git repo on my server and set up the following post-receive hook from this blog:
#!/bin/bash
while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
if [ "master" == "$branch" ]; then
git --work-tree=/path/to/my/project/live/ checkout -f $branch
echo 'Changes pushed live.'
fi
if [ "develop" == "$branch" ]; then
git --work-tree=/path/to/my/project/dev/ checkout -f $branch
echo 'Changes pushed to dev.'
fi
done
So that whenever I push locally to my server, the changes would automatically be published on each branch's folder without the need to manually pull.
I set the right permissions to both live an dev folder:
drwxrwsr-x 2 git git 4096 Sep 29 12:10 live/
drwxrwsr-x 2 git git 4096 Sep 29 12:09 dev/
And pushing from the develop branch works as expected. The problem occurs when I checkout the master branch and do a merge. When I push the master, the new files get copied to the live folder on my server, but the files I removed locally are not being deleted.
How can I make post-receive properly update the live folder? Thanks!
I ran into the same issue. Was looking into rsync to copy tmp folder files to the live folder but then I figured why not just use git glean on the working tree.
I'm not sure if this would be bad but it should only remove untracked files from the folder and use your .gitignore settings to not remove files not in your repo.
It seems to accomplish what I want which is to clear out left over files that were not deleted by the push.
The problem is that git doesn't know what to remove (it does not have an index over in the work tree, keeping track of such things). It should be possible to solve this with an index for each work tree, but I think it's simpler to just
git checkout -f
into a new, empty directory, then rename the new directory and the old one to make the new version "go live". This also shrinks the race condition window: now there's just one brief moment (betweenmv
operations) when there is no version, instead of a slightly longer window (duringcheckout
) when there is a mix of old and new versions.Note: The script you show will go awry if there is a tag named
master
ordevelop
as the reference names for those two arerefs/tags/master
andrefs/tags/develop
respectively. I'd recommend fixing this (if you care :-) ) via shell function andcase
statements to cut down on process spawning in the non-deploy cases, e.g.:(note: totally untested).