Saturday, February 4, 2012

A Nifty Little Script

Here's a nifty little script I wrote for a specific application, but you may find it useful as well.

We use Git for version control. If you're a programmer and you're not familiar with Git, you should check it out. Pro Git is a good resource. Our setup is what I would call a hybrid between centralized (SVN-style) and decentralized version control. Every developer has their own repository on their user account, but we all push and pull to a central repository. It's very similar to the GitHub model of using Git for collaboration. Every developer has their own live version of the site, which makes it a lot easier for everyone to develop and test their own code. There's also a test version of the site, which needs to be updated with the current code. I could do that with git hooks, but I've been using this script running in a daily cron job. It's sort of our equivalent of a nightly build. The script fetches the changes from the central (origin) repository, merges them into the test site, and emails me the list of commit messages, which I then use to inform our data entry/test personnel what the changes were.

#!/bin/sh
cd /home/alpha/www
git fetch origin
if [ -n "$(git log HEAD..origin)" ]
then
 git log HEAD..origin | mail -s "Git Merge to Alpha on $(date)" ME@EMAIL.COM
fi
git merge origin
exit

I should note that

git fetch origin
git merge origin

is exactly equivalent to git pull origin. A pull is just a fetch followed by a merge. I split the two so I could read the log and email it to myself. Some people prefer to split the two so they can check what's being fetched before they merge it.

No comments:

Post a Comment