Wednesday, 19 October 2011

Rapid Moodle development using Git

Recently I've been doing a lot of Moodle development, and every time I start a feature, or work on a bug, I've been creating a new branch. I often also create a new database, and a fresh install just to make sure that there's nothing fruity going on. All this has meant that I've had branches and databases coming out of my ears.

Since I've just taken possession of a new desktop for work, I've taken the opportunity to start afresh with my Moodle branches and I'm trying to be more organised in my branch creation. To that end, I've got the following system going:

  • Generic moodle bug: MDL-<bug number>-<revision>
  • Version for master: MDL-<bug number>-master-<revision>
  • Version for 2.0: MDL-<bug number>-MOODLE_20_STABLE-<revision>
  • Version for 2.1: MDL-<bug number>-MOODLE_21_STABLE-<revision>
This allows me to start work on a bug, and have relevant revisions to my patches in a sane and reasonably sensible (if not a touch long) fashion.

To make life simpler still, I've added to my moodle/config.php. This selects my database (and optionally database username which is sometimes handy) based on my branch name.


$branch = exec("git branch --no-color | grep '^* '| sed 's/^* //'");
$dbuser = 'moodle';

// First check for generic branch parents
if (preg_match('/master/', $branch)) {
    $newbranch = 'master';
} else if (preg_match('/MOODLE_20_STABLE/', $branch)) {
    $newbranch = 'MOODLE_20_STABLE';
} else if (preg_match('/MOODLE_21_STABLE/', $branch)) {
    $newbranch = 'MOODLE_21_STABLE';
} else if (preg_match('/MDL-/', $branch)) {
    // Any remaining MDL- matches which don't specify a branch will be
    // assumed to be on master
    $newbranch = 'MOODLE_21_STABLE';

// And now more specific parents
switch ($branch) {
    case 'example':
        $dbuser = 'some-other-dbuser';
        $newbranch = 'master';

$branch = $newbranch;


$CFG->dbname = 'moodle-' . $branch;
$CFG->dbuser = $dbuser;

I guess I'll see how it goes, but so far it's working well and I intend to replicate this with Mahara too.