It's a bit of an embarrassment when you submit a review to Gerrit only to have it fail CI checks immediately because of something as simple as a syntax error or pep8 failure that you should have caught yourself before submitting...but you forgot to run your validations before submitting the change.

In many cases you can alleviate this through the use of the git pre-commit hook, which will run every time you commit changes locally. You can have the hook run tox or whatever tool your project uses for validation on every commit. This works okay for simple cases, but if the validation takes more than a couple of seconds the delay can be disruptive to the flow of your work.

What you want is something that stays out of the way while you are working locally, but that will prevent you from submitting something for review that's going to fail CI immediately. If you are using the git-review tool to interact with Gerrit (and if you're not, you should be), you're in luck! The git-review tool supports a pre-review hook that does exactly what we want. git-review looks for hooks in a global location (~/.config/git-review/hooks) and in a per-project location (in .git/hooks/). As with standard Git hooks, the pre-review hook must be executable (i.e., chmod u+x .git/hooks/pre-review).

The pre-review script will be run before attempting to submit your changes to Gerrit. If the script exits successfully, the output is hidden and your changes will be submitted normally. If the hook fails, you will see output along the lines of...

Custom script execution failed.
The following command failed with exit code 1
    ".git/hooks/pre-review"
-----------------------

...followed by the output of the pre-review script.

For my work on the tripleo-quickstart project, the contents of my .git/hooks/pre-review script is as simple as:

#!/bin/sh
tox

A collection of git tips

Fri 19 February 2016 by Lars Kellogg-Stedman Tags git

This is a small collection of simple git tips and tricks I use to make my life easier.

Quickly amend an existing commit with new files

I have this alias in place that will amend the current commit while automatically re-using the existing commit message:

alias.fix=commit --amend -C …
read more

Automatic git cache

Mon 19 October 2015 by Lars Kellogg-Stedman Tags git

This post is in response to a comment someone made on irc earlier today:

[I] would really like a git lookaside cache which operated on an upstream repo, but pulled objects locally when they're available

In this post I present a proof-of-concept solution to this request. Please note that thisand …

read more

Using tools badly: time shifting git commits with Workinghours

Fri 10 April 2015 by Lars Kellogg-Stedman Tags git hack

This is a terrible hack. If you are easily offended by bad ideas implemented poorly, move along!

You are working on a wonderful open source project...but you are not supposed to be working on that project! You're supposed to be doing your real work! Unfortunately, your extra-curricular activity is …

read more

Tracking down a kernel bug with git bisect

Mon 21 July 2014 by Lars Kellogg-Stedman Tags docker kernel git

After a recent upgrade of my Fedora 20 system to kernel 3.15.mumble, I started running into a problem (BZ 1121345) with my Docker containers. Operations such as su or runuser would fail with the singularly unhelpful System error message:

$ docker run -ti fedora /bin/bash
bash-4.2# su …
read more