NOTE: FIRST SEE THE UPDATE AT THE END FOR A NICER WAY TO DO THIS. USE AT YOUR OWN RISK, THIS WILL PROBABLY DELETE EVERYTHING ON YOUR SERVER AND EAT YOUR GRANDPARENTS!
There absolutely has to be better ways to do this, but with my weak bash-fu and lots of help from the good people on #git I stumbled through and got something working.
I am using gitosis to host a repo that is shared between a couple of people I trust. They have given me their keys and commit via ssh. I just followed the instructions on http://scie.nti.st/2007/11/14/hosting-git-repositories-the-easy-and-secure-way
I used yafc for the ftp part. It was my first time using it and I love it to bits. No more gui ftp clients for me, or plain old ftp either. The bookmark feature is awesome and really helped me out here.
- Login to your site via yafc anc save it as a bookmark
- Add the script below to your remote repo i.e. login to the server you are pushing to, if you set it up with gitosis you will have /home/git/repos/YourRepo and put it in hooks/post-receive.
- Finally make hooks/post-receive executable to enable it (chmod u+x post-receive)
CHANGES=`mktemp -q -t`
while read oldrev newrev refname
# This gets the paths of the files in the repo that have changed
# and shows if they are added A, deleted D etc.
echo `git diff-tree –name-status -r $oldrev $newrev` > “$CHANGES”
echo $CHANGES >> /home/git/YourRepo.log
UPLOADS=`cat “$CHANGES” | grep -v “^D” | cut -d” ” -f2`
# The sed expression here is to convert the path in the repo to the path on the server
DELETES=`cat “$CHANGES” | grep “^D” | cut -d” ” -f2 | sed -e ‘s/^drupal\/YourRepo.website\///g’`
echo `date` “\nUploading: $UPLOADS\nDeleting: $DELETES” >> /home/git/YourRepo-git-ftp.log
for U in $UPLOADS
# Again to change the repo path into the server filesystem path
FTPPATH=`echo “$U” | sed -e ‘s/^drupal\/YourRepo.website//g’`
# This dumps the files out of the repo on to the filesystem to be uploaded later
mkdir -p $FTPSTAGE`dirname $FTPPATH`
git cat-file blob $newrev:$U > $FTPSTAGE$FTPPATH
# Tell yafc to upload everything that has been dumped from the repo
# and delete everything that needs to be deleted
put -fr $FTPSTAGE/*
rm -r $DELETES
# Tidy up
rm -rf $FTPSTAGE/*
If you’re any good at bash you are probably screaming in disgust right now but if you can spare a minute to explain how to improve this I would be grateful.
Note: this is so lame it doesn’t delete directories that have been created when you delete them from git. It just deletes the files. That’s just because the git command to get the paths doesn’t show when you’ve deleted the directory higher up. It just about works for me now, even though it’s horrible.
UPDATE: Replace all that gubbins with something like the following procedure:
Clone the repo on the server and set up sitecopy to sync that with the live site. Then in the post-receive hook:
sitecopy -u name
if you don’t change GIT_DIR you’ll see fatal: Not a git repository: ‘.’ even though running the hook on it’s own will work fine.
Thanks to: http://firstname.lastname@example.org/msg03458.html