Tip: Show the last git commit in the site footer

Here's a quick tip for anyone using Git to version control their Drupal sites. When looking at a development or QA instance of a site, it's useful to be able to see at a glance what the last commit was. With a touch of settings.php code, we can add this information to our site footer.

// Add the current git revision and date to the site footer. $conf['site_footer'] = '<p>©2010 All Rights Reserved<br /><em>'; $conf['site_footer'] .= shell_exec("git log -1 --pretty=format:'%h - %s (%ci)' --abbrev-commit"); $conf['site_footer'] .= '</em></p>';

This code gives us the following footer:

Site footer showing last git commit

For some of our sites, we also have local branches on development servers that contain specific code for that server. This contains updates to robots.txt or .htaccess to restrict access to the site. Using a local branch breaks the above code, as it will always show the last local commit instead of the last commit that will eventually reach production. Combining git merge-base and the backtick operator allows us to show the proper commit message.

  
// Add the current git revision and date to the site footer.
$conf['site_footer'] = '<p>©2010 All Rights Reserved<br /><em>';
$conf['site_footer'] .= shell_exec("git log -1 --pretty=format:'%h - %s (%ci)' --abbrev-commit `git merge-base local-dev dev`");
$conf['site_footer'] .= '</em></p>';  

What other settings.php hacks do you find useful? Share them with us in the comments below!

Get in touch with us

Tell us about your project or drop us a line. We'd love to hear from you!