[Pelican] Don't Let Me Publish Without Committing

Posted on in Programming

cover image for article

Although I use Git all day long, I actually prefer to use Mercurial for my personal stuff. When I started working on this project to maintain slaptijack.com with Pelican rather than continuing to use Wordpress, one of the things that really interested me was the ability to keep Markdown files in a Mercurial repository hosted at Bitbucket. In fact, although I have the repository marked private right now, one of my goals is to eventually open-source this project, too.

All of this is to say that when I go to publish the site via Fabric (fab publish), I don't want to publish anything that has not been checked into the repository. Although it's just me working on the site, there's a good chance that I might be using a different computer at any given time. The solution to my problem is for publish() to ensure everything is checked in before pushing to the site.

I solved this problem by creating a new function in fabfile.py called hg_check(). This function runs hg status and barfs if there is any output. Since there is only output when something needs to be committed or added and committed, this suits the bill. It's worth noting that barfing on files that need to be added is just as important as files that merely need to be committed. Here's a diff of the changes I made to fabfile.py and the (non-colorized) output you see when hg_check() notices you have uncommitted files.

diff -r bb8be79ea627 fabfile.py
--- a/fabfile.py Fri Oct 23 17:15:01 2015 -0500
+++ b/fabfile.py Fri Oct 23 17:31:57 2015 -0500
@@ -1,4 +1,6 @@
 from fabric.api import *
+from fabric.colors import *
 import fabric.contrib.project as project
 import os
 import shutil
@@ -36,6 +38,16 @@
     """Build local version of site"""
     local('pelican -s pelicanconf.py')

+def hg_check():
+    """Make sure everything has been checked into the mercurial repo
+    before proceeding."""
+    if local("hg status", capture=True):
+        print red("*** WARNING ***")
+        local("hg status")
+        print red("Commit all your changes before publishing to production.")
+        print red("*** WARNING ***")
+        quit()
 def rebuild():
     """`clean` then `build`"""
@@ -78,6 +90,7 @@
 def publish():
     """Publish to production via rsync"""
+    hg_check()
     local('pelican -s publishconf.py')

hg_check() uses fabric.colors to print big red warnings. Danger, Will Robinson!

Anyway, best of luck!

As a brief aside, I used Christophe Marois' awesome hack to float the Fabric logo to the right. Now, there's nothing more than [![Fabric logo -right][1]][2] in my Markdown source for this article. Thanks, Christophe!

My Bookshelf

Reading Now

Other Stuff