Running gofmt as a bzr hook

Michael Terry michael.terry at
Fri May 15 19:37:50 UTC 2015

I use an unenlightened editor that doesn't run gofmt upon saving a file.

So to avoid rookie mistakes, I wrote this tiny bzr hook to run gofmt during
a bzr commit (only for snappy branches).  If you like it too, save it as

from bzrlib import hooks

def run_gofmt(local, master, old_revno, old_revid, future_revno,
future_revid, tree_delta, future_tree):
    import os, subprocess
    from bzrlib import errors, workingtree
    basedir =
    if os.path.exists(basedir + "/cmd/snappy/main.go") and
subprocess.check_output(["gofmt", "-l", basedir]):
        raise errors.BzrError("Run gofmt, you have style errors.")

hooks.install_lazy_named_hook('bzrlib.branch', 'Branch.hooks',
    'pre_commit', run_gofmt, 'Run gofmt on snappy branches')

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the snappy-devel mailing list